shm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. * linux/ipc/shm.c
  3. * Copyright (C) 1992, 1993 Krishna Balasubramanian
  4. * Many improvements/fixes by Bruno Haible.
  5. * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
  6. * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
  7. *
  8. * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  9. * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
  10. * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
  11. * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
  12. * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
  13. * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
  14. * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
  15. *
  16. * support for audit of ipc object properties and permission changes
  17. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  18. *
  19. * namespaces support
  20. * OpenVZ, SWsoft Inc.
  21. * Pavel Emelianov <xemul@openvz.org>
  22. *
  23. * Better ipc lock (kern_ipc_perm.lock) handling
  24. * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
  25. */
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <linux/hugetlb.h>
  29. #include <linux/shm.h>
  30. #include <linux/init.h>
  31. #include <linux/file.h>
  32. #include <linux/mman.h>
  33. #include <linux/shmem_fs.h>
  34. #include <linux/security.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/audit.h>
  37. #include <linux/capability.h>
  38. #include <linux/ptrace.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/rwsem.h>
  41. #include <linux/nsproxy.h>
  42. #include <linux/mount.h>
  43. #include <linux/ipc_namespace.h>
  44. #include <linux/uaccess.h>
  45. #include "util.h"
  46. struct shm_file_data {
  47. int id;
  48. struct ipc_namespace *ns;
  49. struct file *file;
  50. const struct vm_operations_struct *vm_ops;
  51. };
  52. #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
  53. static const struct file_operations shm_file_operations;
  54. static const struct vm_operations_struct shm_vm_ops;
  55. #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
  56. #define shm_unlock(shp) \
  57. ipc_unlock(&(shp)->shm_perm)
  58. static int newseg(struct ipc_namespace *, struct ipc_params *);
  59. static void shm_open(struct vm_area_struct *vma);
  60. static void shm_close(struct vm_area_struct *vma);
  61. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
  62. #ifdef CONFIG_PROC_FS
  63. static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
  64. #endif
  65. void shm_init_ns(struct ipc_namespace *ns)
  66. {
  67. ns->shm_ctlmax = SHMMAX;
  68. ns->shm_ctlall = SHMALL;
  69. ns->shm_ctlmni = SHMMNI;
  70. ns->shm_rmid_forced = 0;
  71. ns->shm_tot = 0;
  72. ipc_init_ids(&shm_ids(ns));
  73. }
  74. /*
  75. * Called with shm_ids.rwsem (writer) and the shp structure locked.
  76. * Only shm_ids.rwsem remains locked on exit.
  77. */
  78. static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  79. {
  80. struct shmid_kernel *shp;
  81. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  82. if (shp->shm_nattch) {
  83. shp->shm_perm.mode |= SHM_DEST;
  84. /* Do not find it any more */
  85. shp->shm_perm.key = IPC_PRIVATE;
  86. shm_unlock(shp);
  87. } else
  88. shm_destroy(ns, shp);
  89. }
  90. #ifdef CONFIG_IPC_NS
  91. void shm_exit_ns(struct ipc_namespace *ns)
  92. {
  93. free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
  94. idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
  95. }
  96. #endif
  97. static int __init ipc_ns_init(void)
  98. {
  99. shm_init_ns(&init_ipc_ns);
  100. return 0;
  101. }
  102. pure_initcall(ipc_ns_init);
  103. void __init shm_init(void)
  104. {
  105. ipc_init_proc_interface("sysvipc/shm",
  106. #if BITS_PER_LONG <= 32
  107. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  108. #else
  109. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  110. #endif
  111. IPC_SHM_IDS, sysvipc_shm_proc_show);
  112. }
  113. static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
  114. {
  115. struct kern_ipc_perm *ipcp = ipc_obtain_object(&shm_ids(ns), id);
  116. if (IS_ERR(ipcp))
  117. return ERR_CAST(ipcp);
  118. return container_of(ipcp, struct shmid_kernel, shm_perm);
  119. }
  120. static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
  121. {
  122. struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
  123. if (IS_ERR(ipcp))
  124. return ERR_CAST(ipcp);
  125. return container_of(ipcp, struct shmid_kernel, shm_perm);
  126. }
  127. /*
  128. * shm_lock_(check_) routines are called in the paths where the rwsem
  129. * is not necessarily held.
  130. */
  131. static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
  132. {
  133. struct kern_ipc_perm *ipcp = ipc_lock(&shm_ids(ns), id);
  134. if (IS_ERR(ipcp))
  135. return (struct shmid_kernel *)ipcp;
  136. return container_of(ipcp, struct shmid_kernel, shm_perm);
  137. }
  138. static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
  139. {
  140. rcu_read_lock();
  141. ipc_lock_object(&ipcp->shm_perm);
  142. }
  143. static void shm_rcu_free(struct rcu_head *head)
  144. {
  145. struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
  146. struct shmid_kernel *shp = ipc_rcu_to_struct(p);
  147. security_shm_free(shp);
  148. ipc_rcu_free(head);
  149. }
  150. static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
  151. {
  152. ipc_rmid(&shm_ids(ns), &s->shm_perm);
  153. }
  154. /* This is called by fork, once for every shm attach. */
  155. static void shm_open(struct vm_area_struct *vma)
  156. {
  157. struct file *file = vma->vm_file;
  158. struct shm_file_data *sfd = shm_file_data(file);
  159. struct shmid_kernel *shp;
  160. shp = shm_lock(sfd->ns, sfd->id);
  161. BUG_ON(IS_ERR(shp));
  162. shp->shm_atim = get_seconds();
  163. shp->shm_lprid = task_tgid_vnr(current);
  164. shp->shm_nattch++;
  165. shm_unlock(shp);
  166. }
  167. /*
  168. * shm_destroy - free the struct shmid_kernel
  169. *
  170. * @ns: namespace
  171. * @shp: struct to free
  172. *
  173. * It has to be called with shp and shm_ids.rwsem (writer) locked,
  174. * but returns with shp unlocked and freed.
  175. */
  176. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  177. {
  178. struct file *shm_file;
  179. shm_file = shp->shm_file;
  180. shp->shm_file = NULL;
  181. ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  182. shm_rmid(ns, shp);
  183. shm_unlock(shp);
  184. if (!is_file_hugepages(shm_file))
  185. shmem_lock(shm_file, 0, shp->mlock_user);
  186. else if (shp->mlock_user)
  187. user_shm_unlock(file_inode(shm_file)->i_size, shp->mlock_user);
  188. fput(shm_file);
  189. ipc_rcu_putref(shp, shm_rcu_free);
  190. }
  191. /*
  192. * shm_may_destroy - identifies whether shm segment should be destroyed now
  193. *
  194. * Returns true if and only if there are no active users of the segment and
  195. * one of the following is true:
  196. *
  197. * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
  198. *
  199. * 2) sysctl kernel.shm_rmid_forced is set to 1.
  200. */
  201. static bool shm_may_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  202. {
  203. return (shp->shm_nattch == 0) &&
  204. (ns->shm_rmid_forced ||
  205. (shp->shm_perm.mode & SHM_DEST));
  206. }
  207. /*
  208. * remove the attach descriptor vma.
  209. * free memory for segment if it is marked destroyed.
  210. * The descriptor has already been removed from the current->mm->mmap list
  211. * and will later be kfree()d.
  212. */
  213. static void shm_close(struct vm_area_struct *vma)
  214. {
  215. struct file *file = vma->vm_file;
  216. struct shm_file_data *sfd = shm_file_data(file);
  217. struct shmid_kernel *shp;
  218. struct ipc_namespace *ns = sfd->ns;
  219. down_write(&shm_ids(ns).rwsem);
  220. /* remove from the list of attaches of the shm segment */
  221. shp = shm_lock(ns, sfd->id);
  222. BUG_ON(IS_ERR(shp));
  223. shp->shm_lprid = task_tgid_vnr(current);
  224. shp->shm_dtim = get_seconds();
  225. shp->shm_nattch--;
  226. if (shm_may_destroy(ns, shp))
  227. shm_destroy(ns, shp);
  228. else
  229. shm_unlock(shp);
  230. up_write(&shm_ids(ns).rwsem);
  231. }
  232. /* Called with ns->shm_ids(ns).rwsem locked */
  233. static int shm_try_destroy_current(int id, void *p, void *data)
  234. {
  235. struct ipc_namespace *ns = data;
  236. struct kern_ipc_perm *ipcp = p;
  237. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  238. if (shp->shm_creator != current)
  239. return 0;
  240. /*
  241. * Mark it as orphaned to destroy the segment when
  242. * kernel.shm_rmid_forced is changed.
  243. * It is noop if the following shm_may_destroy() returns true.
  244. */
  245. shp->shm_creator = NULL;
  246. /*
  247. * Don't even try to destroy it. If shm_rmid_forced=0 and IPC_RMID
  248. * is not set, it shouldn't be deleted here.
  249. */
  250. if (!ns->shm_rmid_forced)
  251. return 0;
  252. if (shm_may_destroy(ns, shp)) {
  253. shm_lock_by_ptr(shp);
  254. shm_destroy(ns, shp);
  255. }
  256. return 0;
  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. void exit_shm(struct task_struct *task)
  286. {
  287. struct ipc_namespace *ns = task->nsproxy->ipc_ns;
  288. if (shm_ids(ns).in_use == 0)
  289. return;
  290. /* Destroy all already created segments, but not mapped yet */
  291. down_write(&shm_ids(ns).rwsem);
  292. if (shm_ids(ns).in_use)
  293. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_current, ns);
  294. up_write(&shm_ids(ns).rwsem);
  295. }
  296. static int shm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  297. {
  298. struct file *file = vma->vm_file;
  299. struct shm_file_data *sfd = shm_file_data(file);
  300. return sfd->vm_ops->fault(vma, vmf);
  301. }
  302. #ifdef CONFIG_NUMA
  303. static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
  304. {
  305. struct file *file = vma->vm_file;
  306. struct shm_file_data *sfd = shm_file_data(file);
  307. int err = 0;
  308. if (sfd->vm_ops->set_policy)
  309. err = sfd->vm_ops->set_policy(vma, new);
  310. return err;
  311. }
  312. static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
  313. unsigned long addr)
  314. {
  315. struct file *file = vma->vm_file;
  316. struct shm_file_data *sfd = shm_file_data(file);
  317. struct mempolicy *pol = NULL;
  318. if (sfd->vm_ops->get_policy)
  319. pol = sfd->vm_ops->get_policy(vma, addr);
  320. else if (vma->vm_policy)
  321. pol = vma->vm_policy;
  322. return pol;
  323. }
  324. #endif
  325. static int shm_mmap(struct file *file, struct vm_area_struct *vma)
  326. {
  327. struct shm_file_data *sfd = shm_file_data(file);
  328. int ret;
  329. ret = sfd->file->f_op->mmap(sfd->file, vma);
  330. if (ret != 0)
  331. return ret;
  332. sfd->vm_ops = vma->vm_ops;
  333. #ifdef CONFIG_MMU
  334. BUG_ON(!sfd->vm_ops->fault);
  335. #endif
  336. vma->vm_ops = &shm_vm_ops;
  337. shm_open(vma);
  338. return ret;
  339. }
  340. static int shm_release(struct inode *ino, struct file *file)
  341. {
  342. struct shm_file_data *sfd = shm_file_data(file);
  343. put_ipc_ns(sfd->ns);
  344. shm_file_data(file) = NULL;
  345. kfree(sfd);
  346. return 0;
  347. }
  348. static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  349. {
  350. struct shm_file_data *sfd = shm_file_data(file);
  351. if (!sfd->file->f_op->fsync)
  352. return -EINVAL;
  353. return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
  354. }
  355. static long shm_fallocate(struct file *file, int mode, loff_t offset,
  356. loff_t len)
  357. {
  358. struct shm_file_data *sfd = shm_file_data(file);
  359. if (!sfd->file->f_op->fallocate)
  360. return -EOPNOTSUPP;
  361. return sfd->file->f_op->fallocate(file, mode, offset, len);
  362. }
  363. static unsigned long shm_get_unmapped_area(struct file *file,
  364. unsigned long addr, unsigned long len, unsigned long pgoff,
  365. unsigned long flags)
  366. {
  367. struct shm_file_data *sfd = shm_file_data(file);
  368. return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
  369. pgoff, flags);
  370. }
  371. static const struct file_operations shm_file_operations = {
  372. .mmap = shm_mmap,
  373. .fsync = shm_fsync,
  374. .release = shm_release,
  375. #ifndef CONFIG_MMU
  376. .get_unmapped_area = shm_get_unmapped_area,
  377. #endif
  378. .llseek = noop_llseek,
  379. .fallocate = shm_fallocate,
  380. };
  381. static const struct file_operations shm_file_operations_huge = {
  382. .mmap = shm_mmap,
  383. .fsync = shm_fsync,
  384. .release = shm_release,
  385. .get_unmapped_area = shm_get_unmapped_area,
  386. .llseek = noop_llseek,
  387. .fallocate = shm_fallocate,
  388. };
  389. int is_file_shm_hugepages(struct file *file)
  390. {
  391. return file->f_op == &shm_file_operations_huge;
  392. }
  393. static const struct vm_operations_struct shm_vm_ops = {
  394. .open = shm_open, /* callback for a new vm-area open */
  395. .close = shm_close, /* callback for when the vm-area is released */
  396. .fault = shm_fault,
  397. #if defined(CONFIG_NUMA)
  398. .set_policy = shm_set_policy,
  399. .get_policy = shm_get_policy,
  400. #endif
  401. };
  402. /**
  403. * newseg - Create a new shared memory segment
  404. * @ns: namespace
  405. * @params: ptr to the structure that contains key, size and shmflg
  406. *
  407. * Called with shm_ids.rwsem held as a writer.
  408. */
  409. static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
  410. {
  411. key_t key = params->key;
  412. int shmflg = params->flg;
  413. size_t size = params->u.size;
  414. int error;
  415. struct shmid_kernel *shp;
  416. size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  417. struct file *file;
  418. char name[13];
  419. int id;
  420. vm_flags_t acctflag = 0;
  421. if (size < SHMMIN || size > ns->shm_ctlmax)
  422. return -EINVAL;
  423. if (numpages << PAGE_SHIFT < size)
  424. return -ENOSPC;
  425. if (ns->shm_tot + numpages < ns->shm_tot ||
  426. ns->shm_tot + numpages > ns->shm_ctlall)
  427. return -ENOSPC;
  428. shp = ipc_rcu_alloc(sizeof(*shp));
  429. if (!shp)
  430. return -ENOMEM;
  431. shp->shm_perm.key = key;
  432. shp->shm_perm.mode = (shmflg & S_IRWXUGO);
  433. shp->mlock_user = NULL;
  434. shp->shm_perm.security = NULL;
  435. error = security_shm_alloc(shp);
  436. if (error) {
  437. ipc_rcu_putref(shp, ipc_rcu_free);
  438. return error;
  439. }
  440. sprintf(name, "SYSV%08x", key);
  441. if (shmflg & SHM_HUGETLB) {
  442. struct hstate *hs;
  443. size_t hugesize;
  444. hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
  445. if (!hs) {
  446. error = -EINVAL;
  447. goto no_file;
  448. }
  449. hugesize = ALIGN(size, huge_page_size(hs));
  450. /* hugetlb_file_setup applies strict accounting */
  451. if (shmflg & SHM_NORESERVE)
  452. acctflag = VM_NORESERVE;
  453. file = hugetlb_file_setup(name, hugesize, acctflag,
  454. &shp->mlock_user, HUGETLB_SHMFS_INODE,
  455. (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
  456. } else {
  457. /*
  458. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  459. * if it's asked for.
  460. */
  461. if ((shmflg & SHM_NORESERVE) &&
  462. sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  463. acctflag = VM_NORESERVE;
  464. file = shmem_file_setup(name, size, acctflag);
  465. }
  466. error = PTR_ERR(file);
  467. if (IS_ERR(file))
  468. goto no_file;
  469. id = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  470. if (id < 0) {
  471. error = id;
  472. goto no_id;
  473. }
  474. shp->shm_cprid = task_tgid_vnr(current);
  475. shp->shm_lprid = 0;
  476. shp->shm_atim = shp->shm_dtim = 0;
  477. shp->shm_ctim = get_seconds();
  478. shp->shm_segsz = size;
  479. shp->shm_nattch = 0;
  480. shp->shm_file = file;
  481. shp->shm_creator = current;
  482. /*
  483. * shmid gets reported as "inode#" in /proc/pid/maps.
  484. * proc-ps tools use this. Changing this will break them.
  485. */
  486. file_inode(file)->i_ino = shp->shm_perm.id;
  487. ns->shm_tot += numpages;
  488. error = shp->shm_perm.id;
  489. ipc_unlock_object(&shp->shm_perm);
  490. rcu_read_unlock();
  491. return error;
  492. no_id:
  493. if (is_file_hugepages(file) && shp->mlock_user)
  494. user_shm_unlock(size, shp->mlock_user);
  495. fput(file);
  496. no_file:
  497. ipc_rcu_putref(shp, shm_rcu_free);
  498. return error;
  499. }
  500. /*
  501. * Called with shm_ids.rwsem and ipcp locked.
  502. */
  503. static inline int shm_security(struct kern_ipc_perm *ipcp, int shmflg)
  504. {
  505. struct shmid_kernel *shp;
  506. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  507. return security_shm_associate(shp, shmflg);
  508. }
  509. /*
  510. * Called with shm_ids.rwsem and ipcp locked.
  511. */
  512. static inline int shm_more_checks(struct kern_ipc_perm *ipcp,
  513. struct ipc_params *params)
  514. {
  515. struct shmid_kernel *shp;
  516. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  517. if (shp->shm_segsz < params->u.size)
  518. return -EINVAL;
  519. return 0;
  520. }
  521. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  522. {
  523. struct ipc_namespace *ns;
  524. static const struct ipc_ops shm_ops = {
  525. .getnew = newseg,
  526. .associate = shm_security,
  527. .more_checks = shm_more_checks,
  528. };
  529. struct ipc_params shm_params;
  530. ns = current->nsproxy->ipc_ns;
  531. shm_params.key = key;
  532. shm_params.flg = shmflg;
  533. shm_params.u.size = size;
  534. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  535. }
  536. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  537. {
  538. switch (version) {
  539. case IPC_64:
  540. return copy_to_user(buf, in, sizeof(*in));
  541. case IPC_OLD:
  542. {
  543. struct shmid_ds out;
  544. memset(&out, 0, sizeof(out));
  545. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  546. out.shm_segsz = in->shm_segsz;
  547. out.shm_atime = in->shm_atime;
  548. out.shm_dtime = in->shm_dtime;
  549. out.shm_ctime = in->shm_ctime;
  550. out.shm_cpid = in->shm_cpid;
  551. out.shm_lpid = in->shm_lpid;
  552. out.shm_nattch = in->shm_nattch;
  553. return copy_to_user(buf, &out, sizeof(out));
  554. }
  555. default:
  556. return -EINVAL;
  557. }
  558. }
  559. static inline unsigned long
  560. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  561. {
  562. switch (version) {
  563. case IPC_64:
  564. if (copy_from_user(out, buf, sizeof(*out)))
  565. return -EFAULT;
  566. return 0;
  567. case IPC_OLD:
  568. {
  569. struct shmid_ds tbuf_old;
  570. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  571. return -EFAULT;
  572. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  573. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  574. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  575. return 0;
  576. }
  577. default:
  578. return -EINVAL;
  579. }
  580. }
  581. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  582. {
  583. switch (version) {
  584. case IPC_64:
  585. return copy_to_user(buf, in, sizeof(*in));
  586. case IPC_OLD:
  587. {
  588. struct shminfo out;
  589. if (in->shmmax > INT_MAX)
  590. out.shmmax = INT_MAX;
  591. else
  592. out.shmmax = (int)in->shmmax;
  593. out.shmmin = in->shmmin;
  594. out.shmmni = in->shmmni;
  595. out.shmseg = in->shmseg;
  596. out.shmall = in->shmall;
  597. return copy_to_user(buf, &out, sizeof(out));
  598. }
  599. default:
  600. return -EINVAL;
  601. }
  602. }
  603. /*
  604. * Calculate and add used RSS and swap pages of a shm.
  605. * Called with shm_ids.rwsem held as a reader
  606. */
  607. static void shm_add_rss_swap(struct shmid_kernel *shp,
  608. unsigned long *rss_add, unsigned long *swp_add)
  609. {
  610. struct inode *inode;
  611. inode = file_inode(shp->shm_file);
  612. if (is_file_hugepages(shp->shm_file)) {
  613. struct address_space *mapping = inode->i_mapping;
  614. struct hstate *h = hstate_file(shp->shm_file);
  615. *rss_add += pages_per_huge_page(h) * mapping->nrpages;
  616. } else {
  617. #ifdef CONFIG_SHMEM
  618. struct shmem_inode_info *info = SHMEM_I(inode);
  619. spin_lock(&info->lock);
  620. *rss_add += inode->i_mapping->nrpages;
  621. *swp_add += info->swapped;
  622. spin_unlock(&info->lock);
  623. #else
  624. *rss_add += inode->i_mapping->nrpages;
  625. #endif
  626. }
  627. }
  628. /*
  629. * Called with shm_ids.rwsem held as a reader
  630. */
  631. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  632. unsigned long *swp)
  633. {
  634. int next_id;
  635. int total, in_use;
  636. *rss = 0;
  637. *swp = 0;
  638. in_use = shm_ids(ns).in_use;
  639. for (total = 0, next_id = 0; total < in_use; next_id++) {
  640. struct kern_ipc_perm *ipc;
  641. struct shmid_kernel *shp;
  642. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  643. if (ipc == NULL)
  644. continue;
  645. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  646. shm_add_rss_swap(shp, rss, swp);
  647. total++;
  648. }
  649. }
  650. /*
  651. * This function handles some shmctl commands which require the rwsem
  652. * to be held in write mode.
  653. * NOTE: no locks must be held, the rwsem is taken inside this function.
  654. */
  655. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  656. struct shmid_ds __user *buf, int version)
  657. {
  658. struct kern_ipc_perm *ipcp;
  659. struct shmid64_ds shmid64;
  660. struct shmid_kernel *shp;
  661. int err;
  662. if (cmd == IPC_SET) {
  663. if (copy_shmid_from_user(&shmid64, buf, version))
  664. return -EFAULT;
  665. }
  666. down_write(&shm_ids(ns).rwsem);
  667. rcu_read_lock();
  668. ipcp = ipcctl_pre_down_nolock(ns, &shm_ids(ns), shmid, cmd,
  669. &shmid64.shm_perm, 0);
  670. if (IS_ERR(ipcp)) {
  671. err = PTR_ERR(ipcp);
  672. goto out_unlock1;
  673. }
  674. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  675. err = security_shm_shmctl(shp, cmd);
  676. if (err)
  677. goto out_unlock1;
  678. switch (cmd) {
  679. case IPC_RMID:
  680. ipc_lock_object(&shp->shm_perm);
  681. /* do_shm_rmid unlocks the ipc object and rcu */
  682. do_shm_rmid(ns, ipcp);
  683. goto out_up;
  684. case IPC_SET:
  685. ipc_lock_object(&shp->shm_perm);
  686. err = ipc_update_perm(&shmid64.shm_perm, ipcp);
  687. if (err)
  688. goto out_unlock0;
  689. shp->shm_ctim = get_seconds();
  690. break;
  691. default:
  692. err = -EINVAL;
  693. goto out_unlock1;
  694. }
  695. out_unlock0:
  696. ipc_unlock_object(&shp->shm_perm);
  697. out_unlock1:
  698. rcu_read_unlock();
  699. out_up:
  700. up_write(&shm_ids(ns).rwsem);
  701. return err;
  702. }
  703. static int shmctl_nolock(struct ipc_namespace *ns, int shmid,
  704. int cmd, int version, void __user *buf)
  705. {
  706. int err;
  707. struct shmid_kernel *shp;
  708. /* preliminary security checks for *_INFO */
  709. if (cmd == IPC_INFO || cmd == SHM_INFO) {
  710. err = security_shm_shmctl(NULL, cmd);
  711. if (err)
  712. return err;
  713. }
  714. switch (cmd) {
  715. case IPC_INFO:
  716. {
  717. struct shminfo64 shminfo;
  718. memset(&shminfo, 0, sizeof(shminfo));
  719. shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
  720. shminfo.shmmax = ns->shm_ctlmax;
  721. shminfo.shmall = ns->shm_ctlall;
  722. shminfo.shmmin = SHMMIN;
  723. if (copy_shminfo_to_user(buf, &shminfo, version))
  724. return -EFAULT;
  725. down_read(&shm_ids(ns).rwsem);
  726. err = ipc_get_maxid(&shm_ids(ns));
  727. up_read(&shm_ids(ns).rwsem);
  728. if (err < 0)
  729. err = 0;
  730. goto out;
  731. }
  732. case SHM_INFO:
  733. {
  734. struct shm_info shm_info;
  735. memset(&shm_info, 0, sizeof(shm_info));
  736. down_read(&shm_ids(ns).rwsem);
  737. shm_info.used_ids = shm_ids(ns).in_use;
  738. shm_get_stat(ns, &shm_info.shm_rss, &shm_info.shm_swp);
  739. shm_info.shm_tot = ns->shm_tot;
  740. shm_info.swap_attempts = 0;
  741. shm_info.swap_successes = 0;
  742. err = ipc_get_maxid(&shm_ids(ns));
  743. up_read(&shm_ids(ns).rwsem);
  744. if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
  745. err = -EFAULT;
  746. goto out;
  747. }
  748. err = err < 0 ? 0 : err;
  749. goto out;
  750. }
  751. case SHM_STAT:
  752. case IPC_STAT:
  753. {
  754. struct shmid64_ds tbuf;
  755. int result;
  756. rcu_read_lock();
  757. if (cmd == SHM_STAT) {
  758. shp = shm_obtain_object(ns, shmid);
  759. if (IS_ERR(shp)) {
  760. err = PTR_ERR(shp);
  761. goto out_unlock;
  762. }
  763. result = shp->shm_perm.id;
  764. } else {
  765. shp = shm_obtain_object_check(ns, shmid);
  766. if (IS_ERR(shp)) {
  767. err = PTR_ERR(shp);
  768. goto out_unlock;
  769. }
  770. result = 0;
  771. }
  772. err = -EACCES;
  773. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  774. goto out_unlock;
  775. err = security_shm_shmctl(shp, cmd);
  776. if (err)
  777. goto out_unlock;
  778. memset(&tbuf, 0, sizeof(tbuf));
  779. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  780. tbuf.shm_segsz = shp->shm_segsz;
  781. tbuf.shm_atime = shp->shm_atim;
  782. tbuf.shm_dtime = shp->shm_dtim;
  783. tbuf.shm_ctime = shp->shm_ctim;
  784. tbuf.shm_cpid = shp->shm_cprid;
  785. tbuf.shm_lpid = shp->shm_lprid;
  786. tbuf.shm_nattch = shp->shm_nattch;
  787. rcu_read_unlock();
  788. if (copy_shmid_to_user(buf, &tbuf, version))
  789. err = -EFAULT;
  790. else
  791. err = result;
  792. goto out;
  793. }
  794. default:
  795. return -EINVAL;
  796. }
  797. out_unlock:
  798. rcu_read_unlock();
  799. out:
  800. return err;
  801. }
  802. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  803. {
  804. struct shmid_kernel *shp;
  805. int err, version;
  806. struct ipc_namespace *ns;
  807. if (cmd < 0 || shmid < 0)
  808. return -EINVAL;
  809. version = ipc_parse_version(&cmd);
  810. ns = current->nsproxy->ipc_ns;
  811. switch (cmd) {
  812. case IPC_INFO:
  813. case SHM_INFO:
  814. case SHM_STAT:
  815. case IPC_STAT:
  816. return shmctl_nolock(ns, shmid, cmd, version, buf);
  817. case IPC_RMID:
  818. case IPC_SET:
  819. return shmctl_down(ns, shmid, cmd, buf, version);
  820. case SHM_LOCK:
  821. case SHM_UNLOCK:
  822. {
  823. struct file *shm_file;
  824. rcu_read_lock();
  825. shp = shm_obtain_object_check(ns, shmid);
  826. if (IS_ERR(shp)) {
  827. err = PTR_ERR(shp);
  828. goto out_unlock1;
  829. }
  830. audit_ipc_obj(&(shp->shm_perm));
  831. err = security_shm_shmctl(shp, cmd);
  832. if (err)
  833. goto out_unlock1;
  834. ipc_lock_object(&shp->shm_perm);
  835. /* check if shm_destroy() is tearing down shp */
  836. if (!ipc_valid_object(&shp->shm_perm)) {
  837. err = -EIDRM;
  838. goto out_unlock0;
  839. }
  840. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  841. kuid_t euid = current_euid();
  842. if (!uid_eq(euid, shp->shm_perm.uid) &&
  843. !uid_eq(euid, shp->shm_perm.cuid)) {
  844. err = -EPERM;
  845. goto out_unlock0;
  846. }
  847. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
  848. err = -EPERM;
  849. goto out_unlock0;
  850. }
  851. }
  852. shm_file = shp->shm_file;
  853. if (is_file_hugepages(shm_file))
  854. goto out_unlock0;
  855. if (cmd == SHM_LOCK) {
  856. struct user_struct *user = current_user();
  857. err = shmem_lock(shm_file, 1, user);
  858. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
  859. shp->shm_perm.mode |= SHM_LOCKED;
  860. shp->mlock_user = user;
  861. }
  862. goto out_unlock0;
  863. }
  864. /* SHM_UNLOCK */
  865. if (!(shp->shm_perm.mode & SHM_LOCKED))
  866. goto out_unlock0;
  867. shmem_lock(shm_file, 0, shp->mlock_user);
  868. shp->shm_perm.mode &= ~SHM_LOCKED;
  869. shp->mlock_user = NULL;
  870. get_file(shm_file);
  871. ipc_unlock_object(&shp->shm_perm);
  872. rcu_read_unlock();
  873. shmem_unlock_mapping(shm_file->f_mapping);
  874. fput(shm_file);
  875. return err;
  876. }
  877. default:
  878. return -EINVAL;
  879. }
  880. out_unlock0:
  881. ipc_unlock_object(&shp->shm_perm);
  882. out_unlock1:
  883. rcu_read_unlock();
  884. return err;
  885. }
  886. /*
  887. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  888. *
  889. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  890. * "raddr" thing points to kernel space, and there has to be a wrapper around
  891. * this.
  892. */
  893. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr,
  894. unsigned long shmlba)
  895. {
  896. struct shmid_kernel *shp;
  897. unsigned long addr;
  898. unsigned long size;
  899. struct file *file;
  900. int err;
  901. unsigned long flags;
  902. unsigned long prot;
  903. int acc_mode;
  904. struct ipc_namespace *ns;
  905. struct shm_file_data *sfd;
  906. struct path path;
  907. fmode_t f_mode;
  908. unsigned long populate = 0;
  909. err = -EINVAL;
  910. if (shmid < 0)
  911. goto out;
  912. else if ((addr = (ulong)shmaddr)) {
  913. if (addr & (shmlba - 1)) {
  914. if (shmflg & SHM_RND)
  915. addr &= ~(shmlba - 1); /* round down */
  916. else
  917. #ifndef __ARCH_FORCE_SHMLBA
  918. if (addr & ~PAGE_MASK)
  919. #endif
  920. goto out;
  921. }
  922. flags = MAP_SHARED | MAP_FIXED;
  923. } else {
  924. if ((shmflg & SHM_REMAP))
  925. goto out;
  926. flags = MAP_SHARED;
  927. }
  928. if (shmflg & SHM_RDONLY) {
  929. prot = PROT_READ;
  930. acc_mode = S_IRUGO;
  931. f_mode = FMODE_READ;
  932. } else {
  933. prot = PROT_READ | PROT_WRITE;
  934. acc_mode = S_IRUGO | S_IWUGO;
  935. f_mode = FMODE_READ | FMODE_WRITE;
  936. }
  937. if (shmflg & SHM_EXEC) {
  938. prot |= PROT_EXEC;
  939. acc_mode |= S_IXUGO;
  940. }
  941. /*
  942. * We cannot rely on the fs check since SYSV IPC does have an
  943. * additional creator id...
  944. */
  945. ns = current->nsproxy->ipc_ns;
  946. rcu_read_lock();
  947. shp = shm_obtain_object_check(ns, shmid);
  948. if (IS_ERR(shp)) {
  949. err = PTR_ERR(shp);
  950. goto out_unlock;
  951. }
  952. err = -EACCES;
  953. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  954. goto out_unlock;
  955. err = security_shm_shmat(shp, shmaddr, shmflg);
  956. if (err)
  957. goto out_unlock;
  958. ipc_lock_object(&shp->shm_perm);
  959. /* check if shm_destroy() is tearing down shp */
  960. if (!ipc_valid_object(&shp->shm_perm)) {
  961. ipc_unlock_object(&shp->shm_perm);
  962. err = -EIDRM;
  963. goto out_unlock;
  964. }
  965. path = shp->shm_file->f_path;
  966. path_get(&path);
  967. shp->shm_nattch++;
  968. size = i_size_read(path.dentry->d_inode);
  969. ipc_unlock_object(&shp->shm_perm);
  970. rcu_read_unlock();
  971. err = -ENOMEM;
  972. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  973. if (!sfd) {
  974. path_put(&path);
  975. goto out_nattch;
  976. }
  977. file = alloc_file(&path, f_mode,
  978. is_file_hugepages(shp->shm_file) ?
  979. &shm_file_operations_huge :
  980. &shm_file_operations);
  981. err = PTR_ERR(file);
  982. if (IS_ERR(file)) {
  983. kfree(sfd);
  984. path_put(&path);
  985. goto out_nattch;
  986. }
  987. file->private_data = sfd;
  988. file->f_mapping = shp->shm_file->f_mapping;
  989. sfd->id = shp->shm_perm.id;
  990. sfd->ns = get_ipc_ns(ns);
  991. sfd->file = shp->shm_file;
  992. sfd->vm_ops = NULL;
  993. err = security_mmap_file(file, prot, flags);
  994. if (err)
  995. goto out_fput;
  996. down_write(&current->mm->mmap_sem);
  997. if (addr && !(shmflg & SHM_REMAP)) {
  998. err = -EINVAL;
  999. if (addr + size < addr)
  1000. goto invalid;
  1001. if (find_vma_intersection(current->mm, addr, addr + size))
  1002. goto invalid;
  1003. /*
  1004. * If shm segment goes below stack, make sure there is some
  1005. * space left for the stack to grow (at least 4 pages).
  1006. */
  1007. if (addr < current->mm->start_stack &&
  1008. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  1009. goto invalid;
  1010. }
  1011. addr = do_mmap_pgoff(file, addr, size, prot, flags, 0, &populate);
  1012. *raddr = addr;
  1013. err = 0;
  1014. if (IS_ERR_VALUE(addr))
  1015. err = (long)addr;
  1016. invalid:
  1017. up_write(&current->mm->mmap_sem);
  1018. if (populate)
  1019. mm_populate(addr, populate);
  1020. out_fput:
  1021. fput(file);
  1022. out_nattch:
  1023. down_write(&shm_ids(ns).rwsem);
  1024. shp = shm_lock(ns, shmid);
  1025. BUG_ON(IS_ERR(shp));
  1026. shp->shm_nattch--;
  1027. if (shm_may_destroy(ns, shp))
  1028. shm_destroy(ns, shp);
  1029. else
  1030. shm_unlock(shp);
  1031. up_write(&shm_ids(ns).rwsem);
  1032. return err;
  1033. out_unlock:
  1034. rcu_read_unlock();
  1035. out:
  1036. return err;
  1037. }
  1038. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  1039. {
  1040. unsigned long ret;
  1041. long err;
  1042. err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
  1043. if (err)
  1044. return err;
  1045. force_successful_syscall_return();
  1046. return (long)ret;
  1047. }
  1048. /*
  1049. * detach and kill segment if marked destroyed.
  1050. * The work is done in shm_close.
  1051. */
  1052. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  1053. {
  1054. struct mm_struct *mm = current->mm;
  1055. struct vm_area_struct *vma;
  1056. unsigned long addr = (unsigned long)shmaddr;
  1057. int retval = -EINVAL;
  1058. #ifdef CONFIG_MMU
  1059. loff_t size = 0;
  1060. struct vm_area_struct *next;
  1061. #endif
  1062. if (addr & ~PAGE_MASK)
  1063. return retval;
  1064. down_write(&mm->mmap_sem);
  1065. /*
  1066. * This function tries to be smart and unmap shm segments that
  1067. * were modified by partial mlock or munmap calls:
  1068. * - It first determines the size of the shm segment that should be
  1069. * unmapped: It searches for a vma that is backed by shm and that
  1070. * started at address shmaddr. It records it's size and then unmaps
  1071. * it.
  1072. * - Then it unmaps all shm vmas that started at shmaddr and that
  1073. * are within the initially determined size.
  1074. * Errors from do_munmap are ignored: the function only fails if
  1075. * it's called with invalid parameters or if it's called to unmap
  1076. * a part of a vma. Both calls in this function are for full vmas,
  1077. * the parameters are directly copied from the vma itself and always
  1078. * valid - therefore do_munmap cannot fail. (famous last words?)
  1079. */
  1080. /*
  1081. * If it had been mremap()'d, the starting address would not
  1082. * match the usual checks anyway. So assume all vma's are
  1083. * above the starting address given.
  1084. */
  1085. vma = find_vma(mm, addr);
  1086. #ifdef CONFIG_MMU
  1087. while (vma) {
  1088. next = vma->vm_next;
  1089. /*
  1090. * Check if the starting address would match, i.e. it's
  1091. * a fragment created by mprotect() and/or munmap(), or it
  1092. * otherwise it starts at this address with no hassles.
  1093. */
  1094. if ((vma->vm_ops == &shm_vm_ops) &&
  1095. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  1096. size = file_inode(vma->vm_file)->i_size;
  1097. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1098. /*
  1099. * We discovered the size of the shm segment, so
  1100. * break out of here and fall through to the next
  1101. * loop that uses the size information to stop
  1102. * searching for matching vma's.
  1103. */
  1104. retval = 0;
  1105. vma = next;
  1106. break;
  1107. }
  1108. vma = next;
  1109. }
  1110. /*
  1111. * We need look no further than the maximum address a fragment
  1112. * could possibly have landed at. Also cast things to loff_t to
  1113. * prevent overflows and make comparisons vs. equal-width types.
  1114. */
  1115. size = PAGE_ALIGN(size);
  1116. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  1117. next = vma->vm_next;
  1118. /* finding a matching vma now does not alter retval */
  1119. if ((vma->vm_ops == &shm_vm_ops) &&
  1120. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  1121. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1122. vma = next;
  1123. }
  1124. #else /* CONFIG_MMU */
  1125. /* under NOMMU conditions, the exact address to be destroyed must be
  1126. * given */
  1127. if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  1128. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  1129. retval = 0;
  1130. }
  1131. #endif
  1132. up_write(&mm->mmap_sem);
  1133. return retval;
  1134. }
  1135. #ifdef CONFIG_PROC_FS
  1136. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  1137. {
  1138. struct user_namespace *user_ns = seq_user_ns(s);
  1139. struct shmid_kernel *shp = it;
  1140. unsigned long rss = 0, swp = 0;
  1141. shm_add_rss_swap(shp, &rss, &swp);
  1142. #if BITS_PER_LONG <= 32
  1143. #define SIZE_SPEC "%10lu"
  1144. #else
  1145. #define SIZE_SPEC "%21lu"
  1146. #endif
  1147. return seq_printf(s,
  1148. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  1149. "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
  1150. SIZE_SPEC " " SIZE_SPEC "\n",
  1151. shp->shm_perm.key,
  1152. shp->shm_perm.id,
  1153. shp->shm_perm.mode,
  1154. shp->shm_segsz,
  1155. shp->shm_cprid,
  1156. shp->shm_lprid,
  1157. shp->shm_nattch,
  1158. from_kuid_munged(user_ns, shp->shm_perm.uid),
  1159. from_kgid_munged(user_ns, shp->shm_perm.gid),
  1160. from_kuid_munged(user_ns, shp->shm_perm.cuid),
  1161. from_kgid_munged(user_ns, shp->shm_perm.cgid),
  1162. shp->shm_atim,
  1163. shp->shm_dtim,
  1164. shp->shm_ctim,
  1165. rss * PAGE_SIZE,
  1166. swp * PAGE_SIZE);
  1167. }
  1168. #endif