shm.c 27 KB

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