shmem.c 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2011 Hugh Dickins.
  10. * Copyright (C) 2011 Google Inc.
  11. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  12. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  13. *
  14. * Extended attribute support for tmpfs:
  15. * Copyright (c) 2004, Luke Kenneth Casson Leighton <[email protected]>
  16. * Copyright (c) 2004 Red Hat, Inc., James Morris <[email protected]>
  17. *
  18. * tiny-shmem:
  19. * Copyright (c) 2004, 2008 Matt Mackall <[email protected]>
  20. *
  21. * This file is released under the GPL.
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/init.h>
  25. #include <linux/vfs.h>
  26. #include <linux/mount.h>
  27. #include <linux/ramfs.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/file.h>
  30. #include <linux/mm.h>
  31. #include <linux/export.h>
  32. #include <linux/swap.h>
  33. #include <linux/uio.h>
  34. #include <linux/khugepaged.h>
  35. static struct vfsmount *shm_mnt;
  36. #ifdef CONFIG_SHMEM
  37. /*
  38. * This virtual memory filesystem is heavily based on the ramfs. It
  39. * extends ramfs by the ability to use swap and honor resource limits
  40. * which makes it a completely usable filesystem.
  41. */
  42. #include <linux/xattr.h>
  43. #include <linux/exportfs.h>
  44. #include <linux/posix_acl.h>
  45. #include <linux/posix_acl_xattr.h>
  46. #include <linux/mman.h>
  47. #include <linux/string.h>
  48. #include <linux/slab.h>
  49. #include <linux/backing-dev.h>
  50. #include <linux/shmem_fs.h>
  51. #include <linux/writeback.h>
  52. #include <linux/blkdev.h>
  53. #include <linux/pagevec.h>
  54. #include <linux/percpu_counter.h>
  55. #include <linux/falloc.h>
  56. #include <linux/splice.h>
  57. #include <linux/security.h>
  58. #include <linux/swapops.h>
  59. #include <linux/mempolicy.h>
  60. #include <linux/namei.h>
  61. #include <linux/ctype.h>
  62. #include <linux/migrate.h>
  63. #include <linux/highmem.h>
  64. #include <linux/seq_file.h>
  65. #include <linux/magic.h>
  66. #include <linux/syscalls.h>
  67. #include <linux/fcntl.h>
  68. #include <uapi/linux/memfd.h>
  69. #include <asm/uaccess.h>
  70. #include <asm/pgtable.h>
  71. #include "internal.h"
  72. #define BLOCKS_PER_PAGE (PAGE_SIZE/512)
  73. #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
  74. /* Pretend that each entry is of this size in directory's i_size */
  75. #define BOGO_DIRENT_SIZE 20
  76. /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
  77. #define SHORT_SYMLINK_LEN 128
  78. /*
  79. * shmem_fallocate communicates with shmem_fault or shmem_writepage via
  80. * inode->i_private (with i_mutex making sure that it has only one user at
  81. * a time): we would prefer not to enlarge the shmem inode just for that.
  82. */
  83. struct shmem_falloc {
  84. wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
  85. pgoff_t start; /* start of range currently being fallocated */
  86. pgoff_t next; /* the next page offset to be fallocated */
  87. pgoff_t nr_falloced; /* how many new pages have been fallocated */
  88. pgoff_t nr_unswapped; /* how often writepage refused to swap out */
  89. };
  90. #ifdef CONFIG_TMPFS
  91. static unsigned long shmem_default_max_blocks(void)
  92. {
  93. return totalram_pages / 2;
  94. }
  95. static unsigned long shmem_default_max_inodes(void)
  96. {
  97. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  98. }
  99. #endif
  100. static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
  101. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  102. struct shmem_inode_info *info, pgoff_t index);
  103. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  104. struct page **pagep, enum sgp_type sgp,
  105. gfp_t gfp, struct mm_struct *fault_mm, int *fault_type);
  106. int shmem_getpage(struct inode *inode, pgoff_t index,
  107. struct page **pagep, enum sgp_type sgp)
  108. {
  109. return shmem_getpage_gfp(inode, index, pagep, sgp,
  110. mapping_gfp_mask(inode->i_mapping), NULL, NULL);
  111. }
  112. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  113. {
  114. return sb->s_fs_info;
  115. }
  116. /*
  117. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  118. * for shared memory and for shared anonymous (/dev/zero) mappings
  119. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  120. * consistent with the pre-accounting of private mappings ...
  121. */
  122. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  123. {
  124. return (flags & VM_NORESERVE) ?
  125. 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
  126. }
  127. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  128. {
  129. if (!(flags & VM_NORESERVE))
  130. vm_unacct_memory(VM_ACCT(size));
  131. }
  132. static inline int shmem_reacct_size(unsigned long flags,
  133. loff_t oldsize, loff_t newsize)
  134. {
  135. if (!(flags & VM_NORESERVE)) {
  136. if (VM_ACCT(newsize) > VM_ACCT(oldsize))
  137. return security_vm_enough_memory_mm(current->mm,
  138. VM_ACCT(newsize) - VM_ACCT(oldsize));
  139. else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
  140. vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
  141. }
  142. return 0;
  143. }
  144. /*
  145. * ... whereas tmpfs objects are accounted incrementally as
  146. * pages are allocated, in order to allow large sparse files.
  147. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  148. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  149. */
  150. static inline int shmem_acct_block(unsigned long flags, long pages)
  151. {
  152. if (!(flags & VM_NORESERVE))
  153. return 0;
  154. return security_vm_enough_memory_mm(current->mm,
  155. pages * VM_ACCT(PAGE_SIZE));
  156. }
  157. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  158. {
  159. if (flags & VM_NORESERVE)
  160. vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
  161. }
  162. static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
  163. {
  164. struct shmem_inode_info *info = SHMEM_I(inode);
  165. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  166. if (shmem_acct_block(info->flags, pages))
  167. return false;
  168. if (sbinfo->max_blocks) {
  169. if (percpu_counter_compare(&sbinfo->used_blocks,
  170. sbinfo->max_blocks - pages) > 0)
  171. goto unacct;
  172. percpu_counter_add(&sbinfo->used_blocks, pages);
  173. }
  174. return true;
  175. unacct:
  176. shmem_unacct_blocks(info->flags, pages);
  177. return false;
  178. }
  179. static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
  180. {
  181. struct shmem_inode_info *info = SHMEM_I(inode);
  182. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  183. if (sbinfo->max_blocks)
  184. percpu_counter_sub(&sbinfo->used_blocks, pages);
  185. shmem_unacct_blocks(info->flags, pages);
  186. }
  187. static const struct super_operations shmem_ops;
  188. static const struct address_space_operations shmem_aops;
  189. static const struct file_operations shmem_file_operations;
  190. static const struct inode_operations shmem_inode_operations;
  191. static const struct inode_operations shmem_dir_inode_operations;
  192. static const struct inode_operations shmem_special_inode_operations;
  193. static const struct vm_operations_struct shmem_vm_ops;
  194. static struct file_system_type shmem_fs_type;
  195. static LIST_HEAD(shmem_swaplist);
  196. static DEFINE_MUTEX(shmem_swaplist_mutex);
  197. static int shmem_reserve_inode(struct super_block *sb)
  198. {
  199. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  200. if (sbinfo->max_inodes) {
  201. spin_lock(&sbinfo->stat_lock);
  202. if (!sbinfo->free_inodes) {
  203. spin_unlock(&sbinfo->stat_lock);
  204. return -ENOSPC;
  205. }
  206. sbinfo->free_inodes--;
  207. spin_unlock(&sbinfo->stat_lock);
  208. }
  209. return 0;
  210. }
  211. static void shmem_free_inode(struct super_block *sb)
  212. {
  213. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  214. if (sbinfo->max_inodes) {
  215. spin_lock(&sbinfo->stat_lock);
  216. sbinfo->free_inodes++;
  217. spin_unlock(&sbinfo->stat_lock);
  218. }
  219. }
  220. /**
  221. * shmem_recalc_inode - recalculate the block usage of an inode
  222. * @inode: inode to recalc
  223. *
  224. * We have to calculate the free blocks since the mm can drop
  225. * undirtied hole pages behind our back.
  226. *
  227. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  228. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  229. *
  230. * It has to be called with the spinlock held.
  231. */
  232. static void shmem_recalc_inode(struct inode *inode)
  233. {
  234. struct shmem_inode_info *info = SHMEM_I(inode);
  235. long freed;
  236. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  237. if (freed > 0) {
  238. info->alloced -= freed;
  239. inode->i_blocks -= freed * BLOCKS_PER_PAGE;
  240. shmem_inode_unacct_blocks(inode, freed);
  241. }
  242. }
  243. bool shmem_charge(struct inode *inode, long pages)
  244. {
  245. struct shmem_inode_info *info = SHMEM_I(inode);
  246. unsigned long flags;
  247. if (!shmem_inode_acct_block(inode, pages))
  248. return false;
  249. /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
  250. inode->i_mapping->nrpages += pages;
  251. spin_lock_irqsave(&info->lock, flags);
  252. info->alloced += pages;
  253. inode->i_blocks += pages * BLOCKS_PER_PAGE;
  254. shmem_recalc_inode(inode);
  255. spin_unlock_irqrestore(&info->lock, flags);
  256. return true;
  257. }
  258. void shmem_uncharge(struct inode *inode, long pages)
  259. {
  260. struct shmem_inode_info *info = SHMEM_I(inode);
  261. unsigned long flags;
  262. /* nrpages adjustment done by __delete_from_page_cache() or caller */
  263. spin_lock_irqsave(&info->lock, flags);
  264. info->alloced -= pages;
  265. inode->i_blocks -= pages * BLOCKS_PER_PAGE;
  266. shmem_recalc_inode(inode);
  267. spin_unlock_irqrestore(&info->lock, flags);
  268. shmem_inode_unacct_blocks(inode, pages);
  269. }
  270. /*
  271. * Replace item expected in radix tree by a new item, while holding tree lock.
  272. */
  273. static int shmem_radix_tree_replace(struct address_space *mapping,
  274. pgoff_t index, void *expected, void *replacement)
  275. {
  276. void **pslot;
  277. void *item;
  278. VM_BUG_ON(!expected);
  279. VM_BUG_ON(!replacement);
  280. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  281. if (!pslot)
  282. return -ENOENT;
  283. item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
  284. if (item != expected)
  285. return -ENOENT;
  286. radix_tree_replace_slot(pslot, replacement);
  287. return 0;
  288. }
  289. /*
  290. * Sometimes, before we decide whether to proceed or to fail, we must check
  291. * that an entry was not already brought back from swap by a racing thread.
  292. *
  293. * Checking page is not enough: by the time a SwapCache page is locked, it
  294. * might be reused, and again be SwapCache, using the same swap as before.
  295. */
  296. static bool shmem_confirm_swap(struct address_space *mapping,
  297. pgoff_t index, swp_entry_t swap)
  298. {
  299. void *item;
  300. rcu_read_lock();
  301. item = radix_tree_lookup(&mapping->page_tree, index);
  302. rcu_read_unlock();
  303. return item == swp_to_radix_entry(swap);
  304. }
  305. /*
  306. * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
  307. *
  308. * SHMEM_HUGE_NEVER:
  309. * disables huge pages for the mount;
  310. * SHMEM_HUGE_ALWAYS:
  311. * enables huge pages for the mount;
  312. * SHMEM_HUGE_WITHIN_SIZE:
  313. * only allocate huge pages if the page will be fully within i_size,
  314. * also respect fadvise()/madvise() hints;
  315. * SHMEM_HUGE_ADVISE:
  316. * only allocate huge pages if requested with fadvise()/madvise();
  317. */
  318. #define SHMEM_HUGE_NEVER 0
  319. #define SHMEM_HUGE_ALWAYS 1
  320. #define SHMEM_HUGE_WITHIN_SIZE 2
  321. #define SHMEM_HUGE_ADVISE 3
  322. /*
  323. * Special values.
  324. * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
  325. *
  326. * SHMEM_HUGE_DENY:
  327. * disables huge on shm_mnt and all mounts, for emergency use;
  328. * SHMEM_HUGE_FORCE:
  329. * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
  330. *
  331. */
  332. #define SHMEM_HUGE_DENY (-1)
  333. #define SHMEM_HUGE_FORCE (-2)
  334. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  335. /* ifdef here to avoid bloating shmem.o when not necessary */
  336. int shmem_huge __read_mostly;
  337. #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
  338. static int shmem_parse_huge(const char *str)
  339. {
  340. if (!strcmp(str, "never"))
  341. return SHMEM_HUGE_NEVER;
  342. if (!strcmp(str, "always"))
  343. return SHMEM_HUGE_ALWAYS;
  344. if (!strcmp(str, "within_size"))
  345. return SHMEM_HUGE_WITHIN_SIZE;
  346. if (!strcmp(str, "advise"))
  347. return SHMEM_HUGE_ADVISE;
  348. if (!strcmp(str, "deny"))
  349. return SHMEM_HUGE_DENY;
  350. if (!strcmp(str, "force"))
  351. return SHMEM_HUGE_FORCE;
  352. return -EINVAL;
  353. }
  354. static const char *shmem_format_huge(int huge)
  355. {
  356. switch (huge) {
  357. case SHMEM_HUGE_NEVER:
  358. return "never";
  359. case SHMEM_HUGE_ALWAYS:
  360. return "always";
  361. case SHMEM_HUGE_WITHIN_SIZE:
  362. return "within_size";
  363. case SHMEM_HUGE_ADVISE:
  364. return "advise";
  365. case SHMEM_HUGE_DENY:
  366. return "deny";
  367. case SHMEM_HUGE_FORCE:
  368. return "force";
  369. default:
  370. VM_BUG_ON(1);
  371. return "bad_val";
  372. }
  373. }
  374. #endif
  375. static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
  376. struct shrink_control *sc, unsigned long nr_to_split)
  377. {
  378. LIST_HEAD(list), *pos, *next;
  379. LIST_HEAD(to_remove);
  380. struct inode *inode;
  381. struct shmem_inode_info *info;
  382. struct page *page;
  383. unsigned long batch = sc ? sc->nr_to_scan : 128;
  384. int removed = 0, split = 0;
  385. if (list_empty(&sbinfo->shrinklist))
  386. return SHRINK_STOP;
  387. spin_lock(&sbinfo->shrinklist_lock);
  388. list_for_each_safe(pos, next, &sbinfo->shrinklist) {
  389. info = list_entry(pos, struct shmem_inode_info, shrinklist);
  390. /* pin the inode */
  391. inode = igrab(&info->vfs_inode);
  392. /* inode is about to be evicted */
  393. if (!inode) {
  394. list_del_init(&info->shrinklist);
  395. removed++;
  396. goto next;
  397. }
  398. /* Check if there's anything to gain */
  399. if (round_up(inode->i_size, PAGE_SIZE) ==
  400. round_up(inode->i_size, HPAGE_PMD_SIZE)) {
  401. list_move(&info->shrinklist, &to_remove);
  402. removed++;
  403. goto next;
  404. }
  405. list_move(&info->shrinklist, &list);
  406. next:
  407. if (!--batch)
  408. break;
  409. }
  410. spin_unlock(&sbinfo->shrinklist_lock);
  411. list_for_each_safe(pos, next, &to_remove) {
  412. info = list_entry(pos, struct shmem_inode_info, shrinklist);
  413. inode = &info->vfs_inode;
  414. list_del_init(&info->shrinklist);
  415. iput(inode);
  416. }
  417. list_for_each_safe(pos, next, &list) {
  418. int ret;
  419. info = list_entry(pos, struct shmem_inode_info, shrinklist);
  420. inode = &info->vfs_inode;
  421. if (nr_to_split && split >= nr_to_split)
  422. goto leave;
  423. page = find_get_page(inode->i_mapping,
  424. (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
  425. if (!page)
  426. goto drop;
  427. /* No huge page at the end of the file: nothing to split */
  428. if (!PageTransHuge(page)) {
  429. put_page(page);
  430. goto drop;
  431. }
  432. /*
  433. * Leave the inode on the list if we failed to lock
  434. * the page at this time.
  435. *
  436. * Waiting for the lock may lead to deadlock in the
  437. * reclaim path.
  438. */
  439. if (!trylock_page(page)) {
  440. put_page(page);
  441. goto leave;
  442. }
  443. ret = split_huge_page(page);
  444. unlock_page(page);
  445. put_page(page);
  446. /* If split failed leave the inode on the list */
  447. if (ret)
  448. goto leave;
  449. split++;
  450. drop:
  451. list_del_init(&info->shrinklist);
  452. removed++;
  453. leave:
  454. iput(inode);
  455. }
  456. spin_lock(&sbinfo->shrinklist_lock);
  457. list_splice_tail(&list, &sbinfo->shrinklist);
  458. sbinfo->shrinklist_len -= removed;
  459. spin_unlock(&sbinfo->shrinklist_lock);
  460. return split;
  461. }
  462. static long shmem_unused_huge_scan(struct super_block *sb,
  463. struct shrink_control *sc)
  464. {
  465. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  466. if (!READ_ONCE(sbinfo->shrinklist_len))
  467. return SHRINK_STOP;
  468. return shmem_unused_huge_shrink(sbinfo, sc, 0);
  469. }
  470. static long shmem_unused_huge_count(struct super_block *sb,
  471. struct shrink_control *sc)
  472. {
  473. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  474. return READ_ONCE(sbinfo->shrinklist_len);
  475. }
  476. #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
  477. #define shmem_huge SHMEM_HUGE_DENY
  478. static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
  479. struct shrink_control *sc, unsigned long nr_to_split)
  480. {
  481. return 0;
  482. }
  483. #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
  484. /*
  485. * Like add_to_page_cache_locked, but error if expected item has gone.
  486. */
  487. static int shmem_add_to_page_cache(struct page *page,
  488. struct address_space *mapping,
  489. pgoff_t index, void *expected)
  490. {
  491. int error, nr = hpage_nr_pages(page);
  492. VM_BUG_ON_PAGE(PageTail(page), page);
  493. VM_BUG_ON_PAGE(index != round_down(index, nr), page);
  494. VM_BUG_ON_PAGE(!PageLocked(page), page);
  495. VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
  496. VM_BUG_ON(expected && PageTransHuge(page));
  497. page_ref_add(page, nr);
  498. page->mapping = mapping;
  499. page->index = index;
  500. spin_lock_irq(&mapping->tree_lock);
  501. if (PageTransHuge(page)) {
  502. void __rcu **results;
  503. pgoff_t idx;
  504. int i;
  505. error = 0;
  506. if (radix_tree_gang_lookup_slot(&mapping->page_tree,
  507. &results, &idx, index, 1) &&
  508. idx < index + HPAGE_PMD_NR) {
  509. error = -EEXIST;
  510. }
  511. if (!error) {
  512. for (i = 0; i < HPAGE_PMD_NR; i++) {
  513. error = radix_tree_insert(&mapping->page_tree,
  514. index + i, page + i);
  515. VM_BUG_ON(error);
  516. }
  517. count_vm_event(THP_FILE_ALLOC);
  518. }
  519. } else if (!expected) {
  520. error = radix_tree_insert(&mapping->page_tree, index, page);
  521. } else {
  522. error = shmem_radix_tree_replace(mapping, index, expected,
  523. page);
  524. }
  525. if (!error) {
  526. mapping->nrpages += nr;
  527. if (PageTransHuge(page))
  528. __inc_node_page_state(page, NR_SHMEM_THPS);
  529. __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
  530. __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
  531. spin_unlock_irq(&mapping->tree_lock);
  532. } else {
  533. page->mapping = NULL;
  534. spin_unlock_irq(&mapping->tree_lock);
  535. page_ref_sub(page, nr);
  536. }
  537. return error;
  538. }
  539. /*
  540. * Like delete_from_page_cache, but substitutes swap for page.
  541. */
  542. static void shmem_delete_from_page_cache(struct page *page, void *radswap)
  543. {
  544. struct address_space *mapping = page->mapping;
  545. int error;
  546. VM_BUG_ON_PAGE(PageCompound(page), page);
  547. spin_lock_irq(&mapping->tree_lock);
  548. error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
  549. page->mapping = NULL;
  550. mapping->nrpages--;
  551. __dec_node_page_state(page, NR_FILE_PAGES);
  552. __dec_node_page_state(page, NR_SHMEM);
  553. spin_unlock_irq(&mapping->tree_lock);
  554. put_page(page);
  555. BUG_ON(error);
  556. }
  557. /*
  558. * Remove swap entry from radix tree, free the swap and its page cache.
  559. */
  560. static int shmem_free_swap(struct address_space *mapping,
  561. pgoff_t index, void *radswap)
  562. {
  563. void *old;
  564. spin_lock_irq(&mapping->tree_lock);
  565. old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
  566. spin_unlock_irq(&mapping->tree_lock);
  567. if (old != radswap)
  568. return -ENOENT;
  569. free_swap_and_cache(radix_to_swp_entry(radswap));
  570. return 0;
  571. }
  572. /*
  573. * Determine (in bytes) how many of the shmem object's pages mapped by the
  574. * given offsets are swapped out.
  575. *
  576. * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
  577. * as long as the inode doesn't go away and racy results are not a problem.
  578. */
  579. unsigned long shmem_partial_swap_usage(struct address_space *mapping,
  580. pgoff_t start, pgoff_t end)
  581. {
  582. struct radix_tree_iter iter;
  583. void **slot;
  584. struct page *page;
  585. unsigned long swapped = 0;
  586. rcu_read_lock();
  587. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  588. if (iter.index >= end)
  589. break;
  590. page = radix_tree_deref_slot(slot);
  591. if (radix_tree_deref_retry(page)) {
  592. slot = radix_tree_iter_retry(&iter);
  593. continue;
  594. }
  595. if (radix_tree_exceptional_entry(page))
  596. swapped++;
  597. if (need_resched()) {
  598. cond_resched_rcu();
  599. slot = radix_tree_iter_next(&iter);
  600. }
  601. }
  602. rcu_read_unlock();
  603. return swapped << PAGE_SHIFT;
  604. }
  605. /*
  606. * Determine (in bytes) how many of the shmem object's pages mapped by the
  607. * given vma is swapped out.
  608. *
  609. * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
  610. * as long as the inode doesn't go away and racy results are not a problem.
  611. */
  612. unsigned long shmem_swap_usage(struct vm_area_struct *vma)
  613. {
  614. struct inode *inode = file_inode(vma->vm_file);
  615. struct shmem_inode_info *info = SHMEM_I(inode);
  616. struct address_space *mapping = inode->i_mapping;
  617. unsigned long swapped;
  618. /* Be careful as we don't hold info->lock */
  619. swapped = READ_ONCE(info->swapped);
  620. /*
  621. * The easier cases are when the shmem object has nothing in swap, or
  622. * the vma maps it whole. Then we can simply use the stats that we
  623. * already track.
  624. */
  625. if (!swapped)
  626. return 0;
  627. if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
  628. return swapped << PAGE_SHIFT;
  629. /* Here comes the more involved part */
  630. return shmem_partial_swap_usage(mapping,
  631. linear_page_index(vma, vma->vm_start),
  632. linear_page_index(vma, vma->vm_end));
  633. }
  634. /*
  635. * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
  636. */
  637. void shmem_unlock_mapping(struct address_space *mapping)
  638. {
  639. struct pagevec pvec;
  640. pgoff_t indices[PAGEVEC_SIZE];
  641. pgoff_t index = 0;
  642. pagevec_init(&pvec, 0);
  643. /*
  644. * Minor point, but we might as well stop if someone else SHM_LOCKs it.
  645. */
  646. while (!mapping_unevictable(mapping)) {
  647. /*
  648. * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
  649. * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
  650. */
  651. pvec.nr = find_get_entries(mapping, index,
  652. PAGEVEC_SIZE, pvec.pages, indices);
  653. if (!pvec.nr)
  654. break;
  655. index = indices[pvec.nr - 1] + 1;
  656. pagevec_remove_exceptionals(&pvec);
  657. check_move_unevictable_pages(pvec.pages, pvec.nr);
  658. pagevec_release(&pvec);
  659. cond_resched();
  660. }
  661. }
  662. /*
  663. * Remove range of pages and swap entries from radix tree, and free them.
  664. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
  665. */
  666. static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
  667. bool unfalloc)
  668. {
  669. struct address_space *mapping = inode->i_mapping;
  670. struct shmem_inode_info *info = SHMEM_I(inode);
  671. pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
  672. pgoff_t end = (lend + 1) >> PAGE_SHIFT;
  673. unsigned int partial_start = lstart & (PAGE_SIZE - 1);
  674. unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
  675. struct pagevec pvec;
  676. pgoff_t indices[PAGEVEC_SIZE];
  677. long nr_swaps_freed = 0;
  678. pgoff_t index;
  679. int i;
  680. if (lend == -1)
  681. end = -1; /* unsigned, so actually very big */
  682. pagevec_init(&pvec, 0);
  683. index = start;
  684. while (index < end) {
  685. pvec.nr = find_get_entries(mapping, index,
  686. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  687. pvec.pages, indices);
  688. if (!pvec.nr)
  689. break;
  690. for (i = 0; i < pagevec_count(&pvec); i++) {
  691. struct page *page = pvec.pages[i];
  692. index = indices[i];
  693. if (index >= end)
  694. break;
  695. if (radix_tree_exceptional_entry(page)) {
  696. if (unfalloc)
  697. continue;
  698. nr_swaps_freed += !shmem_free_swap(mapping,
  699. index, page);
  700. continue;
  701. }
  702. VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
  703. if (!trylock_page(page))
  704. continue;
  705. if (PageTransTail(page)) {
  706. /* Middle of THP: zero out the page */
  707. clear_highpage(page);
  708. unlock_page(page);
  709. continue;
  710. } else if (PageTransHuge(page)) {
  711. if (index == round_down(end, HPAGE_PMD_NR)) {
  712. /*
  713. * Range ends in the middle of THP:
  714. * zero out the page
  715. */
  716. clear_highpage(page);
  717. unlock_page(page);
  718. continue;
  719. }
  720. index += HPAGE_PMD_NR - 1;
  721. i += HPAGE_PMD_NR - 1;
  722. }
  723. if (!unfalloc || !PageUptodate(page)) {
  724. VM_BUG_ON_PAGE(PageTail(page), page);
  725. if (page_mapping(page) == mapping) {
  726. VM_BUG_ON_PAGE(PageWriteback(page), page);
  727. truncate_inode_page(mapping, page);
  728. }
  729. }
  730. unlock_page(page);
  731. }
  732. pagevec_remove_exceptionals(&pvec);
  733. pagevec_release(&pvec);
  734. cond_resched();
  735. index++;
  736. }
  737. if (partial_start) {
  738. struct page *page = NULL;
  739. shmem_getpage(inode, start - 1, &page, SGP_READ);
  740. if (page) {
  741. unsigned int top = PAGE_SIZE;
  742. if (start > end) {
  743. top = partial_end;
  744. partial_end = 0;
  745. }
  746. zero_user_segment(page, partial_start, top);
  747. set_page_dirty(page);
  748. unlock_page(page);
  749. put_page(page);
  750. }
  751. }
  752. if (partial_end) {
  753. struct page *page = NULL;
  754. shmem_getpage(inode, end, &page, SGP_READ);
  755. if (page) {
  756. zero_user_segment(page, 0, partial_end);
  757. set_page_dirty(page);
  758. unlock_page(page);
  759. put_page(page);
  760. }
  761. }
  762. if (start >= end)
  763. return;
  764. index = start;
  765. while (index < end) {
  766. cond_resched();
  767. pvec.nr = find_get_entries(mapping, index,
  768. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  769. pvec.pages, indices);
  770. if (!pvec.nr) {
  771. /* If all gone or hole-punch or unfalloc, we're done */
  772. if (index == start || end != -1)
  773. break;
  774. /* But if truncating, restart to make sure all gone */
  775. index = start;
  776. continue;
  777. }
  778. for (i = 0; i < pagevec_count(&pvec); i++) {
  779. struct page *page = pvec.pages[i];
  780. index = indices[i];
  781. if (index >= end)
  782. break;
  783. if (radix_tree_exceptional_entry(page)) {
  784. if (unfalloc)
  785. continue;
  786. if (shmem_free_swap(mapping, index, page)) {
  787. /* Swap was replaced by page: retry */
  788. index--;
  789. break;
  790. }
  791. nr_swaps_freed++;
  792. continue;
  793. }
  794. lock_page(page);
  795. if (PageTransTail(page)) {
  796. /* Middle of THP: zero out the page */
  797. clear_highpage(page);
  798. unlock_page(page);
  799. /*
  800. * Partial thp truncate due 'start' in middle
  801. * of THP: don't need to look on these pages
  802. * again on !pvec.nr restart.
  803. */
  804. if (index != round_down(end, HPAGE_PMD_NR))
  805. start++;
  806. continue;
  807. } else if (PageTransHuge(page)) {
  808. if (index == round_down(end, HPAGE_PMD_NR)) {
  809. /*
  810. * Range ends in the middle of THP:
  811. * zero out the page
  812. */
  813. clear_highpage(page);
  814. unlock_page(page);
  815. continue;
  816. }
  817. index += HPAGE_PMD_NR - 1;
  818. i += HPAGE_PMD_NR - 1;
  819. }
  820. if (!unfalloc || !PageUptodate(page)) {
  821. VM_BUG_ON_PAGE(PageTail(page), page);
  822. if (page_mapping(page) == mapping) {
  823. VM_BUG_ON_PAGE(PageWriteback(page), page);
  824. truncate_inode_page(mapping, page);
  825. } else {
  826. /* Page was replaced by swap: retry */
  827. unlock_page(page);
  828. index--;
  829. break;
  830. }
  831. }
  832. unlock_page(page);
  833. }
  834. pagevec_remove_exceptionals(&pvec);
  835. pagevec_release(&pvec);
  836. index++;
  837. }
  838. spin_lock_irq(&info->lock);
  839. info->swapped -= nr_swaps_freed;
  840. shmem_recalc_inode(inode);
  841. spin_unlock_irq(&info->lock);
  842. }
  843. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  844. {
  845. shmem_undo_range(inode, lstart, lend, false);
  846. inode->i_ctime = inode->i_mtime = current_time(inode);
  847. }
  848. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  849. static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
  850. struct kstat *stat)
  851. {
  852. struct inode *inode = dentry->d_inode;
  853. struct shmem_inode_info *info = SHMEM_I(inode);
  854. if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
  855. spin_lock_irq(&info->lock);
  856. shmem_recalc_inode(inode);
  857. spin_unlock_irq(&info->lock);
  858. }
  859. generic_fillattr(inode, stat);
  860. return 0;
  861. }
  862. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  863. {
  864. struct inode *inode = d_inode(dentry);
  865. struct shmem_inode_info *info = SHMEM_I(inode);
  866. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  867. int error;
  868. error = setattr_prepare(dentry, attr);
  869. if (error)
  870. return error;
  871. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  872. loff_t oldsize = inode->i_size;
  873. loff_t newsize = attr->ia_size;
  874. /* protected by i_mutex */
  875. if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
  876. (newsize > oldsize && (info->seals & F_SEAL_GROW)))
  877. return -EPERM;
  878. if (newsize != oldsize) {
  879. error = shmem_reacct_size(SHMEM_I(inode)->flags,
  880. oldsize, newsize);
  881. if (error)
  882. return error;
  883. i_size_write(inode, newsize);
  884. inode->i_ctime = inode->i_mtime = current_time(inode);
  885. }
  886. if (newsize <= oldsize) {
  887. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  888. if (oldsize > holebegin)
  889. unmap_mapping_range(inode->i_mapping,
  890. holebegin, 0, 1);
  891. if (info->alloced)
  892. shmem_truncate_range(inode,
  893. newsize, (loff_t)-1);
  894. /* unmap again to remove racily COWed private pages */
  895. if (oldsize > holebegin)
  896. unmap_mapping_range(inode->i_mapping,
  897. holebegin, 0, 1);
  898. /*
  899. * Part of the huge page can be beyond i_size: subject
  900. * to shrink under memory pressure.
  901. */
  902. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
  903. spin_lock(&sbinfo->shrinklist_lock);
  904. /*
  905. * _careful to defend against unlocked access to
  906. * ->shrink_list in shmem_unused_huge_shrink()
  907. */
  908. if (list_empty_careful(&info->shrinklist)) {
  909. list_add_tail(&info->shrinklist,
  910. &sbinfo->shrinklist);
  911. sbinfo->shrinklist_len++;
  912. }
  913. spin_unlock(&sbinfo->shrinklist_lock);
  914. }
  915. }
  916. }
  917. setattr_copy(inode, attr);
  918. if (attr->ia_valid & ATTR_MODE)
  919. error = posix_acl_chmod(inode, inode->i_mode);
  920. return error;
  921. }
  922. static void shmem_evict_inode(struct inode *inode)
  923. {
  924. struct shmem_inode_info *info = SHMEM_I(inode);
  925. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  926. if (inode->i_mapping->a_ops == &shmem_aops) {
  927. shmem_unacct_size(info->flags, inode->i_size);
  928. inode->i_size = 0;
  929. shmem_truncate_range(inode, 0, (loff_t)-1);
  930. if (!list_empty(&info->shrinklist)) {
  931. spin_lock(&sbinfo->shrinklist_lock);
  932. if (!list_empty(&info->shrinklist)) {
  933. list_del_init(&info->shrinklist);
  934. sbinfo->shrinklist_len--;
  935. }
  936. spin_unlock(&sbinfo->shrinklist_lock);
  937. }
  938. if (!list_empty(&info->swaplist)) {
  939. mutex_lock(&shmem_swaplist_mutex);
  940. list_del_init(&info->swaplist);
  941. mutex_unlock(&shmem_swaplist_mutex);
  942. }
  943. }
  944. simple_xattrs_free(&info->xattrs);
  945. WARN_ON(inode->i_blocks);
  946. shmem_free_inode(inode->i_sb);
  947. clear_inode(inode);
  948. }
  949. /*
  950. * If swap found in inode, free it and move page from swapcache to filecache.
  951. */
  952. static int shmem_unuse_inode(struct shmem_inode_info *info,
  953. swp_entry_t swap, struct page **pagep)
  954. {
  955. struct address_space *mapping = info->vfs_inode.i_mapping;
  956. void *radswap;
  957. pgoff_t index;
  958. gfp_t gfp;
  959. int error = 0;
  960. radswap = swp_to_radix_entry(swap);
  961. index = radix_tree_locate_item(&mapping->page_tree, radswap);
  962. if (index == -1)
  963. return -EAGAIN; /* tell shmem_unuse we found nothing */
  964. /*
  965. * Move _head_ to start search for next from here.
  966. * But be careful: shmem_evict_inode checks list_empty without taking
  967. * mutex, and there's an instant in list_move_tail when info->swaplist
  968. * would appear empty, if it were the only one on shmem_swaplist.
  969. */
  970. if (shmem_swaplist.next != &info->swaplist)
  971. list_move_tail(&shmem_swaplist, &info->swaplist);
  972. gfp = mapping_gfp_mask(mapping);
  973. if (shmem_should_replace_page(*pagep, gfp)) {
  974. mutex_unlock(&shmem_swaplist_mutex);
  975. error = shmem_replace_page(pagep, gfp, info, index);
  976. mutex_lock(&shmem_swaplist_mutex);
  977. /*
  978. * We needed to drop mutex to make that restrictive page
  979. * allocation, but the inode might have been freed while we
  980. * dropped it: although a racing shmem_evict_inode() cannot
  981. * complete without emptying the radix_tree, our page lock
  982. * on this swapcache page is not enough to prevent that -
  983. * free_swap_and_cache() of our swap entry will only
  984. * trylock_page(), removing swap from radix_tree whatever.
  985. *
  986. * We must not proceed to shmem_add_to_page_cache() if the
  987. * inode has been freed, but of course we cannot rely on
  988. * inode or mapping or info to check that. However, we can
  989. * safely check if our swap entry is still in use (and here
  990. * it can't have got reused for another page): if it's still
  991. * in use, then the inode cannot have been freed yet, and we
  992. * can safely proceed (if it's no longer in use, that tells
  993. * nothing about the inode, but we don't need to unuse swap).
  994. */
  995. if (!page_swapcount(*pagep))
  996. error = -ENOENT;
  997. }
  998. /*
  999. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  1000. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  1001. * beneath us (pagelock doesn't help until the page is in pagecache).
  1002. */
  1003. if (!error)
  1004. error = shmem_add_to_page_cache(*pagep, mapping, index,
  1005. radswap);
  1006. if (error != -ENOMEM) {
  1007. /*
  1008. * Truncation and eviction use free_swap_and_cache(), which
  1009. * only does trylock page: if we raced, best clean up here.
  1010. */
  1011. delete_from_swap_cache(*pagep);
  1012. set_page_dirty(*pagep);
  1013. if (!error) {
  1014. spin_lock_irq(&info->lock);
  1015. info->swapped--;
  1016. spin_unlock_irq(&info->lock);
  1017. swap_free(swap);
  1018. }
  1019. }
  1020. return error;
  1021. }
  1022. /*
  1023. * Search through swapped inodes to find and replace swap by page.
  1024. */
  1025. int shmem_unuse(swp_entry_t swap, struct page *page)
  1026. {
  1027. struct list_head *this, *next;
  1028. struct shmem_inode_info *info;
  1029. struct mem_cgroup *memcg;
  1030. int error = 0;
  1031. /*
  1032. * There's a faint possibility that swap page was replaced before
  1033. * caller locked it: caller will come back later with the right page.
  1034. */
  1035. if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
  1036. goto out;
  1037. /*
  1038. * Charge page using GFP_KERNEL while we can wait, before taking
  1039. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  1040. * Charged back to the user (not to caller) when swap account is used.
  1041. */
  1042. error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg,
  1043. false);
  1044. if (error)
  1045. goto out;
  1046. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  1047. error = -EAGAIN;
  1048. mutex_lock(&shmem_swaplist_mutex);
  1049. list_for_each_safe(this, next, &shmem_swaplist) {
  1050. info = list_entry(this, struct shmem_inode_info, swaplist);
  1051. if (info->swapped)
  1052. error = shmem_unuse_inode(info, swap, &page);
  1053. else
  1054. list_del_init(&info->swaplist);
  1055. cond_resched();
  1056. if (error != -EAGAIN)
  1057. break;
  1058. /* found nothing in this: move on to search the next */
  1059. }
  1060. mutex_unlock(&shmem_swaplist_mutex);
  1061. if (error) {
  1062. if (error != -ENOMEM)
  1063. error = 0;
  1064. mem_cgroup_cancel_charge(page, memcg, false);
  1065. } else
  1066. mem_cgroup_commit_charge(page, memcg, true, false);
  1067. out:
  1068. unlock_page(page);
  1069. put_page(page);
  1070. return error;
  1071. }
  1072. /*
  1073. * Move the page from the page cache to the swap cache.
  1074. */
  1075. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  1076. {
  1077. struct shmem_inode_info *info;
  1078. struct address_space *mapping;
  1079. struct inode *inode;
  1080. swp_entry_t swap;
  1081. pgoff_t index;
  1082. VM_BUG_ON_PAGE(PageCompound(page), page);
  1083. BUG_ON(!PageLocked(page));
  1084. mapping = page->mapping;
  1085. index = page->index;
  1086. inode = mapping->host;
  1087. info = SHMEM_I(inode);
  1088. if (info->flags & VM_LOCKED)
  1089. goto redirty;
  1090. if (!total_swap_pages)
  1091. goto redirty;
  1092. /*
  1093. * Our capabilities prevent regular writeback or sync from ever calling
  1094. * shmem_writepage; but a stacking filesystem might use ->writepage of
  1095. * its underlying filesystem, in which case tmpfs should write out to
  1096. * swap only in response to memory pressure, and not for the writeback
  1097. * threads or sync.
  1098. */
  1099. if (!wbc->for_reclaim) {
  1100. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  1101. goto redirty;
  1102. }
  1103. /*
  1104. * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
  1105. * value into swapfile.c, the only way we can correctly account for a
  1106. * fallocated page arriving here is now to initialize it and write it.
  1107. *
  1108. * That's okay for a page already fallocated earlier, but if we have
  1109. * not yet completed the fallocation, then (a) we want to keep track
  1110. * of this page in case we have to undo it, and (b) it may not be a
  1111. * good idea to continue anyway, once we're pushing into swap. So
  1112. * reactivate the page, and let shmem_fallocate() quit when too many.
  1113. */
  1114. if (!PageUptodate(page)) {
  1115. if (inode->i_private) {
  1116. struct shmem_falloc *shmem_falloc;
  1117. spin_lock(&inode->i_lock);
  1118. shmem_falloc = inode->i_private;
  1119. if (shmem_falloc &&
  1120. !shmem_falloc->waitq &&
  1121. index >= shmem_falloc->start &&
  1122. index < shmem_falloc->next)
  1123. shmem_falloc->nr_unswapped++;
  1124. else
  1125. shmem_falloc = NULL;
  1126. spin_unlock(&inode->i_lock);
  1127. if (shmem_falloc)
  1128. goto redirty;
  1129. }
  1130. clear_highpage(page);
  1131. flush_dcache_page(page);
  1132. SetPageUptodate(page);
  1133. }
  1134. swap = get_swap_page();
  1135. if (!swap.val)
  1136. goto redirty;
  1137. if (mem_cgroup_try_charge_swap(page, swap))
  1138. goto free_swap;
  1139. /*
  1140. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  1141. * if it's not already there. Do it now before the page is
  1142. * moved to swap cache, when its pagelock no longer protects
  1143. * the inode from eviction. But don't unlock the mutex until
  1144. * we've incremented swapped, because shmem_unuse_inode() will
  1145. * prune a !swapped inode from the swaplist under this mutex.
  1146. */
  1147. mutex_lock(&shmem_swaplist_mutex);
  1148. if (list_empty(&info->swaplist))
  1149. list_add_tail(&info->swaplist, &shmem_swaplist);
  1150. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  1151. spin_lock_irq(&info->lock);
  1152. shmem_recalc_inode(inode);
  1153. info->swapped++;
  1154. spin_unlock_irq(&info->lock);
  1155. swap_shmem_alloc(swap);
  1156. shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
  1157. mutex_unlock(&shmem_swaplist_mutex);
  1158. BUG_ON(page_mapped(page));
  1159. swap_writepage(page, wbc);
  1160. return 0;
  1161. }
  1162. mutex_unlock(&shmem_swaplist_mutex);
  1163. free_swap:
  1164. swapcache_free(swap);
  1165. redirty:
  1166. set_page_dirty(page);
  1167. if (wbc->for_reclaim)
  1168. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  1169. unlock_page(page);
  1170. return 0;
  1171. }
  1172. #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
  1173. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  1174. {
  1175. char buffer[64];
  1176. if (!mpol || mpol->mode == MPOL_DEFAULT)
  1177. return; /* show nothing */
  1178. mpol_to_str(buffer, sizeof(buffer), mpol);
  1179. seq_printf(seq, ",mpol=%s", buffer);
  1180. }
  1181. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  1182. {
  1183. struct mempolicy *mpol = NULL;
  1184. if (sbinfo->mpol) {
  1185. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  1186. mpol = sbinfo->mpol;
  1187. mpol_get(mpol);
  1188. spin_unlock(&sbinfo->stat_lock);
  1189. }
  1190. return mpol;
  1191. }
  1192. #else /* !CONFIG_NUMA || !CONFIG_TMPFS */
  1193. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  1194. {
  1195. }
  1196. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  1197. {
  1198. return NULL;
  1199. }
  1200. #endif /* CONFIG_NUMA && CONFIG_TMPFS */
  1201. #ifndef CONFIG_NUMA
  1202. #define vm_policy vm_private_data
  1203. #endif
  1204. static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
  1205. struct shmem_inode_info *info, pgoff_t index)
  1206. {
  1207. /* Create a pseudo vma that just contains the policy */
  1208. vma->vm_start = 0;
  1209. /* Bias interleave by inode number to distribute better across nodes */
  1210. vma->vm_pgoff = index + info->vfs_inode.i_ino;
  1211. vma->vm_ops = NULL;
  1212. vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  1213. }
  1214. static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
  1215. {
  1216. /* Drop reference taken by mpol_shared_policy_lookup() */
  1217. mpol_cond_put(vma->vm_policy);
  1218. }
  1219. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  1220. struct shmem_inode_info *info, pgoff_t index)
  1221. {
  1222. struct vm_area_struct pvma;
  1223. struct page *page;
  1224. shmem_pseudo_vma_init(&pvma, info, index);
  1225. page = swapin_readahead(swap, gfp, &pvma, 0);
  1226. shmem_pseudo_vma_destroy(&pvma);
  1227. return page;
  1228. }
  1229. static struct page *shmem_alloc_hugepage(gfp_t gfp,
  1230. struct shmem_inode_info *info, pgoff_t index)
  1231. {
  1232. struct vm_area_struct pvma;
  1233. struct inode *inode = &info->vfs_inode;
  1234. struct address_space *mapping = inode->i_mapping;
  1235. pgoff_t idx, hindex;
  1236. void __rcu **results;
  1237. struct page *page;
  1238. if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
  1239. return NULL;
  1240. hindex = round_down(index, HPAGE_PMD_NR);
  1241. rcu_read_lock();
  1242. if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx,
  1243. hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
  1244. rcu_read_unlock();
  1245. return NULL;
  1246. }
  1247. rcu_read_unlock();
  1248. shmem_pseudo_vma_init(&pvma, info, hindex);
  1249. page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
  1250. HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
  1251. shmem_pseudo_vma_destroy(&pvma);
  1252. if (page)
  1253. prep_transhuge_page(page);
  1254. return page;
  1255. }
  1256. static struct page *shmem_alloc_page(gfp_t gfp,
  1257. struct shmem_inode_info *info, pgoff_t index)
  1258. {
  1259. struct vm_area_struct pvma;
  1260. struct page *page;
  1261. shmem_pseudo_vma_init(&pvma, info, index);
  1262. page = alloc_page_vma(gfp, &pvma, 0);
  1263. shmem_pseudo_vma_destroy(&pvma);
  1264. return page;
  1265. }
  1266. static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
  1267. struct inode *inode,
  1268. pgoff_t index, bool huge)
  1269. {
  1270. struct shmem_inode_info *info = SHMEM_I(inode);
  1271. struct page *page;
  1272. int nr;
  1273. int err = -ENOSPC;
  1274. if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
  1275. huge = false;
  1276. nr = huge ? HPAGE_PMD_NR : 1;
  1277. if (!shmem_inode_acct_block(inode, nr))
  1278. goto failed;
  1279. if (huge)
  1280. page = shmem_alloc_hugepage(gfp, info, index);
  1281. else
  1282. page = shmem_alloc_page(gfp, info, index);
  1283. if (page) {
  1284. __SetPageLocked(page);
  1285. __SetPageSwapBacked(page);
  1286. return page;
  1287. }
  1288. err = -ENOMEM;
  1289. shmem_inode_unacct_blocks(inode, nr);
  1290. failed:
  1291. return ERR_PTR(err);
  1292. }
  1293. /*
  1294. * When a page is moved from swapcache to shmem filecache (either by the
  1295. * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
  1296. * shmem_unuse_inode()), it may have been read in earlier from swap, in
  1297. * ignorance of the mapping it belongs to. If that mapping has special
  1298. * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
  1299. * we may need to copy to a suitable page before moving to filecache.
  1300. *
  1301. * In a future release, this may well be extended to respect cpuset and
  1302. * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
  1303. * but for now it is a simple matter of zone.
  1304. */
  1305. static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
  1306. {
  1307. return page_zonenum(page) > gfp_zone(gfp);
  1308. }
  1309. static int shmem_replace_page(struct page **pagep, gfp_t gfp,
  1310. struct shmem_inode_info *info, pgoff_t index)
  1311. {
  1312. struct page *oldpage, *newpage;
  1313. struct address_space *swap_mapping;
  1314. swp_entry_t entry;
  1315. pgoff_t swap_index;
  1316. int error;
  1317. oldpage = *pagep;
  1318. entry.val = page_private(oldpage);
  1319. swap_index = swp_offset(entry);
  1320. swap_mapping = page_mapping(oldpage);
  1321. /*
  1322. * We have arrived here because our zones are constrained, so don't
  1323. * limit chance of success by further cpuset and node constraints.
  1324. */
  1325. gfp &= ~GFP_CONSTRAINT_MASK;
  1326. newpage = shmem_alloc_page(gfp, info, index);
  1327. if (!newpage)
  1328. return -ENOMEM;
  1329. get_page(newpage);
  1330. copy_highpage(newpage, oldpage);
  1331. flush_dcache_page(newpage);
  1332. __SetPageLocked(newpage);
  1333. __SetPageSwapBacked(newpage);
  1334. SetPageUptodate(newpage);
  1335. set_page_private(newpage, entry.val);
  1336. SetPageSwapCache(newpage);
  1337. /*
  1338. * Our caller will very soon move newpage out of swapcache, but it's
  1339. * a nice clean interface for us to replace oldpage by newpage there.
  1340. */
  1341. spin_lock_irq(&swap_mapping->tree_lock);
  1342. error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
  1343. newpage);
  1344. if (!error) {
  1345. __inc_node_page_state(newpage, NR_FILE_PAGES);
  1346. __dec_node_page_state(oldpage, NR_FILE_PAGES);
  1347. }
  1348. spin_unlock_irq(&swap_mapping->tree_lock);
  1349. if (unlikely(error)) {
  1350. /*
  1351. * Is this possible? I think not, now that our callers check
  1352. * both PageSwapCache and page_private after getting page lock;
  1353. * but be defensive. Reverse old to newpage for clear and free.
  1354. */
  1355. oldpage = newpage;
  1356. } else {
  1357. mem_cgroup_migrate(oldpage, newpage);
  1358. lru_cache_add_anon(newpage);
  1359. *pagep = newpage;
  1360. }
  1361. ClearPageSwapCache(oldpage);
  1362. set_page_private(oldpage, 0);
  1363. unlock_page(oldpage);
  1364. put_page(oldpage);
  1365. put_page(oldpage);
  1366. return error;
  1367. }
  1368. /*
  1369. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  1370. *
  1371. * If we allocate a new one we do not mark it dirty. That's up to the
  1372. * vm. If we swap it in we mark it dirty since we also free the swap
  1373. * entry since a page cannot live in both the swap and page cache.
  1374. *
  1375. * fault_mm and fault_type are only supplied by shmem_fault:
  1376. * otherwise they are NULL.
  1377. */
  1378. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  1379. struct page **pagep, enum sgp_type sgp, gfp_t gfp,
  1380. struct mm_struct *fault_mm, int *fault_type)
  1381. {
  1382. struct address_space *mapping = inode->i_mapping;
  1383. struct shmem_inode_info *info = SHMEM_I(inode);
  1384. struct shmem_sb_info *sbinfo;
  1385. struct mm_struct *charge_mm;
  1386. struct mem_cgroup *memcg;
  1387. struct page *page;
  1388. swp_entry_t swap;
  1389. enum sgp_type sgp_huge = sgp;
  1390. pgoff_t hindex = index;
  1391. int error;
  1392. int once = 0;
  1393. int alloced = 0;
  1394. if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
  1395. return -EFBIG;
  1396. if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
  1397. sgp = SGP_CACHE;
  1398. repeat:
  1399. swap.val = 0;
  1400. page = find_lock_entry(mapping, index);
  1401. if (radix_tree_exceptional_entry(page)) {
  1402. swap = radix_to_swp_entry(page);
  1403. page = NULL;
  1404. }
  1405. if (sgp <= SGP_CACHE &&
  1406. ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
  1407. error = -EINVAL;
  1408. goto unlock;
  1409. }
  1410. if (page && sgp == SGP_WRITE)
  1411. mark_page_accessed(page);
  1412. /* fallocated page? */
  1413. if (page && !PageUptodate(page)) {
  1414. if (sgp != SGP_READ)
  1415. goto clear;
  1416. unlock_page(page);
  1417. put_page(page);
  1418. page = NULL;
  1419. }
  1420. if (page || (sgp == SGP_READ && !swap.val)) {
  1421. *pagep = page;
  1422. return 0;
  1423. }
  1424. /*
  1425. * Fast cache lookup did not find it:
  1426. * bring it back from swap or allocate.
  1427. */
  1428. sbinfo = SHMEM_SB(inode->i_sb);
  1429. charge_mm = fault_mm ? : current->mm;
  1430. if (swap.val) {
  1431. /* Look it up and read it in.. */
  1432. page = lookup_swap_cache(swap);
  1433. if (!page) {
  1434. /* Or update major stats only when swapin succeeds?? */
  1435. if (fault_type) {
  1436. *fault_type |= VM_FAULT_MAJOR;
  1437. count_vm_event(PGMAJFAULT);
  1438. mem_cgroup_count_vm_event(fault_mm, PGMAJFAULT);
  1439. }
  1440. /* Here we actually start the io */
  1441. page = shmem_swapin(swap, gfp, info, index);
  1442. if (!page) {
  1443. error = -ENOMEM;
  1444. goto failed;
  1445. }
  1446. }
  1447. /* We have to do this with page locked to prevent races */
  1448. lock_page(page);
  1449. if (!PageSwapCache(page) || page_private(page) != swap.val ||
  1450. !shmem_confirm_swap(mapping, index, swap)) {
  1451. error = -EEXIST; /* try again */
  1452. goto unlock;
  1453. }
  1454. if (!PageUptodate(page)) {
  1455. error = -EIO;
  1456. goto failed;
  1457. }
  1458. wait_on_page_writeback(page);
  1459. if (shmem_should_replace_page(page, gfp)) {
  1460. error = shmem_replace_page(&page, gfp, info, index);
  1461. if (error)
  1462. goto failed;
  1463. }
  1464. error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
  1465. false);
  1466. if (!error) {
  1467. error = shmem_add_to_page_cache(page, mapping, index,
  1468. swp_to_radix_entry(swap));
  1469. /*
  1470. * We already confirmed swap under page lock, and make
  1471. * no memory allocation here, so usually no possibility
  1472. * of error; but free_swap_and_cache() only trylocks a
  1473. * page, so it is just possible that the entry has been
  1474. * truncated or holepunched since swap was confirmed.
  1475. * shmem_undo_range() will have done some of the
  1476. * unaccounting, now delete_from_swap_cache() will do
  1477. * the rest.
  1478. * Reset swap.val? No, leave it so "failed" goes back to
  1479. * "repeat": reading a hole and writing should succeed.
  1480. */
  1481. if (error) {
  1482. mem_cgroup_cancel_charge(page, memcg, false);
  1483. delete_from_swap_cache(page);
  1484. }
  1485. }
  1486. if (error)
  1487. goto failed;
  1488. mem_cgroup_commit_charge(page, memcg, true, false);
  1489. spin_lock_irq(&info->lock);
  1490. info->swapped--;
  1491. shmem_recalc_inode(inode);
  1492. spin_unlock_irq(&info->lock);
  1493. if (sgp == SGP_WRITE)
  1494. mark_page_accessed(page);
  1495. delete_from_swap_cache(page);
  1496. set_page_dirty(page);
  1497. swap_free(swap);
  1498. } else {
  1499. /* shmem_symlink() */
  1500. if (mapping->a_ops != &shmem_aops)
  1501. goto alloc_nohuge;
  1502. if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
  1503. goto alloc_nohuge;
  1504. if (shmem_huge == SHMEM_HUGE_FORCE)
  1505. goto alloc_huge;
  1506. switch (sbinfo->huge) {
  1507. loff_t i_size;
  1508. pgoff_t off;
  1509. case SHMEM_HUGE_NEVER:
  1510. goto alloc_nohuge;
  1511. case SHMEM_HUGE_WITHIN_SIZE:
  1512. off = round_up(index, HPAGE_PMD_NR);
  1513. i_size = round_up(i_size_read(inode), PAGE_SIZE);
  1514. if (i_size >= HPAGE_PMD_SIZE &&
  1515. i_size >> PAGE_SHIFT >= off)
  1516. goto alloc_huge;
  1517. /* fallthrough */
  1518. case SHMEM_HUGE_ADVISE:
  1519. if (sgp_huge == SGP_HUGE)
  1520. goto alloc_huge;
  1521. /* TODO: implement fadvise() hints */
  1522. goto alloc_nohuge;
  1523. }
  1524. alloc_huge:
  1525. page = shmem_alloc_and_acct_page(gfp, inode, index, true);
  1526. if (IS_ERR(page)) {
  1527. alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
  1528. index, false);
  1529. }
  1530. if (IS_ERR(page)) {
  1531. int retry = 5;
  1532. error = PTR_ERR(page);
  1533. page = NULL;
  1534. if (error != -ENOSPC)
  1535. goto failed;
  1536. /*
  1537. * Try to reclaim some spece by splitting a huge page
  1538. * beyond i_size on the filesystem.
  1539. */
  1540. while (retry--) {
  1541. int ret;
  1542. ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
  1543. if (ret == SHRINK_STOP)
  1544. break;
  1545. if (ret)
  1546. goto alloc_nohuge;
  1547. }
  1548. goto failed;
  1549. }
  1550. if (PageTransHuge(page))
  1551. hindex = round_down(index, HPAGE_PMD_NR);
  1552. else
  1553. hindex = index;
  1554. if (sgp == SGP_WRITE)
  1555. __SetPageReferenced(page);
  1556. error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
  1557. PageTransHuge(page));
  1558. if (error)
  1559. goto unacct;
  1560. error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK,
  1561. compound_order(page));
  1562. if (!error) {
  1563. error = shmem_add_to_page_cache(page, mapping, hindex,
  1564. NULL);
  1565. radix_tree_preload_end();
  1566. }
  1567. if (error) {
  1568. mem_cgroup_cancel_charge(page, memcg,
  1569. PageTransHuge(page));
  1570. goto unacct;
  1571. }
  1572. mem_cgroup_commit_charge(page, memcg, false,
  1573. PageTransHuge(page));
  1574. lru_cache_add_anon(page);
  1575. spin_lock_irq(&info->lock);
  1576. info->alloced += 1 << compound_order(page);
  1577. inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
  1578. shmem_recalc_inode(inode);
  1579. spin_unlock_irq(&info->lock);
  1580. alloced = true;
  1581. if (PageTransHuge(page) &&
  1582. DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
  1583. hindex + HPAGE_PMD_NR - 1) {
  1584. /*
  1585. * Part of the huge page is beyond i_size: subject
  1586. * to shrink under memory pressure.
  1587. */
  1588. spin_lock(&sbinfo->shrinklist_lock);
  1589. /*
  1590. * _careful to defend against unlocked access to
  1591. * ->shrink_list in shmem_unused_huge_shrink()
  1592. */
  1593. if (list_empty_careful(&info->shrinklist)) {
  1594. list_add_tail(&info->shrinklist,
  1595. &sbinfo->shrinklist);
  1596. sbinfo->shrinklist_len++;
  1597. }
  1598. spin_unlock(&sbinfo->shrinklist_lock);
  1599. }
  1600. /*
  1601. * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
  1602. */
  1603. if (sgp == SGP_FALLOC)
  1604. sgp = SGP_WRITE;
  1605. clear:
  1606. /*
  1607. * Let SGP_WRITE caller clear ends if write does not fill page;
  1608. * but SGP_FALLOC on a page fallocated earlier must initialize
  1609. * it now, lest undo on failure cancel our earlier guarantee.
  1610. */
  1611. if (sgp != SGP_WRITE && !PageUptodate(page)) {
  1612. struct page *head = compound_head(page);
  1613. int i;
  1614. for (i = 0; i < (1 << compound_order(head)); i++) {
  1615. clear_highpage(head + i);
  1616. flush_dcache_page(head + i);
  1617. }
  1618. SetPageUptodate(head);
  1619. }
  1620. }
  1621. /* Perhaps the file has been truncated since we checked */
  1622. if (sgp <= SGP_CACHE &&
  1623. ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
  1624. if (alloced) {
  1625. ClearPageDirty(page);
  1626. delete_from_page_cache(page);
  1627. spin_lock_irq(&info->lock);
  1628. shmem_recalc_inode(inode);
  1629. spin_unlock_irq(&info->lock);
  1630. }
  1631. error = -EINVAL;
  1632. goto unlock;
  1633. }
  1634. *pagep = page + index - hindex;
  1635. return 0;
  1636. /*
  1637. * Error recovery.
  1638. */
  1639. unacct:
  1640. shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
  1641. if (PageTransHuge(page)) {
  1642. unlock_page(page);
  1643. put_page(page);
  1644. goto alloc_nohuge;
  1645. }
  1646. failed:
  1647. if (swap.val && !shmem_confirm_swap(mapping, index, swap))
  1648. error = -EEXIST;
  1649. unlock:
  1650. if (page) {
  1651. unlock_page(page);
  1652. put_page(page);
  1653. }
  1654. if (error == -ENOSPC && !once++) {
  1655. spin_lock_irq(&info->lock);
  1656. shmem_recalc_inode(inode);
  1657. spin_unlock_irq(&info->lock);
  1658. goto repeat;
  1659. }
  1660. if (error == -EEXIST) /* from above or from radix_tree_insert */
  1661. goto repeat;
  1662. return error;
  1663. }
  1664. /*
  1665. * This is like autoremove_wake_function, but it removes the wait queue
  1666. * entry unconditionally - even if something else had already woken the
  1667. * target.
  1668. */
  1669. static int synchronous_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  1670. {
  1671. int ret = default_wake_function(wait, mode, sync, key);
  1672. list_del_init(&wait->task_list);
  1673. return ret;
  1674. }
  1675. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1676. {
  1677. struct inode *inode = file_inode(vma->vm_file);
  1678. gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
  1679. enum sgp_type sgp;
  1680. int error;
  1681. int ret = VM_FAULT_LOCKED;
  1682. /*
  1683. * Trinity finds that probing a hole which tmpfs is punching can
  1684. * prevent the hole-punch from ever completing: which in turn
  1685. * locks writers out with its hold on i_mutex. So refrain from
  1686. * faulting pages into the hole while it's being punched. Although
  1687. * shmem_undo_range() does remove the additions, it may be unable to
  1688. * keep up, as each new page needs its own unmap_mapping_range() call,
  1689. * and the i_mmap tree grows ever slower to scan if new vmas are added.
  1690. *
  1691. * It does not matter if we sometimes reach this check just before the
  1692. * hole-punch begins, so that one fault then races with the punch:
  1693. * we just need to make racing faults a rare case.
  1694. *
  1695. * The implementation below would be much simpler if we just used a
  1696. * standard mutex or completion: but we cannot take i_mutex in fault,
  1697. * and bloating every shmem inode for this unlikely case would be sad.
  1698. */
  1699. if (unlikely(inode->i_private)) {
  1700. struct shmem_falloc *shmem_falloc;
  1701. spin_lock(&inode->i_lock);
  1702. shmem_falloc = inode->i_private;
  1703. if (shmem_falloc &&
  1704. shmem_falloc->waitq &&
  1705. vmf->pgoff >= shmem_falloc->start &&
  1706. vmf->pgoff < shmem_falloc->next) {
  1707. wait_queue_head_t *shmem_falloc_waitq;
  1708. DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
  1709. ret = VM_FAULT_NOPAGE;
  1710. if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
  1711. !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  1712. /* It's polite to up mmap_sem if we can */
  1713. up_read(&vma->vm_mm->mmap_sem);
  1714. ret = VM_FAULT_RETRY;
  1715. }
  1716. shmem_falloc_waitq = shmem_falloc->waitq;
  1717. prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
  1718. TASK_UNINTERRUPTIBLE);
  1719. spin_unlock(&inode->i_lock);
  1720. schedule();
  1721. /*
  1722. * shmem_falloc_waitq points into the shmem_fallocate()
  1723. * stack of the hole-punching task: shmem_falloc_waitq
  1724. * is usually invalid by the time we reach here, but
  1725. * finish_wait() does not dereference it in that case;
  1726. * though i_lock needed lest racing with wake_up_all().
  1727. */
  1728. spin_lock(&inode->i_lock);
  1729. finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
  1730. spin_unlock(&inode->i_lock);
  1731. return ret;
  1732. }
  1733. spin_unlock(&inode->i_lock);
  1734. }
  1735. sgp = SGP_CACHE;
  1736. if (vma->vm_flags & VM_HUGEPAGE)
  1737. sgp = SGP_HUGE;
  1738. else if (vma->vm_flags & VM_NOHUGEPAGE)
  1739. sgp = SGP_NOHUGE;
  1740. error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
  1741. gfp, vma->vm_mm, &ret);
  1742. if (error)
  1743. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  1744. return ret;
  1745. }
  1746. unsigned long shmem_get_unmapped_area(struct file *file,
  1747. unsigned long uaddr, unsigned long len,
  1748. unsigned long pgoff, unsigned long flags)
  1749. {
  1750. unsigned long (*get_area)(struct file *,
  1751. unsigned long, unsigned long, unsigned long, unsigned long);
  1752. unsigned long addr;
  1753. unsigned long offset;
  1754. unsigned long inflated_len;
  1755. unsigned long inflated_addr;
  1756. unsigned long inflated_offset;
  1757. if (len > TASK_SIZE)
  1758. return -ENOMEM;
  1759. get_area = current->mm->get_unmapped_area;
  1760. addr = get_area(file, uaddr, len, pgoff, flags);
  1761. if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
  1762. return addr;
  1763. if (IS_ERR_VALUE(addr))
  1764. return addr;
  1765. if (addr & ~PAGE_MASK)
  1766. return addr;
  1767. if (addr > TASK_SIZE - len)
  1768. return addr;
  1769. if (shmem_huge == SHMEM_HUGE_DENY)
  1770. return addr;
  1771. if (len < HPAGE_PMD_SIZE)
  1772. return addr;
  1773. if (flags & MAP_FIXED)
  1774. return addr;
  1775. /*
  1776. * Our priority is to support MAP_SHARED mapped hugely;
  1777. * and support MAP_PRIVATE mapped hugely too, until it is COWed.
  1778. * But if caller specified an address hint, respect that as before.
  1779. */
  1780. if (uaddr)
  1781. return addr;
  1782. if (shmem_huge != SHMEM_HUGE_FORCE) {
  1783. struct super_block *sb;
  1784. if (file) {
  1785. VM_BUG_ON(file->f_op != &shmem_file_operations);
  1786. sb = file_inode(file)->i_sb;
  1787. } else {
  1788. /*
  1789. * Called directly from mm/mmap.c, or drivers/char/mem.c
  1790. * for "/dev/zero", to create a shared anonymous object.
  1791. */
  1792. if (IS_ERR(shm_mnt))
  1793. return addr;
  1794. sb = shm_mnt->mnt_sb;
  1795. }
  1796. if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
  1797. return addr;
  1798. }
  1799. offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
  1800. if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
  1801. return addr;
  1802. if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
  1803. return addr;
  1804. inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
  1805. if (inflated_len > TASK_SIZE)
  1806. return addr;
  1807. if (inflated_len < len)
  1808. return addr;
  1809. inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
  1810. if (IS_ERR_VALUE(inflated_addr))
  1811. return addr;
  1812. if (inflated_addr & ~PAGE_MASK)
  1813. return addr;
  1814. inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
  1815. inflated_addr += offset - inflated_offset;
  1816. if (inflated_offset > offset)
  1817. inflated_addr += HPAGE_PMD_SIZE;
  1818. if (inflated_addr > TASK_SIZE - len)
  1819. return addr;
  1820. return inflated_addr;
  1821. }
  1822. #ifdef CONFIG_NUMA
  1823. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  1824. {
  1825. struct inode *inode = file_inode(vma->vm_file);
  1826. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  1827. }
  1828. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1829. unsigned long addr)
  1830. {
  1831. struct inode *inode = file_inode(vma->vm_file);
  1832. pgoff_t index;
  1833. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1834. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  1835. }
  1836. #endif
  1837. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1838. {
  1839. struct inode *inode = file_inode(file);
  1840. struct shmem_inode_info *info = SHMEM_I(inode);
  1841. int retval = -ENOMEM;
  1842. spin_lock_irq(&info->lock);
  1843. if (lock && !(info->flags & VM_LOCKED)) {
  1844. if (!user_shm_lock(inode->i_size, user))
  1845. goto out_nomem;
  1846. info->flags |= VM_LOCKED;
  1847. mapping_set_unevictable(file->f_mapping);
  1848. }
  1849. if (!lock && (info->flags & VM_LOCKED) && user) {
  1850. user_shm_unlock(inode->i_size, user);
  1851. info->flags &= ~VM_LOCKED;
  1852. mapping_clear_unevictable(file->f_mapping);
  1853. }
  1854. retval = 0;
  1855. out_nomem:
  1856. spin_unlock_irq(&info->lock);
  1857. return retval;
  1858. }
  1859. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1860. {
  1861. struct shmem_inode_info *info = SHMEM_I(file_inode(file));
  1862. if (info->seals & F_SEAL_FUTURE_WRITE) {
  1863. /*
  1864. * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
  1865. * "future write" seal active.
  1866. */
  1867. if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
  1868. return -EPERM;
  1869. /*
  1870. * Since the F_SEAL_FUTURE_WRITE seals allow for a MAP_SHARED
  1871. * read-only mapping, take care to not allow mprotect to revert
  1872. * protections.
  1873. */
  1874. vma->vm_flags &= ~(VM_MAYWRITE);
  1875. }
  1876. file_accessed(file);
  1877. vma->vm_ops = &shmem_vm_ops;
  1878. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
  1879. ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
  1880. (vma->vm_end & HPAGE_PMD_MASK)) {
  1881. khugepaged_enter(vma, vma->vm_flags);
  1882. }
  1883. return 0;
  1884. }
  1885. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1886. umode_t mode, dev_t dev, unsigned long flags)
  1887. {
  1888. struct inode *inode;
  1889. struct shmem_inode_info *info;
  1890. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1891. if (shmem_reserve_inode(sb))
  1892. return NULL;
  1893. inode = new_inode(sb);
  1894. if (inode) {
  1895. inode->i_ino = get_next_ino();
  1896. inode_init_owner(inode, dir, mode);
  1897. inode->i_blocks = 0;
  1898. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  1899. inode->i_generation = get_seconds();
  1900. info = SHMEM_I(inode);
  1901. memset(info, 0, (char *)inode - (char *)info);
  1902. spin_lock_init(&info->lock);
  1903. info->seals = F_SEAL_SEAL;
  1904. info->flags = flags & VM_NORESERVE;
  1905. INIT_LIST_HEAD(&info->shrinklist);
  1906. INIT_LIST_HEAD(&info->swaplist);
  1907. simple_xattrs_init(&info->xattrs);
  1908. cache_no_acl(inode);
  1909. switch (mode & S_IFMT) {
  1910. default:
  1911. inode->i_op = &shmem_special_inode_operations;
  1912. init_special_inode(inode, mode, dev);
  1913. break;
  1914. case S_IFREG:
  1915. inode->i_mapping->a_ops = &shmem_aops;
  1916. inode->i_op = &shmem_inode_operations;
  1917. inode->i_fop = &shmem_file_operations;
  1918. mpol_shared_policy_init(&info->policy,
  1919. shmem_get_sbmpol(sbinfo));
  1920. break;
  1921. case S_IFDIR:
  1922. inc_nlink(inode);
  1923. /* Some things misbehave if size == 0 on a directory */
  1924. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1925. inode->i_op = &shmem_dir_inode_operations;
  1926. inode->i_fop = &simple_dir_operations;
  1927. break;
  1928. case S_IFLNK:
  1929. /*
  1930. * Must not load anything in the rbtree,
  1931. * mpol_free_shared_policy will not be called.
  1932. */
  1933. mpol_shared_policy_init(&info->policy, NULL);
  1934. break;
  1935. }
  1936. lockdep_annotate_inode_mutex_key(inode);
  1937. } else
  1938. shmem_free_inode(sb);
  1939. return inode;
  1940. }
  1941. bool shmem_mapping(struct address_space *mapping)
  1942. {
  1943. if (!mapping->host)
  1944. return false;
  1945. return mapping->host->i_sb->s_op == &shmem_ops;
  1946. }
  1947. #ifdef CONFIG_TMPFS
  1948. static const struct inode_operations shmem_symlink_inode_operations;
  1949. static const struct inode_operations shmem_short_symlink_operations;
  1950. #ifdef CONFIG_TMPFS_XATTR
  1951. static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
  1952. #else
  1953. #define shmem_initxattrs NULL
  1954. #endif
  1955. static int
  1956. shmem_write_begin(struct file *file, struct address_space *mapping,
  1957. loff_t pos, unsigned len, unsigned flags,
  1958. struct page **pagep, void **fsdata)
  1959. {
  1960. struct inode *inode = mapping->host;
  1961. struct shmem_inode_info *info = SHMEM_I(inode);
  1962. pgoff_t index = pos >> PAGE_SHIFT;
  1963. /* i_mutex is held by caller */
  1964. if (unlikely(info->seals)) {
  1965. if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
  1966. return -EPERM;
  1967. if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
  1968. return -EPERM;
  1969. }
  1970. return shmem_getpage(inode, index, pagep, SGP_WRITE);
  1971. }
  1972. static int
  1973. shmem_write_end(struct file *file, struct address_space *mapping,
  1974. loff_t pos, unsigned len, unsigned copied,
  1975. struct page *page, void *fsdata)
  1976. {
  1977. struct inode *inode = mapping->host;
  1978. if (pos + copied > inode->i_size)
  1979. i_size_write(inode, pos + copied);
  1980. if (!PageUptodate(page)) {
  1981. struct page *head = compound_head(page);
  1982. if (PageTransCompound(page)) {
  1983. int i;
  1984. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1985. if (head + i == page)
  1986. continue;
  1987. clear_highpage(head + i);
  1988. flush_dcache_page(head + i);
  1989. }
  1990. }
  1991. if (copied < PAGE_SIZE) {
  1992. unsigned from = pos & (PAGE_SIZE - 1);
  1993. zero_user_segments(page, 0, from,
  1994. from + copied, PAGE_SIZE);
  1995. }
  1996. SetPageUptodate(head);
  1997. }
  1998. set_page_dirty(page);
  1999. unlock_page(page);
  2000. put_page(page);
  2001. return copied;
  2002. }
  2003. static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  2004. {
  2005. struct file *file = iocb->ki_filp;
  2006. struct inode *inode = file_inode(file);
  2007. struct address_space *mapping = inode->i_mapping;
  2008. pgoff_t index;
  2009. unsigned long offset;
  2010. enum sgp_type sgp = SGP_READ;
  2011. int error = 0;
  2012. ssize_t retval = 0;
  2013. loff_t *ppos = &iocb->ki_pos;
  2014. /*
  2015. * Might this read be for a stacking filesystem? Then when reading
  2016. * holes of a sparse file, we actually need to allocate those pages,
  2017. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  2018. */
  2019. if (!iter_is_iovec(to))
  2020. sgp = SGP_CACHE;
  2021. index = *ppos >> PAGE_SHIFT;
  2022. offset = *ppos & ~PAGE_MASK;
  2023. for (;;) {
  2024. struct page *page = NULL;
  2025. pgoff_t end_index;
  2026. unsigned long nr, ret;
  2027. loff_t i_size = i_size_read(inode);
  2028. end_index = i_size >> PAGE_SHIFT;
  2029. if (index > end_index)
  2030. break;
  2031. if (index == end_index) {
  2032. nr = i_size & ~PAGE_MASK;
  2033. if (nr <= offset)
  2034. break;
  2035. }
  2036. error = shmem_getpage(inode, index, &page, sgp);
  2037. if (error) {
  2038. if (error == -EINVAL)
  2039. error = 0;
  2040. break;
  2041. }
  2042. if (page) {
  2043. if (sgp == SGP_CACHE)
  2044. set_page_dirty(page);
  2045. unlock_page(page);
  2046. }
  2047. /*
  2048. * We must evaluate after, since reads (unlike writes)
  2049. * are called without i_mutex protection against truncate
  2050. */
  2051. nr = PAGE_SIZE;
  2052. i_size = i_size_read(inode);
  2053. end_index = i_size >> PAGE_SHIFT;
  2054. if (index == end_index) {
  2055. nr = i_size & ~PAGE_MASK;
  2056. if (nr <= offset) {
  2057. if (page)
  2058. put_page(page);
  2059. break;
  2060. }
  2061. }
  2062. nr -= offset;
  2063. if (page) {
  2064. /*
  2065. * If users can be writing to this page using arbitrary
  2066. * virtual addresses, take care about potential aliasing
  2067. * before reading the page on the kernel side.
  2068. */
  2069. if (mapping_writably_mapped(mapping))
  2070. flush_dcache_page(page);
  2071. /*
  2072. * Mark the page accessed if we read the beginning.
  2073. */
  2074. if (!offset)
  2075. mark_page_accessed(page);
  2076. } else {
  2077. page = ZERO_PAGE(0);
  2078. get_page(page);
  2079. }
  2080. /*
  2081. * Ok, we have the page, and it's up-to-date, so
  2082. * now we can copy it to user space...
  2083. */
  2084. ret = copy_page_to_iter(page, offset, nr, to);
  2085. retval += ret;
  2086. offset += ret;
  2087. index += offset >> PAGE_SHIFT;
  2088. offset &= ~PAGE_MASK;
  2089. put_page(page);
  2090. if (!iov_iter_count(to))
  2091. break;
  2092. if (ret < nr) {
  2093. error = -EFAULT;
  2094. break;
  2095. }
  2096. cond_resched();
  2097. }
  2098. *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
  2099. file_accessed(file);
  2100. return retval ? retval : error;
  2101. }
  2102. /*
  2103. * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
  2104. */
  2105. static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
  2106. pgoff_t index, pgoff_t end, int whence)
  2107. {
  2108. struct page *page;
  2109. struct pagevec pvec;
  2110. pgoff_t indices[PAGEVEC_SIZE];
  2111. bool done = false;
  2112. int i;
  2113. pagevec_init(&pvec, 0);
  2114. pvec.nr = 1; /* start small: we may be there already */
  2115. while (!done) {
  2116. pvec.nr = find_get_entries(mapping, index,
  2117. pvec.nr, pvec.pages, indices);
  2118. if (!pvec.nr) {
  2119. if (whence == SEEK_DATA)
  2120. index = end;
  2121. break;
  2122. }
  2123. for (i = 0; i < pvec.nr; i++, index++) {
  2124. if (index < indices[i]) {
  2125. if (whence == SEEK_HOLE) {
  2126. done = true;
  2127. break;
  2128. }
  2129. index = indices[i];
  2130. }
  2131. page = pvec.pages[i];
  2132. if (page && !radix_tree_exceptional_entry(page)) {
  2133. if (!PageUptodate(page))
  2134. page = NULL;
  2135. }
  2136. if (index >= end ||
  2137. (page && whence == SEEK_DATA) ||
  2138. (!page && whence == SEEK_HOLE)) {
  2139. done = true;
  2140. break;
  2141. }
  2142. }
  2143. pagevec_remove_exceptionals(&pvec);
  2144. pagevec_release(&pvec);
  2145. pvec.nr = PAGEVEC_SIZE;
  2146. cond_resched();
  2147. }
  2148. return index;
  2149. }
  2150. static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
  2151. {
  2152. struct address_space *mapping = file->f_mapping;
  2153. struct inode *inode = mapping->host;
  2154. pgoff_t start, end;
  2155. loff_t new_offset;
  2156. if (whence != SEEK_DATA && whence != SEEK_HOLE)
  2157. return generic_file_llseek_size(file, offset, whence,
  2158. MAX_LFS_FILESIZE, i_size_read(inode));
  2159. inode_lock(inode);
  2160. /* We're holding i_mutex so we can access i_size directly */
  2161. if (offset < 0 || offset >= inode->i_size)
  2162. offset = -ENXIO;
  2163. else {
  2164. start = offset >> PAGE_SHIFT;
  2165. end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2166. new_offset = shmem_seek_hole_data(mapping, start, end, whence);
  2167. new_offset <<= PAGE_SHIFT;
  2168. if (new_offset > offset) {
  2169. if (new_offset < inode->i_size)
  2170. offset = new_offset;
  2171. else if (whence == SEEK_DATA)
  2172. offset = -ENXIO;
  2173. else
  2174. offset = inode->i_size;
  2175. }
  2176. }
  2177. if (offset >= 0)
  2178. offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
  2179. inode_unlock(inode);
  2180. return offset;
  2181. }
  2182. /*
  2183. * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
  2184. * so reuse a tag which we firmly believe is never set or cleared on shmem.
  2185. */
  2186. #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
  2187. #define LAST_SCAN 4 /* about 150ms max */
  2188. static void shmem_tag_pins(struct address_space *mapping)
  2189. {
  2190. struct radix_tree_iter iter;
  2191. void **slot;
  2192. pgoff_t start;
  2193. struct page *page;
  2194. unsigned int tagged = 0;
  2195. lru_add_drain();
  2196. start = 0;
  2197. spin_lock_irq(&mapping->tree_lock);
  2198. radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
  2199. page = radix_tree_deref_slot(slot);
  2200. if (!page || radix_tree_exception(page)) {
  2201. if (radix_tree_deref_retry(page)) {
  2202. slot = radix_tree_iter_retry(&iter);
  2203. continue;
  2204. }
  2205. } else if (page_count(page) - page_mapcount(page) > 1) {
  2206. radix_tree_tag_set(&mapping->page_tree, iter.index,
  2207. SHMEM_TAG_PINNED);
  2208. }
  2209. if (++tagged % 1024)
  2210. continue;
  2211. slot = radix_tree_iter_next(&iter);
  2212. spin_unlock_irq(&mapping->tree_lock);
  2213. cond_resched();
  2214. spin_lock_irq(&mapping->tree_lock);
  2215. }
  2216. spin_unlock_irq(&mapping->tree_lock);
  2217. }
  2218. /*
  2219. * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
  2220. * via get_user_pages(), drivers might have some pending I/O without any active
  2221. * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
  2222. * and see whether it has an elevated ref-count. If so, we tag them and wait for
  2223. * them to be dropped.
  2224. * The caller must guarantee that no new user will acquire writable references
  2225. * to those pages to avoid races.
  2226. */
  2227. static int shmem_wait_for_pins(struct address_space *mapping)
  2228. {
  2229. struct radix_tree_iter iter;
  2230. void **slot;
  2231. pgoff_t start;
  2232. struct page *page;
  2233. int error, scan;
  2234. shmem_tag_pins(mapping);
  2235. error = 0;
  2236. for (scan = 0; scan <= LAST_SCAN; scan++) {
  2237. if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
  2238. break;
  2239. if (!scan)
  2240. lru_add_drain_all();
  2241. else if (schedule_timeout_killable((HZ << scan) / 200))
  2242. scan = LAST_SCAN;
  2243. start = 0;
  2244. rcu_read_lock();
  2245. radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
  2246. start, SHMEM_TAG_PINNED) {
  2247. page = radix_tree_deref_slot(slot);
  2248. if (radix_tree_exception(page)) {
  2249. if (radix_tree_deref_retry(page)) {
  2250. slot = radix_tree_iter_retry(&iter);
  2251. continue;
  2252. }
  2253. page = NULL;
  2254. }
  2255. if (page &&
  2256. page_count(page) - page_mapcount(page) != 1) {
  2257. if (scan < LAST_SCAN)
  2258. goto continue_resched;
  2259. /*
  2260. * On the last scan, we clean up all those tags
  2261. * we inserted; but make a note that we still
  2262. * found pages pinned.
  2263. */
  2264. error = -EBUSY;
  2265. }
  2266. spin_lock_irq(&mapping->tree_lock);
  2267. radix_tree_tag_clear(&mapping->page_tree,
  2268. iter.index, SHMEM_TAG_PINNED);
  2269. spin_unlock_irq(&mapping->tree_lock);
  2270. continue_resched:
  2271. if (need_resched()) {
  2272. cond_resched_rcu();
  2273. slot = radix_tree_iter_next(&iter);
  2274. }
  2275. }
  2276. rcu_read_unlock();
  2277. }
  2278. return error;
  2279. }
  2280. #define F_ALL_SEALS (F_SEAL_SEAL | \
  2281. F_SEAL_SHRINK | \
  2282. F_SEAL_GROW | \
  2283. F_SEAL_WRITE | \
  2284. F_SEAL_FUTURE_WRITE)
  2285. int shmem_add_seals(struct file *file, unsigned int seals)
  2286. {
  2287. struct inode *inode = file_inode(file);
  2288. struct shmem_inode_info *info = SHMEM_I(inode);
  2289. int error;
  2290. /*
  2291. * SEALING
  2292. * Sealing allows multiple parties to share a shmem-file but restrict
  2293. * access to a specific subset of file operations. Seals can only be
  2294. * added, but never removed. This way, mutually untrusted parties can
  2295. * share common memory regions with a well-defined policy. A malicious
  2296. * peer can thus never perform unwanted operations on a shared object.
  2297. *
  2298. * Seals are only supported on special shmem-files and always affect
  2299. * the whole underlying inode. Once a seal is set, it may prevent some
  2300. * kinds of access to the file. Currently, the following seals are
  2301. * defined:
  2302. * SEAL_SEAL: Prevent further seals from being set on this file
  2303. * SEAL_SHRINK: Prevent the file from shrinking
  2304. * SEAL_GROW: Prevent the file from growing
  2305. * SEAL_WRITE: Prevent write access to the file
  2306. *
  2307. * As we don't require any trust relationship between two parties, we
  2308. * must prevent seals from being removed. Therefore, sealing a file
  2309. * only adds a given set of seals to the file, it never touches
  2310. * existing seals. Furthermore, the "setting seals"-operation can be
  2311. * sealed itself, which basically prevents any further seal from being
  2312. * added.
  2313. *
  2314. * Semantics of sealing are only defined on volatile files. Only
  2315. * anonymous shmem files support sealing. More importantly, seals are
  2316. * never written to disk. Therefore, there's no plan to support it on
  2317. * other file types.
  2318. */
  2319. if (file->f_op != &shmem_file_operations)
  2320. return -EINVAL;
  2321. if (!(file->f_mode & FMODE_WRITE))
  2322. return -EPERM;
  2323. if (seals & ~(unsigned int)F_ALL_SEALS)
  2324. return -EINVAL;
  2325. inode_lock(inode);
  2326. if (info->seals & F_SEAL_SEAL) {
  2327. error = -EPERM;
  2328. goto unlock;
  2329. }
  2330. if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
  2331. error = mapping_deny_writable(file->f_mapping);
  2332. if (error)
  2333. goto unlock;
  2334. error = shmem_wait_for_pins(file->f_mapping);
  2335. if (error) {
  2336. mapping_allow_writable(file->f_mapping);
  2337. goto unlock;
  2338. }
  2339. }
  2340. info->seals |= seals;
  2341. error = 0;
  2342. unlock:
  2343. inode_unlock(inode);
  2344. return error;
  2345. }
  2346. EXPORT_SYMBOL_GPL(shmem_add_seals);
  2347. int shmem_get_seals(struct file *file)
  2348. {
  2349. if (file->f_op != &shmem_file_operations)
  2350. return -EINVAL;
  2351. return SHMEM_I(file_inode(file))->seals;
  2352. }
  2353. EXPORT_SYMBOL_GPL(shmem_get_seals);
  2354. long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
  2355. {
  2356. long error;
  2357. switch (cmd) {
  2358. case F_ADD_SEALS:
  2359. /* disallow upper 32bit */
  2360. if (arg > UINT_MAX)
  2361. return -EINVAL;
  2362. error = shmem_add_seals(file, arg);
  2363. break;
  2364. case F_GET_SEALS:
  2365. error = shmem_get_seals(file);
  2366. break;
  2367. default:
  2368. error = -EINVAL;
  2369. break;
  2370. }
  2371. return error;
  2372. }
  2373. static long shmem_fallocate(struct file *file, int mode, loff_t offset,
  2374. loff_t len)
  2375. {
  2376. struct inode *inode = file_inode(file);
  2377. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  2378. struct shmem_inode_info *info = SHMEM_I(inode);
  2379. struct shmem_falloc shmem_falloc;
  2380. pgoff_t start, index, end;
  2381. int error;
  2382. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  2383. return -EOPNOTSUPP;
  2384. inode_lock(inode);
  2385. if (mode & FALLOC_FL_PUNCH_HOLE) {
  2386. struct address_space *mapping = file->f_mapping;
  2387. loff_t unmap_start = round_up(offset, PAGE_SIZE);
  2388. loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
  2389. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
  2390. /* protected by i_mutex */
  2391. if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
  2392. error = -EPERM;
  2393. goto out;
  2394. }
  2395. shmem_falloc.waitq = &shmem_falloc_waitq;
  2396. shmem_falloc.start = unmap_start >> PAGE_SHIFT;
  2397. shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
  2398. spin_lock(&inode->i_lock);
  2399. inode->i_private = &shmem_falloc;
  2400. spin_unlock(&inode->i_lock);
  2401. if ((u64)unmap_end > (u64)unmap_start)
  2402. unmap_mapping_range(mapping, unmap_start,
  2403. 1 + unmap_end - unmap_start, 0);
  2404. shmem_truncate_range(inode, offset, offset + len - 1);
  2405. /* No need to unmap again: hole-punching leaves COWed pages */
  2406. spin_lock(&inode->i_lock);
  2407. inode->i_private = NULL;
  2408. wake_up_all(&shmem_falloc_waitq);
  2409. WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.task_list));
  2410. spin_unlock(&inode->i_lock);
  2411. error = 0;
  2412. goto out;
  2413. }
  2414. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  2415. error = inode_newsize_ok(inode, offset + len);
  2416. if (error)
  2417. goto out;
  2418. if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
  2419. error = -EPERM;
  2420. goto out;
  2421. }
  2422. start = offset >> PAGE_SHIFT;
  2423. end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2424. /* Try to avoid a swapstorm if len is impossible to satisfy */
  2425. if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
  2426. error = -ENOSPC;
  2427. goto out;
  2428. }
  2429. shmem_falloc.waitq = NULL;
  2430. shmem_falloc.start = start;
  2431. shmem_falloc.next = start;
  2432. shmem_falloc.nr_falloced = 0;
  2433. shmem_falloc.nr_unswapped = 0;
  2434. spin_lock(&inode->i_lock);
  2435. inode->i_private = &shmem_falloc;
  2436. spin_unlock(&inode->i_lock);
  2437. for (index = start; index < end; index++) {
  2438. struct page *page;
  2439. /*
  2440. * Good, the fallocate(2) manpage permits EINTR: we may have
  2441. * been interrupted because we are using up too much memory.
  2442. */
  2443. if (signal_pending(current))
  2444. error = -EINTR;
  2445. else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
  2446. error = -ENOMEM;
  2447. else
  2448. error = shmem_getpage(inode, index, &page, SGP_FALLOC);
  2449. if (error) {
  2450. /* Remove the !PageUptodate pages we added */
  2451. if (index > start) {
  2452. shmem_undo_range(inode,
  2453. (loff_t)start << PAGE_SHIFT,
  2454. ((loff_t)index << PAGE_SHIFT) - 1, true);
  2455. }
  2456. goto undone;
  2457. }
  2458. /*
  2459. * Inform shmem_writepage() how far we have reached.
  2460. * No need for lock or barrier: we have the page lock.
  2461. */
  2462. shmem_falloc.next++;
  2463. if (!PageUptodate(page))
  2464. shmem_falloc.nr_falloced++;
  2465. /*
  2466. * If !PageUptodate, leave it that way so that freeable pages
  2467. * can be recognized if we need to rollback on error later.
  2468. * But set_page_dirty so that memory pressure will swap rather
  2469. * than free the pages we are allocating (and SGP_CACHE pages
  2470. * might still be clean: we now need to mark those dirty too).
  2471. */
  2472. set_page_dirty(page);
  2473. unlock_page(page);
  2474. put_page(page);
  2475. cond_resched();
  2476. }
  2477. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  2478. i_size_write(inode, offset + len);
  2479. inode->i_ctime = current_time(inode);
  2480. undone:
  2481. spin_lock(&inode->i_lock);
  2482. inode->i_private = NULL;
  2483. spin_unlock(&inode->i_lock);
  2484. out:
  2485. inode_unlock(inode);
  2486. return error;
  2487. }
  2488. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  2489. {
  2490. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  2491. buf->f_type = TMPFS_MAGIC;
  2492. buf->f_bsize = PAGE_SIZE;
  2493. buf->f_namelen = NAME_MAX;
  2494. if (sbinfo->max_blocks) {
  2495. buf->f_blocks = sbinfo->max_blocks;
  2496. buf->f_bavail =
  2497. buf->f_bfree = sbinfo->max_blocks -
  2498. percpu_counter_sum(&sbinfo->used_blocks);
  2499. }
  2500. if (sbinfo->max_inodes) {
  2501. buf->f_files = sbinfo->max_inodes;
  2502. buf->f_ffree = sbinfo->free_inodes;
  2503. }
  2504. /* else leave those fields 0 like simple_statfs */
  2505. return 0;
  2506. }
  2507. /*
  2508. * File creation. Allocate an inode, and we're done..
  2509. */
  2510. static int
  2511. shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2512. {
  2513. struct inode *inode;
  2514. int error = -ENOSPC;
  2515. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  2516. if (inode) {
  2517. error = simple_acl_create(dir, inode);
  2518. if (error)
  2519. goto out_iput;
  2520. error = security_inode_init_security(inode, dir,
  2521. &dentry->d_name,
  2522. shmem_initxattrs, NULL);
  2523. if (error && error != -EOPNOTSUPP)
  2524. goto out_iput;
  2525. error = 0;
  2526. dir->i_size += BOGO_DIRENT_SIZE;
  2527. dir->i_ctime = dir->i_mtime = current_time(dir);
  2528. d_instantiate(dentry, inode);
  2529. dget(dentry); /* Extra count - pin the dentry in core */
  2530. }
  2531. return error;
  2532. out_iput:
  2533. iput(inode);
  2534. return error;
  2535. }
  2536. static int
  2537. shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  2538. {
  2539. struct inode *inode;
  2540. int error = -ENOSPC;
  2541. inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
  2542. if (inode) {
  2543. error = security_inode_init_security(inode, dir,
  2544. NULL,
  2545. shmem_initxattrs, NULL);
  2546. if (error && error != -EOPNOTSUPP)
  2547. goto out_iput;
  2548. error = simple_acl_create(dir, inode);
  2549. if (error)
  2550. goto out_iput;
  2551. d_tmpfile(dentry, inode);
  2552. }
  2553. return error;
  2554. out_iput:
  2555. iput(inode);
  2556. return error;
  2557. }
  2558. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2559. {
  2560. int error;
  2561. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  2562. return error;
  2563. inc_nlink(dir);
  2564. return 0;
  2565. }
  2566. static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2567. bool excl)
  2568. {
  2569. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  2570. }
  2571. /*
  2572. * Link a file..
  2573. */
  2574. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  2575. {
  2576. struct inode *inode = d_inode(old_dentry);
  2577. int ret = 0;
  2578. /*
  2579. * No ordinary (disk based) filesystem counts links as inodes;
  2580. * but each new link needs a new dentry, pinning lowmem, and
  2581. * tmpfs dentries cannot be pruned until they are unlinked.
  2582. * But if an O_TMPFILE file is linked into the tmpfs, the
  2583. * first link must skip that, to get the accounting right.
  2584. */
  2585. if (inode->i_nlink) {
  2586. ret = shmem_reserve_inode(inode->i_sb);
  2587. if (ret)
  2588. goto out;
  2589. }
  2590. dir->i_size += BOGO_DIRENT_SIZE;
  2591. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  2592. inc_nlink(inode);
  2593. ihold(inode); /* New dentry reference */
  2594. dget(dentry); /* Extra pinning count for the created dentry */
  2595. d_instantiate(dentry, inode);
  2596. out:
  2597. return ret;
  2598. }
  2599. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  2600. {
  2601. struct inode *inode = d_inode(dentry);
  2602. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  2603. shmem_free_inode(inode->i_sb);
  2604. dir->i_size -= BOGO_DIRENT_SIZE;
  2605. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  2606. drop_nlink(inode);
  2607. dput(dentry); /* Undo the count from "create" - this does all the work */
  2608. return 0;
  2609. }
  2610. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  2611. {
  2612. if (!simple_empty(dentry))
  2613. return -ENOTEMPTY;
  2614. drop_nlink(d_inode(dentry));
  2615. drop_nlink(dir);
  2616. return shmem_unlink(dir, dentry);
  2617. }
  2618. static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  2619. {
  2620. bool old_is_dir = d_is_dir(old_dentry);
  2621. bool new_is_dir = d_is_dir(new_dentry);
  2622. if (old_dir != new_dir && old_is_dir != new_is_dir) {
  2623. if (old_is_dir) {
  2624. drop_nlink(old_dir);
  2625. inc_nlink(new_dir);
  2626. } else {
  2627. drop_nlink(new_dir);
  2628. inc_nlink(old_dir);
  2629. }
  2630. }
  2631. old_dir->i_ctime = old_dir->i_mtime =
  2632. new_dir->i_ctime = new_dir->i_mtime =
  2633. d_inode(old_dentry)->i_ctime =
  2634. d_inode(new_dentry)->i_ctime = current_time(old_dir);
  2635. return 0;
  2636. }
  2637. static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
  2638. {
  2639. struct dentry *whiteout;
  2640. int error;
  2641. whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
  2642. if (!whiteout)
  2643. return -ENOMEM;
  2644. error = shmem_mknod(old_dir, whiteout,
  2645. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  2646. dput(whiteout);
  2647. if (error)
  2648. return error;
  2649. /*
  2650. * Cheat and hash the whiteout while the old dentry is still in
  2651. * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
  2652. *
  2653. * d_lookup() will consistently find one of them at this point,
  2654. * not sure which one, but that isn't even important.
  2655. */
  2656. d_rehash(whiteout);
  2657. return 0;
  2658. }
  2659. /*
  2660. * The VFS layer already does all the dentry stuff for rename,
  2661. * we just have to decrement the usage count for the target if
  2662. * it exists so that the VFS layer correctly free's it when it
  2663. * gets overwritten.
  2664. */
  2665. static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
  2666. {
  2667. struct inode *inode = d_inode(old_dentry);
  2668. int they_are_dirs = S_ISDIR(inode->i_mode);
  2669. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  2670. return -EINVAL;
  2671. if (flags & RENAME_EXCHANGE)
  2672. return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
  2673. if (!simple_empty(new_dentry))
  2674. return -ENOTEMPTY;
  2675. if (flags & RENAME_WHITEOUT) {
  2676. int error;
  2677. error = shmem_whiteout(old_dir, old_dentry);
  2678. if (error)
  2679. return error;
  2680. }
  2681. if (d_really_is_positive(new_dentry)) {
  2682. (void) shmem_unlink(new_dir, new_dentry);
  2683. if (they_are_dirs) {
  2684. drop_nlink(d_inode(new_dentry));
  2685. drop_nlink(old_dir);
  2686. }
  2687. } else if (they_are_dirs) {
  2688. drop_nlink(old_dir);
  2689. inc_nlink(new_dir);
  2690. }
  2691. old_dir->i_size -= BOGO_DIRENT_SIZE;
  2692. new_dir->i_size += BOGO_DIRENT_SIZE;
  2693. old_dir->i_ctime = old_dir->i_mtime =
  2694. new_dir->i_ctime = new_dir->i_mtime =
  2695. inode->i_ctime = current_time(old_dir);
  2696. return 0;
  2697. }
  2698. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  2699. {
  2700. int error;
  2701. int len;
  2702. struct inode *inode;
  2703. struct page *page;
  2704. struct shmem_inode_info *info;
  2705. len = strlen(symname) + 1;
  2706. if (len > PAGE_SIZE)
  2707. return -ENAMETOOLONG;
  2708. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  2709. if (!inode)
  2710. return -ENOSPC;
  2711. error = security_inode_init_security(inode, dir, &dentry->d_name,
  2712. shmem_initxattrs, NULL);
  2713. if (error) {
  2714. if (error != -EOPNOTSUPP) {
  2715. iput(inode);
  2716. return error;
  2717. }
  2718. error = 0;
  2719. }
  2720. info = SHMEM_I(inode);
  2721. inode->i_size = len-1;
  2722. if (len <= SHORT_SYMLINK_LEN) {
  2723. inode->i_link = kmemdup(symname, len, GFP_KERNEL);
  2724. if (!inode->i_link) {
  2725. iput(inode);
  2726. return -ENOMEM;
  2727. }
  2728. inode->i_op = &shmem_short_symlink_operations;
  2729. } else {
  2730. inode_nohighmem(inode);
  2731. error = shmem_getpage(inode, 0, &page, SGP_WRITE);
  2732. if (error) {
  2733. iput(inode);
  2734. return error;
  2735. }
  2736. inode->i_mapping->a_ops = &shmem_aops;
  2737. inode->i_op = &shmem_symlink_inode_operations;
  2738. memcpy(page_address(page), symname, len);
  2739. SetPageUptodate(page);
  2740. set_page_dirty(page);
  2741. unlock_page(page);
  2742. put_page(page);
  2743. }
  2744. dir->i_size += BOGO_DIRENT_SIZE;
  2745. dir->i_ctime = dir->i_mtime = current_time(dir);
  2746. d_instantiate(dentry, inode);
  2747. dget(dentry);
  2748. return 0;
  2749. }
  2750. static void shmem_put_link(void *arg)
  2751. {
  2752. mark_page_accessed(arg);
  2753. put_page(arg);
  2754. }
  2755. static const char *shmem_get_link(struct dentry *dentry,
  2756. struct inode *inode,
  2757. struct delayed_call *done)
  2758. {
  2759. struct page *page = NULL;
  2760. int error;
  2761. if (!dentry) {
  2762. page = find_get_page(inode->i_mapping, 0);
  2763. if (!page)
  2764. return ERR_PTR(-ECHILD);
  2765. if (!PageUptodate(page)) {
  2766. put_page(page);
  2767. return ERR_PTR(-ECHILD);
  2768. }
  2769. } else {
  2770. error = shmem_getpage(inode, 0, &page, SGP_READ);
  2771. if (error)
  2772. return ERR_PTR(error);
  2773. unlock_page(page);
  2774. }
  2775. set_delayed_call(done, shmem_put_link, page);
  2776. return page_address(page);
  2777. }
  2778. #ifdef CONFIG_TMPFS_XATTR
  2779. /*
  2780. * Superblocks without xattr inode operations may get some security.* xattr
  2781. * support from the LSM "for free". As soon as we have any other xattrs
  2782. * like ACLs, we also need to implement the security.* handlers at
  2783. * filesystem level, though.
  2784. */
  2785. /*
  2786. * Callback for security_inode_init_security() for acquiring xattrs.
  2787. */
  2788. static int shmem_initxattrs(struct inode *inode,
  2789. const struct xattr *xattr_array,
  2790. void *fs_info)
  2791. {
  2792. struct shmem_inode_info *info = SHMEM_I(inode);
  2793. const struct xattr *xattr;
  2794. struct simple_xattr *new_xattr;
  2795. size_t len;
  2796. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  2797. new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
  2798. if (!new_xattr)
  2799. return -ENOMEM;
  2800. len = strlen(xattr->name) + 1;
  2801. new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
  2802. GFP_KERNEL);
  2803. if (!new_xattr->name) {
  2804. kfree(new_xattr);
  2805. return -ENOMEM;
  2806. }
  2807. memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
  2808. XATTR_SECURITY_PREFIX_LEN);
  2809. memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
  2810. xattr->name, len);
  2811. simple_xattr_list_add(&info->xattrs, new_xattr);
  2812. }
  2813. return 0;
  2814. }
  2815. static int shmem_xattr_handler_get(const struct xattr_handler *handler,
  2816. struct dentry *unused, struct inode *inode,
  2817. const char *name, void *buffer, size_t size)
  2818. {
  2819. struct shmem_inode_info *info = SHMEM_I(inode);
  2820. name = xattr_full_name(handler, name);
  2821. return simple_xattr_get(&info->xattrs, name, buffer, size);
  2822. }
  2823. static int shmem_xattr_handler_set(const struct xattr_handler *handler,
  2824. struct dentry *unused, struct inode *inode,
  2825. const char *name, const void *value,
  2826. size_t size, int flags)
  2827. {
  2828. struct shmem_inode_info *info = SHMEM_I(inode);
  2829. name = xattr_full_name(handler, name);
  2830. return simple_xattr_set(&info->xattrs, name, value, size, flags);
  2831. }
  2832. static const struct xattr_handler shmem_security_xattr_handler = {
  2833. .prefix = XATTR_SECURITY_PREFIX,
  2834. .get = shmem_xattr_handler_get,
  2835. .set = shmem_xattr_handler_set,
  2836. };
  2837. static const struct xattr_handler shmem_trusted_xattr_handler = {
  2838. .prefix = XATTR_TRUSTED_PREFIX,
  2839. .get = shmem_xattr_handler_get,
  2840. .set = shmem_xattr_handler_set,
  2841. };
  2842. static const struct xattr_handler *shmem_xattr_handlers[] = {
  2843. #ifdef CONFIG_TMPFS_POSIX_ACL
  2844. &posix_acl_access_xattr_handler,
  2845. &posix_acl_default_xattr_handler,
  2846. #endif
  2847. &shmem_security_xattr_handler,
  2848. &shmem_trusted_xattr_handler,
  2849. NULL
  2850. };
  2851. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  2852. {
  2853. struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
  2854. return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
  2855. }
  2856. #endif /* CONFIG_TMPFS_XATTR */
  2857. static const struct inode_operations shmem_short_symlink_operations = {
  2858. .readlink = generic_readlink,
  2859. .get_link = simple_get_link,
  2860. #ifdef CONFIG_TMPFS_XATTR
  2861. .listxattr = shmem_listxattr,
  2862. #endif
  2863. };
  2864. static const struct inode_operations shmem_symlink_inode_operations = {
  2865. .readlink = generic_readlink,
  2866. .get_link = shmem_get_link,
  2867. #ifdef CONFIG_TMPFS_XATTR
  2868. .listxattr = shmem_listxattr,
  2869. #endif
  2870. };
  2871. static struct dentry *shmem_get_parent(struct dentry *child)
  2872. {
  2873. return ERR_PTR(-ESTALE);
  2874. }
  2875. static int shmem_match(struct inode *ino, void *vfh)
  2876. {
  2877. __u32 *fh = vfh;
  2878. __u64 inum = fh[2];
  2879. inum = (inum << 32) | fh[1];
  2880. return ino->i_ino == inum && fh[0] == ino->i_generation;
  2881. }
  2882. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  2883. struct fid *fid, int fh_len, int fh_type)
  2884. {
  2885. struct inode *inode;
  2886. struct dentry *dentry = NULL;
  2887. u64 inum;
  2888. if (fh_len < 3)
  2889. return NULL;
  2890. inum = fid->raw[2];
  2891. inum = (inum << 32) | fid->raw[1];
  2892. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  2893. shmem_match, fid->raw);
  2894. if (inode) {
  2895. dentry = d_find_alias(inode);
  2896. iput(inode);
  2897. }
  2898. return dentry;
  2899. }
  2900. static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
  2901. struct inode *parent)
  2902. {
  2903. if (*len < 3) {
  2904. *len = 3;
  2905. return FILEID_INVALID;
  2906. }
  2907. if (inode_unhashed(inode)) {
  2908. /* Unfortunately insert_inode_hash is not idempotent,
  2909. * so as we hash inodes here rather than at creation
  2910. * time, we need a lock to ensure we only try
  2911. * to do it once
  2912. */
  2913. static DEFINE_SPINLOCK(lock);
  2914. spin_lock(&lock);
  2915. if (inode_unhashed(inode))
  2916. __insert_inode_hash(inode,
  2917. inode->i_ino + inode->i_generation);
  2918. spin_unlock(&lock);
  2919. }
  2920. fh[0] = inode->i_generation;
  2921. fh[1] = inode->i_ino;
  2922. fh[2] = ((__u64)inode->i_ino) >> 32;
  2923. *len = 3;
  2924. return 1;
  2925. }
  2926. static const struct export_operations shmem_export_ops = {
  2927. .get_parent = shmem_get_parent,
  2928. .encode_fh = shmem_encode_fh,
  2929. .fh_to_dentry = shmem_fh_to_dentry,
  2930. };
  2931. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  2932. bool remount)
  2933. {
  2934. char *this_char, *value, *rest;
  2935. struct mempolicy *mpol = NULL;
  2936. uid_t uid;
  2937. gid_t gid;
  2938. while (options != NULL) {
  2939. this_char = options;
  2940. for (;;) {
  2941. /*
  2942. * NUL-terminate this option: unfortunately,
  2943. * mount options form a comma-separated list,
  2944. * but mpol's nodelist may also contain commas.
  2945. */
  2946. options = strchr(options, ',');
  2947. if (options == NULL)
  2948. break;
  2949. options++;
  2950. if (!isdigit(*options)) {
  2951. options[-1] = '\0';
  2952. break;
  2953. }
  2954. }
  2955. if (!*this_char)
  2956. continue;
  2957. if ((value = strchr(this_char,'=')) != NULL) {
  2958. *value++ = 0;
  2959. } else {
  2960. pr_err("tmpfs: No value for mount option '%s'\n",
  2961. this_char);
  2962. goto error;
  2963. }
  2964. if (!strcmp(this_char,"size")) {
  2965. unsigned long long size;
  2966. size = memparse(value,&rest);
  2967. if (*rest == '%') {
  2968. size <<= PAGE_SHIFT;
  2969. size *= totalram_pages;
  2970. do_div(size, 100);
  2971. rest++;
  2972. }
  2973. if (*rest)
  2974. goto bad_val;
  2975. sbinfo->max_blocks =
  2976. DIV_ROUND_UP(size, PAGE_SIZE);
  2977. } else if (!strcmp(this_char,"nr_blocks")) {
  2978. sbinfo->max_blocks = memparse(value, &rest);
  2979. if (*rest)
  2980. goto bad_val;
  2981. } else if (!strcmp(this_char,"nr_inodes")) {
  2982. sbinfo->max_inodes = memparse(value, &rest);
  2983. if (*rest)
  2984. goto bad_val;
  2985. } else if (!strcmp(this_char,"mode")) {
  2986. if (remount)
  2987. continue;
  2988. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  2989. if (*rest)
  2990. goto bad_val;
  2991. } else if (!strcmp(this_char,"uid")) {
  2992. if (remount)
  2993. continue;
  2994. uid = simple_strtoul(value, &rest, 0);
  2995. if (*rest)
  2996. goto bad_val;
  2997. sbinfo->uid = make_kuid(current_user_ns(), uid);
  2998. if (!uid_valid(sbinfo->uid))
  2999. goto bad_val;
  3000. } else if (!strcmp(this_char,"gid")) {
  3001. if (remount)
  3002. continue;
  3003. gid = simple_strtoul(value, &rest, 0);
  3004. if (*rest)
  3005. goto bad_val;
  3006. sbinfo->gid = make_kgid(current_user_ns(), gid);
  3007. if (!gid_valid(sbinfo->gid))
  3008. goto bad_val;
  3009. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  3010. } else if (!strcmp(this_char, "huge")) {
  3011. int huge;
  3012. huge = shmem_parse_huge(value);
  3013. if (huge < 0)
  3014. goto bad_val;
  3015. if (!has_transparent_hugepage() &&
  3016. huge != SHMEM_HUGE_NEVER)
  3017. goto bad_val;
  3018. sbinfo->huge = huge;
  3019. #endif
  3020. #ifdef CONFIG_NUMA
  3021. } else if (!strcmp(this_char,"mpol")) {
  3022. mpol_put(mpol);
  3023. mpol = NULL;
  3024. if (mpol_parse_str(value, &mpol))
  3025. goto bad_val;
  3026. #endif
  3027. } else {
  3028. pr_err("tmpfs: Bad mount option %s\n", this_char);
  3029. goto error;
  3030. }
  3031. }
  3032. sbinfo->mpol = mpol;
  3033. return 0;
  3034. bad_val:
  3035. pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
  3036. value, this_char);
  3037. error:
  3038. mpol_put(mpol);
  3039. return 1;
  3040. }
  3041. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  3042. {
  3043. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  3044. struct shmem_sb_info config = *sbinfo;
  3045. unsigned long inodes;
  3046. int error = -EINVAL;
  3047. config.mpol = NULL;
  3048. if (shmem_parse_options(data, &config, true))
  3049. return error;
  3050. spin_lock(&sbinfo->stat_lock);
  3051. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  3052. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  3053. goto out;
  3054. if (config.max_inodes < inodes)
  3055. goto out;
  3056. /*
  3057. * Those tests disallow limited->unlimited while any are in use;
  3058. * but we must separately disallow unlimited->limited, because
  3059. * in that case we have no record of how much is already in use.
  3060. */
  3061. if (config.max_blocks && !sbinfo->max_blocks)
  3062. goto out;
  3063. if (config.max_inodes && !sbinfo->max_inodes)
  3064. goto out;
  3065. error = 0;
  3066. sbinfo->huge = config.huge;
  3067. sbinfo->max_blocks = config.max_blocks;
  3068. sbinfo->max_inodes = config.max_inodes;
  3069. sbinfo->free_inodes = config.max_inodes - inodes;
  3070. /*
  3071. * Preserve previous mempolicy unless mpol remount option was specified.
  3072. */
  3073. if (config.mpol) {
  3074. mpol_put(sbinfo->mpol);
  3075. sbinfo->mpol = config.mpol; /* transfers initial ref */
  3076. }
  3077. out:
  3078. spin_unlock(&sbinfo->stat_lock);
  3079. return error;
  3080. }
  3081. static int shmem_show_options(struct seq_file *seq, struct dentry *root)
  3082. {
  3083. struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
  3084. if (sbinfo->max_blocks != shmem_default_max_blocks())
  3085. seq_printf(seq, ",size=%luk",
  3086. sbinfo->max_blocks << (PAGE_SHIFT - 10));
  3087. if (sbinfo->max_inodes != shmem_default_max_inodes())
  3088. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  3089. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  3090. seq_printf(seq, ",mode=%03ho", sbinfo->mode);
  3091. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  3092. seq_printf(seq, ",uid=%u",
  3093. from_kuid_munged(&init_user_ns, sbinfo->uid));
  3094. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  3095. seq_printf(seq, ",gid=%u",
  3096. from_kgid_munged(&init_user_ns, sbinfo->gid));
  3097. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  3098. /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
  3099. if (sbinfo->huge)
  3100. seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
  3101. #endif
  3102. shmem_show_mpol(seq, sbinfo->mpol);
  3103. return 0;
  3104. }
  3105. #define MFD_NAME_PREFIX "memfd:"
  3106. #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
  3107. #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
  3108. #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING)
  3109. SYSCALL_DEFINE2(memfd_create,
  3110. const char __user *, uname,
  3111. unsigned int, flags)
  3112. {
  3113. struct shmem_inode_info *info;
  3114. struct file *file;
  3115. int fd, error;
  3116. char *name;
  3117. long len;
  3118. if (flags & ~(unsigned int)MFD_ALL_FLAGS)
  3119. return -EINVAL;
  3120. /* length includes terminating zero */
  3121. len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
  3122. if (len <= 0)
  3123. return -EFAULT;
  3124. if (len > MFD_NAME_MAX_LEN + 1)
  3125. return -EINVAL;
  3126. name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY);
  3127. if (!name)
  3128. return -ENOMEM;
  3129. strcpy(name, MFD_NAME_PREFIX);
  3130. if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
  3131. error = -EFAULT;
  3132. goto err_name;
  3133. }
  3134. /* terminating-zero may have changed after strnlen_user() returned */
  3135. if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
  3136. error = -EFAULT;
  3137. goto err_name;
  3138. }
  3139. fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
  3140. if (fd < 0) {
  3141. error = fd;
  3142. goto err_name;
  3143. }
  3144. file = shmem_file_setup(name, 0, VM_NORESERVE);
  3145. if (IS_ERR(file)) {
  3146. error = PTR_ERR(file);
  3147. goto err_fd;
  3148. }
  3149. info = SHMEM_I(file_inode(file));
  3150. file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
  3151. file->f_flags |= O_RDWR | O_LARGEFILE;
  3152. if (flags & MFD_ALLOW_SEALING)
  3153. info->seals &= ~F_SEAL_SEAL;
  3154. fd_install(fd, file);
  3155. kfree(name);
  3156. return fd;
  3157. err_fd:
  3158. put_unused_fd(fd);
  3159. err_name:
  3160. kfree(name);
  3161. return error;
  3162. }
  3163. #endif /* CONFIG_TMPFS */
  3164. static void shmem_put_super(struct super_block *sb)
  3165. {
  3166. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  3167. percpu_counter_destroy(&sbinfo->used_blocks);
  3168. mpol_put(sbinfo->mpol);
  3169. kfree(sbinfo);
  3170. sb->s_fs_info = NULL;
  3171. }
  3172. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  3173. {
  3174. struct inode *inode;
  3175. struct shmem_sb_info *sbinfo;
  3176. int err = -ENOMEM;
  3177. /* Round up to L1_CACHE_BYTES to resist false sharing */
  3178. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  3179. L1_CACHE_BYTES), GFP_KERNEL);
  3180. if (!sbinfo)
  3181. return -ENOMEM;
  3182. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  3183. sbinfo->uid = current_fsuid();
  3184. sbinfo->gid = current_fsgid();
  3185. sb->s_fs_info = sbinfo;
  3186. #ifdef CONFIG_TMPFS
  3187. /*
  3188. * Per default we only allow half of the physical ram per
  3189. * tmpfs instance, limiting inodes to one per page of lowmem;
  3190. * but the internal instance is left unlimited.
  3191. */
  3192. if (!(sb->s_flags & MS_KERNMOUNT)) {
  3193. sbinfo->max_blocks = shmem_default_max_blocks();
  3194. sbinfo->max_inodes = shmem_default_max_inodes();
  3195. if (shmem_parse_options(data, sbinfo, false)) {
  3196. err = -EINVAL;
  3197. goto failed;
  3198. }
  3199. } else {
  3200. sb->s_flags |= MS_NOUSER;
  3201. }
  3202. sb->s_export_op = &shmem_export_ops;
  3203. sb->s_flags |= MS_NOSEC;
  3204. #else
  3205. sb->s_flags |= MS_NOUSER;
  3206. #endif
  3207. spin_lock_init(&sbinfo->stat_lock);
  3208. if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
  3209. goto failed;
  3210. sbinfo->free_inodes = sbinfo->max_inodes;
  3211. spin_lock_init(&sbinfo->shrinklist_lock);
  3212. INIT_LIST_HEAD(&sbinfo->shrinklist);
  3213. sb->s_maxbytes = MAX_LFS_FILESIZE;
  3214. sb->s_blocksize = PAGE_SIZE;
  3215. sb->s_blocksize_bits = PAGE_SHIFT;
  3216. sb->s_magic = TMPFS_MAGIC;
  3217. sb->s_op = &shmem_ops;
  3218. sb->s_time_gran = 1;
  3219. #ifdef CONFIG_TMPFS_XATTR
  3220. sb->s_xattr = shmem_xattr_handlers;
  3221. #endif
  3222. #ifdef CONFIG_TMPFS_POSIX_ACL
  3223. sb->s_flags |= MS_POSIXACL;
  3224. #endif
  3225. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  3226. if (!inode)
  3227. goto failed;
  3228. inode->i_uid = sbinfo->uid;
  3229. inode->i_gid = sbinfo->gid;
  3230. sb->s_root = d_make_root(inode);
  3231. if (!sb->s_root)
  3232. goto failed;
  3233. return 0;
  3234. failed:
  3235. shmem_put_super(sb);
  3236. return err;
  3237. }
  3238. static struct kmem_cache *shmem_inode_cachep;
  3239. static struct inode *shmem_alloc_inode(struct super_block *sb)
  3240. {
  3241. struct shmem_inode_info *info;
  3242. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  3243. if (!info)
  3244. return NULL;
  3245. return &info->vfs_inode;
  3246. }
  3247. static void shmem_destroy_callback(struct rcu_head *head)
  3248. {
  3249. struct inode *inode = container_of(head, struct inode, i_rcu);
  3250. if (S_ISLNK(inode->i_mode))
  3251. kfree(inode->i_link);
  3252. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  3253. }
  3254. static void shmem_destroy_inode(struct inode *inode)
  3255. {
  3256. if (S_ISREG(inode->i_mode))
  3257. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  3258. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  3259. }
  3260. static void shmem_init_inode(void *foo)
  3261. {
  3262. struct shmem_inode_info *info = foo;
  3263. inode_init_once(&info->vfs_inode);
  3264. }
  3265. static int shmem_init_inodecache(void)
  3266. {
  3267. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  3268. sizeof(struct shmem_inode_info),
  3269. 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
  3270. return 0;
  3271. }
  3272. static void shmem_destroy_inodecache(void)
  3273. {
  3274. kmem_cache_destroy(shmem_inode_cachep);
  3275. }
  3276. static const struct address_space_operations shmem_aops = {
  3277. .writepage = shmem_writepage,
  3278. .set_page_dirty = __set_page_dirty_no_writeback,
  3279. #ifdef CONFIG_TMPFS
  3280. .write_begin = shmem_write_begin,
  3281. .write_end = shmem_write_end,
  3282. #endif
  3283. #ifdef CONFIG_MIGRATION
  3284. .migratepage = migrate_page,
  3285. #endif
  3286. .error_remove_page = generic_error_remove_page,
  3287. };
  3288. static const struct file_operations shmem_file_operations = {
  3289. .mmap = shmem_mmap,
  3290. .get_unmapped_area = shmem_get_unmapped_area,
  3291. #ifdef CONFIG_TMPFS
  3292. .llseek = shmem_file_llseek,
  3293. .read_iter = shmem_file_read_iter,
  3294. .write_iter = generic_file_write_iter,
  3295. .fsync = noop_fsync,
  3296. .splice_read = generic_file_splice_read,
  3297. .splice_write = iter_file_splice_write,
  3298. .fallocate = shmem_fallocate,
  3299. #endif
  3300. };
  3301. static const struct inode_operations shmem_inode_operations = {
  3302. .getattr = shmem_getattr,
  3303. .setattr = shmem_setattr,
  3304. #ifdef CONFIG_TMPFS_XATTR
  3305. .listxattr = shmem_listxattr,
  3306. .set_acl = simple_set_acl,
  3307. #endif
  3308. };
  3309. static const struct inode_operations shmem_dir_inode_operations = {
  3310. #ifdef CONFIG_TMPFS
  3311. .create = shmem_create,
  3312. .lookup = simple_lookup,
  3313. .link = shmem_link,
  3314. .unlink = shmem_unlink,
  3315. .symlink = shmem_symlink,
  3316. .mkdir = shmem_mkdir,
  3317. .rmdir = shmem_rmdir,
  3318. .mknod = shmem_mknod,
  3319. .rename = shmem_rename2,
  3320. .tmpfile = shmem_tmpfile,
  3321. #endif
  3322. #ifdef CONFIG_TMPFS_XATTR
  3323. .listxattr = shmem_listxattr,
  3324. #endif
  3325. #ifdef CONFIG_TMPFS_POSIX_ACL
  3326. .setattr = shmem_setattr,
  3327. .set_acl = simple_set_acl,
  3328. #endif
  3329. };
  3330. static const struct inode_operations shmem_special_inode_operations = {
  3331. #ifdef CONFIG_TMPFS_XATTR
  3332. .listxattr = shmem_listxattr,
  3333. #endif
  3334. #ifdef CONFIG_TMPFS_POSIX_ACL
  3335. .setattr = shmem_setattr,
  3336. .set_acl = simple_set_acl,
  3337. #endif
  3338. };
  3339. static const struct super_operations shmem_ops = {
  3340. .alloc_inode = shmem_alloc_inode,
  3341. .destroy_inode = shmem_destroy_inode,
  3342. #ifdef CONFIG_TMPFS
  3343. .statfs = shmem_statfs,
  3344. .remount_fs = shmem_remount_fs,
  3345. .show_options = shmem_show_options,
  3346. #endif
  3347. .evict_inode = shmem_evict_inode,
  3348. .drop_inode = generic_delete_inode,
  3349. .put_super = shmem_put_super,
  3350. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  3351. .nr_cached_objects = shmem_unused_huge_count,
  3352. .free_cached_objects = shmem_unused_huge_scan,
  3353. #endif
  3354. };
  3355. static const struct vm_operations_struct shmem_vm_ops = {
  3356. .fault = shmem_fault,
  3357. .map_pages = filemap_map_pages,
  3358. #ifdef CONFIG_NUMA
  3359. .set_policy = shmem_set_policy,
  3360. .get_policy = shmem_get_policy,
  3361. #endif
  3362. };
  3363. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  3364. int flags, const char *dev_name, void *data)
  3365. {
  3366. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  3367. }
  3368. static struct file_system_type shmem_fs_type = {
  3369. .owner = THIS_MODULE,
  3370. .name = "tmpfs",
  3371. .mount = shmem_mount,
  3372. .kill_sb = kill_litter_super,
  3373. .fs_flags = FS_USERNS_MOUNT,
  3374. };
  3375. int __init shmem_init(void)
  3376. {
  3377. int error;
  3378. /* If rootfs called this, don't re-init */
  3379. if (shmem_inode_cachep)
  3380. return 0;
  3381. error = shmem_init_inodecache();
  3382. if (error)
  3383. goto out3;
  3384. error = register_filesystem(&shmem_fs_type);
  3385. if (error) {
  3386. pr_err("Could not register tmpfs\n");
  3387. goto out2;
  3388. }
  3389. shm_mnt = kern_mount(&shmem_fs_type);
  3390. if (IS_ERR(shm_mnt)) {
  3391. error = PTR_ERR(shm_mnt);
  3392. pr_err("Could not kern_mount tmpfs\n");
  3393. goto out1;
  3394. }
  3395. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  3396. if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
  3397. SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
  3398. else
  3399. shmem_huge = 0; /* just in case it was patched */
  3400. #endif
  3401. return 0;
  3402. out1:
  3403. unregister_filesystem(&shmem_fs_type);
  3404. out2:
  3405. shmem_destroy_inodecache();
  3406. out3:
  3407. shm_mnt = ERR_PTR(error);
  3408. return error;
  3409. }
  3410. #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
  3411. static ssize_t shmem_enabled_show(struct kobject *kobj,
  3412. struct kobj_attribute *attr, char *buf)
  3413. {
  3414. int values[] = {
  3415. SHMEM_HUGE_ALWAYS,
  3416. SHMEM_HUGE_WITHIN_SIZE,
  3417. SHMEM_HUGE_ADVISE,
  3418. SHMEM_HUGE_NEVER,
  3419. SHMEM_HUGE_DENY,
  3420. SHMEM_HUGE_FORCE,
  3421. };
  3422. int i, count;
  3423. for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
  3424. const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
  3425. count += sprintf(buf + count, fmt,
  3426. shmem_format_huge(values[i]));
  3427. }
  3428. buf[count - 1] = '\n';
  3429. return count;
  3430. }
  3431. static ssize_t shmem_enabled_store(struct kobject *kobj,
  3432. struct kobj_attribute *attr, const char *buf, size_t count)
  3433. {
  3434. char tmp[16];
  3435. int huge;
  3436. if (count + 1 > sizeof(tmp))
  3437. return -EINVAL;
  3438. memcpy(tmp, buf, count);
  3439. tmp[count] = '\0';
  3440. if (count && tmp[count - 1] == '\n')
  3441. tmp[count - 1] = '\0';
  3442. huge = shmem_parse_huge(tmp);
  3443. if (huge == -EINVAL)
  3444. return -EINVAL;
  3445. if (!has_transparent_hugepage() &&
  3446. huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
  3447. return -EINVAL;
  3448. shmem_huge = huge;
  3449. if (shmem_huge > SHMEM_HUGE_DENY)
  3450. SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
  3451. return count;
  3452. }
  3453. struct kobj_attribute shmem_enabled_attr =
  3454. __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
  3455. #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
  3456. #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
  3457. bool shmem_huge_enabled(struct vm_area_struct *vma)
  3458. {
  3459. struct inode *inode = file_inode(vma->vm_file);
  3460. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  3461. loff_t i_size;
  3462. pgoff_t off;
  3463. if (shmem_huge == SHMEM_HUGE_FORCE)
  3464. return true;
  3465. if (shmem_huge == SHMEM_HUGE_DENY)
  3466. return false;
  3467. switch (sbinfo->huge) {
  3468. case SHMEM_HUGE_NEVER:
  3469. return false;
  3470. case SHMEM_HUGE_ALWAYS:
  3471. return true;
  3472. case SHMEM_HUGE_WITHIN_SIZE:
  3473. off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
  3474. i_size = round_up(i_size_read(inode), PAGE_SIZE);
  3475. if (i_size >= HPAGE_PMD_SIZE &&
  3476. i_size >> PAGE_SHIFT >= off)
  3477. return true;
  3478. case SHMEM_HUGE_ADVISE:
  3479. /* TODO: implement fadvise() hints */
  3480. return (vma->vm_flags & VM_HUGEPAGE);
  3481. default:
  3482. VM_BUG_ON(1);
  3483. return false;
  3484. }
  3485. }
  3486. #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
  3487. #else /* !CONFIG_SHMEM */
  3488. /*
  3489. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  3490. *
  3491. * This is intended for small system where the benefits of the full
  3492. * shmem code (swap-backed and resource-limited) are outweighed by
  3493. * their complexity. On systems without swap this code should be
  3494. * effectively equivalent, but much lighter weight.
  3495. */
  3496. static struct file_system_type shmem_fs_type = {
  3497. .name = "tmpfs",
  3498. .mount = ramfs_mount,
  3499. .kill_sb = kill_litter_super,
  3500. .fs_flags = FS_USERNS_MOUNT,
  3501. };
  3502. int __init shmem_init(void)
  3503. {
  3504. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  3505. shm_mnt = kern_mount(&shmem_fs_type);
  3506. BUG_ON(IS_ERR(shm_mnt));
  3507. return 0;
  3508. }
  3509. int shmem_unuse(swp_entry_t swap, struct page *page)
  3510. {
  3511. return 0;
  3512. }
  3513. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  3514. {
  3515. return 0;
  3516. }
  3517. void shmem_unlock_mapping(struct address_space *mapping)
  3518. {
  3519. }
  3520. #ifdef CONFIG_MMU
  3521. unsigned long shmem_get_unmapped_area(struct file *file,
  3522. unsigned long addr, unsigned long len,
  3523. unsigned long pgoff, unsigned long flags)
  3524. {
  3525. return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
  3526. }
  3527. #endif
  3528. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  3529. {
  3530. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  3531. }
  3532. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  3533. #define shmem_vm_ops generic_file_vm_ops
  3534. #define shmem_file_operations ramfs_file_operations
  3535. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  3536. #define shmem_acct_size(flags, size) 0
  3537. #define shmem_unacct_size(flags, size) do {} while (0)
  3538. #endif /* CONFIG_SHMEM */
  3539. /* common code */
  3540. static const struct dentry_operations anon_ops = {
  3541. .d_dname = simple_dname
  3542. };
  3543. static struct file *__shmem_file_setup(const char *name, loff_t size,
  3544. unsigned long flags, unsigned int i_flags)
  3545. {
  3546. struct file *res;
  3547. struct inode *inode;
  3548. struct path path;
  3549. struct super_block *sb;
  3550. struct qstr this;
  3551. if (IS_ERR(shm_mnt))
  3552. return ERR_CAST(shm_mnt);
  3553. if (size < 0 || size > MAX_LFS_FILESIZE)
  3554. return ERR_PTR(-EINVAL);
  3555. if (shmem_acct_size(flags, size))
  3556. return ERR_PTR(-ENOMEM);
  3557. res = ERR_PTR(-ENOMEM);
  3558. this.name = name;
  3559. this.len = strlen(name);
  3560. this.hash = 0; /* will go */
  3561. sb = shm_mnt->mnt_sb;
  3562. path.mnt = mntget(shm_mnt);
  3563. path.dentry = d_alloc_pseudo(sb, &this);
  3564. if (!path.dentry)
  3565. goto put_memory;
  3566. d_set_d_op(path.dentry, &anon_ops);
  3567. res = ERR_PTR(-ENOSPC);
  3568. inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  3569. if (!inode)
  3570. goto put_memory;
  3571. inode->i_flags |= i_flags;
  3572. d_instantiate(path.dentry, inode);
  3573. inode->i_size = size;
  3574. clear_nlink(inode); /* It is unlinked */
  3575. res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
  3576. if (IS_ERR(res))
  3577. goto put_path;
  3578. res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  3579. &shmem_file_operations);
  3580. if (IS_ERR(res))
  3581. goto put_path;
  3582. return res;
  3583. put_memory:
  3584. shmem_unacct_size(flags, size);
  3585. put_path:
  3586. path_put(&path);
  3587. return res;
  3588. }
  3589. /**
  3590. * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
  3591. * kernel internal. There will be NO LSM permission checks against the
  3592. * underlying inode. So users of this interface must do LSM checks at a
  3593. * higher layer. The users are the big_key and shm implementations. LSM
  3594. * checks are provided at the key or shm level rather than the inode.
  3595. * @name: name for dentry (to be seen in /proc/<pid>/maps
  3596. * @size: size to be set for the file
  3597. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  3598. */
  3599. struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
  3600. {
  3601. return __shmem_file_setup(name, size, flags, S_PRIVATE);
  3602. }
  3603. /**
  3604. * shmem_file_setup - get an unlinked file living in tmpfs
  3605. * @name: name for dentry (to be seen in /proc/<pid>/maps
  3606. * @size: size to be set for the file
  3607. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  3608. */
  3609. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  3610. {
  3611. return __shmem_file_setup(name, size, flags, 0);
  3612. }
  3613. EXPORT_SYMBOL_GPL(shmem_file_setup);
  3614. void shmem_set_file(struct vm_area_struct *vma, struct file *file)
  3615. {
  3616. if (vma->vm_file)
  3617. fput(vma->vm_file);
  3618. vma->vm_file = file;
  3619. vma->vm_ops = &shmem_vm_ops;
  3620. }
  3621. /**
  3622. * shmem_zero_setup - setup a shared anonymous mapping
  3623. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  3624. */
  3625. int shmem_zero_setup(struct vm_area_struct *vma)
  3626. {
  3627. struct file *file;
  3628. loff_t size = vma->vm_end - vma->vm_start;
  3629. /*
  3630. * Cloning a new file under mmap_sem leads to a lock ordering conflict
  3631. * between XFS directory reading and selinux: since this file is only
  3632. * accessible to the user through its mapping, use S_PRIVATE flag to
  3633. * bypass file security, in the same way as shmem_kernel_file_setup().
  3634. */
  3635. file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
  3636. if (IS_ERR(file))
  3637. return PTR_ERR(file);
  3638. shmem_set_file(vma, file);
  3639. if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
  3640. ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
  3641. (vma->vm_end & HPAGE_PMD_MASK)) {
  3642. khugepaged_enter(vma, vma->vm_flags);
  3643. }
  3644. return 0;
  3645. }
  3646. /**
  3647. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  3648. * @mapping: the page's address_space
  3649. * @index: the page index
  3650. * @gfp: the page allocator flags to use if allocating
  3651. *
  3652. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  3653. * with any new page allocations done using the specified allocation flags.
  3654. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  3655. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  3656. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  3657. *
  3658. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  3659. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  3660. */
  3661. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  3662. pgoff_t index, gfp_t gfp)
  3663. {
  3664. #ifdef CONFIG_SHMEM
  3665. struct inode *inode = mapping->host;
  3666. struct page *page;
  3667. int error;
  3668. BUG_ON(mapping->a_ops != &shmem_aops);
  3669. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
  3670. gfp, NULL, NULL);
  3671. if (error)
  3672. page = ERR_PTR(error);
  3673. else
  3674. unlock_page(page);
  3675. return page;
  3676. #else
  3677. /*
  3678. * The tiny !SHMEM case uses ramfs without swap
  3679. */
  3680. return read_cache_page_gfp(mapping, index, gfp);
  3681. #endif
  3682. }
  3683. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);