Skip to content

Commit 36735a6

Browse files
author
Al Viro
committed
mqueue: switch to on-demand creation of internal mount
Instead of doing that upon each ipcns creation, we do that the first time mq_open(2) or mqueue mount is done in an ipcns. What's more, doing that allows to get rid of mount_ns() use - we can go with considerably cheaper mount_nodev(), avoiding the loop over all mqueue superblock instances; ipcns->mq_mnt is used to locate preexisting instance in O(1) time instead of O(instances) mount_ns() would've cost us. Based upon the version by Giuseppe Scrivano <gscrivan@redhat.com>; I've added handling of userland mqueue mounts (original had been broken in that area) and added a switch to mount_nodev(). Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent a713fd7 commit 36735a6

1 file changed

Lines changed: 55 additions & 19 deletions

File tree

‎ipc/mqueue.c‎

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,9 @@ static struct inode *mqueue_get_inode(struct super_block *sb,
325325
static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
326326
{
327327
struct inode *inode;
328-
struct ipc_namespace *ns = sb->s_fs_info;
328+
struct ipc_namespace *ns = data;
329329

330+
sb->s_fs_info = ns;
330331
sb->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
331332
sb->s_blocksize = PAGE_SIZE;
332333
sb->s_blocksize_bits = PAGE_SHIFT;
@@ -343,18 +344,44 @@ static int mqueue_fill_super(struct super_block *sb, void *data, int silent)
343344
return 0;
344345
}
345346

347+
static struct file_system_type mqueue_fs_type;
348+
/*
349+
* Return value is pinned only by reference in ->mq_mnt; it will
350+
* live until ipcns dies. Caller does not need to drop it.
351+
*/
352+
static struct vfsmount *mq_internal_mount(void)
353+
{
354+
struct ipc_namespace *ns = current->nsproxy->ipc_ns;
355+
struct vfsmount *m = ns->mq_mnt;
356+
if (m)
357+
return m;
358+
m = kern_mount_data(&mqueue_fs_type, ns);
359+
spin_lock(&mq_lock);
360+
if (unlikely(ns->mq_mnt)) {
361+
spin_unlock(&mq_lock);
362+
if (!IS_ERR(m))
363+
kern_unmount(m);
364+
return ns->mq_mnt;
365+
}
366+
if (!IS_ERR(m))
367+
ns->mq_mnt = m;
368+
spin_unlock(&mq_lock);
369+
return m;
370+
}
371+
346372
static struct dentry *mqueue_mount(struct file_system_type *fs_type,
347373
int flags, const char *dev_name,
348374
void *data)
349375
{
350-
struct ipc_namespace *ns;
351-
if (flags & SB_KERNMOUNT) {
352-
ns = data;
353-
data = NULL;
354-
} else {
355-
ns = current->nsproxy->ipc_ns;
356-
}
357-
return mount_ns(fs_type, flags, data, ns, ns->user_ns, mqueue_fill_super);
376+
struct vfsmount *m;
377+
if (flags & SB_KERNMOUNT)
378+
return mount_nodev(fs_type, flags, data, mqueue_fill_super);
379+
m = mq_internal_mount();
380+
if (IS_ERR(m))
381+
return ERR_CAST(m);
382+
atomic_inc(&m->mnt_sb->s_active);
383+
down_write(&m->mnt_sb->s_umount);
384+
return dget(m->mnt_root);
358385
}
359386

360387
static void init_once(void *foo)
@@ -743,13 +770,16 @@ static int prepare_open(struct dentry *dentry, int oflag, int ro,
743770
static int do_mq_open(const char __user *u_name, int oflag, umode_t mode,
744771
struct mq_attr *attr)
745772
{
746-
struct vfsmount *mnt = current->nsproxy->ipc_ns->mq_mnt;
747-
struct dentry *root = mnt->mnt_root;
773+
struct vfsmount *mnt = mq_internal_mount();
774+
struct dentry *root;
748775
struct filename *name;
749776
struct path path;
750777
int fd, error;
751778
int ro;
752779

780+
if (IS_ERR(mnt))
781+
return PTR_ERR(mnt);
782+
753783
audit_mq_open(oflag, mode, attr);
754784

755785
if (IS_ERR(name = getname(u_name)))
@@ -760,6 +790,7 @@ static int do_mq_open(const char __user *u_name, int oflag, umode_t mode,
760790
goto out_putname;
761791

762792
ro = mnt_want_write(mnt); /* we'll drop it in any case */
793+
root = mnt->mnt_root;
763794
inode_lock(d_inode(root));
764795
path.dentry = lookup_one_len(name->name, root, strlen(name->name));
765796
if (IS_ERR(path.dentry)) {
@@ -808,6 +839,9 @@ SYSCALL_DEFINE1(mq_unlink, const char __user *, u_name)
808839
struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
809840
struct vfsmount *mnt = ipc_ns->mq_mnt;
810841

842+
if (!mnt)
843+
return -ENOENT;
844+
811845
name = getname(u_name);
812846
if (IS_ERR(name))
813847
return PTR_ERR(name);
@@ -1534,28 +1568,26 @@ int mq_init_ns(struct ipc_namespace *ns)
15341568
ns->mq_msgsize_max = DFLT_MSGSIZEMAX;
15351569
ns->mq_msg_default = DFLT_MSG;
15361570
ns->mq_msgsize_default = DFLT_MSGSIZE;
1571+
ns->mq_mnt = NULL;
15371572

1538-
ns->mq_mnt = kern_mount_data(&mqueue_fs_type, ns);
1539-
if (IS_ERR(ns->mq_mnt)) {
1540-
int err = PTR_ERR(ns->mq_mnt);
1541-
ns->mq_mnt = NULL;
1542-
return err;
1543-
}
15441573
return 0;
15451574
}
15461575

15471576
void mq_clear_sbinfo(struct ipc_namespace *ns)
15481577
{
1549-
ns->mq_mnt->mnt_sb->s_fs_info = NULL;
1578+
if (ns->mq_mnt)
1579+
ns->mq_mnt->mnt_sb->s_fs_info = NULL;
15501580
}
15511581

15521582
void mq_put_mnt(struct ipc_namespace *ns)
15531583
{
1554-
kern_unmount(ns->mq_mnt);
1584+
if (ns->mq_mnt)
1585+
kern_unmount(ns->mq_mnt);
15551586
}
15561587

15571588
static int __init init_mqueue_fs(void)
15581589
{
1590+
struct vfsmount *m;
15591591
int error;
15601592

15611593
mqueue_inode_cachep = kmem_cache_create("mqueue_inode_cache",
@@ -1577,6 +1609,10 @@ static int __init init_mqueue_fs(void)
15771609
if (error)
15781610
goto out_filesystem;
15791611

1612+
m = kern_mount_data(&mqueue_fs_type, &init_ipc_ns);
1613+
if (IS_ERR(m))
1614+
goto out_filesystem;
1615+
init_ipc_ns.mq_mnt = m;
15801616
return 0;
15811617

15821618
out_filesystem:

0 commit comments

Comments
 (0)