Skip to contentSkip to conversation

break out page allocation warning code

PATCH In review Dave Hansen 5 participants 33 messages Apr 15, 2011 – Apr 29, 2011

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 break out page allocation warning code

This originally started as a simple patch to give vmalloc() some more verbose output on failure on top of the plain page allocator messages. Johannes suggested that it might be nicer to lead with the vmalloc() info _before_ the page allocator messages.

But, I do think there's a lot of value in what __alloc_pages_slowpath() does with its filtering and so forth.

This patch creates a new function which other allocators can call instead of relying on the internal page allocator warnings. It also gives this function private rate-limiting which separates it from other printk_ratelimit() users.

---

 linux-2.6.git-dave/include/linux/mm.h |    2 +
 linux-2.6.git-dave/mm/page_alloc.c    |   63 ++++++++++++++++++++++------------
 2 files changed, 44 insertions(+), 21 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~break-out-alloc-failure-messages +2 -0
diff -puN include/linux/mm.h~break-out-alloc-failure-messages include/linux/mm.h
@@ -1365,6 +1365,8 @@ extern void si_meminfo(struct sysinfo * 
 extern void si_meminfo_node(struct sysinfo *val, int nid);
 extern int after_bootmem;
 
+extern void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...);
+
 extern void setup_per_cpu_pageset(void);
 
 extern void zone_pcp_update(struct zone *zone);
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +42 -21

81 lines not on this page · raw message

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

PATCH 2/2 print vmalloc() state after allocation failures

I was tracking down a page allocation failure that ended up in vmalloc(). Since vmalloc() uses 0-order pages, if somebody asks for an insane amount of memory, we'll still get a warning with "order:0" in it. That's not very useful.

During recovery, vmalloc() also nicely frees all of the memory that it got up to the point of the failure. That is wonderful, but it also quickly hides any issues. We have a much different sitation if vmalloc() repeatedly fails 10GB in to:

	vmalloc(100 * 1<<30);

versus repeatedly failing 4096 bytes in to a:

	vmalloc(8192);

This patch will print out messages that look like this:

[   68.123503] vmalloc: allocation failure, allocated 6680576 of 13426688 bytes
[   68.124218] bash: page allocation failure: order:0, mode:0xd2
[   68.124811] Pid: 3770, comm: bash Not tainted 2.6.39-rc3-00082-g85f2e68-dirty #333
[   68.125579] Call Trace:
[   68.125853]  [<ffffffff810f6da6>] warn_alloc_failed+0x146/0x170
[   68.126464]  [<ffffffff8107e05c>] ? printk+0x6c/0x70
[   68.126791]  [<ffffffff8112b5d4>] ? alloc_pages_current+0x94/0xe0
[   68.127661]  [<ffffffff8111ed37>] __vmalloc_node_range+0x237/0x290

...

The 'order' variable is added for clarity when calling warn_alloc_failed() to avoid having an unexplained '0' as an argument. The 'tmp_mask' is there to keep the alloc_pages_node() looking sane. Adding __GFP_NOWARN is done because we now have our own, full error message in vmalloc code.

As a side issue, I also noticed that ctl_ioctl() does vmalloc() based solely on an unverified value passed in from userspace. Granted, it's under CAP_SYS_ADMIN, but it still frightens me a bit.

---

 linux-2.6.git-dave/mm/vmalloc.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +7 -2
