]> asedeno.scripts.mit.edu Git - linux.git/blobdiff - ipc/shm.c
Merge branch 'akpm' (patches from Andrew)
[linux.git] / ipc / shm.c
index 051a3e1fb8df9b2bcb2073e8299d31cdfc938724..4cd402e4cfeb603e2417a3796c3d9b22f3022f89 100644 (file)
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -43,6 +43,7 @@
 #include <linux/nsproxy.h>
 #include <linux/mount.h>
 #include <linux/ipc_namespace.h>
+#include <linux/rhashtable.h>
 
 #include <linux/uaccess.h>
 
@@ -95,14 +96,14 @@ static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
 static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
 #endif
 
-int shm_init_ns(struct ipc_namespace *ns)
+void shm_init_ns(struct ipc_namespace *ns)
 {
        ns->shm_ctlmax = SHMMAX;
        ns->shm_ctlall = SHMALL;
        ns->shm_ctlmni = SHMMNI;
        ns->shm_rmid_forced = 0;
        ns->shm_tot = 0;
-       return ipc_init_ids(&shm_ids(ns));
+       ipc_init_ids(&shm_ids(ns));
 }
 
 /*
@@ -135,9 +136,8 @@ void shm_exit_ns(struct ipc_namespace *ns)
 
 static int __init ipc_ns_init(void)
 {
-       const int err = shm_init_ns(&init_ipc_ns);
-       WARN(err, "ipc: sysv shm_init_ns failed: %d\n", err);
-       return err;
+       shm_init_ns(&init_ipc_ns);
+       return 0;
 }
 
 pure_initcall(ipc_ns_init);
@@ -179,16 +179,34 @@ static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace
  */
 static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
 {
-       struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
+       struct kern_ipc_perm *ipcp;
 
+       rcu_read_lock();
+       ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
+       if (IS_ERR(ipcp))
+               goto err;
+
+       ipc_lock_object(ipcp);
+       /*
+        * ipc_rmid() may have already freed the ID while ipc_lock_object()
+        * was spinning: here verify that the structure is still valid.
+        * Upon races with RMID, return -EIDRM, thus indicating that
+        * the ID points to a removed identifier.
+        */
+       if (ipc_valid_object(ipcp)) {
+               /* return a locked ipc object upon success */
+               return container_of(ipcp, struct shmid_kernel, shm_perm);
+       }
+
+       ipc_unlock_object(ipcp);
+       ipcp = ERR_PTR(-EIDRM);
+err:
+       rcu_read_unlock();
        /*
         * Callers of shm_lock() must validate the status of the returned ipc
-        * object pointer (as returned by ipc_lock()), and error out as
-        * appropriate.
+        * object pointer and error out as appropriate.
         */
-       if (IS_ERR(ipcp))
-               return (void *)ipcp;
-       return container_of(ipcp, struct shmid_kernel, shm_perm);
+       return (void *)ipcp;
 }
 
 static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
@@ -427,6 +445,17 @@ static int shm_split(struct vm_area_struct *vma, unsigned long addr)
        return 0;
 }
 
