Skip to contentSkip to conversation

cpuset: fix allocating page cache/slab object on the unallowed node when memory spread is set

PATCH Applied Miao Xie 6 participants 24 messages Jan 21, 2009 – Feb 12, 2009

Subsystems (5)
lore
Share & tools
lore

Miao Xie

author
#0 lore raw

PATCH 1/1 cpuset: fix allocating page cache/slab object on the unallowed node when memory spread is set

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes. We must update the mem_allowed_list of the tasks in time.

Slab has the same problem.

We fixes the bug by updating tasks' mem_allowed_list and spread flag after its cpuset's mems or spread flag is changed.

---
 include/linux/cpuset.h |    4 -
 include/linux/sched.h  |    1 -
 init/main.c            |    3 +-
 kernel/cpuset.c        |  204 +++++++++++++++--------------------------------
 kernel/kthread.c       |    1 +
 mm/mempolicy.c         |   12 ---
 mm/page_alloc.c        |    5 +-
 7 files changed, 69 insertions(+), 161 deletions(-)
include/linux/cpuset.h +0 -4
index 90c6074..c8155a6 100644
@@ -17,7 +17,6 @@
 
 extern int number_of_cpusets;	/* How many cpusets are defined in system? */
 
-extern int cpuset_init_early(void);
 extern int cpuset_init(void);
 extern void cpuset_init_smp(void);
 extern void cpuset_cpus_allowed(struct task_struct *p, struct cpumask *mask);
@@ -26,7 +25,6 @@ extern void cpuset_cpus_allowed_locked(struct task_struct *p,
 extern nodemask_t cpuset_mems_allowed(struct task_struct *p);
 #define cpuset_current_mems_allowed (current->mems_allowed)
 void cpuset_init_current_mems_allowed(void);
-void cpuset_update_task_memory_state(void);
 int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask);
 
 extern int __cpuset_zone_allowed_softwall(struct zone *z, gfp_t gfp_mask);
@@ -83,7 +81,6 @@ extern void cpuset_print_task_mems_allowed(struct task_struct *p);
 
 #else /* !CONFIG_CPUSETS */
 
-static inline int cpuset_init_early(void) { return 0; }
 static inline int cpuset_init(void) { return 0; }
 static inline void cpuset_init_smp(void) {}
 
@@ -105,7 +102,6 @@ static inline nodemask_t cpuset_mems_allowed(struct task_struct *p)
 
 #define cpuset_current_mems_allowed (node_states[N_HIGH_MEMORY])
 static inline void cpuset_init_current_mems_allowed(void) {}
