Skip to contentSkip to conversation

mm: memcontrol: fix kernel stack account

PATCH Superseded Muchun Song 4 participants 6 messages Mar 3, 2021

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: memcontrol: fix kernel stack account

For simplification 991e7673859e ("mm: memcontrol: account kernel stack per node") has changed the per zone vmalloc backed stack pages accounting to per node. By doing that we have lost a certain precision because those pages might live in different NUMA nodes. In the end NR_KERNEL_STACK_KB exported to the userspace might be over estimated on some nodes while underestimated on others.

This doesn't impose any real problem to correctnes of the kernel behavior as the counter is not used for any internal processing but it can cause some confusion to the userspace.

Address the problem by accounting each vmalloc backing page to its own node.

Fixes 991e7673859e ("mm: memcontrol: account kernel stack per node")

---
Changelog in v2:
- Rework commit log suggested by Michal.

Thanks to Michal and Shakeel for review.

 kernel/fork.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
kernel/fork.c +10 -5
index d66cd1014211..6e2201feb524 100644
@@ -379,14 +379,19 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
 	void *stack = task_stack_page(tsk);
 	struct vm_struct *vm = task_stack_vm_area(tsk);
 
+	if (vm) {
+		int i;
 
-	/* All stack pages are in the same node. */
-	if (vm)
-		mod_lruvec_page_state(vm->pages[0], NR_KERNEL_STACK_KB,
-				      account * (THREAD_SIZE / 1024));
-	else
+		BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);
+
+		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
+			mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB,
+					      account * (PAGE_SIZE / 1024));
+	} else {
+		/* All stack pages are in the same node. */
 		mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB,
 				      account * (THREAD_SIZE / 1024));
+	}
 }
 
 static int memcg_charge_kernel_stack(struct task_struct *tsk)
Signature · 1 line
2.11.0
reviewer Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Muchun Song

For simplification 991e7673859e ("mm: memcontrol: account kernel stack per node") has changed the per zone vmalloc backed stack pages accounting to per node. By doing that we have lost a certain precision because those pages might live in different NUMA nodes. In the end NR_KERNEL_STACK_KB exported to the userspace might be over estimated on some nodes while underestimated on others.

This doesn't impose any real problem to correctnes of the kernel behavior as the counter is not used for any internal processing but it can cause some confusion to the userspace.

You have skipped over one part of the changelog I have proposed and that is to provide an actual data.

Quoted 4 lines

Address the problem by accounting each vmalloc backing page to its own node.

Fixes 991e7673859e ("mm: memcontrol: account kernel stack per node")

Fixes tag might make somebody assume this is worth backporting but I highly doubt so.

Quoted 2 lines

Anyway

as the patch is correct with one comment below

Quoted 16 lines

---
Changelog in v2:
- Rework commit log suggested by Michal.

Thanks to Michal and Shakeel for review.

 kernel/fork.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
kernel/fork.c +0 -0
index d66cd1014211..6e2201feb524 100644
@@ -379,14 +379,19 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
 	void *stack = task_stack_page(tsk);
 	struct vm_struct *vm = task_stack_vm_area(tsk);

kernel/fork.c static void account_kernel_stack(struct task_struct *tsk, int account) · line 385

Quoted 10 lines
kernel/fork.c +3 -5
 
+	if (vm) {
+		int i;
 
-	/* All stack pages are in the same node. */
-	if (vm)
-		mod_lruvec_page_state(vm->pages[0], NR_KERNEL_STACK_KB,
-				      account * (THREAD_SIZE / 1024));
-	else
+		BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);

I do not think we need this BUG_ON. What kind of purpose does it serve?

Quoted 14 lines
kernel/fork.c +7 -0
+
+		for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
+			mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB,
+					      account * (PAGE_SIZE / 1024));
+	} else {
+		/* All stack pages are in the same node. */
 		mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB,
 				      account * (THREAD_SIZE / 1024));
+	}
 }
 
 static int memcg_charge_kernel_stack(struct task_struct *tsk)
Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 9 lines · Michal Hocko
Quoted 7 lines · Muchun Song

For simplification 991e7673859e ("mm: memcontrol: account kernel stack per node") has changed the per zone vmalloc backed stack pages accounting to per node. By doing that we have lost a certain precision because those pages might live in different NUMA nodes. In the end NR_KERNEL_STACK_KB exported to the userspace might be over estimated on some nodes while underestimated on others.

Quoted 6 lines
Quoted 3 lines

This doesn't impose any real problem to correctnes of the kernel behavior as the counter is not used for any internal processing but it can cause some confusion to the userspace.

You have skipped over one part of the changelog I have proposed and that is to provide an actual data.

