shm.c 32 KB

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