shm.c 29 KB

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