shm.c 27 KB

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