Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ipc/namespace.c: use a work queue to free_ipc

the reason is to avoid a delay caused by the synchronize_rcu() call in
kern_umount() when the mqueue mount is freed.

the code:

#define _GNU_SOURCE
#include <sched.h>
#include <error.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
int i;

for (i = 0; i < 1000; i++)
if (unshare(CLONE_NEWIPC) < 0)
error(EXIT_FAILURE, errno, "unshare");
}

goes from

Command being timed: "./ipc-namespace"
User time (seconds): 0.00
System time (seconds): 0.06
Percent of CPU this job got: 0%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:08.05

to

Command being timed: "./ipc-namespace"
User time (seconds): 0.00
System time (seconds): 0.02
Percent of CPU this job got: 96%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.03

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Paul E. McKenney <paulmck@kernel.org>
Reviewed-by: Waiman Long <longman@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Link: http://lkml.kernel.org/r/20200225145419.527994-1-gscrivan@redhat.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Giuseppe Scrivano and committed by
Linus Torvalds
e1eb26fa 4b78e201

+24 -2
+2
include/linux/ipc_namespace.h
··· 68 68 struct user_namespace *user_ns; 69 69 struct ucounts *ucounts; 70 70 71 + struct llist_node mnt_llist; 72 + 71 73 struct ns_common ns; 72 74 } __randomize_layout; 73 75
+22 -2
ipc/namespace.c
··· 117 117 118 118 static void free_ipc_ns(struct ipc_namespace *ns) 119 119 { 120 + /* mq_put_mnt() waits for a grace period as kern_unmount() 121 + * uses synchronize_rcu(). 122 + */ 123 + mq_put_mnt(ns); 120 124 sem_exit_ns(ns); 121 125 msg_exit_ns(ns); 122 126 shm_exit_ns(ns); ··· 130 126 ns_free_inum(&ns->ns); 131 127 kfree(ns); 132 128 } 129 + 130 + static LLIST_HEAD(free_ipc_list); 131 + static void free_ipc(struct work_struct *unused) 132 + { 133 + struct llist_node *node = llist_del_all(&free_ipc_list); 134 + struct ipc_namespace *n, *t; 135 + 136 + llist_for_each_entry_safe(n, t, node, mnt_llist) 137 + free_ipc_ns(n); 138 + } 139 + 140 + /* 141 + * The work queue is used to avoid the cost of synchronize_rcu in kern_unmount. 142 + */ 143 + static DECLARE_WORK(free_ipc_work, free_ipc); 133 144 134 145 /* 135 146 * put_ipc_ns - drop a reference to an ipc namespace. ··· 167 148 if (refcount_dec_and_lock(&ns->count, &mq_lock)) { 168 149 mq_clear_sbinfo(ns); 169 150 spin_unlock(&mq_lock); 170 - mq_put_mnt(ns); 171 - free_ipc_ns(ns); 151 + 152 + if (llist_add(&ns->mnt_llist, &free_ipc_list)) 153 + schedule_work(&free_ipc_work); 172 154 } 173 155 } 174 156