shm.c 32 KB

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