Unable to understand a piece of code
HI, mm/slub.c: line 3973 int __kmem_cache_shrink(struct kmem_cache *s) { int node; int i; struct kmem_cache_node *n; struct page *page; struct page *t; struct list_head discard; struct list_head promote[SHRINK_PROMOTE_MAX]; unsigned long flags; int ret = 0; flush_all(s); for_each_kmem_cache_node(s, node, n) { How uninitialized variable node is being used in macro for_each_kmem_cache_node? node is a local variable with no extern and not initialized. mm/slab.h: line 490 #define for_each_kmem_cache_node(__s, __node, __n) \ for (__node = 0; __node < nr_node_ids; __node++) \ if ((__n = get_node(__s, __node))) As we see for loop is based on node. Thanks in advance. Regards, Amit Kumar
On Mon, May 20, 2019 at 05:37:41AM +0530, Amit Kumar wrote:
HI,
mm/slub.c: line 3973 int __kmem_cache_shrink(struct kmem_cache *s) { int node; int i; struct kmem_cache_node *n; struct page *page; struct page *t; struct list_head discard; struct list_head promote[SHRINK_PROMOTE_MAX]; unsigned long flags; int ret = 0;
flush_all(s); for_each_kmem_cache_node(s, node, n) {
How uninitialized variable node is being used in macro for_each_kmem_cache_node?
node is a local variable with no extern and not initialized.
mm/slab.h: line 490 #define for_each_kmem_cache_node(__s, __node, __n) \ for (__node = 0; __node < nr_node_ids; __node++) \
This _is_ the initialization of node. Macros are kinda funky, there isn't much more to say about that other than perhaps read up some more about how macros work in C and read a bunch more macros in the kernel e.g. include/linux/list.h Good luck, Tobin.
On Mon, 20 May 2019, 6:54 am Tobin C. Harding, <me@tobin.cc> wrote:
On Mon, May 20, 2019 at 05:37:41AM +0530, Amit Kumar wrote:
HI,
mm/slub.c: line 3973 int __kmem_cache_shrink(struct kmem_cache *s) { int node; int i; struct kmem_cache_node *n; struct page *page; struct page *t; struct list_head discard; struct list_head promote[SHRINK_PROMOTE_MAX]; unsigned long flags; int ret = 0;
flush_all(s); for_each_kmem_cache_node(s, node, n) {
How uninitialized variable node is being used in macro for_each_kmem_cache_node?
node is a local variable with no extern and not initialized.
mm/slab.h: line 490 #define for_each_kmem_cache_node(__s, __node, __n) \ for (__node = 0; __node < nr_node_ids; __node++) \
This _is_ the initialization of node.
First thank you for your reply. I just did not use my brain that node is initialized using __node inside for loop. I know well macros are literally replaced and do not exist anymore after preprocessing. Macros are kinda funky, there
isn't much more to say about that other than perhaps read up some more about how macros work in C and read a bunch more macros in the kernel e.g. include/linux/list.h
Good luck, Tobin.
On Mon, May 20, 2019 at 05:37:41AM +0530, Amit Kumar wrote:
HI,
mm/slub.c: line 3973 int __kmem_cache_shrink(struct kmem_cache *s) { int node; int i; struct kmem_cache_node *n; struct page *page; struct page *t; struct list_head discard; struct list_head promote[SHRINK_PROMOTE_MAX]; unsigned long flags; int ret = 0;
flush_all(s); for_each_kmem_cache_node(s, node, n) {
How uninitialized variable node is being used in macro for_each_kmem_cache_node?
node is a local variable with no extern and not initialized.
mm/slab.h: line 490 #define for_each_kmem_cache_node(__s, __node, __n) \ for (__node = 0; __node < nr_node_ids; __node++) \ if ((__n = get_node(__s, __node)))
As we see for loop is based on node.
Hi, This sentence is almost correct. The for loop is *not based* on node but *uses* the node variable. If you check, __node gets initialised to 0 at the beginning of the for loop. So whatever you use that macro with will be initialised to 0 at the beginning of the for loop. Bests, Máté
Thanks in advance.
Regards, Amit Kumar
participants (3)
-
Amit Kumar -
Máté Eckl -
Tobin C. Harding