shmem.c 109 KB

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