cregit-Linux how code gets into the kernel

Release 4.7 ipc/shm.c

Directory: ipc
/*
 * linux/ipc/shm.c
 * Copyright (C) 1992, 1993 Krishna Balasubramanian
 *       Many improvements/fixes by Bruno Haible.
 * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
 * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
 *
 * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
 * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
 * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
 * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
 * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
 * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
 * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
 *
 * support for audit of ipc object properties and permission changes
 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
 *
 * namespaces support
 * OpenVZ, SWsoft Inc.
 * Pavel Emelianov <xemul@openvz.org>
 *
 * Better ipc lock (kern_ipc_perm.lock) handling
 * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
 */

#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/hugetlb.h>
#include <linux/shm.h>
#include <linux/init.h>
#include <linux/file.h>
#include <linux/mman.h>
#include <linux/shmem_fs.h>
#include <linux/security.h>
#include <linux/syscalls.h>
#include <linux/audit.h>
#include <linux/capability.h>
#include <linux/ptrace.h>
#include <linux/seq_file.h>
#include <linux/rwsem.h>
#include <linux/nsproxy.h>
#include <linux/mount.h>
#include <linux/ipc_namespace.h>

#include <linux/uaccess.h>

#include "util.h"


struct shm_file_data {
	
int id;
	
struct ipc_namespace *ns;
	
struct file *file;
	
const struct vm_operations_struct *vm_ops;
};


#define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))


static const struct file_operations shm_file_operations;

static const struct vm_operations_struct shm_vm_ops;


#define shm_ids(ns)	((ns)->ids[IPC_SHM_IDS])


#define shm_unlock(shp)			\
	ipc_unlock(&(shp)->shm_perm)

static int newseg(struct ipc_namespace *, struct ipc_params *);
static void shm_open(struct vm_area_struct *vma);
static void shm_close(struct vm_area_struct *vma);
static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
#ifdef CONFIG_PROC_FS
static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
#endif


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; ipc_init_ids(&shm_ids(ns)); }

Contributors

PersonTokensPropCommitsCommitProp
kirill korotaevkirill korotaev3163.27%120.00%
vasiliy kulikovvasiliy kulikov612.24%120.00%
pre-gitpre-git612.24%120.00%
pierre peifferpierre peiffer36.12%120.00%
wang congwang cong36.12%120.00%
Total49100.00%5100.00%

/* * Called with shm_ids.rwsem (writer) and the shp structure locked. * Only shm_ids.rwsem remains locked on exit. */
static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp) { struct shmid_kernel *shp; shp = container_of(ipcp, struct shmid_kernel, shm_perm); if (shp->shm_nattch) { shp->shm_perm.mode |= SHM_DEST; /* Do not find it any more */ shp->shm_perm.key = IPC_PRIVATE; shm_unlock(shp); } else shm_destroy(ns, shp); }

Contributors

PersonTokensPropCommitsCommitProp
kirill korotaevkirill korotaev5171.83%133.33%
pierre peifferpierre peiffer1926.76%133.33%
pre-gitpre-git11.41%133.33%
Total71100.00%3100.00%

#ifdef CONFIG_IPC_NS
void shm_exit_ns(struct ipc_namespace *ns) { free_ipcs(ns, &shm_ids(ns), do_shm_rmid); idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr); }

Contributors

PersonTokensPropCommitsCommitProp
kirill korotaevkirill korotaev1747.22%120.00%
serge hallynserge hallyn1336.11%120.00%
pierre peifferpierre peiffer38.33%240.00%
nadia derbeynadia derbey38.33%120.00%
Total36100.00%5100.00%

