shm.c 32 KB

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