shm.c 34 KB

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