shm.c 34 KB

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