#endif
static int __init ipc_ns_init(void) { shm_init_ns(&init_ipc_ns); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git950.00%350.00%
linus torvaldslinus torvalds738.89%116.67%
kirill korotaevkirill korotaev15.56%116.67%
pierre peifferpierre peiffer15.56%116.67%
Total18100.00%6100.00%

pure_initcall(ipc_ns_init);
void __init shm_init(void) { ipc_init_proc_interface("sysvipc/shm", #if BITS_PER_LONG <= 32 " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n", #else " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n", #endif IPC_SHM_IDS, sysvipc_shm_proc_show); }

Contributors

PersonTokensPropCommitsCommitProp
helge dellerhelge deller1240.00%112.50%
pre-gitpre-git826.67%450.00%
linus torvaldslinus torvalds723.33%112.50%
mike waychisonmike waychison26.67%112.50%
kirill korotaevkirill korotaev13.33%112.50%
Total30100.00%8100.00%


static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id) { struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id); if (IS_ERR(ipcp)) return ERR_CAST(ipcp); return container_of(ipcp, struct shmid_kernel, shm_perm); }

Contributors

PersonTokensPropCommitsCommitProp
davidlohr buesodavidlohr bueso57100.00%2100.00%
Total57100.00%2100.00%


static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id) { struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id); if (IS_ERR(ipcp)) return ERR_CAST(ipcp); return container_of(ipcp, struct shmid_kernel, shm_perm); }

Contributors

PersonTokensPropCommitsCommitProp
davidlohr buesodavidlohr bueso57100.00%1100.00%
Total57100.00%1100.00%

/* * shm_lock_(check_) routines are called in the paths where the rwsem * is not necessarily held. */
static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id) { struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id); /* * Callers of shm_lock() must validate the status of the returned ipc * object pointer (as returned by ipc_lock()), and error out as * appropriate. */ if (IS_ERR(ipcp)) return (void *)ipcp; return container_of(ipcp, struct shmid_kernel, shm_perm); }

Contributors

PersonTokensPropCommitsCommitProp
nadia derbeynadia derbey3661.02%233.33%
kirill a. shutemovkirill a. shutemov1118.64%116.67%
kirill korotaevkirill korotaev46.78%116.67%
pre-gitpre-git46.78%116.67%
pierre peifferpierre peiffer46.78%116.67%
Total59100.00%6100.00%


static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp) { rcu_read_lock(); ipc_lock_object(&ipcp->shm_perm); }

Contributors

PersonTokensPropCommitsCommitProp
vasiliy kulikovvasiliy kulikov2295.65%150.00%
davidlohr buesodavidlohr bueso14.35%150.00%
Total23100.00%2100.00%


static void shm_rcu_free(struct rcu_head *head) { struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu); struct shmid_kernel *shp = ipc_rcu_to_struct(p); security_shm_free(shp); ipc_rcu_free(head); }

Contributors

PersonTokensPropCommitsCommitProp
davidlohr buesodavidlohr bueso46100.00%1100.00%
Total46100.00%1100.00%


static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s) { list_del(&s->shm_clist); ipc_rmid(&shm_ids(ns), &s->shm_perm); }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git1333.33%240.00%
nadia derbeynadia derbey1025.64%120.00%
jack millerjack miller820.51%120.00%
kirill korotaevkirill korotaev820.51%120.00%
Total39100.00%5100.00%


static int __shm_open(struct vm_area_struct *vma) { struct file *file = vma->vm_file; struct shm_file_data *sfd = shm_file_data(file); struct shmid_kernel *shp; shp = shm_lock(sfd->ns, sfd->id); if (IS_ERR(shp)) return PTR_ERR(shp); shp->shm_atim = get_seconds(); shp->shm_lprid = task_tgid_vnr(current); shp->shm_nattch++; shm_unlock(shp); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git3538.89%646.15%
eric w. biedermaneric w. biederman2527.78%17.69%
kirill a. shutemovkirill a. shutemov1921.11%17.69%
kirill korotaevkirill korotaev44.44%17.69%
pavel emelianovpavel emelianov33.33%17.69%
andi kleenandi kleen22.22%17.69%
eric sesterhenneric sesterhenn11.11%17.69%
andrew mortonandrew morton11.11%17.69%
Total90100.00%13100.00%

/* This is called by fork, once for every shm attach. */
static void shm_open(struct vm_area_struct *vma) { int err = __shm_open(vma); /* * We raced in the idr lookup or with shm_destroy(). * Either way, the ID is busted. */ WARN_ON_ONCE(err); }

Contributors

PersonTokensPropCommitsCommitProp
kirill a. shutemovkirill a. shutemov2496.00%150.00%
pre-gitpre-git14.00%150.00%
Total25100.00%2100.00%

/* * shm_destroy - free the struct shmid_kernel * * @ns: namespace * @shp: struct to free * * It has to be called with shp and shm_ids.rwsem (writer) locked, * but returns with shp unlocked and freed. */
static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp) { struct file *shm_file; shm_file = shp->shm_file; shp->shm_file = NULL; ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT; shm_rmid(ns, shp); shm_unlock(shp); if (!is_file_hugepages(shm_file)) shmem_lock(shm_file, 0, shp->mlock_user); else if (shp->mlock_user) user_shm_unlock(i_size_read(file_inode(shm_file)), shp->mlock_user); fput(shm_file); ipc_rcu_putref(shp, shm_rcu_free); }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git3934.21%531.25%
greg thelengreg thelen1714.91%16.25%
rik van rielrik van riel1311.40%16.25%
hugh dickinshugh dickins1210.53%212.50%
kirill korotaevkirill korotaev97.89%16.25%
andrew mortonandrew morton87.02%16.25%
linus torvaldslinus torvalds76.14%16.25%
al viroal viro32.63%16.25%
dave hansendave hansen32.63%16.25%
davidlohr buesodavidlohr bueso21.75%16.25%
manfred spraulmanfred spraul10.88%16.25%
Total114100.00%16100.00%