diff -puN mm/vmalloc.c~vmalloc-warn mm/vmalloc.c
@@ -1534,6 +1534,7 @@ static void *__vmalloc_node(unsigned lon
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, int node, void *caller)
 {
+	int order = 0;
 	struct page **pages;
 	unsigned int nr_pages, array_size, i;
 	gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
@@ -1560,11 +1561,12 @@ static void *__vmalloc_area_node(struct 
 
 	for (i = 0; i < area->nr_pages; i++) {
 		struct page *page;
+		gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
 
 		if (node < 0)
-			page = alloc_page(gfp_mask);
+			page = alloc_page(tmp_mask);
 		else
-			page = alloc_pages_node(node, gfp_mask, 0);
+			page = alloc_pages_node(node, tmp_mask, order);
 
 		if (unlikely(!page)) {
 			/* Successfully allocated i pages, free them in __vunmap() */
@@ -1579,6 +1581,9 @@ static void *__vmalloc_area_node(struct 
 	return area->addr;
 
 fail:
+	warn_alloc_failed(gfp_mask, order, "vmalloc: allocation failure, "
+			  "allocated %ld of %ld bytes\n",
+			  (area->nr_pages*PAGE_SIZE), area->size);
 	vfree(area->addr);
 	return NULL;
 }

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw

Re: [PATCH 2/2] print vmalloc() state after allocation failures

mm/vmalloc.c static void *__vmalloc_node(unsigned lon · line 1537

Quoted 9 lines · Dave Hansen

diff -puN mm/vmalloc.c~vmalloc-warn mm/vmalloc.c
--- linux-2.6.git/mm/vmalloc.c~vmalloc-warn 2011-04-15
08:49:06.823306620 -0700
+++ linux-2.6.git-dave/mm/vmalloc.c 2011-04-15 09:20:17.926460283 -0700

mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +1 -0
@@ -1534,6 +1534,7 @@ static void *__vmalloc_node(unsigned lon
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, int node, void *caller)
 {
+	int order = 0;

Could we make that const?

mm/vmalloc.c static void *__vmalloc_area_node(struct · line 1567

Quoted 13 lines
mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +3 -2
 	struct page **pages;
 	unsigned int nr_pages, array_size, i;
 	gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
@@ -1560,11 +1561,12 @@ static void *__vmalloc_area_node(struct
 	for (i = 0; i < area->nr_pages; i++) {
 		struct page *page;
+		gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
 		if (node < 0)
-			page = alloc_page(gfp_mask);
+			page = alloc_page(tmp_mask);
 		else
-			page = alloc_pages_node(node, gfp_mask, 0);
+			page = alloc_pages_node(node, tmp_mask, order);

so it'll be more visible that we are passing 0 here.

Quoted 19 lines
mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +0 -0
 		if (unlikely(!page)) {
 			/* Successfully allocated i pages, free them in __vunmap() */
@@ -1579,6 +1581,9 @@ static void *__vmalloc_area_node(struct
 	return area->addr;

fail:

mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +3 -0
+	warn_alloc_failed(gfp_mask, order, "vmalloc: allocation failure, "
+			  "allocated %ld of %ld bytes\n",
+			  (area->nr_pages*PAGE_SIZE), area->size);
 	vfree(area->addr);
 	return NULL;
 }

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Michal "mina86" Nazarewicz    (o o)
ooo +-----<email/xmpp: [email protected]>-----ooO--(_)--Ooo--
#3 lore raw

PATCH 2/2 print vmalloc() state after allocation failures

Quoted 12 lines · Michal Nazarewicz

Review static void *__vmalloc_node(unsigned lon · line 1537

Quoted 9 lines · Dave Hansen

diff -puN mm/vmalloc.c~vmalloc-warn mm/vmalloc.c
--- linux-2.6.git/mm/vmalloc.c~vmalloc-warn 2011-04-15
08:49:06.823306620 -0700
+++ linux-2.6.git-dave/mm/vmalloc.c 2011-04-15 09:20:17.926460283 -0700

(quoted diff) +1 -0
@@ -1534,6 +1534,7 @@ static void *__vmalloc_node(unsigned lon
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, int node, void *caller)
 {
+	int order = 0;

Could we make that const?

Sure. Here's a replacement patch. Compiles and boots for me.

--

I was tracking down a page allocation failure that ended up in vmalloc(). Since vmalloc() uses 0-order pages, if somebody asks for an insane amount of memory, we'll still get a warning with "order:0" in it. That's not very useful.

During recovery, vmalloc() also nicely frees all of the memory that it got up to the point of the failure. That is wonderful, but it also quickly hides any issues. We have a much different sitation if vmalloc() repeatedly fails 10GB in to:

	vmalloc(100 * 1<<30);

versus repeatedly failing 4096 bytes in to a:

	vmalloc(8192);

This patch will print out messages that look like this:

[   68.123503] vmalloc: allocation failure, allocated 6680576 of 13426688 bytes
[   68.124218] bash: page allocation failure: order:0, mode:0xd2
[   68.124811] Pid: 3770, comm: bash Not tainted 2.6.39-rc3-00082-g85f2e68-dirty #333
[   68.125579] Call Trace:
[   68.125853]  [<ffffffff810f6da6>] warn_alloc_failed+0x146/0x170
[   68.126464]  [<ffffffff8107e05c>] ? printk+0x6c/0x70
[   68.126791]  [<ffffffff8112b5d4>] ? alloc_pages_current+0x94/0xe0
[   68.127661]  [<ffffffff8111ed37>] __vmalloc_node_range+0x237/0x290

...

The 'order' variable is added for clarity when calling warn_alloc_failed() to avoid having an unexplained '0' as an argument. The 'tmp_mask' is there to keep the alloc_pages_node() looking sane. Adding __GFP_NOWARN is done because we now have our own, full error message in vmalloc code.

As a side issue, I also noticed that ctl_ioctl() does vmalloc() based solely on an unverified value passed in from userspace. Granted, it's under CAP_SYS_ADMIN, but it still frightens me a bit.

---

 linux-2.6.git-dave/mm/vmalloc.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +7 -2
diff -puN mm/vmalloc.c~vmalloc-warn mm/vmalloc.c
@@ -1534,6 +1534,7 @@ static void *__vmalloc_node(unsigned lon
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, int node, void *caller)
 {
+	const int order = 0;
 	struct page **pages;
 	unsigned int nr_pages, array_size, i;
 	gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
@@ -1560,11 +1561,12 @@ static void *__vmalloc_area_node(struct 
 
 	for (i = 0; i < area->nr_pages; i++) {
 		struct page *page;
+		gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
 
 		if (node < 0)
-			page = alloc_page(gfp_mask);
+			page = alloc_page(tmp_mask);
 		else
-			page = alloc_pages_node(node, gfp_mask, 0);
+			page = alloc_pages_node(node, tmp_mask, order);
 
 		if (unlikely(!page)) {
 			/* Successfully allocated i pages, free them in __vunmap() */
@@ -1579,6 +1581,9 @@ static void *__vmalloc_area_node(struct 
 	return area->addr;
 
 fail:
+	warn_alloc_failed(gfp_mask, order, "vmalloc: allocation failure, "
+			  "allocated %ld of %ld bytes\n",
+			  (area->nr_pages*PAGE_SIZE), area->size);
 	vfree(area->addr);
 	return NULL;
 }

_

-- Dave

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Re: [PATCH 2/2] print vmalloc() state after allocation failures

Quoted 27 lines · Dave Hansen

diff -puN mm/vmalloc.c~vmalloc-warn mm/vmalloc.c

mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +4 -2
@@ -1534,6 +1534,7 @@ static void *__vmalloc_node(unsigned lon
 static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,
 				 pgprot_t prot, int node, void *caller)
 {
+	const int order = 0;
 	struct page **pages;
 	unsigned int nr_pages, array_size, i;
 	gfp_t nested_gfp = (gfp_mask & GFP_RECLAIM_MASK) | __GFP_ZERO;
@@ -1560,11 +1561,12 @@ static void *__vmalloc_area_node(struct 
 
 	for (i = 0; i < area->nr_pages; i++) {
 		struct page *page;
+		gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;
 
 		if (node < 0)
-			page = alloc_page(gfp_mask);
+			page = alloc_page(tmp_mask);
 		else
-			page = alloc_pages_node(node, gfp_mask, 0);
+			page = alloc_pages_node(node, tmp_mask, order);
 
 		if (unlikely(!page)) {
 			/* Successfully allocated i pages, free them in __vunmap() */

mm/vmalloc.c static void *__vmalloc_area_node(struct · line 1589

Quoted 9 lines
mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +3 -0
@@ -1579,6 +1581,9 @@ static void *__vmalloc_area_node(struct 
 	return area->addr;
 
 fail:
+	warn_alloc_failed(gfp_mask, order, "vmalloc: allocation failure, "
+			  "allocated %ld of %ld bytes\n",
+			  (area->nr_pages*PAGE_SIZE), area->size);
 	vfree(area->addr);
 	return NULL;
 }

Sorry, I still don't understand why this isn't just a three-liner patch to call warn_alloc_failed(). I don't see the benefit of the "order" or "tmp_mask" variables at all, they'll just be removed next time someone goes down the mm/* directory and looks for variables that are used only once or are unchanged as a cleanup.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw

Re: [PATCH 2/2] print vmalloc() state after allocation failures

Quoted 8 lines · David Rientjes

mm/vmalloc.c static void *__vmalloc_area_node(struct · line 1589

Quoted 7 lines

fail:

mm/vmalloc.c renamed from mm/vmalloc.c~vmalloc-warn +3 -0
+     warn_alloc_failed(gfp_mask, order, "vmalloc: allocation failure, "
+                       "allocated %ld of %ld bytes\n",
+                       (area->nr_pages*PAGE_SIZE), area->size);
      vfree(area->addr);
      return NULL;
 }
Quoted 5 lines

Sorry, I still don't understand why this isn't just a three-liner patch to call warn_alloc_failed(). I don't see the benefit of the "order" or "tmp_mask" variables at all, they'll just be removed next time someone goes down the mm/* directory and looks for variables that are used only once or are unchanged as a cleanup.

Without the "order" variable, we have:

warn_alloc_failed(gfp_mask, 0, "vmalloc: allocation failure, "
"allocated %ld of %ld bytes\n", (area->nr_pages*PAGE_SIZE), area->size);

I *HATE* those with a passion. What is the '0' _doing_? Is it for "0 pages", "do not print", "_do_ print"? There's no way to tell without going and finding warn_alloc_failed()'s definition.

With 'order' in there, the code self-documents, at least from the caller's side. It makes it 100% clear that the "0" being passed to the allocators is that same as the one passed to the warning; it draws a link between the allocations and the allocation error message:

warn_alloc_failed(gfp_mask, order, "vmalloc: allocation failure, "
"allocated %ld of %ld bytes\n", (area->nr_pages*PAGE_SIZE), area->size);

As for the 'tmp_mask' business. Right now we have:

        for (i = 0; i < area->nr_pages; i++) {
                struct page *page;

+ gfp_t tmp_mask = gfp_mask | __GFP_NOWARN;

                if (node < 0)

- page = alloc_page(gfp_mask);
+ page = alloc_page(tmp_mask);
else
- page = alloc_pages_node(node, gfp_mask, 0);
+ page = alloc_pages_node(node, tmp_mask, order);

The alternative is this:

        for (i = 0; i < area->nr_pages; i++) {
                struct page *page;

                if (node < 0)

- page = alloc_page(gfp_mask);
+ page = alloc_page(gfp_mask | __GFP_NOWARN);
else
- page = alloc_pages_node(node, gfp_mask, 0);
+ page = alloc_pages_node(node, gfp_mask | __GFP_NOWARN,
+ order);

I can go look, but I bet the compiler compiles down to the same thing. Plus, they're the same number of lines in the end. I know which one appeals to me visually.

I think we're pretty deep in personal preference territory here. If I hear a consensus that folks like it one way over another, I'm happy to change it.

-- Dave

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
In reply to Dave Hansen
Quoted 50 lines · Dave Hansen

This originally started as a simple patch to give vmalloc() some more verbose output on failure on top of the plain page allocator messages. Johannes suggested that it might be nicer to lead with the vmalloc() info _before_ the page allocator messages.

But, I do think there's a lot of value in what __alloc_pages_slowpath() does with its filtering and so forth.

This patch creates a new function which other allocators can call instead of relying on the internal page allocator warnings. It also gives this function private rate-limiting which separates it from other printk_ratelimit() users.

---

 linux-2.6.git-dave/include/linux/mm.h |    2 +
 linux-2.6.git-dave/mm/page_alloc.c    |   63 ++++++++++++++++++++++------------
 2 files changed, 44 insertions(+), 21 deletions(-)

diff -puN include/linux/mm.h~break-out-alloc-failure-messages include/linux/mm.h

include/linux/mm.h renamed from include/linux/mm.h~break-out-alloc-failure-messages +2 -0
@@ -1365,6 +1365,8 @@ extern void si_meminfo(struct sysinfo * 
 extern void si_meminfo_node(struct sysinfo *val, int nid);
 extern int after_bootmem;
 
+extern void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...);
+
 extern void setup_per_cpu_pageset(void);
 
 extern void zone_pcp_update(struct zone *zone);

diff -puN mm/page_alloc.c~break-out-alloc-failure-messages mm/page_alloc.c

mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +1 -0
@@ -54,6 +54,7 @@
 #include <trace/events/kmem.h>
 #include <linux/ftrace_event.h>
 #include <linux/memcontrol.h>
+#include <linux/ratelimit.h>
 
 #include <asm/tlbflush.h>
 #include <asm/div64.h>
@@ -1734,6 +1735,46 @@ static inline bool should_suppress_show_
 	return ret;
 }
 

mm/page_alloc.c static inline bool should_suppress_show_ · line 1747

Quoted 10 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +10 -0
+static DEFINE_RATELIMIT_STATE(nopage_rs,
+		DEFAULT_RATELIMIT_INTERVAL,
+		DEFAULT_RATELIMIT_BURST);
+
+void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
+{
+	va_list args;
+	unsigned int filter = SHOW_MEM_FILTER_NODES;
+	const gfp_t wait = gfp_mask & __GFP_WAIT;
+

"wait" is unnecessary. You didn't do "const gfp_t nowarn = gfp_mask & __GFP_NOWARN;" for the same reason.

Quoted 14 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +14 -0
+	if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
+		return;
+
+	/*
+	 * This documents exceptions given to allocations in certain
+	 * contexts that are allowed to allocate outside current's set
+	 * of allowed nodes.
+	 */
+	if (!(gfp_mask & __GFP_NOMEMALLOC))
+		if (test_thread_flag(TIF_MEMDIE) ||
+		    (current->flags & (PF_MEMALLOC | PF_EXITING)))
+			filter &= ~SHOW_MEM_FILTER_NODES;
+	if (in_interrupt() || !wait)
+		filter &= ~SHOW_MEM_FILTER_NODES;

mm/page_alloc.c static inline bool should_suppress_show_ · line 1771

Quoted 10 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +10 -0
+
+	if (fmt) {
+		printk(KERN_WARNING);
+		va_start(args, fmt);
+		vprintk(fmt, args);
+		va_end(args);
+	}
+
+	printk(KERN_WARNING "%s: page allocation failure: order:%d, mode:0x%x\n",
+			current->comm, order, gfp_mask);

pr_warning()?

current->comm should always be printed with get_task_comm() to avoid racing with /proc/pid/comm. Since this function can be called potentially deep in the stack, you may need to serialize this with a statically-allocated buffer.

Quoted 28 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +6 -15
+
+	dump_stack();
+	if (!should_suppress_show_mem())
+		show_mem(filter);
+}
+
 static inline int
 should_alloc_retry(gfp_t gfp_mask, unsigned int order,
 				unsigned long pages_reclaimed)
@@ -2176,27 +2217,7 @@ rebalance:
 	}
 
 nopage:
-	if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
-		unsigned int filter = SHOW_MEM_FILTER_NODES;
-
-		/*
-		 * This documents exceptions given to allocations in certain
-		 * contexts that are allowed to allocate outside current's set
-		 * of allowed nodes.
-		 */
-		if (!(gfp_mask & __GFP_NOMEMALLOC))
-			if (test_thread_flag(TIF_MEMDIE) ||
-			    (current->flags & (PF_MEMALLOC | PF_EXITING)))
-				filter &= ~SHOW_MEM_FILTER_NODES;
-		if (in_interrupt() || !wait)
-			filter &= ~SHOW_MEM_FILTER_NODES;
-

mm/page_alloc.c rebalance: · line 2223

Quoted 10 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +1 -6
-		pr_warning("%s: page allocation failure. order:%d, mode:0x%x\n",
-			current->comm, order, gfp_mask);
-		dump_stack();
-		if (!should_suppress_show_mem())
-			show_mem(filter);
-	}
+	warn_alloc_failed(gfp_mask, order, NULL);
 	return page;
 got_pg:
 	if (kmemcheck_enabled)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
Quoted 9 lines · David Rientjes

mm/page_alloc.c static inline bool should_suppress_show_ · line 1747

Quoted 6 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +6 -0
+void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
+{
+	va_list args;
+	unsigned int filter = SHOW_MEM_FILTER_NODES;
+	const gfp_t wait = gfp_mask & __GFP_WAIT;
+

"wait" is unnecessary. You didn't do "const gfp_t nowarn = gfp_mask & __GFP_NOWARN;" for the same reason.

This line is just a copy from the __alloc_pages_slowpath() one. I guess we only use it once, so I've got no problem killing it.

Quoted 18 lines
Quoted 18 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +18 -0
+	if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
+		return;
+
+	/*
+	 * This documents exceptions given to allocations in certain
+	 * contexts that are allowed to allocate outside current's set
+	 * of allowed nodes.
+	 */
+	if (!(gfp_mask & __GFP_NOMEMALLOC))
+		if (test_thread_flag(TIF_MEMDIE) ||
+		    (current->flags & (PF_MEMALLOC | PF_EXITING)))
+			filter &= ~SHOW_MEM_FILTER_NODES;
+	if (in_interrupt() || !wait)
+		filter &= ~SHOW_MEM_FILTER_NODES;
+
+	if (fmt) {
+		printk(KERN_WARNING);
+		va_start(args, fmt);
Quoted 8 lines

mm/page_alloc.c static inline bool should_suppress_show_ · line 1771

Quoted 6 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +6 -0
+		vprintk(fmt, args);
+		va_end(args);
+	}
+
+	printk(KERN_WARNING "%s: page allocation failure: order:%d, mode:0x%x\n",
+			current->comm, order, gfp_mask);

pr_warning()?

OK, I'll change it back.

Quoted 4 lines

current->comm should always be printed with get_task_comm() to avoid racing with /proc/pid/comm. Since this function can be called potentially deep in the stack, you may need to serialize this with a statically-allocated buffer.

This code was already in page_alloc.c. I'm simply breaking it out here trying to keep the changes down to what is needed minimally to move the code. Correcting this preexisting problem sounds like a great follow-on patch.

-- Dave

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
Quoted 47 lines · Dave Hansen
Quoted 9 lines

mm/page_alloc.c static inline bool should_suppress_show_ · line 1747

Quoted 6 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +6 -0
+void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
+{
+	va_list args;
+	unsigned int filter = SHOW_MEM_FILTER_NODES;
+	const gfp_t wait = gfp_mask & __GFP_WAIT;
+

"wait" is unnecessary. You didn't do "const gfp_t nowarn = gfp_mask & __GFP_NOWARN;" for the same reason.

This line is just a copy from the __alloc_pages_slowpath() one. I guess we only use it once, so I've got no problem killing it.

Quoted 18 lines
Quoted 18 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +18 -0
+	if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
+		return;
+
+	/*
+	 * This documents exceptions given to allocations in certain
+	 * contexts that are allowed to allocate outside current's set
+	 * of allowed nodes.
+	 */
+	if (!(gfp_mask & __GFP_NOMEMALLOC))
+		if (test_thread_flag(TIF_MEMDIE) ||
+		    (current->flags & (PF_MEMALLOC | PF_EXITING)))
+			filter &= ~SHOW_MEM_FILTER_NODES;
+	if (in_interrupt() || !wait)
+		filter &= ~SHOW_MEM_FILTER_NODES;
+
+	if (fmt) {
+		printk(KERN_WARNING);
+		va_start(args, fmt);
Quoted 8 lines

mm/page_alloc.c static inline bool should_suppress_show_ · line 1771

Quoted 6 lines
mm/page_alloc.c renamed from mm/page_alloc.c~break-out-alloc-failure-messages +6 -0
+		vprintk(fmt, args);
+		va_end(args);
+	}
+
+	printk(KERN_WARNING "%s: page allocation failure: order:%d, mode:0x%x\n",
+			current->comm, order, gfp_mask);

pr_warning()?

OK, I'll change it back.

Quoted 4 lines

current->comm should always be printed with get_task_comm() to avoid racing with /proc/pid/comm. Since this function can be called potentially deep in the stack, you may need to serialize this with a statically-allocated buffer.

Quoted 5 lines

This code was already in page_alloc.c. I'm simply breaking it out here trying to keep the changes down to what is needed minimally to move the code. Correcting this preexisting problem sounds like a great follow-on patch.

It shouldn't be a follow-on patch since you're introducing a new feature here (vmalloc allocation failure warnings) and what I'm identifying is a race in the access to current->comm. A bug fix for a race should always preceed a feature that touches the same code.

There's two options to fixing the race:

- provide a statically-allocated buffer to use for get_task_comm() and copy current->comm over before printing it, or

- take task_lock(current) to protect against /proc/pid/comm.

The latter probably isn't safe because we could potentially already be holding task_lock(current) during a GFP_ATOMIC page allocation.

#9 lore raw
Quoted 4 lines · David Rientjes

It shouldn't be a follow-on patch since you're introducing a new feature here (vmalloc allocation failure warnings) and what I'm identifying is a race in the access to current->comm. A bug fix for a race should always preceed a feature that touches the same code.

Dude. Seriously. Glass house! a63d83f4

I'll go look in to it, though.

-- Dave

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#10 lore raw
Quoted 7 lines · Dave Hansen
Quoted 4 lines

It shouldn't be a follow-on patch since you're introducing a new feature here (vmalloc allocation failure warnings) and what I'm identifying is a race in the access to current->comm. A bug fix for a race should always preceed a feature that touches the same code.

Dude. Seriously. Glass house! a63d83f4

Not sure what you're implying here. The commit you've identified is the oom killer rewrite and the oom killer is very specific about making sure to always hold task_lock() whenever dereferencing ->comm, even for current, to guard against /proc/pid/comm or prctl(). The oom killer is different from your usecase, however, because we can always take task_lock(current) in the oom killer because it's in a blockable context, whereas page allocation warnings can occur in a superset.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw
In reply to David Rientjes
Quoted 7 lines · David Rientjes

- provide a statically-allocated buffer to use for get_task_comm() and copy current->comm over before printing it, or

- take task_lock(current) to protect against /proc/pid/comm.

The latter probably isn't safe because we could potentially already be holding task_lock(current) during a GFP_ATOMIC page allocation.

I'm not sure get_task_comm() is suitable, either. It takes the task lock:

char *get_task_comm(char *buf, struct task_struct *tsk)

{
        /* buf must be at least sizeof(tsk->comm) in size */
        task_lock(tsk);
        strncpy(buf, tsk->comm, sizeof(tsk->comm));
        task_unlock(tsk);
        return buf;
}

-- Dave

#12 lore raw
In reply to David Rientjes
Quoted 4 lines · David Rientjes

It shouldn't be a follow-on patch since you're introducing a new feature here (vmalloc allocation failure warnings) and what I'm identifying is a race in the access to current->comm. A bug fix for a race should always preceed a feature that touches the same code.

So, what's the race here? kmemleak.c says?

/*
* There is a small chance of a race with set_task_comm(),
* however using get_task_comm() here may cause locking
* dependency issues with current->alloc_lock. In the worst
* case, the command line is not correct.
*/
strncpy(object->comm, current->comm, sizeof(object->comm));

We're trying to make sure we don't print out a partially updated tsk->comm? Or, is there a bigger issue here like potential oopses or kernel information leaks.

1. We require that no memory allocator ever holds the task lock for the current task, and we audit all the existing GFP_ATOMIC users in the kernel to ensure they're not doing it now. In the case of a problem, we end up with a hung kernel while trying to get a message out to the console.
2. We remove current->comm from the printk(), and deal with the information loss.
3. We live with corrupted output, like the other ~400 in-kernel users of ->comm do. (I'm assuming that very few of them hold the task lock). In the case of a race, we get junk on the console, but an otherwise fine bug report (the way it is now).
4. We come up with some way to print out current->comm, without holding any task locks. We could do this by copying it somewhere safe on each context switch. Could probably also do it with RCU.

There's also a very, very odd message in fs/exec.c:

/*
* Threads may access current->comm without holding
* the task lock, so write the string carefully.
* Readers without a lock may see incomplete new
* names but are safe from non-terminating string reads. */

-- Dave

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw
Quoted 35 lines · David Rientjes
Quoted 4 lines

It shouldn't be a follow-on patch since you're introducing a new feature here (vmalloc allocation failure warnings) and what I'm identifying is a race in the access to current->comm. A bug fix for a race should always preceed a feature that touches the same code.

So, what's the race here? kmemleak.c says?

/*
* There is a small chance of a race with set_task_comm(),
* however using get_task_comm() here may cause locking
* dependency issues with current->alloc_lock. In the worst
* case, the command line is not correct.
*/
strncpy(object->comm, current->comm, sizeof(object->comm));

We're trying to make sure we don't print out a partially updated tsk->comm? Or, is there a bigger issue here like potential oopses or kernel information leaks.

1. We require that no memory allocator ever holds the task lock for the current task, and we audit all the existing GFP_ATOMIC users in the kernel to ensure they're not doing it now. In the case of a problem, we end up with a hung kernel while trying to get a message out to the console.
2. We remove current->comm from the printk(), and deal with the information loss.
3. We live with corrupted output, like the other ~400 in-kernel users of ->comm do. (I'm assuming that very few of them hold the task lock). In the case of a race, we get junk on the console, but an otherwise fine bug report (the way it is now).
4. We come up with some way to print out current->comm, without holding any task locks. We could do this by copying it somewhere safe on each context switch. Could probably also do it with RCU.

Quoted 8 lines

There's also a very, very odd message in fs/exec.c:

/*
* Threads may access current->comm without holding
* the task lock, so write the string carefully.
* Readers without a lock may see incomplete new
* names but are safe from non-terminating string reads. */

The rule is,

1) writing comm
need task_lock
2) read _another_ thread's comm
need task_lock
3) read own comm
no need task_lock

That's the reason why oom-kill.c need task_lock and other a lot of place don't need task_lock. I agree this is very strange. it's only historical reason.

The comment of set_task_comm() explained a race against (3).

Thanks.

#14 lore raw
Quoted 9 lines · KOSAKI Motohiro

The rule is,

1) writing comm
need task_lock
2) read _another_ thread's comm
need task_lock
3) read own comm
no need task_lock

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

#15 lore raw
Quoted 12 lines · KOSAKI Motohiro
Quoted 10 lines

The rule is,

1) writing comm
need task_lock
2) read _another_ thread's comm
need task_lock
3) read own comm
no need task_lock

Quoted 5 lines

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

Right. /proc/pid/comm is evil. We have to fix it. otherwise we need change all of current->comm user. It's very lots!

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#16 lore raw
Quoted 9 lines · KOSAKI Motohiro
Quoted 5 lines

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

Right. /proc/pid/comm is evil. We have to fix it. otherwise we need change all of current->comm user. It's very lots!

Fixing it in this case would be removing it and only allowing it for current via the usual prctl() :) The code was introduced in 4614a696bd1c (procfs: allow threads to rename siblings via /proc/pid/tasks/tid/comm) in December 2009 and seems to originally be meant for debugging. We simply can't continue to let it modify any thread's ->comm unless we change the over 300 current->comm deferences in the kernel.

I'd prefer that we remove /proc/pid/comm entirely or at least prevent writing to it unless CONFIG_EXPERT.

#17 lore raw
Quoted 19 lines · David Rientjes
Quoted 9 lines · KOSAKI Motohiro
Quoted 5 lines

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

Right. /proc/pid/comm is evil. We have to fix it. otherwise we need change all of current->comm user. It's very lots!

Fixing it in this case would be removing it and only allowing it for current via the usual prctl() :) The code was introduced in 4614a696bd1c (procfs: allow threads to rename siblings via /proc/pid/tasks/tid/comm) in December 2009 and seems to originally be meant for debugging. We simply can't continue to let it modify any thread's ->comm unless we change the over 300 current->comm deferences in the kernel.

Quoted 2 lines

I'd prefer that we remove /proc/pid/comm entirely or at least prevent writing to it unless CONFIG_EXPERT.

Eeeh. That's probably going to be a tough sell, as I think there is wider interest in what it provides. Its useful for debugging applications not kernels, so I doubt folks will want to rebuild their kernel to try to analyze a java issue.

So I'm well aware that there is the chance that you catch the race and read an incomplete/invalid comm (it was discussed at length when the change went in), but somewhere I've missed how that's causing actual problems. Other then just being "evil" and having the documented race, could you clarify what the issue is that your hitting?

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#18 lore raw
Quoted 28 lines · David Rientjes
Quoted 19 lines · KOSAKI Motohiro
Quoted 10 lines
Quoted 6 lines

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

Right. /proc/pid/comm is evil. We have to fix it. otherwise we need change all of current->comm user. It's very lots!

Fixing it in this case would be removing it and only allowing it for current via the usual prctl() :) The code was introduced in 4614a696bd1c (procfs: allow threads to rename siblings via /proc/pid/tasks/tid/comm) in December 2009 and seems to originally be meant for debugging. We simply can't continue to let it modify any thread's ->comm unless we change the over 300 current->comm deferences in the kernel.

Quoted 2 lines

I'd prefer that we remove /proc/pid/comm entirely or at least prevent writing to it unless CONFIG_EXPERT.

Eeeh. That's probably going to be a tough sell, as I think there is wider interest in what it provides. Its useful for debugging applications not kernels, so I doubt folks will want to rebuild their kernel to try to analyze a java issue.

Quoted 5 lines

So I'm well aware that there is the chance that you catch the race and read an incomplete/invalid comm (it was discussed at length when the change went in), but somewhere I've missed how that's causing actual problems. Other then just being "evil" and having the documented race, could you clarify what the issue is that your hitting?

The problem is, there is no documented as well. Okay, I recognized you introduced new locking rule for task->comm. But there is no documented it. Thus, We have no way to review current callsites are correct or not. Can you please do it? And, I have a question. Do you mean now task->comm reader don't need task_lock() even if it is another thread?

_if_ every task->comm reader have to realize it has a chance to read incomplete/invalid comm, task_lock() doesn't makes any help.

And one correction.
------------------------------------------------------------------
static ssize_t comm_write(struct file *file, const char __user *buf,
size_t count, loff_t *offset)

{
        struct inode *inode = file->f_path.dentry->d_inode;
        struct task_struct *p;
        char buffer[TASK_COMM_LEN];

        memset(buffer, 0, sizeof(buffer));
        if (count > sizeof(buffer) - 1)
                count = sizeof(buffer) - 1;
        if (copy_from_user(buffer, buf, count))
                return -EFAULT;

        p = get_proc_task(inode);
        if (!p)
                return -ESRCH;

        if (same_thread_group(current, p))
                set_task_comm(p, buffer);
        else
                count = -EINVAL;

------------------------------------------------------------------

This code doesn't have proper credential check. IOW, you forgot to pthread_setuid_np() case.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#19 lore raw
Quoted 14 lines
Quoted 8 lines
Quoted 2 lines

I'd prefer that we remove /proc/pid/comm entirely or at least prevent writing to it unless CONFIG_EXPERT.

Eeeh. That's probably going to be a tough sell, as I think there is wider interest in what it provides. Its useful for debugging applications not kernels, so I doubt folks will want to rebuild their kernel to try to analyze a java issue.

Quoted 5 lines

So I'm well aware that there is the chance that you catch the race and read an incomplete/invalid comm (it was discussed at length when the change went in), but somewhere I've missed how that's causing actual problems. Other then just being "evil" and having the documented race, could you clarify what the issue is that your hitting?

Quoted 8 lines

The problem is, there is no documented as well. Okay, I recognized you introduced new locking rule for task->comm. But there is no documented it. Thus, We have no way to review current callsites are correct or not. Can you please do it? And, I have a question. Do you mean now task->comm reader don't need task_lock() even if it is another thread?

_if_ every task->comm reader have to realize it has a chance to read incomplete/invalid comm, task_lock() doesn't makes any help.

ping?

#20 lore raw
In reply to KOSAKI Motohiro
Quoted 34 lines · KOSAKI Motohiro
Quoted 28 lines · David Rientjes
Quoted 19 lines · KOSAKI Motohiro
Quoted 10 lines
Quoted 6 lines

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

Right. /proc/pid/comm is evil. We have to fix it. otherwise we need change all of current->comm user. It's very lots!

Fixing it in this case would be removing it and only allowing it for current via the usual prctl() :) The code was introduced in 4614a696bd1c (procfs: allow threads to rename siblings via /proc/pid/tasks/tid/comm) in December 2009 and seems to originally be meant for debugging. We simply can't continue to let it modify any thread's ->comm unless we change the over 300 current->comm deferences in the kernel.

Quoted 2 lines

I'd prefer that we remove /proc/pid/comm entirely or at least prevent writing to it unless CONFIG_EXPERT.

Eeeh. That's probably going to be a tough sell, as I think there is wider interest in what it provides. Its useful for debugging applications not kernels, so I doubt folks will want to rebuild their kernel to try to analyze a java issue.

Quoted 5 lines

So I'm well aware that there is the chance that you catch the race and read an incomplete/invalid comm (it was discussed at length when the change went in), but somewhere I've missed how that's causing actual problems. Other then just being "evil" and having the documented race, could you clarify what the issue is that your hitting?

Quoted 8 lines

The problem is, there is no documented as well. Okay, I recognized you introduced new locking rule for task->comm. But there is no documented it. Thus, We have no way to review current callsites are correct or not. Can you please do it? And, I have a question. Do you mean now task->comm reader don't need task_lock() even if it is another thread?

_if_ every task->comm reader have to realize it has a chance to read incomplete/invalid comm, task_lock() doesn't makes any help.

Sorry if this somehow got off on the wrong foot. Its just surprising to see such passion bubble up after almost two years of quiet since the proc patch went in.

So I'm not proposing comm be totally lock free (Dave Hansen might do that for me, we'll see :) but when the original patch was proposed, the idea that transient empty or incomplete comms would be possible was brought up and didn't seem to be a big enough issue at the time to block it from being merged.

Its just having a more specific case where these transient null/incomplete comms causes an issue would help prioritize the need for correctness.

In the meantime, I'll put some effort into trying to protect unlocked current->comm acccess using get_task_comm() where possible. Won't happen in a day, and help would be appreciated.

When we hit the point where the remaining places are where the task_lock can't be taken, we can either live with the possible incomplete comm or add a new lock to protect just the comm.

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#21 lore raw
Quoted 4 lines · john stultz

Sorry if this somehow got off on the wrong foot. Its just surprising to see such passion bubble up after almost two years of quiet since the proc patch went in.

It hasn't been two years, it hasn't even been 18 months.

	$ git diff 4614a696bd1c.. | grep "^+.*current\->comm" | wc -l
	42

Apparently those dozens of new references directly to current->comm since the change also were unaware of the need to use get_task_comm() to avoid a racy writer. I don't think there's any code in the kernel that is ok with corrupted task names being printed: those messages are usually important.

Quoted 6 lines

So I'm not proposing comm be totally lock free (Dave Hansen might do that for me, we'll see :) but when the original patch was proposed, the idea that transient empty or incomplete comms would be possible was brought up and didn't seem to be a big enough issue at the time to block it from being merged.

I'm not really interested in the discussion that happened at the time, I'm concerned about racy readers of any thread's comm that result in corrupted strings being printed or used in the kernel.

Quoted 4 lines

Its just having a more specific case where these transient null/incomplete comms causes an issue would help prioritize the need for correctness.

It doesn't seem like there was any due diligence to ensure other code wasn't broken. When comm could only be changed by prctl(), we needed no protection for current->comm and so code naturally will reference it directly. Since that's now changed, no audit was done to ensure the 300+ references throughout the tree doesn't require non-racy reads.

Quoted 4 lines

In the meantime, I'll put some effort into trying to protect unlocked current->comm acccess using get_task_comm() where possible. Won't happen in a day, and help would be appreciated.

We need to stop protecting ->comm with ->alloc_lock since it is used for other members of task_struct that may or may not be held in a function that wants to read ->comm. We should probably introduce a seqlock.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#22 lore raw
Quoted 9 lines · David Rientjes
Quoted 4 lines · john stultz

In the meantime, I'll put some effort into trying to protect unlocked current->comm acccess using get_task_comm() where possible. Won't happen in a day, and help would be appreciated.

We need to stop protecting ->comm with ->alloc_lock since it is used for other members of task_struct that may or may not be held in a function that wants to read ->comm. We should probably introduce a seqlock.

Agreed. My initial approach is to consolidate accesses to use get_task_comm(), with special case to skip the locking if tsk==current, as well as a lock free __get_task_comm() for cases where its not current being accessed and the task locking is already done.

Once that's all done, the next step is to switch to a seqlock (or possibly RCU if Dave is still playing with that idea), internally in the get_task_comm implementation and then yank the special __get_task_comm.

But other suggestions are welcome.

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#23 lore raw
Quoted 11 lines · john stultz
Quoted 9 lines · David Rientjes
Quoted 4 lines · john stultz

In the meantime, I'll put some effort into trying to protect unlocked current->comm acccess using get_task_comm() where possible. Won't happen in a day, and help would be appreciated.

We need to stop protecting ->comm with ->alloc_lock since it is used for other members of task_struct that may or may not be held in a function that wants to read ->comm. We should probably introduce a seqlock.

Quoted 8 lines

Agreed. My initial approach is to consolidate accesses to use get_task_comm(), with special case to skip the locking if tsk==current, as well as a lock free __get_task_comm() for cases where its not current being accessed and the task locking is already done.

Once that's all done, the next step is to switch to a seqlock (or possibly RCU if Dave is still playing with that idea), internally in the get_task_comm implementation and then yank the special __get_task_comm.

So thinking further, this can be simplified by adding the seqlock first, and then retaining the task_locking only in the set_task_comm path until all comm accessors are converted to using get_task_comm.

I'll be sending out some initial patches for review shortly.

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#24 lore raw
Quoted 4 lines · john stultz

So thinking further, this can be simplified by adding the seqlock first, and then retaining the task_locking only in the set_task_comm path until all comm accessors are converted to using get_task_comm.

On second thought, I think it would be better to just retain using a spinlock but instead of using alloc_lock, introduce a new spinlock to task_struct for the sole purpose of protecting comm.

And, instead, of using get_task_comm() to write into a preallocated buffer, I think it would be easier in the vast majority of cases that you'll need to convert to just provide task_comm_lock(p) and task_comm_unlock(p) so that p->comm can be dereferenced safely. get_task_comm() could use that interface itself and then write into a preallocated buffer.

The problem with using get_task_comm() everywhere is it requires 16 additional bytes to be allocated on the stack in hundreds of locations around the kernel which may or may not be safe.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#25 lore raw
Quoted 7 lines · David Rientjes
Quoted 4 lines · john stultz

So thinking further, this can be simplified by adding the seqlock first, and then retaining the task_locking only in the set_task_comm path until all comm accessors are converted to using get_task_comm.

Quoted 8 lines

On second thought, I think it would be better to just retain using a spinlock but instead of using alloc_lock, introduce a new spinlock to task_struct for the sole purpose of protecting comm.

And, instead, of using get_task_comm() to write into a preallocated buffer, I think it would be easier in the vast majority of cases that you'll need to convert to just provide task_comm_lock(p) and task_comm_unlock(p) so that p->comm can be dereferenced safely.

So my concern with this is that it means one more lock that could be mis-nested. By keeping the locking isolated to the get/set_task_comm, we can be sure that won't happen.

Also tracking new current->comm references will be easier if we just don't allow new ones. Validating that all the comm references are correctly locked becomes more difficult if we need locking at each use site.

Further, since I'm not convinced that we never reference current->comm from irq context, if we go with spinlocks, we're going to have to disable irqs in the read path as well. seqlocks were nice for that aspect.

Quoted 6 lines

get_task_comm() could use that interface itself and then write into a preallocated buffer.

The problem with using get_task_comm() everywhere is it requires 16 additional bytes to be allocated on the stack in hundreds of locations around the kernel which may or may not be safe.

True. Although is this maybe a bit overzealous?

Maybe I can make sure not to add any mid-layer stack nesting by limiting the scope of the 16bytes to just around where it is used. This would ensure we're only adding 16bytes to any current usage.

Other ideas?

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#26 lore raw
Quoted 8 lines · john stultz
Quoted 7 lines · David Rientjes
Quoted 4 lines · john stultz

So thinking further, this can be simplified by adding the seqlock first, and then retaining the task_locking only in the set_task_comm path until all comm accessors are converted to using get_task_comm.

Quoted 8 lines
Quoted 8 lines

On second thought, I think it would be better to just retain using a spinlock but instead of using alloc_lock, introduce a new spinlock to task_struct for the sole purpose of protecting comm.

And, instead, of using get_task_comm() to write into a preallocated buffer, I think it would be easier in the vast majority of cases that you'll need to convert to just provide task_comm_lock(p) and task_comm_unlock(p) so that p->comm can be dereferenced safely.

Ok.. trying to find a middle ground here by replying to my own concerns. :)

Quoted 8 lines

So my concern with this is that it means one more lock that could be mis-nested. By keeping the locking isolated to the get/set_task_comm, we can be sure that won't happen.

Also tracking new current->comm references will be easier if we just don't allow new ones. Validating that all the comm references are correctly locked becomes more difficult if we need locking at each use site.

So maybe we still ban current->comm access and instead have a lightweight get_comm_locked() accessor or something that. Then we can add debugging options to validate that the lock is properly held internally.

Quoted 4 lines

Further, since I'm not convinced that we never reference current->comm from irq context, if we go with spinlocks, we're going to have to disable irqs in the read path as well. seqlocks were nice for that aspect.

rwlocks can resolve this concern.

Any other thoughts?

-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#27 lore raw
In reply to KOSAKI Motohiro
Quoted 23 lines · KOSAKI Motohiro

And one correction.
------------------------------------------------------------------
static ssize_t comm_write(struct file *file, const char __user *buf,
size_t count, loff_t *offset)

{
        struct inode *inode = file->f_path.dentry->d_inode;
        struct task_struct *p;
        char buffer[TASK_COMM_LEN];

        memset(buffer, 0, sizeof(buffer));
        if (count > sizeof(buffer) - 1)
                count = sizeof(buffer) - 1;
        if (copy_from_user(buffer, buf, count))
                return -EFAULT;

        p = get_proc_task(inode);
        if (!p)
                return -ESRCH;

        if (same_thread_group(current, p))
                set_task_comm(p, buffer);
        else
                count = -EINVAL;
Quoted 4 lines

------------------------------------------------------------------

This code doesn't have proper credential check. IOW, you forgot to pthread_setuid_np() case.

Sorry, could you expand on this a bit? Google isn't coming up with much for pthread_setuid_np. Can a thread actually end up with different uid then the process it is a member of?

Or is same_thread_group not really what I think it is? What would be a better way to check that the two threads are members of the same process?

thanks
-john

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#28 lore raw
Quoted 26 lines · KOSAKI Motohiro
Quoted 25 lines

And one correction.
------------------------------------------------------------------
static ssize_t comm_write(struct file *file, const char __user *buf,
size_t count, loff_t *offset)

{
        struct inode *inode = file->f_path.dentry->d_inode;
        struct task_struct *p;
        char buffer[TASK_COMM_LEN];

        memset(buffer, 0, sizeof(buffer));
        if (count > sizeof(buffer) - 1)
                count = sizeof(buffer) - 1;
        if (copy_from_user(buffer, buf, count))
                return -EFAULT;

        p = get_proc_task(inode);
        if (!p)
                return -ESRCH;

        if (same_thread_group(current, p))
                set_task_comm(p, buffer);
        else
                count = -EINVAL;

------------------------------------------------------------------

Quoted 6 lines
Quoted 2 lines

This code doesn't have proper credential check. IOW, you forgot to pthread_setuid_np() case.

Sorry, could you expand on this a bit? Google isn't coming up with much for pthread_setuid_np. Can a thread actually end up with different uid then the process it is a member of?

Yes. Linux kernel _always_ only care per-thread uid.
glibc 2.3.3 or earlier, it use kernel syscall straight forward. and then userland application also don't have a way to change per-process uid.

glbc 2.3.4 or later, glibc implement per-process setuid by using signal for inter thread communication. (ie, every thread call setuid() syscall internally). Hm, currently pthread_setuid_np don't have proper exported header file. so, parpaps, we need to only worry about syscall(NR_uid) and old libc?

Anyway, If you see task_struct definition, you can easily find it has cred.

Thanks.

Quoted 9 lines

Or is same_thread_group not really what I think it is? What would be a better way to check that the two threads are members of the same process?

thanks
-john

#29 lore raw
In reply to David Rientjes
Quoted 10 lines · David Rientjes
Quoted 8 lines · KOSAKI Motohiro

The rule is,

1) writing comm
need task_lock
2) read _another_ thread's comm
need task_lock
3) read own comm
no need task_lock

Quoted 5 lines

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

Everybody still goes through set_task_comm() to _set_ it, though. That means that the worst case scenario that we get is output truncated (possibly to nothing). We already have at least one existing user in mm/ (kmemleak) that thinks this is OK. I'd tend to err in the direction of taking a truncated or empty task name to possibly locking up the system.

There are also plenty of instances of current->comm going in to the kernel these days. I count 18 added since 2.6.37.

As for a long-term fix, locks probably aren't the answer. Would something like this completely untested patch work? It would have the added bonus that it keeps tsk->comm users working for the moment. We could eventually add an rcu_read_lock()-annotated access function.

---

 linux-2.6.git-dave/fs/exec.c                 |   22 +++++++++++++++-------
 linux-2.6.git-dave/include/linux/init_task.h |    3 ++-
 linux-2.6.git-dave/include/linux/sched.h     |    3 ++-
 3 files changed, 19 insertions(+), 9 deletions(-)

diff -puN mm/page_alloc.c~tsk_comm mm/page_alloc.c

include/linux/sched.h renamed from include/linux/sched.h~tsk_comm +2 -1
diff -puN include/linux/sched.h~tsk_comm include/linux/sched.h
@@ -1334,10 +1334,11 @@ struct task_struct {
 					 * credentials (COW) */
 	struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */
 
-	char comm[TASK_COMM_LEN]; /* executable name excluding path
+	char comm_buf[TASK_COMM_LEN]; /* executable name excluding path
 				     - access with [gs]et_task_comm (which lock
 				       it with task_lock())
 				     - initialized normally by setup_new_exec */
+	char __rcu *comm;
 /* file system info */
 	int link_count, total_link_count;
 #ifdef CONFIG_SYSVIPC
include/linux/init_task.h renamed from include/linux/init_task.h~tsk_comm +2 -1
diff -puN include/linux/init_task.h~tsk_comm include/linux/init_task.h
@@ -161,7 +161,8 @@ extern struct cred init_cred;
 	.group_leader	= &tsk,						\
 	RCU_INIT_POINTER(.real_cred, &init_cred),			\
 	RCU_INIT_POINTER(.cred, &init_cred),				\
-	.comm		= "swapper",					\
+	.comm_buf	= "swapper",					\
+	.comm		= &tsk.comm_buf, 				\
 	.thread		= INIT_THREAD,					\
 	.fs		= &init_fs,					\
 	.files		= &init_files,					\
fs/exec.c renamed from fs/exec.c~tsk_comm +15 -7
diff -puN fs/exec.c~tsk_comm fs/exec.c
@@ -1007,17 +1007,25 @@ char *get_task_comm(char *buf, struct ta
 
 void set_task_comm(struct task_struct *tsk, char *buf)
 {
+	char tmp_comm[TASK_COMM_LEN];
+
 	task_lock(tsk);
 
+	memcpy(tmp_comm, tsk->comm_buf, TASK_COMM_LEN);
+	tsk->comm = tmp;
 	/*
-	 * Threads may access current->comm without holding
-	 * the task lock, so write the string carefully.
-	 * Readers without a lock may see incomplete new
-	 * names but are safe from non-terminating string reads.
+	 * Make sure no one is still looking at tsk->comm_buf
 	 */
-	memset(tsk->comm, 0, TASK_COMM_LEN);
-	wmb();
-	strlcpy(tsk->comm, buf, sizeof(tsk->comm));
+	synchronize_rcu();
+
+	strlcpy(tsk->comm_buf, buf, sizeof(tsk->comm));
+	tsk->comm = tsk->com_buff;
+	/*
+	 * Make sure no one is still looking at the
+	 * stack-allocated buffer
+	 */
+	synchronize_rcu();
+
 	task_unlock(tsk);
 	perf_event_comm(tsk);
 }

-- Dave

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#30 lore raw

Hi

(Cc to John Stultz who/proc/<pid>/comm author. I think we need to hear his opinion)

Quoted 24 lines · David Rientjes
Quoted 10 lines · KOSAKI Motohiro
Quoted 8 lines

The rule is,

1) writing comm
need task_lock
2) read _another_ thread's comm
need task_lock
3) read own comm
no need task_lock

Quoted 5 lines

That was true a while ago, but you now need to protect every thread's ->comm with get_task_comm() or ensuring task_lock() is held to protect against /proc/pid/comm which can change other thread's ->comm. That was different before when prctl(PR_SET_NAME) would only operate on current, so no lock was needed when reading current->comm.

Everybody still goes through set_task_comm() to _set_ it, though. That means that the worst case scenario that we get is output truncated (possibly to nothing). We already have at least one existing user in mm/ (kmemleak) that thinks this is OK. I'd tend to err in the direction of taking a truncated or empty task name to possibly locking up the system.

Quoted 7 lines

There are also plenty of instances of current->comm going in to the kernel these days. I count 18 added since 2.6.37.

As for a long-term fix, locks probably aren't the answer. Would something like this completely untested patch work? It would have the added bonus that it keeps tsk->comm users working for the moment. We could eventually add an rcu_read_lock()-annotated access function.

The concept is ok to me. but AFAIK some caller are now using ARRAY_SIZE(tsk->comm). or sizeof(tsk->comm). Probably callers need to be changed too.

Thanks.

#31 lore raw
Quoted 2 lines

The concept is ok to me. but AFAIK some caller are now using ARRAY_SIZE(tsk->comm). or sizeof(tsk->comm). Probably callers need to be changed too.

one more correction.

Quoted 9 lines

void set_task_comm(struct task_struct *tsk, char *buf) {

fs/exec.c renamed from fs/exec.c~tsk_comm +4 -0
+	char tmp_comm[TASK_COMM_LEN];
+
 	task_lock(tsk);
 
+	memcpy(tmp_comm, tsk->comm_buf, TASK_COMM_LEN);
+	tsk->comm = tmp;
 	/*

fs/exec.c char *get_task_comm(char *buf, struct ta · line 1019

Quoted 10 lines
fs/exec.c renamed from fs/exec.c~tsk_comm +2 -7
-	 * Threads may access current->comm without holding
-	 * the task lock, so write the string carefully.
-	 * Readers without a lock may see incomplete new
-	 * names but are safe from non-terminating string reads.
+	 * Make sure no one is still looking at tsk->comm_buf
 	 */
-	memset(tsk->comm, 0, TASK_COMM_LEN);
-	wmb();
-	strlcpy(tsk->comm, buf, sizeof(tsk->comm));
+	synchronize_rcu();

The doc says,

/**
* synchronize_rcu - wait until a grace period has elapsed. *

And here is under spinlock.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#32 lore raw
Quoted 14 lines · KOSAKI Motohiro

fs/exec.c char *get_task_comm(char *buf, struct ta · line 1019

Quoted 13 lines
fs/exec.c renamed from fs/exec.c~tsk_comm +4 -7
+     memcpy(tmp_comm, tsk->comm_buf, TASK_COMM_LEN);
+     tsk->comm = tmp;
      /*
-      * Threads may access current->comm without holding
-      * the task lock, so write the string carefully.
-      * Readers without a lock may see incomplete new
-      * names but are safe from non-terminating string reads.
+      * Make sure no one is still looking at tsk->comm_buf
       */
-     memset(tsk->comm, 0, TASK_COMM_LEN);
-     wmb();
-     strlcpy(tsk->comm, buf, sizeof(tsk->comm));
+     synchronize_rcu();
Quoted 4 lines

The doc says,

/**
* synchronize_rcu - wait until a grace period has elapsed.

Yeah, yeah... see "completely untested". :)

I'll see if dropping the locks or something else equally hackish can help.

-- Dave