Because this is a problem I found by looking at the code, not a real world problem. I do not have any actual data. :-(

Quoted 8 lines
Quoted 5 lines

Address the problem by accounting each vmalloc backing page to its own node.

Fixes 991e7673859e ("mm: memcontrol: account kernel stack per node")

Fixes tag might make somebody assume this is worth backporting but I highly doubt so.

OK. I can remove the Fixes tag.

Quoted 6 lines
Quoted 3 lines

Anyway

Thanks for your review.

Quoted 23 lines

as the patch is correct with one comment below

Quoted 20 lines

---
Changelog in v2:
- Rework commit log suggested by Michal.

Thanks to Michal and Shakeel for review.

 kernel/fork.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
kernel/fork.c +2 -0
index d66cd1014211..6e2201feb524 100644
@@ -379,14 +379,19 @@ static void account_kernel_stack(struct task_struct *tsk, int account)
      void *stack = task_stack_page(tsk);
      struct vm_struct *vm = task_stack_vm_area(tsk);
 
+     if (vm) {
+             int i;
 
Quoted 8 lines

kernel/fork.c static void account_kernel_stack(struct task_struct *tsk, int account) · line 385

Quoted 6 lines
kernel/fork.c +1 -5
-     /* All stack pages are in the same node. */
-     if (vm)
-             mod_lruvec_page_state(vm->pages[0], NR_KERNEL_STACK_KB,
-                                   account * (THREAD_SIZE / 1024));
-     else
+             BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);

I do not think we need this BUG_ON. What kind of purpose does it serve?

vm->nr_pages should be always equal to THREAD_SIZE / PAGE_SIZE if the system is not corrupted. It makes sense to remove the BUG_ON. I will remove it in the next version. Thanks.

Quoted 19 lines
Quoted 14 lines
kernel/fork.c +7 -1
+
+             for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
+                     mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB,
+                                           account * (PAGE_SIZE / 1024));
+     } else {
+             /* All stack pages are in the same node. */
              mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB,
                                    account * (THREAD_SIZE / 1024));
+     }
 }
 
 static int memcg_charge_kernel_stack(struct task_struct *tsk)
--
Quoted 1 line

2.11.0

--
Michal Hocko
SUSE Labs

reviewer
#3 lore raw
Quoted 14 lines · Muchun Song
Quoted 13 lines · Michal Hocko
Quoted 10 lines · Muchun Song

For simplification 991e7673859e ("mm: memcontrol: account kernel stack per node") has changed the per zone vmalloc backed stack pages accounting to per node. By doing that we have lost a certain precision because those pages might live in different NUMA nodes. In the end NR_KERNEL_STACK_KB exported to the userspace might be over estimated on some nodes while underestimated on others.

This doesn't impose any real problem to correctnes of the kernel behavior as the counter is not used for any internal processing but it can cause some confusion to the userspace.

Quoted 5 lines
Quoted 2 lines

You have skipped over one part of the changelog I have proposed and that is to provide an actual data.

Because this is a problem I found by looking at the code, not a real world problem. I do not have any actual data. :-(

As I've mentioned several times already, this is all fine but it should be made explicit in the changelog. There might be people spending their time to evaluate this code to find out whether this is something that somebody depend on.

[...]

Quoted 11 lines
Quoted 8 lines

kernel/fork.c static void account_kernel_stack(struct task_struct *tsk, int account) · line 385

Quoted 6 lines
kernel/fork.c +1 -5
-     /* All stack pages are in the same node. */
-     if (vm)
-             mod_lruvec_page_state(vm->pages[0], NR_KERNEL_STACK_KB,
-                                   account * (THREAD_SIZE / 1024));
-     else
+             BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);

I do not think we need this BUG_ON. What kind of purpose does it serve?

vm->nr_pages should be always equal to THREAD_SIZE / PAGE_SIZE if the system is not corrupted.

BUG_ON is not an annotation for "this shouldn't happen". Even if the system was corrupted and nr_pages wouldn't match then this is not a reason to crash the kernel right away.

In general there should be a very _strong_ reason to add a BUG_ON.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 1 line · Michal Hocko

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

+ BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE);

I do not think we need this BUG_ON. What kind of purpose does it serve?

Quoted 9 lines
Quoted 2 lines

vm->nr_pages should be always equal to THREAD_SIZE / PAGE_SIZE if the system is not corrupted.

BUG_ON is not an annotation for "this shouldn't happen". Even if the system was corrupted and nr_pages wouldn't match then this is not a reason to crash the kernel right away.

In general there should be a very _strong_ reason to add a BUG_ON.

I agree with Michal. We should remove this BUG_ON or at least convert it into VM_BUG_ON.

Acked-by on v2 1/1
#5 lore raw
In reply to Muchun Song
Quoted 11 lines · Muchun Song

For simplification 991e7673859e ("mm: memcontrol: account kernel stack per node") has changed the per zone vmalloc backed stack pages accounting to per node. By doing that we have lost a certain precision because those pages might live in different NUMA nodes. In the end NR_KERNEL_STACK_KB exported to the userspace might be over estimated on some nodes while underestimated on others.

This doesn't impose any real problem to correctnes of the kernel behavior as the counter is not used for any internal processing but it can cause some confusion to the userspace.

Quoted 6 lines

Address the problem by accounting each vmalloc backing page to its own node.

Fixes 991e7673859e ("mm: memcontrol: account kernel stack per node")

With changes proposed by Shakeel and Michal, this looks good to me.

I guess the BUG_ON() was inspired by memcg_charge_kernel_stack() - not really your fault for following that example. But yeah, please drop it from your patch. Thanks!