/* * shm_may_destroy - identifies whether shm segment should be destroyed now * * Returns true if and only if there are no active users of the segment and * one of the following is true: * * 1) shmctl(id, IPC_RMID, NULL) was called for this shp * * 2) sysctl kernel.shm_rmid_forced is set to 1. */
static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp) { return (shp->shm_nattch == 0) && (ns->shm_rmid_forced || (shp->shm_perm.mode & SHM_DEST)); }

Contributors

PersonTokensPropCommitsCommitProp
vasiliy kulikovvasiliy kulikov41100.00%1100.00%
Total41100.00%1100.00%

/* * remove the attach descriptor vma. * free memory for segment if it is marked destroyed. * The descriptor has already been removed from the current->mm->mmap list * and will later be kfree()d. */
static void shm_close(struct vm_area_struct *vma) { struct file *file = vma->vm_file; struct shm_file_data *sfd = shm_file_data(file); struct shmid_kernel *shp; struct ipc_namespace *ns = sfd->ns; down_write(&shm_ids(ns).rwsem); /* remove from the list of attaches of the shm segment */ shp = shm_lock(ns, sfd->id); /* * We raced in the idr lookup or with shm_destroy(). * Either way, the ID is busted. */ if (WARN_ON_ONCE(IS_ERR(shp))) goto done; /* no-op */ shp->shm_lprid = task_tgid_vnr(current); shp->shm_dtim = get_seconds(); shp->shm_nattch--; if (shm_may_destroy(ns, shp)) shm_destroy(ns, shp); else shm_unlock(shp); done: up_write(&shm_ids(ns).rwsem); }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git5539.86%533.33%
vasiliy kulikovvasiliy kulikov3122.46%16.67%
kirill a. shutemovkirill a. shutemov1712.32%16.67%
eric w. biedermaneric w. biederman1510.87%16.67%
kirill korotaevkirill korotaev107.25%16.67%
pavel emelianovpavel emelianov32.17%16.67%
andi kleenandi kleen21.45%16.67%
nadia derbeynadia derbey21.45%213.33%
davidlohr buesodavidlohr bueso21.45%16.67%
eric sesterhenneric sesterhenn10.72%16.67%
Total138100.00%15100.00%

/* Called with ns->shm_ids(ns).rwsem locked */
static int shm_try_destroy_orphaned(int id, void *p, void *data) { struct ipc_namespace *ns = data; struct kern_ipc_perm *ipcp = p; struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm); /* * We want to destroy segments without users and with already * exit'ed originating process. * * As shp->* are changed under rwsem, it's safe to skip shp locking. */ if (shp->shm_creator != NULL) return 0; if (shm_may_destroy(ns, shp)) { shm_lock_by_ptr(shp); shm_destroy(ns, shp); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
vasiliy kulikovvasiliy kulikov7488.10%333.33%
pre-gitpre-git89.52%444.44%
davidlohr buesodavidlohr bueso11.19%111.11%
andrew mortonandrew morton11.19%111.11%
Total84100.00%9100.00%


void shm_destroy_orphaned(struct ipc_namespace *ns) { down_write(&shm_ids(ns).rwsem); if (shm_ids(ns).in_use) idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns); up_write(&shm_ids(ns).rwsem); }

