posix mqueue namespace (v14)
cover posix mqueue namespace (v14)
IPC namespaces are completely disjoint id->object mappings. A task can pass CLONE_NEWIPC to unshare and clone to get a new, empty, IPC namespace. Until now this has supported SYSV IPC.
Most Posix IPC is done in userspace. The posix mqueue support, however, is implemented on top of the mqueue fs.
This patchset implements multiple mqueue fs instances, one per IPC namespace to be precise.
To create a new ipc namespace with posix mq support, you should now:
unshare(CLONE_NEWIPC|CLONE_NEWNS); umount /dev/mqueue mount -t mqueue mqueue /dev/mqueue
It's perfectly valid to do vfs operations on files in another ipc_namespace's /dev/mqueue, but any use of mq_open(3) and friends will act in your own ipc_ns. After the ipc namespace has exited, you can still unlink but no longer create files in that fs (since accounting is carried.
Changelog:
v14: (Jan 16 2009) port to linux-next v13: (Dec 28 2009) 1. addressed comments by Dave and Suka 2. ported Cedric's patch to make posix mq sysctls per-namespace
When convenient, it would be great to see this tested in -mm.
thanks,
-serge
PATCH 1/3 mqueue ns: move mqueue_mnt into struct ipc_namespace
Move mqueue vfsmount plus a few tunables into the ipc_namespace struct. The CONFIG_IPC_NS boolean and the ipc_namespace struct will serve both the posix message queue namespaces and the SYSV ipc namespaces.
Changelog:
Dec 18: fix dummy fn #defines
--- include/linux/ipc_namespace.h | 32 ++++++++++-- init/Kconfig | 4 +- ipc/mqueue.c | 116 ++++++++++++++++++++++------------------- ipc/msgutil.c | 21 +++++++ ipc/namespace.c | 2 + ipc/util.c | 9 --- ipc/util.h | 15 +++++ 7 files changed, 131 insertions(+), 68 deletions(-)
include/linux/ipc_namespace.h +28 -4
@@ -44,24 +44,48 @@ struct ipc_namespace {int shm_tot;struct notifier_block ipcns_nb; + +struct vfsmount *mq_mnt; +unsigned int mq_queues_count; +unsigned int mq_queues_max; +unsigned int mq_msg_max; +unsigned int mq_msgsize_max; + };extern struct ipc_namespace init_ipc_ns;extern atomic_t nr_ipc_ns; -#ifdef CONFIG_SYSVIPC +#if defined(CONFIG_POSIX_MQUEUE ) || defined(CONFIG_SYSVIPC )#define INIT_IPC_NS (ns) .ns = &init_ipc_ns, +#else +#define INIT_IPC_NS (ns) +#endif +#ifdef CONFIG_SYSVIPC extern int register_ipcns_notifier (struct ipc_namespace *);extern int cond_register_ipcns_notifier (struct ipc_namespace *);extern void unregister_ipcns_notifier (struct ipc_namespace *);extern int ipcns_notify (unsigned long ); -#else /* CONFIG_SYSVIPC */ -#define INIT_IPC_NS (ns) +#define register_ipcns_notifier (ns) (0) +#define cond_register_ipcns_notifier (ns) (0) +#define unregister_ipcns_notifier (ns) ((void) 0) +#define ipcns_notify (l) (0)#endif /* CONFIG_SYSVIPC */ -#if defined(CONFIG_SYSVIPC ) && defined(CONFIG_IPC_NS ) +#ifdef CONFIG_POSIX_MQUEUE +extern void mq_init_ns (struct ipc_namespace *ns); +/* default values */ +#define DFLT_QUEUESMAX 256/* max number of message queues */ +#define DFLT_MSGMAX 10/* max number of messages in each queue */ +#define HARD_MSGMAX (131072/sizeof(void *)) +#define DFLT_MSGSIZEMAX 8192/* max message size */ +#else +#define mq_init_ns (ns) ((void) 0) +#endif + +#if defined(CONFIG_IPC_NS )extern void free_ipc_ns (struct kref *kref);extern struct ipc_namespace *copy_ipcs (unsigned long flags,struct ipc_namespace *ns);
init/Kconfig +2 -2
@@ -517,10 +517,10 @@ config UTS_NS config IPC_NS bool "IPC namespace" - depends on NAMESPACES && SYSVIPC + depends on NAMESPACES && (SYSVIPC || POSIX_MQUEUE) help In this namespace tasks work with IPC ids which correspond to - different IPC objects in different namespaces + different IPC objects in different namespaces. config USER_NS bool "User namespace (EXPERIMENTAL)"
ipc/mqueue.c +63 -53
336 lines not on this page · raw message
ipc/msgutil.c +21 -0
@@ -13,10 +13,31 @@#include <linux/security.h> #include <linux/slab.h> #include <linux/ipc.h> +#include <linux/ipc_namespace.h> #include <asm/uaccess.h> #include "util.h" +/* +* The next 2 defines are here bc this is the only file +* compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE +* and not CONFIG_IPC_NS. +*/ +struct ipc_namespace init_ipc_ns = { + .kref = { + .refcount =ATOMIC_INIT (2 ), + }, +#ifdef CONFIG_POSIX_MQUEUE + .mq_mnt =NULL , + .mq_queues_count =0 , + .mq_queues_max =DFLT_QUEUESMAX , + .mq_msg_max =DFLT_MSGMAX , + .mq_msgsize_max =DFLT_MSGSIZEMAX , +#endif +}; + +atomic_t nr_ipc_ns =ATOMIC_INIT (1 ); +struct msg_msgseg {struct msg_msgseg * next;/* the next part of the message follows immediately */
ipc/namespace.c +2 -0
@@ -25,6 +25,7 @@ static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)sem_init_ns (ns);msg_init_ns (ns);shm_init_ns (ns); +mq_init_ns (ns);/* * msgmni has already been computed for the new ipc ns.
@@ -101,6 +102,7 @@ void free_ipc_ns(struct kref *kref)sem_exit_ns (ns);msg_exit_ns (ns);shm_exit_ns (ns); +mq_exit_ns (ns);kfree (ns);atomic_dec (&nr_ipc_ns);
ipc/util.c +0 -9
@@ -47,15 +47,6 @@ struct ipc_proc_iface {int (*show)(struct seq_file *,void *); }; -struct ipc_namespace init_ipc_ns = { - .kref = { - .refcount =ATOMIC_INIT (2 ), - }, -}; - -atomic_t nr_ipc_ns =ATOMIC_INIT (1 ); - -#ifdef CONFIG_MEMORY_HOTPLUG static void ipc_memory_notifier (struct work_struct *work)
ipc/util.h +15 -0
@@ -20,6 +20,13 @@ void shm_init (void);struct ipc_namespace ; +#ifdef CONFIG_POSIX_MQUEUE +void mq_exit_ns (struct ipc_namespace *ns); +#else +#define mq_exit_ns (ns) ((void) 0) +#endif + +#ifdef CONFIG_SYSVIPC void sem_init_ns (struct ipc_namespace *ns);void msg_init_ns (struct ipc_namespace *ns);void shm_init_ns (struct ipc_namespace *ns);
@@ -27,6 +34,14 @@ void shm_init_ns(struct ipc_namespace *ns);void sem_exit_ns (struct ipc_namespace *ns);void msg_exit_ns (struct ipc_namespace *ns);void shm_exit_ns (struct ipc_namespace *ns); +#else +#define sem_init_ns (ns) ((void) 0) +#define msg_init_ns (ns) ((void) 0) +#define shm_init_ns (ns) ((void) 0) +#define sem_exit_ns (ns) ((void) 0) +#define msg_exit_ns (ns) ((void) 0) +#define shm_exit_ns (ns) ((void) 0) +#endif /* * Structure that holds the parameters needed by the ipc operations
Signature · 1 line
1.5.4.3
Re: [PATCH 1/3] mqueue ns: move mqueue_mnt into struct ipc_namespace
Quoted 13 lines · On Fri, 16 Jan 2009 20:03:22 -0600 "Serge E. Hallyn" <[email protected]> wrote:
Move mqueue vfsmount plus a few tunables into the
ipc_namespacestruct. TheCONFIG_IPC_NSboolean and theipc_namespacestruct will serve both the posix message queue namespaces and the SYSV ipc namespaces....
include/linux/ipc_namespace.h +0
-0@@ -44,24 +44,48 @@ struct ipc_namespace {int shm_tot;
include/linux/ipc_namespace.h struct ipc_namespace { · line 54
Quoted 10 lines
include/linux/ipc_namespace.h +7
-0struct notifier_block ipcns_nb; + +struct vfsmount *mq_mnt; +unsigned int mq_queues_count; +unsigned int mq_queues_max; +unsigned int mq_msg_max; +unsigned int mq_msgsize_max; + };
Some documentation for the new fields would be nice. It should mention the locking protocol for those fields.
Quoted 12 lines
include/linux/ipc_namespace.h +5
-1extern struct ipc_namespace init_ipc_ns;extern atomic_t nr_ipc_ns; -#ifdef CONFIG_SYSVIPC +#if defined(CONFIG_POSIX_MQUEUE ) || defined(CONFIG_SYSVIPC )#define INIT_IPC_NS (ns) .ns = &init_ipc_ns, +#else +#define INIT_IPC_NS (ns) +#endif +#ifdef CONFIG_SYSVIPC extern int register_ipcns_notifier (struct ipc_namespace *);
include/linux/ipc_namespace.h struct ipc_namespace { · line 74
Quoted 10 lines
include/linux/ipc_namespace.h +4
-2extern int cond_register_ipcns_notifier (struct ipc_namespace *);extern void unregister_ipcns_notifier (struct ipc_namespace *);extern int ipcns_notify (unsigned long ); -#else /* CONFIG_SYSVIPC */ -#define INIT_IPC_NS (ns) +#define register_ipcns_notifier (ns) (0) +#define cond_register_ipcns_notifier (ns) (0) +#define unregister_ipcns_notifier (ns) ((void) 0) +#define ipcns_notify (l) (0)
Can we use inlines here? We get typechecking..
Quoted 19 lines
include/linux/ipc_namespace.h +13
-1#endif /* CONFIG_SYSVIPC */ -#if defined(CONFIG_SYSVIPC ) && defined(CONFIG_IPC_NS ) +#ifdef CONFIG_POSIX_MQUEUE +extern void mq_init_ns (struct ipc_namespace *ns); +/* default values */ +#define DFLT_QUEUESMAX 256/* max number of message queues */ +#define DFLT_MSGMAX 10/* max number of messages in each queue */ +#define HARD_MSGMAX (131072/sizeof(void *)) +#define DFLT_MSGSIZEMAX 8192/* max message size */ +#else +#define mq_init_ns (ns) ((void) 0) +#endif + +void mq_init_ns (struct ipc_namespace *ns) { + ns->mq_queues_count =0 ;
include/linux/ipc_namespace.h struct ipc_namespace {
Quoted 10 lines
include/linux/ipc_namespace.h +10
-0+ ns->mq_queues_max =DFLT_QUEUESMAX ; + ns->mq_msg_max =DFLT_MSGMAX ; + ns->mq_msgsize_max =DFLT_MSGSIZEMAX ; + ns->mq_mnt =mntget (init_ipc_ns.mq_mnt); +} + +void mq_exit_ns (struct ipc_namespace *ns) { +/* will need to clear out ns->mq_mnt->mnt_sb->s_fs_info here */ +mntput (ns->mq_mnt); +}
You might want to ask checkpatch about this, please.
Quoted 29 lines
static struct inode *mqueue_get_inode(struct
super_block*sb, int mode,include/linux/ipc_namespace.h +4
-2struct mq_attr *attr) {struct user_struct *u =current_user ();struct inode *inode; +struct ipc_namespace *ipc_ns = &init_ipc_ns; inode =new_inode (sb);if (inode) {@@ -141,8 +144,8 @@ static struct inode *mqueue_get_inode(struct super_block *sb, int mode, info->qsize =0 ; info->user =NULL ;/* set when all is ok */ memset (&info->attr,0 ,sizeof (info->attr)); - info->attr.mq_maxmsg = msg_max; - info->attr.mq_msgsize = msgsize_max; + info->attr.mq_maxmsg = ipc_ns->mq_msg_max; + info->attr.mq_msgsize = ipc_ns->mq_msgsize_max;if (attr) { info->attr.mq_maxmsg = attr->mq_maxmsg; info->attr.mq_msgsize = attr->mq_msgsize;@@ -242,6 +245,7 @@ static void mqueue_delete_inode(struct inode *inode)struct user_struct *user;unsigned long mq_bytes;int i; +struct ipc_namespace *ipc_ns = &init_ipc_ns;if (S_ISDIR (inode->i_mode)) {clear_inode (inode);
include/linux/ipc_namespace.h static void mqueue_delete_inode(struct inode *inode) · line 269
Quoted 5 lines
include/linux/ipc_namespace.h +1
-1@@ -262,7 +266,7 @@ static void mqueue_delete_inode(struct inode *inode)if (user) {spin_lock (&mq_lock); user->mq_bytes -= mq_bytes; - queues_count--; + ipc_ns->mq_queues_count--;
ah-hah. mqueue_lock!
Quoted 17 lines
include/linux/ipc_namespace.h +3
-2spin_unlock (&mq_lock);free_uid (user); }@@ -274,20 +278,22 @@ static int mqueue_create(struct inode *dir, struct dentry *dentry,struct inode *inode;struct mq_attr *attr = dentry->d_fsdata;int error; +struct ipc_namespace *ipc_ns = &init_ipc_ns;spin_lock (&mq_lock); -if (queues_count >= queues_max && !capable (CAP_SYS_RESOURCE )) { +if (ipc_ns->mq_queues_count >= ipc_ns->mq_queues_max && + !capable (CAP_SYS_RESOURCE )) { error = -ENOSPC ; goto out_lock; } - queues_count++;
include/linux/ipc_namespace.h static int mqueue_create(struct inode *dir, struct dentry *dentry, · line 297
Quoted 10 lines
include/linux/ipc_namespace.h +2
-1+ ipc_ns->mq_queues_count++;spin_unlock (&mq_lock); inode =mqueue_get_inode (dir->i_sb, mode, attr);if (!inode) { error = -ENOMEM ;spin_lock (&mq_lock); - queues_count--; + ipc_ns->mq_queues_count--; goto out_lock;
hm. That should have been called out_unlock, conventionally.
Quoted 27 lines
include/linux/ipc_namespace.h +2
-2}@@ -562,7 +568,7 @@ static void remove_notification(struct mqueue_inode_info *info) info->notify_owner =NULL ; }@@ -1212,14 +1222,14 @@ static int msg_maxsize_limit_max = MAX_MSGSIZEMAX;static ctl_table mq_sysctls[] = { { .procname ="queues_max" , - .data = &queues_max, + .data = &init_ipc_ns.mq_queues_max, .maxlen =sizeof (int ), .mode =0644 , .proc_handler = &proc_dointvec, }, { .procname ="msg_max" , - .data = &msg_max, + .data = &init_ipc_ns.mq_msg_max, .maxlen =sizeof (int ), .mode =0644 , .proc_handler = &proc_dointvec_minmax,
include/linux/ipc_namespace.h static ctl_table mq_sysctls[] = { · line 1244
Quoted 8 lines
include/linux/ipc_namespace.h +1
-1@@ -1228,7 +1238,7 @@ static ctl_table mq_sysctls[] = { }, { .procname = "msgsize_max", - .data = &msgsize_max, + .data = &init_ipc_ns.mq_msgsize_max, .maxlen = sizeof(int), .mode = 0644, .proc_handler = &proc_dointvec_minmax,
So the sysctl has new semantics. Some description of the designed decisions here would be appropriate.
If I'm running in IPC namespace "foo" and I modify msg_max, it seems that this will alter the tunable on the top-level namespace but will leave my namespace unaltered. Surprise?
(comes back after reading patch #3)
Ah. This seems to have been secretly fixed in that patch?
include/linux/ipc_namespace.h
Quoted 11 lines
...
include/linux/ipc_namespace.h +8
-0+/* +* The next 2 defines are here bc this is the only file +* compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE +* and not CONFIG_IPC_NS. +*/ +struct ipc_namespace init_ipc_ns = { + .kref = { + .refcount =ATOMIC_INIT (2 ),
2? How come? Needs a comment?
Quoted 40 lines
include/linux/ipc_namespace.h +12
-0+ }, +#ifdef CONFIG_POSIX_MQUEUE + .mq_mnt = NULL, + .mq_queues_count = 0, + .mq_queues_max = DFLT_QUEUESMAX, + .mq_msg_max = DFLT_MSGMAX, + .mq_msgsize_max = DFLT_MSGSIZEMAX, +#endif +}; + +atomic_t nr_ipc_ns =ATOMIC_INIT (1 ); +struct msg_msgseg {struct msg_msgseg * next;/* the next part of the message follows immediately */ ipc/namespace.c +2
-0@@ -25,6 +25,7 @@ static struct ipc_namespace *clone_ipc_ns(struct ipc_namespace *old_ns)sem_init_ns (ns);msg_init_ns (ns);shm_init_ns (ns); +mq_init_ns (ns);/* * msgmni has already been computed for the new ipc ns. @@ -101,6 +102,7 @@ void free_ipc_ns(struct kref *kref)sem_exit_ns (ns);msg_exit_ns (ns);shm_exit_ns (ns); +mq_exit_ns (ns);kfree (ns);atomic_dec (&nr_ipc_ns);ipc/util.c +0
-0
ipc/util.c struct ipc_proc_iface { · line 52
Quoted 6 lines
ipc/util.c +0
-3@@ -47,15 +47,6 @@ struct ipc_proc_iface {int (*show)(struct seq_file *,void *); }; -struct ipc_namespace init_ipc_ns = { - .kref = { - .refcount =ATOMIC_INIT (2 ),
OK, I missed that last time ;)
Quoted 28 lines
ipc/util.c +0
-6- }, -}; - -atomic_t nr_ipc_ns =ATOMIC_INIT (1 ); - -#ifdef CONFIG_MEMORY_HOTPLUG static void ipc_memory_notifier (struct work_struct *work)ipc/util.h +7
-0@@ -20,6 +20,13 @@ void shm_init (void);struct ipc_namespace ; +#ifdef CONFIG_POSIX_MQUEUE +void mq_exit_ns (struct ipc_namespace *ns); +#else +#define mq_exit_ns (ns) ((void) 0) +#endif + +#ifdef CONFIG_SYSVIPC void sem_init_ns (struct ipc_namespace *ns);void msg_init_ns (struct ipc_namespace *ns);void shm_init_ns (struct ipc_namespace *ns);
ipc/util.h void shm_init_ns(struct ipc_namespace *ns); · line 43
Quoted 10 lines
ipc/util.h +7
-0@@ -27,6 +34,14 @@ void shm_init_ns(struct ipc_namespace *ns);void sem_exit_ns (struct ipc_namespace *ns);void msg_exit_ns (struct ipc_namespace *ns);void shm_exit_ns (struct ipc_namespace *ns); +#else +#define sem_init_ns (ns) ((void) 0) +#define msg_init_ns (ns) ((void) 0) +#define shm_init_ns (ns) ((void) 0) +#define sem_exit_ns (ns) ((void) 0) +#define msg_exit_ns (ns) ((void) 0) +#define shm_exit_ns (ns) ((void) 0)
Again, could be written in C.
Quoted 1 line
ipc/util.h +1
-0+#endif
PATCH 2/3 ipc namespaces: implement support for posix msqueues
In reply to Serge E. HallynImplement multiple mounts of the mqueue file system, and link it to usage of CLONE_NEWIPC.
Each ipc ns has a corresponding mqueuefs superblock. When a user does clone(CLONE_NEWIPC) or unshare(CLONE_NEWIPC), the unshare will cause an internal mount of a new mqueuefs sb linked to the new ipc ns.
When a user does 'mount -t mqueue mqueue /dev/mqueue', he mounts the mqueuefs superblock.
Posix message queues can be worked with both through the mq_* system calls (see mq_overview(7)), and through the VFS through the mqueue mount. Any usage of mq_open() and friends will work with the acting task's ipc namespace. Any actions through the VFS will work with the mqueuefs in which the file was created. So if a user doesn't remount mqueuefs after unshare(CLONE_NEWIPC), mq_open("/ab") will not be reflected in "ls /dev/mqueue".
If task a mounts mqueue for ipc_ns:1, then clones task b with a new ipcns, ipcns:2, and then task a is the last task in ipc_ns:1 to exit, then (1) ipc_ns:1 will be freed, (2) it's superblock will live on until task b umounts the corresponding mqueuefs, and vfs actions will continue to succeed, but (3) sb->s_fs_info will be NULL for the sb corresponding to the deceased ipc_ns:1.
To make this happen, we must protect the ipc reference count when
a. a task exits and drops its ipcns->count, since
it might be dropping it to 0 and freeing the ipcns
b. a task accesses the ipcns through its mqueuefs
interface, since it bumps the ipcns refcount and might race with the last task in the ipcns exiting.
So the kref is changed to an atomic_t so we can use atomic_dec_and_lock(&ns->count,mq_lock), and every access to the ipcns through ns = mqueuefs_sb->s_fs_info is protected by the same lock.
Changelog:
Dec 19: Commented put_ipc_ns (As per Dave Hansen comment)
Addressed nits (per Sukadev Bhattiprolu comment)
Dec 17: removed unused static fn (get_ipcns_from_sb)
--- include/linux/ipc_namespace.h | 16 ++--- ipc/mqueue.c | 140 ++++++++++++++++++++++++++++++++--------- ipc/msgutil.c | 8 +-- ipc/namespace.c | 41 ++++++++++-- ipc/util.h | 6 +- 5 files changed, 160 insertions(+), 51 deletions(-)
include/linux/ipc_namespace.h +7 -9
@@ -25,7 +25,7 @@ struct ipc_ids { };struct ipc_namespace { -struct kref kref; +atomic_t count;struct ipc_ids ids[3 ];int sem_ctls[4 ];
@@ -56,6 +56,7 @@ struct ipc_namespace {extern struct ipc_namespace init_ipc_ns;extern atomic_t nr_ipc_ns; +extern spinlock_t mq_lock;#if defined(CONFIG_POSIX_MQUEUE ) || defined(CONFIG_SYSVIPC )#define INIT_IPC_NS (ns) .ns = &init_ipc_ns,#else
@@ -75,18 +76,18 @@ extern int ipcns_notify(unsigned long);#endif /* CONFIG_SYSVIPC */ #ifdef CONFIG_POSIX_MQUEUE -extern void mq_init_ns (struct ipc_namespace *ns); +extern int mq_init_ns (struct ipc_namespace *ns);/* default values */ #define DFLT_QUEUESMAX 256/* max number of message queues */ #define DFLT_MSGMAX 10/* max number of messages in each queue */ #define HARD_MSGMAX (131072/sizeof(void *))#define DFLT_MSGSIZEMAX 8192/* max message size */ #else -#define mq_init_ns (ns) ((void) 0) +#define mq_init_ns (ns) (0)#endif #if defined(CONFIG_IPC_NS ) -extern void free_ipc_ns (struct kref *kref); +extern void free_ipc_ns (struct ipc_namespace *ns);extern struct ipc_namespace *copy_ipcs (unsigned long flags,struct ipc_namespace *ns); extern void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
@@ -96,14 +97,11 @@ extern void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,static inline struct ipc_namespace *get_ipc_ns (struct ipc_namespace *ns) {if (ns) -kref_get (&ns->kref); +atomic_inc (&ns->count);return ns; } -static inline void put_ipc_ns (struct ipc_namespace *ns) -{ -kref_put (&ns->kref, free_ipc_ns); -} +extern void put_ipc_ns (struct ipc_namespace *ns);#else static inline struct ipc_namespace *copy_ipcs (unsigned long flags,struct ipc_namespace *ns)
ipc/mqueue.c +111 -29
258 lines not on this page · raw message
ipc/msgutil.c +3 -5
@@ -18,18 +18,16 @@#include "util.h" +spinlock_t mq_lock; +/* * The next 2 defines are here bc this is the only file * compiled when either CONFIG_SYSVIPC and CONFIG_POSIX_MQUEUE * and not CONFIG_IPC_NS. */ struct ipc_namespace init_ipc_ns = { - .kref = { - .refcount =ATOMIC_INIT (2 ), - }, + .count =ATOMIC_INIT (2 ), #ifdef CONFIG_POSIX_MQUEUE - .mq_mnt =NULL , - .mq_queues_count =0 , .mq_queues_max =DFLT_QUEUESMAX , .mq_msg_max =DFLT_MSGMAX , .mq_msgsize_max = DFLT_MSGSIZEMAX,
ipc/namespace.c +35 -6
83 lines not on this page · raw message
ipc/util.h +4 -2
@@ -21,9 +21,11 @@ void shm_init (void);struct ipc_namespace ;#ifdef CONFIG_POSIX_MQUEUE -void mq_exit_ns (struct ipc_namespace *ns); +extern void mq_clear_sbinfo (struct ipc_namespace *ns); +extern void mq_put_mnt (struct ipc_namespace *ns);#else -#define mq_exit_ns (ns) ((void) 0) +#define mq_clear_sbinfo (ns) ((void) 0) +#define mq_put_mnt (ns) ((void) 0)#endif #ifdef CONFIG_SYSVIPC
Signature · 1 line
1.5.4.3
Re: [PATCH 2/3] ipc namespaces: implement support for posix msqueues
Quoted 20 lines · On Fri, 16 Jan 2009 20:03:32 -0600 "Serge E. Hallyn" <[email protected]> wrote:
Implement multiple mounts of the mqueue file system, and link it to usage of
CLONE_NEWIPC.Each ipc ns has a corresponding mqueuefs superblock. When a user does
clone(CLONE_NEWIPC)orunshare(CLONE_NEWIPC), the unshare will cause an internal mount of a new mqueuefs sb linked to the new ipc ns.When a user does 'mount -t mqueue mqueue
/dev/mqueue', he mounts the mqueuefs superblock.Posix message queues can be worked with both through the mq_* system calls (see
mq_overview(7)), and through the VFS through the mqueue mount. Any usage ofmq_open()and friends will work with the acting task's ipc namespace. Any actions through the VFS will work with the mqueuefs in which the file was created. So if a user doesn't remount mqueuefs afterunshare(CLONE_NEWIPC),mq_open("/ab")will not be reflected in "ls/dev/mqueue".
Quoted 7 lines
If task a mounts mqueue for ipc_ns:1, then clones task b with a new ipcns, ipcns:2, and then task a is the last task in ipc_ns:1 to exit, then (1) ipc_ns:1 will be freed, (2) it's superblock will live on until task b umounts the corresponding mqueuefs, and vfs actions will continue to succeed, but (3)
sb->s_fs_infowill be NULL for the sb corresponding to the deceased ipc_ns:1.
I suppose that stuff like this should find its way into a manpage one day. That'll be fun for someone.
Quoted 26 lines
...
include/linux/ipc_namespace.h +2
-1@@ -25,7 +25,7 @@ struct ipc_ids { };struct ipc_namespace { -struct kref kref; +atomic_t count;struct ipc_ids ids[3 ];int sem_ctls[4 ];@@ -56,6 +56,7 @@ struct ipc_namespace {extern struct ipc_namespace init_ipc_ns;extern atomic_t nr_ipc_ns; +extern spinlock_t mq_lock;#if defined(CONFIG_POSIX_MQUEUE ) || defined(CONFIG_SYSVIPC )#define INIT_IPC_NS (ns) .ns = &init_ipc_ns,#else @@ -75,18 +76,18 @@ extern int ipcns_notify(unsigned long);#endif /* CONFIG_SYSVIPC */ #ifdef CONFIG_POSIX_MQUEUE
include/linux/ipc_namespace.h extern int ipcns_notify(unsigned long); · line 86
Quoted 10 lines
include/linux/ipc_namespace.h +2
-2-extern void mq_init_ns (struct ipc_namespace *ns); +extern int mq_init_ns (struct ipc_namespace *ns);/* default values */ #define DFLT_QUEUESMAX 256/* max number of message queues */ #define DFLT_MSGMAX 10/* max number of messages in each queue */ #define HARD_MSGMAX (131072/sizeof(void *))#define DFLT_MSGSIZEMAX 8192/* max message size */ #else -#define mq_init_ns (ns) ((void) 0) +#define mq_init_ns (ns) (0)
argh
Quoted 21 lines
include/linux/ipc_namespace.h +7
-3#endif #if defined(CONFIG_IPC_NS ) -extern void free_ipc_ns (struct kref *kref); +extern void free_ipc_ns (struct ipc_namespace *ns);extern struct ipc_namespace *copy_ipcs (unsigned long flags,struct ipc_namespace *ns);extern void free_ipcs (struct ipc_namespace *ns,struct ipc_ids *ids, +/* +* This routine should be called with the mq_lock held. +*/ +static inline struct ipc_namespace *__get_ns_from_inode(struct inode *inode) +{ +return get_ipc_ns (inode->i_sb->s_fs_info); } -void mq_exit_ns(struct ipc_namespace *ns) { -/* will need to clear out ns->mq_mnt->mnt_sb->s_fs_info here */
include/linux/ipc_namespace.h extern int ipcns_notify(unsigned long);
Quoted 10 lines
include/linux/ipc_namespace.h +8
-1-mntput (ns->mq_mnt); +static inline struct ipc_namespace *get_ns_from_inode (struct inode *inode) +{ +struct ipc_namespace *ns; + +spin_lock (&mq_lock); + ns =__get_ns_from_inode (inode); +spin_unlock (&mq_lock); +return ns; }
No need for the explicit inlining here.
Quoted 28 lines
...
include/linux/ipc_namespace.h +25
-0+static int compare_sb_single_ns (struct super_block *sb,void *data) +{ +return sb->s_fs_info == data; +} + +static int set_sb_single_ns (struct super_block *sb,void *data) +{ + sb->s_fs_info = data; +return set_anon_super (sb,NULL ); +} + +static int get_sb_single_ns (struct file_system_type *fs_type, +int flags,void *data, +int (*fill_super)(struct super_block *,void *,int ), +struct vfsmount *mnt) +{ +struct super_block *s; +int error; + + s =sget (fs_type, compare_sb_single_ns, set_sb_single_ns, data); +if (IS_ERR (s)) +return PTR_ERR (s); +if (!s->s_root) { + s->s_flags = flags; + error =fill_super (s, data, flags &MS_SILENT ?1 :0 );
include/linux/ipc_namespace.h
Quoted 10 lines
include/linux/ipc_namespace.h +9
-0+if (error) { +up_write (&s->s_umount); +deactivate_super (s); +return error; + } + s->s_flags |=MS_ACTIVE ; + } +do_remount_sb (s, flags, data,0 ); +return simple_set_mnt (mnt, s); }
The above doesn't seem specific to mqueue. Is it in the best place?
Quoted 29 lines
static int mqueue_get_sb(struct
file_system_type*fs_type,include/linux/ipc_namespace.h +7
-2int flags, const char *dev_name, void *data, struct vfsmount *mnt) { - return get_sb_single(fs_type, flags, data, mqueue_fill_super, mnt); + struct ipc_namespace *ns = data; + +if (!(flags &MS_KERNMOUNT )) + ns = current->nsproxy->ipc_ns; +return get_sb_single_ns (fs_type, flags, ns, mqueue_fill_super, mnt); }static void init_once (void *foo)@@ -245,12 +295,13 @@ static void mqueue_delete_inode(struct inode *inode)struct user_struct *user;unsigned long mq_bytes;int i; -struct ipc_namespace *ipc_ns = &init_ipc_ns; +struct ipc_namespace *ipc_ns;if (S_ISDIR (inode->i_mode)) {clear_inode (inode);return ; } + ipc_ns =get_ns_from_inode (inode); info =MQUEUE_I (inode);spin_lock (&info->lock);for (i =0 ; i < info->attr.mq_curmsgs; i++)
include/linux/ipc_namespace.h static void mqueue_delete_inode(struct inode *inode) · line 320
Quoted 5 lines
include/linux/ipc_namespace.h +1
-1@@ -266,10 +317,12 @@ static void mqueue_delete_inode(struct inode *inode)if (user) {spin_lock (&mq_lock); user->mq_bytes -= mq_bytes; - ipc_ns->mq_queues_count--; +if (ipc_ns)
The reader might be wondering why ipc_ns==NULL is an acceptable state, and what that state actually means. This reader is wondering that.
Quoted 10 lines
include/linux/ipc_namespace.h +2
-0+ ipc_ns->mq_queues_count--;spin_unlock (&mq_lock);free_uid (user); } +put_ipc_ns (ipc_ns); }
include/linux/ipc_namespace.h static void mqueue_delete_inode(struct inode *inode)
Quoted 10 lines
include/linux/ipc_namespace.h +10
-0+int mq_init_ns (struct ipc_namespace *ns) +{ + ns->mq_queues_count =0 ; + ns->mq_queues_max =DFLT_QUEUESMAX ; + ns->mq_msg_max =DFLT_MSGMAX ; + ns->mq_msgsize_max =DFLT_MSGSIZEMAX ; + + ns->mq_mnt =kern_mount_data (&mqueue_fs_type, ns); +if (IS_ERR (ns->mq_mnt)) +return PTR_ERR (ns->mq_mnt);
It seems dangerous to leave an IS_ERR() pointer sitting in ns->mq_mnt for someone else to trip over.
Quoted 40 lines
include/linux/ipc_namespace.h +16
-4+return 0 ; +} + +void mq_clear_sbinfo (struct ipc_namespace *ns) +{ + ns->mq_mnt->mnt_sb->s_fs_info =NULL ; +} + +void mq_put_mnt (struct ipc_namespace *ns) +{ +mntput (ns->mq_mnt); +} +static int msg_max_limit_min =MIN_MSGMAX ;static int msg_max_limit_max =MAX_MSGMAX ;@@ -1284,15 +1367,14 @@ static int __init init_mqueue_fs(void)if (error) goto out_sysctl; - init_ipc_ns.mq_mnt =kern_mount (&mqueue_fs_type); +spin_lock_init (&mq_lock); + + init_ipc_ns.mq_mnt =kern_mount_data (&mqueue_fs_type, &init_ipc_ns);if (IS_ERR (init_ipc_ns.mq_mnt)) { error =PTR_ERR (init_ipc_ns.mq_mnt); goto out_filesystem; } -/* internal initialization - not common for vfs */ -spin_lock_init (&mq_lock); -return 0 ; out_filesystem:ipc/msgutil.c +0
-0
ipc/msgutil.c line 21
Quoted 4 lines
ipc/msgutil.c +1
-0@@ -18,18 +18,16 @@#include "util.h" +spinlock_t mq_lock;
DEFINE_SPINLOCK(), please. For lockdep reasons.
Quoted 19 lines
...
ipc/msgutil.c +16
-0+/* +* put_ipc_ns - drop a reference to an ipc namespace. +* @ns: the namespace to put +* +* If this is the last task in the namespace exiting, and +* it is dropping the refcount to 0, then it can race with +* a task in another ipc namespace but in a mounts namespace +* which has this ipcns's mqueuefs mounted, doing some action +* with one of the mqueuefs files. That can raise the refcount. +* So dropping the refcount, and raising the refcount when +* accessing it through the VFS, are protected with mq_lock. +* +* (Clearly, a task raising the refcount on its own ipc_ns +* needn't take mq_lock since it can't race with the last task +* in the ipcns exiting). +*/
ipc/msgutil.c
Quoted 10 lines
ipc/msgutil.c +8
-1+void put_ipc_ns (struct ipc_namespace *ns) { -struct ipc_namespace *ns; +if (ns &&atomic_dec_and_lock (&ns->count, &mq_lock)) { +mq_clear_sbinfo (ns); +spin_unlock (&mq_lock); +mq_put_mnt (ns); +free_ipc_ns (ns); + } +}
Why are we supporting calls with a NULL arg here?
Quoted 19 lines
ipc/msgutil.c +2
-2- ns = container_of(kref, struct ipc_namespace, kref); +void free_ipc_ns (struct ipc_namespace *ns) +{/* * Unregistering the hotplug notifier at the beginning guarantees * that the ipc namespace won't be freed while we are inside the @@ -102,7 +132,6 @@ void free_ipc_ns(struct kref *kref)sem_exit_ns (ns);msg_exit_ns (ns);shm_exit_ns (ns); -mq_exit_ns (ns);kfree (ns);atomic_dec (&nr_ipc_ns);ipc/util.h +0
-0
ipc/util.h void shm_init (void); · line 28
Quoted 10 lines
ipc/util.h +4
-2@@ -21,9 +21,11 @@ void shm_init (void);struct ipc_namespace ;#ifdef CONFIG_POSIX_MQUEUE -void mq_exit_ns (struct ipc_namespace *ns); +extern void mq_clear_sbinfo (struct ipc_namespace *ns); +extern void mq_put_mnt (struct ipc_namespace *ns);#else -#define mq_exit_ns (ns) ((void) 0) +#define mq_clear_sbinfo (ns) ((void) 0) +#define mq_put_mnt (ns) ((void) 0)
argh.
Quoted 2 lines
ipc/util.h +0
-0#endif
Re: [PATCH 2/3] ipc namespaces: implement support for posix msqueues
Quoted 31 lines · Andrew Morton
Quoted 20 lines · On Fri, 16 Jan 2009 20:03:32 -0600 "Serge E. Hallyn" <[email protected]> wrote:
Implement multiple mounts of the mqueue file system, and link it to usage of
CLONE_NEWIPC.Each ipc ns has a corresponding mqueuefs superblock. When a user does
clone(CLONE_NEWIPC)orunshare(CLONE_NEWIPC), the unshare will cause an internal mount of a new mqueuefs sb linked to the new ipc ns.When a user does 'mount -t mqueue mqueue
/dev/mqueue', he mounts the mqueuefs superblock.Posix message queues can be worked with both through the mq_* system calls (see
mq_overview(7)), and through the VFS through the mqueue mount. Any usage ofmq_open()and friends will work with the acting task's ipc namespace. Any actions through the VFS will work with the mqueuefs in which the file was created. So if a user doesn't remount mqueuefs afterunshare(CLONE_NEWIPC),mq_open("/ab")will not be reflected in "ls/dev/mqueue".Quoted 7 lines
If task a mounts mqueue for ipc_ns:1, then clones task b with a new ipcns, ipcns:2, and then task a is the last task in ipc_ns:1 to exit, then (1) ipc_ns:1 will be freed, (2) it's superblock will live on until task b umounts the corresponding mqueuefs, and vfs actions will continue to succeed, but (3)
sb->s_fs_infowill be NULL for the sb corresponding to the deceased ipc_ns:1.
Quoted 2 lines
I suppose that stuff like this should find its way into a manpage one day. That'll be fun for someone.
Nadia wrote an update for the mq_overview.7 page. But the semantics have changed a bit since then so I'll need to revise that.
Quoted 18 lines
Quoted 18 lines
ipc/mqueue.c +18
-0+static int get_sb_single_ns (struct file_system_type *fs_type, +int flags,void *data, +int (*fill_super)(struct super_block *,void *,int ), +struct vfsmount *mnt) +{ +struct super_block *s; +int error; + + s =sget (fs_type, compare_sb_single_ns, set_sb_single_ns, data); +if (IS_ERR (s)) +return PTR_ERR (s); +if (!s->s_root) { + s->s_flags = flags; + error =fill_super (s, data, flags &MS_SILENT ?1 :0 ); +if (error) { +up_write (&s->s_umount); +deactivate_super (s); +return error;
Quoted 8 lines
ipc/mqueue.c out_inode: · line 257
Quoted 6 lines
ipc/mqueue.c +5
-0+ } + s->s_flags |=MS_ACTIVE ; + } +do_remount_sb (s, flags, data,0 ); +return simple_set_mnt (mnt, s); }The above doesn't seem specific to mqueue. Is it in the best place?
No, I think there is commonality with devpts and perhaps also proc. Now that devpts namespaces are upstream i should first move it to using a more generic helper, then introduce posixmq namespaces as another user.
Quoted 9 lines
ipc/mqueue.c static void mqueue_delete_inode(struct inode *inode) · line 320
Quoted 6 lines
ipc/mqueue.c +1
-1@@ -266,10 +317,12 @@ static void mqueue_delete_inode(struct inode *inode)if (user) {spin_lock (&mq_lock); user->mq_bytes -= mq_bytes; - ipc_ns->mq_queues_count--; +if (ipc_ns)The reader might be wondering why ipc_ns==NULL is an acceptable state, and what that state actually means. This reader is wondering that.
That's actually a central part of how we resolved the dependency between the mqfs mount (pinned by vfs) and mqns (pinned by nsproxy). So the mqns pins its mqfs vfsmount, but the mnt->mnt_sb->s_fs_info is not pinned by the mqns. Rather, s_fs_info is protected by the mq_lock and either non-NULL and valid, or NULL. The mqns takes the mq_lock to set it to NULL, and any reader of s_fs_info must dereference it and pin the mqns under mq_lock.
Quoted 19 lines
Quoted 19 lines
ipc/namespace.c +17
-1+* put_ipc_ns - drop a reference to an ipc namespace. +* @ns: the namespace to put +* +* If this is the last task in the namespace exiting, and +* it is dropping the refcount to 0, then it can race with +* a task in another ipc namespace but in a mounts namespace +* which has this ipcns's mqueuefs mounted, doing some action +* with one of the mqueuefs files. That can raise the refcount. +* So dropping the refcount, and raising the refcount when +* accessing it through the VFS, are protected with mq_lock. +* +* (Clearly, a task raising the refcount on its own ipc_ns +* needn't take mq_lock since it can't race with the last task +* in the ipcns exiting). +*/ +void put_ipc_ns (struct ipc_namespace *ns) { -struct ipc_namespace *ns; +if (ns &&atomic_dec_and_lock (&ns->count, &mq_lock)) {
Quoted 8 lines
ipc/namespace.c void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids, · line 119
Quoted 6 lines
ipc/namespace.c +6
-0+mq_clear_sbinfo (ns); +spin_unlock (&mq_lock); +mq_put_mnt (ns); +free_ipc_ns (ns); + } +}Why are we supporting calls with a NULL arg here?
Hmm, I'm not sure that's necessary. Will look into it.
Every other comment I'll fix in the next version. Thanks very much.
-serge
PATCH 3/3 mqueue namespace: adapt sysctl
In reply to Serge E. HallynLargely inspired from ipc/ipc_sysctl.c. This patch isolates the mqueue sysctl stuff in its own file.
--- include/linux/ipc_namespace.h | 14 +++++ init/Kconfig | 6 ++ ipc/Makefile | 1 + ipc/mq_sysctl.c | 125 +++++++++++++++++++++++++++++++++++++++++ ipc/mqueue.c | 65 +--------------------- 5 files changed, 147 insertions(+), 64 deletions(-) create mode 100644 ipc/mq_sysctl.c
include/linux/ipc_namespace.h +14 -0
@@ -121,4 +121,18 @@ static inline void put_ipc_ns(struct ipc_namespace *ns) { }#endif + +#ifdef CONFIG_POSIX_MQUEUE_SYSCTL + +struct ctl_table_header ; +extern struct ctl_table_header *mq_register_sysctl_table (void ); + +#else /* CONFIG_POSIX_MQUEUE_SYSCTL */ + +static inline struct ctl_table_header *mq_register_sysctl_table (void ) +{ +return NULL ; +} + +#endif /* CONFIG_POSIX_MQUEUE_SYSCTL */ #endif
init/Kconfig +6 -0
@@ -148,6 +148,12 @@ config POSIX_MQUEUE If unsure, say Y. +config POSIX_MQUEUE_SYSCTL + bool + depends on POSIX_MQUEUE + depends on SYSCTL + default y + config BSD_PROCESS_ACCT bool "BSD Process Accounting" help
ipc/Makefile +1 -0
@@ -8,4 +8,5 @@ obj-$(CONFIG_SYSVIPC_SYSCTL) += ipc_sysctl.o obj_mq-$(CONFIG_COMPAT) += compat_mq.o obj-$(CONFIG_POSIX_MQUEUE) += mqueue.o msgutil.o $(obj_mq-y) obj-$(CONFIG_IPC_NS) += namespace.o +obj-$(CONFIG_POSIX_MQUEUE_SYSCTL) += mq_sysctl.o
ipc/mq_sysctl.c new file +125 -0
125 lines not on this page · raw message
ipc/mqueue.c +1 -64
83 lines not on this page · raw message
Signature · 1 line
1.5.4.3
Quoted 26 lines · On Fri, 16 Jan 2009 20:02:48 -0600 "Serge E. Hallyn" <[email protected]> wrote:
IPC namespaces are completely disjoint
id->objectmappings. A task can passCLONE_NEWIPCto unshare and clone to get a new, empty, IPC namespace. Until now this has supported SYSV IPC.Most Posix IPC is done in userspace. The posix mqueue support, however, is implemented on top of the mqueue fs.
This patchset implements multiple mqueue fs instances, one per IPC namespace to be precise.
To create a new ipc namespace with posix mq support, you should now:
unshare(CLONE_NEWIPC|CLONE_NEWNS); umount /dev/mqueue mount -t mqueue mqueue /dev/mqueueIt's perfectly valid to do vfs operations on files in another ipc_namespace's
/dev/mqueue, but any use ofmq_open(3)and friends will act in your ownipc_ns. After the ipc namespace has exited, you can still unlink but no longer create files in that fs (since accounting is carried.Changelog:
Quoted 8 lines
v14: (Jan 16 2009) port to linux-next v13: (Dec 28 2009) 1. addressed comments by Dave and Suka 2. ported Cedric's patch to make posix mq sysctls per-namespaceWhen convenient, it would be great to see this tested in -mm.
hm. Who is going to test it?
Quoted 35 lines · Andrew Morton
Quoted 32 lines · On Fri, 16 Jan 2009 20:02:48 -0600 "Serge E. Hallyn" <[email protected]> wrote:
IPC namespaces are completely disjoint
id->objectmappings. A task can passCLONE_NEWIPCto unshare and clone to get a new, empty, IPC namespace. Until now this has supported SYSV IPC.Most Posix IPC is done in userspace. The posix mqueue support, however, is implemented on top of the mqueue fs.
This patchset implements multiple mqueue fs instances, one per IPC namespace to be precise.
To create a new ipc namespace with posix mq support, you should now:
unshare(CLONE_NEWIPC|CLONE_NEWNS); umount /dev/mqueue mount -t mqueue mqueue /dev/mqueueIt's perfectly valid to do vfs operations on files in another ipc_namespace's
/dev/mqueue, but any use ofmq_open(3)and friends will act in your ownipc_ns. After the ipc namespace has exited, you can still unlink but no longer create files in that fs (since accounting is carried.Changelog:
v14: (Jan 16 2009) port to linux-next v13: (Dec 28 2009) 1. addressed comments by Dave and Suka 2. ported Cedric's patch to make posix mq sysctls per-namespace
Quoted 4 lines
Quoted 2 lines
When convenient, it would be great to see this tested in -mm.
hm. Who is going to test it?
Everyone using posix mq with an -mm kernel :)
There are ltp testcases which I hope can be pushed once these patches appear headed upstream.
thanks,
-serge