shmem.c 108 KB

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