shm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. */
  17. #include <linux/config.h>
  18. #include <linux/slab.h>
  19. #include <linux/mm.h>
  20. #include <linux/hugetlb.h>
  21. #include <linux/shm.h>
  22. #include <linux/init.h>
  23. #include <linux/file.h>
  24. #include <linux/mman.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/shmem_fs.h>
  27. #include <linux/security.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/audit.h>
  30. #include <asm/uaccess.h>
  31. #include "util.h"
  32. #define shm_flags shm_perm.mode
  33. static struct file_operations shm_file_operations;
  34. static struct vm_operations_struct shm_vm_ops;
  35. static struct ipc_ids shm_ids;
  36. #define shm_lock(id) ((struct shmid_kernel*)ipc_lock(&shm_ids,id))
  37. #define shm_unlock(shp) ipc_unlock(&(shp)->shm_perm)
  38. #define shm_get(id) ((struct shmid_kernel*)ipc_get(&shm_ids,id))
  39. #define shm_buildid(id, seq) \
  40. ipc_buildid(&shm_ids, id, seq)
  41. static int newseg (key_t key, int shmflg, size_t size);
  42. static void shm_open (struct vm_area_struct *shmd);
  43. static void shm_close (struct vm_area_struct *shmd);
  44. #ifdef CONFIG_PROC_FS
  45. static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
  46. #endif
  47. size_t shm_ctlmax = SHMMAX;
  48. size_t shm_ctlall = SHMALL;
  49. int shm_ctlmni = SHMMNI;
  50. static int shm_tot; /* total number of shared memory pages */
  51. void __init shm_init (void)
  52. {
  53. ipc_init_ids(&shm_ids, 1);
  54. #ifdef CONFIG_PROC_FS
  55. create_proc_read_entry("sysvipc/shm", 0, NULL, sysvipc_shm_read_proc, NULL);
  56. #endif
  57. }
  58. static inline int shm_checkid(struct shmid_kernel *s, int id)
  59. {
  60. if (ipc_checkid(&shm_ids,&s->shm_perm,id))
  61. return -EIDRM;
  62. return 0;
  63. }
  64. static inline struct shmid_kernel *shm_rmid(int id)
  65. {
  66. return (struct shmid_kernel *)ipc_rmid(&shm_ids,id);
  67. }
  68. static inline int shm_addid(struct shmid_kernel *shp)
  69. {
  70. return ipc_addid(&shm_ids, &shp->shm_perm, shm_ctlmni);
  71. }
  72. static inline void shm_inc (int id) {
  73. struct shmid_kernel *shp;
  74. if(!(shp = shm_lock(id)))
  75. BUG();
  76. shp->shm_atim = get_seconds();
  77. shp->shm_lprid = current->tgid;
  78. shp->shm_nattch++;
  79. shm_unlock(shp);
  80. }
  81. /* This is called by fork, once for every shm attach. */
  82. static void shm_open (struct vm_area_struct *shmd)
  83. {
  84. shm_inc (shmd->vm_file->f_dentry->d_inode->i_ino);
  85. }
  86. /*
  87. * shm_destroy - free the struct shmid_kernel
  88. *
  89. * @shp: struct to free
  90. *
  91. * It has to be called with shp and shm_ids.sem locked,
  92. * but returns with shp unlocked and freed.
  93. */
  94. static void shm_destroy (struct shmid_kernel *shp)
  95. {
  96. shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  97. shm_rmid (shp->id);
  98. shm_unlock(shp);
  99. if (!is_file_hugepages(shp->shm_file))
  100. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  101. else
  102. user_shm_unlock(shp->shm_file->f_dentry->d_inode->i_size,
  103. shp->mlock_user);
  104. fput (shp->shm_file);
  105. security_shm_free(shp);
  106. ipc_rcu_putref(shp);
  107. }
  108. /*
  109. * remove the attach descriptor shmd.
  110. * free memory for segment if it is marked destroyed.
  111. * The descriptor has already been removed from the current->mm->mmap list
  112. * and will later be kfree()d.
  113. */
  114. static void shm_close (struct vm_area_struct *shmd)
  115. {
  116. struct file * file = shmd->vm_file;
  117. int id = file->f_dentry->d_inode->i_ino;
  118. struct shmid_kernel *shp;
  119. down (&shm_ids.sem);
  120. /* remove from the list of attaches of the shm segment */
  121. if(!(shp = shm_lock(id)))
  122. BUG();
  123. shp->shm_lprid = current->tgid;
  124. shp->shm_dtim = get_seconds();
  125. shp->shm_nattch--;
  126. if(shp->shm_nattch == 0 &&
  127. shp->shm_flags & SHM_DEST)
  128. shm_destroy (shp);
  129. else
  130. shm_unlock(shp);
  131. up (&shm_ids.sem);
  132. }
  133. static int shm_mmap(struct file * file, struct vm_area_struct * vma)
  134. {
  135. file_accessed(file);
  136. vma->vm_ops = &shm_vm_ops;
  137. shm_inc(file->f_dentry->d_inode->i_ino);
  138. return 0;
  139. }
  140. static struct file_operations shm_file_operations = {
  141. .mmap = shm_mmap
  142. };
  143. static struct vm_operations_struct shm_vm_ops = {
  144. .open = shm_open, /* callback for a new vm-area open */
  145. .close = shm_close, /* callback for when the vm-area is released */
  146. .nopage = shmem_nopage,
  147. #ifdef CONFIG_NUMA
  148. .set_policy = shmem_set_policy,
  149. .get_policy = shmem_get_policy,
  150. #endif
  151. };
  152. static int newseg (key_t key, int shmflg, size_t size)
  153. {
  154. int error;
  155. struct shmid_kernel *shp;
  156. int numpages = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
  157. struct file * file;
  158. char name[13];
  159. int id;
  160. if (size < SHMMIN || size > shm_ctlmax)
  161. return -EINVAL;
  162. if (shm_tot + numpages >= shm_ctlall)
  163. return -ENOSPC;
  164. shp = ipc_rcu_alloc(sizeof(*shp));
  165. if (!shp)
  166. return -ENOMEM;
  167. shp->shm_perm.key = key;
  168. shp->shm_flags = (shmflg & S_IRWXUGO);
  169. shp->mlock_user = NULL;
  170. shp->shm_perm.security = NULL;
  171. error = security_shm_alloc(shp);
  172. if (error) {
  173. ipc_rcu_putref(shp);
  174. return error;
  175. }
  176. if (shmflg & SHM_HUGETLB) {
  177. /* hugetlb_zero_setup takes care of mlock user accounting */
  178. file = hugetlb_zero_setup(size);
  179. shp->mlock_user = current->user;
  180. } else {
  181. sprintf (name, "SYSV%08x", key);
  182. file = shmem_file_setup(name, size, VM_ACCOUNT);
  183. }
  184. error = PTR_ERR(file);
  185. if (IS_ERR(file))
  186. goto no_file;
  187. error = -ENOSPC;
  188. id = shm_addid(shp);
  189. if(id == -1)
  190. goto no_id;
  191. shp->shm_cprid = current->tgid;
  192. shp->shm_lprid = 0;
  193. shp->shm_atim = shp->shm_dtim = 0;
  194. shp->shm_ctim = get_seconds();
  195. shp->shm_segsz = size;
  196. shp->shm_nattch = 0;
  197. shp->id = shm_buildid(id,shp->shm_perm.seq);
  198. shp->shm_file = file;
  199. file->f_dentry->d_inode->i_ino = shp->id;
  200. if (shmflg & SHM_HUGETLB)
  201. set_file_hugepages(file);
  202. else
  203. file->f_op = &shm_file_operations;
  204. shm_tot += numpages;
  205. shm_unlock(shp);
  206. return shp->id;
  207. no_id:
  208. fput(file);
  209. no_file:
  210. security_shm_free(shp);
  211. ipc_rcu_putref(shp);
  212. return error;
  213. }
  214. asmlinkage long sys_shmget (key_t key, size_t size, int shmflg)
  215. {
  216. struct shmid_kernel *shp;
  217. int err, id = 0;
  218. down(&shm_ids.sem);
  219. if (key == IPC_PRIVATE) {
  220. err = newseg(key, shmflg, size);
  221. } else if ((id = ipc_findkey(&shm_ids, key)) == -1) {
  222. if (!(shmflg & IPC_CREAT))
  223. err = -ENOENT;
  224. else
  225. err = newseg(key, shmflg, size);
  226. } else if ((shmflg & IPC_CREAT) && (shmflg & IPC_EXCL)) {
  227. err = -EEXIST;
  228. } else {
  229. shp = shm_lock(id);
  230. if(shp==NULL)
  231. BUG();
  232. if (shp->shm_segsz < size)
  233. err = -EINVAL;
  234. else if (ipcperms(&shp->shm_perm, shmflg))
  235. err = -EACCES;
  236. else {
  237. int shmid = shm_buildid(id, shp->shm_perm.seq);
  238. err = security_shm_associate(shp, shmflg);
  239. if (!err)
  240. err = shmid;
  241. }
  242. shm_unlock(shp);
  243. }
  244. up(&shm_ids.sem);
  245. return err;
  246. }
  247. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  248. {
  249. switch(version) {
  250. case IPC_64:
  251. return copy_to_user(buf, in, sizeof(*in));
  252. case IPC_OLD:
  253. {
  254. struct shmid_ds out;
  255. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  256. out.shm_segsz = in->shm_segsz;
  257. out.shm_atime = in->shm_atime;
  258. out.shm_dtime = in->shm_dtime;
  259. out.shm_ctime = in->shm_ctime;
  260. out.shm_cpid = in->shm_cpid;
  261. out.shm_lpid = in->shm_lpid;
  262. out.shm_nattch = in->shm_nattch;
  263. return copy_to_user(buf, &out, sizeof(out));
  264. }
  265. default:
  266. return -EINVAL;
  267. }
  268. }
  269. struct shm_setbuf {
  270. uid_t uid;
  271. gid_t gid;
  272. mode_t mode;
  273. };
  274. static inline unsigned long copy_shmid_from_user(struct shm_setbuf *out, void __user *buf, int version)
  275. {
  276. switch(version) {
  277. case IPC_64:
  278. {
  279. struct shmid64_ds tbuf;
  280. if (copy_from_user(&tbuf, buf, sizeof(tbuf)))
  281. return -EFAULT;
  282. out->uid = tbuf.shm_perm.uid;
  283. out->gid = tbuf.shm_perm.gid;
  284. out->mode = tbuf.shm_flags;
  285. return 0;
  286. }
  287. case IPC_OLD:
  288. {
  289. struct shmid_ds tbuf_old;
  290. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  291. return -EFAULT;
  292. out->uid = tbuf_old.shm_perm.uid;
  293. out->gid = tbuf_old.shm_perm.gid;
  294. out->mode = tbuf_old.shm_flags;
  295. return 0;
  296. }
  297. default:
  298. return -EINVAL;
  299. }
  300. }
  301. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  302. {
  303. switch(version) {
  304. case IPC_64:
  305. return copy_to_user(buf, in, sizeof(*in));
  306. case IPC_OLD:
  307. {
  308. struct shminfo out;
  309. if(in->shmmax > INT_MAX)
  310. out.shmmax = INT_MAX;
  311. else
  312. out.shmmax = (int)in->shmmax;
  313. out.shmmin = in->shmmin;
  314. out.shmmni = in->shmmni;
  315. out.shmseg = in->shmseg;
  316. out.shmall = in->shmall;
  317. return copy_to_user(buf, &out, sizeof(out));
  318. }
  319. default:
  320. return -EINVAL;
  321. }
  322. }
  323. static void shm_get_stat(unsigned long *rss, unsigned long *swp)
  324. {
  325. int i;
  326. *rss = 0;
  327. *swp = 0;
  328. for (i = 0; i <= shm_ids.max_id; i++) {
  329. struct shmid_kernel *shp;
  330. struct inode *inode;
  331. shp = shm_get(i);
  332. if(!shp)
  333. continue;
  334. inode = shp->shm_file->f_dentry->d_inode;
  335. if (is_file_hugepages(shp->shm_file)) {
  336. struct address_space *mapping = inode->i_mapping;
  337. *rss += (HPAGE_SIZE/PAGE_SIZE)*mapping->nrpages;
  338. } else {
  339. struct shmem_inode_info *info = SHMEM_I(inode);
  340. spin_lock(&info->lock);
  341. *rss += inode->i_mapping->nrpages;
  342. *swp += info->swapped;
  343. spin_unlock(&info->lock);
  344. }
  345. }
  346. }
  347. asmlinkage long sys_shmctl (int shmid, int cmd, struct shmid_ds __user *buf)
  348. {
  349. struct shm_setbuf setbuf;
  350. struct shmid_kernel *shp;
  351. int err, version;
  352. if (cmd < 0 || shmid < 0) {
  353. err = -EINVAL;
  354. goto out;
  355. }
  356. version = ipc_parse_version(&cmd);
  357. switch (cmd) { /* replace with proc interface ? */
  358. case IPC_INFO:
  359. {
  360. struct shminfo64 shminfo;
  361. err = security_shm_shmctl(NULL, cmd);
  362. if (err)
  363. return err;
  364. memset(&shminfo,0,sizeof(shminfo));
  365. shminfo.shmmni = shminfo.shmseg = shm_ctlmni;
  366. shminfo.shmmax = shm_ctlmax;
  367. shminfo.shmall = shm_ctlall;
  368. shminfo.shmmin = SHMMIN;
  369. if(copy_shminfo_to_user (buf, &shminfo, version))
  370. return -EFAULT;
  371. /* reading a integer is always atomic */
  372. err= shm_ids.max_id;
  373. if(err<0)
  374. err = 0;
  375. goto out;
  376. }
  377. case SHM_INFO:
  378. {
  379. struct shm_info shm_info;
  380. err = security_shm_shmctl(NULL, cmd);
  381. if (err)
  382. return err;
  383. memset(&shm_info,0,sizeof(shm_info));
  384. down(&shm_ids.sem);
  385. shm_info.used_ids = shm_ids.in_use;
  386. shm_get_stat (&shm_info.shm_rss, &shm_info.shm_swp);
  387. shm_info.shm_tot = shm_tot;
  388. shm_info.swap_attempts = 0;
  389. shm_info.swap_successes = 0;
  390. err = shm_ids.max_id;
  391. up(&shm_ids.sem);
  392. if(copy_to_user (buf, &shm_info, sizeof(shm_info))) {
  393. err = -EFAULT;
  394. goto out;
  395. }
  396. err = err < 0 ? 0 : err;
  397. goto out;
  398. }
  399. case SHM_STAT:
  400. case IPC_STAT:
  401. {
  402. struct shmid64_ds tbuf;
  403. int result;
  404. memset(&tbuf, 0, sizeof(tbuf));
  405. shp = shm_lock(shmid);
  406. if(shp==NULL) {
  407. err = -EINVAL;
  408. goto out;
  409. } else if(cmd==SHM_STAT) {
  410. err = -EINVAL;
  411. if (shmid > shm_ids.max_id)
  412. goto out_unlock;
  413. result = shm_buildid(shmid, shp->shm_perm.seq);
  414. } else {
  415. err = shm_checkid(shp,shmid);
  416. if(err)
  417. goto out_unlock;
  418. result = 0;
  419. }
  420. err=-EACCES;
  421. if (ipcperms (&shp->shm_perm, S_IRUGO))
  422. goto out_unlock;
  423. err = security_shm_shmctl(shp, cmd);
  424. if (err)
  425. goto out_unlock;
  426. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf.shm_perm);
  427. tbuf.shm_segsz = shp->shm_segsz;
  428. tbuf.shm_atime = shp->shm_atim;
  429. tbuf.shm_dtime = shp->shm_dtim;
  430. tbuf.shm_ctime = shp->shm_ctim;
  431. tbuf.shm_cpid = shp->shm_cprid;
  432. tbuf.shm_lpid = shp->shm_lprid;
  433. if (!is_file_hugepages(shp->shm_file))
  434. tbuf.shm_nattch = shp->shm_nattch;
  435. else
  436. tbuf.shm_nattch = file_count(shp->shm_file) - 1;
  437. shm_unlock(shp);
  438. if(copy_shmid_to_user (buf, &tbuf, version))
  439. err = -EFAULT;
  440. else
  441. err = result;
  442. goto out;
  443. }
  444. case SHM_LOCK:
  445. case SHM_UNLOCK:
  446. {
  447. shp = shm_lock(shmid);
  448. if(shp==NULL) {
  449. err = -EINVAL;
  450. goto out;
  451. }
  452. err = shm_checkid(shp,shmid);
  453. if(err)
  454. goto out_unlock;
  455. if (!capable(CAP_IPC_LOCK)) {
  456. err = -EPERM;
  457. if (current->euid != shp->shm_perm.uid &&
  458. current->euid != shp->shm_perm.cuid)
  459. goto out_unlock;
  460. if (cmd == SHM_LOCK &&
  461. !current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
  462. goto out_unlock;
  463. }
  464. err = security_shm_shmctl(shp, cmd);
  465. if (err)
  466. goto out_unlock;
  467. if(cmd==SHM_LOCK) {
  468. struct user_struct * user = current->user;
  469. if (!is_file_hugepages(shp->shm_file)) {
  470. err = shmem_lock(shp->shm_file, 1, user);
  471. if (!err) {
  472. shp->shm_flags |= SHM_LOCKED;
  473. shp->mlock_user = user;
  474. }
  475. }
  476. } else if (!is_file_hugepages(shp->shm_file)) {
  477. shmem_lock(shp->shm_file, 0, shp->mlock_user);
  478. shp->shm_flags &= ~SHM_LOCKED;
  479. shp->mlock_user = NULL;
  480. }
  481. shm_unlock(shp);
  482. goto out;
  483. }
  484. case IPC_RMID:
  485. {
  486. /*
  487. * We cannot simply remove the file. The SVID states
  488. * that the block remains until the last person
  489. * detaches from it, then is deleted. A shmat() on
  490. * an RMID segment is legal in older Linux and if
  491. * we change it apps break...
  492. *
  493. * Instead we set a destroyed flag, and then blow
  494. * the name away when the usage hits zero.
  495. */
  496. down(&shm_ids.sem);
  497. shp = shm_lock(shmid);
  498. err = -EINVAL;
  499. if (shp == NULL)
  500. goto out_up;
  501. err = shm_checkid(shp, shmid);
  502. if(err)
  503. goto out_unlock_up;
  504. if (current->euid != shp->shm_perm.uid &&
  505. current->euid != shp->shm_perm.cuid &&
  506. !capable(CAP_SYS_ADMIN)) {
  507. err=-EPERM;
  508. goto out_unlock_up;
  509. }
  510. err = security_shm_shmctl(shp, cmd);
  511. if (err)
  512. goto out_unlock_up;
  513. if (shp->shm_nattch){
  514. shp->shm_flags |= SHM_DEST;
  515. /* Do not find it any more */
  516. shp->shm_perm.key = IPC_PRIVATE;
  517. shm_unlock(shp);
  518. } else
  519. shm_destroy (shp);
  520. up(&shm_ids.sem);
  521. goto out;
  522. }
  523. case IPC_SET:
  524. {
  525. if (copy_shmid_from_user (&setbuf, buf, version)) {
  526. err = -EFAULT;
  527. goto out;
  528. }
  529. if ((err = audit_ipc_perms(0, setbuf.uid, setbuf.gid, setbuf.mode)))
  530. return err;
  531. down(&shm_ids.sem);
  532. shp = shm_lock(shmid);
  533. err=-EINVAL;
  534. if(shp==NULL)
  535. goto out_up;
  536. err = shm_checkid(shp,shmid);
  537. if(err)
  538. goto out_unlock_up;
  539. err=-EPERM;
  540. if (current->euid != shp->shm_perm.uid &&
  541. current->euid != shp->shm_perm.cuid &&
  542. !capable(CAP_SYS_ADMIN)) {
  543. goto out_unlock_up;
  544. }
  545. err = security_shm_shmctl(shp, cmd);
  546. if (err)
  547. goto out_unlock_up;
  548. shp->shm_perm.uid = setbuf.uid;
  549. shp->shm_perm.gid = setbuf.gid;
  550. shp->shm_flags = (shp->shm_flags & ~S_IRWXUGO)
  551. | (setbuf.mode & S_IRWXUGO);
  552. shp->shm_ctim = get_seconds();
  553. break;
  554. }
  555. default:
  556. err = -EINVAL;
  557. goto out;
  558. }
  559. err = 0;
  560. out_unlock_up:
  561. shm_unlock(shp);
  562. out_up:
  563. up(&shm_ids.sem);
  564. goto out;
  565. out_unlock:
  566. shm_unlock(shp);
  567. out:
  568. return err;
  569. }
  570. /*
  571. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  572. *
  573. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  574. * "raddr" thing points to kernel space, and there has to be a wrapper around
  575. * this.
  576. */
  577. long do_shmat(int shmid, char __user *shmaddr, int shmflg, ulong *raddr)
  578. {
  579. struct shmid_kernel *shp;
  580. unsigned long addr;
  581. unsigned long size;
  582. struct file * file;
  583. int err;
  584. unsigned long flags;
  585. unsigned long prot;
  586. unsigned long o_flags;
  587. int acc_mode;
  588. void *user_addr;
  589. if (shmid < 0) {
  590. err = -EINVAL;
  591. goto out;
  592. } else if ((addr = (ulong)shmaddr)) {
  593. if (addr & (SHMLBA-1)) {
  594. if (shmflg & SHM_RND)
  595. addr &= ~(SHMLBA-1); /* round down */
  596. else
  597. #ifndef __ARCH_FORCE_SHMLBA
  598. if (addr & ~PAGE_MASK)
  599. #endif
  600. return -EINVAL;
  601. }
  602. flags = MAP_SHARED | MAP_FIXED;
  603. } else {
  604. if ((shmflg & SHM_REMAP))
  605. return -EINVAL;
  606. flags = MAP_SHARED;
  607. }
  608. if (shmflg & SHM_RDONLY) {
  609. prot = PROT_READ;
  610. o_flags = O_RDONLY;
  611. acc_mode = S_IRUGO;
  612. } else {
  613. prot = PROT_READ | PROT_WRITE;
  614. o_flags = O_RDWR;
  615. acc_mode = S_IRUGO | S_IWUGO;
  616. }
  617. if (shmflg & SHM_EXEC) {
  618. prot |= PROT_EXEC;
  619. acc_mode |= S_IXUGO;
  620. }
  621. /*
  622. * We cannot rely on the fs check since SYSV IPC does have an
  623. * additional creator id...
  624. */
  625. shp = shm_lock(shmid);
  626. if(shp == NULL) {
  627. err = -EINVAL;
  628. goto out;
  629. }
  630. err = shm_checkid(shp,shmid);
  631. if (err) {
  632. shm_unlock(shp);
  633. goto out;
  634. }
  635. if (ipcperms(&shp->shm_perm, acc_mode)) {
  636. shm_unlock(shp);
  637. err = -EACCES;
  638. goto out;
  639. }
  640. err = security_shm_shmat(shp, shmaddr, shmflg);
  641. if (err) {
  642. shm_unlock(shp);
  643. return err;
  644. }
  645. file = shp->shm_file;
  646. size = i_size_read(file->f_dentry->d_inode);
  647. shp->shm_nattch++;
  648. shm_unlock(shp);
  649. down_write(&current->mm->mmap_sem);
  650. if (addr && !(shmflg & SHM_REMAP)) {
  651. user_addr = ERR_PTR(-EINVAL);
  652. if (find_vma_intersection(current->mm, addr, addr + size))
  653. goto invalid;
  654. /*
  655. * If shm segment goes below stack, make sure there is some
  656. * space left for the stack to grow (at least 4 pages).
  657. */
  658. if (addr < current->mm->start_stack &&
  659. addr > current->mm->start_stack - size - PAGE_SIZE * 5)
  660. goto invalid;
  661. }
  662. user_addr = (void*) do_mmap (file, addr, size, prot, flags, 0);
  663. invalid:
  664. up_write(&current->mm->mmap_sem);
  665. down (&shm_ids.sem);
  666. if(!(shp = shm_lock(shmid)))
  667. BUG();
  668. shp->shm_nattch--;
  669. if(shp->shm_nattch == 0 &&
  670. shp->shm_flags & SHM_DEST)
  671. shm_destroy (shp);
  672. else
  673. shm_unlock(shp);
  674. up (&shm_ids.sem);
  675. *raddr = (unsigned long) user_addr;
  676. err = 0;
  677. if (IS_ERR(user_addr))
  678. err = PTR_ERR(user_addr);
  679. out:
  680. return err;
  681. }
  682. /*
  683. * detach and kill segment if marked destroyed.
  684. * The work is done in shm_close.
  685. */
  686. asmlinkage long sys_shmdt(char __user *shmaddr)
  687. {
  688. struct mm_struct *mm = current->mm;
  689. struct vm_area_struct *vma, *next;
  690. unsigned long addr = (unsigned long)shmaddr;
  691. loff_t size = 0;
  692. int retval = -EINVAL;
  693. down_write(&mm->mmap_sem);
  694. /*
  695. * This function tries to be smart and unmap shm segments that
  696. * were modified by partial mlock or munmap calls:
  697. * - It first determines the size of the shm segment that should be
  698. * unmapped: It searches for a vma that is backed by shm and that
  699. * started at address shmaddr. It records it's size and then unmaps
  700. * it.
  701. * - Then it unmaps all shm vmas that started at shmaddr and that
  702. * are within the initially determined size.
  703. * Errors from do_munmap are ignored: the function only fails if
  704. * it's called with invalid parameters or if it's called to unmap
  705. * a part of a vma. Both calls in this function are for full vmas,
  706. * the parameters are directly copied from the vma itself and always
  707. * valid - therefore do_munmap cannot fail. (famous last words?)
  708. */
  709. /*
  710. * If it had been mremap()'d, the starting address would not
  711. * match the usual checks anyway. So assume all vma's are
  712. * above the starting address given.
  713. */
  714. vma = find_vma(mm, addr);
  715. while (vma) {
  716. next = vma->vm_next;
  717. /*
  718. * Check if the starting address would match, i.e. it's
  719. * a fragment created by mprotect() and/or munmap(), or it
  720. * otherwise it starts at this address with no hassles.
  721. */
  722. if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
  723. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  724. size = vma->vm_file->f_dentry->d_inode->i_size;
  725. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  726. /*
  727. * We discovered the size of the shm segment, so
  728. * break out of here and fall through to the next
  729. * loop that uses the size information to stop
  730. * searching for matching vma's.
  731. */
  732. retval = 0;
  733. vma = next;
  734. break;
  735. }
  736. vma = next;
  737. }
  738. /*
  739. * We need look no further than the maximum address a fragment
  740. * could possibly have landed at. Also cast things to loff_t to
  741. * prevent overflows and make comparisions vs. equal-width types.
  742. */
  743. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  744. next = vma->vm_next;
  745. /* finding a matching vma now does not alter retval */
  746. if ((vma->vm_ops == &shm_vm_ops || is_vm_hugetlb_page(vma)) &&
  747. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff)
  748. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
  749. vma = next;
  750. }
  751. up_write(&mm->mmap_sem);
  752. return retval;
  753. }
  754. #ifdef CONFIG_PROC_FS
  755. static int sysvipc_shm_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
  756. {
  757. off_t pos = 0;
  758. off_t begin = 0;
  759. int i, len = 0;
  760. down(&shm_ids.sem);
  761. len += sprintf(buffer, " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime\n");
  762. for(i = 0; i <= shm_ids.max_id; i++) {
  763. struct shmid_kernel* shp;
  764. shp = shm_lock(i);
  765. if(shp!=NULL) {
  766. #define SMALL_STRING "%10d %10d %4o %10u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
  767. #define BIG_STRING "%10d %10d %4o %21u %5u %5u %5d %5u %5u %5u %5u %10lu %10lu %10lu\n"
  768. char *format;
  769. if (sizeof(size_t) <= sizeof(int))
  770. format = SMALL_STRING;
  771. else
  772. format = BIG_STRING;
  773. len += sprintf(buffer + len, format,
  774. shp->shm_perm.key,
  775. shm_buildid(i, shp->shm_perm.seq),
  776. shp->shm_flags,
  777. shp->shm_segsz,
  778. shp->shm_cprid,
  779. shp->shm_lprid,
  780. is_file_hugepages(shp->shm_file) ? (file_count(shp->shm_file) - 1) : shp->shm_nattch,
  781. shp->shm_perm.uid,
  782. shp->shm_perm.gid,
  783. shp->shm_perm.cuid,
  784. shp->shm_perm.cgid,
  785. shp->shm_atim,
  786. shp->shm_dtim,
  787. shp->shm_ctim);
  788. shm_unlock(shp);
  789. pos += len;
  790. if(pos < offset) {
  791. len = 0;
  792. begin = pos;
  793. }
  794. if(pos > offset + length)
  795. goto done;
  796. }
  797. }
  798. *eof = 1;
  799. done:
  800. up(&shm_ids.sem);
  801. *start = buffer + (offset - begin);
  802. len -= (offset - begin);
  803. if(len > length)
  804. len = length;
  805. if(len < 0)
  806. len = 0;
  807. return len;
  808. }
  809. #endif