shm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * linux/ipc/shm.c
  3. * Copyright (C) 1992, 1993 Krishna Balasubramanian
  4. * Many improvements/fixes by Bruno Haible.
  5. * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
  6. * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
  7. *
  8. * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  9. * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
  10. * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
  11. * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
  12. * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
  13. * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
  14. * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
  15. *
  16. * support for audit of ipc object properties and permission changes
  17. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  18. *
  19. * namespaces support
  20. * OpenVZ, SWsoft Inc.
  21. * Pavel Emelianov <xemul@openvz.org>
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/shm.h>
  27. #include <linux/init.h>
  28. #include <linux/file.h>
  29. #include <linux/mman.h>
  30. #include <linux/shmem_fs.h>
  31. #include <linux/security.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/audit.h>
  34. #include <linux/capability.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/rwsem.h>
  38. #include <linux/nsproxy.h>
  39. #include <linux/mount.h>
  40. #include <linux/ipc_namespace.h>
  41. #include <asm/uaccess.h>
  42. #include "util.h"
  43. struct shm_file_data {
  44. int id;
  45. struct ipc_namespace *ns;
  46. struct file *file;
  47. const struct vm_operations_struct *vm_ops;
  48. };
  49. #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
  50. static const struct file_operations shm_file_operations;
  51. static const struct vm_operations_struct shm_vm_ops;
  52. #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
  53. #define shm_unlock(shp) \
  54. ipc_unlock(&(shp)->shm_perm)
  55. static int newseg(struct ipc_namespace *, struct ipc_params *);
  56. static void shm_open(struct vm_area_struct *vma);
  57. static void shm_close(struct vm_area_struct *vma);
  58. static void shm_destroy (struct ipc_namespace *ns, struct shmid_kernel *shp);
  59. #ifdef CONFIG_PROC_FS
  60. static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
  61. #endif
  62. void shm_init_ns(struct ipc_namespace *ns)
  63. {
  64. ns->shm_ctlmax = SHMMAX;
  65. ns->shm_ctlall = SHMALL;
  66. ns->shm_ctlmni = SHMMNI;
  67. ns->shm_rmid_forced = 0;
  68. ns->shm_tot = 0;
  69. ipc_init_ids(&shm_ids(ns));
  70. }
  71. /*
  72. * Called with shm_ids.rw_mutex (writer) and the shp structure locked.
  73. * Only shm_ids.rw_mutex remains locked on exit.
  74. */
  75. static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  76. {
  77. struct shmid_kernel *shp;
  78. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  79. if (shp->shm_nattch){
  80. shp->shm_perm.mode |= SHM_DEST;
  81. /* Do not find it any more */
  82. shp->shm_perm.key = IPC_PRIVATE;
  83. shm_unlock(shp);
  84. } else
  85. shm_destroy(ns, shp);
  86. }
  87. #ifdef CONFIG_IPC_NS
  88. void shm_exit_ns(struct ipc_namespace *ns)
  89. {
  90. free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
  91. idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
  92. }
  93. #endif
  94. static int __init ipc_ns_init(void)
  95. {
  96. shm_init_ns(&init_ipc_ns);
  97. return 0;
  98. }
  99. pure_initcall(ipc_ns_init);
  100. void __init shm_init (void)
  101. {
  102. ipc_init_proc_interface("sysvipc/shm",
  103. #if BITS_PER_LONG <= 32
  104. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  105. #else
  106. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  107. #endif
  108. IPC_SHM_IDS, sysvipc_shm_proc_show);
  109. }
  110. /*
  111. * shm_lock_(check_) routines are called in the paths where the rw_mutex
  112. * is not necessarily held.
  113. */
  114. static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
  115. {
  116. struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
  117. if (IS_ERR(ipcp))
  118. return (struct shmid_kernel *)ipcp;
  119. return container_of(ipcp, struct shmid_kernel, shm_perm);
  120. }
  121. static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
  122. {
  123. rcu_read_lock();
  124. spin_lock(&ipcp->shm_perm.lock);
  125. }
  126. static inline struct shmid_kernel *shm_lock_check(struct ipc_namespace *ns,
  127. int id)
  128. {
  129. struct kern_ipc_perm *ipcp = ipc_lock_check(&shm_ids(ns), id);
  130. if (IS_ERR(ipcp))
  131. return (struct shmid_kernel *)ipcp;
  132. return container_of(ipcp, struct shmid_kernel, shm_perm);
  133. }
  134. static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
  135. {
  136. ipc_rmid(&shm_ids(ns), &s->shm_perm);
  137. }
  138. /* This is called by fork, once for every shm attach. */
  139. static void shm_open(struct vm_area_struct *vma)
  140. {
  141. struct file *file = vma->vm_file;
  142. struct shm_file_data *sfd = shm_file_data(file);
  143. struct shmid_kernel *shp;
  144. shp = shm_lock(sfd->ns, sfd->id);
  145. BUG_ON(IS_ERR(shp));
  146. shp->shm_atim = get_seconds();
  147. shp->shm_lprid = task_tgid_vnr(current);
  148. shp->shm_nattch++;
  149. shm_unlock(shp);
  150. }
  151. /*
  152. * shm_destroy - free the struct shmid_kernel
  153. *
  154. * @ns: namespace
  155. * @shp: struct to free
  156. *
  157. * It has to be called with shp and shm_ids.rw_mutex (writer) locked,
  158. * but returns with shp unlocked and freed.
  159. */
  160. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  161. {
  162. ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  163. shm_rmid(ns, shp);
  164. shm_unlock(shp);
  165. if (!is_file_hugepages(shp->shm_file))
  166. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  167. else if (shp->mlock_user)
  168. user_shm_unlock(shp->shm_file->f_path.dentry->d_inode->i_size,
  169. shp->mlock_user);
  170. fput (shp->shm_file);
  171. security_shm_free(shp);
  172. ipc_rcu_putref(shp);
  173. }
  174. /*
  175. * shm_may_destroy - identifies whether shm segment should be destroyed now
  176. *
  177. * Returns true if and only if there are no active users of the segment and
  178. * one of the following is true:
  179. *
  180. * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
  181. *
  182. * 2) sysctl kernel.shm_rmid_forced is set to 1.
  183. */
  184. static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  185. {
  186. return (shp->shm_nattch == 0) &&
  187. (ns->shm_rmid_forced ||
  188. (shp->shm_perm.mode & SHM_DEST));
  189. }
  190. /*
  191. * remove the attach descriptor vma.
  192. * free memory for segment if it is marked destroyed.
  193. * The descriptor has already been removed from the current->mm->mmap list
  194. * and will later be kfree()d.
  195. */
  196. static void shm_close(struct vm_area_struct *vma)
  197. {
  198. struct file * file = vma->vm_file;
  199. struct shm_file_data *sfd = shm_file_data(file);
  200. struct shmid_kernel *shp;
  201. struct ipc_namespace *ns = sfd->ns;
  202. down_write(&shm_ids(ns).rw_mutex);
  203. /* remove from the list of attaches of the shm segment */
  204. shp = shm_lock(ns, sfd->id);
  205. BUG_ON(IS_ERR(shp));
  206. shp->shm_lprid = task_tgid_vnr(current);
  207. shp->shm_dtim = get_seconds();
  208. shp->shm_nattch--;
  209. if (shm_may_destroy(ns, shp))
  210. shm_destroy(ns, shp);
  211. else
  212. shm_unlock(shp);
  213. up_write(&shm_ids(ns).rw_mutex);
  214. }
  215. /* Called with ns->shm_ids(ns).rw_mutex locked */
  216. static int shm_try_destroy_current(int id, void *p, void *data)
  217. {
  218. struct ipc_namespace *ns = data;
  219. struct kern_ipc_perm *ipcp = p;
  220. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  221. if (shp->shm_creator != current)
  222. return 0;
  223. /*
  224. * Mark it as orphaned to destroy the segment when
  225. * kernel.shm_rmid_forced is changed.
  226. * It is noop if the following shm_may_destroy() returns true.
  227. */
  228. shp->shm_creator = NULL;
  229. /*
  230. * Don't even try to destroy it. If shm_rmid_forced=0 and IPC_RMID
  231. * is not set, it shouldn't be deleted here.
  232. */
  233. if (!ns->shm_rmid_forced)
  234. return 0;
  235. if (shm_may_destroy(ns, shp)) {
  236. shm_lock_by_ptr(shp);
  237. shm_destroy(ns, shp);
  238. }
  239. return 0;
  240. }
  241. /* Called with ns->shm_ids(ns).rw_mutex locked */
  242. static int shm_try_destroy_orphaned(int id, void *p, void *data)
  243. {
  244. struct ipc_namespace *ns = data;
  245. struct kern_ipc_perm *ipcp = p;
  246. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  247. /*
  248. * We want to destroy segments without users and with already
  249. * exit'ed originating process.
  250. *
  251. * As shp->* are changed under rw_mutex, it's safe to skip shp locking.
  252. */
  253. if (shp->shm_creator != NULL)
  254. return 0;
  255. if (shm_may_destroy(ns, shp)) {
  256. shm_lock_by_ptr(shp);
  257. shm_destroy(ns, shp);
  258. }
  259. return 0;
  260. }
  261. void shm_destroy_orphaned(struct ipc_namespace *ns)
  262. {
  263. down_write(&shm_ids(ns).rw_mutex);
  264. if (shm_ids(ns).in_use)
  265. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
  266. up_write(&shm_ids(ns).rw_mutex);
  267. }
  268. void exit_shm(struct task_struct *task)
  269. {
  270. struct ipc_namespace *ns = task->nsproxy->ipc_ns;
  271. if (shm_ids(ns).in_use == 0)
  272. return;
  273. /* Destroy all already created segments, but not mapped yet */
  274. down_write(&shm_ids(ns).rw_mutex);
  275. if (shm_ids(ns).in_use)
  276. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_current, ns);
  277. up_write(&shm_ids(ns).rw_mutex);
  278. }
  279. static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  280. {
  281. struct file *file = vma->vm_file;
  282. struct shm_file_data *sfd = shm_file_data(file);
  283. return sfd->vm_ops->fault(vma, vmf);
  284. }
  285. #ifdef CONFIG_NUMA
  286. static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  287. {
  288. struct file *file = vma->vm_file;
  289. struct shm_file_data *sfd = shm_file_data(file);
  290. int err = 0;
  291. if (sfd->vm_ops->set_policy)
  292. err = sfd->vm_ops->set_policy(vma, new);
  293. return err;
  294. }
  295. static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
  296. unsigned long addr)
  297. {
  298. struct file *file = vma->vm_file;
  299. struct shm_file_data *sfd = shm_file_data(file);
  300. struct mempolicy *pol = NULL;
  301. if (sfd->vm_ops->get_policy)
  302. pol = sfd->vm_ops->get_policy(vma, addr);
  303. else if (vma->vm_policy)
  304. pol = vma->vm_policy;
  305. return pol;
  306. }
  307. #endif
  308. static int shm_mmap(struct file * file, struct vm_area_struct * vma)
  309. {
  310. struct shm_file_data *sfd = shm_file_data(file);
  311. int ret;
  312. ret = sfd->file->f_op->mmap(sfd->file, vma);
  313. if (ret != 0)
  314. return ret;
  315. sfd->vm_ops = vma->vm_ops;
  316. #ifdef CONFIG_MMU
  317. BUG_ON(!sfd->vm_ops->fault);
  318. #endif
  319. vma->vm_ops = &shm_vm_ops;
  320. shm_open(vma);
  321. return ret;
  322. }
  323. static int shm_release(struct inode *ino, struct file *file)
  324. {
  325. struct shm_file_data *sfd = shm_file_data(file);
  326. put_ipc_ns(sfd->ns);
  327. shm_file_data(file) = NULL;
  328. kfree(sfd);
  329. return 0;
  330. }
  331. static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  332. {
  333. struct shm_file_data *sfd = shm_file_data(file);
  334. if (!sfd->file->f_op->fsync)
  335. return -EINVAL;
  336. return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
  337. }
  338. static unsigned long shm_get_unmapped_area(struct file *file,
  339. unsigned long addr, unsigned long len, unsigned long pgoff,
  340. unsigned long flags)
  341. {
  342. struct shm_file_data *sfd = shm_file_data(file);
  343. return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
  344. pgoff, flags);
  345. }
  346. static const struct file_operations shm_file_operations = {
  347. .mmap = shm_mmap,
  348. .fsync = shm_fsync,
  349. .release = shm_release,
  350. #ifndef CONFIG_MMU
  351. .get_unmapped_area = shm_get_unmapped_area,
  352. #endif
  353. .llseek = noop_llseek,
  354. };
  355. static const struct file_operations shm_file_operations_huge = {
  356. .mmap = shm_mmap,
  357. .fsync = shm_fsync,
  358. .release = shm_release,
  359. .get_unmapped_area = shm_get_unmapped_area,
  360. .llseek = noop_llseek,
  361. };
  362. int is_file_shm_hugepages(struct file *file)
  363. {
  364. return file->f_op == &shm_file_operations_huge;
  365. }
  366. static const struct vm_operations_struct shm_vm_ops = {
  367. .open = shm_open, /* callback for a new vm-area open */
  368. .close = shm_close, /* callback for when the vm-area is released */
  369. .fault = shm_fault,
  370. #if defined(CONFIG_NUMA)
  371. .set_policy = shm_set_policy,
  372. .get_policy = shm_get_policy,
  373. #endif
  374. };
  375. /**
  376. * newseg - Create a new shared memory segment
  377. * @ns: namespace
  378. * @params: ptr to the structure that contains key, size and shmflg
  379. *
  380. * Called with shm_ids.rw_mutex held as a writer.
  381. */
  382. static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
  383. {
  384. key_t key = params->key;
  385. int shmflg = params->flg;
  386. size_t size = params->u.size;
  387. int error;
  388. struct shmid_kernel *shp;
  389. int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
  390. struct file * file;
  391. char name[13];
  392. int id;
  393. vm_flags_t acctflag = 0;
  394. if (size < SHMMIN || size > ns->shm_ctlmax)
  395. return -EINVAL;
  396. if (ns->shm_tot + numpages > ns->shm_ctlall)
  397. return -ENOSPC;
  398. shp = ipc_rcu_alloc(sizeof(*shp));
  399. if (!shp)
  400. return -ENOMEM;
  401. shp->shm_perm.key = key;
  402. shp->shm_perm.mode = (shmflg & S_IRWXUGO);
  403. shp->mlock_user = NULL;
  404. shp->shm_perm.security = NULL;
  405. error = security_shm_alloc(shp);
  406. if (error) {
  407. ipc_rcu_putref(shp);
  408. return error;
  409. }
  410. sprintf (name, "SYSV%08x", key);
  411. if (shmflg & SHM_HUGETLB) {
  412. /* hugetlb_file_setup applies strict accounting */
  413. if (shmflg & SHM_NORESERVE)
  414. acctflag = VM_NORESERVE;
  415. file = hugetlb_file_setup(name, size, acctflag,
  416. &shp->mlock_user, HUGETLB_SHMFS_INODE);
  417. } else {
  418. /*
  419. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  420. * if it's asked for.
  421. */
  422. if ((shmflg & SHM_NORESERVE) &&
  423. sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  424. acctflag = VM_NORESERVE;
  425. file = shmem_file_setup(name, size, acctflag);
  426. }
  427. error = PTR_ERR(file);
  428. if (IS_ERR(file))
  429. goto no_file;
  430. id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  431. if (id < 0) {
  432. error = id;
  433. goto no_id;
  434. }
  435. shp->shm_cprid = task_tgid_vnr(current);
  436. shp->shm_lprid = 0;
  437. shp->shm_atim = shp->shm_dtim = 0;
  438. shp->shm_ctim = get_seconds();
  439. shp->shm_segsz = size;
  440. shp->shm_nattch = 0;
  441. shp->shm_file = file;
  442. shp->shm_creator = current;
  443. /*
  444. * shmid gets reported as "inode#" in /proc/pid/maps.
  445. * proc-ps tools use this. Changing this will break them.
  446. */
  447. file->f_dentry->d_inode->i_ino = shp->shm_perm.id;
  448. ns->shm_tot += numpages;
  449. error = shp->shm_perm.id;
  450. shm_unlock(shp);
  451. return error;
  452. no_id:
  453. if (is_file_hugepages(file) && shp->mlock_user)
  454. user_shm_unlock(size, shp->mlock_user);
  455. fput(file);
  456. no_file:
  457. security_shm_free(shp);
  458. ipc_rcu_putref(shp);
  459. return error;
  460. }
  461. /*
  462. * Called with shm_ids.rw_mutex and ipcp locked.
  463. */
  464. static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
  465. {
  466. struct shmid_kernel *shp;
  467. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  468. return security_shm_associate(shp, shmflg);
  469. }
  470. /*
  471. * Called with shm_ids.rw_mutex and ipcp locked.
  472. */
  473. static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
  474. struct ipc_params *params)
  475. {
  476. struct shmid_kernel *shp;
  477. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  478. if (shp->shm_segsz < params->u.size)
  479. return -EINVAL;
  480. return 0;
  481. }
  482. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  483. {
  484. struct ipc_namespace *ns;
  485. struct ipc_ops shm_ops;
  486. struct ipc_params shm_params;
  487. ns = current->nsproxy->ipc_ns;
  488. shm_ops.getnew = newseg;
  489. shm_ops.associate = shm_security;
  490. shm_ops.more_checks = shm_more_checks;
  491. shm_params.key = key;
  492. shm_params.flg = shmflg;
  493. shm_params.u.size = size;
  494. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  495. }
  496. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  497. {
  498. switch(version) {
  499. case IPC_64:
  500. return copy_to_user(buf, in, sizeof(*in));
  501. case IPC_OLD:
  502. {
  503. struct shmid_ds out;
  504. memset(&out, 0, sizeof(out));
  505. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  506. out.shm_segsz = in->shm_segsz;
  507. out.shm_atime = in->shm_atime;
  508. out.shm_dtime = in->shm_dtime;
  509. out.shm_ctime = in->shm_ctime;
  510. out.shm_cpid = in->shm_cpid;
  511. out.shm_lpid = in->shm_lpid;
  512. out.shm_nattch = in->shm_nattch;
  513. return copy_to_user(buf, &out, sizeof(out));
  514. }
  515. default:
  516. return -EINVAL;
  517. }
  518. }
  519. static inline unsigned long
  520. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  521. {
  522. switch(version) {
  523. case IPC_64:
  524. if (copy_from_user(out, buf, sizeof(*out)))
  525. return -EFAULT;
  526. return 0;
  527. case IPC_OLD:
  528. {
  529. struct shmid_ds tbuf_old;
  530. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  531. return -EFAULT;
  532. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  533. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  534. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  535. return 0;
  536. }
  537. default:
  538. return -EINVAL;
  539. }
  540. }
  541. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  542. {
  543. switch(version) {
  544. case IPC_64:
  545. return copy_to_user(buf, in, sizeof(*in));
  546. case IPC_OLD:
  547. {
  548. struct shminfo out;
  549. if(in->shmmax > INT_MAX)
  550. out.shmmax = INT_MAX;
  551. else
  552. out.shmmax = (int)in->shmmax;
  553. out.shmmin = in->shmmin;
  554. out.shmmni = in->shmmni;
  555. out.shmseg = in->shmseg;
  556. out.shmall = in->shmall;
  557. return copy_to_user(buf, &out, sizeof(out));
  558. }
  559. default:
  560. return -EINVAL;
  561. }
  562. }
  563. /*
  564. * Calculate and add used RSS and swap pages of a shm.
  565. * Called with shm_ids.rw_mutex held as a reader
  566. */
  567. static void shm_add_rss_swap(struct shmid_kernel *shp,
  568. unsigned long *rss_add, unsigned long *swp_add)
  569. {
  570. struct inode *inode;
  571. inode = shp->shm_file->f_path.dentry->d_inode;
  572. if (is_file_hugepages(shp->shm_file)) {
  573. struct address_space *mapping = inode->i_mapping;
  574. struct hstate *h = hstate_file(shp->shm_file);
  575. *rss_add += pages_per_huge_page(h) * mapping->nrpages;
  576. } else {
  577. #ifdef CONFIG_SHMEM
  578. struct shmem_inode_info *info = SHMEM_I(inode);
  579. spin_lock(&info->lock);
  580. *rss_add += inode->i_mapping->nrpages;
  581. *swp_add += info->swapped;
  582. spin_unlock(&info->lock);
  583. #else
  584. *rss_add += inode->i_mapping->nrpages;
  585. #endif
  586. }
  587. }
  588. /*
  589. * Called with shm_ids.rw_mutex held as a reader
  590. */
  591. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  592. unsigned long *swp)
  593. {
  594. int next_id;
  595. int total, in_use;
  596. *rss = 0;
  597. *swp = 0;
  598. in_use = shm_ids(ns).in_use;
  599. for (total = 0, next_id = 0; total < in_use; next_id++) {
  600. struct kern_ipc_perm *ipc;
  601. struct shmid_kernel *shp;
  602. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  603. if (ipc == NULL)
  604. continue;
  605. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  606. shm_add_rss_swap(shp, rss, swp);
  607. total++;
  608. }
  609. }
  610. /*
  611. * This function handles some shmctl commands which require the rw_mutex
  612. * to be held in write mode.
  613. * NOTE: no locks must be held, the rw_mutex is taken inside this function.
  614. */
  615. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  616. struct shmid_ds __user *buf, int version)
  617. {
  618. struct kern_ipc_perm *ipcp;
  619. struct shmid64_ds shmid64;
  620. struct shmid_kernel *shp;
  621. int err;
  622. if (cmd == IPC_SET) {
  623. if (copy_shmid_from_user(&shmid64, buf, version))
  624. return -EFAULT;
  625. }
  626. ipcp = ipcctl_pre_down(ns, &shm_ids(ns), shmid, cmd,
  627. &shmid64.shm_perm, 0);
  628. if (IS_ERR(ipcp))
  629. return PTR_ERR(ipcp);
  630. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  631. err = security_shm_shmctl(shp, cmd);
  632. if (err)
  633. goto out_unlock;
  634. switch (cmd) {
  635. case IPC_RMID:
  636. do_shm_rmid(ns, ipcp);
  637. goto out_up;
  638. case IPC_SET:
  639. ipc_update_perm(&shmid64.shm_perm, ipcp);
  640. shp->shm_ctim = get_seconds();
  641. break;
  642. default:
  643. err = -EINVAL;
  644. }
  645. out_unlock:
  646. shm_unlock(shp);
  647. out_up:
  648. up_write(&shm_ids(ns).rw_mutex);
  649. return err;
  650. }
  651. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  652. {
  653. struct shmid_kernel *shp;
  654. int err, version;
  655. struct ipc_namespace *ns;
  656. if (cmd < 0 || shmid < 0) {
  657. err = -EINVAL;
  658. goto out;
  659. }
  660. version = ipc_parse_version(&cmd);
  661. ns = current->nsproxy->ipc_ns;
  662. switch (cmd) { /* replace with proc interface ? */
  663. case IPC_INFO:
  664. {
  665. struct shminfo64 shminfo;
  666. err = security_shm_shmctl(NULL, cmd);
  667. if (err)
  668. return err;
  669. memset(&shminfo, 0, sizeof(shminfo));
  670. shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
  671. shminfo.shmmax = ns->shm_ctlmax;
  672. shminfo.shmall = ns->shm_ctlall;
  673. shminfo.shmmin = SHMMIN;
  674. if(copy_shminfo_to_user (buf, &shminfo, version))
  675. return -EFAULT;
  676. down_read(&shm_ids(ns).rw_mutex);
  677. err = ipc_get_maxid(&shm_ids(ns));
  678. up_read(&shm_ids(ns).rw_mutex);
  679. if(err<0)
  680. err = 0;
  681. goto out;
  682. }
  683. case SHM_INFO:
  684. {
  685. struct shm_info shm_info;
  686. err = security_shm_shmctl(NULL, cmd);
  687. if (err)
  688. return err;
  689. memset(&shm_info, 0, sizeof(shm_info));
  690. down_read(&shm_ids(ns).rw_mutex);
  691. shm_info.used_ids = shm_ids(ns).in_use;
  692. shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
  693. shm_info.shm_tot = ns->shm_tot;
  694. shm_info.swap_attempts = 0;
  695. shm_info.swap_successes = 0;
  696. err = ipc_get_maxid(&shm_ids(ns));
  697. up_read(&shm_ids(ns).rw_mutex);
  698. if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
  699. err = -EFAULT;
  700. goto out;
  701. }
  702. err = err < 0 ? 0 : err;
  703. goto out;
  704. }
  705. case SHM_STAT:
  706. case IPC_STAT:
  707. {
  708. struct shmid64_ds tbuf;
  709. int result;
  710. if (cmd == SHM_STAT) {
  711. shp = shm_lock(ns, shmid);
  712. if (IS_ERR(shp)) {
  713. err = PTR_ERR(shp);
  714. goto out;
  715. }
  716. result = shp->shm_perm.id;
  717. } else {
  718. shp = shm_lock_check(ns, shmid);
  719. if (IS_ERR(shp)) {
  720. err = PTR_ERR(shp);
  721. goto out;
  722. }
  723. result = 0;
  724. }
  725. err = -EACCES;
  726. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  727. goto out_unlock;
  728. err = security_shm_shmctl(shp, cmd);
  729. if (err)
  730. goto out_unlock;
  731. memset(&tbuf, 0, sizeof(tbuf));
  732. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  733. tbuf.shm_segsz = shp->shm_segsz;
  734. tbuf.shm_atime = shp->shm_atim;
  735. tbuf.shm_dtime = shp->shm_dtim;
  736. tbuf.shm_ctime = shp->shm_ctim;
  737. tbuf.shm_cpid = shp->shm_cprid;
  738. tbuf.shm_lpid = shp->shm_lprid;
  739. tbuf.shm_nattch = shp->shm_nattch;
  740. shm_unlock(shp);
  741. if(copy_shmid_to_user (buf, &tbuf, version))
  742. err = -EFAULT;
  743. else
  744. err = result;
  745. goto out;
  746. }
  747. case SHM_LOCK:
  748. case SHM_UNLOCK:
  749. {
  750. struct file *uninitialized_var(shm_file);
  751. lru_add_drain_all(); /* drain pagevecs to lru lists */
  752. shp = shm_lock_check(ns, shmid);
  753. if (IS_ERR(shp)) {
  754. err = PTR_ERR(shp);
  755. goto out;
  756. }
  757. audit_ipc_obj(&(shp->shm_perm));
  758. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  759. uid_t euid = current_euid();
  760. err = -EPERM;
  761. if (euid != shp->shm_perm.uid &&
  762. euid != shp->shm_perm.cuid)
  763. goto out_unlock;
  764. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK))
  765. goto out_unlock;
  766. }
  767. err = security_shm_shmctl(shp, cmd);
  768. if (err)
  769. goto out_unlock;
  770. if(cmd==SHM_LOCK) {
  771. struct user_struct *user = current_user();
  772. if (!is_file_hugepages(shp->shm_file)) {
  773. err = shmem_lock(shp->shm_file, 1, user);
  774. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
  775. shp->shm_perm.mode |= SHM_LOCKED;
  776. shp->mlock_user = user;
  777. }
  778. }
  779. } else if (!is_file_hugepages(shp->shm_file)) {
  780. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  781. shp->shm_perm.mode &= ~SHM_LOCKED;
  782. shp->mlock_user = NULL;
  783. }
  784. shm_unlock(shp);
  785. goto out;
  786. }
  787. case IPC_RMID:
  788. case IPC_SET:
  789. err = shmctl_down(ns, shmid, cmd, buf, version);
  790. return err;
  791. default:
  792. return -EINVAL;
  793. }
  794. out_unlock:
  795. shm_unlock(shp);
  796. out:
  797. return err;
  798. }
  799. /*
  800. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  801. *
  802. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  803. * "raddr" thing points to kernel space, and there has to be a wrapper around
  804. * this.
  805. */
  806. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
  807. {
  808. struct shmid_kernel *shp;
  809. unsigned long addr;
  810. unsigned long size;
  811. struct file * file;
  812. int err;
  813. unsigned long flags;
  814. unsigned long prot;
  815. int acc_mode;
  816. unsigned long user_addr;
  817. struct ipc_namespace *ns;
  818. struct shm_file_data *sfd;
  819. struct path path;
  820. fmode_t f_mode;
  821. err = -EINVAL;
  822. if (shmid < 0)
  823. goto out;
  824. else if ((addr = (ulong)shmaddr)) {
  825. if (addr & (SHMLBA-1)) {
  826. if (shmflg & SHM_RND)
  827. addr &= ~(SHMLBA-1); /* round down */
  828. else
  829. #ifndef __ARCH_FORCE_SHMLBA
  830. if (addr & ~PAGE_MASK)
  831. #endif
  832. goto out;
  833. }
  834. flags = MAP_SHARED | MAP_FIXED;
  835. } else {
  836. if ((shmflg & SHM_REMAP))
  837. goto out;
  838. flags = MAP_SHARED;
  839. }
  840. if (shmflg & SHM_RDONLY) {
  841. prot = PROT_READ;
  842. acc_mode = S_IRUGO;
  843. f_mode = FMODE_READ;
  844. } else {
  845. prot = PROT_READ | PROT_WRITE;
  846. acc_mode = S_IRUGO | S_IWUGO;
  847. f_mode = FMODE_READ | FMODE_WRITE;
  848. }
  849. if (shmflg & SHM_EXEC) {
  850. prot |= PROT_EXEC;
  851. acc_mode |= S_IXUGO;
  852. }
  853. /*
  854. * We cannot rely on the fs check since SYSV IPC does have an
  855. * additional creator id...
  856. */
  857. ns = current->nsproxy->ipc_ns;
  858. shp = shm_lock_check(ns, shmid);
  859. if (IS_ERR(shp)) {
  860. err = PTR_ERR(shp);
  861. goto out;
  862. }
  863. err = -EACCES;
  864. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  865. goto out_unlock;
  866. err = security_shm_shmat(shp, shmaddr, shmflg);
  867. if (err)
  868. goto out_unlock;
  869. path = shp->shm_file->f_path;
  870. path_get(&path);
  871. shp->shm_nattch++;
  872. size = i_size_read(path.dentry->d_inode);
  873. shm_unlock(shp);
  874. err = -ENOMEM;
  875. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  876. if (!sfd)
  877. goto out_put_dentry;
  878. file = alloc_file(&path, f_mode,
  879. is_file_hugepages(shp->shm_file) ?
  880. &shm_file_operations_huge :
  881. &shm_file_operations);
  882. if (!file)
  883. goto out_free;
  884. file->private_data = sfd;
  885. file->f_mapping = shp->shm_file->f_mapping;
  886. sfd->id = shp->shm_perm.id;
  887. sfd->ns = get_ipc_ns(ns);
  888. sfd->file = shp->shm_file;
  889. sfd->vm_ops = NULL;
  890. down_write(&current->mm->mmap_sem);
  891. if (addr && !(shmflg & SHM_REMAP)) {
  892. err = -EINVAL;
  893. if (find_vma_intersection(current->mm, addr, addr + size))
  894. goto invalid;
  895. /*
  896. * If shm segment goes below stack, make sure there is some
  897. * space left for the stack to grow (at least 4 pages).
  898. */
  899. if (addr < current->mm->start_stack &&
  900. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  901. goto invalid;
  902. }
  903. user_addr = do_mmap (file, addr, size, prot, flags, 0);
  904. *raddr = user_addr;
  905. err = 0;
  906. if (IS_ERR_VALUE(user_addr))
  907. err = (long)user_addr;
  908. invalid:
  909. up_write(&current->mm->mmap_sem);
  910. fput(file);
  911. out_nattch:
  912. down_write(&shm_ids(ns).rw_mutex);
  913. shp = shm_lock(ns, shmid);
  914. BUG_ON(IS_ERR(shp));
  915. shp->shm_nattch--;
  916. if (shm_may_destroy(ns, shp))
  917. shm_destroy(ns, shp);
  918. else
  919. shm_unlock(shp);
  920. up_write(&shm_ids(ns).rw_mutex);
  921. out:
  922. return err;
  923. out_unlock:
  924. shm_unlock(shp);
  925. goto out;
  926. out_free:
  927. kfree(sfd);
  928. out_put_dentry:
  929. path_put(&path);
  930. goto out_nattch;
  931. }
  932. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  933. {
  934. unsigned long ret;
  935. long err;
  936. err = do_shmat(shmid, shmaddr, shmflg, &ret);
  937. if (err)
  938. return err;
  939. force_successful_syscall_return();
  940. return (long)ret;
  941. }
  942. /*
  943. * detach and kill segment if marked destroyed.
  944. * The work is done in shm_close.
  945. */
  946. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  947. {
  948. struct mm_struct *mm = current->mm;
  949. struct vm_area_struct *vma;
  950. unsigned long addr = (unsigned long)shmaddr;
  951. int retval = -EINVAL;
  952. #ifdef CONFIG_MMU
  953. loff_t size = 0;
  954. struct vm_area_struct *next;
  955. #endif
  956. if (addr & ~PAGE_MASK)
  957. return retval;
  958. down_write(&mm->mmap_sem);
  959. /*
  960. * This function tries to be smart and unmap shm segments that
  961. * were modified by partial mlock or munmap calls:
  962. * - It first determines the size of the shm segment that should be
  963. * unmapped: It searches for a vma that is backed by shm and that
  964. * started at address shmaddr. It records it's size and then unmaps
  965. * it.
  966. * - Then it unmaps all shm vmas that started at shmaddr and that
  967. * are within the initially determined size.
  968. * Errors from do_munmap are ignored: the function only fails if
  969. * it's called with invalid parameters or if it's called to unmap
  970. * a part of a vma. Both calls in this function are for full vmas,
  971. * the parameters are directly copied from the vma itself and always
  972. * valid - therefore do_munmap cannot fail. (famous last words?)
  973. */
  974. /*
  975. * If it had been mremap()'d, the starting address would not
  976. * match the usual checks anyway. So assume all vma's are
  977. * above the starting address given.
  978. */
  979. vma = find_vma(mm, addr);
  980. #ifdef CONFIG_MMU
  981. while (vma) {
  982. next = vma->vm_next;
  983. /*
  984. * Check if the starting address would match, i.e. it's
  985. * a fragment created by mprotect() and/or munmap(), or it
  986. * otherwise it starts at this address with no hassles.
  987. */
  988. if ((vma->vm_ops == &shm_vm_ops) &&
  989. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  990. size = vma->vm_file->f_path.dentry->d_inode->i_size;
  991. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  992. /*
  993. * We discovered the size of the shm segment, so
  994. * break out of here and fall through to the next
  995. * loop that uses the size information to stop
  996. * searching for matching vma's.
  997. */
  998. retval = 0;
  999. vma = next;
  1000. break;
  1001. }
  1002. vma = next;
  1003. }
  1004. /*
  1005. * We need look no further than the maximum address a fragment
  1006. * could possibly have landed at. Also cast things to loff_t to
  1007. * prevent overflows and make comparisons vs. equal-width types.
  1008. */
  1009. size = PAGE_ALIGN(size);
  1010. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  1011. next = vma->vm_next;
  1012. /* finding a matching vma now does not alter retval */
  1013. if ((vma->vm_ops == &shm_vm_ops) &&
  1014. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  1015. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1016. vma = next;
  1017. }
  1018. #else /* CONFIG_MMU */
  1019. /* under NOMMU conditions, the exact address to be destroyed must be
  1020. * given */
  1021. retval = -EINVAL;
  1022. if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  1023. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1024. retval = 0;
  1025. }
  1026. #endif
  1027. up_write(&mm->mmap_sem);
  1028. return retval;
  1029. }
  1030. #ifdef CONFIG_PROC_FS
  1031. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  1032. {
  1033. struct shmid_kernel *shp = it;
  1034. unsigned long rss = 0, swp = 0;
  1035. shm_add_rss_swap(shp, &rss, &swp);
  1036. #if BITS_PER_LONG <= 32
  1037. #define SIZE_SPEC "%10lu"
  1038. #else
  1039. #define SIZE_SPEC "%21lu"
  1040. #endif
  1041. return seq_printf(s,
  1042. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  1043. "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
  1044. SIZE_SPEC " " SIZE_SPEC "\n",
  1045. shp->shm_perm.key,
  1046. shp->shm_perm.id,
  1047. shp->shm_perm.mode,
  1048. shp->shm_segsz,
  1049. shp->shm_cprid,
  1050. shp->shm_lprid,
  1051. shp->shm_nattch,
  1052. shp->shm_perm.uid,
  1053. shp->shm_perm.gid,
  1054. shp->shm_perm.cuid,
  1055. shp->shm_perm.cgid,
  1056. shp->shm_atim,
  1057. shp->shm_dtim,
  1058. shp->shm_ctim,
  1059. rss * PAGE_SIZE,
  1060. swp * PAGE_SIZE);
  1061. }
  1062. #endif