pid: annotate data-races around pid_ns->pid_allocated
Subsystems (4)
PATCH 1/1 pid: annotate data-races around pid_ns->pid_allocated
Suppress syzbot reports by annotating these accesses using READ_ONCE() / WRITE_ONCE().
--- kernel/fork.c | 2 +- kernel/pid.c | 7 ++++--- kernel/pid_namespace.c | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-)
kernel/fork.c +1 -1
@@ -2584,7 +2584,7 @@ __latent_entropy struct task_struct *copy_process(rseq_fork (p, clone_flags);/* Don't start children in a dying pid namespace */ -if (unlikely (!(ns_of_pid (pid)->pid_allocated &PIDNS_ADDING ))) { +if (unlikely (!(READ_ONCE (ns_of_pid (pid)->pid_allocated) &PIDNS_ADDING ))) { retval = -ENOMEM ; goto bad_fork_core_free; }
kernel/pid.c +4 -3
@@ -122,7 +122,8 @@ void free_pid(struct pid *pid)for (i =0 ; i <= pid->level; i++) {struct upid *upid = pid->numbers + i;struct pid_namespace *ns = upid->ns; -switch (--ns->pid_allocated) { +WRITE_ONCE (ns->pid_allocated,READ_ONCE (ns->pid_allocated) -1 ); +switch (READ_ONCE (ns->pid_allocated)) {case 2 :case 1 :/* When all that is left in the pid namespace
@@ -271,13 +272,13 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, upid = pid->numbers + ns->level;idr_preload (GFP_KERNEL );spin_lock (&pidmap_lock); -if (!(ns->pid_allocated &PIDNS_ADDING )) +if (!(READ_ONCE (ns->pid_allocated) &PIDNS_ADDING )) goto out_unlock;pidfs_add_pid (pid);for ( ; upid >= pid->numbers; --upid) {/* Make the PID visible to find_pid_ns. */ idr_replace (&upid->ns->idr, pid, upid->nr); - upid->ns->pid_allocated++; +WRITE_ONCE (upid->ns->pid_allocated,READ_ONCE (upid->ns->pid_allocated) +1 ); }spin_unlock (&pidmap_lock);idr_preload_end ();
kernel/pid_namespace.c +1 -1
@@ -268,7 +268,7 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)*/ for (;;) {set_current_state (TASK_INTERRUPTIBLE ); -if (pid_ns->pid_allocated == init_pids) +if (READ_ONCE (pid_ns->pid_allocated) == init_pids)break ;schedule (); }
Signature · 1 line
2.47.1
Quoted 3 lines · Jiayuan Chen
Suppress syzbot reports by annotating these accesses using
READ_ONCE()/WRITE_ONCE().
...
kernel/pid.c void free_pid(struct pid *pid) · line 126
Quoted 9 lines
kernel/pid.c +2
-1@@ -122,7 +122,8 @@ void free_pid(struct pid *pid)for (i =0 ; i <= pid->level; i++) {struct upid *upid = pid->numbers + i;struct pid_namespace *ns = upid->ns; -switch (--ns->pid_allocated) { +WRITE_ONCE (ns->pid_allocated,READ_ONCE (ns->pid_allocated) -1 ); +switch (READ_ONCE (ns->pid_allocated)) {
I keep forgetting how kcsan works, but we don't need READ_ONCE(ns->pid_allocated) under pidmap_lock?
Same for other functions which read/modify ->pid_allocated with this lock held.
Oleg.
Quoted 33 lines · Oleg Nesterov
Quoted 5 lines · Jiayuan Chen
Suppress syzbot reports by annotating these accesses using
READ_ONCE()/WRITE_ONCE()....
Quoted 11 lines
---
a/kernel/pid.c+++
b/kernel/pid.c@@ -122,7 +122,8 @@ void
free_pid(struct pid *pid)for (i = 0; i <=
pid->level; i++) {struct upid *upid =
pid->numbers+ i;Quoted 8 lines
struct
pid_namespace*ns =upid->ns;- switch (--ns->pid_allocated) {
+
WRITE_ONCE(ns->pid_allocated, READ_ONCE(ns->pid_allocated) - 1);+ switch (
READ_ONCE(ns->pid_allocated)) {I keep forgetting how kcsan works, but we don't need
Quoted 8 lines
READ_ONCE(ns->pid_allocated)underpidmap_lock?Same for other functions which read/modify ->pid_allocated with
this lock held.
Oleg.
However, not all places that read/write pid_allocated are locked, for example:
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/pid_namespace.c#n271
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/fork.c#n2602
So, in fact, the pidmap_lock is not effective. And if we were to add locks to all these places, it would be too heavy.
There's no actual impact on usage without locks, so I think it might be more suitable to add these macros, KASAN can recognize READ_ONCE and WRITE_ONCE and suppress warnings.
Thanks.
Quoted 11 lines · Jiayuan Chen
However, not all places that read/write
pid_allocatedare locked, for example:
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/pid_namespace.c#n271
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/fork.c#n2602So, in fact, the
pidmap_lockis not effective. And if we were to add locks to all these places, it would be too heavy.There's no actual impact on usage without locks, so I think it might be more suitable to add these macros, KASAN can recognize
READ_ONCEandWRITE_ONCEand suppress warnings.
Wouldn't it be nicer to add data_race() to mark those places where the race (presumably) doesn't matter? (Instead of _ONCE'ing places that are under the lock.)
0.02€,
Michal
Quoted 47 lines · Jiayuan Chen
Quoted 33 lines · Oleg Nesterov
Quoted 5 lines · Jiayuan Chen
Suppress syzbot reports by annotating these accesses using
READ_ONCE()/WRITE_ONCE()....
Quoted 11 lines
---
a/kernel/pid.c+++
b/kernel/pid.c@@ -122,7 +122,8 @@ void
free_pid(struct pid *pid)for (i = 0; i <=
pid->level; i++) {struct upid *upid =
pid->numbers+ i;Quoted 8 lines
struct
pid_namespace*ns =upid->ns;- switch (--ns->pid_allocated) {
+
WRITE_ONCE(ns->pid_allocated, READ_ONCE(ns->pid_allocated) - 1);+ switch (
READ_ONCE(ns->pid_allocated)) {I keep forgetting how kcsan works, but we don't need
Quoted 8 lines
READ_ONCE(ns->pid_allocated)underpidmap_lock?Same for other functions which read/modify ->pid_allocated with
this lock held.
Oleg.
Quoted 7 lines
However, not all places that read/write
pid_allocatedare locked, for example:
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/pid_namespace.c#n271
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/fork.c#n2602So, in fact, the
pidmap_lockis not effective. And if we were to add locks to all these places, it would be too heavy.
It seems you misunderstood me. I didn't argue with the lockless READ_ONCE()s outside of pidmap_lock.
Oleg.
Quoted 53 lines · Oleg Nesterov
Quoted 52 lines · Jiayuan Chen
Quoted 33 lines · Oleg Nesterov
Quoted 5 lines · Jiayuan Chen
Suppress syzbot reports by annotating these accesses using
READ_ONCE()/WRITE_ONCE()....
Quoted 11 lines
---
a/kernel/pid.c+++
b/kernel/pid.c@@ -122,7 +122,8 @@ void
free_pid(struct pid *pid)for (i = 0; i <=
pid->level; i++) {struct upid *upid =
pid->numbers+ i;Quoted 8 lines
struct
pid_namespace*ns =upid->ns;- switch (--ns->pid_allocated) {
+
WRITE_ONCE(ns->pid_allocated, READ_ONCE(ns->pid_allocated) - 1);+ switch (
READ_ONCE(ns->pid_allocated)) {I keep forgetting how kcsan works, but we don't need
Quoted 8 lines
READ_ONCE(ns->pid_allocated)underpidmap_lock?Same for other functions which read/modify ->pid_allocated with
this lock held.
Oleg.
However, not all places that read/write
pid_allocatedare locked, for example:
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/pid_namespace.c#n271
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/fork.c#n2602
Quoted 5 lines
Quoted 2 lines
So, in fact, the
pidmap_lockis not effective. And if we were to add locks to all these places, it would be too heavy.It seems you misunderstood me. I didn't argue with the lockless READ_ONCE()s outside of
pidmap_lock.
Agreed. We should only add those annotations where they're really needed (someone once taught me ;).
Quoted 104 lines · Christian Brauner
Quoted 101 lines · Oleg Nesterov
On 04/23, Jiayuan Chen wrote:
Quoted 74 lines · Oleg Nesterov
Quoted 9 lines · Jiayuan Chen
Suppress syzbot reports by annotating these accesses using
READ_ONCE()/WRITE_ONCE()....
Quoted 30 lines
---
a/kernel/pid.c+++
b/kernel/pid.c(quoted diff) +0
-0@@ -122,7 +122,8 @@ void free_pid(struct pid *pid) for (i = 0; i <= pid->level; i++) {struct upid *upid =
pid->numbers+ i;struct
pid_namespace*ns =upid->ns;- switch (--ns->pid_allocated) {
Quoted 7 lines
+
WRITE_ONCE(ns->pid_allocated, READ_ONCE(ns->pid_allocated) - 1);+ switch (
READ_ONCE(ns->pid_allocated)) {I keep forgetting how kcsan works, but we don't need
READ_ONCE(ns->pid_allocated)underpidmap_lock?Same for other functions which read/modify ->pid_allocated with
Quoted 7 lines
this lock held.
Oleg.
However, not all places that read/write
pid_allocatedare locked,for example:
https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/fork.c#n2602
So, in fact, the
pidmap_lockis not effective. And if we were to add locksto all these places, it would be too heavy.
Quoted 9 lines
Quoted 4 lines
It seems you misunderstood me. I didn't argue with the lockless READ_ONCE()s
outside of
pidmap_lock.Agreed. We should only add those annotations where they're really
needed (someone once taught me ;).
Thank you for your suggestion, it make sense to me.