cregit-Linux how code gets into the kernel

Release 4.17 ipc/shm.c

Directory: ipc
// SPDX-License-Identifier: GPL-2.0
/*
 * 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 shmid_kernel /* private to the kernel */
{
	
struct kern_ipc_perm	shm_perm;
	
struct file		*shm_file;
	
unsigned long		shm_nattch;
	
unsigned long		shm_segsz;
	
time64_t		shm_atim;
	
time64_t		shm_dtim;
	
time64_t		shm_ctim;
	
struct pid		*shm_cprid;
	
struct pid		*shm_lprid;
	
struct user_struct	*mlock_user;

	/* The task created the shm object.  NULL if the task is dead. */
	
struct task_struct	*shm_creator;
	
struct list_head	shm_clist;	/* list by creator */

} __randomize_layout;

/* shm_mode upper byte flags */

#define SHM_DEST	01000	
/* segment will be destroyed on last detach */

#define SHM_LOCKED	02000   
/* segment will not be swapped */


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


int 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)); }

Contributors

PersonTokensPropCommitsCommitProp
Kirill Korotaev2958.00%116.67%
Linus Torvalds (pre-git)612.00%116.67%
Vasiliy Kulikov612.00%116.67%
WANG Cong36.00%116.67%
Pierre Peiffer36.00%116.67%
Guillaume Knispel36.00%116.67%
Total50100.00%6100.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 */ ipc_set_key_private(&shm_ids(ns), &shp->shm_perm); shm_unlock(shp); } else shm_destroy(ns, shp); }

Contributors

PersonTokensPropCommitsCommitProp
Kirill Korotaev4761.04%125.00%
Pierre Peiffer1924.68%125.00%
Guillaume Knispel1012.99%125.00%
Linus Torvalds (pre-git)11.30%125.00%
Total77100.00%4100.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); rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht); }

Contributors

PersonTokensPropCommitsCommitProp
Kirill Korotaev1734.69%116.67%
Guillaume Knispel1326.53%116.67%
Serge E. Hallyn1326.53%116.67%
Nadia Derbey36.12%116.67%
Pierre Peiffer36.12%233.33%
Total49100.00%6100.00%

#endif
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; }

Contributors

PersonTokensPropCommitsCommitProp
Guillaume Knispel1445.16%116.67%
Linus Torvalds (pre-git)929.03%233.33%
Linus Torvalds619.35%116.67%
Kirill Korotaev13.23%116.67%
Pierre Peiffer13.23%116.67%
Total31100.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 Deller1240.00%120.00%
Linus Torvalds (pre-git)826.67%120.00%
Linus Torvalds723.33%120.00%
Mike Waychison26.67%120.00%
Kirill Korotaev13.33%120.00%
Total30100.00%5100.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 Bueso A57100.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 Bueso A57100.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 Derbey3661.02%228.57%
Kirill A. Shutemov1118.64%114.29%
Pierre Peiffer46.78%114.29%
Kirill Korotaev46.78%114.29%
Linus Torvalds (pre-git)46.78%228.57%
Total59100.00%7100.00%


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

Contributors

PersonTokensPropCommitsCommitProp
Vasiliy Kulikov2295.65%150.00%
Davidlohr Bueso A14.35%150.00%
Total23100.00%2100.00%


static void shm_rcu_free(struct rcu_head *head) { struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm, rcu); struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel, shm_perm); security_shm_free(&shp->shm_perm); kvfree(shp); }

Contributors

PersonTokensPropCommitsCommitProp
Davidlohr Bueso A3972.22%120.00%
Manfred Spraul1018.52%120.00%
Eric W. Biedermann35.56%120.00%
Kees Cook23.70%240.00%
Total54100.00%5100.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
Linus Torvalds (pre-git)1333.33%350.00%
Nadia Derbey1025.64%116.67%
Jack Miller820.51%116.67%
Kirill Korotaev820.51%116.67%
Total39100.00%6100.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); if (shp->shm_file != sfd->file) { /* ID was reused */ shm_unlock(shp); return -EINVAL; } shp->shm_atim = ktime_get_real_seconds(); ipc_update_pid(&shp->shm_lprid, task_tgid(current)); shp->shm_nattch++; shm_unlock(shp); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)3429.31%533.33%
Eric W. Biedermann3126.72%213.33%
Eric Biggers2218.97%16.67%
Kirill A. Shutemov1916.38%16.67%
Kirill Korotaev43.45%16.67%
Pavel Emelyanov21.72%16.67%
Deepa Dinamani10.86%16.67%
Andi Kleen10.86%16.67%
Eric Sesterhenn / Snakebyte10.86%16.67%
Andrew Morton10.86%16.67%
Total116100.00%15100.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. Shutemov2496.00%150.00%
Linus Torvalds (pre-git)14.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_update_pid(&shp->shm_cprid, NULL); ipc_update_pid(&shp->shm_lprid, NULL); ipc_rcu_putref(&shp->shm_perm, shm_rcu_free); }

