shm.c 26 KB

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