shm.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/ipc/shm.c
  4. * Copyright (C) 1992, 1993 Krishna Balasubramanian
  5. * Many improvements/fixes by Bruno Haible.
  6. * Replaced `struct shm_desc' by `struct vm_area_struct', July 1994.
  7. * Fixed the shm swap deallocation (shm_unuse()), August 1998 Andrea Arcangeli.
  8. *
  9. * /proc/sysvipc/shm support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
  10. * BIGMEM support, Andrea Arcangeli <andrea@suse.de>
  11. * SMP thread shm, Jean-Luc Boyard <jean-luc.boyard@siemens.fr>
  12. * HIGHMEM support, Ingo Molnar <mingo@redhat.com>
  13. * Make shmmax, shmall, shmmni sysctl'able, Christoph Rohland <cr@sap.com>
  14. * Shared /dev/zero support, Kanoj Sarcar <kanoj@sgi.com>
  15. * Move the mm functionality over to mm/shmem.c, Christoph Rohland <cr@sap.com>
  16. *
  17. * support for audit of ipc object properties and permission changes
  18. * Dustin Kirkland <dustin.kirkland@us.ibm.com>
  19. *
  20. * namespaces support
  21. * OpenVZ, SWsoft Inc.
  22. * Pavel Emelianov <xemul@openvz.org>
  23. *
  24. * Better ipc lock (kern_ipc_perm.lock) handling
  25. * Davidlohr Bueso <davidlohr.bueso@hp.com>, June 2013.
  26. */
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/hugetlb.h>
  30. #include <linux/shm.h>
  31. #include <uapi/linux/shm.h>
  32. #include <linux/init.h>
  33. #include <linux/file.h>
  34. #include <linux/mman.h>
  35. #include <linux/shmem_fs.h>
  36. #include <linux/security.h>
  37. #include <linux/syscalls.h>
  38. #include <linux/audit.h>
  39. #include <linux/capability.h>
  40. #include <linux/ptrace.h>
  41. #include <linux/seq_file.h>
  42. #include <linux/rwsem.h>
  43. #include <linux/nsproxy.h>
  44. #include <linux/mount.h>
  45. #include <linux/ipc_namespace.h>
  46. #include <linux/rhashtable.h>
  47. #include <linux/nstree.h>
  48. #include <linux/uaccess.h>
  49. #include "util.h"
  50. struct shmid_kernel /* private to the kernel */
  51. {
  52. struct kern_ipc_perm shm_perm;
  53. struct file *shm_file;
  54. unsigned long shm_nattch;
  55. unsigned long shm_segsz;
  56. time64_t shm_atim;
  57. time64_t shm_dtim;
  58. time64_t shm_ctim;
  59. struct pid *shm_cprid;
  60. struct pid *shm_lprid;
  61. struct ucounts *mlock_ucounts;
  62. /*
  63. * The task created the shm object, for
  64. * task_lock(shp->shm_creator)
  65. */
  66. struct task_struct *shm_creator;
  67. /*
  68. * List by creator. task_lock(->shm_creator) required for read/write.
  69. * If list_empty(), then the creator is dead already.
  70. */
  71. struct list_head shm_clist;
  72. struct ipc_namespace *ns;
  73. } __randomize_layout;
  74. /* shm_mode upper byte flags */
  75. #define SHM_DEST 01000 /* segment will be destroyed on last detach */
  76. #define SHM_LOCKED 02000 /* segment will not be swapped */
  77. struct shm_file_data {
  78. int id;
  79. struct ipc_namespace *ns;
  80. struct file *file;
  81. const struct vm_operations_struct *vm_ops;
  82. };
  83. #define shm_file_data(file) (*((struct shm_file_data **)&(file)->private_data))
  84. static const struct file_operations shm_file_operations;
  85. static const struct vm_operations_struct shm_vm_ops;
  86. #define shm_ids(ns) ((ns)->ids[IPC_SHM_IDS])
  87. #define shm_unlock(shp) \
  88. ipc_unlock(&(shp)->shm_perm)
  89. static int newseg(struct ipc_namespace *, struct ipc_params *);
  90. static void shm_open(struct vm_area_struct *vma);
  91. static void shm_close(struct vm_area_struct *vma);
  92. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp);
  93. #ifdef CONFIG_PROC_FS
  94. static int sysvipc_shm_proc_show(struct seq_file *s, void *it);
  95. #endif
  96. void shm_init_ns(struct ipc_namespace *ns)
  97. {
  98. ns->shm_ctlmax = SHMMAX;
  99. ns->shm_ctlall = SHMALL;
  100. ns->shm_ctlmni = SHMMNI;
  101. ns->shm_rmid_forced = 0;
  102. ns->shm_tot = 0;
  103. ipc_init_ids(&shm_ids(ns));
  104. }
  105. /*
  106. * Called with shm_ids.rwsem (writer) and the shp structure locked.
  107. * Only shm_ids.rwsem remains locked on exit.
  108. */
  109. static void do_shm_rmid(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
  110. {
  111. struct shmid_kernel *shp;
  112. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  113. WARN_ON(ns != shp->ns);
  114. if (shp->shm_nattch) {
  115. shp->shm_perm.mode |= SHM_DEST;
  116. /* Do not find it any more */
  117. ipc_set_key_private(&shm_ids(ns), &shp->shm_perm);
  118. shm_unlock(shp);
  119. } else
  120. shm_destroy(ns, shp);
  121. }
  122. #ifdef CONFIG_IPC_NS
  123. void shm_exit_ns(struct ipc_namespace *ns)
  124. {
  125. free_ipcs(ns, &shm_ids(ns), do_shm_rmid);
  126. idr_destroy(&ns->ids[IPC_SHM_IDS].ipcs_idr);
  127. rhashtable_destroy(&ns->ids[IPC_SHM_IDS].key_ht);
  128. }
  129. #endif
  130. static int __init ipc_ns_init(void)
  131. {
  132. shm_init_ns(&init_ipc_ns);
  133. ns_tree_add(&init_ipc_ns);
  134. return 0;
  135. }
  136. pure_initcall(ipc_ns_init);
  137. void __init shm_init(void)
  138. {
  139. ipc_init_proc_interface("sysvipc/shm",
  140. #if BITS_PER_LONG <= 32
  141. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  142. #else
  143. " key shmid perms size cpid lpid nattch uid gid cuid cgid atime dtime ctime rss swap\n",
  144. #endif
  145. IPC_SHM_IDS, sysvipc_shm_proc_show);
  146. }
  147. static inline struct shmid_kernel *shm_obtain_object(struct ipc_namespace *ns, int id)
  148. {
  149. struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
  150. if (IS_ERR(ipcp))
  151. return ERR_CAST(ipcp);
  152. return container_of(ipcp, struct shmid_kernel, shm_perm);
  153. }
  154. static inline struct shmid_kernel *shm_obtain_object_check(struct ipc_namespace *ns, int id)
  155. {
  156. struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&shm_ids(ns), id);
  157. if (IS_ERR(ipcp))
  158. return ERR_CAST(ipcp);
  159. return container_of(ipcp, struct shmid_kernel, shm_perm);
  160. }
  161. /*
  162. * shm_lock_(check_) routines are called in the paths where the rwsem
  163. * is not necessarily held.
  164. */
  165. static inline struct shmid_kernel *shm_lock(struct ipc_namespace *ns, int id)
  166. {
  167. struct kern_ipc_perm *ipcp;
  168. rcu_read_lock();
  169. ipcp = ipc_obtain_object_idr(&shm_ids(ns), id);
  170. if (IS_ERR(ipcp))
  171. goto err;
  172. ipc_lock_object(ipcp);
  173. /*
  174. * ipc_rmid() may have already freed the ID while ipc_lock_object()
  175. * was spinning: here verify that the structure is still valid.
  176. * Upon races with RMID, return -EIDRM, thus indicating that
  177. * the ID points to a removed identifier.
  178. */
  179. if (ipc_valid_object(ipcp)) {
  180. /* return a locked ipc object upon success */
  181. return container_of(ipcp, struct shmid_kernel, shm_perm);
  182. }
  183. ipc_unlock_object(ipcp);
  184. ipcp = ERR_PTR(-EIDRM);
  185. err:
  186. rcu_read_unlock();
  187. /*
  188. * Callers of shm_lock() must validate the status of the returned ipc
  189. * object pointer and error out as appropriate.
  190. */
  191. return ERR_CAST(ipcp);
  192. }
  193. static inline void shm_lock_by_ptr(struct shmid_kernel *ipcp)
  194. {
  195. rcu_read_lock();
  196. ipc_lock_object(&ipcp->shm_perm);
  197. }
  198. static void shm_rcu_free(struct rcu_head *head)
  199. {
  200. struct kern_ipc_perm *ptr = container_of(head, struct kern_ipc_perm,
  201. rcu);
  202. struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
  203. shm_perm);
  204. security_shm_free(&shp->shm_perm);
  205. kfree(shp);
  206. }
  207. /*
  208. * It has to be called with shp locked.
  209. * It must be called before ipc_rmid()
  210. */
  211. static inline void shm_clist_rm(struct shmid_kernel *shp)
  212. {
  213. struct task_struct *creator;
  214. /* ensure that shm_creator does not disappear */
  215. rcu_read_lock();
  216. /*
  217. * A concurrent exit_shm may do a list_del_init() as well.
  218. * Just do nothing if exit_shm already did the work
  219. */
  220. if (!list_empty(&shp->shm_clist)) {
  221. /*
  222. * shp->shm_creator is guaranteed to be valid *only*
  223. * if shp->shm_clist is not empty.
  224. */
  225. creator = shp->shm_creator;
  226. task_lock(creator);
  227. /*
  228. * list_del_init() is a nop if the entry was already removed
  229. * from the list.
  230. */
  231. list_del_init(&shp->shm_clist);
  232. task_unlock(creator);
  233. }
  234. rcu_read_unlock();
  235. }
  236. static inline void shm_rmid(struct shmid_kernel *s)
  237. {
  238. shm_clist_rm(s);
  239. ipc_rmid(&shm_ids(s->ns), &s->shm_perm);
  240. }
  241. static int __shm_open(struct shm_file_data *sfd)
  242. {
  243. struct shmid_kernel *shp;
  244. shp = shm_lock(sfd->ns, sfd->id);
  245. if (IS_ERR(shp))
  246. return PTR_ERR(shp);
  247. if (shp->shm_file != sfd->file) {
  248. /* ID was reused */
  249. shm_unlock(shp);
  250. return -EINVAL;
  251. }
  252. shp->shm_atim = ktime_get_real_seconds();
  253. ipc_update_pid(&shp->shm_lprid, task_tgid(current));
  254. shp->shm_nattch++;
  255. shm_unlock(shp);
  256. return 0;
  257. }
  258. /* This is called by fork, once for every shm attach. */
  259. static void shm_open(struct vm_area_struct *vma)
  260. {
  261. struct file *file = vma->vm_file;
  262. struct shm_file_data *sfd = shm_file_data(file);
  263. int err;
  264. /* Always call underlying open if present */
  265. if (sfd->vm_ops->open)
  266. sfd->vm_ops->open(vma);
  267. err = __shm_open(sfd);
  268. /*
  269. * We raced in the idr lookup or with shm_destroy().
  270. * Either way, the ID is busted.
  271. */
  272. WARN_ON_ONCE(err);
  273. }
  274. /*
  275. * shm_destroy - free the struct shmid_kernel
  276. *
  277. * @ns: namespace
  278. * @shp: struct to free
  279. *
  280. * It has to be called with shp and shm_ids.rwsem (writer) locked,
  281. * but returns with shp unlocked and freed.
  282. */
  283. static void shm_destroy(struct ipc_namespace *ns, struct shmid_kernel *shp)
  284. {
  285. struct file *shm_file;
  286. shm_file = shp->shm_file;
  287. shp->shm_file = NULL;
  288. ns->shm_tot -= (shp->shm_segsz + PAGE_SIZE - 1) >> PAGE_SHIFT;
  289. shm_rmid(shp);
  290. shm_unlock(shp);
  291. if (!is_file_hugepages(shm_file))
  292. shmem_lock(shm_file, 0, shp->mlock_ucounts);
  293. fput(shm_file);
  294. ipc_update_pid(&shp->shm_cprid, NULL);
  295. ipc_update_pid(&shp->shm_lprid, NULL);
  296. ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
  297. }
  298. /*
  299. * shm_may_destroy - identifies whether shm segment should be destroyed now
  300. *
  301. * Returns true if and only if there are no active users of the segment and
  302. * one of the following is true:
  303. *
  304. * 1) shmctl(id, IPC_RMID, NULL) was called for this shp
  305. *
  306. * 2) sysctl kernel.shm_rmid_forced is set to 1.
  307. */
  308. static bool shm_may_destroy(struct shmid_kernel *shp)
  309. {
  310. return (shp->shm_nattch == 0) &&
  311. (shp->ns->shm_rmid_forced ||
  312. (shp->shm_perm.mode & SHM_DEST));
  313. }
  314. /*
  315. * remove the attach descriptor vma.
  316. * free memory for segment if it is marked destroyed.
  317. * The descriptor has already been removed from the current->mm->mmap list
  318. * and will later be kfree()d.
  319. */
  320. static void __shm_close(struct shm_file_data *sfd)
  321. {
  322. struct shmid_kernel *shp;
  323. struct ipc_namespace *ns = sfd->ns;
  324. down_write(&shm_ids(ns).rwsem);
  325. /* remove from the list of attaches of the shm segment */
  326. shp = shm_lock(ns, sfd->id);
  327. /*
  328. * We raced in the idr lookup or with shm_destroy().
  329. * Either way, the ID is busted.
  330. */
  331. if (WARN_ON_ONCE(IS_ERR(shp)))
  332. goto done; /* no-op */
  333. ipc_update_pid(&shp->shm_lprid, task_tgid(current));
  334. shp->shm_dtim = ktime_get_real_seconds();
  335. shp->shm_nattch--;
  336. if (shm_may_destroy(shp))
  337. shm_destroy(ns, shp);
  338. else
  339. shm_unlock(shp);
  340. done:
  341. up_write(&shm_ids(ns).rwsem);
  342. }
  343. static void shm_close(struct vm_area_struct *vma)
  344. {
  345. struct file *file = vma->vm_file;
  346. struct shm_file_data *sfd = shm_file_data(file);
  347. /* Always call underlying close if present */
  348. if (sfd->vm_ops->close)
  349. sfd->vm_ops->close(vma);
  350. __shm_close(sfd);
  351. }
  352. /* Called with ns->shm_ids(ns).rwsem locked */
  353. static int shm_try_destroy_orphaned(int id, void *p, void *data)
  354. {
  355. struct ipc_namespace *ns = data;
  356. struct kern_ipc_perm *ipcp = p;
  357. struct shmid_kernel *shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  358. /*
  359. * We want to destroy segments without users and with already
  360. * exit'ed originating process.
  361. *
  362. * As shp->* are changed under rwsem, it's safe to skip shp locking.
  363. */
  364. if (!list_empty(&shp->shm_clist))
  365. return 0;
  366. if (shm_may_destroy(shp)) {
  367. shm_lock_by_ptr(shp);
  368. shm_destroy(ns, shp);
  369. }
  370. return 0;
  371. }
  372. void shm_destroy_orphaned(struct ipc_namespace *ns)
  373. {
  374. down_write(&shm_ids(ns).rwsem);
  375. if (shm_ids(ns).in_use) {
  376. rcu_read_lock();
  377. idr_for_each(&shm_ids(ns).ipcs_idr, &shm_try_destroy_orphaned, ns);
  378. rcu_read_unlock();
  379. }
  380. up_write(&shm_ids(ns).rwsem);
  381. }
  382. /* Locking assumes this will only be called with task == current */
  383. void exit_shm(struct task_struct *task)
  384. {
  385. for (;;) {
  386. struct shmid_kernel *shp;
  387. struct ipc_namespace *ns;
  388. task_lock(task);
  389. if (list_empty(&task->sysvshm.shm_clist)) {
  390. task_unlock(task);
  391. break;
  392. }
  393. shp = list_first_entry(&task->sysvshm.shm_clist, struct shmid_kernel,
  394. shm_clist);
  395. /*
  396. * 1) Get pointer to the ipc namespace. It is worth to say
  397. * that this pointer is guaranteed to be valid because
  398. * shp lifetime is always shorter than namespace lifetime
  399. * in which shp lives.
  400. * We taken task_lock it means that shp won't be freed.
  401. */
  402. ns = shp->ns;
  403. /*
  404. * 2) If kernel.shm_rmid_forced is not set then only keep track of
  405. * which shmids are orphaned, so that a later set of the sysctl
  406. * can clean them up.
  407. */
  408. if (!ns->shm_rmid_forced)
  409. goto unlink_continue;
  410. /*
  411. * 3) get a reference to the namespace.
  412. * The refcount could be already 0. If it is 0, then
  413. * the shm objects will be free by free_ipc_work().
  414. */
  415. ns = get_ipc_ns_not_zero(ns);
  416. if (!ns) {
  417. unlink_continue:
  418. list_del_init(&shp->shm_clist);
  419. task_unlock(task);
  420. continue;
  421. }
  422. /*
  423. * 4) get a reference to shp.
  424. * This cannot fail: shm_clist_rm() is called before
  425. * ipc_rmid(), thus the refcount cannot be 0.
  426. */
  427. WARN_ON(!ipc_rcu_getref(&shp->shm_perm));
  428. /*
  429. * 5) unlink the shm segment from the list of segments
  430. * created by current.
  431. * This must be done last. After unlinking,
  432. * only the refcounts obtained above prevent IPC_RMID
  433. * from destroying the segment or the namespace.
  434. */
  435. list_del_init(&shp->shm_clist);
  436. task_unlock(task);
  437. /*
  438. * 6) we have all references
  439. * Thus lock & if needed destroy shp.
  440. */
  441. down_write(&shm_ids(ns).rwsem);
  442. shm_lock_by_ptr(shp);
  443. /*
  444. * rcu_read_lock was implicitly taken in shm_lock_by_ptr, it's
  445. * safe to call ipc_rcu_putref here
  446. */
  447. ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
  448. if (ipc_valid_object(&shp->shm_perm)) {
  449. if (shm_may_destroy(shp))
  450. shm_destroy(ns, shp);
  451. else
  452. shm_unlock(shp);
  453. } else {
  454. /*
  455. * Someone else deleted the shp from namespace
  456. * idr/kht while we have waited.
  457. * Just unlock and continue.
  458. */
  459. shm_unlock(shp);
  460. }
  461. up_write(&shm_ids(ns).rwsem);
  462. put_ipc_ns(ns); /* paired with get_ipc_ns_not_zero */
  463. }
  464. }
  465. static vm_fault_t shm_fault(struct vm_fault *vmf)
  466. {
  467. struct file *file = vmf->vma->vm_file;
  468. struct shm_file_data *sfd = shm_file_data(file);
  469. return sfd->vm_ops->fault(vmf);
  470. }
  471. static int shm_may_split(struct vm_area_struct *vma, unsigned long addr)
  472. {
  473. struct file *file = vma->vm_file;
  474. struct shm_file_data *sfd = shm_file_data(file);
  475. if (sfd->vm_ops->may_split)
  476. return sfd->vm_ops->may_split(vma, addr);
  477. return 0;
  478. }
  479. static unsigned long shm_pagesize(struct vm_area_struct *vma)
  480. {
  481. struct file *file = vma->vm_file;
  482. struct shm_file_data *sfd = shm_file_data(file);
  483. if (sfd->vm_ops->pagesize)
  484. return sfd->vm_ops->pagesize(vma);
  485. return PAGE_SIZE;
  486. }
  487. #ifdef CONFIG_NUMA
  488. static int shm_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  489. {
  490. struct shm_file_data *sfd = shm_file_data(vma->vm_file);
  491. int err = 0;
  492. if (sfd->vm_ops->set_policy)
  493. err = sfd->vm_ops->set_policy(vma, mpol);
  494. return err;
  495. }
  496. static struct mempolicy *shm_get_policy(struct vm_area_struct *vma,
  497. unsigned long addr, pgoff_t *ilx)
  498. {
  499. struct shm_file_data *sfd = shm_file_data(vma->vm_file);
  500. struct mempolicy *mpol = vma->vm_policy;
  501. if (sfd->vm_ops->get_policy)
  502. mpol = sfd->vm_ops->get_policy(vma, addr, ilx);
  503. return mpol;
  504. }
  505. #endif
  506. static int shm_mmap(struct file *file, struct vm_area_struct *vma)
  507. {
  508. struct shm_file_data *sfd = shm_file_data(file);
  509. int ret;
  510. /*
  511. * In case of remap_file_pages() emulation, the file can represent an
  512. * IPC ID that was removed, and possibly even reused by another shm
  513. * segment already. Propagate this case as an error to caller.
  514. */
  515. ret = __shm_open(sfd);
  516. if (ret)
  517. return ret;
  518. ret = vfs_mmap(sfd->file, vma);
  519. if (ret) {
  520. __shm_close(sfd);
  521. return ret;
  522. }
  523. sfd->vm_ops = vma->vm_ops;
  524. #ifdef CONFIG_MMU
  525. WARN_ON(!sfd->vm_ops->fault);
  526. #endif
  527. vma->vm_ops = &shm_vm_ops;
  528. return 0;
  529. }
  530. static int shm_release(struct inode *ino, struct file *file)
  531. {
  532. struct shm_file_data *sfd = shm_file_data(file);
  533. put_ipc_ns(sfd->ns);
  534. fput(sfd->file);
  535. shm_file_data(file) = NULL;
  536. kfree(sfd);
  537. return 0;
  538. }
  539. static int shm_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  540. {
  541. struct shm_file_data *sfd = shm_file_data(file);
  542. if (!sfd->file->f_op->fsync)
  543. return -EINVAL;
  544. return sfd->file->f_op->fsync(sfd->file, start, end, datasync);
  545. }
  546. static long shm_fallocate(struct file *file, int mode, loff_t offset,
  547. loff_t len)
  548. {
  549. struct shm_file_data *sfd = shm_file_data(file);
  550. if (!sfd->file->f_op->fallocate)
  551. return -EOPNOTSUPP;
  552. return sfd->file->f_op->fallocate(file, mode, offset, len);
  553. }
  554. static unsigned long shm_get_unmapped_area(struct file *file,
  555. unsigned long addr, unsigned long len, unsigned long pgoff,
  556. unsigned long flags)
  557. {
  558. struct shm_file_data *sfd = shm_file_data(file);
  559. return sfd->file->f_op->get_unmapped_area(sfd->file, addr, len,
  560. pgoff, flags);
  561. }
  562. static const struct file_operations shm_file_operations = {
  563. .mmap = shm_mmap,
  564. .fsync = shm_fsync,
  565. .release = shm_release,
  566. .get_unmapped_area = shm_get_unmapped_area,
  567. .llseek = noop_llseek,
  568. .fallocate = shm_fallocate,
  569. };
  570. /*
  571. * shm_file_operations_huge is now identical to shm_file_operations
  572. * except for fop_flags
  573. */
  574. static const struct file_operations shm_file_operations_huge = {
  575. .mmap = shm_mmap,
  576. .fsync = shm_fsync,
  577. .release = shm_release,
  578. .get_unmapped_area = shm_get_unmapped_area,
  579. .llseek = noop_llseek,
  580. .fallocate = shm_fallocate,
  581. .fop_flags = FOP_HUGE_PAGES,
  582. };
  583. static const struct vm_operations_struct shm_vm_ops = {
  584. .open = shm_open, /* callback for a new vm-area open */
  585. .close = shm_close, /* callback for when the vm-area is released */
  586. .fault = shm_fault,
  587. .may_split = shm_may_split,
  588. .pagesize = shm_pagesize,
  589. #if defined(CONFIG_NUMA)
  590. .set_policy = shm_set_policy,
  591. .get_policy = shm_get_policy,
  592. #endif
  593. };
  594. /**
  595. * newseg - Create a new shared memory segment
  596. * @ns: namespace
  597. * @params: ptr to the structure that contains key, size and shmflg
  598. *
  599. * Called with shm_ids.rwsem held as a writer.
  600. */
  601. static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
  602. {
  603. key_t key = params->key;
  604. int shmflg = params->flg;
  605. size_t size = params->u.size;
  606. int error;
  607. struct shmid_kernel *shp;
  608. size_t numpages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  609. const bool has_no_reserve = shmflg & SHM_NORESERVE;
  610. vma_flags_t acctflag = EMPTY_VMA_FLAGS;
  611. struct file *file;
  612. char name[13];
  613. if (size < SHMMIN || size > ns->shm_ctlmax)
  614. return -EINVAL;
  615. if (numpages << PAGE_SHIFT < size)
  616. return -ENOSPC;
  617. if (ns->shm_tot + numpages < ns->shm_tot ||
  618. ns->shm_tot + numpages > ns->shm_ctlall)
  619. return -ENOSPC;
  620. shp = kmalloc_obj(*shp, GFP_KERNEL_ACCOUNT);
  621. if (unlikely(!shp))
  622. return -ENOMEM;
  623. shp->shm_perm.key = key;
  624. shp->shm_perm.mode = (shmflg & S_IRWXUGO);
  625. shp->mlock_ucounts = NULL;
  626. shp->shm_perm.security = NULL;
  627. error = security_shm_alloc(&shp->shm_perm);
  628. if (error) {
  629. kfree(shp);
  630. return error;
  631. }
  632. sprintf(name, "SYSV%08x", key);
  633. if (shmflg & SHM_HUGETLB) {
  634. struct hstate *hs;
  635. size_t hugesize;
  636. hs = hstate_sizelog((shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
  637. if (!hs) {
  638. error = -EINVAL;
  639. goto no_file;
  640. }
  641. hugesize = ALIGN(size, huge_page_size(hs));
  642. /* hugetlb_file_setup applies strict accounting */
  643. if (has_no_reserve)
  644. vma_flags_set(&acctflag, VMA_NORESERVE_BIT);
  645. file = hugetlb_file_setup(name, hugesize, acctflag,
  646. HUGETLB_SHMFS_INODE, (shmflg >> SHM_HUGE_SHIFT) & SHM_HUGE_MASK);
  647. } else {
  648. /*
  649. * Do not allow no accounting for OVERCOMMIT_NEVER, even
  650. * if it's asked for.
  651. */
  652. if (has_no_reserve && sysctl_overcommit_memory != OVERCOMMIT_NEVER)
  653. vma_flags_set(&acctflag, VMA_NORESERVE_BIT);
  654. file = shmem_kernel_file_setup(name, size, acctflag);
  655. }
  656. error = PTR_ERR(file);
  657. if (IS_ERR(file))
  658. goto no_file;
  659. shp->shm_cprid = get_pid(task_tgid(current));
  660. shp->shm_lprid = NULL;
  661. shp->shm_atim = shp->shm_dtim = 0;
  662. shp->shm_ctim = ktime_get_real_seconds();
  663. shp->shm_segsz = size;
  664. shp->shm_nattch = 0;
  665. shp->shm_file = file;
  666. shp->shm_creator = current;
  667. /* ipc_addid() locks shp upon success. */
  668. error = ipc_addid(&shm_ids(ns), &shp->shm_perm, ns->shm_ctlmni);
  669. if (error < 0)
  670. goto no_id;
  671. shp->ns = ns;
  672. task_lock(current);
  673. list_add(&shp->shm_clist, &current->sysvshm.shm_clist);
  674. task_unlock(current);
  675. /*
  676. * shmid gets reported as "inode#" in /proc/pid/maps.
  677. * proc-ps tools use this. Changing this will break them.
  678. */
  679. file_inode(file)->i_ino = shp->shm_perm.id;
  680. ns->shm_tot += numpages;
  681. error = shp->shm_perm.id;
  682. ipc_unlock_object(&shp->shm_perm);
  683. rcu_read_unlock();
  684. return error;
  685. no_id:
  686. ipc_update_pid(&shp->shm_cprid, NULL);
  687. ipc_update_pid(&shp->shm_lprid, NULL);
  688. fput(file);
  689. ipc_rcu_putref(&shp->shm_perm, shm_rcu_free);
  690. return error;
  691. no_file:
  692. call_rcu(&shp->shm_perm.rcu, shm_rcu_free);
  693. return error;
  694. }
  695. /*
  696. * Called with shm_ids.rwsem and ipcp locked.
  697. */
  698. static int shm_more_checks(struct kern_ipc_perm *ipcp, struct ipc_params *params)
  699. {
  700. struct shmid_kernel *shp;
  701. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  702. if (shp->shm_segsz < params->u.size)
  703. return -EINVAL;
  704. return 0;
  705. }
  706. long ksys_shmget(key_t key, size_t size, int shmflg)
  707. {
  708. struct ipc_namespace *ns;
  709. static const struct ipc_ops shm_ops = {
  710. .getnew = newseg,
  711. .associate = security_shm_associate,
  712. .more_checks = shm_more_checks,
  713. };
  714. struct ipc_params shm_params;
  715. ns = current->nsproxy->ipc_ns;
  716. shm_params.key = key;
  717. shm_params.flg = shmflg;
  718. shm_params.u.size = size;
  719. return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params);
  720. }
  721. SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg)
  722. {
  723. return ksys_shmget(key, size, shmflg);
  724. }
  725. static inline unsigned long copy_shmid_to_user(void __user *buf, struct shmid64_ds *in, int version)
  726. {
  727. switch (version) {
  728. case IPC_64:
  729. return copy_to_user(buf, in, sizeof(*in));
  730. case IPC_OLD:
  731. {
  732. struct shmid_ds out;
  733. memset(&out, 0, sizeof(out));
  734. ipc64_perm_to_ipc_perm(&in->shm_perm, &out.shm_perm);
  735. out.shm_segsz = in->shm_segsz;
  736. out.shm_atime = in->shm_atime;
  737. out.shm_dtime = in->shm_dtime;
  738. out.shm_ctime = in->shm_ctime;
  739. out.shm_cpid = in->shm_cpid;
  740. out.shm_lpid = in->shm_lpid;
  741. out.shm_nattch = in->shm_nattch;
  742. return copy_to_user(buf, &out, sizeof(out));
  743. }
  744. default:
  745. return -EINVAL;
  746. }
  747. }
  748. static inline unsigned long
  749. copy_shmid_from_user(struct shmid64_ds *out, void __user *buf, int version)
  750. {
  751. switch (version) {
  752. case IPC_64:
  753. if (copy_from_user(out, buf, sizeof(*out)))
  754. return -EFAULT;
  755. return 0;
  756. case IPC_OLD:
  757. {
  758. struct shmid_ds tbuf_old;
  759. if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
  760. return -EFAULT;
  761. out->shm_perm.uid = tbuf_old.shm_perm.uid;
  762. out->shm_perm.gid = tbuf_old.shm_perm.gid;
  763. out->shm_perm.mode = tbuf_old.shm_perm.mode;
  764. return 0;
  765. }
  766. default:
  767. return -EINVAL;
  768. }
  769. }
  770. static inline unsigned long copy_shminfo_to_user(void __user *buf, struct shminfo64 *in, int version)
  771. {
  772. switch (version) {
  773. case IPC_64:
  774. return copy_to_user(buf, in, sizeof(*in));
  775. case IPC_OLD:
  776. {
  777. struct shminfo out;
  778. if (in->shmmax > INT_MAX)
  779. out.shmmax = INT_MAX;
  780. else
  781. out.shmmax = (int)in->shmmax;
  782. out.shmmin = in->shmmin;
  783. out.shmmni = in->shmmni;
  784. out.shmseg = in->shmseg;
  785. out.shmall = in->shmall;
  786. return copy_to_user(buf, &out, sizeof(out));
  787. }
  788. default:
  789. return -EINVAL;
  790. }
  791. }
  792. /*
  793. * Calculate and add used RSS and swap pages of a shm.
  794. * Called with shm_ids.rwsem held as a reader
  795. */
  796. static void shm_add_rss_swap(struct shmid_kernel *shp,
  797. unsigned long *rss_add, unsigned long *swp_add)
  798. {
  799. struct inode *inode;
  800. inode = file_inode(shp->shm_file);
  801. if (is_file_hugepages(shp->shm_file)) {
  802. struct address_space *mapping = inode->i_mapping;
  803. struct hstate *h = hstate_file(shp->shm_file);
  804. *rss_add += pages_per_huge_page(h) * mapping->nrpages;
  805. } else {
  806. #ifdef CONFIG_SHMEM
  807. struct shmem_inode_info *info = SHMEM_I(inode);
  808. spin_lock_irq(&info->lock);
  809. *rss_add += inode->i_mapping->nrpages;
  810. *swp_add += info->swapped;
  811. spin_unlock_irq(&info->lock);
  812. #else
  813. *rss_add += inode->i_mapping->nrpages;
  814. #endif
  815. }
  816. }
  817. /*
  818. * Called with shm_ids.rwsem held as a reader
  819. */
  820. static void shm_get_stat(struct ipc_namespace *ns, unsigned long *rss,
  821. unsigned long *swp)
  822. {
  823. int next_id;
  824. int total, in_use;
  825. *rss = 0;
  826. *swp = 0;
  827. in_use = shm_ids(ns).in_use;
  828. for (total = 0, next_id = 0; total < in_use; next_id++) {
  829. struct kern_ipc_perm *ipc;
  830. struct shmid_kernel *shp;
  831. ipc = idr_find(&shm_ids(ns).ipcs_idr, next_id);
  832. if (ipc == NULL)
  833. continue;
  834. shp = container_of(ipc, struct shmid_kernel, shm_perm);
  835. shm_add_rss_swap(shp, rss, swp);
  836. total++;
  837. }
  838. }
  839. /*
  840. * This function handles some shmctl commands which require the rwsem
  841. * to be held in write mode.
  842. * NOTE: no locks must be held, the rwsem is taken inside this function.
  843. */
  844. static int shmctl_down(struct ipc_namespace *ns, int shmid, int cmd,
  845. struct shmid64_ds *shmid64)
  846. {
  847. struct kern_ipc_perm *ipcp;
  848. struct shmid_kernel *shp;
  849. int err;
  850. down_write(&shm_ids(ns).rwsem);
  851. rcu_read_lock();
  852. ipcp = ipcctl_obtain_check(ns, &shm_ids(ns), shmid, cmd,
  853. &shmid64->shm_perm, 0);
  854. if (IS_ERR(ipcp)) {
  855. err = PTR_ERR(ipcp);
  856. goto out_unlock1;
  857. }
  858. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  859. err = security_shm_shmctl(&shp->shm_perm, cmd);
  860. if (err)
  861. goto out_unlock1;
  862. switch (cmd) {
  863. case IPC_RMID:
  864. ipc_lock_object(&shp->shm_perm);
  865. /* do_shm_rmid unlocks the ipc object and rcu */
  866. do_shm_rmid(ns, ipcp);
  867. goto out_up;
  868. case IPC_SET:
  869. ipc_lock_object(&shp->shm_perm);
  870. err = ipc_update_perm(&shmid64->shm_perm, ipcp);
  871. if (err)
  872. goto out_unlock0;
  873. shp->shm_ctim = ktime_get_real_seconds();
  874. break;
  875. default:
  876. err = -EINVAL;
  877. goto out_unlock1;
  878. }
  879. out_unlock0:
  880. ipc_unlock_object(&shp->shm_perm);
  881. out_unlock1:
  882. rcu_read_unlock();
  883. out_up:
  884. up_write(&shm_ids(ns).rwsem);
  885. return err;
  886. }
  887. static int shmctl_ipc_info(struct ipc_namespace *ns,
  888. struct shminfo64 *shminfo)
  889. {
  890. int err = security_shm_shmctl(NULL, IPC_INFO);
  891. if (!err) {
  892. memset(shminfo, 0, sizeof(*shminfo));
  893. shminfo->shmmni = shminfo->shmseg = ns->shm_ctlmni;
  894. shminfo->shmmax = ns->shm_ctlmax;
  895. shminfo->shmall = ns->shm_ctlall;
  896. shminfo->shmmin = SHMMIN;
  897. down_read(&shm_ids(ns).rwsem);
  898. err = ipc_get_maxidx(&shm_ids(ns));
  899. up_read(&shm_ids(ns).rwsem);
  900. if (err < 0)
  901. err = 0;
  902. }
  903. return err;
  904. }
  905. static int shmctl_shm_info(struct ipc_namespace *ns,
  906. struct shm_info *shm_info)
  907. {
  908. int err = security_shm_shmctl(NULL, SHM_INFO);
  909. if (!err) {
  910. memset(shm_info, 0, sizeof(*shm_info));
  911. down_read(&shm_ids(ns).rwsem);
  912. shm_info->used_ids = shm_ids(ns).in_use;
  913. shm_get_stat(ns, &shm_info->shm_rss, &shm_info->shm_swp);
  914. shm_info->shm_tot = ns->shm_tot;
  915. shm_info->swap_attempts = 0;
  916. shm_info->swap_successes = 0;
  917. err = ipc_get_maxidx(&shm_ids(ns));
  918. up_read(&shm_ids(ns).rwsem);
  919. if (err < 0)
  920. err = 0;
  921. }
  922. return err;
  923. }
  924. static int shmctl_stat(struct ipc_namespace *ns, int shmid,
  925. int cmd, struct shmid64_ds *tbuf)
  926. {
  927. struct shmid_kernel *shp;
  928. int err;
  929. memset(tbuf, 0, sizeof(*tbuf));
  930. rcu_read_lock();
  931. if (cmd == SHM_STAT || cmd == SHM_STAT_ANY) {
  932. shp = shm_obtain_object(ns, shmid);
  933. if (IS_ERR(shp)) {
  934. err = PTR_ERR(shp);
  935. goto out_unlock;
  936. }
  937. } else { /* IPC_STAT */
  938. shp = shm_obtain_object_check(ns, shmid);
  939. if (IS_ERR(shp)) {
  940. err = PTR_ERR(shp);
  941. goto out_unlock;
  942. }
  943. }
  944. /*
  945. * Semantically SHM_STAT_ANY ought to be identical to
  946. * that functionality provided by the /proc/sysvipc/
  947. * interface. As such, only audit these calls and
  948. * do not do traditional S_IRUGO permission checks on
  949. * the ipc object.
  950. */
  951. if (cmd == SHM_STAT_ANY)
  952. audit_ipc_obj(&shp->shm_perm);
  953. else {
  954. err = -EACCES;
  955. if (ipcperms(ns, &shp->shm_perm, S_IRUGO))
  956. goto out_unlock;
  957. }
  958. err = security_shm_shmctl(&shp->shm_perm, cmd);
  959. if (err)
  960. goto out_unlock;
  961. ipc_lock_object(&shp->shm_perm);
  962. if (!ipc_valid_object(&shp->shm_perm)) {
  963. ipc_unlock_object(&shp->shm_perm);
  964. err = -EIDRM;
  965. goto out_unlock;
  966. }
  967. kernel_to_ipc64_perm(&shp->shm_perm, &tbuf->shm_perm);
  968. tbuf->shm_segsz = shp->shm_segsz;
  969. tbuf->shm_atime = shp->shm_atim;
  970. tbuf->shm_dtime = shp->shm_dtim;
  971. tbuf->shm_ctime = shp->shm_ctim;
  972. #ifndef CONFIG_64BIT
  973. tbuf->shm_atime_high = shp->shm_atim >> 32;
  974. tbuf->shm_dtime_high = shp->shm_dtim >> 32;
  975. tbuf->shm_ctime_high = shp->shm_ctim >> 32;
  976. #endif
  977. tbuf->shm_cpid = pid_vnr(shp->shm_cprid);
  978. tbuf->shm_lpid = pid_vnr(shp->shm_lprid);
  979. tbuf->shm_nattch = shp->shm_nattch;
  980. if (cmd == IPC_STAT) {
  981. /*
  982. * As defined in SUS:
  983. * Return 0 on success
  984. */
  985. err = 0;
  986. } else {
  987. /*
  988. * SHM_STAT and SHM_STAT_ANY (both Linux specific)
  989. * Return the full id, including the sequence number
  990. */
  991. err = shp->shm_perm.id;
  992. }
  993. ipc_unlock_object(&shp->shm_perm);
  994. out_unlock:
  995. rcu_read_unlock();
  996. return err;
  997. }
  998. static int shmctl_do_lock(struct ipc_namespace *ns, int shmid, int cmd)
  999. {
  1000. struct shmid_kernel *shp;
  1001. struct file *shm_file;
  1002. int err;
  1003. rcu_read_lock();
  1004. shp = shm_obtain_object_check(ns, shmid);
  1005. if (IS_ERR(shp)) {
  1006. err = PTR_ERR(shp);
  1007. goto out_unlock1;
  1008. }
  1009. audit_ipc_obj(&(shp->shm_perm));
  1010. err = security_shm_shmctl(&shp->shm_perm, cmd);
  1011. if (err)
  1012. goto out_unlock1;
  1013. ipc_lock_object(&shp->shm_perm);
  1014. /* check if shm_destroy() is tearing down shp */
  1015. if (!ipc_valid_object(&shp->shm_perm)) {
  1016. err = -EIDRM;
  1017. goto out_unlock0;
  1018. }
  1019. if (!ns_capable(ns->user_ns, CAP_IPC_LOCK)) {
  1020. kuid_t euid = current_euid();
  1021. if (!uid_eq(euid, shp->shm_perm.uid) &&
  1022. !uid_eq(euid, shp->shm_perm.cuid)) {
  1023. err = -EPERM;
  1024. goto out_unlock0;
  1025. }
  1026. if (cmd == SHM_LOCK && !rlimit(RLIMIT_MEMLOCK)) {
  1027. err = -EPERM;
  1028. goto out_unlock0;
  1029. }
  1030. }
  1031. shm_file = shp->shm_file;
  1032. if (is_file_hugepages(shm_file))
  1033. goto out_unlock0;
  1034. if (cmd == SHM_LOCK) {
  1035. struct ucounts *ucounts = current_ucounts();
  1036. err = shmem_lock(shm_file, 1, ucounts);
  1037. if (!err && !(shp->shm_perm.mode & SHM_LOCKED)) {
  1038. shp->shm_perm.mode |= SHM_LOCKED;
  1039. shp->mlock_ucounts = ucounts;
  1040. }
  1041. goto out_unlock0;
  1042. }
  1043. /* SHM_UNLOCK */
  1044. if (!(shp->shm_perm.mode & SHM_LOCKED))
  1045. goto out_unlock0;
  1046. shmem_lock(shm_file, 0, shp->mlock_ucounts);
  1047. shp->shm_perm.mode &= ~SHM_LOCKED;
  1048. shp->mlock_ucounts = NULL;
  1049. get_file(shm_file);
  1050. ipc_unlock_object(&shp->shm_perm);
  1051. rcu_read_unlock();
  1052. shmem_unlock_mapping(shm_file->f_mapping);
  1053. fput(shm_file);
  1054. return err;
  1055. out_unlock0:
  1056. ipc_unlock_object(&shp->shm_perm);
  1057. out_unlock1:
  1058. rcu_read_unlock();
  1059. return err;
  1060. }
  1061. static long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf, int version)
  1062. {
  1063. int err;
  1064. struct ipc_namespace *ns;
  1065. struct shmid64_ds sem64;
  1066. if (cmd < 0 || shmid < 0)
  1067. return -EINVAL;
  1068. ns = current->nsproxy->ipc_ns;
  1069. switch (cmd) {
  1070. case IPC_INFO: {
  1071. struct shminfo64 shminfo;
  1072. err = shmctl_ipc_info(ns, &shminfo);
  1073. if (err < 0)
  1074. return err;
  1075. if (copy_shminfo_to_user(buf, &shminfo, version))
  1076. err = -EFAULT;
  1077. return err;
  1078. }
  1079. case SHM_INFO: {
  1080. struct shm_info shm_info;
  1081. err = shmctl_shm_info(ns, &shm_info);
  1082. if (err < 0)
  1083. return err;
  1084. if (copy_to_user(buf, &shm_info, sizeof(shm_info)))
  1085. err = -EFAULT;
  1086. return err;
  1087. }
  1088. case SHM_STAT:
  1089. case SHM_STAT_ANY:
  1090. case IPC_STAT: {
  1091. err = shmctl_stat(ns, shmid, cmd, &sem64);
  1092. if (err < 0)
  1093. return err;
  1094. if (copy_shmid_to_user(buf, &sem64, version))
  1095. err = -EFAULT;
  1096. return err;
  1097. }
  1098. case IPC_SET:
  1099. if (copy_shmid_from_user(&sem64, buf, version))
  1100. return -EFAULT;
  1101. fallthrough;
  1102. case IPC_RMID:
  1103. return shmctl_down(ns, shmid, cmd, &sem64);
  1104. case SHM_LOCK:
  1105. case SHM_UNLOCK:
  1106. return shmctl_do_lock(ns, shmid, cmd);
  1107. default:
  1108. return -EINVAL;
  1109. }
  1110. }
  1111. SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  1112. {
  1113. return ksys_shmctl(shmid, cmd, buf, IPC_64);
  1114. }
  1115. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  1116. long ksys_old_shmctl(int shmid, int cmd, struct shmid_ds __user *buf)
  1117. {
  1118. int version = ipc_parse_version(&cmd);
  1119. return ksys_shmctl(shmid, cmd, buf, version);
  1120. }
  1121. SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, struct shmid_ds __user *, buf)
  1122. {
  1123. return ksys_old_shmctl(shmid, cmd, buf);
  1124. }
  1125. #endif
  1126. #ifdef CONFIG_COMPAT
  1127. struct compat_shmid_ds {
  1128. struct compat_ipc_perm shm_perm;
  1129. int shm_segsz;
  1130. old_time32_t shm_atime;
  1131. old_time32_t shm_dtime;
  1132. old_time32_t shm_ctime;
  1133. compat_ipc_pid_t shm_cpid;
  1134. compat_ipc_pid_t shm_lpid;
  1135. unsigned short shm_nattch;
  1136. unsigned short shm_unused;
  1137. compat_uptr_t shm_unused2;
  1138. compat_uptr_t shm_unused3;
  1139. };
  1140. struct compat_shminfo64 {
  1141. compat_ulong_t shmmax;
  1142. compat_ulong_t shmmin;
  1143. compat_ulong_t shmmni;
  1144. compat_ulong_t shmseg;
  1145. compat_ulong_t shmall;
  1146. compat_ulong_t __unused1;
  1147. compat_ulong_t __unused2;
  1148. compat_ulong_t __unused3;
  1149. compat_ulong_t __unused4;
  1150. };
  1151. struct compat_shm_info {
  1152. compat_int_t used_ids;
  1153. compat_ulong_t shm_tot, shm_rss, shm_swp;
  1154. compat_ulong_t swap_attempts, swap_successes;
  1155. };
  1156. static int copy_compat_shminfo_to_user(void __user *buf, struct shminfo64 *in,
  1157. int version)
  1158. {
  1159. if (in->shmmax > INT_MAX)
  1160. in->shmmax = INT_MAX;
  1161. if (version == IPC_64) {
  1162. struct compat_shminfo64 info;
  1163. memset(&info, 0, sizeof(info));
  1164. info.shmmax = in->shmmax;
  1165. info.shmmin = in->shmmin;
  1166. info.shmmni = in->shmmni;
  1167. info.shmseg = in->shmseg;
  1168. info.shmall = in->shmall;
  1169. return copy_to_user(buf, &info, sizeof(info));
  1170. } else {
  1171. struct shminfo info;
  1172. memset(&info, 0, sizeof(info));
  1173. info.shmmax = in->shmmax;
  1174. info.shmmin = in->shmmin;
  1175. info.shmmni = in->shmmni;
  1176. info.shmseg = in->shmseg;
  1177. info.shmall = in->shmall;
  1178. return copy_to_user(buf, &info, sizeof(info));
  1179. }
  1180. }
  1181. static int put_compat_shm_info(struct shm_info *ip,
  1182. struct compat_shm_info __user *uip)
  1183. {
  1184. struct compat_shm_info info;
  1185. memset(&info, 0, sizeof(info));
  1186. info.used_ids = ip->used_ids;
  1187. info.shm_tot = ip->shm_tot;
  1188. info.shm_rss = ip->shm_rss;
  1189. info.shm_swp = ip->shm_swp;
  1190. info.swap_attempts = ip->swap_attempts;
  1191. info.swap_successes = ip->swap_successes;
  1192. return copy_to_user(uip, &info, sizeof(info));
  1193. }
  1194. static int copy_compat_shmid_to_user(void __user *buf, struct shmid64_ds *in,
  1195. int version)
  1196. {
  1197. if (version == IPC_64) {
  1198. struct compat_shmid64_ds v;
  1199. memset(&v, 0, sizeof(v));
  1200. to_compat_ipc64_perm(&v.shm_perm, &in->shm_perm);
  1201. v.shm_atime = lower_32_bits(in->shm_atime);
  1202. v.shm_atime_high = upper_32_bits(in->shm_atime);
  1203. v.shm_dtime = lower_32_bits(in->shm_dtime);
  1204. v.shm_dtime_high = upper_32_bits(in->shm_dtime);
  1205. v.shm_ctime = lower_32_bits(in->shm_ctime);
  1206. v.shm_ctime_high = upper_32_bits(in->shm_ctime);
  1207. v.shm_segsz = in->shm_segsz;
  1208. v.shm_nattch = in->shm_nattch;
  1209. v.shm_cpid = in->shm_cpid;
  1210. v.shm_lpid = in->shm_lpid;
  1211. return copy_to_user(buf, &v, sizeof(v));
  1212. } else {
  1213. struct compat_shmid_ds v;
  1214. memset(&v, 0, sizeof(v));
  1215. to_compat_ipc_perm(&v.shm_perm, &in->shm_perm);
  1216. v.shm_perm.key = in->shm_perm.key;
  1217. v.shm_atime = in->shm_atime;
  1218. v.shm_dtime = in->shm_dtime;
  1219. v.shm_ctime = in->shm_ctime;
  1220. v.shm_segsz = in->shm_segsz;
  1221. v.shm_nattch = in->shm_nattch;
  1222. v.shm_cpid = in->shm_cpid;
  1223. v.shm_lpid = in->shm_lpid;
  1224. return copy_to_user(buf, &v, sizeof(v));
  1225. }
  1226. }
  1227. static int copy_compat_shmid_from_user(struct shmid64_ds *out, void __user *buf,
  1228. int version)
  1229. {
  1230. memset(out, 0, sizeof(*out));
  1231. if (version == IPC_64) {
  1232. struct compat_shmid64_ds __user *p = buf;
  1233. return get_compat_ipc64_perm(&out->shm_perm, &p->shm_perm);
  1234. } else {
  1235. struct compat_shmid_ds __user *p = buf;
  1236. return get_compat_ipc_perm(&out->shm_perm, &p->shm_perm);
  1237. }
  1238. }
  1239. static long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr, int version)
  1240. {
  1241. struct ipc_namespace *ns;
  1242. struct shmid64_ds sem64;
  1243. int err;
  1244. ns = current->nsproxy->ipc_ns;
  1245. if (cmd < 0 || shmid < 0)
  1246. return -EINVAL;
  1247. switch (cmd) {
  1248. case IPC_INFO: {
  1249. struct shminfo64 shminfo;
  1250. err = shmctl_ipc_info(ns, &shminfo);
  1251. if (err < 0)
  1252. return err;
  1253. if (copy_compat_shminfo_to_user(uptr, &shminfo, version))
  1254. err = -EFAULT;
  1255. return err;
  1256. }
  1257. case SHM_INFO: {
  1258. struct shm_info shm_info;
  1259. err = shmctl_shm_info(ns, &shm_info);
  1260. if (err < 0)
  1261. return err;
  1262. if (put_compat_shm_info(&shm_info, uptr))
  1263. err = -EFAULT;
  1264. return err;
  1265. }
  1266. case IPC_STAT:
  1267. case SHM_STAT_ANY:
  1268. case SHM_STAT:
  1269. err = shmctl_stat(ns, shmid, cmd, &sem64);
  1270. if (err < 0)
  1271. return err;
  1272. if (copy_compat_shmid_to_user(uptr, &sem64, version))
  1273. err = -EFAULT;
  1274. return err;
  1275. case IPC_SET:
  1276. if (copy_compat_shmid_from_user(&sem64, uptr, version))
  1277. return -EFAULT;
  1278. fallthrough;
  1279. case IPC_RMID:
  1280. return shmctl_down(ns, shmid, cmd, &sem64);
  1281. case SHM_LOCK:
  1282. case SHM_UNLOCK:
  1283. return shmctl_do_lock(ns, shmid, cmd);
  1284. default:
  1285. return -EINVAL;
  1286. }
  1287. return err;
  1288. }
  1289. COMPAT_SYSCALL_DEFINE3(shmctl, int, shmid, int, cmd, void __user *, uptr)
  1290. {
  1291. return compat_ksys_shmctl(shmid, cmd, uptr, IPC_64);
  1292. }
  1293. #ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
  1294. long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr)
  1295. {
  1296. int version = compat_ipc_parse_version(&cmd);
  1297. return compat_ksys_shmctl(shmid, cmd, uptr, version);
  1298. }
  1299. COMPAT_SYSCALL_DEFINE3(old_shmctl, int, shmid, int, cmd, void __user *, uptr)
  1300. {
  1301. return compat_ksys_old_shmctl(shmid, cmd, uptr);
  1302. }
  1303. #endif
  1304. #endif
  1305. /*
  1306. * Fix shmaddr, allocate descriptor, map shm, add attach descriptor to lists.
  1307. *
  1308. * NOTE! Despite the name, this is NOT a direct system call entrypoint. The
  1309. * "raddr" thing points to kernel space, and there has to be a wrapper around
  1310. * this.
  1311. */
  1312. long do_shmat(int shmid, char __user *shmaddr, int shmflg,
  1313. ulong *raddr, unsigned long shmlba)
  1314. {
  1315. struct shmid_kernel *shp;
  1316. unsigned long addr = (unsigned long)shmaddr;
  1317. unsigned long size;
  1318. struct file *file, *base;
  1319. int err;
  1320. unsigned long flags = MAP_SHARED;
  1321. unsigned long prot;
  1322. int acc_mode;
  1323. struct ipc_namespace *ns;
  1324. struct shm_file_data *sfd;
  1325. int f_flags;
  1326. unsigned long populate = 0;
  1327. err = -EINVAL;
  1328. if (shmid < 0)
  1329. goto out;
  1330. if (addr) {
  1331. if (addr & (shmlba - 1)) {
  1332. if (shmflg & SHM_RND) {
  1333. addr &= ~(shmlba - 1); /* round down */
  1334. /*
  1335. * Ensure that the round-down is non-nil
  1336. * when remapping. This can happen for
  1337. * cases when addr < shmlba.
  1338. */
  1339. if (!addr && (shmflg & SHM_REMAP))
  1340. goto out;
  1341. } else
  1342. #ifndef __ARCH_FORCE_SHMLBA
  1343. if (addr & ~PAGE_MASK)
  1344. #endif
  1345. goto out;
  1346. }
  1347. flags |= MAP_FIXED;
  1348. } else if ((shmflg & SHM_REMAP))
  1349. goto out;
  1350. if (shmflg & SHM_RDONLY) {
  1351. prot = PROT_READ;
  1352. acc_mode = S_IRUGO;
  1353. f_flags = O_RDONLY;
  1354. } else {
  1355. prot = PROT_READ | PROT_WRITE;
  1356. acc_mode = S_IRUGO | S_IWUGO;
  1357. f_flags = O_RDWR;
  1358. }
  1359. if (shmflg & SHM_EXEC) {
  1360. prot |= PROT_EXEC;
  1361. acc_mode |= S_IXUGO;
  1362. }
  1363. /*
  1364. * We cannot rely on the fs check since SYSV IPC does have an
  1365. * additional creator id...
  1366. */
  1367. ns = current->nsproxy->ipc_ns;
  1368. rcu_read_lock();
  1369. shp = shm_obtain_object_check(ns, shmid);
  1370. if (IS_ERR(shp)) {
  1371. err = PTR_ERR(shp);
  1372. goto out_unlock;
  1373. }
  1374. err = -EACCES;
  1375. if (ipcperms(ns, &shp->shm_perm, acc_mode))
  1376. goto out_unlock;
  1377. err = security_shm_shmat(&shp->shm_perm, shmaddr, shmflg);
  1378. if (err)
  1379. goto out_unlock;
  1380. ipc_lock_object(&shp->shm_perm);
  1381. /* check if shm_destroy() is tearing down shp */
  1382. if (!ipc_valid_object(&shp->shm_perm)) {
  1383. ipc_unlock_object(&shp->shm_perm);
  1384. err = -EIDRM;
  1385. goto out_unlock;
  1386. }
  1387. /*
  1388. * We need to take a reference to the real shm file to prevent the
  1389. * pointer from becoming stale in cases where the lifetime of the outer
  1390. * file extends beyond that of the shm segment. It's not usually
  1391. * possible, but it can happen during remap_file_pages() emulation as
  1392. * that unmaps the memory, then does ->mmap() via file reference only.
  1393. * We'll deny the ->mmap() if the shm segment was since removed, but to
  1394. * detect shm ID reuse we need to compare the file pointers.
  1395. */
  1396. base = get_file(shp->shm_file);
  1397. shp->shm_nattch++;
  1398. size = i_size_read(file_inode(base));
  1399. ipc_unlock_object(&shp->shm_perm);
  1400. rcu_read_unlock();
  1401. err = -ENOMEM;
  1402. sfd = kzalloc_obj(*sfd);
  1403. if (!sfd) {
  1404. fput(base);
  1405. goto out_nattch;
  1406. }
  1407. file = alloc_file_clone(base, f_flags,
  1408. is_file_hugepages(base) ?
  1409. &shm_file_operations_huge :
  1410. &shm_file_operations);
  1411. err = PTR_ERR(file);
  1412. if (IS_ERR(file)) {
  1413. kfree(sfd);
  1414. fput(base);
  1415. goto out_nattch;
  1416. }
  1417. sfd->id = shp->shm_perm.id;
  1418. sfd->ns = get_ipc_ns(ns);
  1419. sfd->file = base;
  1420. sfd->vm_ops = NULL;
  1421. file->private_data = sfd;
  1422. err = security_mmap_file(file, prot, flags);
  1423. if (err)
  1424. goto out_fput;
  1425. if (mmap_write_lock_killable(current->mm)) {
  1426. err = -EINTR;
  1427. goto out_fput;
  1428. }
  1429. if (addr && !(shmflg & SHM_REMAP)) {
  1430. err = -EINVAL;
  1431. if (addr + size < addr)
  1432. goto invalid;
  1433. if (find_vma_intersection(current->mm, addr, addr + size))
  1434. goto invalid;
  1435. }
  1436. addr = do_mmap(file, addr, size, prot, flags, 0, 0, &populate, NULL);
  1437. *raddr = addr;
  1438. err = 0;
  1439. if (IS_ERR_VALUE(addr))
  1440. err = (long)addr;
  1441. invalid:
  1442. mmap_write_unlock(current->mm);
  1443. if (populate)
  1444. mm_populate(addr, populate);
  1445. out_fput:
  1446. fput(file);
  1447. out_nattch:
  1448. down_write(&shm_ids(ns).rwsem);
  1449. shp = shm_lock(ns, shmid);
  1450. shp->shm_nattch--;
  1451. if (shm_may_destroy(shp))
  1452. shm_destroy(ns, shp);
  1453. else
  1454. shm_unlock(shp);
  1455. up_write(&shm_ids(ns).rwsem);
  1456. return err;
  1457. out_unlock:
  1458. rcu_read_unlock();
  1459. out:
  1460. return err;
  1461. }
  1462. SYSCALL_DEFINE3(shmat, int, shmid, char __user *, shmaddr, int, shmflg)
  1463. {
  1464. unsigned long ret;
  1465. long err;
  1466. err = do_shmat(shmid, shmaddr, shmflg, &ret, SHMLBA);
  1467. if (err)
  1468. return err;
  1469. force_successful_syscall_return();
  1470. return (long)ret;
  1471. }
  1472. #ifdef CONFIG_COMPAT
  1473. #ifndef COMPAT_SHMLBA
  1474. #define COMPAT_SHMLBA SHMLBA
  1475. #endif
  1476. COMPAT_SYSCALL_DEFINE3(shmat, int, shmid, compat_uptr_t, shmaddr, int, shmflg)
  1477. {
  1478. unsigned long ret;
  1479. long err;
  1480. err = do_shmat(shmid, compat_ptr(shmaddr), shmflg, &ret, COMPAT_SHMLBA);
  1481. if (err)
  1482. return err;
  1483. force_successful_syscall_return();
  1484. return (long)ret;
  1485. }
  1486. #endif
  1487. /*
  1488. * detach and kill segment if marked destroyed.
  1489. * The work is done in shm_close.
  1490. */
  1491. long ksys_shmdt(char __user *shmaddr)
  1492. {
  1493. struct mm_struct *mm = current->mm;
  1494. struct vm_area_struct *vma;
  1495. unsigned long addr = (unsigned long)shmaddr;
  1496. int retval = -EINVAL;
  1497. #ifdef CONFIG_MMU
  1498. loff_t size = 0;
  1499. struct file *file;
  1500. VMA_ITERATOR(vmi, mm, addr);
  1501. #endif
  1502. if (addr & ~PAGE_MASK)
  1503. return retval;
  1504. if (mmap_write_lock_killable(mm))
  1505. return -EINTR;
  1506. /*
  1507. * This function tries to be smart and unmap shm segments that
  1508. * were modified by partial mlock or munmap calls:
  1509. * - It first determines the size of the shm segment that should be
  1510. * unmapped: It searches for a vma that is backed by shm and that
  1511. * started at address shmaddr. It records it's size and then unmaps
  1512. * it.
  1513. * - Then it unmaps all shm vmas that started at shmaddr and that
  1514. * are within the initially determined size and that are from the
  1515. * same shm segment from which we determined the size.
  1516. * Errors from do_munmap are ignored: the function only fails if
  1517. * it's called with invalid parameters or if it's called to unmap
  1518. * a part of a vma. Both calls in this function are for full vmas,
  1519. * the parameters are directly copied from the vma itself and always
  1520. * valid - therefore do_munmap cannot fail. (famous last words?)
  1521. */
  1522. /*
  1523. * If it had been mremap()'d, the starting address would not
  1524. * match the usual checks anyway. So assume all vma's are
  1525. * above the starting address given.
  1526. */
  1527. #ifdef CONFIG_MMU
  1528. for_each_vma(vmi, vma) {
  1529. /*
  1530. * Check if the starting address would match, i.e. it's
  1531. * a fragment created by mprotect() and/or munmap(), or it
  1532. * otherwise it starts at this address with no hassles.
  1533. */
  1534. if ((vma->vm_ops == &shm_vm_ops) &&
  1535. (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
  1536. /*
  1537. * Record the file of the shm segment being
  1538. * unmapped. With mremap(), someone could place
  1539. * page from another segment but with equal offsets
  1540. * in the range we are unmapping.
  1541. */
  1542. file = vma->vm_file;
  1543. size = i_size_read(file_inode(vma->vm_file));
  1544. do_vmi_align_munmap(&vmi, vma, mm, vma->vm_start,
  1545. vma->vm_end, NULL, false);
  1546. /*
  1547. * We discovered the size of the shm segment, so
  1548. * break out of here and fall through to the next
  1549. * loop that uses the size information to stop
  1550. * searching for matching vma's.
  1551. */
  1552. retval = 0;
  1553. vma = vma_next(&vmi);
  1554. break;
  1555. }
  1556. }
  1557. /*
  1558. * We need look no further than the maximum address a fragment
  1559. * could possibly have landed at. Also cast things to loff_t to
  1560. * prevent overflows and make comparisons vs. equal-width types.
  1561. */
  1562. size = PAGE_ALIGN(size);
  1563. while (vma && (loff_t)(vma->vm_end - addr) <= size) {
  1564. /* finding a matching vma now does not alter retval */
  1565. if ((vma->vm_ops == &shm_vm_ops) &&
  1566. ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
  1567. (vma->vm_file == file)) {
  1568. do_vmi_align_munmap(&vmi, vma, mm, vma->vm_start,
  1569. vma->vm_end, NULL, false);
  1570. }
  1571. vma = vma_next(&vmi);
  1572. }
  1573. #else /* CONFIG_MMU */
  1574. vma = vma_lookup(mm, addr);
  1575. /* under NOMMU conditions, the exact address to be destroyed must be
  1576. * given
  1577. */
  1578. if (vma && vma->vm_start == addr && vma->vm_ops == &shm_vm_ops) {
  1579. do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
  1580. retval = 0;
  1581. }
  1582. #endif
  1583. mmap_write_unlock(mm);
  1584. return retval;
  1585. }
  1586. SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
  1587. {
  1588. return ksys_shmdt(shmaddr);
  1589. }
  1590. #ifdef CONFIG_PROC_FS
  1591. static int sysvipc_shm_proc_show(struct seq_file *s, void *it)
  1592. {
  1593. struct pid_namespace *pid_ns = ipc_seq_pid_ns(s);
  1594. struct user_namespace *user_ns = seq_user_ns(s);
  1595. struct kern_ipc_perm *ipcp = it;
  1596. struct shmid_kernel *shp;
  1597. unsigned long rss = 0, swp = 0;
  1598. shp = container_of(ipcp, struct shmid_kernel, shm_perm);
  1599. shm_add_rss_swap(shp, &rss, &swp);
  1600. #if BITS_PER_LONG <= 32
  1601. #define SIZE_SPEC "%10lu"
  1602. #else
  1603. #define SIZE_SPEC "%21lu"
  1604. #endif
  1605. seq_printf(s,
  1606. "%10d %10d %4o " SIZE_SPEC " %5u %5u "
  1607. "%5lu %5u %5u %5u %5u %10llu %10llu %10llu "
  1608. SIZE_SPEC " " SIZE_SPEC "\n",
  1609. shp->shm_perm.key,
  1610. shp->shm_perm.id,
  1611. shp->shm_perm.mode,
  1612. shp->shm_segsz,
  1613. pid_nr_ns(shp->shm_cprid, pid_ns),
  1614. pid_nr_ns(shp->shm_lprid, pid_ns),
  1615. shp->shm_nattch,
  1616. from_kuid_munged(user_ns, shp->shm_perm.uid),
  1617. from_kgid_munged(user_ns, shp->shm_perm.gid),
  1618. from_kuid_munged(user_ns, shp->shm_perm.cuid),
  1619. from_kgid_munged(user_ns, shp->shm_perm.cgid),
  1620. shp->shm_atim,
  1621. shp->shm_dtim,
  1622. shp->shm_ctim,
  1623. rss * PAGE_SIZE,
  1624. swp * PAGE_SIZE);
  1625. return 0;
  1626. }
  1627. #endif