Contributors

PersonTokensPropCommitsCommitProp
Linus Torvalds (pre-git)3928.47%527.78%
Eric W. Biedermann2014.60%15.56%
Greg Thelen1712.41%15.56%
Rik Van Riel139.49%15.56%
Hugh Dickins128.76%211.11%
Kirill Korotaev96.57%15.56%
Andrew Morton85.84%15.56%
Linus Torvalds75.11%15.56%
Manfred Spraul42.92%211.11%
Dave Hansen32.19%15.56%
Al Viro32.19%15.56%
Davidlohr Bueso A21.46%15.56%
Total137100.00%18100.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 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 */ ipc_update_pid(&shp->shm_lprid, task_tgid(current)); shp->shm_dtim = ktime_get_real_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
Linus Torvalds (pre-git)5438.03%529.41%
Vasiliy Kulikov3121.83%15.88%
Eric W. Biedermann2114.79%211.76%
Kirill A. Shutemov1711.97%15.88%
Kirill Korotaev107.04%15.88%
Nadia Derbey21.41%211.76%
Pavel Emelyanov21.41%15.88%
Davidlohr Bueso A21.41%15.88%
Eric Sesterhenn / Snakebyte10.70%15.88%
Deepa Dinamani10.70%15.88%
Andi Kleen10.70%15.88%
Total142100.00%17100.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 Kulikov7488.10%337.50%
Linus Torvalds (pre-git)89.52%337.50%
Andrew Morton11.19%112.50%
Davidlohr Bueso A11.19%112.50%
Total84100.00%8100.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 Kulikov5596.49%266.67%
Davidlohr Bueso A23.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 Miller11766.86%218.18%
Vasiliy Kulikov4525.71%327.27%
Linus Torvalds (pre-git)74.00%327.27%
Kirill Korotaev31.71%19.09%
Davidlohr Bueso A21.14%19.09%
Nadia Derbey10.57%19.09%
Total175100.00%11100.00%


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

Contributors

PersonTokensPropCommitsCommitProp
Eric W. Biedermann1945.24%18.33%
Linus Torvalds (pre-git)1126.19%433.33%
Nicholas Piggin614.29%216.67%
Dave Jiang24.76%18.33%
Adrian Bunk12.38%18.33%
Hugh Dickins12.38%18.33%
David Howells12.38%18.33%
Kirill Korotaev12.38%18.33%
Total42100.00%12100.00%


static int shm_split(struct vm_area_struct *vma, unsigned long addr) { struct file *file = vma->vm_file; struct shm_file_data *sfd = shm_file_data(file); if (sfd->vm_ops->split) return sfd->vm_ops->split(vma, addr); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Mike Kravetz57100.00%1100.00%
Total57100.00%1100.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. Biedermann4875.00%133.33%
Kirill Korotaev1523.44%133.33%
Adrian 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. Biedermann7087.50%125.00%
Adam Litke78.75%125.00%
Kirill Korotaev22.50%125.00%
Adrian Bunk11.25%125.00%
Total80100.00%4100.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 an * IPC ID that was removed, and possibly even reused by another shm * segment already. Propagate this case as an error to caller. */ ret = __shm_open(vma); if (ret) return ret; ret = call_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. Biedermann6159.80%112.50%
Kirill A. Shutemov2221.57%112.50%
Nicholas Piggin98.82%112.50%
David Howells54.90%112.50%
Kirill Korotaev21.96%112.50%
Eric Biggers10.98%112.50%
Miklos Szeredi10.98%112.50%
Davidlohr Bueso A10.98%112.50%
Total102100.00%8100.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); fput(sfd->file); shm_file_data(file) = NULL; kfree(sfd); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Eric W. Biedermann4887.27%150.00%
Eric Biggers712.73%150.00%
Total55100.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 Litke3960.00%125.00%
Josef Bacik1015.38%125.00%
Christoph Hellwig913.85%125.00%
Jeff Layton710.77%125.00%
Total65100.00%4100.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 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. Biedermann5490.00%150.00%
Al 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, .get_unmapped_area = shm_get_unmapped_area, .llseek = noop_llseek, .fallocate = shm_fallocate, }; /* * shm_file_operations_huge is now identical to shm_file_operations, * but we keep it distinct for the sake of is_file_shm_hugepages(). */ 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 Viro1794.44%150.00%
Yaowei 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, .split = shm_split, #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]; 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 = kvmalloc(sizeof(*shp), GFP_KERNEL); if (unlikely(!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->shm_perm); if (error) { kvfree(shp); 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