shm.c 34 KB

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