shm.c 26 KB

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