pidfs: convert rb-tree to rhashtable
Christian Brauner
maintainerRFC PATCH 1/1 pidfs: convert rb-tree to rhashtable
Convert the pidfs inode-to-pid mapping from an rb-tree with seqcount protection to an rhashtable. This removes the global pidmap_lock contention from pidfs_ino_get_pid() lookups and allows the hashtable insert to happen outside the pidmap_lock.
pidfs_add_pid() is split. pidfs_prepare_pid() allocates inode number and initializes pid fields and is called inside pidmap_lock. pidfs_add_pid() inserts pid into rhashtable and is called outside pidmap_lock. Insertion into the rhashtable can fail and memory allocation may happen so we need to drop the spinlock.
The hashtable removal is deferred to the RCU callback to ensure safe concurrent lookups. To guard against accidently opening an already reaped task pidfs_ino_get_pid() uses additional checks beyond pid_vnr(). If pid->attr is PIDFS_PID_DEAD or NULL the pid either never had a pidfd or it already went through pidfs_exit() aka the process as already reaped. If pid->attr is valid check PIDFS_ATTR_BIT_EXIT to figure out whether the task has exited. Switch to refcount_inc_not_zero() to ensure that the pid isn't about to be freed.
This slightly changes visibility semantics: pidfd creation is denied after pidfs_exit() runs, which is just before the pid number is removed from the via free_pid(). That should not be an issue though.
I haven't perfed this and I would like to make this Mateusz problem...
--- fs/pidfs.c | 107 ++++++++++++++++++++++++++++---------------------- include/linux/pid.h | 4 +- include/linux/pidfs.h | 3 +- kernel/pid.c | 19 ++++++--- 4 files changed, 79 insertions(+), 54 deletions(-)
fs/pidfs.c +61 -46
@@ -21,6 +21,7 @@#include <linux/utsname.h> #include <net/net_namespace.h> #include <linux/coredump.h> +#include <linux/rhashtable.h> #include <linux/xattr.h> #include "internal.h"
@@ -55,7 +56,23 @@ struct pidfs_attr {__u32 coredump_signal; }; -static struct rb_root pidfs_ino_tree =RB_ROOT ; +static struct rhashtable pidfs_ino_ht; + +static int pidfs_ino_ht_cmp (struct rhashtable_compare_arg *arg,const void *pidp) +{ +const u64 *ino = arg->key; +const struct pid *pid = pidp; + +return pid->ino != *ino; +} + +static const struct rhashtable_params pidfs_ino_ht_params = { + .key_offset = offsetof(struct pid , ino), + .key_len =sizeof (u64), + .head_offset = offsetof(struct pid , pidfs_hash), + .obj_cmpfn = pidfs_ino_ht_cmp, + .automatic_shrinking = true, +};#if BITS_PER_LONG ==32 static inline unsigned long pidfs_ino (u64 ino)
@@ -84,21 +101,11 @@ static inline u32 pidfs_gen(u64 ino) }#endif -static int pidfs_ino_cmp (struct rb_node *a,const struct rb_node *b) -{ - struct pid *pid_a = rb_entry(a, struct pid, pidfs_node); - struct pid *pid_b = rb_entry(b, struct pid, pidfs_node); -u64 pid_ino_a = pid_a->ino; -u64 pid_ino_b = pid_b->ino; - -if (pid_ino_a < pid_ino_b) -return -1 ; -if (pid_ino_a > pid_ino_b) -return 1 ; -return 0 ; -} - -void pidfs_add_pid (struct pid *pid) +/* +* Allocate inode number and initialize pidfs fields. +* Called with pidmap_lock held. +*/ +void pidfs_prepare_pid (struct pid *pid) {static u64 pidfs_ino_nr =2 ;
@@ -134,17 +141,23 @@ void pidfs_add_pid(struct pid *pid) pid->stashed =NULL ; pid->attr =NULL ; pidfs_ino_nr++; +} -write_seqcount_begin (&pidmap_lock_seq); -rb_find_add_rcu (&pid->pidfs_node, &pidfs_ino_tree, pidfs_ino_cmp); -write_seqcount_end (&pidmap_lock_seq); +/* +* Insert pid into the pidfs hashtable. +* Must be called without holding pidmap_lock (can allocate memory). +* Returns 0 on success, negative error on failure. +*/ +int pidfs_add_pid (struct pid *pid) +{ +return rhashtable_insert_fast (&pidfs_ino_ht, &pid->pidfs_hash, + pidfs_ino_ht_params); }void pidfs_remove_pid (struct pid *pid) { -write_seqcount_begin (&pidmap_lock_seq); -rb_erase (&pid->pidfs_node, &pidfs_ino_tree); -write_seqcount_end (&pidmap_lock_seq); +rhashtable_remove_fast (&pidfs_ino_ht, &pid->pidfs_hash, + pidfs_ino_ht_params); }void pidfs_free_pid (struct pid *pid)
@@ -773,43 +786,42 @@ static int pidfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,return FILEID_KERNFS ; } -static int pidfs_ino_find (const void *key,const struct rb_node *node) -{ -const u64 pid_ino = *(u64 *)key; - const struct pid *pid = rb_entry(node, struct pid, pidfs_node); - -if (pid_ino < pid->ino) -return -1 ; -if (pid_ino > pid->ino) -return 1 ; -return 0 ; -} -/* Find a struct pid based on the inode number. */ static struct pid *pidfs_ino_get_pid (u64 ino) {struct pid *pid; -struct rb_node *node; -unsigned int seq; +struct pidfs_attr *attr;guard (rcu)(); -do { - seq =read_seqcount_begin (&pidmap_lock_seq); - node =rb_find_rcu (&ino, &pidfs_ino_tree, pidfs_ino_find); -if (node) -break ; - }while (read_seqcount_retry (&pidmap_lock_seq, seq)); -if (!node) + pid =rhashtable_lookup (&pidfs_ino_ht, &ino, pidfs_ino_ht_params); +if (!pid)return NULL ; - pid = rb_entry(node, struct pid, pidfs_node); -/* Within our pid namespace hierarchy? */ if (pid_vnr (pid) ==0 )return NULL ; -return get_pid (pid); +/* +* If attr is NULL the pid is still in the IDR but never had +* a pidfd. If attr is an error the pid went through pidfs_exit() +* and is about to be removed. Either way, deny access. +*/ + attr =READ_ONCE (pid->attr); +if (IS_ERR_OR_NULL (attr)) +return NULL ; + +/* +* If PIDFS_ATTR_BIT_EXIT is set the task has exited and we +* should not allow new file handle lookups. +*/ +if (test_bit (PIDFS_ATTR_BIT_EXIT , &attr->attr_mask)) +return NULL ; + +if (!refcount_inc_not_zero (&pid->count)) +return NULL ; + +return pid; } static struct dentry *pidfs_fh_to_dentry(struct super_block *sb,
@@ -1086,6 +1098,9 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)void __init pidfs_init (void ) { +if (rhashtable_init (&pidfs_ino_ht, &pidfs_ino_ht_params)) +panic ("Failed to initialize pidfs hashtable" ); + pidfs_attr_cachep =kmem_cache_create ("pidfs_attr_cache" ,sizeof (struct pidfs_attr ),0 , (SLAB_HWCACHE_ALIGN |SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT |SLAB_PANIC ),NULL );
include/linux/pid.h +2 -2
@@ -6,6 +6,7 @@#include <linux/rculist.h> #include <linux/rcupdate.h> #include <linux/refcount.h> +#include <linux/rhashtable-types.h> #include <linux/sched.h> #include <linux/wait.h>
@@ -60,7 +61,7 @@ struct pid {spinlock_t lock;struct {u64 ino; -struct rb_node pidfs_node; +struct rhash_head pidfs_hash;struct dentry *stashed;struct pidfs_attr *attr; };
@@ -73,7 +74,6 @@ struct pid {struct upid numbers[]; }; -extern seqcount_spinlock_t pidmap_lock_seq;extern struct pid init_struct_pid;struct file ;
include/linux/pidfs.h +2 -1
@@ -6,7 +6,8 @@ struct coredump_params;struct file *pidfs_alloc_file (struct pid *pid,unsigned int flags);void __init pidfs_init (void ); -void pidfs_add_pid (struct pid *pid); +void pidfs_prepare_pid (struct pid *pid); +int pidfs_add_pid (struct pid *pid);void pidfs_remove_pid (struct pid *pid);void pidfs_exit (struct task_struct *tsk);#ifdef CONFIG_COREDUMP
kernel/pid.c +14 -5
@@ -43,7 +43,6 @@#include <linux/sched/task.h> #include <linux/idr.h> #include <linux/pidfs.h> -#include <linux/seqlock.h> #include <net/sock.h> #include <uapi/linux/pidfd.h>
@@ -85,7 +84,6 @@ struct pid_namespace init_pid_ns = {EXPORT_SYMBOL_GPL (init_pid_ns);static __cacheline_aligned_in_smp DEFINE_SPINLOCK (pidmap_lock ); -seqcount_spinlock_t pidmap_lock_seq =SEQCNT_SPINLOCK_ZERO (pidmap_lock_seq, &pidmap_lock);void put_pid (struct pid *pid) {
@@ -106,6 +104,7 @@ EXPORT_SYMBOL_GPL(put_pid);static void delayed_put_pid (struct rcu_head *rhp) { struct pid *pid = container_of(rhp, struct pid, rcu); +pidfs_remove_pid (pid);put_pid (pid); }
@@ -141,7 +140,6 @@ void free_pid(struct pid *pid)idr_remove (&ns->idr, upid->nr); } -pidfs_remove_pid (pid);spin_unlock (&pidmap_lock);call_rcu (&pid->rcu, delayed_put_pid);
@@ -315,7 +313,14 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, retval = -ENOMEM ;if (unlikely (!(ns->pid_allocated &PIDNS_ADDING ))) goto out_free; -pidfs_add_pid (pid); +pidfs_prepare_pid (pid); +spin_unlock (&pidmap_lock); + + retval =pidfs_add_pid (pid); +if (retval) + goto out_free_idr; + +spin_lock (&pidmap_lock);for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {/* Make the PID visible to find_pid_ns. */ idr_replace (&upid->ns->idr, pid, upid->nr);
@@ -328,6 +333,11 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,return pid; out_free: +spin_unlock (&pidmap_lock); +out_free_idr: +idr_preload_end (); + +spin_lock (&pidmap_lock);while (++i <= ns->level) { upid = pid->numbers + i;idr_remove (&upid->ns->idr, upid->nr);
@@ -338,7 +348,6 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,idr_set_cursor (&ns->idr,0 );spin_unlock (&pidmap_lock); -idr_preload_end (); out_abort:put_pid_ns (ns);
---
base-commit: f54c7e54d2de2d7b58aa54604218a6fc00bb2e77
change-id: 20260119-work-pidfs-rhashtable-9d14071bd77a
Quoted 289 lines · Christian Brauner
Convert the pidfs inode-to-pid mapping from an rb-tree with seqcount protection to an rhashtable. This removes the global
pidmap_lockcontention frompidfs_ino_get_pid()lookups and allows the hashtable insert to happen outside thepidmap_lock.
pidfs_add_pid()is split.pidfs_prepare_pid()allocates inode number and initializes pid fields and is called insidepidmap_lock.pidfs_add_pid()inserts pid into rhashtable and is called outsidepidmap_lock. Insertion into the rhashtable can fail and memory allocation may happen so we need to drop the spinlock.The hashtable removal is deferred to the RCU callback to ensure safe concurrent lookups. To guard against accidently opening an already reaped task
pidfs_ino_get_pid()uses additional checks beyondpid_vnr(). Ifpid->attrisPIDFS_PID_DEADor NULL the pid either never had a pidfd or it already went throughpidfs_exit()aka the process as already reaped. Ifpid->attris valid checkPIDFS_ATTR_BIT_EXITto figure out whether the task has exited. Switch torefcount_inc_not_zero()to ensure that the pid isn't about to be freed.This slightly changes visibility semantics: pidfd creation is denied after
pidfs_exit()runs, which is just before the pid number is removed from the viafree_pid(). That should not be an issue though.I haven't perfed this and I would like to make this Mateusz problem...
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 107 ++++++++++++++++++++++++++++---------------------- include/linux/pid.h | 4 +- include/linux/pidfs.h | 3 +- kernel/pid.c | 19 ++++++--- 4 files changed, 79 insertions(+), 54 deletions(-)fs/pidfs.c +61
-46@@ -21,6 +21,7 @@#include <linux/utsname.h> #include <net/net_namespace.h> #include <linux/coredump.h> +#include <linux/rhashtable.h> #include <linux/xattr.h> #include "internal.h" @@ -55,7 +56,23 @@ struct pidfs_attr {__u32 coredump_signal; }; -static struct rb_root pidfs_ino_tree =RB_ROOT ; +static struct rhashtable pidfs_ino_ht; + +static int pidfs_ino_ht_cmp (struct rhashtable_compare_arg *arg,const void *pidp) +{ +const u64 *ino = arg->key; +const struct pid *pid = pidp; + +return pid->ino != *ino; +} + +static const struct rhashtable_params pidfs_ino_ht_params = { + .key_offset = offsetof(struct pid , ino), + .key_len =sizeof (u64), + .head_offset = offsetof(struct pid , pidfs_hash), + .obj_cmpfn = pidfs_ino_ht_cmp, + .automatic_shrinking = true, +};#if BITS_PER_LONG ==32 static inline unsigned long pidfs_ino (u64 ino)@@ -84,21 +101,11 @@ static inline u32 pidfs_gen(u64 ino) }#endif -static int pidfs_ino_cmp (struct rb_node *a,const struct rb_node *b) -{ - struct pid *pid_a = rb_entry(a, struct pid, pidfs_node); - struct pid *pid_b = rb_entry(b, struct pid, pidfs_node); -u64 pid_ino_a = pid_a->ino; -u64 pid_ino_b = pid_b->ino; - -if (pid_ino_a < pid_ino_b) -return -1 ; -if (pid_ino_a > pid_ino_b) -return 1 ; -return 0 ; -} - -void pidfs_add_pid (struct pid *pid) +/* +* Allocate inode number and initialize pidfs fields. +* Called with pidmap_lock held. +*/ +void pidfs_prepare_pid (struct pid *pid) {static u64 pidfs_ino_nr =2 ;@@ -134,17 +141,23 @@ void pidfs_add_pid(struct pid *pid) pid->stashed =NULL ; pid->attr =NULL ; pidfs_ino_nr++; +} -write_seqcount_begin (&pidmap_lock_seq); -rb_find_add_rcu (&pid->pidfs_node, &pidfs_ino_tree, pidfs_ino_cmp); -write_seqcount_end (&pidmap_lock_seq); +/* +* Insert pid into the pidfs hashtable. +* Must be called without holding pidmap_lock (can allocate memory). +* Returns 0 on success, negative error on failure. +*/ +int pidfs_add_pid (struct pid *pid) +{ +return rhashtable_insert_fast (&pidfs_ino_ht, &pid->pidfs_hash, + pidfs_ino_ht_params); }void pidfs_remove_pid (struct pid *pid) { -write_seqcount_begin (&pidmap_lock_seq); -rb_erase (&pid->pidfs_node, &pidfs_ino_tree); -write_seqcount_end (&pidmap_lock_seq); +rhashtable_remove_fast (&pidfs_ino_ht, &pid->pidfs_hash, + pidfs_ino_ht_params); }void pidfs_free_pid (struct pid *pid)@@ -773,43 +786,42 @@ static int pidfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,return FILEID_KERNFS ; } -static int pidfs_ino_find (const void *key,const struct rb_node *node) -{ -const u64 pid_ino = *(u64 *)key; - const struct pid *pid = rb_entry(node, struct pid, pidfs_node); - -if (pid_ino < pid->ino) -return -1 ; -if (pid_ino > pid->ino) -return 1 ; -return 0 ; -} -/* Find a struct pid based on the inode number. */ static struct pid *pidfs_ino_get_pid (u64 ino) {struct pid *pid; -struct rb_node *node; -unsigned int seq; +struct pidfs_attr *attr;guard (rcu)(); -do { - seq =read_seqcount_begin (&pidmap_lock_seq); - node =rb_find_rcu (&ino, &pidfs_ino_tree, pidfs_ino_find); -if (node) -break ; - }while (read_seqcount_retry (&pidmap_lock_seq, seq)); -if (!node) + pid =rhashtable_lookup (&pidfs_ino_ht, &ino, pidfs_ino_ht_params); +if (!pid)return NULL ; - pid = rb_entry(node, struct pid, pidfs_node); -/* Within our pid namespace hierarchy? */ if (pid_vnr (pid) ==0 )return NULL ; -return get_pid (pid); +/* +* If attr is NULL the pid is still in the IDR but never had +* a pidfd. If attr is an error the pid went through pidfs_exit() +* and is about to be removed. Either way, deny access. +*/ + attr =READ_ONCE (pid->attr); +if (IS_ERR_OR_NULL (attr)) +return NULL ; + +/* +* If PIDFS_ATTR_BIT_EXIT is set the task has exited and we +* should not allow new file handle lookups. +*/ +if (test_bit (PIDFS_ATTR_BIT_EXIT , &attr->attr_mask)) +return NULL ; + +if (!refcount_inc_not_zero (&pid->count)) +return NULL ; + +return pid; } static struct dentry *pidfs_fh_to_dentry(struct super_block *sb,@@ -1086,6 +1098,9 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)void __init pidfs_init (void ) { +if (rhashtable_init (&pidfs_ino_ht, &pidfs_ino_ht_params)) +panic ("Failed to initialize pidfs hashtable" ); + pidfs_attr_cachep =kmem_cache_create ("pidfs_attr_cache" ,sizeof (struct pidfs_attr ),0 , (SLAB_HWCACHE_ALIGN |SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT |SLAB_PANIC ),NULL );include/linux/pid.h +2
-2@@ -6,6 +6,7 @@#include <linux/rculist.h> #include <linux/rcupdate.h> #include <linux/refcount.h> +#include <linux/rhashtable-types.h> #include <linux/sched.h> #include <linux/wait.h> @@ -60,7 +61,7 @@ struct pid {spinlock_t lock;struct {u64 ino; -struct rb_node pidfs_node; +struct rhash_head pidfs_hash;struct dentry *stashed;struct pidfs_attr *attr; };@@ -73,7 +74,6 @@ struct pid {struct upid numbers[]; }; -extern seqcount_spinlock_t pidmap_lock_seq;extern struct pid init_struct_pid;struct file ;include/linux/pidfs.h +2
-1@@ -6,7 +6,8 @@ struct coredump_params;struct file *pidfs_alloc_file (struct pid *pid,unsigned int flags);void __init pidfs_init (void ); -void pidfs_add_pid (struct pid *pid); +void pidfs_prepare_pid (struct pid *pid); +int pidfs_add_pid (struct pid *pid);void pidfs_remove_pid (struct pid *pid);void pidfs_exit (struct task_struct *tsk);#ifdef CONFIG_COREDUMP kernel/pid.c +1
-3@@ -43,7 +43,6 @@#include <linux/sched/task.h> #include <linux/idr.h> #include <linux/pidfs.h> -#include <linux/seqlock.h> #include <net/sock.h> #include <uapi/linux/pidfd.h> @@ -85,7 +84,6 @@ struct pid_namespace init_pid_ns = {EXPORT_SYMBOL_GPL (init_pid_ns);static __cacheline_aligned_in_smp DEFINE_SPINLOCK (pidmap_lock ); -seqcount_spinlock_t pidmap_lock_seq =SEQCNT_SPINLOCK_ZERO (pidmap_lock_seq, &pidmap_lock);void put_pid (struct pid *pid) {@@ -106,6 +104,7 @@ EXPORT_SYMBOL_GPL(put_pid);static void delayed_put_pid (struct rcu_head *rhp) { struct pid *pid = container_of(rhp, struct pid, rcu); +pidfs_remove_pid (pid);put_pid (pid); }@@ -141,7 +140,6 @@ void free_pid(struct pid *pid)idr_remove (&ns->idr, upid->nr); } -pidfs_remove_pid (pid);spin_unlock (&pidmap_lock);call_rcu (&pid->rcu, delayed_put_pid);@@ -315,7 +313,14 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, retval = -ENOMEM ;if (unlikely (!(ns->pid_allocated &PIDNS_ADDING )))
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, · line 323
Quoted 10 lines
kernel/pid.c +8
-1goto out_free; -pidfs_add_pid (pid); +pidfs_prepare_pid (pid); +spin_unlock (&pidmap_lock); + + retval =pidfs_add_pid (pid); +if (retval) + goto out_free_idr; + +spin_lock (&pidmap_lock);
This brings back the relock trip, reducing eficacy of the patch.
Longer term someone(tm) will need to implement lockless alloc_pid (in the fast path anyway).
In order to facilitate that the pidfs thing needs to get its own synchronisation. To my understanding rhashtable covers its own locking just fine, so the thing left to handle is ino allocation.
I was unable to find a ready to use mechanism, but conceptually it can be implemented the same way get_next_ino is sorted out, except on 64 bit values and ignoring wraparound (as in, the code can assume this will never wrap).
With these in place there is no pidfs-specific work happening under pidmap_lock.
I have not looked much into pidfs itself, I'm sure there is some complexity there to sort out, but scalability-wise the above will do it.
Christian Brauner
maintainerQuoted 303 lines · Mateusz Guzik
Quoted 289 lines · Christian Brauner
Convert the pidfs inode-to-pid mapping from an rb-tree with seqcount protection to an rhashtable. This removes the global
pidmap_lockcontention frompidfs_ino_get_pid()lookups and allows the hashtable insert to happen outside thepidmap_lock.
pidfs_add_pid()is split.pidfs_prepare_pid()allocates inode number and initializes pid fields and is called insidepidmap_lock.pidfs_add_pid()inserts pid into rhashtable and is called outsidepidmap_lock. Insertion into the rhashtable can fail and memory allocation may happen so we need to drop the spinlock.The hashtable removal is deferred to the RCU callback to ensure safe concurrent lookups. To guard against accidently opening an already reaped task
pidfs_ino_get_pid()uses additional checks beyondpid_vnr(). Ifpid->attrisPIDFS_PID_DEADor NULL the pid either never had a pidfd or it already went throughpidfs_exit()aka the process as already reaped. Ifpid->attris valid checkPIDFS_ATTR_BIT_EXITto figure out whether the task has exited. Switch torefcount_inc_not_zero()to ensure that the pid isn't about to be freed.This slightly changes visibility semantics: pidfd creation is denied after
pidfs_exit()runs, which is just before the pid number is removed from the viafree_pid(). That should not be an issue though.I haven't perfed this and I would like to make this Mateusz problem...
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 107 ++++++++++++++++++++++++++++---------------------- include/linux/pid.h | 4 +- include/linux/pidfs.h | 3 +- kernel/pid.c | 19 ++++++--- 4 files changed, 79 insertions(+), 54 deletions(-)fs/pidfs.c +61
-46@@ -21,6 +21,7 @@#include <linux/utsname.h> #include <net/net_namespace.h> #include <linux/coredump.h> +#include <linux/rhashtable.h> #include <linux/xattr.h> #include "internal.h" @@ -55,7 +56,23 @@ struct pidfs_attr {__u32 coredump_signal; }; -static struct rb_root pidfs_ino_tree =RB_ROOT ; +static struct rhashtable pidfs_ino_ht; + +static int pidfs_ino_ht_cmp (struct rhashtable_compare_arg *arg,const void *pidp) +{ +const u64 *ino = arg->key; +const struct pid *pid = pidp; + +return pid->ino != *ino; +} + +static const struct rhashtable_params pidfs_ino_ht_params = { + .key_offset = offsetof(struct pid , ino), + .key_len =sizeof (u64), + .head_offset = offsetof(struct pid , pidfs_hash), + .obj_cmpfn = pidfs_ino_ht_cmp, + .automatic_shrinking = true, +};#if BITS_PER_LONG ==32 static inline unsigned long pidfs_ino (u64 ino)@@ -84,21 +101,11 @@ static inline u32 pidfs_gen(u64 ino) }#endif -static int pidfs_ino_cmp (struct rb_node *a,const struct rb_node *b) -{ - struct pid *pid_a = rb_entry(a, struct pid, pidfs_node); - struct pid *pid_b = rb_entry(b, struct pid, pidfs_node); -u64 pid_ino_a = pid_a->ino; -u64 pid_ino_b = pid_b->ino; - -if (pid_ino_a < pid_ino_b) -return -1 ; -if (pid_ino_a > pid_ino_b) -return 1 ; -return 0 ; -} - -void pidfs_add_pid (struct pid *pid) +/* +* Allocate inode number and initialize pidfs fields. +* Called with pidmap_lock held. +*/ +void pidfs_prepare_pid (struct pid *pid) {static u64 pidfs_ino_nr =2 ;@@ -134,17 +141,23 @@ void pidfs_add_pid(struct pid *pid) pid->stashed =NULL ; pid->attr =NULL ; pidfs_ino_nr++; +} -write_seqcount_begin (&pidmap_lock_seq); -rb_find_add_rcu (&pid->pidfs_node, &pidfs_ino_tree, pidfs_ino_cmp); -write_seqcount_end (&pidmap_lock_seq); +/* +* Insert pid into the pidfs hashtable. +* Must be called without holding pidmap_lock (can allocate memory). +* Returns 0 on success, negative error on failure. +*/ +int pidfs_add_pid (struct pid *pid) +{ +return rhashtable_insert_fast (&pidfs_ino_ht, &pid->pidfs_hash, + pidfs_ino_ht_params); }void pidfs_remove_pid (struct pid *pid) { -write_seqcount_begin (&pidmap_lock_seq); -rb_erase (&pid->pidfs_node, &pidfs_ino_tree); -write_seqcount_end (&pidmap_lock_seq); +rhashtable_remove_fast (&pidfs_ino_ht, &pid->pidfs_hash, + pidfs_ino_ht_params); }void pidfs_free_pid (struct pid *pid)@@ -773,43 +786,42 @@ static int pidfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,return FILEID_KERNFS ; } -static int pidfs_ino_find (const void *key,const struct rb_node *node) -{ -const u64 pid_ino = *(u64 *)key; - const struct pid *pid = rb_entry(node, struct pid, pidfs_node); - -if (pid_ino < pid->ino) -return -1 ; -if (pid_ino > pid->ino) -return 1 ; -return 0 ; -} -/* Find a struct pid based on the inode number. */ static struct pid *pidfs_ino_get_pid (u64 ino) {struct pid *pid; -struct rb_node *node; -unsigned int seq; +struct pidfs_attr *attr;guard (rcu)(); -do { - seq =read_seqcount_begin (&pidmap_lock_seq); - node =rb_find_rcu (&ino, &pidfs_ino_tree, pidfs_ino_find); -if (node) -break ; - }while (read_seqcount_retry (&pidmap_lock_seq, seq)); -if (!node) + pid =rhashtable_lookup (&pidfs_ino_ht, &ino, pidfs_ino_ht_params); +if (!pid)return NULL ; - pid = rb_entry(node, struct pid, pidfs_node); -/* Within our pid namespace hierarchy? */ if (pid_vnr (pid) ==0 )return NULL ; -return get_pid (pid); +/* +* If attr is NULL the pid is still in the IDR but never had +* a pidfd. If attr is an error the pid went through pidfs_exit() +* and is about to be removed. Either way, deny access. +*/ + attr =READ_ONCE (pid->attr); +if (IS_ERR_OR_NULL (attr)) +return NULL ; + +/* +* If PIDFS_ATTR_BIT_EXIT is set the task has exited and we +* should not allow new file handle lookups. +*/ +if (test_bit (PIDFS_ATTR_BIT_EXIT , &attr->attr_mask)) +return NULL ; + +if (!refcount_inc_not_zero (&pid->count)) +return NULL ; + +return pid; } static struct dentry *pidfs_fh_to_dentry(struct super_block *sb,@@ -1086,6 +1098,9 @@ struct file *pidfs_alloc_file(struct pid *pid, unsigned int flags)void __init pidfs_init (void ) { +if (rhashtable_init (&pidfs_ino_ht, &pidfs_ino_ht_params)) +panic ("Failed to initialize pidfs hashtable" ); + pidfs_attr_cachep =kmem_cache_create ("pidfs_attr_cache" ,sizeof (struct pidfs_attr ),0 , (SLAB_HWCACHE_ALIGN |SLAB_RECLAIM_ACCOUNT |SLAB_ACCOUNT |SLAB_PANIC ),NULL );include/linux/pid.h +2
-2@@ -6,6 +6,7 @@#include <linux/rculist.h> #include <linux/rcupdate.h> #include <linux/refcount.h> +#include <linux/rhashtable-types.h> #include <linux/sched.h> #include <linux/wait.h> @@ -60,7 +61,7 @@ struct pid {spinlock_t lock;struct {u64 ino; -struct rb_node pidfs_node; +struct rhash_head pidfs_hash;struct dentry *stashed;struct pidfs_attr *attr; };@@ -73,7 +74,6 @@ struct pid {struct upid numbers[]; }; -extern seqcount_spinlock_t pidmap_lock_seq;extern struct pid init_struct_pid;struct file ;include/linux/pidfs.h +2
-1@@ -6,7 +6,8 @@ struct coredump_params;struct file *pidfs_alloc_file (struct pid *pid,unsigned int flags);void __init pidfs_init (void ); -void pidfs_add_pid (struct pid *pid); +void pidfs_prepare_pid (struct pid *pid); +int pidfs_add_pid (struct pid *pid);void pidfs_remove_pid (struct pid *pid);void pidfs_exit (struct task_struct *tsk);#ifdef CONFIG_COREDUMP kernel/pid.c +1
-3@@ -43,7 +43,6 @@#include <linux/sched/task.h> #include <linux/idr.h> #include <linux/pidfs.h> -#include <linux/seqlock.h> #include <net/sock.h> #include <uapi/linux/pidfd.h> @@ -85,7 +84,6 @@ struct pid_namespace init_pid_ns = {EXPORT_SYMBOL_GPL (init_pid_ns);static __cacheline_aligned_in_smp DEFINE_SPINLOCK (pidmap_lock ); -seqcount_spinlock_t pidmap_lock_seq =SEQCNT_SPINLOCK_ZERO (pidmap_lock_seq, &pidmap_lock);void put_pid (struct pid *pid) {@@ -106,6 +104,7 @@ EXPORT_SYMBOL_GPL(put_pid);static void delayed_put_pid (struct rcu_head *rhp) { struct pid *pid = container_of(rhp, struct pid, rcu); +pidfs_remove_pid (pid);put_pid (pid); }@@ -141,7 +140,6 @@ void free_pid(struct pid *pid)idr_remove (&ns->idr, upid->nr); } -pidfs_remove_pid (pid);spin_unlock (&pidmap_lock);call_rcu (&pid->rcu, delayed_put_pid);@@ -315,7 +313,14 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, retval = -ENOMEM ;if (unlikely (!(ns->pid_allocated &PIDNS_ADDING )))kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, · line 323
Quoted 10 lines
kernel/pid.c +8
-1goto out_free; -pidfs_add_pid (pid); +pidfs_prepare_pid (pid); +spin_unlock (&pidmap_lock); + + retval =pidfs_add_pid (pid); +if (retval) + goto out_free_idr; + +spin_lock (&pidmap_lock);This brings back the relock trip, reducing eficacy of the patch.
Quoted 6 lines
Longer term someone(tm) will need to implement lockless
alloc_pid(in the fast path anyway).In order to facilitate that the pidfs thing needs to get its own synchronisation. To my understanding rhashtable covers its own locking just fine, so the thing left to handle is ino allocation.
I'm very confused why inode allocation would matter. Inode allocation for pidfs is literally just a plain increment. IOW, it's not using any atomic at all. So that can happen under pidmap_lock without any issue and I don't see the need to change to any complex per-cpu allocation mechanism for this.
Quoted 9 lines · Christian Brauner
Quoted 6 lines · Mateusz Guzik
Longer term someone(tm) will need to implement lockless
alloc_pid(in the fast path anyway).In order to facilitate that the pidfs thing needs to get its own synchronisation. To my understanding rhashtable covers its own locking just fine, so the thing left to handle is ino allocation.
Quoted 5 lines
I'm very confused why inode allocation would matter. Inode allocation for pidfs is literally just a plain increment. IOW, it's not using any atomic at all. So that can happen under
pidmap_lockwithout any issue and I don't see the need to change to any complex per-cpu allocation mechanism for this.
I am not saying this bit poses a problem as is. I am saying down the road pid allocation will need to be reworked to operate locklessly (or worst case with fine-grained locking) in which case the the pid ino thing wont be able to rely on the pidmap lock.
It is easy to sort out as is, so I think it should be sorted out while pidfs support is being patched.
The nice practical thing about unique 64 bit ids is that you can afford to not free them when no longer used as it is considered unrealistic for the counter to overflow in the lifetime of the box.
Making it scalable is a well known problem with simple approaches and the kernel is already doing something of the sort for 32-bit inos in get_next_ino(). If anything I'm surprised the kernel does not provide a generic mechanism for 64 bits (at least I failed to find out and when I asked around people could not point me at one either). Getting this done for 64 bit is a matter of implementing a nearly identical routine, except with 64-bit types and with overflow check removed.
However, the real compliant about this patch is the re-introduced double acquire of pidmap_lock.
Christian Brauner
maintainerQuoted 16 lines · Mateusz Guzik
Quoted 9 lines · Christian Brauner
Quoted 6 lines · Mateusz Guzik
Longer term someone(tm) will need to implement lockless
alloc_pid(in the fast path anyway).In order to facilitate that the pidfs thing needs to get its own synchronisation. To my understanding rhashtable covers its own locking just fine, so the thing left to handle is ino allocation.
Quoted 5 lines
I'm very confused why inode allocation would matter. Inode allocation for pidfs is literally just a plain increment. IOW, it's not using any atomic at all. So that can happen under
pidmap_lockwithout any issue and I don't see the need to change to any complex per-cpu allocation mechanism for this.
Quoted 7 lines
I am not saying this bit poses a problem as is. I am saying down the road pid allocation will need to be reworked to operate locklessly (or worst case with fine-grained locking) in which case the the pid ino thing wont be able to rely on the pidmap lock.
It is easy to sort out as is, so I think it should be sorted out while pidfs support is being patched.
I don't have time for that but I will move pidfs out of pidmap_lock completely.
Quoted 3 lines
The nice practical thing about unique 64 bit ids is that you can afford to not free them when no longer used as it is considered unrealistic for the counter to overflow in the lifetime of the box.
I'm still confused what this has to do with pidfd inodes.
Quoted 4 lines
Making it scalable is a well known problem with simple approaches and the kernel is already doing something of the sort for 32-bit inos in
get_next_ino(). If anything I'm surprised the kernel does not provide
include/linux/cookie.h
is what you want.
Quoted 7 lines
a generic mechanism for 64 bits (at least I failed to find out and when I asked around people could not point me at one either). Getting this done for 64 bit is a matter of implementing a nearly identical routine, except with 64-bit types and with overflow check removed.
However, the real compliant about this patch is the re-introduced double acquire of
pidmap_lock.
That's easy to sort out:
fs/pidfs.c | 4 +++- kernel/pid.c | 13 ++++++------- 2 files changed, 9 insertions(+), 8 deletions(-)
fs/pidfs.c +3 -0
@@ -138,6 +138,7 @@ void pidfs_prepare_pid(struct pid *pid) pidfs_ino_nr +=2 ; pid->ino = pidfs_ino_nr; + pid->pidfs_hash.next =NULL ; pid->stashed =NULL ; pid->attr =NULL ; pidfs_ino_nr++;
@@ -145,7 +146,8 @@ void pidfs_prepare_pid(struct pid *pid)/* * Insert pid into the pidfs hashtable. +* @pid: pid to add +* * Returns 0 on success, negative error on failure. */ int pidfs_add_pid (struct pid *pid)
kernel/pid.c +6 -7
@@ -313,14 +313,9 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, retval = -ENOMEM ;if (unlikely (!(ns->pid_allocated &PIDNS_ADDING ))) goto out_free; -pidfs_prepare_pid (pid); -spin_unlock (&pidmap_lock); - retval =pidfs_add_pid (pid); -if (retval) - goto out_free_idr; +pidfs_prepare_pid (pid); -spin_lock (&pidmap_lock);for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {/* Make the PID visible to find_pid_ns. */ idr_replace (&upid->ns->idr, pid, upid->nr);
@@ -330,11 +325,15 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,idr_preload_end ();ns_ref_active_get (ns); +if (pidfs_add_pid (pid)) { +free_pid (pid); + pid =ERR_PTR (-ENOMEM ); + } +return pid; out_free:spin_unlock (&pidmap_lock); -out_free_idr:idr_preload_end ();spin_lock (&pidmap_lock);
Quoted 26 lines · Christian Brauner
Quoted 16 lines · Mateusz Guzik
Quoted 9 lines · Christian Brauner
Quoted 6 lines · Mateusz Guzik
Longer term someone(tm) will need to implement lockless
alloc_pid(in the fast path anyway).In order to facilitate that the pidfs thing needs to get its own synchronisation. To my understanding rhashtable covers its own locking just fine, so the thing left to handle is ino allocation.
Quoted 5 lines
I'm very confused why inode allocation would matter. Inode allocation for pidfs is literally just a plain increment. IOW, it's not using any atomic at all. So that can happen under
pidmap_lockwithout any issue and I don't see the need to change to any complex per-cpu allocation mechanism for this.Quoted 7 lines
I am not saying this bit poses a problem as is. I am saying down the road pid allocation will need to be reworked to operate locklessly (or worst case with fine-grained locking) in which case the the pid ino thing wont be able to rely on the pidmap lock.
It is easy to sort out as is, so I think it should be sorted out while pidfs support is being patched.
Quoted 8 lines
I don't have time for that but I will move pidfs out of
pidmap_lockcompletely.Quoted 3 lines
The nice practical thing about unique 64 bit ids is that you can afford to not free them when no longer used as it is considered unrealistic for the counter to overflow in the lifetime of the box.
I'm still confused what this has to do with pidfd inodes.
the thing is only maintained by pidfd
Quoted 72 lines
Quoted 5 lines
Making it scalable is a well known problem with simple approaches and the kernel is already doing something of the sort for 32-bit inos in
get_next_ino(). If anything I'm surprised the kernel does not provide
include/linux/cookie.his what you want.
Quoted 7 lines
a generic mechanism for 64 bits (at least I failed to find out and when I asked around people could not point me at one either). Getting this done for 64 bit is a matter of implementing a nearly identical routine, except with 64-bit types and with overflow check removed.
However, the real compliant about this patch is the re-introduced double acquire of
pidmap_lock.That's easy to sort out:
fs/pidfs.c | 4 +++- kernel/pid.c | 13 ++++++------- 2 files changed, 9 insertions(+), 8 deletions(-)fs/pidfs.c +3
-0@@ -138,6 +138,7 @@ void pidfs_prepare_pid(struct pid *pid) pidfs_ino_nr +=2 ; pid->ino = pidfs_ino_nr; + pid->pidfs_hash.next =NULL ; pid->stashed =NULL ; pid->attr =NULL ; pidfs_ino_nr++;@@ -145,7 +146,8 @@ void pidfs_prepare_pid(struct pid *pid)/* * Insert pid into the pidfs hashtable. +* @pid: pid to add +* * Returns 0 on success, negative error on failure. */ int pidfs_add_pid (struct pid *pid)kernel/pid.c +4
-6@@ -313,14 +313,9 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, retval = -ENOMEM ;if (unlikely (!(ns->pid_allocated &PIDNS_ADDING ))) goto out_free; -pidfs_prepare_pid (pid); -spin_unlock (&pidmap_lock); - retval =pidfs_add_pid (pid); -if (retval) - goto out_free_idr; +pidfs_prepare_pid (pid); -spin_lock (&pidmap_lock);for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {/* Make the PID visible to find_pid_ns. */ idr_replace (&upid->ns->idr, pid, upid->nr);@@ -330,11 +325,15 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,idr_preload_end ();ns_ref_active_get (ns); +if (pidfs_add_pid (pid)) { +free_pid (pid); + pid =ERR_PTR (-ENOMEM );
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, · line 339
Quoted 10 lines
kernel/pid.c +2
-1+ } +return pid; out_free:spin_unlock (&pidmap_lock); -out_free_idr:idr_preload_end ();spin_lock (&pidmap_lock);
I can't review the entirety of pidfs interaction, performance-wise this looks fine to me.
I booted the combination of both patches and got the expected win of bumping the rate to over 700K ops/s when messing with threads. So the approach has my stamp of approval.
Jan Kara
reviewerQuoted 5 lines · Christian Brauner
Convert the pidfs inode-to-pid mapping from an rb-tree with seqcount protection to an rhashtable. This removes the global
pidmap_lockcontention frompidfs_ino_get_pid()lookups and allows the hashtable insert to happen outside thepidmap_lock.
Quoted 8 lines
pidfs_add_pid()is split.pidfs_prepare_pid()allocates inode number and initializes pid fields and is called insidepidmap_lock.pidfs_add_pid()inserts pid into rhashtable and is called outsidepidmap_lock. Insertion into the rhashtable can fail and memory allocation may happen so we need to drop the spinlock.The hashtable removal is deferred to the RCU callback to ensure safe concurrent lookups.
This doesn't look correct to me. See below in the code for details...
Quoted 8 lines
To guard against accidently opening an already
reaped taskpidfs_ino_get_pid()uses additional checks beyondpid_vnr(). Ifpid->attrisPIDFS_PID_DEADor NULL the pid either never had a pidfd or it already went throughpidfs_exit()aka the process as already reaped. Ifpid->attris valid checkPIDFS_ATTR_BIT_EXITto figure out whether the task has exited. Switch torefcount_inc_not_zero()to ensure that the pid isn't about to be freed.
Quoted 5 lines
This slightly changes visibility semantics: pidfd creation is denied after
pidfs_exit()runs, which is just before the pid number is removed from the viafree_pid(). That should not be an issue though.I haven't perfed this and I would like to make this Mateusz problem...
;-) I guess this note means you want to have some perf data justifying this change. I agree that when replacing data structure like this we should have some measurements showing that the expected improvements indeed translate to a real improvements as well. It has happened to me more than once that a theoretical improvement didn't really work out in practice...
Quoted 25 lines
Signed-off-by Christian Brauner <[email protected]>--- fs/pidfs.c | 107 ++++++++++++++++++++++++++++---------------------- include/linux/pid.h | 4 +- include/linux/pidfs.h | 3 +- kernel/pid.c | 19 ++++++--- 4 files changed, 79 insertions(+), 54 deletions(-)fs/pidfs.c +1
-0@@ -21,6 +21,7 @@#include <linux/utsname.h> #include <net/net_namespace.h> #include <linux/coredump.h> +#include <linux/rhashtable.h> #include <linux/xattr.h> #include "internal.h" @@ -55,7 +56,23 @@ struct pidfs_attr {__u32 coredump_signal; };
fs/pidfs.c struct pidfs_attr { · line 67
Quoted 10 lines
fs/pidfs.c +9
-1-static struct rb_root pidfs_ino_tree =RB_ROOT ; +static struct rhashtable pidfs_ino_ht; + +static int pidfs_ino_ht_cmp (struct rhashtable_compare_arg *arg,const void *pidp) +{ +const u64 *ino = arg->key; +const struct pid *pid = pidp; + +return pid->ino != *ino; +}
I don't think this comparison function is really needed. rhashtable provides its own comparison function which is good enough for simple fixed-length keys (rhashtable_compare() is used if .obj_cmpfn is not provided).
Quoted 11 lines
fs/pidfs.c +8
-0+ +static const struct rhashtable_params pidfs_ino_ht_params = { + .key_offset = offsetof(struct pid , ino), + .key_len =sizeof (u64), + .head_offset = offsetof(struct pid , pidfs_hash), + .obj_cmpfn = pidfs_ino_ht_cmp, + .automatic_shrinking = true, +};#if BITS_PER_LONG ==32 static inline unsigned long pidfs_ino (u64 ino)
...
Quoted 35 lines
/* Find a struct pid based on the inode number. */ static struct pid *pidfs_ino_get_pid(u64 ino) {
fs/pidfs.c +10
-12struct pid *pid; -struct rb_node *node; -unsigned int seq; +struct pidfs_attr *attr;guard (rcu)(); -do { - seq =read_seqcount_begin (&pidmap_lock_seq); - node =rb_find_rcu (&ino, &pidfs_ino_tree, pidfs_ino_find); -if (node) -break ; - }while (read_seqcount_retry (&pidmap_lock_seq, seq)); -if (!node) + pid =rhashtable_lookup (&pidfs_ino_ht, &ino, pidfs_ino_ht_params); +if (!pid)return NULL ; - pid = rb_entry(node, struct pid, pidfs_node); -/* Within our pid namespace hierarchy? */ if (pid_vnr (pid) ==0 )return NULL ; -return get_pid (pid); +/* +* If attr is NULL the pid is still in the IDR but never had +* a pidfd. If attr is an error the pid went through pidfs_exit() +* and is about to be removed. Either way, deny access. +*/ + attr =READ_ONCE (pid->attr); +if (IS_ERR_OR_NULL (attr))
fs/pidfs.c static int pidfs_encode_fh(struct inode *inode, u32 *fh, int *max_len, · line 821
Quoted 10 lines
fs/pidfs.c +10
-0+return NULL ; + +/* +* If PIDFS_ATTR_BIT_EXIT is set the task has exited and we +* should not allow new file handle lookups. +*/ +if (test_bit (PIDFS_ATTR_BIT_EXIT , &attr->attr_mask)) +return NULL ; + +if (!refcount_inc_not_zero (&pid->count))
If this ever sees zero pid->count we have a problem because it means we are reading freed memory, aren't we? I think RCU must be protecting from the last reference of struct pid being dropped and struct pid getting freed...
Quoted 6 lines
fs/pidfs.c +3
-0+return NULL ; + +return pid; } static struct dentry *pidfs_fh_to_dentry(struct super_block *sb,
...
Quoted 4 lines
kernel/pid.c +0
-0
...
Quoted 9 lines
kernel/pid.c +1
-0@@ -106,6 +104,7 @@ EXPORT_SYMBOL_GPL(put_pid);static void delayed_put_pid (struct rcu_head *rhp) { struct pid *pid = container_of(rhp, struct pid, rcu); +pidfs_remove_pid (pid);put_pid (pid); }
kernel/pid.c void free_pid(struct pid *pid) · line 145
Quoted 7 lines
kernel/pid.c +0
-1@@ -141,7 +140,6 @@ void free_pid(struct pid *pid)idr_remove (&ns->idr, upid->nr); } -pidfs_remove_pid (pid);spin_unlock (&pidmap_lock);call_rcu (&pid->rcu, delayed_put_pid);
I don't understand this. If you remove pid from the rhashtable after the rcu period expires, then there can be rcu readers of rhashtable accessing the freed object. So IMHO you need to keep the call to pidfs_remove_pid() before call_rcu().
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, · line 323
Quoted 13 lines
kernel/pid.c +8
-1@@ -315,7 +313,14 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, retval = -ENOMEM ;if (unlikely (!(ns->pid_allocated &PIDNS_ADDING ))) goto out_free; -pidfs_add_pid (pid); +pidfs_prepare_pid (pid); +spin_unlock (&pidmap_lock); + + retval =pidfs_add_pid (pid); +if (retval) + goto out_free_idr; + +spin_lock (&pidmap_lock);
The PIDNS_ADDING logic now looks suspicious to me. With this change you can check for PIDNS_ADDING here, then have another thread execute disable_pid_allocation() and then still add new pid to the ns here so AFAICT zap_pid_ns_processes() can miss the newly added entry? I'm sorry if I'm missing something, I don't really deeply understand this code.
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, · line 338
Quoted 10 lines
kernel/pid.c +3
-0for (upid = pid->numbers + ns->level; upid >= pid->numbers; --upid) {/* Make the PID visible to find_pid_ns. */ idr_replace (&upid->ns->idr, pid, upid->nr);@@ -328,6 +333,11 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,return pid; out_free: +spin_unlock (&pidmap_lock); +out_free_idr: +idr_preload_end ();
As a side note idr_preload games in this last part of alloc_pid() look pointless to me because the only thing you do seems to be idr_replace() which doesn't need any allocation.
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, · line 353
Quoted 13 lines
kernel/pid.c +2
-1+ +spin_lock (&pidmap_lock);while (++i <= ns->level) { upid = pid->numbers + i;idr_remove (&upid->ns->idr, upid->nr);@@ -338,7 +348,6 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid,idr_set_cursor (&ns->idr,0 );spin_unlock (&pidmap_lock); -idr_preload_end (); out_abort:put_pid_ns (ns);
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 29 lines · Jan Kara
Quoted 5 lines · Christian Brauner
Convert the pidfs inode-to-pid mapping from an rb-tree with seqcount protection to an rhashtable. This removes the global
pidmap_lockcontention frompidfs_ino_get_pid()lookups and allows the hashtable insert to happen outside thepidmap_lock.Quoted 8 lines
pidfs_add_pid()is split.pidfs_prepare_pid()allocates inode number and initializes pid fields and is called insidepidmap_lock.pidfs_add_pid()inserts pid into rhashtable and is called outsidepidmap_lock. Insertion into the rhashtable can fail and memory allocation may happen so we need to drop the spinlock.The hashtable removal is deferred to the RCU callback to ensure safe concurrent lookups.
This doesn't look correct to me. See below in the code for details...
Quoted 12 lines
To guard against accidently opening an already
reaped taskpidfs_ino_get_pid()uses additional checks beyondpid_vnr(). Ifpid->attrisPIDFS_PID_DEADor NULL the pid either never had a pidfd or it already went throughpidfs_exit()aka the process as already reaped. Ifpid->attris valid checkPIDFS_ATTR_BIT_EXITto figure out whether the task has exited. Switch torefcount_inc_not_zero()to ensure that the pid isn't about to be freed.This slightly changes visibility semantics: pidfd creation is denied after
pidfs_exit()runs, which is just before the pid number is removed from the viafree_pid(). That should not be an issue though.
Quoted 7 lines
Quoted 1 line
I haven't perfed this and I would like to make this Mateusz problem...
;-) I guess this note means you want to have some perf data justifying this change. I agree that when replacing data structure like this we should have some measurements showing that the expected improvements indeed translate to a real improvements as well. It has happened to me more than once that a theoretical improvement didn't really work out in practice...
Moving this logic out of pidmap_lock is worth it if it means we can stop dropping an reacquiring it.
Note that this is very much RFC so I don't expect it to work as is.
Quoted 4 lines
Quoted 4 lines
fs/pidfs.c +4
-0+static int pidfs_ino_ht_cmp (struct rhashtable_compare_arg *arg,const void *pidp) +{ +const u64 *ino = arg->key; +const struct pid *pid = pidp;
Quoted 8 lines
fs/pidfs.c struct pidfs_attr { · line 67
Quoted 3 lines
fs/pidfs.c +3
-0+ +return pid->ino != *ino; +}I don't think this comparison function is really needed. rhashtable provides its own comparison function which is good enough for simple fixed-length keys (
rhashtable_compare()is used if .obj_cmpfn is not provided).
Ah, thanks!
Quoted 40 lines
fs/pidfs.c static int pidfs_encode_fh(struct inode *inode, u32 *fh, int *max_len, · line 821
Quoted 8 lines
fs/pidfs.c +8
-0+/* +* If PIDFS_ATTR_BIT_EXIT is set the task has exited and we +* should not allow new file handle lookups. +*/ +if (test_bit (PIDFS_ATTR_BIT_EXIT , &attr->attr_mask)) +return NULL ; + +if (!refcount_inc_not_zero (&pid->count))If this ever sees zero
pid->countwe have a problem because it means we are reading freed memory, aren't we? I think RCU must be protecting from the last reference of struct pid being dropped and struct pid getting freed...Quoted 6 lines
fs/pidfs.c +3
-0+return NULL ; + +return pid; } static struct dentry *pidfs_fh_to_dentry(struct super_block *sb,...
Quoted 4 lines
kernel/pid.c +0
-0...
Quoted 15 lines
kernel/pid.c +1
-1@@ -106,6 +104,7 @@ EXPORT_SYMBOL_GPL(put_pid);static void delayed_put_pid (struct rcu_head *rhp) { struct pid *pid = container_of(rhp, struct pid, rcu); +pidfs_remove_pid (pid);put_pid (pid); }@@ -141,7 +140,6 @@ void free_pid(struct pid *pid)idr_remove (&ns->idr, upid->nr); } -pidfs_remove_pid (pid);spin_unlock (&pidmap_lock);
Quoted 6 lines
kernel/pid.c void free_pid(struct pid *pid) · line 145
Quoted 1 line
kernel/pid.c +0
-0call_rcu (&pid->rcu, delayed_put_pid);I don't understand this. If you remove pid from the rhashtable after the rcu period expires, then there can be rcu readers of rhashtable accessing the freed object. So IMHO you need to keep the call to
pidfs_remove_pid()beforecall_rcu().
Yes, you're absolutely right.
Quoted 12 lines
Quoted 12 lines
kernel/pid.c +6
-1@@ -315,7 +313,14 @@ struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, retval = -ENOMEM ;if (unlikely (!(ns->pid_allocated &PIDNS_ADDING ))) goto out_free; -pidfs_add_pid (pid); +pidfs_prepare_pid (pid); +spin_unlock (&pidmap_lock); + + retval =pidfs_add_pid (pid); +if (retval) + goto out_free_idr;
Quoted 8 lines
kernel/pid.c struct pid *alloc_pid(struct pid_namespace *ns, pid_t *arg_set_tid, · line 323
Quoted 2 lines
kernel/pid.c +2
-0+ +spin_lock (&pidmap_lock);The
PIDNS_ADDINGlogic now looks suspicious to me. With this change you can check forPIDNS_ADDINGhere, then have another thread executedisable_pid_allocation()and then still add new pid to the ns here so AFAICTzap_pid_ns_processes()can miss the newly added entry? I'm sorry if I'm missing something, I don't really deeply understand this code.
The new version doesn't drop the lock at all anymore.