+static unsigned long shm_pagesize(struct vm_area_struct *vma)
+{
+       struct file *file = vma->vm_file;
+       struct shm_file_data *sfd = shm_file_data(file);
+
+       if (sfd->vm_ops->pagesize)
+               return sfd->vm_ops->pagesize(vma);
+
+       return PAGE_SIZE;
+}
+
 #ifdef CONFIG_NUMA
 static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
 {
@@ -554,6 +583,7 @@ static const struct vm_operations_struct shm_vm_ops = {
        .close  = shm_close,    /* callback for when the vm-area is released */
        .fault  = shm_fault,
        .split  = shm_split,
+       .pagesize = shm_pagesize,
 #if defined(CONFIG_NUMA)
        .set_policy = shm_set_policy,
        .get_policy = shm_get_policy,
@@ -671,6 +701,8 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
        if (is_file_hugepages(file) && shp->mlock_user)
                user_shm_unlock(size, shp->mlock_user);
        fput(file);
+       ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
+       return error;
 no_file:
        call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
        return error;
@@ -866,7 +898,7 @@ static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
        down_write(&shm_ids(ns).rwsem);
        rcu_read_lock();
 
-       ipcp = ipcctl_pre_down_nolock(ns, &shm_ids(ns), shmid, cmd,
+       ipcp = ipcctl_obtain_check(ns, &shm_ids(ns), shmid, cmd,
                                      &shmid64->shm_perm, 0);
        if (IS_ERR(ipcp)) {
                err = PTR_ERR(ipcp);
@@ -917,7 +949,7 @@ static int shmctl_ipc_info(struct ipc_namespace *ns,
                shminfo->shmall = ns->shm_ctlall;
                shminfo->shmmin = SHMMIN;
                down_read(&shm_ids(ns).rwsem);
-               err = ipc_get_maxid(&shm_ids(ns));
+               err = ipc_get_maxidx(&shm_ids(ns));
                up_read(&shm_ids(ns).rwsem);
                if (err < 0)
                        err = 0;
@@ -937,7 +969,7 @@ static int shmctl_shm_info(struct ipc_namespace *ns,
                shm_info->shm_tot = ns->shm_tot;
                shm_info->swap_attempts = 0;
                shm_info->swap_successes = 0;
-               err = ipc_get_maxid(&shm_ids(ns));
+               err = ipc_get_maxidx(&shm_ids(ns));
                up_read(&shm_ids(ns).rwsem);
                if (err < 0)
                        err = 0;
@@ -949,7 +981,6 @@ static int shmctl_stat(struct ipc_namespace *ns, int shmid,
                        int cmd, struct shmid64_ds *tbuf)
 {
        struct shmid_kernel *shp;
-       int id = 0;
        int err;
 
        memset(tbuf, 0, sizeof(*tbuf));
@@ -961,7 +992,6 @@ static int shmctl_stat(struct ipc_namespace *ns, int shmid,
                        err = PTR_ERR(shp);
                        goto out_unlock;
                }
-               id = shp->shm_perm.id;
        } else { /* IPC_STAT */
                shp = shm_obtain_object_check(ns, shmid);
                if (IS_ERR(shp)) {
@@ -1011,10 +1041,21 @@ static int shmctl_stat(struct ipc_namespace *ns, int shmid,
        tbuf->shm_lpid  = pid_vnr(shp->shm_lprid);
        tbuf->shm_nattch = shp->shm_nattch;
 
-       ipc_unlock_object(&shp->shm_perm);
-       rcu_read_unlock();
-       return id;
+       if (cmd == IPC_STAT) {
+               /*
+                * As defined in SUS:
+                * Return 0 on success
+                */
+               err = 0;
+       } else {
+               /*
+                * SHM_STAT and SHM_STAT_ANY (both Linux specific)
+                * Return the full id, including the sequence number
+                */
+               err = shp->shm_perm.id;
+       }
 
+       ipc_unlock_object(&shp->shm_perm);
 out_unlock:
        rcu_read_unlock();
        return err;
@@ -1354,15 +1395,14 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
        struct shmid_kernel *shp;
        unsigned long addr = (unsigned long)shmaddr;
        unsigned long size;
-       struct file *file;
+       struct file *file, *base;
        int    err;
        unsigned long flags = MAP_SHARED;
        unsigned long prot;
        int acc_mode;
        struct ipc_namespace *ns;
        struct shm_file_data *sfd;
-       struct path path;
-       fmode_t f_mode;
+       int f_flags;
        unsigned long populate = 0;
 
        err = -EINVAL;
@@ -1395,11 +1435,11 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
        if (shmflg & SHM_RDONLY) {
                prot = PROT_READ;
                acc_mode = S_IRUGO;
-               f_mode = FMODE_READ;
+               f_flags = O_RDONLY;
        } else {
                prot = PROT_READ | PROT_WRITE;
                acc_mode = S_IRUGO | S_IWUGO;
-               f_mode = FMODE_READ | FMODE_WRITE;
+               f_flags = O_RDWR;
        }
        if (shmflg & SHM_EXEC) {
                prot |= PROT_EXEC;
@@ -1435,46 +1475,44 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
                goto out_unlock;
        }
 
-       path = shp->shm_file->f_path;
-       path_get(&path);
+       /*
+        * We need to take a reference to the real shm file to prevent the
+        * pointer from becoming stale in cases where the lifetime of the outer
+        * file extends beyond that of the shm segment.  It's not usually
+        * possible, but it can happen during remap_file_pages() emulation as
+        * that unmaps the memory, then does ->mmap() via file reference only.
+        * We'll deny the ->mmap() if the shm segment was since removed, but to
+        * detect shm ID reuse we need to compare the file pointers.
+        */
+       base = get_file(shp->shm_file);
        shp->shm_nattch++;
-       size = i_size_read(d_inode(path.dentry));
+       size = i_size_read(file_inode(base));
        ipc_unlock_object(&shp->shm_perm);
        rcu_read_unlock();
 
        err = -ENOMEM;
        sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
        if (!sfd) {
-               path_put(&path);
+               fput(base);
                goto out_nattch;
        }
 
-       file = alloc_file(&path, f_mode,
-                         is_file_hugepages(shp->shm_file) ?
+       file = alloc_file_clone(base, f_flags,
+                         is_file_hugepages(base) ?
                                &shm_file_operations_huge :
                                &shm_file_operations);
        err = PTR_ERR(file);
        if (IS_ERR(file)) {
                kfree(sfd);
-               path_put(&path);
+               fput(base);
                goto out_nattch;
        }
 
-       file->private_data = sfd;
-       file->f_mapping = shp->shm_file->f_mapping;
        sfd->id = shp->shm_perm.id;
        sfd->ns = get_ipc_ns(ns);
-       /*
-        * We need to take a reference to the real shm file to prevent the
-        * pointer from becoming stale in cases where the lifetime of the outer
-        * file extends beyond that of the shm segment.  It's not usually
-        * possible, but it can happen during remap_file_pages() emulation as
-        * that unmaps the memory, then does ->mmap() via file reference only.
-        * We'll deny the ->mmap() if the shm segment was since removed, but to
-        * detect shm ID reuse we need to compare the file pointers.
-        */
-       sfd->file = get_file(shp->shm_file);
+       sfd->file = base;
        sfd->vm_ops = NULL;
+       file->private_data = sfd;
 
        err = security_mmap_file(file, prot, flags);
        if (err)