shm.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. vm_flags_t 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(ns, &shm_ids(ns), shmid, cmd,
  536. &shmid64.shm_perm, 0);
  537. if (IS_ERR(ipcp))
  538. return PTR_ERR(ipcp);
  539. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  540. err = security_shm_shmctl(shp, cmd);
  541. if (err)
  542. goto out_unlock;
  543. switch (cmd) {
  544. case IPC_RMID:
  545. do_shm_rmid(ns, ipcp);
  546. goto out_up;
  547. case IPC_SET:
  548. ipc_update_perm(&shmid64.shm_perm, ipcp);
  549. shp->shm_ctim = get_seconds();
  550. break;
  551. default:
  552. err = -EINVAL;
  553. }
  554. out_unlock:
  555. shm_unlock(shp);
  556. out_up:
  557. up_write(&shm_ids(ns).rw_mutex);
  558. return err;
  559. }
  560. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  561. {
  562. struct shmid_kernel *shp;
  563. int err, version;
  564. struct ipc_namespace *ns;
  565. if (cmd < 0 || shmid < 0) {
  566. err = -EINVAL;
  567. goto out;
  568. }
  569. version = ipc_parse_version(&cmd);
  570. ns = current->nsproxy->ipc_ns;
  571. switch (cmd) { /* replace with proc interface ? */
  572. case IPC_INFO:
  573. {
  574. struct shminfo64 shminfo;
  575. err = security_shm_shmctl(NULL, cmd);
  576. if (err)
  577. return err;
  578. memset(&shminfo, 0, sizeof(shminfo));
  579. shminfo.shmmni = shminfo.shmseg = ns->shm_ctlmni;
  580. shminfo.shmmax = ns->shm_ctlmax;
  581. shminfo.shmall = ns->shm_ctlall;
  582. shminfo.shmmin = SHMMIN;
  583. if(copy_shminfo_to_user (buf, &shminfo, version))
  584. return -EFAULT;
  585. down_read(&shm_ids(ns).rw_mutex);
  586. err = ipc_get_maxid(&shm_ids(ns));
  587. up_read(&shm_ids(ns).rw_mutex);
  588. if(err<0)
  589. err = 0;
  590. goto out;
  591. }
  592. case SHM_INFO:
  593. {
  594. struct shm_info shm_info;
  595. err = security_shm_shmctl(NULL, cmd);
  596. if (err)
  597. return err;
  598. memset(&shm_info, 0, sizeof(shm_info));
  599. down_read(&shm_ids(ns).rw_mutex);
  600. shm_info.used_ids = shm_ids(ns).in_use;
  601. shm_get_stat (ns, &shm_info.shm_rss, &shm_info.shm_swp);
  602. shm_info.shm_tot = ns->shm_tot;
  603. shm_info.swap_attempts = 0;
  604. shm_info.swap_successes = 0;
  605. err = ipc_get_maxid(&shm_ids(ns));
  606. up_read(&shm_ids(ns).rw_mutex);
  607. if (copy_to_user(buf, &shm_info, sizeof(shm_info))) {
  608. err = -EFAULT;
  609. goto out;
  610. }
  611. err = err < 0 ? 0 : err;
  612. goto out;
  613. }
  614. case SHM_STAT:
  615. case IPC_STAT:
  616. {
  617. struct shmid64_ds tbuf;
  618. int result;
  619. if (cmd == SHM_STAT) {
  620. shp = shm_lock(ns, shmid);
  621. if (IS_ERR(shp)) {
  622. err = PTR_ERR(shp);
  623. goto out;
  624. }
  625. result = shp->shm_perm.id;
  626. } else {
  627. shp = shm_lock_check(ns, shmid);
  628. if (IS_ERR(shp)) {
  629. err = PTR_ERR(shp);
  630. goto out;
  631. }
  632. result = 0;
  633. }
  634. err = -EACCES;
  635. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  636. goto out_unlock;
  637. err = security_shm_shmctl(shp, cmd);
  638. if (err)
  639. goto out_unlock;
  640. memset(&tbuf, 0, sizeof(tbuf));
  641. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  642. tbuf.shm_segsz = shp->shm_segsz;
  643. tbuf.shm_atime = shp->shm_atim;
  644. tbuf.shm_dtime = shp->shm_dtim;
  645. tbuf.shm_ctime = shp->shm_ctim;
  646. tbuf.shm_cpid = shp->shm_cprid;
  647. tbuf.shm_lpid = shp->shm_lprid;
  648. tbuf.shm_nattch = shp->shm_nattch;
  649. shm_unlock(shp);
  650. if(copy_shmid_to_user (buf, &tbuf, version))
  651. err = -EFAULT;
  652. else
  653. err = result;
  654. goto out;
  655. }
  656. case SHM_LOCK:
  657. case SHM_UNLOCK:
  658. {
  659. struct file *uninitialized_var(shm_file);
  660. lru_add_drain_all(); /* drain pagevecs to lru lists */
  661. shp = shm_lock_check(ns, shmid);
  662. if (IS_ERR(shp)) {
  663. err = PTR_ERR(shp);
  664. goto out;
  665. }
  666. audit_ipc_obj(&(shp->shm_perm));
  667. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  668. uid_t euid = current_euid();
  669. err = -EPERM;
  670. if (euid != shp->shm_perm.uid &&
  671. euid != shp->shm_perm.cuid)
  672. goto out_unlock;
  673. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK))
  674. goto out_unlock;
  675. }
  676. err = security_shm_shmctl(shp, cmd);
  677. if (err)
  678. goto out_unlock;
  679. if(cmd==SHM_LOCK) {
  680. struct user_struct *user = current_user();
  681. if (!is_file_hugepages(shp->shm_file)) {
  682. err = shmem_lock(shp->shm_file, 1, user);
  683. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)){
  684. shp->shm_perm.mode |= SHM_LOCKED;
  685. shp->mlock_user = user;
  686. }
  687. }
  688. } else if (!is_file_hugepages(shp->shm_file)) {
  689. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  690. shp->shm_perm.mode &= ~SHM_LOCKED;
  691. shp->mlock_user = NULL;
  692. }
  693. shm_unlock(shp);
  694. goto out;
  695. }
  696. case IPC_RMID:
  697. case IPC_SET:
  698. err = shmctl_down(ns, shmid, cmd, buf, version);
  699. return err;
  700. default:
  701. return -EINVAL;
  702. }
  703. out_unlock:
  704. shm_unlock(shp);
  705. out:
  706. return err;
  707. }
  708. /*
  709. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  710. *
  711. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  712. * "raddr" thing points to kernel space, and there has to be a wrapper around
  713. * this.
  714. */
  715. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
  716. {
  717. struct shmid_kernel *shp;
  718. unsigned long addr;
  719. unsigned long size;
  720. struct file * file;
  721. int err;
  722. unsigned long flags;
  723. unsigned long prot;
  724. int acc_mode;
  725. unsigned long user_addr;
  726. struct ipc_namespace *ns;
  727. struct shm_file_data *sfd;
  728. struct path path;
  729. fmode_t f_mode;
  730. err = -EINVAL;
  731. if (shmid < 0)
  732. goto out;
  733. else if ((addr = (ulong)shmaddr)) {
  734. if (addr & (SHMLBA-1)) {
  735. if (shmflg & SHM_RND)
  736. addr &= ~(SHMLBA-1); /* round down */
  737. else
  738. #ifndef __ARCH_FORCE_SHMLBA
  739. if (addr & ~PAGE_MASK)
  740. #endif
  741. goto out;
  742. }
  743. flags = MAP_SHARED | MAP_FIXED;
  744. } else {
  745. if ((shmflg & SHM_REMAP))
  746. goto out;
  747. flags = MAP_SHARED;
  748. }
  749. if (shmflg & SHM_RDONLY) {
  750. prot = PROT_READ;
  751. acc_mode = S_IRUGO;
  752. f_mode = FMODE_READ;
  753. } else {
  754. prot = PROT_READ | PROT_WRITE;
  755. acc_mode = S_IRUGO | S_IWUGO;
  756. f_mode = FMODE_READ | FMODE_WRITE;
  757. }
  758. if (shmflg & SHM_EXEC) {
  759. prot |= PROT_EXEC;
  760. acc_mode |= S_IXUGO;
  761. }
  762. /*
  763. * We cannot rely on the fs check since SYSV IPC does have an
  764. * additional creator id...
  765. */
  766. ns = current->nsproxy->ipc_ns;
  767. shp = shm_lock_check(ns, shmid);
  768. if (IS_ERR(shp)) {
  769. err = PTR_ERR(shp);
  770. goto out;
  771. }
  772. err = -EACCES;
  773. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  774. goto out_unlock;
  775. err = security_shm_shmat(shp, shmaddr, shmflg);
  776. if (err)
  777. goto out_unlock;
  778. path = shp->shm_file->f_path;
  779. path_get(&path);
  780. shp->shm_nattch++;
  781. size = i_size_read(path.dentry->d_inode);
  782. shm_unlock(shp);
  783. err = -ENOMEM;
  784. sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
  785. if (!sfd)
  786. goto out_put_dentry;
  787. file = alloc_file(&path, f_mode,
  788. is_file_hugepages(shp->shm_file) ?
  789. &shm_file_operations_huge :
  790. &shm_file_operations);
  791. if (!file)
  792. goto out_free;
  793. file->private_data = sfd;
  794. file->f_mapping = shp->shm_file->f_mapping;
  795. sfd->id = shp->shm_perm.id;
  796. sfd->ns = get_ipc_ns(ns);
  797. sfd->file = shp->shm_file;
  798. sfd->vm_ops = NULL;
  799. down_write(&current->mm->mmap_sem);
  800. if (addr && !(shmflg & SHM_REMAP)) {
  801. err = -EINVAL;
  802. if (find_vma_intersection(current->mm, addr, addr + size))
  803. goto invalid;
  804. /*
  805. * If shm segment goes below stack, make sure there is some
  806. * space left for the stack to grow (at least 4 pages).
  807. */
  808. if (addr < current->mm->start_stack &&
  809. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  810. goto invalid;
  811. }
  812. user_addr = do_mmap (file, addr, size, prot, flags, 0);
  813. *raddr = user_addr;
  814. err = 0;
  815. if (IS_ERR_VALUE(user_addr))
  816. err = (long)user_addr;
  817. invalid:
  818. up_write(&current->mm->mmap_sem);
  819. fput(file);
  820. out_nattch:
  821. down_write(&shm_ids(ns).rw_mutex);
  822. shp = shm_lock(ns, shmid);
  823. BUG_ON(IS_ERR(shp));
  824. shp->shm_nattch--;
  825. if(shp->shm_nattch == 0 &&
  826. shp->shm_perm.mode & SHM_DEST)
  827. shm_destroy(ns, shp);
  828. else
  829. shm_unlock(shp);
  830. up_write(&shm_ids(ns).rw_mutex);
  831. out:
  832. return err;
  833. out_unlock:
  834. shm_unlock(shp);
  835. goto out;
  836. out_free:
  837. kfree(sfd);
  838. out_put_dentry:
  839. path_put(&path);
  840. goto out_nattch;
  841. }
  842. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  843. {
  844. unsigned long ret;
  845. long err;
  846. err = do_shmat(shmid, shmaddr, shmflg, &ret);
  847. if (err)
  848. return err;
  849. force_successful_syscall_return();
  850. return (long)ret;
  851. }
  852. /*
  853. * detach and kill segment if marked destroyed.
  854. * The work is done in shm_close.
  855. */
  856. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  857. {
  858. struct mm_struct *mm = current->mm;
  859. struct vm_area_struct *vma;
  860. unsigned long addr = (unsigned long)shmaddr;
  861. int retval = -EINVAL;
  862. #ifdef CONFIG_MMU
  863. loff_t size = 0;
  864. struct vm_area_struct *next;
  865. #endif
  866. if (addr & ~PAGE_MASK)
  867. return retval;
  868. down_write(&mm->mmap_sem);
  869. /*
  870. * This function tries to be smart and unmap shm segments that
  871. * were modified by partial mlock or munmap calls:
  872. * - It first determines the size of the shm segment that should be
  873. * unmapped: It searches for a vma that is backed by shm and that
  874. * started at address shmaddr. It records it's size and then unmaps
  875. * it.
  876. * - Then it unmaps all shm vmas that started at shmaddr and that
  877. * are within the initially determined size.
  878. * Errors from do_munmap are ignored: the function only fails if
  879. * it's called with invalid parameters or if it's called to unmap
  880. * a part of a vma. Both calls in this function are for full vmas,
  881. * the parameters are directly copied from the vma itself and always
  882. * valid - therefore do_munmap cannot fail. (famous last words?)
  883. */
  884. /*
  885. * If it had been mremap()'d, the starting address would not
  886. * match the usual checks anyway. So assume all vma's are
  887. * above the starting address given.
  888. */
  889. vma = find_vma(mm, addr);
  890. #ifdef CONFIG_MMU
  891. while (vma) {
  892. next = vma->vm_next;
  893. /*
  894. * Check if the starting address would match, i.e. it's
  895. * a fragment created by mprotect() and/or munmap(), or it
  896. * otherwise it starts at this address with no hassles.
  897. */
  898. if ((vma->vm_ops == &shm_vm_ops) &&
  899. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  900. size = vma->vm_file->f_path.dentry->d_inode->i_size;
  901. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  902. /*
  903. * We discovered the size of the shm segment, so
  904. * break out of here and fall through to the next
  905. * loop that uses the size information to stop
  906. * searching for matching vma's.
  907. */
  908. retval = 0;
  909. vma = next;
  910. break;
  911. }
  912. vma = next;
  913. }
  914. /*
  915. * We need look no further than the maximum address a fragment
  916. * could possibly have landed at. Also cast things to loff_t to
  917. * prevent overflows and make comparisons vs. equal-width types.
  918. */
  919. size = PAGE_ALIGN(size);
  920. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  921. next = vma->vm_next;
  922. /* finding a matching vma now does not alter retval */
  923. if ((vma->vm_ops == &shm_vm_ops) &&
  924. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  925. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  926. vma = next;
  927. }
  928. #else /* CONFIG_MMU */
  929. /* under NOMMU conditions, the exact address to be destroyed must be
  930. * given */
  931. retval = -EINVAL;
  932. if (vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  933. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  934. retval = 0;
  935. }
  936. #endif
  937. up_write(&mm->mmap_sem);
  938. return retval;
  939. }
  940. #ifdef CONFIG_PROC_FS
  941. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  942. {
  943. struct shmid_kernel *shp = it;
  944. unsigned long rss = 0, swp = 0;
  945. shm_add_rss_swap(shp, &rss, &swp);
  946. #if BITS_PER_LONG <= 32
  947. #define SIZE_SPEC "%10lu"
  948. #else
  949. #define SIZE_SPEC "%21lu"
  950. #endif
  951. return seq_printf(s,
  952. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  953. "%5lu %5u %5u %5u %5u %10lu %10lu %10lu "
  954. SIZE_SPEC " " SIZE_SPEC "\n",
  955. shp->shm_perm.key,
  956. shp->shm_perm.id,
  957. shp->shm_perm.mode,
  958. shp->shm_segsz,
  959. shp->shm_cprid,
  960. shp->shm_lprid,
  961. shp->shm_nattch,
  962. shp->shm_perm.uid,
  963. shp->shm_perm.gid,
  964. shp->shm_perm.cuid,
  965. shp->shm_perm.cgid,
  966. shp->shm_atim,
  967. shp->shm_dtim,
  968. shp->shm_ctim,
  969. rss * PAGE_SIZE,
  970. swp * PAGE_SIZE);
  971. }
  972. #endif