Contributors

PersonTokensPropCommitsCommitProp
vasiliy kulikovvasiliy kulikov5596.49%266.67%
davidlohr buesodavidlohr bueso23.51%133.33%
Total57100.00%3100.00%

/* Locking assumes this will only be called with task == current */
void exit_shm(struct task_struct *task) { struct ipc_namespace *ns = task->nsproxy->ipc_ns; struct shmid_kernel *shp, *n; if (list_empty(&task->sysvshm.shm_clist)) return; /* * If kernel.shm_rmid_forced is not set then only keep track of * which shmids are orphaned, so that a later set of the sysctl * can clean them up. */ if (!ns->shm_rmid_forced) { down_read(&shm_ids(ns).rwsem); list_for_each_entry(shp, &task->sysvshm.shm_clist, shm_clist) shp->shm_creator = NULL; /* * Only under read lock but we are only called on current * so no entry on the list will be shared. */ list_del(&task->sysvshm.shm_clist); up_read(&shm_ids(ns).rwsem); return; } /* * Destroy all already created segments, that were not yet mapped, * and mark any mapped as orphan to cover the sysctl toggling. * Destroy is skipped if shm_may_destroy() returns false. */ down_write(&shm_ids(ns).rwsem); list_for_each_entry_safe(shp, n, &task->sysvshm.shm_clist, shm_clist) { shp->shm_creator = NULL; if (shm_may_destroy(ns, shp)) { shm_lock_by_ptr(shp); shm_destroy(ns, shp); } } /* Remove the list head from any segments still attached. */ list_del(&task->sysvshm.shm_clist); up_write(&shm_ids(ns).rwsem); }

Contributors

PersonTokensPropCommitsCommitProp
jack millerjack miller11766.86%220.00%
vasiliy kulikovvasiliy kulikov4525.71%330.00%
pre-gitpre-git74.00%220.00%
kirill korotaevkirill korotaev31.71%110.00%
davidlohr buesodavidlohr bueso21.14%110.00%
nadia derbeynadia derbey10.57%110.00%
Total175100.00%10100.00%


static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) { struct file *file = vma->vm_file; struct shm_file_data *sfd = shm_file_data(file); return sfd->vm_ops->fault(vma, vmf); }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman2144.68%19.09%
pre-gitpre-git1429.79%436.36%
nick pigginnick piggin714.89%218.18%
kirill korotaevkirill korotaev24.26%19.09%
david howellsdavid howells12.13%19.09%
hugh dickinshugh dickins12.13%19.09%
adrian bunkadrian bunk12.13%19.09%
Total47100.00%11100.00%

