Kernel Newbies archive on lore.kernel.org
 help / Atom feed
* Unable to understand a piece of code
@ 2019-05-20  0:07 Amit Kumar
  2019-05-20  1:23 ` Tobin C. Harding
  2019-05-22 17:57 ` Máté Eckl
  0 siblings, 2 replies; 4+ messages in thread
From: Amit Kumar @ 2019-05-20  0:07 UTC (permalink / raw)
  To: linux-newbie, Kernel Newbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Unable to understand a piece of code
  2019-05-20  0:07 Unable to understand a piece of code Amit Kumar
@ 2019-05-20  1:23 ` Tobin C. Harding
  2019-05-20  3:33   ` Amit Kumar
  2019-05-22 17:57 ` Máté Eckl
  1 sibling, 1 reply; 4+ messages in thread
From: Tobin C. Harding @ 2019-05-20  1:23 UTC (permalink / raw)
  To: Amit Kumar; +Cc: linux-newbie, Kernel Newbies

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.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Unable to understand a piece of code
  2019-05-20  1:23 ` Tobin C. Harding
@ 2019-05-20  3:33   ` Amit Kumar
  0 siblings, 0 replies; 4+ messages in thread
From: Amit Kumar @ 2019-05-20  3:33 UTC (permalink / raw)
  To: Tobin C. Harding; +Cc: linux-newbie, Kernel Newbies

[-- Attachment #1.1: Type: text/plain, Size: 1286 bytes --]

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.
>

[-- Attachment #1.2: Type: text/html, Size: 1966 bytes --]

<div dir="auto"><div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 20 May 2019, 6:54 am Tobin C. Harding, &lt;me@tobin.cc&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, May 20, 2019 at 05:37:41AM +0530, Amit Kumar wrote:<br>
&gt; HI,<br>
&gt; <br>
&gt; mm/slub.c: line 3973<br>
&gt; int __kmem_cache_shrink(struct kmem_cache *s)<br>
&gt; {<br>
&gt; int node;<br>
&gt; int i;<br>
&gt; struct kmem_cache_node *n;<br>
&gt; struct page *page;<br>
&gt; struct page *t;<br>
&gt; struct list_head discard;<br>
&gt; struct list_head promote[SHRINK_PROMOTE_MAX];<br>
&gt; unsigned long flags;<br>
&gt; int ret = 0;<br>
&gt; <br>
&gt; flush_all(s);<br>
&gt; for_each_kmem_cache_node(s, node, n) {<br>
&gt; <br>
&gt; How uninitialized variable node is being used in macro for_each_kmem_cache_node?<br>
&gt;<br>
&gt; node is a local variable with no extern and not initialized.<br>
&gt; <br>
&gt; mm/slab.h: line 490<br>
&gt; #define for_each_kmem_cache_node(__s, __node, __n) \<br>
&gt; for (__node = 0; __node &lt; nr_node_ids; __node++) \<br>
<br>
This _is_ the initialization of node.  </blockquote></div></div><div dir="auto">First thank you for your reply.</div><div dir="auto">I just did not use my brain that node is initialized using __node inside for loop.</div><div dir="auto">I know well macros are literally replaced and do not exist anymore after preprocessing.</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Macros are kinda funky, there<br>
isn&#39;t much more to say about that other than perhaps read up some more<br>
about how macros work in C and read a bunch more macros in the kernel<br>
e.g. include/linux/list.h<br>
<br>
Good luck,<br>
Tobin.<br>
</blockquote></div></div></div>

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Unable to understand a piece of code
  2019-05-20  0:07 Unable to understand a piece of code Amit Kumar
  2019-05-20  1:23 ` Tobin C. Harding
@ 2019-05-22 17:57 ` Máté Eckl
  1 sibling, 0 replies; 4+ messages in thread
From: Máté Eckl @ 2019-05-22 17:57 UTC (permalink / raw)
  To: Amit Kumar; +Cc: linux-newbie, Kernel Newbies

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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-20  0:07 Unable to understand a piece of code Amit Kumar
2019-05-20  1:23 ` Tobin C. Harding
2019-05-20  3:33   ` Amit Kumar
2019-05-22 17:57 ` Máté Eckl

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org kernelnewbies@archiver.kernel.org
	public-inbox-index kernelnewbies


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/ public-inbox