shm.c 27 KB

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