rcu_read_lock/unlock protect find_task_by_vpid call
PATCH 1/1 rcu_read_lock/unlock protect find_task_by_vpid call
Commit 4221a9918e38b7494cee341dda7b7b4bb8c04bde "Add RCU check for find_task_by_vpid()" introduced rcu_lockdep_assert to find_task_by_pid_ns. Assertion failed in sys_ioprio_get. The patch is fixing assertion failure in ioprio_set as well.
===================================================
[ INFO: suspicious rcu_dereference_check() usage. ]
---------------------------------------------------
kernel/pid.c:419 invoked rcu_dereference_check() without protection!
rcu_scheduler_active = 1, debug_locks = 0
1 lock held by iotop/4254:
#0: (tasklist_lock){.?.?..}, at: [<ffffffff811104b4>] sys_ioprio_get+0x22/0x2da
stack backtrace: Pid: 4254, comm: iotop Not tainted Call Trace: [<ffffffff810656f2>] lockdep_rcu_dereference+0xaa/0xb2 [<ffffffff81053c67>] find_task_by_pid_ns+0x4f/0x68 [<ffffffff81053c9d>] find_task_by_vpid+0x1d/0x1f [<ffffffff811104e2>] sys_ioprio_get+0x50/0x2da [<ffffffff81002182>] system_call_fastpath+0x16/0x1b
---
fs/ioprio.c +8 -2
@@ -113,8 +113,11 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =set_task_ioprio (p, ioprio);break ;
@@ -202,8 +205,11 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =get_task_ioprio (p);break ;
fs/ioprio.c SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
Quoted 33 lines · Sergey Senozhatsky
Commit
4221a9918e38b7494cee341dda7b7b4bb8c04bde"Add RCU check forfind_task_by_vpid()" introducedrcu_lockdep_asserttofind_task_by_pid_ns. Assertion failed insys_ioprio_get. The patch is fixing assertion failure inioprio_setas well.===================================================
[ INFO: suspiciousrcu_dereference_check()usage. ]
---------------------------------------------------
kernel/pid.c:419invokedrcu_dereference_check()without protection!
rcu_scheduler_active= 1,debug_locks= 0
1 lock held by iotop/4254:
#0: (tasklist_lock){.?.?..}, at: [<ffffffff811104b4>]sys_ioprio_get+0x22/0x2dastack backtrace: Pid: 4254, comm: iotop Not tainted Call Trace: [<ffffffff810656f2>] lockdep_rcu_dereference+0xaa/0xb2 [<ffffffff81053c67>] find_task_by_pid_ns+0x4f/0x68 [<ffffffff81053c9d>] find_task_by_vpid+0x1d/0x1f [<ffffffff811104e2>] sys_ioprio_get+0x50/0x2da [<ffffffff81002182>] system_call_fastpath+0x16/0x1bSigned-off-by Sergey Senozhatsky <[email protected]>---
fs/ioprio.c +0
-0@@ -113,8 +113,11 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
Interesting...
The task-list lock is read-held at this point, which should mean that the PID mapping cannot change. The lockdep_tasklist_lock_is_held() function does lockdep_is_held(&tasklist_lock), which must therefore only be checking for write-holding the lock. The fix would be to make lockdep_tasklist_lock_is_held() check for either read-holding or write-holding tasklist lock.
Or is there some subtle reason that read-holding the tasklist lock is not sufficient?
Thanx, Paul
Quoted 31 lines
fs/ioprio.c +8
-3case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =set_task_ioprio (p, ioprio);break ;@@ -202,8 +205,11 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =get_task_ioprio (p);break ; --To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 44 lines · Paul E. McKenney
fs/ioprio.c SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)
Quoted 33 lines · Sergey Senozhatsky
Commit
4221a9918e38b7494cee341dda7b7b4bb8c04bde"Add RCU check forfind_task_by_vpid()" introducedrcu_lockdep_asserttofind_task_by_pid_ns. Assertion failed insys_ioprio_get. The patch is fixing assertion failure inioprio_setas well.===================================================
[ INFO: suspiciousrcu_dereference_check()usage. ]
---------------------------------------------------
kernel/pid.c:419invokedrcu_dereference_check()without protection!
rcu_scheduler_active= 1,debug_locks= 0
1 lock held by iotop/4254:
#0: (tasklist_lock){.?.?..}, at: [<ffffffff811104b4>]sys_ioprio_get+0x22/0x2dastack backtrace: Pid: 4254, comm: iotop Not tainted Call Trace: [<ffffffff810656f2>] lockdep_rcu_dereference+0xaa/0xb2 [<ffffffff81053c67>] find_task_by_pid_ns+0x4f/0x68 [<ffffffff81053c9d>] find_task_by_vpid+0x1d/0x1f [<ffffffff811104e2>] sys_ioprio_get+0x50/0x2da [<ffffffff81002182>] system_call_fastpath+0x16/0x1bSigned-off-by Sergey Senozhatsky <[email protected]>---
fs/ioprio.c +0
-0@@ -113,8 +113,11 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)Interesting...
The task-list lock is read-held at this point, which should mean that the PID mapping cannot change. The
lockdep_tasklist_lock_is_held()function doeslockdep_is_held(&tasklist_lock), which must therefore only be checking for write-holding the lock. The fix would be to makelockdep_tasklist_lock_is_held()check for either read-holding or write-holding tasklist lock.
Quoted 3 lines
Or is there some subtle reason that read-holding the tasklist lock is not sufficient?
Hello,
On the kernel/pid.c side we have the requirement that find_task_by_vpid -> find_task_by_pid_ns
should be called with rcu_read_lock.
/*
* Must be called under rcu_read_lock().
*/
struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
{
rcu_lockdep_assert(rcu_read_lock_held());
return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
}Should it be changed to (let's say)
struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns) {
- rcu_lockdep_assert(rcu_read_lock_held());
+ rcu_lockdep_assert(rcu_read_lock_held() || lockdep_tasklist_lock_is_held()); return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID); }
Sergey
Quoted 34 lines
Thanx, Paul
Quoted 31 lines
fs/ioprio.c +8
-3case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =set_task_ioprio (p, ioprio);break ;@@ -202,8 +205,11 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =get_task_ioprio (p);break ; --To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 10 lines · Sergey Senozhatsky
Quoted 9 lines · Paul E. McKenney
Interesting...
The task-list lock is read-held at this point, which should mean that the PID mapping cannot change. The
lockdep_tasklist_lock_is_held()function doeslockdep_is_held(&tasklist_lock), which must therefore only be checking for write-holding the lock. The fix would be to makelockdep_tasklist_lock_is_held()check for either read-holding or write-holding tasklist lock.
Quoted 2 lines
Quoted 2 lines
Or is there some subtle reason that read-holding the tasklist lock is not sufficient?
This was discussed in the thread at http://kerneltrap.org/mailarchive/linux-kernel/2009/12/10/4517520 . Quoting from one of posts in that thead http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388
| Usually tasklist gives enough protection, but if copy_process() fails | it calls free_pid() lockless and does call_rcu(delayed_put_pid(). | This means, without rcu lock find_pid_ns() can't scan the hash table | safely.
And now the patch that adds
rcu_lockdep_assert(rcu_read_lock_held());
was merged in accordance with that comment.
Therefore, I thing below change is not good.
Quoted 8 lines
Should it be changed to (let's say)
struct
task_struct*find_task_by_pid_ns(pid_t nr, structpid_namespace*ns) {(quoted diff) +1
-1- rcu_lockdep_assert(rcu_read_lock_held()); + rcu_lockdep_assert(rcu_read_lock_held() || lockdep_tasklist_lock_is_held());return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID); }
Regards.
Quoted 29 lines · Tetsuo Handa
Quoted 10 lines · Sergey Senozhatsky
Quoted 9 lines · Paul E. McKenney
Interesting...
The task-list lock is read-held at this point, which should mean that the PID mapping cannot change. The
lockdep_tasklist_lock_is_held()function doeslockdep_is_held(&tasklist_lock), which must therefore only be checking for write-holding the lock. The fix would be to makelockdep_tasklist_lock_is_held()check for either read-holding or write-holding tasklist lock.Quoted 2 lines
Quoted 2 lines
Or is there some subtle reason that read-holding the tasklist lock is not sufficient?
This was discussed in the thread at http://kerneltrap.org/mailarchive/linux-kernel/2009/12/10/4517520 . Quoting from one of posts in that thead http://kerneltrap.org/mailarchive/linux-kernel/2010/2/8/4536388
| Usually tasklist gives enough protection, but if copy_process() fails | it calls free_pid() lockless and does call_rcu(delayed_put_pid(). | This means, without rcu lock find_pid_ns() can't scan the hash table | safely.And now the patch that adds
rcu_lockdep_assert(rcu_read_lock_held());was merged in accordance with that comment.
Therefore, I thing below change is not good.
Quoted 8 lines
Quoted 8 lines
Should it be changed to (let's say)
struct
task_struct*find_task_by_pid_ns(pid_t nr, structpid_namespace*ns) {(quoted diff) +1
-1- rcu_lockdep_assert(rcu_read_lock_held()); + rcu_lockdep_assert(rcu_read_lock_held() || lockdep_tasklist_lock_is_held());return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID); }
So we should remove the lockdep_tasklist_lock_is_held() and then apply Sergey's patch, correct?
Thanx, Paul
Quoted 2 lines · Paul E. McKenney
So we should remove the
lockdep_tasklist_lock_is_held()and then apply Sergey's patch, correct?
Yes. rcu_lockdep_assert(rcu_read_lock_held()) in find_task_by_pid_ns() is correct and callers need to use rcu_read_lock().
As of 2.6.32, there are 20+ users who missed rcu_read_lock(). So, similar reports will be posted like popcorn.
http://kerneltrap.org/mailarchive/linux-kernel/2009/12/11/4518016
Quoted 10 lines · Tetsuo Handa
Quoted 2 lines · Paul E. McKenney
So we should remove the
lockdep_tasklist_lock_is_held()and then apply Sergey's patch, correct?Yes. rcu_lockdep_assert(rcu_read_lock_held()) in
find_task_by_pid_ns()is correct and callers need to usercu_read_lock().As of 2.6.32, there are 20+ users who missed
rcu_read_lock(). So, similar reports will be posted like popcorn.
http://kerneltrap.org/mailarchive/linux-kernel/2009/12/11/4518016
OK, let's see how these are in 2.6.36... The summary is that almost
all of the popcorn kernels have already popped.
Quoted 3 lines
Users missing
read_lock(&tasklist_lock)when callingfind_task_by_vpid():
get_net_ns_by_pid()innet/core/net_namespace.c
Fixed with rcu_read_lock().
Quoted 1 line
seq_print_userip_objs()inkernel/trace/trace_output.c
Fixed with rcu_read_lock().
Quoted 1 line
fill_pid()inkernel/taskstats.c
This one seems to be named fill_stats_for_pid(), and it is fixed with rcu_read_lock().
Quoted 1 line
fill_tgid()inkernel/taskstats.c
This one seems to be named fill_stats_for_tgid(), and it is fixed with rcu_read_lock().
Quoted 1 line
futex_find_get_task()inkernel/futex.c
Fixed with rcu_read_lock().
Quoted 1 line
SYSCALL_DEFINE3(get_robust_list)inkernel/futex.c
Fixed with rcu_read_lock().
Quoted 1 line
compat_sys_get_robust_list()inkernel/futex_compat.c
Fixed with rcu_read_lock().
Quoted 1 line
ptrace_get_task_struct()inkernel/ptrace.c
Fixed with rcu_read_lock().
Quoted 1 line
do_send_specific()inkernel/signal.c
Fixed with rcu_read_lock().
Quoted 1 line
find_get_context()inkernel/perf_event.c
Fixed with rcu_read_lock() plus code motion.
Quoted 1 line
posix_cpu_clock_get()inkernel/posix-cpu-timers.c
Fixed with rcu_read_lock().
Quoted 1 line
good_sigevent()inkernel/posix-timers.c
Fixed with rcu_read_lock() in caller.
Quoted 1 line
attach_task_by_pid()inkernel/cgroup.c
Fixed with rcu_read_lock().
Quoted 1 line
SYSCALL_DEFINE1(getpgid)inkernel/sys.c
Fixed with rcu_read_lock().
Quoted 1 line
SYSCALL_DEFINE1(getsid)inkernel/sys.c
Fixed with rcu_read_lock().
Quoted 1 line
do_sched_setscheduler()inkernel/sched.c
Fixed with rcu_read_lock().
Quoted 4 lines
Users missing
rcu_read_lock()when callingfind_task_by_vpid():
SYSCALL_DEFINE3(ioprio_set)infs/ioprio.c
Patches in flight.
Quoted 1 line
SYSCALL_DEFINE2(ioprio_get)infs/ioprio.c
Patches in flight.
Quoted 1 line
cap_get_target_pid()inkernel/capability.c
Fixed with rcu_read_lock().
Quoted 1 line
audit_prepare_user_tty()inkernel/audit.c
Fixed with rcu_read_lock().
Quoted 1 line
audit_receive_msg()inkernel/audit.c
Fixed with rcu_read_lock(), two instances.
Quoted 1 line
check_clock()inkernel/posix-cpu-timers.c
This one has read_lock(&tasklist_lock).
Quoted 1 line
posix_cpu_timer_create()inkernel/posix-cpu-timers.c
This one has read_lock(&tasklist_lock).
Quoted 1 line
SYSCALL_DEFINE3(setpriority)inkernel/sys.c
This one has both read_lock(&tasklist_lock) and rcu_read_lock(). Both belt -and- suspenders...
Quoted 1 line
SYSCALL_DEFINE2(getpriority)inkernel/sys.c
Ditto.
Quoted 1 line
SYSCALL_DEFINE2(setpgid)inkernel/sys.c
This one has both write_lock_irq(&tasklist_lock) and rcu_read_lock(). -Serious- belt along with the suspenders...
Quoted 1 line
SYSCALL_DEFINE1(sched_getscheduler)inkernel/sched.c
Fixed with rcu_read_lock().
Quoted 1 line
SYSCALL_DEFINE2(sched_getparam)inkernel/sched.c
Fixed with rcu_read_lock().
Quoted 1 line
sched_setaffinity()inkernel/sched.c
Fixed with rcu_read_lock().
Quoted 1 line
sched_getaffinity()inkernel/sched.c
Fixed with rcu_read_lock().
Quoted 1 line
SYSCALL_DEFINE2(sched_rr_get_interval)inkernel/sched.c
Fixed with rcu_read_lock().
Quoted 1 line
tomoyo_is_select_one()insecurity/tomoyo/common.c
This one has both read_lock(&tasklist_lock) and rcu_read_lock(). Both belt -and- suspenders... (Also renamed to tomoyo_select_one().)
Quoted 1 line
tomoyo_read_pid()insecurity/tomoyo/common.c
This one has both read_lock(&tasklist_lock) and rcu_read_lock(). Both belt -and- suspenders...
Quoted 1 line
SYSCALL_DEFINE6(move_pages)inmm/migrate.c
This one does read_lock(&tasklist_lock).
Quoted 1 line
SYSCALL_DEFINE4(migrate_pages)inmm/mempolicy.c
No call to find_task_by_vpid() in migrate_pages() anymore, but there is one protected by read_lock(&tasklist_lock) in move_pages().
Quoted 1 line
find_process_by_pid()inarch/mips/kernel/mips-mt-fpaff.c
No change, so let's check the callers: mipsmt_sys_sched_setaffinity(): rcu_read_lock(). mipsmt_sys_sched_getaffinity(): read_lock(&tasklist_lock). do_sched_setscheduler(): rcu_read_lock(). sched_getscheduler(): rcu_read_lock(). sched_getparam(): rcu_read_lock(). sched_setaffinity(): rcu_read_lock(). sched_getaffinity(); rcu_read_lock(). sched_rr_get_interval(): rcu_read_lock(). So all of these are protected.
Quoted 1 line
pfm_get_task()inarch/ia64/kernel/perfmon.c
This one does read_lock(&tasklist_lock).
Quoted 1 line
cxn_pin_by_pid()inarch/frv/mm/mmu-context.c
This one does read_lock(&tasklist_lock).
Quoted 4 lines
Users missing
read_lock(&tasklist_lock)when callingfind_task_by_pid_ns():
rest_init()ininit/main.c
Fixed with rcu_read_lock().
Quoted 1 line
proc_pid_lookup()infs/proc/base.c
Fixed with rcu_read_lock().
Quoted 1 line
proc_task_lookup()infs/proc/base.c
Fixed with rcu_read_lock().
Quoted 1 line
first_tid()infs/proc/base.c
Fixed with rcu_read_lock().
Quoted 1 line
getthread()inkernel/kgdb.c
This is now in kernel/debug/gdbstub.c... It looks to need an rcu_read_lock(). It has a comment, but...
Quoted 1 line
mconsole_stack()inarch/um/drivers/mconsole_kern.c
This one still needs help.
Quoted 4 lines
Users missing
rcu_read_lock()when callingfind_task_by_pid_ns():
rest_init()ininit/main.c
Fixed with rcu_read_lock().
Quoted 1 line
getthread()inkernel/kgdb.c
This one still needs help.
Quoted 1 line
mconsole_stack()inarch/um/drivers/mconsole_kern.c
This one still needs help.
So mostly there...
Thanx, Paul
Hello.
Quoted 6 lines · Paul E. McKenney
Quoted 3 lines
Users missing
rcu_read_lock()when callingfind_task_by_vpid():
check_clock()inkernel/posix-cpu-timers.cThis one has
read_lock(&tasklist_lock).
Excuse me. Holding tasklist_lock lock does not help.
We must call rcu_read_lock() explicitly.
That's why 9728e5d6 "kernel/pid.c: update comment on find_task_by_pid_ns" was made.
I think there are users who needlessly call read_lock(&tasklist_lock) when they can use rcu_read_lock() instead.
But I don't know when to use read_lock(&tasklist_lock).
If read_lock(&tasklist_lock) is needed only when we want to access the "struct task_struct" after rcu_read_unlock(), maybe it is cleaner to use a helper like
struct task_struct *find_task_and_get(pid_t pid)
{
struct task_struct *task;
read_lock(&tasklist_lock);
rcu_read_lock();
task = find_task_by_vpid(pid);
rcu_read_unlock();
if (task)
get_task_struct(task);
read_unlock(&tasklist_lock);
return task;
}and hide tasklist_lock.
Regards.
Quoted 5 lines · Tetsuo Handa
Hello.
Quoted 2 lines · Paul E. McKenney
Quoted 2 lines
Users missing
rcu_read_lock()when callingfind_task_by_vpid():
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
check_clock()inkernel/posix-cpu-timers.cThis one has
read_lock(&tasklist_lock).Excuse me. Holding
tasklist_locklock does not help.
We must callrcu_read_lock()explicitly.
That's why9728e5d6"kernel/pid.c: update comment onfind_task_by_pid_ns" was made.
OK, good point, there are a few more kernels of unpopped corn here.
Quoted 21 lines
I think there are users who needlessly call
read_lock(&tasklist_lock)when they can usercu_read_lock()instead.
But I don't know when to useread_lock(&tasklist_lock).If
read_lock(&tasklist_lock)is needed only when we want to access the "structtask_struct" afterrcu_read_unlock(), maybe it is cleaner to use a helper likestruct
task_struct*find_task_and_get(pid_t pid){ struct task_struct *task; read_lock(&tasklist_lock); rcu_read_lock(); task = find_task_by_vpid(pid); rcu_read_unlock(); if (task) get_task_struct(task); read_unlock(&tasklist_lock); return task; }
Quoted 1 line
and hide
tasklist_lock.
This makes a lot of sense to me! That said, most of the current open-coded variants of your find_task_and_get() seem to have the rcu_read_unlock() after the get_task_struct() rather than before. But I don't claim to understand the locking design of this part of the kernel well enough to say which is the best approach.
So, either way, will you be submitting the patches for this?
Thanx, Paul
Sergey Senozhatsky
Reviewed-by Oleg NesterovQuoted 8 lines · Paul E. McKenney
Quoted 7 lines · Tetsuo Handa
Hello.
Quoted 4 lines · Paul E. McKenney
Quoted 3 lines
Users missing
rcu_read_lock()when callingfind_task_by_vpid():
check_clock()inkernel/posix-cpu-timers.c
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
This one has
read_lock(&tasklist_lock).Excuse me. Holding
tasklist_locklock does not help.
We must callrcu_read_lock()explicitly.
That's why9728e5d6"kernel/pid.c: update comment onfind_task_by_pid_ns" was made.OK, good point, there are a few more kernels of unpopped corn here.
Hello,
I prepared a patch for posix-cpu-timers.
[PATCH] posix-cpu-timers: rcu_read_lock/unlock protect find_task_by_vpid call
http://lkml.org/lkml/2010/11/3/257
Sergey
Quoted 33 lines
Quoted 21 lines
I think there are users who needlessly call
read_lock(&tasklist_lock)when they can usercu_read_lock()instead.
But I don't know when to useread_lock(&tasklist_lock).If
read_lock(&tasklist_lock)is needed only when we want to access the "structtask_struct" afterrcu_read_unlock(), maybe it is cleaner to use a helper likestruct
task_struct*find_task_and_get(pid_t pid){ struct task_struct *task; read_lock(&tasklist_lock); rcu_read_lock(); task = find_task_by_vpid(pid); rcu_read_unlock(); if (task) get_task_struct(task); read_unlock(&tasklist_lock); return task; }Quoted 1 line
and hide
tasklist_lock.This makes a lot of sense to me! That said, most of the current open-coded variants of your
find_task_and_get()seem to have thercu_read_unlock()after theget_task_struct()rather than before. But I don't claim to understand the locking design of this part of the kernel well enough to say which is the best approach.So, either way, will you be submitting the patches for this?
Thanx, Paul
Quoted 18 lines · Sergey Senozhatsky
Quoted 8 lines · Paul E. McKenney
Quoted 7 lines · Tetsuo Handa
Hello.
Quoted 4 lines · Paul E. McKenney
Quoted 3 lines
Users missing
rcu_read_lock()when callingfind_task_by_vpid():
check_clock()inkernel/posix-cpu-timers.cQuoted 8 lines
Quoted 5 lines
Quoted 2 lines
This one has
read_lock(&tasklist_lock).Excuse me. Holding
tasklist_locklock does not help.
We must callrcu_read_lock()explicitly.
That's why9728e5d6"kernel/pid.c: update comment onfind_task_by_pid_ns" was made.OK, good point, there are a few more kernels of unpopped corn here.
Quoted 7 lines
Hello,
I prepared a patch for posix-cpu-timers.[PATCH] posix-cpu-timers: rcu_read_lock/unlock protect
find_task_by_vpidcallReviewed-by Oleg Nesterov <[email protected]>
Ah, very good, thank you!!!
Thanx, Paul
Quoted 36 lines
Sergey
Quoted 33 lines
Quoted 21 lines
I think there are users who needlessly call
read_lock(&tasklist_lock)when they can usercu_read_lock()instead.
But I don't know when to useread_lock(&tasklist_lock).If
read_lock(&tasklist_lock)is needed only when we want to access the "structtask_struct" afterrcu_read_unlock(), maybe it is cleaner to use a helper likestruct
task_struct*find_task_and_get(pid_t pid){ struct task_struct *task; read_lock(&tasklist_lock); rcu_read_lock(); task = find_task_by_vpid(pid); rcu_read_unlock(); if (task) get_task_struct(task); read_unlock(&tasklist_lock); return task; }Quoted 1 line
and hide
tasklist_lock.This makes a lot of sense to me! That said, most of the current open-coded variants of your
find_task_and_get()seem to have thercu_read_unlock()after theget_task_struct()rather than before. But I don't claim to understand the locking design of this part of the kernel well enough to say which is the best approach.So, either way, will you be submitting the patches for this?
Thanx, Paul
Quoted 48 lines · Sergey Senozhatsky
Commit
4221a9918e38b7494cee341dda7b7b4bb8c04bde"Add RCU check forfind_task_by_vpid()" introducedrcu_lockdep_asserttofind_task_by_pid_ns. Assertion failed insys_ioprio_get. The patch is fixing assertion failure inioprio_setas well.===================================================
[ INFO: suspiciousrcu_dereference_check()usage. ]
---------------------------------------------------
kernel/pid.c:419invokedrcu_dereference_check()without protection!
rcu_scheduler_active= 1,debug_locks= 0
1 lock held by iotop/4254:
#0: (tasklist_lock){.?.?..}, at: [<ffffffff811104b4>]sys_ioprio_get+0x22/0x2dastack backtrace: Pid: 4254, comm: iotop Not tainted Call Trace: [<ffffffff810656f2>] lockdep_rcu_dereference+0xaa/0xb2 [<ffffffff81053c67>] find_task_by_pid_ns+0x4f/0x68 [<ffffffff81053c9d>] find_task_by_vpid+0x1d/0x1f [<ffffffff811104e2>] sys_ioprio_get+0x50/0x2da [<ffffffff81002182>] system_call_fastpath+0x16/0x1bSigned-off-by Sergey Senozhatsky <[email protected]>---
fs/ioprio.c +4
-1@@ -113,8 +113,11 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =set_task_ioprio (p, ioprio);break ;@@ -202,8 +205,11 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)case IOPRIO_WHO_PROCESS :if (!who)
fs/ioprio.c SYSCALL_DEFINE2(ioprio_get, int, which, int, who) · line 215
Quoted 10 lines
fs/ioprio.c +4
-1p = current; - else + else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =get_task_ioprio (p);break ;
If you add the rcu_read_lock/unlock() sections, we would also need to update the comment above accordingly.
Thanks,
Davidlohr
From: Davidlohr Bueso <[email protected]>
Subject: [PATCH] ioprio: remove comment to not use RCU
--- fs/ioprio.c | 5 ----- 1 files changed, 0 insertions(+), 5 deletions(-)
fs/ioprio.c +0 -0
@@ -103,11 +103,6 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who,
int, ioprio)
}
ret = -ESRCH;
- /*
- * We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic",
- * so we can't use rcu_read_lock(). See re-copy of ->ioprio
- * in copy_process().
- */
read_lock(&tasklist_lock);
switch (which) {
case IOPRIO_WHO_PROCESS:
Signature · 1 line
1.7.1
Quoted 54 lines · Davidlohr Bueso
Quoted 53 lines · Sergey Senozhatsky
Commit
4221a9918e38b7494cee341dda7b7b4bb8c04bde"Add RCU check forfind_task_by_vpid()" introducedrcu_lockdep_asserttofind_task_by_pid_ns. Assertion failed insys_ioprio_get. The patch is fixing assertion failure inioprio_setas well.===================================================
[ INFO: suspiciousrcu_dereference_check()usage. ]
---------------------------------------------------
kernel/pid.c:419invokedrcu_dereference_check()without protection!
rcu_scheduler_active= 1,debug_locks= 0
1 lock held by iotop/4254:
#0: (tasklist_lock){.?.?..}, at: [<ffffffff811104b4>]sys_ioprio_get+0x22/0x2dastack backtrace: Pid: 4254, comm: iotop Not tainted Call Trace: [<ffffffff810656f2>] lockdep_rcu_dereference+0xaa/0xb2 [<ffffffff81053c67>] find_task_by_pid_ns+0x4f/0x68 [<ffffffff81053c9d>] find_task_by_vpid+0x1d/0x1f [<ffffffff811104e2>] sys_ioprio_get+0x50/0x2da [<ffffffff81002182>] system_call_fastpath+0x16/0x1bSigned-off-by Sergey Senozhatsky <[email protected]>---
fs/ioprio.c +6
-2@@ -113,8 +113,11 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =set_task_ioprio (p, ioprio);break ;@@ -202,8 +205,11 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who);
Quoted 9 lines
fs/ioprio.c SYSCALL_DEFINE2(ioprio_get, int, which, int, who) · line 215
Quoted 5 lines
fs/ioprio.c +2
-0+rcu_read_unlock (); + }if (p) ret =get_task_ioprio (p);break ;If you add the rcu_read_lock/unlock() sections, we would also need to update the comment above accordingly.
Hello,
I think, this comment is relevant to IOPRIO_WHO_PGRP/IOPRIO_WHO_USER cases. I only touched IOPRIO_WHO_PROCESS and IOPRIO_WHO_PROCESS.
So, imho, no need to remove it.
Sergey
Quoted 26 lines
From: Davidlohr Bueso <[email protected]>
Subject: [PATCH] ioprio: remove comment to not use RCUSigned-off-by Davidlohr Bueso <[email protected]>--- fs/ioprio.c | 5 ----- 1 files changed, 0 insertions(+), 5 deletions(-)fs/ioprio.c +0
-5@@ -103,11 +103,6 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio) } ret = -ESRCH ; -/* -* We want IOPRIO_WHO_PGRP/IOPRIO_WHO_USER to be "atomic", -* so we can't use rcu_read_lock(). See re-copy of ->ioprio -* in copy_process(). -*/ read_lock (&tasklist_lock);switch (which) {case
IOPRIO_WHO_PROCESS:
Quoted 56 lines · Sergey Senozhatsky
Quoted 55 lines · Davidlohr Bueso
Quoted 54 lines · Sergey Senozhatsky
Commit
4221a9918e38b7494cee341dda7b7b4bb8c04bde"Add RCU check forfind_task_by_vpid()" introducedrcu_lockdep_asserttofind_task_by_pid_ns. Assertion failed insys_ioprio_get. The patch is fixing assertion failure inioprio_setas well.===================================================
[ INFO: suspiciousrcu_dereference_check()usage. ]
---------------------------------------------------
kernel/pid.c:419invokedrcu_dereference_check()without protection!
rcu_scheduler_active= 1,debug_locks= 0
1 lock held by iotop/4254:
#0: (tasklist_lock){.?.?..}, at: [<ffffffff811104b4>]sys_ioprio_get+0x22/0x2dastack backtrace: Pid: 4254, comm: iotop Not tainted Call Trace: [<ffffffff810656f2>] lockdep_rcu_dereference+0xaa/0xb2 [<ffffffff81053c67>] find_task_by_pid_ns+0x4f/0x68 [<ffffffff81053c9d>] find_task_by_vpid+0x1d/0x1f [<ffffffff811104e2>] sys_ioprio_get+0x50/0x2da [<ffffffff81002182>] system_call_fastpath+0x16/0x1bSigned-off-by Sergey Senozhatsky <[email protected]>---
fs/ioprio.c +7
-2@@ -113,8 +113,11 @@ SYSCALL_DEFINE3(ioprio_set, int, which, int, who, int, ioprio)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock (); + }if (p) ret =set_task_ioprio (p, ioprio);break ;@@ -202,8 +205,11 @@ SYSCALL_DEFINE2(ioprio_get, int, which, int, who)case IOPRIO_WHO_PROCESS :if (!who) p = current; -else +else { +rcu_read_lock (); p =find_task_by_vpid (who); +rcu_read_unlock ();
Quoted 8 lines
Quoted 8 lines
fs/ioprio.c SYSCALL_DEFINE2(ioprio_get, int, which, int, who) · line 215
Quoted 4 lines
fs/ioprio.c +1
-0+ }if (p) ret =get_task_ioprio (p);break ;If you add the rcu_read_lock/unlock() sections, we would also need to update the comment above accordingly.
Ah, yes indeed, misread the cases, sorry about the noise.
Thanks,
Davidlohr