fix rate limited ipc_namespace freeing
cover fix rate limited ipc_namespace freeing
The test case below fails on 5.17 (and a bunch of older kernels) with unshare getting -ENOSPC, because the rate at which ipc_namespace structures can be freed is limited by each item on the to-free list waiting in synchronize_rcu.
Making kern_unmount use queue_rcu_work gets rid of that slowdown, allowing a batch of vfsmount structures to be freed after each RCU grace period has expired.
That, in turn, allows us to just get rid of the workqueue in ipc/namespace.c completely.
With these two changes the test case reliably succeeds at calling unshare a million times, even with max_ipc_namespaces reduced to 1000 :)
#define _GNU_SOURCE
#include <sched.h>
#include <error.h>
#include <errno.h>
#include <stdlib.h>
int main()
{
int i;
for (i = 0; i < 1000000; i++) {
if (unshare(CLONE_NEWIPC) < 0)
error(EXIT_FAILURE, errno, "unshare");
}
}Rik van Riel (2):
vfs: free vfsmount through rcu work from kern_unmount
ipc: get rid of free_ipc_work workqueue
fs/namespace.c | 11 +++++++++-- include/linux/ipc_namespace.h | 2 -- include/linux/mount.h | 2 ++ ipc/namespace.c | 21 +-------------------- 4 files changed, 12 insertions(+), 24 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 vfs: free vfsmount through rcu work from kern_unmount
After kern_unmount returns, callers can no longer access the vfsmount structure. However, the vfsmount structure does need to be kept around until the end of the RCU grace period, to make sure other accesses have all gone away too.
This can be accomplished by either gating each kern_unmount on synchronize_rcu (the comment in the code says it all), or by deferring the freeing until the next grace period, where it needs to be handled in a workqueue due to the locking in mntput_no_expire().
--- fs/namespace.c | 11 +++++++++-- include/linux/mount.h | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-)
fs/namespace.c +9 -2
@@ -4384,13 +4384,20 @@ struct vfsmount *kern_mount(struct file_system_type *type) }EXPORT_SYMBOL_GPL (kern_mount); +static void mntput_rcu_work (struct work_struct *work) +{ +struct vfsmount *mnt =container_of (to_rcu_work (work), + struct vfsmount, free_rwork); +mntput (mnt); +} +void kern_unmount (struct vfsmount *mnt) {/* release long term mount so mount point can be released */ if (!IS_ERR_OR_NULL (mnt)) {real_mount (mnt)->mnt_ns =NULL ; -synchronize_rcu ();/* yecchhh... */ -mntput (mnt); +INIT_RCU_WORK (&mnt->free_rwork, mntput_rcu_work); +queue_rcu_work (system_wq, &mnt->free_rwork); } }EXPORT_SYMBOL (kern_unmount);
include/linux/mount.h +2 -0
@@ -16,6 +16,7 @@#include <linux/spinlock.h> #include <linux/seqlock.h> #include <linux/atomic.h> +#include <linux/workqueue.h> struct super_block ;struct vfsmount ;
@@ -73,6 +74,7 @@ struct vfsmount {struct super_block *mnt_sb;/* pointer to superblock */ int mnt_flags;struct user_namespace *mnt_userns; +struct rcu_work free_rwork; } __randomize_layout;static inline struct user_namespace *mnt_user_ns (const struct vfsmount *mnt)
Signature · 1 line
2.34.1
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 10 lines · Rik van Riel
After
kern_unmountreturns, callers can no longer access the vfsmount structure. However, the vfsmount structure does need to be kept around until the end of the RCU grace period, to make sure other accesses have all gone away too.This can be accomplished by either gating each
kern_unmountonsynchronize_rcu(the comment in the code says it all), or by deferring the freeing until the next grace period, where it needs to be handled in a workqueue due to the locking inmntput_no_expire().
NAK. There's code that relies upon kern_unmount() being synchronous. That's precisely the reason why MNT_INTERNAL is treated that way in mntput_no_expire().
Re: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 12 lines · Al Viro
Quoted 10 lines · Rik van Riel
After
kern_unmountreturns, callers can no longer access the vfsmount structure. However, the vfsmount structure does need to be kept around until the end of the RCU grace period, to make sure other accesses have all gone away too.This can be accomplished by either gating each
kern_unmountonsynchronize_rcu(the comment in the code says it all), or by deferring the freeing until the next grace period, where it needs to be handled in a workqueue due to the locking inmntput_no_expire().
Quoted 3 lines
NAK. There's code that relies upon
kern_unmount()being synchronous. That's precisely the reason whyMNT_INTERNALis treated that way inmntput_no_expire().
Fair enough. Should I make a kern_unmount_rcu() version that gets called just from mq_put_mnt()?
Signature · 1 line
All Rights Reversed.
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 13 lines · Rik van Riel
Quoted 12 lines · Al Viro
Quoted 10 lines · Rik van Riel
After
kern_unmountreturns, callers can no longer access the vfsmount structure. However, the vfsmount structure does need to be kept around until the end of the RCU grace period, to make sure other accesses have all gone away too.This can be accomplished by either gating each
kern_unmountonsynchronize_rcu(the comment in the code says it all), or by deferring the freeing until the next grace period, where it needs to be handled in a workqueue due to the locking inmntput_no_expire().
Quoted 6 lines
Quoted 3 lines
NAK. There's code that relies upon
kern_unmount()being synchronous. That's precisely the reason whyMNT_INTERNALis treated that way inmntput_no_expire().Fair enough. Should I make a
kern_unmount_rcu()version that gets called just frommq_put_mnt()?
Umm... I'm not sure you can afford having struct ipc_namespace freed and reused before the mqueue superblock gets at least to deactivate_locked_super().
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 18 lines · Al Viro
Quoted 17 lines · Rik van Riel
Quoted 15 lines · Al Viro
Quoted 10 lines · Rik van Riel
After
kern_unmountreturns, callers can no longer access the vfsmount structure. However, the vfsmount structure does need to be kept around until the end of the RCU grace period, to make sure other accesses have all gone away too.This can be accomplished by either gating each
kern_unmountonsynchronize_rcu(the comment in the code says it all), or by deferring the freeing until the next grace period, where it needs to be handled in a workqueue due to the locking inmntput_no_expire().NAK. There's code that relies upon
kern_unmount()being synchronous. That's precisely the reason whyMNT_INTERNALis treated that way inmntput_no_expire().
Quoted 6 lines
Quoted 2 lines
Fair enough. Should I make a
kern_unmount_rcu()version that gets called just frommq_put_mnt()?Umm... I'm not sure you can afford having struct
ipc_namespacefreed and reused before the mqueue superblock gets at least todeactivate_locked_super().
BTW, that's a good demonstration of the problems with making those beasts async. struct mount is *not* accessed past kern_unmount(), but the objects used by the superblock might very well be - in this case they (struct ipc_namespace, pointed to by s->s_fs_data) are freed by the caller after kern_unmount() returns. And possibly reused. Now note that they are used as search keys by mqueue_get_tree() and it becomes very fishy.
If you want to go that way, make it something like
void put_ipc_ns(struct ipc_namespace *ns)
{
if (refcount_dec_and_lock(&ns->ns.count, &mq_lock)) {
mq_clear_sbinfo(ns);
spin_unlock(&mq_lock);
kern_unmount_rcu(ns->mq_mnt);
}
}and give mqueue this for ->kill_sb():
static void mqueue_kill_super(struct super_block *sb)
{
struct ipc_namespace *ns = sb->s_fs_info;
kill_litter_super(sb);
do the rest of free_ipc_ns();
}One thing: kern_unmount_rcu() needs a very big warning about the caution needed from its callers. It's really not safe for general use, and it will be a temptation for folks with scalability problems like this one to just use it instead of kern_unmount() and declare the problem solved.
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 54 lines · Al Viro
Quoted 18 lines · Al Viro
Quoted 17 lines · Rik van Riel
Quoted 15 lines · Al Viro
Quoted 10 lines · Rik van Riel
After
kern_unmountreturns, callers can no longer access the vfsmount structure. However, the vfsmount structure does need to be kept around until the end of the RCU grace period, to make sure other accesses have all gone away too.This can be accomplished by either gating each
kern_unmountonsynchronize_rcu(the comment in the code says it all), or by deferring the freeing until the next grace period, where it needs to be handled in a workqueue due to the locking inmntput_no_expire().NAK. There's code that relies upon
kern_unmount()being synchronous. That's precisely the reason whyMNT_INTERNALis treated that way inmntput_no_expire().Quoted 6 lines
Quoted 2 lines
Fair enough. Should I make a
kern_unmount_rcu()version that gets called just frommq_put_mnt()?Umm... I'm not sure you can afford having struct
ipc_namespacefreed and reused before the mqueue superblock gets at least todeactivate_locked_super().BTW, that's a good demonstration of the problems with making those beasts async. struct mount is *not* accessed past
kern_unmount(), but the objects used by the superblock might very well be - in this case they (structipc_namespace, pointed to bys->s_fs_data) are freed by the caller afterkern_unmount()returns. And possibly reused. Now note that they are used as search keys bymqueue_get_tree()and it becomes very fishy.If you want to go that way, make it something like
void
put_ipc_ns(struct ipc_namespace *ns){ if (refcount_dec_and_lock(&ns->ns.count, &mq_lock)) { mq_clear_sbinfo(ns); spin_unlock(&mq_lock); kern_unmount_rcu(ns->mq_mnt); } }and give mqueue this for ->kill_sb():
static void
mqueue_kill_super(struct super_block *sb){ struct ipc_namespace *ns = sb->s_fs_info; kill_litter_super(sb); do the rest of free_ipc_ns(); }
Quoted 5 lines
One thing:
kern_unmount_rcu()needs a very big warning about the caution needed from its callers. It's really not safe for general use, and it will be a temptation for folks with scalability problems like this one to just use it instead ofkern_unmount()and declare the problem solved.
FWIW, that won't work correctly wrt failure exits. I'm digging through the lifetime rules in there right now, will post when I'm done.
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 3 lines · Al Viro
FWIW, that won't work correctly wrt failure exits. I'm digging through the lifetime rules in there right now, will post when I'm done.
OK, now that I'd reconstructed the picture... The problems with delayed shutdown are prevented by mq_clear_sbinfo() call in there - mqueue is capable of more or less gracefully dealing with having ->s_fs_info ripped from under it, which is what that thing does. Before the kern_unmount(). And since that code is non-modular, we don't need to protect that either.
IOW, having
void put_ipc_ns(struct ipc_namespace *ns)
{
if (refcount_dec_and_lock(&ns->ns.count, &mq_lock)) {
mq_clear_sbinfo(ns);
spin_unlock(&mq_lock);
free_ipc_ns(ns);
}
}and
void mq_put_mnt(struct ipc_namespace *ns)
{
/*
* The only reason it's safe to have the mntput async
* is that we'd already ripped the ipc_namespace away
* from the mqueue superblock, by having called
* mq_clear_sbinfo().
*
* NOTE: kern_unmount_rcu() IS NOT SAFE TO USE
* WITHOUT SERIOUS PRECAUTIONS.
*
* Anything that is used by filesystem must either
* be already taken away (and fs must survive that)
* or have its destruction delayed until the superblock
* shutdown.
*
*/
kern_unmount_rcu(ns->mq_mnt);
}
would suffice. free_ipc_work/free_ipc/mnt_llist can be killed off.
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
In reply to Rik van Rielinclude/linux/mount.h struct vfsmount { · line 78
Quoted 8 lines · Rik van Riel
struct
super_block;
struct vfsmount;include/linux/mount.h +1
-0@@ -73,6 +74,7 @@ struct vfsmount {struct super_block *mnt_sb;/* pointer to superblock */ int mnt_flags;struct user_namespace *mnt_userns; +struct rcu_work free_rwork; } __randomize_layout;
Wait, what? First of all, that has no business being in vfsmount - everything that deeply internal belongs in struct mount, not in its public part. Moreover, there's already mount->mnt_rcu, so what's the point duplicating that?
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 15 lines · Al Viro
include/linux/mount.h struct vfsmount { · line 78
Quoted 8 lines · Rik van Riel
struct
super_block;
struct vfsmount;include/linux/mount.h +1
-0@@ -73,6 +74,7 @@ struct vfsmount {struct super_block *mnt_sb;/* pointer to superblock */ int mnt_flags;struct user_namespace *mnt_userns; +struct rcu_work free_rwork; } __randomize_layout;Wait, what? First of all, that has no business being in vfsmount - everything that deeply internal belongs in struct mount, not in its public part. Moreover, there's already
mount->mnt_rcu, so what's the point duplicating that?
Argh... You need rcu_work there...
OK, so make that a member of the same union mnt_rcu is. In struct mount, please. And I'm not sure I like the idea of shoving that much into struct mount, TBH...
Al Viro
maintainerRe: [PATCH 1/2] vfs: free vfsmount through rcu work from kern_unmount
Quoted 17 lines · Al Viro
Quoted 15 lines · Al Viro
include/linux/mount.h struct vfsmount { · line 78
Quoted 8 lines · Rik van Riel
struct
super_block;
struct vfsmount;include/linux/mount.h +1
-0@@ -73,6 +74,7 @@ struct vfsmount {struct super_block *mnt_sb;/* pointer to superblock */ int mnt_flags;struct user_namespace *mnt_userns; +struct rcu_work free_rwork; } __randomize_layout;Wait, what? First of all, that has no business being in vfsmount - everything that deeply internal belongs in struct mount, not in its public part. Moreover, there's already
mount->mnt_rcu, so what's the point duplicating that?
Quoted 5 lines
Argh... You need
rcu_workthere...OK, so make that a member of the same union
mnt_rcuis. In struct mount, please. And I'm not sure I like the idea of shoving that much into struct mount, TBH...
We might have a plenty of struct mount instances. Very few of them will ever be internal, in the first place. Fewer yet - using kern_unmount_rcu(). And it's not small. If anything, I would consider something like
call_rcu(&m->mnt_rcu, callback)
with callback adding struct mount to llist and doing schedule_delayed_work() on that. With work consisting of doing mntput on everything in it.
I'll get some sleep and put together something along those lines tomorrow morning.
PATCH 2/2 ipc: get rid of free_ipc_work workqueue
In reply to Rik van RielWith kern_unmount deferring the freeing of the vfsmount structure through queue_rcu_work, we no longer need a separate workqueue for freeing up ipc_namespace structures.
--- include/linux/ipc_namespace.h | 2 -- ipc/namespace.c | 21 +-------------------- 2 files changed, 1 insertion(+), 22 deletions(-)
include/linux/ipc_namespace.h +0 -2
@@ -67,8 +67,6 @@ struct ipc_namespace {struct user_namespace *user_ns;struct ucounts *ucounts; -struct llist_node mnt_llist; -struct ns_common ns; } __randomize_layout;
ipc/namespace.c +1 -20
@@ -117,9 +117,6 @@ void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,static void free_ipc_ns (struct ipc_namespace *ns) { -/* mq_put_mnt() waits for a grace period as kern_unmount() -* uses synchronize_rcu(). -*/ mq_put_mnt (ns);sem_exit_ns (ns);msg_exit_ns (ns);
@@ -131,21 +128,6 @@ static void free_ipc_ns(struct ipc_namespace *ns)kfree (ns); } -static LLIST_HEAD (free_ipc_list); -static void free_ipc (struct work_struct *unused) -{ -struct llist_node *node =llist_del_all (&free_ipc_list); -struct ipc_namespace *n, *t; - -llist_for_each_entry_safe (n, t, node, mnt_llist) -free_ipc_ns (n); -} - -/* -* The work queue is used to avoid the cost of synchronize_rcu in kern_unmount. -*/ -static DECLARE_WORK(free_ipc_work, free_ipc); -/* * put_ipc_ns - drop a reference to an ipc namespace. * @ns: the namespace to put
@@ -168,8 +150,7 @@ void put_ipc_ns(struct ipc_namespace *ns)mq_clear_sbinfo (ns);spin_unlock (&mq_lock); -if (llist_add (&ns->mnt_llist, &free_ipc_list)) -schedule_work (&free_ipc_work); +free_ipc_ns (ns); } }
Signature · 1 line
2.34.1