shm.c 27 KB

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