shm.c 29 KB

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