-static inline void cpuset_update_task_memory_state(void) {}
 
 static inline int cpuset_nodemask_valid_mems_allowed(nodemask_t *nodemask)
 {
include/linux/sched.h +0 -1
index 4cae9b8..2c9a93c 100644
@@ -1332,7 +1332,6 @@ struct task_struct {
 #endif
 #ifdef CONFIG_CPUSETS
 	nodemask_t mems_allowed;
-	int cpuset_mems_generation;
 	int cpuset_mem_spread_rotor;
 #endif
 #ifdef CONFIG_CGROUPS
init/main.c +2 -1
index 8442094..3ce3b5d 100644
@@ -635,7 +635,6 @@ asmlinkage void __init start_kernel(void)
 #endif
 	vmalloc_init();
 	vfs_caches_init_early();
-	cpuset_init_early();
 	page_cgroup_init();
 	mem_init();
 	enable_debug_pagealloc();
@@ -845,6 +844,8 @@ static int __init kernel_init(void * unused)
 	 */
 	init_pid_ns.child_reaper = current;
 
+	current->mems_allowed = node_possible_map;
+
 	cad_pid = task_pid(current);
 
 	smp_prepare_cpus(setup_max_cpus);
kernel/cpuset.c +65 -139

349 lines not on this page · raw message

kernel/kthread.c +1 -0
index 4fbc456..90469e6 100644
@@ -242,6 +242,7 @@ int kthreadd(void *unused)
 	set_user_nice(tsk, KTHREAD_NICE_LEVEL);
 	set_cpus_allowed_ptr(tsk, CPU_MASK_ALL_PTR);
 
+	current->mems_allowed = node_possible_map;
 	current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
 
 	for (;;) {
mm/mempolicy.c +0 -12
index 3eb4a6f..5912b03 100644
@@ -222,10 +222,6 @@ static struct mempolicy *mpol_new(unsigned short mode, unsigned short flags,
 	policy->flags = flags;
 
 	if (nodes) {
-		/*
-		 * cpuset related setup doesn't apply to local allocation
-		 */
-		cpuset_update_task_memory_state();
 		if (flags & MPOL_F_RELATIVE_NODES)
 			mpol_relative_nodemask(&cpuset_context_nmask, nodes,
 					       &cpuset_current_mems_allowed);
@@ -674,7 +670,6 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask,
 	struct vm_area_struct *vma = NULL;
 	struct mempolicy *pol = current->mempolicy;
 
-	cpuset_update_task_memory_state();
 	if (flags &
 		~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED))
 		return -EINVAL;
@@ -1545,8 +1540,6 @@ alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr)
 	struct mempolicy *pol = get_vma_policy(current, vma, addr);
 	struct zonelist *zl;
 
-	cpuset_update_task_memory_state();
-
 	if (unlikely(pol->mode == MPOL_INTERLEAVE)) {
 		unsigned nid;
 
@@ -1585,16 +1578,11 @@ alloc_page_vma(gfp_t gfp, struct vm_area_struct *vma, unsigned long addr)
  *	interrupt context and apply the current process NUMA policy.
  *	Returns NULL when no page can be allocated.
  *
- *	Don't call cpuset_update_task_memory_state() unless
- *	1) it's ok to take cpuset_sem (can WAIT), and
- *	2) allocating for current task (not interrupt).
  */
 struct page *alloc_pages_current(gfp_t gfp, unsigned order)
 {
 	struct mempolicy *pol = current->mempolicy;
 
-	if ((gfp & __GFP_WAIT) && !in_interrupt())
-		cpuset_update_task_memory_state();
 	if (!pol || in_interrupt() || (gfp & __GFP_THISNODE))
 		pol = &default_policy;
 
mm/page_alloc.c +1 -4
index 5675b30..503219c 100644
@@ -1573,10 +1573,7 @@ nofail_alloc:
 
 	/* We now go into synchronous reclaim */
 	cpuset_memory_pressure_bump();
-	/*
-	 * The task's cpuset might have expanded its set of allowable nodes
-	 */
-	cpuset_update_task_memory_state();
+
 	p->flags |= PF_MEMALLOC;
 	reclaim_state.reclaimed_slab = 0;
 	p->reclaim_state = &reclaim_state;
Signature · 1 line
1.6.0.3
#1 lore raw
Quoted 10 lines · On Wednesday 21 January 2009 19:06:20 Miao Xie wrote:

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes. We must update the mem_allowed_list of the tasks in time.

Slab has the same problem.

We fixes the bug by updating tasks' mem_allowed_list and spread flag after its cpuset's mems or spread flag is changed.

Thank you, this is a much better way to handle the problem, and keeps those costly function calls out of fastpaths.

I don't know the cpuset code well enough to review it right now, but the mm/ pieces thank you kindly for it :) Nice patch.

#2 lore raw
In reply to Miao Xie
Quoted 4 lines · Miao Xie

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes.

Can you give a more concrete example of how the current code can break?

Quoted 1 line

We must update the mem_allowed_list of the tasks in time.

This is a fairly fundamental change to the way that mems_allowed is handled - it dates back to fairly early in cpusets' history.

Quoted 4 lines

- * The task_struct fields mems_allowed and mems_generation may only
- * be accessed in the context of that task, so require no locks.
+ * The task_struct fields mems_allowed may only be accessed in the context
+ * of that task, so require no locks.

This comment is no longer true, since mems_allowed is now being updated by other processes.

What's to stop a task reading current->mems_allowed and racing with an update from update_tasks_nodemask() ? Or else, why can that not lead to badness?

Paul

maintainer Applied
#4 lore raw
In reply to Miao Xie
Quoted 17 lines · On Wed, 21 Jan 2009 16:06:20 +0800 Miao Xie <[email protected]> wrote:

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes. We must update the mem_allowed_list of the tasks in time.

Slab has the same problem.

We fixes the bug by updating tasks' mem_allowed_list and spread flag after its cpuset's mems or spread flag is changed.

...

kernel/kthread.c int kthreadd(void *unused) · line 246

Quoted 5 lines
kernel/kthread.c +1 -0
@@ -242,6 +242,7 @@ int kthreadd(void *unused)
 	set_user_nice(tsk, KTHREAD_NICE_LEVEL);
 	set_cpus_allowed_ptr(tsk, CPU_MASK_ALL_PTR);
 
+	current->mems_allowed = node_possible_map;
 	current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;

Why this change? kthreadd() is called from rest_init(), before anyone has had a chance to alter ->mems_allowed?

I queued the patch pending your response to Paul's questions, thanks.

#5 lore raw

What exactly is breaking? You do a memory spread in a set of nodes determined by the cpuset? Or how do you restrict the nodes that memory spread is allowed to cycle through?

author
#3 lore raw
In reply to Paul Menage
Quoted 7 lines · on 2009-1-21 18:41 Paul Menage wrote:
Quoted 4 lines · Miao Xie

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes.

Can you give a more concrete example of how the current code can break?

I'm sorry for replying late.

A test program which reproduces the problem on current kernel is attached. This program just repeats reading the file "DATAFILE" after receiving SIGUSR1 and exits after receiving SIGINT.

	Test Task
	  sigsuspend();
	  while(!end) {
		read DATAFILE;
		sigsuspend();
	  }

Steps to reproduce:

# mkdir /dev/cpuset
# mount -t cpuset xxx /dev/cpuset
# mkdir /dev/cpuset/0
# echo 0 > /dev/cpuset/0/cpus
# echo 1-2 > /dev/cpuset/0/mems
# echo 1 > /dev/cpuset/0/memory_spread_page
# dd if=/dev/zero of=DATAFILE bs=4096 count=20480	<- create a 80M file
# ./mem-hog &
# tst_pid=$!
# cat /proc/$tst_pid/status
...
Mems_allowed:   00000000,00000007
Mems_allowed_list:      0-2
...
# echo $tst_pid > /dev/cpuset/0/tasks
# echo 3 > /proc/sys/vm/drop_caches
# find /sys/devices/system/node/ -name "meminfo" -exec cat {} + | grep "FilePages"
Node 0 FilePages:         15488 kB
Node 1 FilePages:             0 kB
Node 2 FilePages:             0 kB
# cat /proc/$tst_pid/status
...
Mems_allowed:   00000000,00000007		<- old memory list
Mems_allowed_list:      0-2
...
# kill -s sigusr1 $tst_pid
	...wait a moment...
# find /sys/devices/system/node/ -name "meminfo" -exec cat {} + | grep "FilePages"
Node 0 FilePages:         97548 kB
Node 1 FilePages:             0 kB
Node 2 FilePages:             0 kB
# cat /proc/$tst_pid/status
...
Mems_allowed:   00000000,00000007               <- old memory list
Mems_allowed_list:      0-2
...
Quoted 4 lines
Quoted 1 line

We must update the mem_allowed_list of the tasks in time.

This is a fairly fundamental change to the way that mems_allowed is handled - it dates back to fairly early in cpusets' history.

I found Mr. Paul Jackson had discussed it with somebody:

http://lkml.org/lkml/2004/8/11/211

According to what Mr. Paul Jackson said, my patch has a serious bug. Maybe we fix this bug just by choosing a good callsite for cpuset_update_task_memory_state().

Thanks!
Miao

Quoted 5 lines
Quoted 4 lines

- * The task_struct fields mems_allowed and mems_generation may only
- * be accessed in the context of that task, so require no locks.
+ * The task_struct fields mems_allowed may only be accessed in the context
+ * of that task, so require no locks.

Quoted 11 lines

This comment is no longer true, since mems_allowed is now being updated by other processes.

What's to stop a task reading current->mems_allowed and racing with an update from update_tasks_nodemask() ? Or else, why can that not lead to badness?

Paul

/* gcc mem-hog.c -g -o mem-hog -Wall -Wextra */

#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <signal.h>
#include <fcntl.h>

#define UNUSED __attribute__ ((unused))
#define BUFFER_SIZE 1024

volatile int end;

void sighandler1(UNUSED int signo)
{
}

void sigint(UNUSED int signo)

{
	end = 1;
}

int page_cache_hog(void)

{
	int fd = -1;
	char buff[BUFFER_SIZE];
	char path[BUFFER_SIZE];
	int ret = 0;

	sprintf(path, "%s", "DATAFILE");
	fd = open(path, O_RDONLY);
	if (fd == -1) {
		warn("open %s failed", path);
		return -1;
	}

	while ((ret = read(fd, buff, sizeof(buff))) > 0);
	if (ret == -1)
		warn("read %s failed", path);

	close(fd);
	return ret;
}

int mem_hog(void)

{
	sigset_t sigset;
	int ret = 0;

	if (sigemptyset(&sigset) < 0)
		err(1, "sigemptyset()");
	sigsuspend(&sigset);

	while (!end) {
		ret = page_cache_hog();

		sigsuspend(&sigset);
	}

	return ret;
}

int main(UNUSED int argc, UNUSED char *argv[])

{
	struct sigaction sa1, sa2;

	sa1.sa_handler = sighandler1;
	if (sigemptyset(&sa1.sa_mask) < 0)
		err(1, "sigemptyset()");

	sa1.sa_flags = 0;
	if (sigaction(SIGUSR1, &sa1, NULL) < 0)
		err(1, "sigaction()");

	sa2.sa_handler = sigint;
	if (sigemptyset(&sa2.sa_mask) < 0)
		err(1, "sigemptyset()");

	sa2.sa_flags = 0;
	if (sigaction(SIGINT, &sa2, NULL) < 0)
		err(1, "sigaction()");

	return mem_hog();
}
author
#6 lore raw
In reply to Andrew Morton
Quoted 23 lines · on 2009-1-28 6:42 Andrew Morton wrote:
Quoted 20 lines · On Wed, 21 Jan 2009 16:06:20 +0800 Miao Xie <[email protected]> wrote:

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes. We must update the mem_allowed_list of the tasks in time.

Slab has the same problem.

We fixes the bug by updating tasks' mem_allowed_list and spread flag after its cpuset's mems or spread flag is changed.

...

kernel/kthread.c +0 -0
@@ -242,6 +242,7 @@ int kthreadd(void *unused)
 	set_user_nice(tsk, KTHREAD_NICE_LEVEL);
 	set_cpus_allowed_ptr(tsk, CPU_MASK_ALL_PTR);
 
Quoted 5 lines

kernel/kthread.c int kthreadd(void *unused) · line 246

Quoted 2 lines
kernel/kthread.c +1 -0
+	current->mems_allowed = node_possible_map;
 	current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;

Why this change? kthreadd() is called from rest_init(), before anyone has had a chance to alter ->mems_allowed?

I found that after mems_allowed of kthreadd was not initialized applying this patch, every bit of it is 1, so...
Maybe it is redundant.

Thanks!
Miao

Quoted 5 lines

I queued the patch pending your response to Paul's questions, thanks.

maintainer
#7 lore raw
Quoted 30 lines · On Tue, 03 Feb 2009 11:25:25 +0800 Miao Xie <[email protected]> wrote:
Quoted 23 lines · on 2009-1-28 6:42 Andrew Morton wrote:
Quoted 20 lines · On Wed, 21 Jan 2009 16:06:20 +0800 Miao Xie <[email protected]> wrote:

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes. We must update the mem_allowed_list of the tasks in time.

Slab has the same problem.

We fixes the bug by updating tasks' mem_allowed_list and spread flag after its cpuset's mems or spread flag is changed.

...

kernel/kthread.c +0 -0
@@ -242,6 +242,7 @@ int kthreadd(void *unused)
 	set_user_nice(tsk, KTHREAD_NICE_LEVEL);
 	set_cpus_allowed_ptr(tsk, CPU_MASK_ALL_PTR);
 
Quoted 5 lines

kernel/kthread.c int kthreadd(void *unused) · line 246

Quoted 2 lines
kernel/kthread.c +1 -0
+	current->mems_allowed = node_possible_map;
 	current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;

Why this change? kthreadd() is called from rest_init(), before anyone has had a chance to alter ->mems_allowed?

Quoted 3 lines

I found that after mems_allowed of kthreadd was not initialized applying this patch, every bit of it is 1, so...
Maybe it is redundant.

I think it is redundant. kthreadd's mems_allowed _should_ be all-ones. Or at least, all-nodes-allowed.

I wasn't able to find out where the setting of init'smems_allowed happens, after a bit of grepping and hunting. It should be done within INIT_TASK, but isn't.

Still, kthreadd is reliably parented by swapper, and there shold be no need to alter its mems_allowed.

Similarly, what was the reason for setting current->mems_allowed in kernel_init()? That also should be unneeded.

Finally, I've somewhat lost track of where we are with this patch. Paul, do you see any other remaining issues?

#8 lore raw
Quoted 44 lines · Andrew Morton
Quoted 30 lines · On Tue, 03 Feb 2009 11:25:25 +0800 Miao Xie <[email protected]> wrote:
Quoted 23 lines · on 2009-1-28 6:42 Andrew Morton wrote:
Quoted 20 lines · On Wed, 21 Jan 2009 16:06:20 +0800 Miao Xie <[email protected]> wrote:

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes. We must update the mem_allowed_list of the tasks in time.

Slab has the same problem.

We fixes the bug by updating tasks' mem_allowed_list and spread flag after its cpuset's mems or spread flag is changed.

...

kernel/kthread.c +0 -0
@@ -242,6 +242,7 @@ int kthreadd(void *unused)
   set_user_nice(tsk, KTHREAD_NICE_LEVEL);
   set_cpus_allowed_ptr(tsk, CPU_MASK_ALL_PTR);
 
Quoted 5 lines

kernel/kthread.c int kthreadd(void *unused) · line 246

Quoted 2 lines
kernel/kthread.c +1 -0
+  current->mems_allowed = node_possible_map;
   current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;

Why this change? kthreadd() is called from rest_init(), before anyone has had a chance to alter ->mems_allowed?

Quoted 3 lines

I found that after mems_allowed of kthreadd was not initialized applying this patch, every bit of it is 1, so...
Maybe it is redundant.

I think it is redundant. kthreadd's mems_allowed _should_ be all-ones. Or at least, all-nodes-allowed.

I wasn't able to find out where the setting of init'smems_allowed happens, after a bit of grepping and hunting. It should be done within INIT_TASK, but isn't.

Quoted 8 lines

Still, kthreadd is reliably parented by swapper, and there shold be no need to alter its mems_allowed.

Similarly, what was the reason for setting current->mems_allowed in kernel_init()? That also should be unneeded.

Finally, I've somewhat lost track of where we are with this patch. Paul, do you see any other remaining issues?

AFAICS this patch still has a race between a thread reading its mems_allowed, and another thread updating it. The current architecture of having task->mems_allowed be only updatable by current was PaulJ's code originally, and I'm a bit loathe to touch it. But if we're going to, we'll need at the minimum to add a lock for any code that touches current->mems_allowed.

Paul

author
#23 lore raw
In reply to Andrew Morton
Quoted 35 lines · on 2009-2-4 6:16 Andrew Morton wrote:
Quoted 24 lines · On Tue, 03 Feb 2009 11:25:25 +0800 Miao Xie <[email protected]> wrote:
Quoted 23 lines · on 2009-1-28 6:42 Andrew Morton wrote:
Quoted 20 lines · On Wed, 21 Jan 2009 16:06:20 +0800 Miao Xie <[email protected]> wrote:

The task still allocated the page caches on old node after modifying its cpuset's mems when 'memory_spread_page' was set, it is caused by the old mem_allowed_list of the task, the current kernel doesn't updates it unless some function invokes cpuset_update_task_memory_state(), it is too late sometimes. We must update the mem_allowed_list of the tasks in time.

Slab has the same problem.

We fixes the bug by updating tasks' mem_allowed_list and spread flag after its cpuset's mems or spread flag is changed.

...

kernel/kthread.c +0 -0
@@ -242,6 +242,7 @@ int kthreadd(void *unused)
 	set_user_nice(tsk, KTHREAD_NICE_LEVEL);
 	set_cpus_allowed_ptr(tsk, CPU_MASK_ALL_PTR);
 
Quoted 7 lines
Quoted 4 lines

kernel/kthread.c int kthreadd(void *unused) · line 246

Quoted 2 lines
kernel/kthread.c +1 -0
+	current->mems_allowed = node_possible_map;
 	current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;

Why this change? kthreadd() is called from rest_init(), before anyone has had a chance to alter ->mems_allowed?

I found that after mems_allowed of kthreadd was not initialized applying this patch, every bit of it is 1, so...
Maybe it is redundant.

Quoted 6 lines

I think it is redundant. kthreadd's mems_allowed _should_ be all-ones. Or at least, all-nodes-allowed.

I wasn't able to find out where the setting of init'smems_allowed happens, after a bit of grepping and hunting. It should be done within INIT_TASK, but isn't.

I found it. Call trace is following:
start_kernel()
->build_all_zonelists()

      ->cpuset_init_current_mems_allowed()
          ->nodes_setall(current->mems_allowed);

then, it is inherited by kthreadd.

Quoted 3 lines

Still, kthreadd is reliably parented by swapper, and there shold be no need to alter its mems_allowed.

but it is strange to users because there are not so many nodes in the system in fact, so I think using node_possible_map to set init's mems_allowed is better.

Quoted 2 lines

Similarly, what was the reason for setting current->mems_allowed in kernel_init()? That also should be unneeded.

The same as above.

Quoted 7 lines

Finally, I've somewhat lost track of where we are with this patch. Paul, do you see any other remaining issues?

author
#9 lore raw
In reply to Paul Menage
Quoted 3 lines · on 2009-2-4 6:49 Paul Menage wrote:
Quoted 2 lines · Andrew Morton

On Tue, 03 Feb 2009 11:25:25 +0800
Miao Xie <[email protected]> wrote:

[snip]

Quoted 14 lines
Quoted 5 lines

I wasn't able to find out where the setting of init'smems_allowed happens, after a bit of grepping and hunting. It should be done within INIT_TASK, but isn't.

Quoted 8 lines

Still, kthreadd is reliably parented by swapper, and there shold be no need to alter its mems_allowed.

Similarly, what was the reason for setting current->mems_allowed in kernel_init()? That also should be unneeded.

Finally, I've somewhat lost track of where we are with this patch. Paul, do you see any other remaining issues?

Quoted 6 lines

AFAICS this patch still has a race between a thread reading its mems_allowed, and another thread updating it. The current architecture of having task->mems_allowed be only updatable by current was PaulJ's code originally, and I'm a bit loathe to touch it. But if we're going to, we'll need at the minimum to add a lock for any code that touches current->mems_allowed.

Agree! But mems_allowed is touched in the module of memory management in general, adding a lock to protect mems_allowed may lead to performance regression.

Quoted 5 lines

Paul

#10 lore raw
Quoted 11 lines · Miao Xie
Quoted 6 lines

AFAICS this patch still has a race between a thread reading its mems_allowed, and another thread updating it. The current architecture of having task->mems_allowed be only updatable by current was PaulJ's code originally, and I'm a bit loathe to touch it. But if we're going to, we'll need at the minimum to add a lock for any code that touches current->mems_allowed.

Agree! But mems_allowed is touched in the module of memory management in general, adding a lock to protect mems_allowed may lead to performance regression.

I've asked Andrew to remove this from -mm until we can figure out:

- whether it's needed
- how to do it safely.

Basically the problem that you're seeing is that when you move a task into a cpuset that has memory_spread_page=1, it fails to call cpuset_update_task_memory_state()?

One thing missing from your test - can you verify that the test program really did get migrated into the child cpuset? If it failed (and echo failed to report the error) that would explain what you're seeing.

Paul

#11 lore raw
Quoted 8 lines · On Saturday 07 February 2009 06:19:27 Paul Menage wrote:
Quoted 7 lines · Miao Xie
Quoted 6 lines

AFAICS this patch still has a race between a thread reading its mems_allowed, and another thread updating it. The current architecture of having task->mems_allowed be only updatable by current was PaulJ's code originally, and I'm a bit loathe to touch it. But if we're going to, we'll need at the minimum to add a lock for any code that touches current->mems_allowed.

Quoted 8 lines
Quoted 3 lines

Agree! But mems_allowed is touched in the module of memory management in general, adding a lock to protect mems_allowed may lead to performance regression.

I've asked Andrew to remove this from -mm until we can figure out:

- whether it's needed
- how to do it safely.

Is it a problem if mems_allowed can get sampled in an unsafe way? It will happen only quite rarely. This code seems to be far simpler and more robust than the current fragile scheme, so it would be nice to use it.

#12 lore raw
Quoted 2 lines · Nick Piggin

Is it a problem if mems_allowed can get sampled in an unsafe way?

It could cause an OOM to be incorrectly triggered if the task didn't see all the mems that it was meant to have access to. (In the extreme case, it could see no mems at all).

Quoted 3 lines

It will happen only quite rarely. This code seems to be far simpler and more robust than the current fragile scheme, so it would be nice to use it.

Agreed, the existing task->mems_allowed / cpuset_update_memory_state() code is a bit unwieldy. It's pretty much all inherited from the original cpusets code.

A nicer way to do it might be:

- get rid of task->mems_allowed entirely

- have cpuset->mems_allowed be a pointer to an immutable RCU-protected nodemask (so updating the nodemask for a cpuset would always replace it with a fresh one and RCU-free the old one)

- make cpuset_zone_allowed_*() use rcu_read_lock() and just check *(task_cs(current)->mems_allowed) rather than current->mems_allowed

- ensure that get_page_from_freelist() is also RCU-safe (right now I think it's not since cpuset_zone_allowed_softwall() can sleep, but I think cgroups/cpusets is sufficiently RCU-safe now that we could quite likely remove the mutex lock from cpuset_zone_allowed_*()

- add an rcu_read_lock() in hugetlb.c:cpuset_mems_nr()

- figure out where the mempolicy updates currently done in cpuset_update_task_memory_state() need to occur - this is part of the code that I'm pretty fuzzy on. (Maybe we can just copy the bits from Miao's patch for this?)

Anything else?

Paul

#13 lore raw
Quoted 6 lines · On Tuesday 10 February 2009 22:37:28 Paul Menage wrote:
Quoted 1 line · Nick Piggin

Is it a problem if mems_allowed can get sampled in an unsafe way?

It could cause an OOM to be incorrectly triggered if the task didn't see all the mems that it was meant to have access to. (In the extreme case, it could see no mems at all).

You can't be overly worried about concurrency cases. We're talking about two threads here, one chancing mems_allowed, and the other performing an allocation.

It would be possible, depending on timing, for the allocating thread to see either pre or post mems_allowed even if access was fully locked.

The only difference is that a partially changed mems_allowed could be seen. But what does this really mean? Some combination of the new and the old nodes. I don't think this is too much of a problem.

Quoted 19 lines
Quoted 3 lines

It will happen only quite rarely. This code seems to be far simpler and more robust than the current fragile scheme, so it would be nice to use it.

Agreed, the existing task->mems_allowed / cpuset_update_memory_state() code is a bit unwieldy. It's pretty much all inherited from the original cpusets code.

A nicer way to do it might be:

- get rid of task->mems_allowed entirely

- have cpuset->mems_allowed be a pointer to an immutable RCU-protected nodemask (so updating the nodemask for a cpuset would always replace it with a fresh one and RCU-free the old one)

- make cpuset_zone_allowed_*() use rcu_read_lock() and just check *(task_cs(current)->mems_allowed) rather than current->mems_allowed

Quoted 6 lines

- ensure that get_page_from_freelist() is also RCU-safe (right now I think it's not since cpuset_zone_allowed_softwall() can sleep, but I think cgroups/cpusets is sufficiently RCU-safe now that we could quite likely remove the mutex lock from cpuset_zone_allowed_*()

- add an rcu_read_lock() in hugetlb.c:cpuset_mems_nr()

This could work if we *really* need an atomic snapshot of mems_allowed. seqcount synchronisation would be an alternative too that could allow sleeping more easily than SRCU (OTOH if you don't need sleeping, then RCU should be faster than seqcount).

But I'm not convinced we do need this to be atomic.

Quoted 4 lines

- figure out where the mempolicy updates currently done in cpuset_update_task_memory_state() need to occur - this is part of the code that I'm pretty fuzzy on. (Maybe we can just copy the bits from Miao's patch for this?)

Basically we want to push all that into the sites where the memory policy is actually changed. So yes they should all go away.

#14 lore raw
Quoted 3 lines · Nick Piggin

It would be possible, depending on timing, for the allocating thread to see either pre or post mems_allowed even if access was fully locked.

Right - seeing either the pre set or the post set is fine.

Quoted 4 lines

The only difference is that a partially changed mems_allowed could be seen. But what does this really mean? Some combination of the new and the old nodes. I don't think this is too much of a problem.

But if the old and new nodes are disjoint, that could lead to seeing no nodes.

Also, having the results of cpuset_zone_allowed() and cpuset_current_mems_allowed change at random times over the course of a call to alloc_pages() might cause interesting effects (e.g. we make progress freeing pages from one set of nodes, and then call get_page_from_freelist() on a different set of nodes).

Quoted 7 lines

This could work if we *really* need an atomic snapshot of mems_allowed. seqcount synchronisation would be an alternative too that could allow sleeping more easily than SRCU (OTOH if you don't need sleeping, then RCU should be faster than seqcount).

But I'm not convinced we do need this to be atomic.

It's possible that I'm being overly-paranoid here. The decision to make mems_allowed updates be purely pulled by the task itself predates my involvement with cpusets code by a long time. Paul Jackson (CC'd) may have opinions here, but I suspect his sgi.com email address no longer works, and I don't have any more recent address for him.

Paul

#15 lore raw
Quoted 1 line · On Thursday 12 February 2009 12:19:11 Paul Menage wrote:

On Wed, Feb 11, 2009 at 4:54 PM, Nick Piggin <[email protected]>

Quoted 11 lines · wrote:
Quoted 2 lines

It would be possible, depending on timing, for the allocating thread to see either pre or post mems_allowed even if access was fully locked.

Right - seeing either the pre set or the post set is fine.

Quoted 3 lines

The only difference is that a partially changed mems_allowed could be seen. But what does this really mean? Some combination of the new and the old nodes. I don't think this is too much of a problem.

But if the old and new nodes are disjoint, that could lead to seeing no nodes.

Well we could structure updates as setting all new allowed nodes, then clearing newly disallowed ones.

Quoted 5 lines

Also, having the results of cpuset_zone_allowed() and cpuset_current_mems_allowed change at random times over the course of a call to alloc_pages() might cause interesting effects (e.g. we make progress freeing pages from one set of nodes, and then call get_page_from_freelist() on a different set of nodes).

But again, is this really a problem? We're talking about a tiny possibility in a very uncommon case anyway when the cpuset is changing.

If it can cause an outright error like OOM of course that's no good, but if it just requires us to go around the reclaim loop or allocate from another zone... I don't think that's so bad.

Quoted 10 lines
Quoted 6 lines

This could work if we *really* need an atomic snapshot of mems_allowed. seqcount synchronisation would be an alternative too that could allow sleeping more easily than SRCU (OTOH if you don't need sleeping, then RCU should be faster than seqcount).

But I'm not convinced we do need this to be atomic.

It's possible that I'm being overly-paranoid here. The decision to make mems_allowed updates be purely pulled by the task itself predates my involvement with cpusets code by a long time.

It's not such a bad model, but the problem with it is that it needs to be carefully spread over the VM, and in fastpaths too. Now if it were something really critical, fine, but I'm hoping we can do without.

#16 lore raw
Quoted 6 lines · Nick Piggin
Quoted 2 lines

But if the old and new nodes are disjoint, that could lead to seeing no nodes.

Well we could structure updates as setting all new allowed nodes, then clearing newly disallowed ones.

OK, good point.

Paul

author
#21 lore raw
In reply to Paul Menage
Quoted 23 lines · on 2009-2-12 9:19 Paul Menage wrote:
Quoted 2 lines · Nick Piggin

It would be possible, depending on timing, for the allocating thread to see either pre or post mems_allowed even if access was fully locked.

Right - seeing either the pre set or the post set is fine.

Quoted 3 lines

The only difference is that a partially changed mems_allowed could be seen. But what does this really mean? Some combination of the new and the old nodes. I don't think this is too much of a problem.

But if the old and new nodes are disjoint, that could lead to seeing no nodes.

Also, having the results of cpuset_zone_allowed() and cpuset_current_mems_allowed change at random times over the course of a call to alloc_pages() might cause interesting effects (e.g. we make progress freeing pages from one set of nodes, and then call get_page_from_freelist() on a different set of nodes).

Quoted 5 lines

This could work if we *really* need an atomic snapshot of mems_allowed. seqcount synchronisation would be an alternative too that could allow sleeping more easily than SRCU (OTOH if you don't need sleeping, then RCU should be faster than seqcount).

Quoted 7 lines
Quoted 1 line

But I'm not convinced we do need this to be atomic.

It's possible that I'm being overly-paranoid here. The decision to make mems_allowed updates be purely pulled by the task itself predates my involvement with cpusets code by a long time. Paul Jackson (CC'd) may have opinions here, but I suspect his sgi.com email address no longer works, and I don't have any more recent address for him.

I think it's [email protected](CC'd).

Author: Paul Jackson <[email protected]>
Date:   Fri Oct 3 15:23:42 2008 -0700

cpusets: remove pj from cpuset maintainers

Remove myself from the kernel MAINTAINERS file for cpusets. I am leaving SGI and probably will not be active in Linux kernel work. I can be reached at <[email protected]>. Contact Derek Fults <[email protected]> for future SGI+cpuset related issues. I'm off to the next chapter of this good life.

Thanks!
Miao

Quoted 5 lines

Paul

author
#17 lore raw
In reply to Nick Piggin
Quoted 7 lines · on 2009-2-12 9:55 Nick Piggin wrote:
Quoted 1 line · On Thursday 12 February 2009 12:19:11 Paul Menage wrote:

On Wed, Feb 11, 2009 at 4:54 PM, Nick Piggin <[email protected]>

Quoted 4 lines · wrote:
Quoted 2 lines

It would be possible, depending on timing, for the allocating thread to see either pre or post mems_allowed even if access was fully locked.

Right - seeing either the pre set or the post set is fine.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The only difference is that a partially changed mems_allowed could be seen. But what does this really mean? Some combination of the new and the old nodes. I don't think this is too much of a problem.

But if the old and new nodes are disjoint, that could lead to seeing no nodes.

Well we could structure updates as setting all new allowed nodes, then clearing newly disallowed ones.

But it still has the other problem. such as:
Task1 Task2
get_page_from_freelist() while(1) {

{
	for_each_zone_zonelist_nodemask() {
								change Task1's mems_allowed
		if (!cpuset_zone_allowed_softwall())
			goto try_next_zone;
try_next_zone:
		...
	}
}							}

In the extreme case, Task1 will be completely unable to allocate memory at worst. At least, it will lead to the delay of allocate pages. Though the probability of this case is very low, we have to take into account.

Thanks!
Miao

Quoted 36 lines
Quoted 7 lines

Also, having the results of cpuset_zone_allowed() and cpuset_current_mems_allowed change at random times over the course of a call to alloc_pages() might cause interesting effects (e.g. we make progress freeing pages from one set of nodes, and then call get_page_from_freelist() on a different set of nodes).

But again, is this really a problem? We're talking about a tiny possibility in a very uncommon case anyway when the cpuset is changing.

If it can cause an outright error like OOM of course that's no good, but if it just requires us to go around the reclaim loop or allocate from another zone... I don't think that's so bad.

Quoted 9 lines
Quoted 6 lines

This could work if we *really* need an atomic snapshot of mems_allowed. seqcount synchronisation would be an alternative too that could allow sleeping more easily than SRCU (OTOH if you don't need sleeping, then RCU should be faster than seqcount).

But I'm not convinced we do need this to be atomic.

It's possible that I'm being overly-paranoid here. The decision to make mems_allowed updates be purely pulled by the task itself predates my involvement with cpusets code by a long time.

It's not such a bad model, but the problem with it is that it needs to be carefully spread over the VM, and in fastpaths too. Now if it were something really critical, fine, but I'm hoping we can do without.

author
#19 lore raw
In reply to Nick Piggin
Quoted 7 lines · on 2009-2-12 9:55 Nick Piggin wrote:
Quoted 1 line · On Thursday 12 February 2009 12:19:11 Paul Menage wrote:

On Wed, Feb 11, 2009 at 4:54 PM, Nick Piggin <[email protected]>

Quoted 4 lines · wrote:
Quoted 2 lines

It would be possible, depending on timing, for the allocating thread to see either pre or post mems_allowed even if access was fully locked.

Right - seeing either the pre set or the post set is fine.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The only difference is that a partially changed mems_allowed could be seen. But what does this really mean? Some combination of the new and the old nodes. I don't think this is too much of a problem.

But if the old and new nodes are disjoint, that could lead to seeing no nodes.

Well we could structure updates as setting all new allowed nodes, then clearing newly disallowed ones.

But it still has the other problem. such as:
Task1 Task2
get_page_from_freelist() while(1) {

{
	for_each_zone_zonelist_nodemask() {
								change Task1's mems_allowed
		if (!cpuset_zone_allowed_softwall())
			goto try_next_zone;
try_next_zone:
		...
	}
}							}

In the extreme case, Task1 will be completely unable to allocate memory at worst. At least, it will lead to the delay of allocate pages. Though the probability of this case is very low, we have to take into account.

Thanks!
Miao

Quoted 36 lines
Quoted 7 lines

Also, having the results of cpuset_zone_allowed() and cpuset_current_mems_allowed change at random times over the course of a call to alloc_pages() might cause interesting effects (e.g. we make progress freeing pages from one set of nodes, and then call get_page_from_freelist() on a different set of nodes).

But again, is this really a problem? We're talking about a tiny possibility in a very uncommon case anyway when the cpuset is changing.

If it can cause an outright error like OOM of course that's no good, but if it just requires us to go around the reclaim loop or allocate from another zone... I don't think that's so bad.

Quoted 9 lines
Quoted 6 lines

This could work if we *really* need an atomic snapshot of mems_allowed. seqcount synchronisation would be an alternative too that could allow sleeping more easily than SRCU (OTOH if you don't need sleeping, then RCU should be faster than seqcount).

But I'm not convinced we do need this to be atomic.

It's possible that I'm being overly-paranoid here. The decision to make mems_allowed updates be purely pulled by the task itself predates my involvement with cpusets code by a long time.

It's not such a bad model, but the problem with it is that it needs to be carefully spread over the VM, and in fastpaths too. Now if it were something really critical, fine, but I'm hoping we can do without.

#20 lore raw
Quoted 33 lines · On Thursday 12 February 2009 19:27:16 Miao Xie wrote:
Quoted 13 lines · on 2009-2-12 9:55 Nick Piggin wrote:
Quoted 1 line · On Thursday 12 February 2009 12:19:11 Paul Menage wrote:

On Wed, Feb 11, 2009 at 4:54 PM, Nick Piggin <[email protected]>

Quoted 9 lines · wrote:
Quoted 2 lines

It would be possible, depending on timing, for the allocating thread to see either pre or post mems_allowed even if access was fully locked.

Right - seeing either the pre set or the post set is fine.

Quoted 3 lines

The only difference is that a partially changed mems_allowed could be seen. But what does this really mean? Some combination of the new and the old nodes. I don't think this is too much of a problem.

Quoted 5 lines
Quoted 2 lines

But if the old and new nodes are disjoint, that could lead to seeing no nodes.

Well we could structure updates as setting all new allowed nodes, then clearing newly disallowed ones.

But it still has the other problem. such as:
Task1 Task2
get_page_from_freelist() while(1) {

{
	for_each_zone_zonelist_nodemask() {
								change Task1's mems_allowed
		if (!cpuset_zone_allowed_softwall())
			goto try_next_zone;
try_next_zone:
		...
	}
}							}
Quoted 3 lines

In the extreme case, Task1 will be completely unable to allocate memory at worst. At least, it will lead to the delay of allocate pages. Though the probability of this case is very low, we have to take into account.

Hmm, but a task with that permission could kill task1 in a number of ways. Is it really worth worrying about?

#18 lore raw
In reply to Miao Xie
Quoted 4 lines · Miao Xie

In the extreme case, Task1 will be completely unable to allocate memory at worst. At least, it will lead to the delay of allocate pages. Though the probability of this case is very low, we have to take into account.

As Nick says, this is basically maliciousness on the part of (privileged) Task2. I don't think it's a situation we need to even try to deal with (other than not oopsing, and doing the right thing once Task2 stops being an ass).

Paul