#ifdef CONFIG_NUMA
static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new) { struct file *file = vma->vm_file; struct shm_file_data *sfd = shm_file_data(file); int err = 0; if (sfd->vm_ops->set_policy) err = sfd->vm_ops->set_policy(vma, new); return err; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman4875.00%133.33%
kirill korotaevkirill korotaev1523.44%133.33%
adrian bunkadrian bunk11.56%133.33%
Total64100.00%3100.00%


static struct mempolicy *shm_get_policy(struct vm_area_struct *vma, unsigned long addr) { struct file *file = vma->vm_file; struct shm_file_data *sfd = shm_file_data(file); struct mempolicy *pol = NULL; if (sfd->vm_ops->get_policy) pol = sfd->vm_ops->get_policy(vma, addr); else if (vma->vm_policy) pol = vma->vm_policy; return pol; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman7290.00%133.33%
adam litkeadam litke78.75%133.33%
adrian bunkadrian bunk11.25%133.33%
Total80100.00%3100.00%

#endif
static int shm_mmap(struct file *file, struct vm_area_struct *vma) { struct shm_file_data *sfd = shm_file_data(file); int ret; /* * In case of remap_file_pages() emulation, the file can represent * removed IPC ID: propogate shm_lock() error to caller. */ ret =__shm_open(vma); if (ret) return ret; ret = sfd->file->f_op->mmap(sfd->file, vma); if (ret) { shm_close(vma); return ret; } sfd->vm_ops = vma->vm_ops; #ifdef CONFIG_MMU WARN_ON(!sfd->vm_ops->fault); #endif vma->vm_ops = &shm_vm_ops; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman7064.81%120.00%
kirill a. shutemovkirill a. shutemov2321.30%120.00%
nick pigginnick piggin98.33%120.00%
david howellsdavid howells54.63%120.00%
davidlohr buesodavidlohr bueso10.93%120.00%
Total108100.00%5100.00%


static int shm_release(struct inode *ino, struct file *file) { struct shm_file_data *sfd = shm_file_data(file); put_ipc_ns(sfd->ns); shm_file_data(file) = NULL; kfree(sfd); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman4593.75%150.00%
kirill korotaevkirill korotaev36.25%150.00%
Total48100.00%2100.00%


static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync) { struct shm_file_data *sfd = shm_file_data(file); if (!sfd->file->f_op->fsync) return -EINVAL; return sfd->file->f_op->fsync(sfd->file, start, end, datasync); }

Contributors

PersonTokensPropCommitsCommitProp
adam litkeadam litke4264.62%133.33%
christoph hellwigchristoph hellwig1320.00%133.33%
josef bacikjosef bacik1015.38%133.33%
Total65100.00%3100.00%


static long shm_fallocate(struct file *file, int mode, loff_t offset, loff_t len) { struct shm_file_data *sfd = shm_file_data(file); if (!sfd->file->f_op->fallocate) return -EOPNOTSUPP; return sfd->file->f_op->fallocate(file, mode, offset, len); }

Contributors

PersonTokensPropCommitsCommitProp
will deaconwill deacon63100.00%1100.00%
Total63100.00%1100.00%


static unsigned long shm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { struct shm_file_data *sfd = shm_file_data(file); return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len, pgoff, flags); }

Contributors

PersonTokensPropCommitsCommitProp
eric w. biedermaneric w. biederman5490.00%150.00%
al viroal viro610.00%150.00%
Total60100.00%2100.00%

static const struct file_operations shm_file_operations = { .mmap = shm_mmap, .fsync = shm_fsync, .release = shm_release, #ifndef CONFIG_MMU .get_unmapped_area = shm_get_unmapped_area, #endif .llseek = noop_llseek, .fallocate = shm_fallocate, }; static const struct file_operations shm_file_operations_huge = { .mmap = shm_mmap, .fsync = shm_fsync, .release = shm_release, .get_unmapped_area = shm_get_unmapped_area, .llseek = noop_llseek, .fallocate = shm_fallocate, };
bool is_file_shm_hugepages(struct file *file) { return file->f_op == &shm_file_operations_huge; }

Contributors

PersonTokensPropCommitsCommitProp
al viroal viro1794.44%150.00%
yaowei baiyaowei bai15.56%150.00%
Total18100.00%2100.00%

static const struct vm_operations_struct shm_vm_ops = { .open = shm_open, /* callback for a new vm-area open */ .close = shm_close, /* callback for when the vm-area is released */ .fault = shm_fault, #if defined(CONFIG_NUMA) .set_policy = shm_set_policy, .get_policy = shm_get_policy, #endif }; /** * newseg - Create a new shared memory segment * @ns: namespace * @params: ptr to the structure that contains key, size and shmflg * * Called with shm_ids.rwsem held as a writer. */
static int newseg(struct ipc_namespace *ns, struct ipc_params *params) { key_t key = params->key; int shmflg = params->flg; size_t size = params->u.size; int error; struct shmid_kernel *shp; size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; struct file *file; char name[13]; int id; vm_flags_t acctflag = 0; if (size < SHMMIN || size > ns->shm_ctlmax) return -EINVAL; if (numpages << PAGE_SHIFT < size) return -ENOSPC; if (ns->shm_tot + numpages < ns->shm_tot || ns->shm_tot + numpages > ns->shm_ctlall) return -ENOSPC; shp = ipc_rcu_alloc(sizeof(*shp)); if (!shp) return -ENOMEM; shp->shm_perm.key = key; shp->shm_perm.mode = (shmflg & S_IRWXUGO); shp->mlock_user = NULL; shp->shm_perm.security = NULL; error = security_shm_alloc(shp); if (error) { ipc_rcu_putref(shp, ipc_rcu_free); return error; } sprintf(name, "SYSV%08x", key); if (shmflg & SHM_HUGETLB) { struct hstate *hs; size_t hugesize; hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK); if (!hs) { error = -EINVAL; goto no_file; } hugesize = ALIGN(size, huge_page_size(hs)); /* hugetlb_file_setup applies strict accounting */ if (shmflg & SHM_NORESERVE) acctflag = VM_NORESERVE; file = hugetlb_file_setup(name, hugesize, acctflag, &shp->mlock_user, HUGETLB_SHMFS_INODE, (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK); } else { /* * Do not allow no accounting for OVERCOMMIT_NEVER, even * if it's asked for. */ if ((shmflg & SHM_NORESERVE) && sysctl_overcommit_memory != OVERCOMMIT_NEVER) acctflag = VM_NORESERVE; file = shmem_kernel_file_setup(name, size, acctflag); } error = PTR_ERR(file); if (IS_ERR(file)) goto no_file; shp->shm_cprid = task_tgid_vnr(current); shp->shm_lprid = 0; shp->shm_atim = shp->shm_dtim = 0; shp->shm_ctim = get_seconds(); shp->shm_segsz = size; shp->shm_nattch = 0; shp->shm_file = file; shp->shm_creator = current; id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni); if (id < 0) { error = id; goto no_id; } list_add(&shp->shm_clist, &current->sysvshm.shm_clist); /* * shmid gets reported as "inode#" in /proc/pid/maps. * proc-ps tools use this. Changing this will break them. */ file_inode(file)->i_ino = shp->shm_perm.id; ns->shm_tot += numpages; error = shp->shm_perm.id; ipc_unlock_object(&shp->shm_perm); rcu_read_unlock(); return error; no_id: if (is_file_hugepages(file) && shp->mlock_user) user_shm_unlock(size, shp->mlock_user); fput(file); no_file: ipc_rcu_putref(shp, shm_rcu_free); return error; }

Contributors

PersonTokensPropCommitsCommitProp
pre-gitpre-git18433.64%1628.07%
linus torvaldslinus torvalds366.58%23.51%
nadia derbeynadia derbey346.22%23.51%
badari pulavartybadari pulavarty254.57%23.51%
manfred spraulmanfred spraul254.57%47.02%
naoya horiguchinaoya horiguchi244.39%11.75%
stephen d. smalleystephen d. smalley244.39%23.51%
hugh dickinshugh dickins244.39%35.26%
andrew mortonandrew morton244.39%47.02%
greg kroah-hartmangreg kroah-hartman224.02%35.26%
li zefanli zefan183.29%11.75%
mel gormanmel gorman173.11%11.75%
jack millerjack miller152.74%11.75%
kirill korotaevkirill korotaev132.38%11.75%
rik van rielrik van riel122.19%11.75%
eric w. biedermaneric w. biederman122.19%11.75%
davidlohr buesodavidlohr bueso112.01%35.26%
andi kleenandi kleen101.83%23.51%
vasiliy kulikovvasiliy kulikov61.10%11.75%
al viroal viro30.55%11.75%
pavel emelianovpavel emelianov30.55%11.75%
eric b munsoneric b munson20.37%11.75%
kosaki motohirokosaki motohiro10.18%11.75%
robin holtrobin holt10.18%11.75%
guy streeterguy streeter10.18%11.75%
Total547100.00%57100.00%

/* * Called with shm_ids.rwsem and ipcp locked. */
static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg) { struct shmid_kernel *shp; shp = container_of(ipcp, struct shmid_kernel, shm_perm); return security_shm_associate(shp, shmflg); }

Contributors

PersonTokensPropCommitsCommitProp
nadia derbeynadia derbey40100.00%2100.00%
Total40100.00%2100.00%

/* * Called with shm_ids.rwsem and ipcp locked. */
static inline int shm_more_checks(struct kern_ipc_perm *ipcp, struct ipc_params *params) { struct shmid_kernel *shp; shp = container_of(ipcp, struct shmid_kernel, shm_perm); if (shp->shm_segsz < params->u.size) return -EINVAL; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
nadia derbeynadia derbey53100.00%2100.00%
Total53100.00%2100.00%

SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg) { struct ipc_namespace *ns; static const struct ipc_ops shm_ops = { .getnew = newseg, .associate = shm_security, .more_checks = shm_more_checks, }; struct ipc_params shm_params; ns = current->nsproxy->ipc_ns; shm_params.key = key; shm_params.flg = shmflg; shm_params.u.size = size; return ipcget(