shm.c 34 KB

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