swapfile.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788
  1. /*
  2. * linux/mm/swapfile.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. * Swap reorganised 29.12.95, Stephen Tweedie
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/sched/mm.h>
  9. #include <linux/sched/task.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/mman.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/swap.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/namei.h>
  18. #include <linux/shmem_fs.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/random.h>
  21. #include <linux/writeback.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/seq_file.h>
  24. #include <linux/init.h>
  25. #include <linux/ksm.h>
  26. #include <linux/rmap.h>
  27. #include <linux/security.h>
  28. #include <linux/backing-dev.h>
  29. #include <linux/mutex.h>
  30. #include <linux/capability.h>
  31. #include <linux/syscalls.h>
  32. #include <linux/memcontrol.h>
  33. #include <linux/poll.h>
  34. #include <linux/oom.h>
  35. #include <linux/frontswap.h>
  36. #include <linux/swapfile.h>
  37. #include <linux/export.h>
  38. #include <linux/swap_slots.h>
  39. #include <linux/sort.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/tlbflush.h>
  42. #include <linux/swapops.h>
  43. #include <linux/swap_cgroup.h>
  44. static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
  45. unsigned char);
  46. static void free_swap_count_continuations(struct swap_info_struct *);
  47. static sector_t map_swap_entry(swp_entry_t, struct block_device**);
  48. DEFINE_SPINLOCK(swap_lock);
  49. static unsigned int nr_swapfiles;
  50. atomic_long_t nr_swap_pages;
  51. /*
  52. * Some modules use swappable objects and may try to swap them out under
  53. * memory pressure (via the shrinker). Before doing so, they may wish to
  54. * check to see if any swap space is available.
  55. */
  56. EXPORT_SYMBOL_GPL(nr_swap_pages);
  57. /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
  58. long total_swap_pages;
  59. static int least_priority = -1;
  60. static const char Bad_file[] = "Bad swap file entry ";
  61. static const char Unused_file[] = "Unused swap file entry ";
  62. static const char Bad_offset[] = "Bad swap offset entry ";
  63. static const char Unused_offset[] = "Unused swap offset entry ";
  64. /*
  65. * all active swap_info_structs
  66. * protected with swap_lock, and ordered by priority.
  67. */
  68. PLIST_HEAD(swap_active_head);
  69. /*
  70. * all available (active, not full) swap_info_structs
  71. * protected with swap_avail_lock, ordered by priority.
  72. * This is used by get_swap_page() instead of swap_active_head
  73. * because swap_active_head includes all swap_info_structs,
  74. * but get_swap_page() doesn't need to look at full ones.
  75. * This uses its own lock instead of swap_lock because when a
  76. * swap_info_struct changes between not-full/full, it needs to
  77. * add/remove itself to/from this list, but the swap_info_struct->lock
  78. * is held and the locking order requires swap_lock to be taken
  79. * before any swap_info_struct->lock.
  80. */
  81. static struct plist_head *swap_avail_heads;
  82. static DEFINE_SPINLOCK(swap_avail_lock);
  83. struct swap_info_struct *swap_info[MAX_SWAPFILES];
  84. static DEFINE_MUTEX(swapon_mutex);
  85. static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
  86. /* Activity counter to indicate that a swapon or swapoff has occurred */
  87. static atomic_t proc_poll_event = ATOMIC_INIT(0);
  88. atomic_t nr_rotate_swap = ATOMIC_INIT(0);
  89. static struct swap_info_struct *swap_type_to_swap_info(int type)
  90. {
  91. if (type >= READ_ONCE(nr_swapfiles))
  92. return NULL;
  93. smp_rmb(); /* Pairs with smp_wmb in alloc_swap_info. */
  94. return READ_ONCE(swap_info[type]);
  95. }
  96. static inline unsigned char swap_count(unsigned char ent)
  97. {
  98. return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */
  99. }
  100. /* returns 1 if swap entry is freed */
  101. static int
  102. __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
  103. {
  104. swp_entry_t entry = swp_entry(si->type, offset);
  105. struct page *page;
  106. int ret = 0;
  107. page = find_get_page(swap_address_space(entry), swp_offset(entry));
  108. if (!page)
  109. return 0;
  110. /*
  111. * This function is called from scan_swap_map() and it's called
  112. * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
  113. * We have to use trylock for avoiding deadlock. This is a special
  114. * case and you should use try_to_free_swap() with explicit lock_page()
  115. * in usual operations.
  116. */
  117. if (trylock_page(page)) {
  118. ret = try_to_free_swap(page);
  119. unlock_page(page);
  120. }
  121. put_page(page);
  122. return ret;
  123. }
  124. /*
  125. * swapon tell device that all the old swap contents can be discarded,
  126. * to allow the swap device to optimize its wear-levelling.
  127. */
  128. static int discard_swap(struct swap_info_struct *si)
  129. {
  130. struct swap_extent *se;
  131. sector_t start_block;
  132. sector_t nr_blocks;
  133. int err = 0;
  134. /* Do not discard the swap header page! */
  135. se = &si->first_swap_extent;
  136. start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
  137. nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
  138. if (nr_blocks) {
  139. err = blkdev_issue_discard(si->bdev, start_block,
  140. nr_blocks, GFP_KERNEL, 0);
  141. if (err)
  142. return err;
  143. cond_resched();
  144. }
  145. list_for_each_entry(se, &si->first_swap_extent.list, list) {
  146. start_block = se->start_block << (PAGE_SHIFT - 9);
  147. nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
  148. err = blkdev_issue_discard(si->bdev, start_block,
  149. nr_blocks, GFP_KERNEL, 0);
  150. if (err)
  151. break;
  152. cond_resched();
  153. }
  154. return err; /* That will often be -EOPNOTSUPP */
  155. }
  156. /*
  157. * swap allocation tell device that a cluster of swap can now be discarded,
  158. * to allow the swap device to optimize its wear-levelling.
  159. */
  160. static void discard_swap_cluster(struct swap_info_struct *si,
  161. pgoff_t start_page, pgoff_t nr_pages)
  162. {
  163. struct swap_extent *se = si->curr_swap_extent;
  164. int found_extent = 0;
  165. while (nr_pages) {
  166. if (se->start_page <= start_page &&
  167. start_page < se->start_page + se->nr_pages) {
  168. pgoff_t offset = start_page - se->start_page;
  169. sector_t start_block = se->start_block + offset;
  170. sector_t nr_blocks = se->nr_pages - offset;
  171. if (nr_blocks > nr_pages)
  172. nr_blocks = nr_pages;
  173. start_page += nr_blocks;
  174. nr_pages -= nr_blocks;
  175. if (!found_extent++)
  176. si->curr_swap_extent = se;
  177. start_block <<= PAGE_SHIFT - 9;
  178. nr_blocks <<= PAGE_SHIFT - 9;
  179. if (blkdev_issue_discard(si->bdev, start_block,
  180. nr_blocks, GFP_NOIO, 0))
  181. break;
  182. }
  183. se = list_next_entry(se, list);
  184. }
  185. }
  186. #ifdef CONFIG_THP_SWAP
  187. #define SWAPFILE_CLUSTER HPAGE_PMD_NR
  188. #define swap_entry_size(size) (size)
  189. #else
  190. #define SWAPFILE_CLUSTER 256
  191. /*
  192. * Define swap_entry_size() as constant to let compiler to optimize
  193. * out some code if !CONFIG_THP_SWAP
  194. */
  195. #define swap_entry_size(size) 1
  196. #endif
  197. #define LATENCY_LIMIT 256
  198. static inline void cluster_set_flag(struct swap_cluster_info *info,
  199. unsigned int flag)
  200. {
  201. info->flags = flag;
  202. }
  203. static inline unsigned int cluster_count(struct swap_cluster_info *info)
  204. {
  205. return info->data;
  206. }
  207. static inline void cluster_set_count(struct swap_cluster_info *info,
  208. unsigned int c)
  209. {
  210. info->data = c;
  211. }
  212. static inline void cluster_set_count_flag(struct swap_cluster_info *info,
  213. unsigned int c, unsigned int f)
  214. {
  215. info->flags = f;
  216. info->data = c;
  217. }
  218. static inline unsigned int cluster_next(struct swap_cluster_info *info)
  219. {
  220. return info->data;
  221. }
  222. static inline void cluster_set_next(struct swap_cluster_info *info,
  223. unsigned int n)
  224. {
  225. info->data = n;
  226. }
  227. static inline void cluster_set_next_flag(struct swap_cluster_info *info,
  228. unsigned int n, unsigned int f)
  229. {
  230. info->flags = f;
  231. info->data = n;
  232. }
  233. static inline bool cluster_is_free(struct swap_cluster_info *info)
  234. {
  235. return info->flags & CLUSTER_FLAG_FREE;
  236. }
  237. static inline bool cluster_is_null(struct swap_cluster_info *info)
  238. {
  239. return info->flags & CLUSTER_FLAG_NEXT_NULL;
  240. }
  241. static inline void cluster_set_null(struct swap_cluster_info *info)
  242. {
  243. info->flags = CLUSTER_FLAG_NEXT_NULL;
  244. info->data = 0;
  245. }
  246. static inline bool cluster_is_huge(struct swap_cluster_info *info)
  247. {
  248. if (IS_ENABLED(CONFIG_THP_SWAP))
  249. return info->flags & CLUSTER_FLAG_HUGE;
  250. return false;
  251. }
  252. static inline void cluster_clear_huge(struct swap_cluster_info *info)
  253. {
  254. info->flags &= ~CLUSTER_FLAG_HUGE;
  255. }
  256. static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si,
  257. unsigned long offset)
  258. {
  259. struct swap_cluster_info *ci;
  260. ci = si->cluster_info;
  261. if (ci) {
  262. ci += offset / SWAPFILE_CLUSTER;
  263. spin_lock(&ci->lock);
  264. }
  265. return ci;
  266. }
  267. static inline void unlock_cluster(struct swap_cluster_info *ci)
  268. {
  269. if (ci)
  270. spin_unlock(&ci->lock);
  271. }
  272. /*
  273. * Determine the locking method in use for this device. Return
  274. * swap_cluster_info if SSD-style cluster-based locking is in place.
  275. */
  276. static inline struct swap_cluster_info *lock_cluster_or_swap_info(
  277. struct swap_info_struct *si, unsigned long offset)
  278. {
  279. struct swap_cluster_info *ci;
  280. /* Try to use fine-grained SSD-style locking if available: */
  281. ci = lock_cluster(si, offset);
  282. /* Otherwise, fall back to traditional, coarse locking: */
  283. if (!ci)
  284. spin_lock(&si->lock);
  285. return ci;
  286. }
  287. static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si,
  288. struct swap_cluster_info *ci)
  289. {
  290. if (ci)
  291. unlock_cluster(ci);
  292. else
  293. spin_unlock(&si->lock);
  294. }
  295. static inline bool cluster_list_empty(struct swap_cluster_list *list)
  296. {
  297. return cluster_is_null(&list->head);
  298. }
  299. static inline unsigned int cluster_list_first(struct swap_cluster_list *list)
  300. {
  301. return cluster_next(&list->head);
  302. }
  303. static void cluster_list_init(struct swap_cluster_list *list)
  304. {
  305. cluster_set_null(&list->head);
  306. cluster_set_null(&list->tail);
  307. }
  308. static void cluster_list_add_tail(struct swap_cluster_list *list,
  309. struct swap_cluster_info *ci,
  310. unsigned int idx)
  311. {
  312. if (cluster_list_empty(list)) {
  313. cluster_set_next_flag(&list->head, idx, 0);
  314. cluster_set_next_flag(&list->tail, idx, 0);
  315. } else {
  316. struct swap_cluster_info *ci_tail;
  317. unsigned int tail = cluster_next(&list->tail);
  318. /*
  319. * Nested cluster lock, but both cluster locks are
  320. * only acquired when we held swap_info_struct->lock
  321. */
  322. ci_tail = ci + tail;
  323. spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING);
  324. cluster_set_next(ci_tail, idx);
  325. spin_unlock(&ci_tail->lock);
  326. cluster_set_next_flag(&list->tail, idx, 0);
  327. }
  328. }
  329. static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
  330. struct swap_cluster_info *ci)
  331. {
  332. unsigned int idx;
  333. idx = cluster_next(&list->head);
  334. if (cluster_next(&list->tail) == idx) {
  335. cluster_set_null(&list->head);
  336. cluster_set_null(&list->tail);
  337. } else
  338. cluster_set_next_flag(&list->head,
  339. cluster_next(&ci[idx]), 0);
  340. return idx;
  341. }
  342. /* Add a cluster to discard list and schedule it to do discard */
  343. static void swap_cluster_schedule_discard(struct swap_info_struct *si,
  344. unsigned int idx)
  345. {
  346. /*
  347. * If scan_swap_map() can't find a free cluster, it will check
  348. * si->swap_map directly. To make sure the discarding cluster isn't
  349. * taken by scan_swap_map(), mark the swap entries bad (occupied). It
  350. * will be cleared after discard
  351. */
  352. memset(si->swap_map + idx * SWAPFILE_CLUSTER,
  353. SWAP_MAP_BAD, SWAPFILE_CLUSTER);
  354. cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
  355. schedule_work(&si->discard_work);
  356. }
  357. static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
  358. {
  359. struct swap_cluster_info *ci = si->cluster_info;
  360. cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE);
  361. cluster_list_add_tail(&si->free_clusters, ci, idx);
  362. }
  363. /*
  364. * Doing discard actually. After a cluster discard is finished, the cluster
  365. * will be added to free cluster list. caller should hold si->lock.
  366. */
  367. static void swap_do_scheduled_discard(struct swap_info_struct *si)
  368. {
  369. struct swap_cluster_info *info, *ci;
  370. unsigned int idx;
  371. info = si->cluster_info;
  372. while (!cluster_list_empty(&si->discard_clusters)) {
  373. idx = cluster_list_del_first(&si->discard_clusters, info);
  374. spin_unlock(&si->lock);
  375. discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
  376. SWAPFILE_CLUSTER);
  377. spin_lock(&si->lock);
  378. ci = lock_cluster(si, idx * SWAPFILE_CLUSTER);
  379. __free_cluster(si, idx);
  380. memset(si->swap_map + idx * SWAPFILE_CLUSTER,
  381. 0, SWAPFILE_CLUSTER);
  382. unlock_cluster(ci);
  383. }
  384. }
  385. static void swap_discard_work(struct work_struct *work)
  386. {
  387. struct swap_info_struct *si;
  388. si = container_of(work, struct swap_info_struct, discard_work);
  389. spin_lock(&si->lock);
  390. swap_do_scheduled_discard(si);
  391. spin_unlock(&si->lock);
  392. }
  393. static void alloc_cluster(struct swap_info_struct *si, unsigned long idx)
  394. {
  395. struct swap_cluster_info *ci = si->cluster_info;
  396. VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx);
  397. cluster_list_del_first(&si->free_clusters, ci);
  398. cluster_set_count_flag(ci + idx, 0, 0);
  399. }
  400. static void free_cluster(struct swap_info_struct *si, unsigned long idx)
  401. {
  402. struct swap_cluster_info *ci = si->cluster_info + idx;
  403. VM_BUG_ON(cluster_count(ci) != 0);
  404. /*
  405. * If the swap is discardable, prepare discard the cluster
  406. * instead of free it immediately. The cluster will be freed
  407. * after discard.
  408. */
  409. if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
  410. (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
  411. swap_cluster_schedule_discard(si, idx);
  412. return;
  413. }
  414. __free_cluster(si, idx);
  415. }
  416. /*
  417. * The cluster corresponding to page_nr will be used. The cluster will be
  418. * removed from free cluster list and its usage counter will be increased.
  419. */
  420. static void inc_cluster_info_page(struct swap_info_struct *p,
  421. struct swap_cluster_info *cluster_info, unsigned long page_nr)
  422. {
  423. unsigned long idx = page_nr / SWAPFILE_CLUSTER;
  424. if (!cluster_info)
  425. return;
  426. if (cluster_is_free(&cluster_info[idx]))
  427. alloc_cluster(p, idx);
  428. VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
  429. cluster_set_count(&cluster_info[idx],
  430. cluster_count(&cluster_info[idx]) + 1);
  431. }
  432. /*
  433. * The cluster corresponding to page_nr decreases one usage. If the usage
  434. * counter becomes 0, which means no page in the cluster is in using, we can
  435. * optionally discard the cluster and add it to free cluster list.
  436. */
  437. static void dec_cluster_info_page(struct swap_info_struct *p,
  438. struct swap_cluster_info *cluster_info, unsigned long page_nr)
  439. {
  440. unsigned long idx = page_nr / SWAPFILE_CLUSTER;
  441. if (!cluster_info)
  442. return;
  443. VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
  444. cluster_set_count(&cluster_info[idx],
  445. cluster_count(&cluster_info[idx]) - 1);
  446. if (cluster_count(&cluster_info[idx]) == 0)
  447. free_cluster(p, idx);
  448. }
  449. /*
  450. * It's possible scan_swap_map() uses a free cluster in the middle of free
  451. * cluster list. Avoiding such abuse to avoid list corruption.
  452. */
  453. static bool
  454. scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
  455. unsigned long offset)
  456. {
  457. struct percpu_cluster *percpu_cluster;
  458. bool conflict;
  459. offset /= SWAPFILE_CLUSTER;
  460. conflict = !cluster_list_empty(&si->free_clusters) &&
  461. offset != cluster_list_first(&si->free_clusters) &&
  462. cluster_is_free(&si->cluster_info[offset]);
  463. if (!conflict)
  464. return false;
  465. percpu_cluster = this_cpu_ptr(si->percpu_cluster);
  466. cluster_set_null(&percpu_cluster->index);
  467. return true;
  468. }
  469. /*
  470. * Try to get a swap entry from current cpu's swap entry pool (a cluster). This
  471. * might involve allocating a new cluster for current CPU too.
  472. */
  473. static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
  474. unsigned long *offset, unsigned long *scan_base)
  475. {
  476. struct percpu_cluster *cluster;
  477. struct swap_cluster_info *ci;
  478. bool found_free;
  479. unsigned long tmp, max;
  480. new_cluster:
  481. cluster = this_cpu_ptr(si->percpu_cluster);
  482. if (cluster_is_null(&cluster->index)) {
  483. if (!cluster_list_empty(&si->free_clusters)) {
  484. cluster->index = si->free_clusters.head;
  485. cluster->next = cluster_next(&cluster->index) *
  486. SWAPFILE_CLUSTER;
  487. } else if (!cluster_list_empty(&si->discard_clusters)) {
  488. /*
  489. * we don't have free cluster but have some clusters in
  490. * discarding, do discard now and reclaim them
  491. */
  492. swap_do_scheduled_discard(si);
  493. *scan_base = *offset = si->cluster_next;
  494. goto new_cluster;
  495. } else
  496. return false;
  497. }
  498. found_free = false;
  499. /*
  500. * Other CPUs can use our cluster if they can't find a free cluster,
  501. * check if there is still free entry in the cluster
  502. */
  503. tmp = cluster->next;
  504. max = min_t(unsigned long, si->max,
  505. (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
  506. if (tmp >= max) {
  507. cluster_set_null(&cluster->index);
  508. goto new_cluster;
  509. }
  510. ci = lock_cluster(si, tmp);
  511. while (tmp < max) {
  512. if (!si->swap_map[tmp]) {
  513. found_free = true;
  514. break;
  515. }
  516. tmp++;
  517. }
  518. unlock_cluster(ci);
  519. if (!found_free) {
  520. cluster_set_null(&cluster->index);
  521. goto new_cluster;
  522. }
  523. cluster->next = tmp + 1;
  524. *offset = tmp;
  525. *scan_base = tmp;
  526. return found_free;
  527. }
  528. static void __del_from_avail_list(struct swap_info_struct *p)
  529. {
  530. int nid;
  531. for_each_node(nid)
  532. plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]);
  533. }
  534. static void del_from_avail_list(struct swap_info_struct *p)
  535. {
  536. spin_lock(&swap_avail_lock);
  537. __del_from_avail_list(p);
  538. spin_unlock(&swap_avail_lock);
  539. }
  540. static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset,
  541. unsigned int nr_entries)
  542. {
  543. unsigned int end = offset + nr_entries - 1;
  544. if (offset == si->lowest_bit)
  545. si->lowest_bit += nr_entries;
  546. if (end == si->highest_bit)
  547. si->highest_bit -= nr_entries;
  548. si->inuse_pages += nr_entries;
  549. if (si->inuse_pages == si->pages) {
  550. si->lowest_bit = si->max;
  551. si->highest_bit = 0;
  552. del_from_avail_list(si);
  553. }
  554. }
  555. static void add_to_avail_list(struct swap_info_struct *p)
  556. {
  557. int nid;
  558. spin_lock(&swap_avail_lock);
  559. for_each_node(nid) {
  560. WARN_ON(!plist_node_empty(&p->avail_lists[nid]));
  561. plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]);
  562. }
  563. spin_unlock(&swap_avail_lock);
  564. }
  565. static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
  566. unsigned int nr_entries)
  567. {
  568. unsigned long end = offset + nr_entries - 1;
  569. void (*swap_slot_free_notify)(struct block_device *, unsigned long);
  570. if (offset < si->lowest_bit)
  571. si->lowest_bit = offset;
  572. if (end > si->highest_bit) {
  573. bool was_full = !si->highest_bit;
  574. si->highest_bit = end;
  575. if (was_full && (si->flags & SWP_WRITEOK))
  576. add_to_avail_list(si);
  577. }
  578. atomic_long_add(nr_entries, &nr_swap_pages);
  579. si->inuse_pages -= nr_entries;
  580. if (si->flags & SWP_BLKDEV)
  581. swap_slot_free_notify =
  582. si->bdev->bd_disk->fops->swap_slot_free_notify;
  583. else
  584. swap_slot_free_notify = NULL;
  585. while (offset <= end) {
  586. frontswap_invalidate_page(si->type, offset);
  587. if (swap_slot_free_notify)
  588. swap_slot_free_notify(si->bdev, offset);
  589. offset++;
  590. }
  591. }
  592. static int scan_swap_map_slots(struct swap_info_struct *si,
  593. unsigned char usage, int nr,
  594. swp_entry_t slots[])
  595. {
  596. struct swap_cluster_info *ci;
  597. unsigned long offset;
  598. unsigned long scan_base;
  599. unsigned long last_in_cluster = 0;
  600. int latency_ration = LATENCY_LIMIT;
  601. int n_ret = 0;
  602. if (nr > SWAP_BATCH)
  603. nr = SWAP_BATCH;
  604. /*
  605. * We try to cluster swap pages by allocating them sequentially
  606. * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
  607. * way, however, we resort to first-free allocation, starting
  608. * a new cluster. This prevents us from scattering swap pages
  609. * all over the entire swap partition, so that we reduce
  610. * overall disk seek times between swap pages. -- sct
  611. * But we do now try to find an empty cluster. -Andrea
  612. * And we let swap pages go all over an SSD partition. Hugh
  613. */
  614. si->flags += SWP_SCANNING;
  615. scan_base = offset = si->cluster_next;
  616. /* SSD algorithm */
  617. if (si->cluster_info) {
  618. if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
  619. goto checks;
  620. else
  621. goto scan;
  622. }
  623. if (unlikely(!si->cluster_nr--)) {
  624. if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
  625. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  626. goto checks;
  627. }
  628. spin_unlock(&si->lock);
  629. /*
  630. * If seek is expensive, start searching for new cluster from
  631. * start of partition, to minimize the span of allocated swap.
  632. * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info
  633. * case, just handled by scan_swap_map_try_ssd_cluster() above.
  634. */
  635. scan_base = offset = si->lowest_bit;
  636. last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
  637. /* Locate the first empty (unaligned) cluster */
  638. for (; last_in_cluster <= si->highest_bit; offset++) {
  639. if (si->swap_map[offset])
  640. last_in_cluster = offset + SWAPFILE_CLUSTER;
  641. else if (offset == last_in_cluster) {
  642. spin_lock(&si->lock);
  643. offset -= SWAPFILE_CLUSTER - 1;
  644. si->cluster_next = offset;
  645. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  646. goto checks;
  647. }
  648. if (unlikely(--latency_ration < 0)) {
  649. cond_resched();
  650. latency_ration = LATENCY_LIMIT;
  651. }
  652. }
  653. offset = scan_base;
  654. spin_lock(&si->lock);
  655. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  656. }
  657. checks:
  658. if (si->cluster_info) {
  659. while (scan_swap_map_ssd_cluster_conflict(si, offset)) {
  660. /* take a break if we already got some slots */
  661. if (n_ret)
  662. goto done;
  663. if (!scan_swap_map_try_ssd_cluster(si, &offset,
  664. &scan_base))
  665. goto scan;
  666. }
  667. }
  668. if (!(si->flags & SWP_WRITEOK))
  669. goto no_page;
  670. if (!si->highest_bit)
  671. goto no_page;
  672. if (offset > si->highest_bit)
  673. scan_base = offset = si->lowest_bit;
  674. ci = lock_cluster(si, offset);
  675. /* reuse swap entry of cache-only swap if not busy. */
  676. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  677. int swap_was_freed;
  678. unlock_cluster(ci);
  679. spin_unlock(&si->lock);
  680. swap_was_freed = __try_to_reclaim_swap(si, offset);
  681. spin_lock(&si->lock);
  682. /* entry was freed successfully, try to use this again */
  683. if (swap_was_freed)
  684. goto checks;
  685. goto scan; /* check next one */
  686. }
  687. if (si->swap_map[offset]) {
  688. unlock_cluster(ci);
  689. if (!n_ret)
  690. goto scan;
  691. else
  692. goto done;
  693. }
  694. si->swap_map[offset] = usage;
  695. inc_cluster_info_page(si, si->cluster_info, offset);
  696. unlock_cluster(ci);
  697. swap_range_alloc(si, offset, 1);
  698. si->cluster_next = offset + 1;
  699. slots[n_ret++] = swp_entry(si->type, offset);
  700. /* got enough slots or reach max slots? */
  701. if ((n_ret == nr) || (offset >= si->highest_bit))
  702. goto done;
  703. /* search for next available slot */
  704. /* time to take a break? */
  705. if (unlikely(--latency_ration < 0)) {
  706. if (n_ret)
  707. goto done;
  708. spin_unlock(&si->lock);
  709. cond_resched();
  710. spin_lock(&si->lock);
  711. latency_ration = LATENCY_LIMIT;
  712. }
  713. /* try to get more slots in cluster */
  714. if (si->cluster_info) {
  715. if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
  716. goto checks;
  717. else
  718. goto done;
  719. }
  720. /* non-ssd case */
  721. ++offset;
  722. /* non-ssd case, still more slots in cluster? */
  723. if (si->cluster_nr && !si->swap_map[offset]) {
  724. --si->cluster_nr;
  725. goto checks;
  726. }
  727. done:
  728. si->flags -= SWP_SCANNING;
  729. return n_ret;
  730. scan:
  731. spin_unlock(&si->lock);
  732. while (++offset <= si->highest_bit) {
  733. if (!si->swap_map[offset]) {
  734. spin_lock(&si->lock);
  735. goto checks;
  736. }
  737. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  738. spin_lock(&si->lock);
  739. goto checks;
  740. }
  741. if (unlikely(--latency_ration < 0)) {
  742. cond_resched();
  743. latency_ration = LATENCY_LIMIT;
  744. }
  745. }
  746. offset = si->lowest_bit;
  747. while (offset < scan_base) {
  748. if (!si->swap_map[offset]) {
  749. spin_lock(&si->lock);
  750. goto checks;
  751. }
  752. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  753. spin_lock(&si->lock);
  754. goto checks;
  755. }
  756. if (unlikely(--latency_ration < 0)) {
  757. cond_resched();
  758. latency_ration = LATENCY_LIMIT;
  759. }
  760. offset++;
  761. }
  762. spin_lock(&si->lock);
  763. no_page:
  764. si->flags -= SWP_SCANNING;
  765. return n_ret;
  766. }
  767. static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)
  768. {
  769. unsigned long idx;
  770. struct swap_cluster_info *ci;
  771. unsigned long offset, i;
  772. unsigned char *map;
  773. /*
  774. * Should not even be attempting cluster allocations when huge
  775. * page swap is disabled. Warn and fail the allocation.
  776. */
  777. if (!IS_ENABLED(CONFIG_THP_SWAP)) {
  778. VM_WARN_ON_ONCE(1);
  779. return 0;
  780. }
  781. if (cluster_list_empty(&si->free_clusters))
  782. return 0;
  783. idx = cluster_list_first(&si->free_clusters);
  784. offset = idx * SWAPFILE_CLUSTER;
  785. ci = lock_cluster(si, offset);
  786. alloc_cluster(si, idx);
  787. cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE);
  788. map = si->swap_map + offset;
  789. for (i = 0; i < SWAPFILE_CLUSTER; i++)
  790. map[i] = SWAP_HAS_CACHE;
  791. unlock_cluster(ci);
  792. swap_range_alloc(si, offset, SWAPFILE_CLUSTER);
  793. *slot = swp_entry(si->type, offset);
  794. return 1;
  795. }
  796. static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)
  797. {
  798. unsigned long offset = idx * SWAPFILE_CLUSTER;
  799. struct swap_cluster_info *ci;
  800. ci = lock_cluster(si, offset);
  801. cluster_set_count_flag(ci, 0, 0);
  802. free_cluster(si, idx);
  803. unlock_cluster(ci);
  804. swap_range_free(si, offset, SWAPFILE_CLUSTER);
  805. }
  806. static unsigned long scan_swap_map(struct swap_info_struct *si,
  807. unsigned char usage)
  808. {
  809. swp_entry_t entry;
  810. int n_ret;
  811. n_ret = scan_swap_map_slots(si, usage, 1, &entry);
  812. if (n_ret)
  813. return swp_offset(entry);
  814. else
  815. return 0;
  816. }
  817. int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
  818. {
  819. unsigned long size = swap_entry_size(entry_size);
  820. struct swap_info_struct *si, *next;
  821. long avail_pgs;
  822. int n_ret = 0;
  823. int node;
  824. /* Only single cluster request supported */
  825. WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
  826. avail_pgs = atomic_long_read(&nr_swap_pages) / size;
  827. if (avail_pgs <= 0)
  828. goto noswap;
  829. if (n_goal > SWAP_BATCH)
  830. n_goal = SWAP_BATCH;
  831. if (n_goal > avail_pgs)
  832. n_goal = avail_pgs;
  833. atomic_long_sub(n_goal * size, &nr_swap_pages);
  834. spin_lock(&swap_avail_lock);
  835. start_over:
  836. node = numa_node_id();
  837. plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
  838. /* requeue si to after same-priority siblings */
  839. plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]);
  840. spin_unlock(&swap_avail_lock);
  841. spin_lock(&si->lock);
  842. if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
  843. spin_lock(&swap_avail_lock);
  844. if (plist_node_empty(&si->avail_lists[node])) {
  845. spin_unlock(&si->lock);
  846. goto nextsi;
  847. }
  848. WARN(!si->highest_bit,
  849. "swap_info %d in list but !highest_bit\n",
  850. si->type);
  851. WARN(!(si->flags & SWP_WRITEOK),
  852. "swap_info %d in list but !SWP_WRITEOK\n",
  853. si->type);
  854. __del_from_avail_list(si);
  855. spin_unlock(&si->lock);
  856. goto nextsi;
  857. }
  858. if (size == SWAPFILE_CLUSTER) {
  859. if (!(si->flags & SWP_FILE))
  860. n_ret = swap_alloc_cluster(si, swp_entries);
  861. } else
  862. n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE,
  863. n_goal, swp_entries);
  864. spin_unlock(&si->lock);
  865. if (n_ret || size == SWAPFILE_CLUSTER)
  866. goto check_out;
  867. pr_debug("scan_swap_map of si %d failed to find offset\n",
  868. si->type);
  869. spin_lock(&swap_avail_lock);
  870. nextsi:
  871. /*
  872. * if we got here, it's likely that si was almost full before,
  873. * and since scan_swap_map() can drop the si->lock, multiple
  874. * callers probably all tried to get a page from the same si
  875. * and it filled up before we could get one; or, the si filled
  876. * up between us dropping swap_avail_lock and taking si->lock.
  877. * Since we dropped the swap_avail_lock, the swap_avail_head
  878. * list may have been modified; so if next is still in the
  879. * swap_avail_head list then try it, otherwise start over
  880. * if we have not gotten any slots.
  881. */
  882. if (plist_node_empty(&next->avail_lists[node]))
  883. goto start_over;
  884. }
  885. spin_unlock(&swap_avail_lock);
  886. check_out:
  887. if (n_ret < n_goal)
  888. atomic_long_add((long)(n_goal - n_ret) * size,
  889. &nr_swap_pages);
  890. noswap:
  891. return n_ret;
  892. }
  893. /* The only caller of this function is now suspend routine */
  894. swp_entry_t get_swap_page_of_type(int type)
  895. {
  896. struct swap_info_struct *si = swap_type_to_swap_info(type);
  897. pgoff_t offset;
  898. if (!si)
  899. goto fail;
  900. spin_lock(&si->lock);
  901. if (si->flags & SWP_WRITEOK) {
  902. atomic_long_dec(&nr_swap_pages);
  903. /* This is called for allocating swap entry, not cache */
  904. offset = scan_swap_map(si, 1);
  905. if (offset) {
  906. spin_unlock(&si->lock);
  907. return swp_entry(type, offset);
  908. }
  909. atomic_long_inc(&nr_swap_pages);
  910. }
  911. spin_unlock(&si->lock);
  912. fail:
  913. return (swp_entry_t) {0};
  914. }
  915. static struct swap_info_struct *__swap_info_get(swp_entry_t entry)
  916. {
  917. struct swap_info_struct *p;
  918. unsigned long offset, type;
  919. if (!entry.val)
  920. goto out;
  921. type = swp_type(entry);
  922. p = swap_type_to_swap_info(type);
  923. if (!p)
  924. goto bad_nofile;
  925. if (!(p->flags & SWP_USED))
  926. goto bad_device;
  927. offset = swp_offset(entry);
  928. if (offset >= p->max)
  929. goto bad_offset;
  930. return p;
  931. bad_offset:
  932. pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val);
  933. goto out;
  934. bad_device:
  935. pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val);
  936. goto out;
  937. bad_nofile:
  938. pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val);
  939. out:
  940. return NULL;
  941. }
  942. static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
  943. {
  944. struct swap_info_struct *p;
  945. p = __swap_info_get(entry);
  946. if (!p)
  947. goto out;
  948. if (!p->swap_map[swp_offset(entry)])
  949. goto bad_free;
  950. return p;
  951. bad_free:
  952. pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val);
  953. goto out;
  954. out:
  955. return NULL;
  956. }
  957. static struct swap_info_struct *swap_info_get(swp_entry_t entry)
  958. {
  959. struct swap_info_struct *p;
  960. p = _swap_info_get(entry);
  961. if (p)
  962. spin_lock(&p->lock);
  963. return p;
  964. }
  965. static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
  966. struct swap_info_struct *q)
  967. {
  968. struct swap_info_struct *p;
  969. p = _swap_info_get(entry);
  970. if (p != q) {
  971. if (q != NULL)
  972. spin_unlock(&q->lock);
  973. if (p != NULL)
  974. spin_lock(&p->lock);
  975. }
  976. return p;
  977. }
  978. static unsigned char __swap_entry_free_locked(struct swap_info_struct *p,
  979. unsigned long offset,
  980. unsigned char usage)
  981. {
  982. unsigned char count;
  983. unsigned char has_cache;
  984. count = p->swap_map[offset];
  985. has_cache = count & SWAP_HAS_CACHE;
  986. count &= ~SWAP_HAS_CACHE;
  987. if (usage == SWAP_HAS_CACHE) {
  988. VM_BUG_ON(!has_cache);
  989. has_cache = 0;
  990. } else if (count == SWAP_MAP_SHMEM) {
  991. /*
  992. * Or we could insist on shmem.c using a special
  993. * swap_shmem_free() and free_shmem_swap_and_cache()...
  994. */
  995. count = 0;
  996. } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
  997. if (count == COUNT_CONTINUED) {
  998. if (swap_count_continued(p, offset, count))
  999. count = SWAP_MAP_MAX | COUNT_CONTINUED;
  1000. else
  1001. count = SWAP_MAP_MAX;
  1002. } else
  1003. count--;
  1004. }
  1005. usage = count | has_cache;
  1006. p->swap_map[offset] = usage ? : SWAP_HAS_CACHE;
  1007. return usage;
  1008. }
  1009. static unsigned char __swap_entry_free(struct swap_info_struct *p,
  1010. swp_entry_t entry, unsigned char usage)
  1011. {
  1012. struct swap_cluster_info *ci;
  1013. unsigned long offset = swp_offset(entry);
  1014. ci = lock_cluster_or_swap_info(p, offset);
  1015. usage = __swap_entry_free_locked(p, offset, usage);
  1016. unlock_cluster_or_swap_info(p, ci);
  1017. return usage;
  1018. }
  1019. static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
  1020. {
  1021. struct swap_cluster_info *ci;
  1022. unsigned long offset = swp_offset(entry);
  1023. unsigned char count;
  1024. ci = lock_cluster(p, offset);
  1025. count = p->swap_map[offset];
  1026. VM_BUG_ON(count != SWAP_HAS_CACHE);
  1027. p->swap_map[offset] = 0;
  1028. dec_cluster_info_page(p, p->cluster_info, offset);
  1029. unlock_cluster(ci);
  1030. mem_cgroup_uncharge_swap(entry, 1);
  1031. swap_range_free(p, offset, 1);
  1032. }
  1033. /*
  1034. * Caller has made sure that the swap device corresponding to entry
  1035. * is still around or has not been recycled.
  1036. */
  1037. void swap_free(swp_entry_t entry)
  1038. {
  1039. struct swap_info_struct *p;
  1040. p = _swap_info_get(entry);
  1041. if (p) {
  1042. if (!__swap_entry_free(p, entry, 1))
  1043. free_swap_slot(entry);
  1044. }
  1045. }
  1046. /*
  1047. * Called after dropping swapcache to decrease refcnt to swap entries.
  1048. */
  1049. void put_swap_page(struct page *page, swp_entry_t entry)
  1050. {
  1051. unsigned long offset = swp_offset(entry);
  1052. unsigned long idx = offset / SWAPFILE_CLUSTER;
  1053. struct swap_cluster_info *ci;
  1054. struct swap_info_struct *si;
  1055. unsigned char *map;
  1056. unsigned int i, free_entries = 0;
  1057. unsigned char val;
  1058. int size = swap_entry_size(hpage_nr_pages(page));
  1059. si = _swap_info_get(entry);
  1060. if (!si)
  1061. return;
  1062. ci = lock_cluster_or_swap_info(si, offset);
  1063. if (size == SWAPFILE_CLUSTER) {
  1064. VM_BUG_ON(!cluster_is_huge(ci));
  1065. map = si->swap_map + offset;
  1066. for (i = 0; i < SWAPFILE_CLUSTER; i++) {
  1067. val = map[i];
  1068. VM_BUG_ON(!(val & SWAP_HAS_CACHE));
  1069. if (val == SWAP_HAS_CACHE)
  1070. free_entries++;
  1071. }
  1072. cluster_clear_huge(ci);
  1073. if (free_entries == SWAPFILE_CLUSTER) {
  1074. unlock_cluster_or_swap_info(si, ci);
  1075. spin_lock(&si->lock);
  1076. ci = lock_cluster(si, offset);
  1077. memset(map, 0, SWAPFILE_CLUSTER);
  1078. unlock_cluster(ci);
  1079. mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER);
  1080. swap_free_cluster(si, idx);
  1081. spin_unlock(&si->lock);
  1082. return;
  1083. }
  1084. }
  1085. for (i = 0; i < size; i++, entry.val++) {
  1086. if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) {
  1087. unlock_cluster_or_swap_info(si, ci);
  1088. free_swap_slot(entry);
  1089. if (i == size - 1)
  1090. return;
  1091. lock_cluster_or_swap_info(si, offset);
  1092. }
  1093. }
  1094. unlock_cluster_or_swap_info(si, ci);
  1095. }
  1096. #ifdef CONFIG_THP_SWAP
  1097. int split_swap_cluster(swp_entry_t entry)
  1098. {
  1099. struct swap_info_struct *si;
  1100. struct swap_cluster_info *ci;
  1101. unsigned long offset = swp_offset(entry);
  1102. si = _swap_info_get(entry);
  1103. if (!si)
  1104. return -EBUSY;
  1105. ci = lock_cluster(si, offset);
  1106. cluster_clear_huge(ci);
  1107. unlock_cluster(ci);
  1108. return 0;
  1109. }
  1110. #endif
  1111. static int swp_entry_cmp(const void *ent1, const void *ent2)
  1112. {
  1113. const swp_entry_t *e1 = ent1, *e2 = ent2;
  1114. return (int)swp_type(*e1) - (int)swp_type(*e2);
  1115. }
  1116. void swapcache_free_entries(swp_entry_t *entries, int n)
  1117. {
  1118. struct swap_info_struct *p, *prev;
  1119. int i;
  1120. if (n <= 0)
  1121. return;
  1122. prev = NULL;
  1123. p = NULL;
  1124. /*
  1125. * Sort swap entries by swap device, so each lock is only taken once.
  1126. * nr_swapfiles isn't absolutely correct, but the overhead of sort() is
  1127. * so low that it isn't necessary to optimize further.
  1128. */
  1129. if (nr_swapfiles > 1)
  1130. sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);
  1131. for (i = 0; i < n; ++i) {
  1132. p = swap_info_get_cont(entries[i], prev);
  1133. if (p)
  1134. swap_entry_free(p, entries[i]);
  1135. prev = p;
  1136. }
  1137. if (p)
  1138. spin_unlock(&p->lock);
  1139. }
  1140. /*
  1141. * How many references to page are currently swapped out?
  1142. * This does not give an exact answer when swap count is continued,
  1143. * but does include the high COUNT_CONTINUED flag to allow for that.
  1144. */
  1145. int page_swapcount(struct page *page)
  1146. {
  1147. int count = 0;
  1148. struct swap_info_struct *p;
  1149. struct swap_cluster_info *ci;
  1150. swp_entry_t entry;
  1151. unsigned long offset;
  1152. entry.val = page_private(page);
  1153. p = _swap_info_get(entry);
  1154. if (p) {
  1155. offset = swp_offset(entry);
  1156. ci = lock_cluster_or_swap_info(p, offset);
  1157. count = swap_count(p->swap_map[offset]);
  1158. unlock_cluster_or_swap_info(p, ci);
  1159. }
  1160. return count;
  1161. }
  1162. int __swap_count(struct swap_info_struct *si, swp_entry_t entry)
  1163. {
  1164. pgoff_t offset = swp_offset(entry);
  1165. return swap_count(si->swap_map[offset]);
  1166. }
  1167. static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
  1168. {
  1169. int count = 0;
  1170. pgoff_t offset = swp_offset(entry);
  1171. struct swap_cluster_info *ci;
  1172. ci = lock_cluster_or_swap_info(si, offset);
  1173. count = swap_count(si->swap_map[offset]);
  1174. unlock_cluster_or_swap_info(si, ci);
  1175. return count;
  1176. }
  1177. /*
  1178. * How many references to @entry are currently swapped out?
  1179. * This does not give an exact answer when swap count is continued,
  1180. * but does include the high COUNT_CONTINUED flag to allow for that.
  1181. */
  1182. int __swp_swapcount(swp_entry_t entry)
  1183. {
  1184. int count = 0;
  1185. struct swap_info_struct *si;
  1186. si = __swap_info_get(entry);
  1187. if (si)
  1188. count = swap_swapcount(si, entry);
  1189. return count;
  1190. }
  1191. /*
  1192. * How many references to @entry are currently swapped out?
  1193. * This considers COUNT_CONTINUED so it returns exact answer.
  1194. */
  1195. int swp_swapcount(swp_entry_t entry)
  1196. {
  1197. int count, tmp_count, n;
  1198. struct swap_info_struct *p;
  1199. struct swap_cluster_info *ci;
  1200. struct page *page;
  1201. pgoff_t offset;
  1202. unsigned char *map;
  1203. p = _swap_info_get(entry);
  1204. if (!p)
  1205. return 0;
  1206. offset = swp_offset(entry);
  1207. ci = lock_cluster_or_swap_info(p, offset);
  1208. count = swap_count(p->swap_map[offset]);
  1209. if (!(count & COUNT_CONTINUED))
  1210. goto out;
  1211. count &= ~COUNT_CONTINUED;
  1212. n = SWAP_MAP_MAX + 1;
  1213. page = vmalloc_to_page(p->swap_map + offset);
  1214. offset &= ~PAGE_MASK;
  1215. VM_BUG_ON(page_private(page) != SWP_CONTINUED);
  1216. do {
  1217. page = list_next_entry(page, lru);
  1218. map = kmap_atomic(page);
  1219. tmp_count = map[offset];
  1220. kunmap_atomic(map);
  1221. count += (tmp_count & ~COUNT_CONTINUED) * n;
  1222. n *= (SWAP_CONT_MAX + 1);
  1223. } while (tmp_count & COUNT_CONTINUED);
  1224. out:
  1225. unlock_cluster_or_swap_info(p, ci);
  1226. return count;
  1227. }
  1228. static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
  1229. swp_entry_t entry)
  1230. {
  1231. struct swap_cluster_info *ci;
  1232. unsigned char *map = si->swap_map;
  1233. unsigned long roffset = swp_offset(entry);
  1234. unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER);
  1235. int i;
  1236. bool ret = false;
  1237. ci = lock_cluster_or_swap_info(si, offset);
  1238. if (!ci || !cluster_is_huge(ci)) {
  1239. if (swap_count(map[roffset]))
  1240. ret = true;
  1241. goto unlock_out;
  1242. }
  1243. for (i = 0; i < SWAPFILE_CLUSTER; i++) {
  1244. if (swap_count(map[offset + i])) {
  1245. ret = true;
  1246. break;
  1247. }
  1248. }
  1249. unlock_out:
  1250. unlock_cluster_or_swap_info(si, ci);
  1251. return ret;
  1252. }
  1253. static bool page_swapped(struct page *page)
  1254. {
  1255. swp_entry_t entry;
  1256. struct swap_info_struct *si;
  1257. if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page)))
  1258. return page_swapcount(page) != 0;
  1259. page = compound_head(page);
  1260. entry.val = page_private(page);
  1261. si = _swap_info_get(entry);
  1262. if (si)
  1263. return swap_page_trans_huge_swapped(si, entry);
  1264. return false;
  1265. }
  1266. static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,
  1267. int *total_swapcount)
  1268. {
  1269. int i, map_swapcount, _total_mapcount, _total_swapcount;
  1270. unsigned long offset = 0;
  1271. struct swap_info_struct *si;
  1272. struct swap_cluster_info *ci = NULL;
  1273. unsigned char *map = NULL;
  1274. int mapcount, swapcount = 0;
  1275. /* hugetlbfs shouldn't call it */
  1276. VM_BUG_ON_PAGE(PageHuge(page), page);
  1277. if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) {
  1278. mapcount = page_trans_huge_mapcount(page, total_mapcount);
  1279. if (PageSwapCache(page))
  1280. swapcount = page_swapcount(page);
  1281. if (total_swapcount)
  1282. *total_swapcount = swapcount;
  1283. return mapcount + swapcount;
  1284. }
  1285. page = compound_head(page);
  1286. _total_mapcount = _total_swapcount = map_swapcount = 0;
  1287. if (PageSwapCache(page)) {
  1288. swp_entry_t entry;
  1289. entry.val = page_private(page);
  1290. si = _swap_info_get(entry);
  1291. if (si) {
  1292. map = si->swap_map;
  1293. offset = swp_offset(entry);
  1294. }
  1295. }
  1296. if (map)
  1297. ci = lock_cluster(si, offset);
  1298. for (i = 0; i < HPAGE_PMD_NR; i++) {
  1299. mapcount = atomic_read(&page[i]._mapcount) + 1;
  1300. _total_mapcount += mapcount;
  1301. if (map) {
  1302. swapcount = swap_count(map[offset + i]);
  1303. _total_swapcount += swapcount;
  1304. }
  1305. map_swapcount = max(map_swapcount, mapcount + swapcount);
  1306. }
  1307. unlock_cluster(ci);
  1308. if (PageDoubleMap(page)) {
  1309. map_swapcount -= 1;
  1310. _total_mapcount -= HPAGE_PMD_NR;
  1311. }
  1312. mapcount = compound_mapcount(page);
  1313. map_swapcount += mapcount;
  1314. _total_mapcount += mapcount;
  1315. if (total_mapcount)
  1316. *total_mapcount = _total_mapcount;
  1317. if (total_swapcount)
  1318. *total_swapcount = _total_swapcount;
  1319. return map_swapcount;
  1320. }
  1321. /*
  1322. * We can write to an anon page without COW if there are no other references
  1323. * to it. And as a side-effect, free up its swap: because the old content
  1324. * on disk will never be read, and seeking back there to write new content
  1325. * later would only waste time away from clustering.
  1326. *
  1327. * NOTE: total_map_swapcount should not be relied upon by the caller if
  1328. * reuse_swap_page() returns false, but it may be always overwritten
  1329. * (see the other implementation for CONFIG_SWAP=n).
  1330. */
  1331. bool reuse_swap_page(struct page *page, int *total_map_swapcount)
  1332. {
  1333. int count, total_mapcount, total_swapcount;
  1334. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1335. if (unlikely(PageKsm(page)))
  1336. return false;
  1337. count = page_trans_huge_map_swapcount(page, &total_mapcount,
  1338. &total_swapcount);
  1339. if (total_map_swapcount)
  1340. *total_map_swapcount = total_mapcount + total_swapcount;
  1341. if (count == 1 && PageSwapCache(page) &&
  1342. (likely(!PageTransCompound(page)) ||
  1343. /* The remaining swap count will be freed soon */
  1344. total_swapcount == page_swapcount(page))) {
  1345. if (!PageWriteback(page)) {
  1346. page = compound_head(page);
  1347. delete_from_swap_cache(page);
  1348. SetPageDirty(page);
  1349. } else {
  1350. swp_entry_t entry;
  1351. struct swap_info_struct *p;
  1352. entry.val = page_private(page);
  1353. p = swap_info_get(entry);
  1354. if (p->flags & SWP_STABLE_WRITES) {
  1355. spin_unlock(&p->lock);
  1356. return false;
  1357. }
  1358. spin_unlock(&p->lock);
  1359. }
  1360. }
  1361. return count <= 1;
  1362. }
  1363. /*
  1364. * If swap is getting full, or if there are no more mappings of this page,
  1365. * then try_to_free_swap is called to free its swap space.
  1366. */
  1367. int try_to_free_swap(struct page *page)
  1368. {
  1369. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1370. if (!PageSwapCache(page))
  1371. return 0;
  1372. if (PageWriteback(page))
  1373. return 0;
  1374. if (page_swapped(page))
  1375. return 0;
  1376. /*
  1377. * Once hibernation has begun to create its image of memory,
  1378. * there's a danger that one of the calls to try_to_free_swap()
  1379. * - most probably a call from __try_to_reclaim_swap() while
  1380. * hibernation is allocating its own swap pages for the image,
  1381. * but conceivably even a call from memory reclaim - will free
  1382. * the swap from a page which has already been recorded in the
  1383. * image as a clean swapcache page, and then reuse its swap for
  1384. * another page of the image. On waking from hibernation, the
  1385. * original page might be freed under memory pressure, then
  1386. * later read back in from swap, now with the wrong data.
  1387. *
  1388. * Hibernation suspends storage while it is writing the image
  1389. * to disk so check that here.
  1390. */
  1391. if (pm_suspended_storage())
  1392. return 0;
  1393. page = compound_head(page);
  1394. delete_from_swap_cache(page);
  1395. SetPageDirty(page);
  1396. return 1;
  1397. }
  1398. /*
  1399. * Free the swap entry like above, but also try to
  1400. * free the page cache entry if it is the last user.
  1401. */
  1402. int free_swap_and_cache(swp_entry_t entry)
  1403. {
  1404. struct swap_info_struct *p;
  1405. struct page *page = NULL;
  1406. unsigned char count;
  1407. if (non_swap_entry(entry))
  1408. return 1;
  1409. p = _swap_info_get(entry);
  1410. if (p) {
  1411. count = __swap_entry_free(p, entry, 1);
  1412. if (count == SWAP_HAS_CACHE &&
  1413. !swap_page_trans_huge_swapped(p, entry)) {
  1414. page = find_get_page(swap_address_space(entry),
  1415. swp_offset(entry));
  1416. if (page && !trylock_page(page)) {
  1417. put_page(page);
  1418. page = NULL;
  1419. }
  1420. } else if (!count)
  1421. free_swap_slot(entry);
  1422. }
  1423. if (page) {
  1424. /*
  1425. * Not mapped elsewhere, or swap space full? Free it!
  1426. * Also recheck PageSwapCache now page is locked (above).
  1427. */
  1428. if (PageSwapCache(page) && !PageWriteback(page) &&
  1429. (!page_mapped(page) || mem_cgroup_swap_full(page)) &&
  1430. !swap_page_trans_huge_swapped(p, entry)) {
  1431. page = compound_head(page);
  1432. delete_from_swap_cache(page);
  1433. SetPageDirty(page);
  1434. }
  1435. unlock_page(page);
  1436. put_page(page);
  1437. }
  1438. return p != NULL;
  1439. }
  1440. #ifdef CONFIG_HIBERNATION
  1441. /*
  1442. * Find the swap type that corresponds to given device (if any).
  1443. *
  1444. * @offset - number of the PAGE_SIZE-sized block of the device, starting
  1445. * from 0, in which the swap header is expected to be located.
  1446. *
  1447. * This is needed for the suspend to disk (aka swsusp).
  1448. */
  1449. int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
  1450. {
  1451. struct block_device *bdev = NULL;
  1452. int type;
  1453. if (device)
  1454. bdev = bdget(device);
  1455. spin_lock(&swap_lock);
  1456. for (type = 0; type < nr_swapfiles; type++) {
  1457. struct swap_info_struct *sis = swap_info[type];
  1458. if (!(sis->flags & SWP_WRITEOK))
  1459. continue;
  1460. if (!bdev) {
  1461. if (bdev_p)
  1462. *bdev_p = bdgrab(sis->bdev);
  1463. spin_unlock(&swap_lock);
  1464. return type;
  1465. }
  1466. if (bdev == sis->bdev) {
  1467. struct swap_extent *se = &sis->first_swap_extent;
  1468. if (se->start_block == offset) {
  1469. if (bdev_p)
  1470. *bdev_p = bdgrab(sis->bdev);
  1471. spin_unlock(&swap_lock);
  1472. bdput(bdev);
  1473. return type;
  1474. }
  1475. }
  1476. }
  1477. spin_unlock(&swap_lock);
  1478. if (bdev)
  1479. bdput(bdev);
  1480. return -ENODEV;
  1481. }
  1482. /*
  1483. * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  1484. * corresponding to given index in swap_info (swap type).
  1485. */
  1486. sector_t swapdev_block(int type, pgoff_t offset)
  1487. {
  1488. struct block_device *bdev;
  1489. struct swap_info_struct *si = swap_type_to_swap_info(type);
  1490. if (!si || !(si->flags & SWP_WRITEOK))
  1491. return 0;
  1492. return map_swap_entry(swp_entry(type, offset), &bdev);
  1493. }
  1494. /*
  1495. * Return either the total number of swap pages of given type, or the number
  1496. * of free pages of that type (depending on @free)
  1497. *
  1498. * This is needed for software suspend
  1499. */
  1500. unsigned int count_swap_pages(int type, int free)
  1501. {
  1502. unsigned int n = 0;
  1503. spin_lock(&swap_lock);
  1504. if ((unsigned int)type < nr_swapfiles) {
  1505. struct swap_info_struct *sis = swap_info[type];
  1506. spin_lock(&sis->lock);
  1507. if (sis->flags & SWP_WRITEOK) {
  1508. n = sis->pages;
  1509. if (free)
  1510. n -= sis->inuse_pages;
  1511. }
  1512. spin_unlock(&sis->lock);
  1513. }
  1514. spin_unlock(&swap_lock);
  1515. return n;
  1516. }
  1517. #endif /* CONFIG_HIBERNATION */
  1518. static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
  1519. {
  1520. return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte);
  1521. }
  1522. /*
  1523. * No need to decide whether this PTE shares the swap entry with others,
  1524. * just let do_wp_page work it out if a write is requested later - to
  1525. * force COW, vm_page_prot omits write permission from any private vma.
  1526. */
  1527. static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
  1528. unsigned long addr, swp_entry_t entry, struct page *page)
  1529. {
  1530. struct page *swapcache;
  1531. struct mem_cgroup *memcg;
  1532. spinlock_t *ptl;
  1533. pte_t *pte;
  1534. int ret = 1;
  1535. swapcache = page;
  1536. page = ksm_might_need_to_copy(page, vma, addr);
  1537. if (unlikely(!page))
  1538. return -ENOMEM;
  1539. if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
  1540. &memcg, false)) {
  1541. ret = -ENOMEM;
  1542. goto out_nolock;
  1543. }
  1544. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  1545. if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) {
  1546. mem_cgroup_cancel_charge(page, memcg, false);
  1547. ret = 0;
  1548. goto out;
  1549. }
  1550. dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
  1551. inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
  1552. get_page(page);
  1553. set_pte_at(vma->vm_mm, addr, pte,
  1554. pte_mkold(mk_pte(page, vma->vm_page_prot)));
  1555. if (page == swapcache) {
  1556. page_add_anon_rmap(page, vma, addr, false);
  1557. mem_cgroup_commit_charge(page, memcg, true, false);
  1558. } else { /* ksm created a completely new copy */
  1559. page_add_new_anon_rmap(page, vma, addr, false);
  1560. mem_cgroup_commit_charge(page, memcg, false, false);
  1561. lru_cache_add_active_or_unevictable(page, vma);
  1562. }
  1563. swap_free(entry);
  1564. /*
  1565. * Move the page to the active list so it is not
  1566. * immediately swapped out again after swapon.
  1567. */
  1568. activate_page(page);
  1569. out:
  1570. pte_unmap_unlock(pte, ptl);
  1571. out_nolock:
  1572. if (page != swapcache) {
  1573. unlock_page(page);
  1574. put_page(page);
  1575. }
  1576. return ret;
  1577. }
  1578. static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  1579. unsigned long addr, unsigned long end,
  1580. swp_entry_t entry, struct page *page)
  1581. {
  1582. pte_t swp_pte = swp_entry_to_pte(entry);
  1583. pte_t *pte;
  1584. int ret = 0;
  1585. /*
  1586. * We don't actually need pte lock while scanning for swp_pte: since
  1587. * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
  1588. * page table while we're scanning; though it could get zapped, and on
  1589. * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
  1590. * of unmatched parts which look like swp_pte, so unuse_pte must
  1591. * recheck under pte lock. Scanning without pte lock lets it be
  1592. * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
  1593. */
  1594. pte = pte_offset_map(pmd, addr);
  1595. do {
  1596. /*
  1597. * swapoff spends a _lot_ of time in this loop!
  1598. * Test inline before going to call unuse_pte.
  1599. */
  1600. if (unlikely(pte_same_as_swp(*pte, swp_pte))) {
  1601. pte_unmap(pte);
  1602. ret = unuse_pte(vma, pmd, addr, entry, page);
  1603. if (ret)
  1604. goto out;
  1605. pte = pte_offset_map(pmd, addr);
  1606. }
  1607. } while (pte++, addr += PAGE_SIZE, addr != end);
  1608. pte_unmap(pte - 1);
  1609. out:
  1610. return ret;
  1611. }
  1612. static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  1613. unsigned long addr, unsigned long end,
  1614. swp_entry_t entry, struct page *page)
  1615. {
  1616. pmd_t *pmd;
  1617. unsigned long next;
  1618. int ret;
  1619. pmd = pmd_offset(pud, addr);
  1620. do {
  1621. cond_resched();
  1622. next = pmd_addr_end(addr, end);
  1623. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  1624. continue;
  1625. ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
  1626. if (ret)
  1627. return ret;
  1628. } while (pmd++, addr = next, addr != end);
  1629. return 0;
  1630. }
  1631. static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
  1632. unsigned long addr, unsigned long end,
  1633. swp_entry_t entry, struct page *page)
  1634. {
  1635. pud_t *pud;
  1636. unsigned long next;
  1637. int ret;
  1638. pud = pud_offset(p4d, addr);
  1639. do {
  1640. next = pud_addr_end(addr, end);
  1641. if (pud_none_or_clear_bad(pud))
  1642. continue;
  1643. ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
  1644. if (ret)
  1645. return ret;
  1646. } while (pud++, addr = next, addr != end);
  1647. return 0;
  1648. }
  1649. static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
  1650. unsigned long addr, unsigned long end,
  1651. swp_entry_t entry, struct page *page)
  1652. {
  1653. p4d_t *p4d;
  1654. unsigned long next;
  1655. int ret;
  1656. p4d = p4d_offset(pgd, addr);
  1657. do {
  1658. next = p4d_addr_end(addr, end);
  1659. if (p4d_none_or_clear_bad(p4d))
  1660. continue;
  1661. ret = unuse_pud_range(vma, p4d, addr, next, entry, page);
  1662. if (ret)
  1663. return ret;
  1664. } while (p4d++, addr = next, addr != end);
  1665. return 0;
  1666. }
  1667. static int unuse_vma(struct vm_area_struct *vma,
  1668. swp_entry_t entry, struct page *page)
  1669. {
  1670. pgd_t *pgd;
  1671. unsigned long addr, end, next;
  1672. int ret;
  1673. if (page_anon_vma(page)) {
  1674. addr = page_address_in_vma(page, vma);
  1675. if (addr == -EFAULT)
  1676. return 0;
  1677. else
  1678. end = addr + PAGE_SIZE;
  1679. } else {
  1680. addr = vma->vm_start;
  1681. end = vma->vm_end;
  1682. }
  1683. pgd = pgd_offset(vma->vm_mm, addr);
  1684. do {
  1685. next = pgd_addr_end(addr, end);
  1686. if (pgd_none_or_clear_bad(pgd))
  1687. continue;
  1688. ret = unuse_p4d_range(vma, pgd, addr, next, entry, page);
  1689. if (ret)
  1690. return ret;
  1691. } while (pgd++, addr = next, addr != end);
  1692. return 0;
  1693. }
  1694. static int unuse_mm(struct mm_struct *mm,
  1695. swp_entry_t entry, struct page *page)
  1696. {
  1697. struct vm_area_struct *vma;
  1698. int ret = 0;
  1699. if (!down_read_trylock(&mm->mmap_sem)) {
  1700. /*
  1701. * Activate page so shrink_inactive_list is unlikely to unmap
  1702. * its ptes while lock is dropped, so swapoff can make progress.
  1703. */
  1704. activate_page(page);
  1705. unlock_page(page);
  1706. down_read(&mm->mmap_sem);
  1707. lock_page(page);
  1708. }
  1709. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1710. if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
  1711. break;
  1712. cond_resched();
  1713. }
  1714. up_read(&mm->mmap_sem);
  1715. return (ret < 0)? ret: 0;
  1716. }
  1717. /*
  1718. * Scan swap_map (or frontswap_map if frontswap parameter is true)
  1719. * from current position to next entry still in use.
  1720. * Recycle to start on reaching the end, returning 0 when empty.
  1721. */
  1722. static unsigned int find_next_to_unuse(struct swap_info_struct *si,
  1723. unsigned int prev, bool frontswap)
  1724. {
  1725. unsigned int max = si->max;
  1726. unsigned int i = prev;
  1727. unsigned char count;
  1728. /*
  1729. * No need for swap_lock here: we're just looking
  1730. * for whether an entry is in use, not modifying it; false
  1731. * hits are okay, and sys_swapoff() has already prevented new
  1732. * allocations from this area (while holding swap_lock).
  1733. */
  1734. for (;;) {
  1735. if (++i >= max) {
  1736. if (!prev) {
  1737. i = 0;
  1738. break;
  1739. }
  1740. /*
  1741. * No entries in use at top of swap_map,
  1742. * loop back to start and recheck there.
  1743. */
  1744. max = prev + 1;
  1745. prev = 0;
  1746. i = 1;
  1747. }
  1748. count = READ_ONCE(si->swap_map[i]);
  1749. if (count && swap_count(count) != SWAP_MAP_BAD)
  1750. if (!frontswap || frontswap_test(si, i))
  1751. break;
  1752. if ((i % LATENCY_LIMIT) == 0)
  1753. cond_resched();
  1754. }
  1755. return i;
  1756. }
  1757. /*
  1758. * We completely avoid races by reading each swap page in advance,
  1759. * and then search for the process using it. All the necessary
  1760. * page table adjustments can then be made atomically.
  1761. *
  1762. * if the boolean frontswap is true, only unuse pages_to_unuse pages;
  1763. * pages_to_unuse==0 means all pages; ignored if frontswap is false
  1764. */
  1765. int try_to_unuse(unsigned int type, bool frontswap,
  1766. unsigned long pages_to_unuse)
  1767. {
  1768. struct swap_info_struct *si = swap_info[type];
  1769. struct mm_struct *start_mm;
  1770. volatile unsigned char *swap_map; /* swap_map is accessed without
  1771. * locking. Mark it as volatile
  1772. * to prevent compiler doing
  1773. * something odd.
  1774. */
  1775. unsigned char swcount;
  1776. struct page *page;
  1777. swp_entry_t entry;
  1778. unsigned int i = 0;
  1779. int retval = 0;
  1780. /*
  1781. * When searching mms for an entry, a good strategy is to
  1782. * start at the first mm we freed the previous entry from
  1783. * (though actually we don't notice whether we or coincidence
  1784. * freed the entry). Initialize this start_mm with a hold.
  1785. *
  1786. * A simpler strategy would be to start at the last mm we
  1787. * freed the previous entry from; but that would take less
  1788. * advantage of mmlist ordering, which clusters forked mms
  1789. * together, child after parent. If we race with dup_mmap(), we
  1790. * prefer to resolve parent before child, lest we miss entries
  1791. * duplicated after we scanned child: using last mm would invert
  1792. * that.
  1793. */
  1794. start_mm = &init_mm;
  1795. mmget(&init_mm);
  1796. /*
  1797. * Keep on scanning until all entries have gone. Usually,
  1798. * one pass through swap_map is enough, but not necessarily:
  1799. * there are races when an instance of an entry might be missed.
  1800. */
  1801. while ((i = find_next_to_unuse(si, i, frontswap)) != 0) {
  1802. if (signal_pending(current)) {
  1803. retval = -EINTR;
  1804. break;
  1805. }
  1806. /*
  1807. * Get a page for the entry, using the existing swap
  1808. * cache page if there is one. Otherwise, get a clean
  1809. * page and read the swap into it.
  1810. */
  1811. swap_map = &si->swap_map[i];
  1812. entry = swp_entry(type, i);
  1813. page = read_swap_cache_async(entry,
  1814. GFP_HIGHUSER_MOVABLE, NULL, 0, false);
  1815. if (!page) {
  1816. /*
  1817. * Either swap_duplicate() failed because entry
  1818. * has been freed independently, and will not be
  1819. * reused since sys_swapoff() already disabled
  1820. * allocation from here, or alloc_page() failed.
  1821. */
  1822. swcount = *swap_map;
  1823. /*
  1824. * We don't hold lock here, so the swap entry could be
  1825. * SWAP_MAP_BAD (when the cluster is discarding).
  1826. * Instead of fail out, We can just skip the swap
  1827. * entry because swapoff will wait for discarding
  1828. * finish anyway.
  1829. */
  1830. if (!swcount || swcount == SWAP_MAP_BAD)
  1831. continue;
  1832. retval = -ENOMEM;
  1833. break;
  1834. }
  1835. /*
  1836. * Don't hold on to start_mm if it looks like exiting.
  1837. */
  1838. if (atomic_read(&start_mm->mm_users) == 1) {
  1839. mmput(start_mm);
  1840. start_mm = &init_mm;
  1841. mmget(&init_mm);
  1842. }
  1843. /*
  1844. * Wait for and lock page. When do_swap_page races with
  1845. * try_to_unuse, do_swap_page can handle the fault much
  1846. * faster than try_to_unuse can locate the entry. This
  1847. * apparently redundant "wait_on_page_locked" lets try_to_unuse
  1848. * defer to do_swap_page in such a case - in some tests,
  1849. * do_swap_page and try_to_unuse repeatedly compete.
  1850. */
  1851. wait_on_page_locked(page);
  1852. wait_on_page_writeback(page);
  1853. lock_page(page);
  1854. wait_on_page_writeback(page);
  1855. /*
  1856. * Remove all references to entry.
  1857. */
  1858. swcount = *swap_map;
  1859. if (swap_count(swcount) == SWAP_MAP_SHMEM) {
  1860. retval = shmem_unuse(entry, page);
  1861. /* page has already been unlocked and released */
  1862. if (retval < 0)
  1863. break;
  1864. continue;
  1865. }
  1866. if (swap_count(swcount) && start_mm != &init_mm)
  1867. retval = unuse_mm(start_mm, entry, page);
  1868. if (swap_count(*swap_map)) {
  1869. int set_start_mm = (*swap_map >= swcount);
  1870. struct list_head *p = &start_mm->mmlist;
  1871. struct mm_struct *new_start_mm = start_mm;
  1872. struct mm_struct *prev_mm = start_mm;
  1873. struct mm_struct *mm;
  1874. mmget(new_start_mm);
  1875. mmget(prev_mm);
  1876. spin_lock(&mmlist_lock);
  1877. while (swap_count(*swap_map) && !retval &&
  1878. (p = p->next) != &start_mm->mmlist) {
  1879. mm = list_entry(p, struct mm_struct, mmlist);
  1880. if (!mmget_not_zero(mm))
  1881. continue;
  1882. spin_unlock(&mmlist_lock);
  1883. mmput(prev_mm);
  1884. prev_mm = mm;
  1885. cond_resched();
  1886. swcount = *swap_map;
  1887. if (!swap_count(swcount)) /* any usage ? */
  1888. ;
  1889. else if (mm == &init_mm)
  1890. set_start_mm = 1;
  1891. else
  1892. retval = unuse_mm(mm, entry, page);
  1893. if (set_start_mm && *swap_map < swcount) {
  1894. mmput(new_start_mm);
  1895. mmget(mm);
  1896. new_start_mm = mm;
  1897. set_start_mm = 0;
  1898. }
  1899. spin_lock(&mmlist_lock);
  1900. }
  1901. spin_unlock(&mmlist_lock);
  1902. mmput(prev_mm);
  1903. mmput(start_mm);
  1904. start_mm = new_start_mm;
  1905. }
  1906. if (retval) {
  1907. unlock_page(page);
  1908. put_page(page);
  1909. break;
  1910. }
  1911. /*
  1912. * If a reference remains (rare), we would like to leave
  1913. * the page in the swap cache; but try_to_unmap could
  1914. * then re-duplicate the entry once we drop page lock,
  1915. * so we might loop indefinitely; also, that page could
  1916. * not be swapped out to other storage meanwhile. So:
  1917. * delete from cache even if there's another reference,
  1918. * after ensuring that the data has been saved to disk -
  1919. * since if the reference remains (rarer), it will be
  1920. * read from disk into another page. Splitting into two
  1921. * pages would be incorrect if swap supported "shared
  1922. * private" pages, but they are handled by tmpfs files.
  1923. *
  1924. * Given how unuse_vma() targets one particular offset
  1925. * in an anon_vma, once the anon_vma has been determined,
  1926. * this splitting happens to be just what is needed to
  1927. * handle where KSM pages have been swapped out: re-reading
  1928. * is unnecessarily slow, but we can fix that later on.
  1929. */
  1930. if (swap_count(*swap_map) &&
  1931. PageDirty(page) && PageSwapCache(page)) {
  1932. struct writeback_control wbc = {
  1933. .sync_mode = WB_SYNC_NONE,
  1934. };
  1935. swap_writepage(compound_head(page), &wbc);
  1936. lock_page(page);
  1937. wait_on_page_writeback(page);
  1938. }
  1939. /*
  1940. * It is conceivable that a racing task removed this page from
  1941. * swap cache just before we acquired the page lock at the top,
  1942. * or while we dropped it in unuse_mm(). The page might even
  1943. * be back in swap cache on another swap area: that we must not
  1944. * delete, since it may not have been written out to swap yet.
  1945. */
  1946. if (PageSwapCache(page) &&
  1947. likely(page_private(page) == entry.val) &&
  1948. (!PageTransCompound(page) ||
  1949. !swap_page_trans_huge_swapped(si, entry)))
  1950. delete_from_swap_cache(compound_head(page));
  1951. /*
  1952. * So we could skip searching mms once swap count went
  1953. * to 1, we did not mark any present ptes as dirty: must
  1954. * mark page dirty so shrink_page_list will preserve it.
  1955. */
  1956. SetPageDirty(page);
  1957. unlock_page(page);
  1958. put_page(page);
  1959. /*
  1960. * Make sure that we aren't completely killing
  1961. * interactive performance.
  1962. */
  1963. cond_resched();
  1964. if (frontswap && pages_to_unuse > 0) {
  1965. if (!--pages_to_unuse)
  1966. break;
  1967. }
  1968. }
  1969. mmput(start_mm);
  1970. return retval;
  1971. }
  1972. /*
  1973. * After a successful try_to_unuse, if no swap is now in use, we know
  1974. * we can empty the mmlist. swap_lock must be held on entry and exit.
  1975. * Note that mmlist_lock nests inside swap_lock, and an mm must be
  1976. * added to the mmlist just after page_duplicate - before would be racy.
  1977. */
  1978. static void drain_mmlist(void)
  1979. {
  1980. struct list_head *p, *next;
  1981. unsigned int type;
  1982. for (type = 0; type < nr_swapfiles; type++)
  1983. if (swap_info[type]->inuse_pages)
  1984. return;
  1985. spin_lock(&mmlist_lock);
  1986. list_for_each_safe(p, next, &init_mm.mmlist)
  1987. list_del_init(p);
  1988. spin_unlock(&mmlist_lock);
  1989. }
  1990. /*
  1991. * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
  1992. * corresponds to page offset for the specified swap entry.
  1993. * Note that the type of this function is sector_t, but it returns page offset
  1994. * into the bdev, not sector offset.
  1995. */
  1996. static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
  1997. {
  1998. struct swap_info_struct *sis;
  1999. struct swap_extent *start_se;
  2000. struct swap_extent *se;
  2001. pgoff_t offset;
  2002. sis = swp_swap_info(entry);
  2003. *bdev = sis->bdev;
  2004. offset = swp_offset(entry);
  2005. start_se = sis->curr_swap_extent;
  2006. se = start_se;
  2007. for ( ; ; ) {
  2008. if (se->start_page <= offset &&
  2009. offset < (se->start_page + se->nr_pages)) {
  2010. return se->start_block + (offset - se->start_page);
  2011. }
  2012. se = list_next_entry(se, list);
  2013. sis->curr_swap_extent = se;
  2014. BUG_ON(se == start_se); /* It *must* be present */
  2015. }
  2016. }
  2017. /*
  2018. * Returns the page offset into bdev for the specified page's swap entry.
  2019. */
  2020. sector_t map_swap_page(struct page *page, struct block_device **bdev)
  2021. {
  2022. swp_entry_t entry;
  2023. entry.val = page_private(page);
  2024. return map_swap_entry(entry, bdev);
  2025. }
  2026. /*
  2027. * Free all of a swapdev's extent information
  2028. */
  2029. static void destroy_swap_extents(struct swap_info_struct *sis)
  2030. {
  2031. while (!list_empty(&sis->first_swap_extent.list)) {
  2032. struct swap_extent *se;
  2033. se = list_first_entry(&sis->first_swap_extent.list,
  2034. struct swap_extent, list);
  2035. list_del(&se->list);
  2036. kfree(se);
  2037. }
  2038. if (sis->flags & SWP_FILE) {
  2039. struct file *swap_file = sis->swap_file;
  2040. struct address_space *mapping = swap_file->f_mapping;
  2041. sis->flags &= ~SWP_FILE;
  2042. mapping->a_ops->swap_deactivate(swap_file);
  2043. }
  2044. }
  2045. /*
  2046. * Add a block range (and the corresponding page range) into this swapdev's
  2047. * extent list. The extent list is kept sorted in page order.
  2048. *
  2049. * This function rather assumes that it is called in ascending page order.
  2050. */
  2051. int
  2052. add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
  2053. unsigned long nr_pages, sector_t start_block)
  2054. {
  2055. struct swap_extent *se;
  2056. struct swap_extent *new_se;
  2057. struct list_head *lh;
  2058. if (start_page == 0) {
  2059. se = &sis->first_swap_extent;
  2060. sis->curr_swap_extent = se;
  2061. se->start_page = 0;
  2062. se->nr_pages = nr_pages;
  2063. se->start_block = start_block;
  2064. return 1;
  2065. } else {
  2066. lh = sis->first_swap_extent.list.prev; /* Highest extent */
  2067. se = list_entry(lh, struct swap_extent, list);
  2068. BUG_ON(se->start_page + se->nr_pages != start_page);
  2069. if (se->start_block + se->nr_pages == start_block) {
  2070. /* Merge it */
  2071. se->nr_pages += nr_pages;
  2072. return 0;
  2073. }
  2074. }
  2075. /*
  2076. * No merge. Insert a new extent, preserving ordering.
  2077. */
  2078. new_se = kmalloc(sizeof(*se), GFP_KERNEL);
  2079. if (new_se == NULL)
  2080. return -ENOMEM;
  2081. new_se->start_page = start_page;
  2082. new_se->nr_pages = nr_pages;
  2083. new_se->start_block = start_block;
  2084. list_add_tail(&new_se->list, &sis->first_swap_extent.list);
  2085. return 1;
  2086. }
  2087. /*
  2088. * A `swap extent' is a simple thing which maps a contiguous range of pages
  2089. * onto a contiguous range of disk blocks. An ordered list of swap extents
  2090. * is built at swapon time and is then used at swap_writepage/swap_readpage
  2091. * time for locating where on disk a page belongs.
  2092. *
  2093. * If the swapfile is an S_ISBLK block device, a single extent is installed.
  2094. * This is done so that the main operating code can treat S_ISBLK and S_ISREG
  2095. * swap files identically.
  2096. *
  2097. * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
  2098. * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
  2099. * swapfiles are handled *identically* after swapon time.
  2100. *
  2101. * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
  2102. * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
  2103. * some stray blocks are found which do not fall within the PAGE_SIZE alignment
  2104. * requirements, they are simply tossed out - we will never use those blocks
  2105. * for swapping.
  2106. *
  2107. * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
  2108. * prevents root from shooting her foot off by ftruncating an in-use swapfile,
  2109. * which will scribble on the fs.
  2110. *
  2111. * The amount of disk space which a single swap extent represents varies.
  2112. * Typically it is in the 1-4 megabyte range. So we can have hundreds of
  2113. * extents in the list. To avoid much list walking, we cache the previous
  2114. * search location in `curr_swap_extent', and start new searches from there.
  2115. * This is extremely effective. The average number of iterations in
  2116. * map_swap_page() has been measured at about 0.3 per page. - akpm.
  2117. */
  2118. static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
  2119. {
  2120. struct file *swap_file = sis->swap_file;
  2121. struct address_space *mapping = swap_file->f_mapping;
  2122. struct inode *inode = mapping->host;
  2123. int ret;
  2124. if (S_ISBLK(inode->i_mode)) {
  2125. ret = add_swap_extent(sis, 0, sis->max, 0);
  2126. *span = sis->pages;
  2127. return ret;
  2128. }
  2129. if (mapping->a_ops->swap_activate) {
  2130. ret = mapping->a_ops->swap_activate(sis, swap_file, span);
  2131. if (!ret) {
  2132. sis->flags |= SWP_FILE;
  2133. ret = add_swap_extent(sis, 0, sis->max, 0);
  2134. *span = sis->pages;
  2135. }
  2136. return ret;
  2137. }
  2138. return generic_swapfile_activate(sis, swap_file, span);
  2139. }
  2140. static int swap_node(struct swap_info_struct *p)
  2141. {
  2142. struct block_device *bdev;
  2143. if (p->bdev)
  2144. bdev = p->bdev;
  2145. else
  2146. bdev = p->swap_file->f_inode->i_sb->s_bdev;
  2147. return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE;
  2148. }
  2149. static void _enable_swap_info(struct swap_info_struct *p, int prio,
  2150. unsigned char *swap_map,
  2151. struct swap_cluster_info *cluster_info)
  2152. {
  2153. int i;
  2154. if (prio >= 0)
  2155. p->prio = prio;
  2156. else
  2157. p->prio = --least_priority;
  2158. /*
  2159. * the plist prio is negated because plist ordering is
  2160. * low-to-high, while swap ordering is high-to-low
  2161. */
  2162. p->list.prio = -p->prio;
  2163. for_each_node(i) {
  2164. if (p->prio >= 0)
  2165. p->avail_lists[i].prio = -p->prio;
  2166. else {
  2167. if (swap_node(p) == i)
  2168. p->avail_lists[i].prio = 1;
  2169. else
  2170. p->avail_lists[i].prio = -p->prio;
  2171. }
  2172. }
  2173. p->swap_map = swap_map;
  2174. p->cluster_info = cluster_info;
  2175. p->flags |= SWP_WRITEOK;
  2176. atomic_long_add(p->pages, &nr_swap_pages);
  2177. total_swap_pages += p->pages;
  2178. assert_spin_locked(&swap_lock);
  2179. /*
  2180. * both lists are plists, and thus priority ordered.
  2181. * swap_active_head needs to be priority ordered for swapoff(),
  2182. * which on removal of any swap_info_struct with an auto-assigned
  2183. * (i.e. negative) priority increments the auto-assigned priority
  2184. * of any lower-priority swap_info_structs.
  2185. * swap_avail_head needs to be priority ordered for get_swap_page(),
  2186. * which allocates swap pages from the highest available priority
  2187. * swap_info_struct.
  2188. */
  2189. plist_add(&p->list, &swap_active_head);
  2190. add_to_avail_list(p);
  2191. }
  2192. static void enable_swap_info(struct swap_info_struct *p, int prio,
  2193. unsigned char *swap_map,
  2194. struct swap_cluster_info *cluster_info,
  2195. unsigned long *frontswap_map)
  2196. {
  2197. frontswap_init(p->type, frontswap_map);
  2198. spin_lock(&swap_lock);
  2199. spin_lock(&p->lock);
  2200. _enable_swap_info(p, prio, swap_map, cluster_info);
  2201. spin_unlock(&p->lock);
  2202. spin_unlock(&swap_lock);
  2203. }
  2204. static void reinsert_swap_info(struct swap_info_struct *p)
  2205. {
  2206. spin_lock(&swap_lock);
  2207. spin_lock(&p->lock);
  2208. _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info);
  2209. spin_unlock(&p->lock);
  2210. spin_unlock(&swap_lock);
  2211. }
  2212. bool has_usable_swap(void)
  2213. {
  2214. bool ret = true;
  2215. spin_lock(&swap_lock);
  2216. if (plist_head_empty(&swap_active_head))
  2217. ret = false;
  2218. spin_unlock(&swap_lock);
  2219. return ret;
  2220. }
  2221. SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
  2222. {
  2223. struct swap_info_struct *p = NULL;
  2224. unsigned char *swap_map;
  2225. struct swap_cluster_info *cluster_info;
  2226. unsigned long *frontswap_map;
  2227. struct file *swap_file, *victim;
  2228. struct address_space *mapping;
  2229. struct inode *inode;
  2230. struct filename *pathname;
  2231. int err, found = 0;
  2232. unsigned int old_block_size;
  2233. if (!capable(CAP_SYS_ADMIN))
  2234. return -EPERM;
  2235. BUG_ON(!current->mm);
  2236. pathname = getname(specialfile);
  2237. if (IS_ERR(pathname))
  2238. return PTR_ERR(pathname);
  2239. victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
  2240. err = PTR_ERR(victim);
  2241. if (IS_ERR(victim))
  2242. goto out;
  2243. mapping = victim->f_mapping;
  2244. spin_lock(&swap_lock);
  2245. plist_for_each_entry(p, &swap_active_head, list) {
  2246. if (p->flags & SWP_WRITEOK) {
  2247. if (p->swap_file->f_mapping == mapping) {
  2248. found = 1;
  2249. break;
  2250. }
  2251. }
  2252. }
  2253. if (!found) {
  2254. err = -EINVAL;
  2255. spin_unlock(&swap_lock);
  2256. goto out_dput;
  2257. }
  2258. if (!security_vm_enough_memory_mm(current->mm, p->pages))
  2259. vm_unacct_memory(p->pages);
  2260. else {
  2261. err = -ENOMEM;
  2262. spin_unlock(&swap_lock);
  2263. goto out_dput;
  2264. }
  2265. del_from_avail_list(p);
  2266. spin_lock(&p->lock);
  2267. if (p->prio < 0) {
  2268. struct swap_info_struct *si = p;
  2269. int nid;
  2270. plist_for_each_entry_continue(si, &swap_active_head, list) {
  2271. si->prio++;
  2272. si->list.prio--;
  2273. for_each_node(nid) {
  2274. if (si->avail_lists[nid].prio != 1)
  2275. si->avail_lists[nid].prio--;
  2276. }
  2277. }
  2278. least_priority++;
  2279. }
  2280. plist_del(&p->list, &swap_active_head);
  2281. atomic_long_sub(p->pages, &nr_swap_pages);
  2282. total_swap_pages -= p->pages;
  2283. p->flags &= ~SWP_WRITEOK;
  2284. spin_unlock(&p->lock);
  2285. spin_unlock(&swap_lock);
  2286. disable_swap_slots_cache_lock();
  2287. set_current_oom_origin();
  2288. err = try_to_unuse(p->type, false, 0); /* force unuse all pages */
  2289. clear_current_oom_origin();
  2290. if (err) {
  2291. /* re-insert swap space back into swap_list */
  2292. reinsert_swap_info(p);
  2293. reenable_swap_slots_cache_unlock();
  2294. goto out_dput;
  2295. }
  2296. reenable_swap_slots_cache_unlock();
  2297. flush_work(&p->discard_work);
  2298. destroy_swap_extents(p);
  2299. if (p->flags & SWP_CONTINUED)
  2300. free_swap_count_continuations(p);
  2301. if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev)))
  2302. atomic_dec(&nr_rotate_swap);
  2303. mutex_lock(&swapon_mutex);
  2304. spin_lock(&swap_lock);
  2305. spin_lock(&p->lock);
  2306. drain_mmlist();
  2307. /* wait for anyone still in scan_swap_map */
  2308. p->highest_bit = 0; /* cuts scans short */
  2309. while (p->flags >= SWP_SCANNING) {
  2310. spin_unlock(&p->lock);
  2311. spin_unlock(&swap_lock);
  2312. schedule_timeout_uninterruptible(1);
  2313. spin_lock(&swap_lock);
  2314. spin_lock(&p->lock);
  2315. }
  2316. swap_file = p->swap_file;
  2317. old_block_size = p->old_block_size;
  2318. p->swap_file = NULL;
  2319. p->max = 0;
  2320. swap_map = p->swap_map;
  2321. p->swap_map = NULL;
  2322. cluster_info = p->cluster_info;
  2323. p->cluster_info = NULL;
  2324. frontswap_map = frontswap_map_get(p);
  2325. spin_unlock(&p->lock);
  2326. spin_unlock(&swap_lock);
  2327. frontswap_invalidate_area(p->type);
  2328. frontswap_map_set(p, NULL);
  2329. mutex_unlock(&swapon_mutex);
  2330. free_percpu(p->percpu_cluster);
  2331. p->percpu_cluster = NULL;
  2332. vfree(swap_map);
  2333. kvfree(cluster_info);
  2334. kvfree(frontswap_map);
  2335. /* Destroy swap account information */
  2336. swap_cgroup_swapoff(p->type);
  2337. exit_swap_address_space(p->type);
  2338. inode = mapping->host;
  2339. if (S_ISBLK(inode->i_mode)) {
  2340. struct block_device *bdev = I_BDEV(inode);
  2341. set_blocksize(bdev, old_block_size);
  2342. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  2343. } else {
  2344. inode_lock(inode);
  2345. inode->i_flags &= ~S_SWAPFILE;
  2346. inode_unlock(inode);
  2347. }
  2348. filp_close(swap_file, NULL);
  2349. /*
  2350. * Clear the SWP_USED flag after all resources are freed so that swapon
  2351. * can reuse this swap_info in alloc_swap_info() safely. It is ok to
  2352. * not hold p->lock after we cleared its SWP_WRITEOK.
  2353. */
  2354. spin_lock(&swap_lock);
  2355. p->flags = 0;
  2356. spin_unlock(&swap_lock);
  2357. err = 0;
  2358. atomic_inc(&proc_poll_event);
  2359. wake_up_interruptible(&proc_poll_wait);
  2360. out_dput:
  2361. filp_close(victim, NULL);
  2362. out:
  2363. putname(pathname);
  2364. return err;
  2365. }
  2366. #ifdef CONFIG_PROC_FS
  2367. static __poll_t swaps_poll(struct file *file, poll_table *wait)
  2368. {
  2369. struct seq_file *seq = file->private_data;
  2370. poll_wait(file, &proc_poll_wait, wait);
  2371. if (seq->poll_event != atomic_read(&proc_poll_event)) {
  2372. seq->poll_event = atomic_read(&proc_poll_event);
  2373. return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
  2374. }
  2375. return EPOLLIN | EPOLLRDNORM;
  2376. }
  2377. /* iterator */
  2378. static void *swap_start(struct seq_file *swap, loff_t *pos)
  2379. {
  2380. struct swap_info_struct *si;
  2381. int type;
  2382. loff_t l = *pos;
  2383. mutex_lock(&swapon_mutex);
  2384. if (!l)
  2385. return SEQ_START_TOKEN;
  2386. for (type = 0; (si = swap_type_to_swap_info(type)); type++) {
  2387. if (!(si->flags & SWP_USED) || !si->swap_map)
  2388. continue;
  2389. if (!--l)
  2390. return si;
  2391. }
  2392. return NULL;
  2393. }
  2394. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  2395. {
  2396. struct swap_info_struct *si = v;
  2397. int type;
  2398. if (v == SEQ_START_TOKEN)
  2399. type = 0;
  2400. else
  2401. type = si->type + 1;
  2402. for (; (si = swap_type_to_swap_info(type)); type++) {
  2403. if (!(si->flags & SWP_USED) || !si->swap_map)
  2404. continue;
  2405. ++*pos;
  2406. return si;
  2407. }
  2408. return NULL;
  2409. }
  2410. static void swap_stop(struct seq_file *swap, void *v)
  2411. {
  2412. mutex_unlock(&swapon_mutex);
  2413. }
  2414. static int swap_show(struct seq_file *swap, void *v)
  2415. {
  2416. struct swap_info_struct *si = v;
  2417. struct file *file;
  2418. int len;
  2419. if (si == SEQ_START_TOKEN) {
  2420. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  2421. return 0;
  2422. }
  2423. file = si->swap_file;
  2424. len = seq_file_path(swap, file, " \t\n\\");
  2425. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  2426. len < 40 ? 40 - len : 1, " ",
  2427. S_ISBLK(file_inode(file)->i_mode) ?
  2428. "partition" : "file\t",
  2429. si->pages << (PAGE_SHIFT - 10),
  2430. si->inuse_pages << (PAGE_SHIFT - 10),
  2431. si->prio);
  2432. return 0;
  2433. }
  2434. static const struct seq_operations swaps_op = {
  2435. .start = swap_start,
  2436. .next = swap_next,
  2437. .stop = swap_stop,
  2438. .show = swap_show
  2439. };
  2440. static int swaps_open(struct inode *inode, struct file *file)
  2441. {
  2442. struct seq_file *seq;
  2443. int ret;
  2444. ret = seq_open(file, &swaps_op);
  2445. if (ret)
  2446. return ret;
  2447. seq = file->private_data;
  2448. seq->poll_event = atomic_read(&proc_poll_event);
  2449. return 0;
  2450. }
  2451. static const struct file_operations proc_swaps_operations = {
  2452. .open = swaps_open,
  2453. .read = seq_read,
  2454. .llseek = seq_lseek,
  2455. .release = seq_release,
  2456. .poll = swaps_poll,
  2457. };
  2458. static int __init procswaps_init(void)
  2459. {
  2460. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  2461. return 0;
  2462. }
  2463. __initcall(procswaps_init);
  2464. #endif /* CONFIG_PROC_FS */
  2465. #ifdef MAX_SWAPFILES_CHECK
  2466. static int __init max_swapfiles_check(void)
  2467. {
  2468. MAX_SWAPFILES_CHECK();
  2469. return 0;
  2470. }
  2471. late_initcall(max_swapfiles_check);
  2472. #endif
  2473. static struct swap_info_struct *alloc_swap_info(void)
  2474. {
  2475. struct swap_info_struct *p;
  2476. unsigned int type;
  2477. int i;
  2478. int size = sizeof(*p) + nr_node_ids * sizeof(struct plist_node);
  2479. p = kvzalloc(size, GFP_KERNEL);
  2480. if (!p)
  2481. return ERR_PTR(-ENOMEM);
  2482. spin_lock(&swap_lock);
  2483. for (type = 0; type < nr_swapfiles; type++) {
  2484. if (!(swap_info[type]->flags & SWP_USED))
  2485. break;
  2486. }
  2487. if (type >= MAX_SWAPFILES) {
  2488. spin_unlock(&swap_lock);
  2489. kvfree(p);
  2490. return ERR_PTR(-EPERM);
  2491. }
  2492. if (type >= nr_swapfiles) {
  2493. p->type = type;
  2494. WRITE_ONCE(swap_info[type], p);
  2495. /*
  2496. * Write swap_info[type] before nr_swapfiles, in case a
  2497. * racing procfs swap_start() or swap_next() is reading them.
  2498. * (We never shrink nr_swapfiles, we never free this entry.)
  2499. */
  2500. smp_wmb();
  2501. WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1);
  2502. } else {
  2503. kvfree(p);
  2504. p = swap_info[type];
  2505. /*
  2506. * Do not memset this entry: a racing procfs swap_next()
  2507. * would be relying on p->type to remain valid.
  2508. */
  2509. }
  2510. INIT_LIST_HEAD(&p->first_swap_extent.list);
  2511. plist_node_init(&p->list, 0);
  2512. for_each_node(i)
  2513. plist_node_init(&p->avail_lists[i], 0);
  2514. p->flags = SWP_USED;
  2515. spin_unlock(&swap_lock);
  2516. spin_lock_init(&p->lock);
  2517. spin_lock_init(&p->cont_lock);
  2518. return p;
  2519. }
  2520. static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
  2521. {
  2522. int error;
  2523. if (S_ISBLK(inode->i_mode)) {
  2524. p->bdev = bdgrab(I_BDEV(inode));
  2525. error = blkdev_get(p->bdev,
  2526. FMODE_READ | FMODE_WRITE | FMODE_EXCL, p);
  2527. if (error < 0) {
  2528. p->bdev = NULL;
  2529. return error;
  2530. }
  2531. p->old_block_size = block_size(p->bdev);
  2532. error = set_blocksize(p->bdev, PAGE_SIZE);
  2533. if (error < 0)
  2534. return error;
  2535. p->flags |= SWP_BLKDEV;
  2536. } else if (S_ISREG(inode->i_mode)) {
  2537. p->bdev = inode->i_sb->s_bdev;
  2538. inode_lock(inode);
  2539. if (IS_SWAPFILE(inode))
  2540. return -EBUSY;
  2541. } else
  2542. return -EINVAL;
  2543. return 0;
  2544. }
  2545. /*
  2546. * Find out how many pages are allowed for a single swap device. There
  2547. * are two limiting factors:
  2548. * 1) the number of bits for the swap offset in the swp_entry_t type, and
  2549. * 2) the number of bits in the swap pte, as defined by the different
  2550. * architectures.
  2551. *
  2552. * In order to find the largest possible bit mask, a swap entry with
  2553. * swap type 0 and swap offset ~0UL is created, encoded to a swap pte,
  2554. * decoded to a swp_entry_t again, and finally the swap offset is
  2555. * extracted.
  2556. *
  2557. * This will mask all the bits from the initial ~0UL mask that can't
  2558. * be encoded in either the swp_entry_t or the architecture definition
  2559. * of a swap pte.
  2560. */
  2561. unsigned long generic_max_swapfile_size(void)
  2562. {
  2563. return swp_offset(pte_to_swp_entry(
  2564. swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
  2565. }
  2566. /* Can be overridden by an architecture for additional checks. */
  2567. __weak unsigned long max_swapfile_size(void)
  2568. {
  2569. return generic_max_swapfile_size();
  2570. }
  2571. static unsigned long read_swap_header(struct swap_info_struct *p,
  2572. union swap_header *swap_header,
  2573. struct inode *inode)
  2574. {
  2575. int i;
  2576. unsigned long maxpages;
  2577. unsigned long swapfilepages;
  2578. unsigned long last_page;
  2579. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  2580. pr_err("Unable to find swap-space signature\n");
  2581. return 0;
  2582. }
  2583. /* swap partition endianess hack... */
  2584. if (swab32(swap_header->info.version) == 1) {
  2585. swab32s(&swap_header->info.version);
  2586. swab32s(&swap_header->info.last_page);
  2587. swab32s(&swap_header->info.nr_badpages);
  2588. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  2589. return 0;
  2590. for (i = 0; i < swap_header->info.nr_badpages; i++)
  2591. swab32s(&swap_header->info.badpages[i]);
  2592. }
  2593. /* Check the swap header's sub-version */
  2594. if (swap_header->info.version != 1) {
  2595. pr_warn("Unable to handle swap header version %d\n",
  2596. swap_header->info.version);
  2597. return 0;
  2598. }
  2599. p->lowest_bit = 1;
  2600. p->cluster_next = 1;
  2601. p->cluster_nr = 0;
  2602. maxpages = max_swapfile_size();
  2603. last_page = swap_header->info.last_page;
  2604. if (!last_page) {
  2605. pr_warn("Empty swap-file\n");
  2606. return 0;
  2607. }
  2608. if (last_page > maxpages) {
  2609. pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
  2610. maxpages << (PAGE_SHIFT - 10),
  2611. last_page << (PAGE_SHIFT - 10));
  2612. }
  2613. if (maxpages > last_page) {
  2614. maxpages = last_page + 1;
  2615. /* p->max is an unsigned int: don't overflow it */
  2616. if ((unsigned int)maxpages == 0)
  2617. maxpages = UINT_MAX;
  2618. }
  2619. p->highest_bit = maxpages - 1;
  2620. if (!maxpages)
  2621. return 0;
  2622. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  2623. if (swapfilepages && maxpages > swapfilepages) {
  2624. pr_warn("Swap area shorter than signature indicates\n");
  2625. return 0;
  2626. }
  2627. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  2628. return 0;
  2629. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  2630. return 0;
  2631. return maxpages;
  2632. }
  2633. #define SWAP_CLUSTER_INFO_COLS \
  2634. DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info))
  2635. #define SWAP_CLUSTER_SPACE_COLS \
  2636. DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER)
  2637. #define SWAP_CLUSTER_COLS \
  2638. max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS)
  2639. static int setup_swap_map_and_extents(struct swap_info_struct *p,
  2640. union swap_header *swap_header,
  2641. unsigned char *swap_map,
  2642. struct swap_cluster_info *cluster_info,
  2643. unsigned long maxpages,
  2644. sector_t *span)
  2645. {
  2646. unsigned int j, k;
  2647. unsigned int nr_good_pages;
  2648. int nr_extents;
  2649. unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
  2650. unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS;
  2651. unsigned long i, idx;
  2652. nr_good_pages = maxpages - 1; /* omit header page */
  2653. cluster_list_init(&p->free_clusters);
  2654. cluster_list_init(&p->discard_clusters);
  2655. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  2656. unsigned int page_nr = swap_header->info.badpages[i];
  2657. if (page_nr == 0 || page_nr > swap_header->info.last_page)
  2658. return -EINVAL;
  2659. if (page_nr < maxpages) {
  2660. swap_map[page_nr] = SWAP_MAP_BAD;
  2661. nr_good_pages--;
  2662. /*
  2663. * Haven't marked the cluster free yet, no list
  2664. * operation involved
  2665. */
  2666. inc_cluster_info_page(p, cluster_info, page_nr);
  2667. }
  2668. }
  2669. /* Haven't marked the cluster free yet, no list operation involved */
  2670. for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
  2671. inc_cluster_info_page(p, cluster_info, i);
  2672. if (nr_good_pages) {
  2673. swap_map[0] = SWAP_MAP_BAD;
  2674. /*
  2675. * Not mark the cluster free yet, no list
  2676. * operation involved
  2677. */
  2678. inc_cluster_info_page(p, cluster_info, 0);
  2679. p->max = maxpages;
  2680. p->pages = nr_good_pages;
  2681. nr_extents = setup_swap_extents(p, span);
  2682. if (nr_extents < 0)
  2683. return nr_extents;
  2684. nr_good_pages = p->pages;
  2685. }
  2686. if (!nr_good_pages) {
  2687. pr_warn("Empty swap-file\n");
  2688. return -EINVAL;
  2689. }
  2690. if (!cluster_info)
  2691. return nr_extents;
  2692. /*
  2693. * Reduce false cache line sharing between cluster_info and
  2694. * sharing same address space.
  2695. */
  2696. for (k = 0; k < SWAP_CLUSTER_COLS; k++) {
  2697. j = (k + col) % SWAP_CLUSTER_COLS;
  2698. for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) {
  2699. idx = i * SWAP_CLUSTER_COLS + j;
  2700. if (idx >= nr_clusters)
  2701. continue;
  2702. if (cluster_count(&cluster_info[idx]))
  2703. continue;
  2704. cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
  2705. cluster_list_add_tail(&p->free_clusters, cluster_info,
  2706. idx);
  2707. }
  2708. }
  2709. return nr_extents;
  2710. }
  2711. /*
  2712. * Helper to sys_swapon determining if a given swap
  2713. * backing device queue supports DISCARD operations.
  2714. */
  2715. static bool swap_discardable(struct swap_info_struct *si)
  2716. {
  2717. struct request_queue *q = bdev_get_queue(si->bdev);
  2718. if (!q || !blk_queue_discard(q))
  2719. return false;
  2720. return true;
  2721. }
  2722. SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
  2723. {
  2724. struct swap_info_struct *p;
  2725. struct filename *name;
  2726. struct file *swap_file = NULL;
  2727. struct address_space *mapping;
  2728. int prio;
  2729. int error;
  2730. union swap_header *swap_header;
  2731. int nr_extents;
  2732. sector_t span;
  2733. unsigned long maxpages;
  2734. unsigned char *swap_map = NULL;
  2735. struct swap_cluster_info *cluster_info = NULL;
  2736. unsigned long *frontswap_map = NULL;
  2737. struct page *page = NULL;
  2738. struct inode *inode = NULL;
  2739. bool inced_nr_rotate_swap = false;
  2740. if (swap_flags & ~SWAP_FLAGS_VALID)
  2741. return -EINVAL;
  2742. if (!capable(CAP_SYS_ADMIN))
  2743. return -EPERM;
  2744. if (!swap_avail_heads)
  2745. return -ENOMEM;
  2746. p = alloc_swap_info();
  2747. if (IS_ERR(p))
  2748. return PTR_ERR(p);
  2749. INIT_WORK(&p->discard_work, swap_discard_work);
  2750. name = getname(specialfile);
  2751. if (IS_ERR(name)) {
  2752. error = PTR_ERR(name);
  2753. name = NULL;
  2754. goto bad_swap;
  2755. }
  2756. swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
  2757. if (IS_ERR(swap_file)) {
  2758. error = PTR_ERR(swap_file);
  2759. swap_file = NULL;
  2760. goto bad_swap;
  2761. }
  2762. p->swap_file = swap_file;
  2763. mapping = swap_file->f_mapping;
  2764. inode = mapping->host;
  2765. /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */
  2766. error = claim_swapfile(p, inode);
  2767. if (unlikely(error))
  2768. goto bad_swap;
  2769. /*
  2770. * Read the swap header.
  2771. */
  2772. if (!mapping->a_ops->readpage) {
  2773. error = -EINVAL;
  2774. goto bad_swap;
  2775. }
  2776. page = read_mapping_page(mapping, 0, swap_file);
  2777. if (IS_ERR(page)) {
  2778. error = PTR_ERR(page);
  2779. goto bad_swap;
  2780. }
  2781. swap_header = kmap(page);
  2782. maxpages = read_swap_header(p, swap_header, inode);
  2783. if (unlikely(!maxpages)) {
  2784. error = -EINVAL;
  2785. goto bad_swap;
  2786. }
  2787. /* OK, set up the swap map and apply the bad block list */
  2788. swap_map = vzalloc(maxpages);
  2789. if (!swap_map) {
  2790. error = -ENOMEM;
  2791. goto bad_swap;
  2792. }
  2793. if (bdi_cap_stable_pages_required(inode_to_bdi(inode)))
  2794. p->flags |= SWP_STABLE_WRITES;
  2795. if (bdi_cap_synchronous_io(inode_to_bdi(inode)))
  2796. p->flags |= SWP_SYNCHRONOUS_IO;
  2797. if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  2798. int cpu;
  2799. unsigned long ci, nr_cluster;
  2800. p->flags |= SWP_SOLIDSTATE;
  2801. /*
  2802. * select a random position to start with to help wear leveling
  2803. * SSD
  2804. */
  2805. p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
  2806. nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
  2807. cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
  2808. GFP_KERNEL);
  2809. if (!cluster_info) {
  2810. error = -ENOMEM;
  2811. goto bad_swap;
  2812. }
  2813. for (ci = 0; ci < nr_cluster; ci++)
  2814. spin_lock_init(&((cluster_info + ci)->lock));
  2815. p->percpu_cluster = alloc_percpu(struct percpu_cluster);
  2816. if (!p->percpu_cluster) {
  2817. error = -ENOMEM;
  2818. goto bad_swap;
  2819. }
  2820. for_each_possible_cpu(cpu) {
  2821. struct percpu_cluster *cluster;
  2822. cluster = per_cpu_ptr(p->percpu_cluster, cpu);
  2823. cluster_set_null(&cluster->index);
  2824. }
  2825. } else {
  2826. atomic_inc(&nr_rotate_swap);
  2827. inced_nr_rotate_swap = true;
  2828. }
  2829. error = swap_cgroup_swapon(p->type, maxpages);
  2830. if (error)
  2831. goto bad_swap;
  2832. nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
  2833. cluster_info, maxpages, &span);
  2834. if (unlikely(nr_extents < 0)) {
  2835. error = nr_extents;
  2836. goto bad_swap;
  2837. }
  2838. /* frontswap enabled? set up bit-per-page map for frontswap */
  2839. if (IS_ENABLED(CONFIG_FRONTSWAP))
  2840. frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages),
  2841. sizeof(long),
  2842. GFP_KERNEL);
  2843. if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {
  2844. /*
  2845. * When discard is enabled for swap with no particular
  2846. * policy flagged, we set all swap discard flags here in
  2847. * order to sustain backward compatibility with older
  2848. * swapon(8) releases.
  2849. */
  2850. p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD |
  2851. SWP_PAGE_DISCARD);
  2852. /*
  2853. * By flagging sys_swapon, a sysadmin can tell us to
  2854. * either do single-time area discards only, or to just
  2855. * perform discards for released swap page-clusters.
  2856. * Now it's time to adjust the p->flags accordingly.
  2857. */
  2858. if (swap_flags & SWAP_FLAG_DISCARD_ONCE)
  2859. p->flags &= ~SWP_PAGE_DISCARD;
  2860. else if (swap_flags & SWAP_FLAG_DISCARD_PAGES)
  2861. p->flags &= ~SWP_AREA_DISCARD;
  2862. /* issue a swapon-time discard if it's still required */
  2863. if (p->flags & SWP_AREA_DISCARD) {
  2864. int err = discard_swap(p);
  2865. if (unlikely(err))
  2866. pr_err("swapon: discard_swap(%p): %d\n",
  2867. p, err);
  2868. }
  2869. }
  2870. error = init_swap_address_space(p->type, maxpages);
  2871. if (error)
  2872. goto bad_swap;
  2873. mutex_lock(&swapon_mutex);
  2874. prio = -1;
  2875. if (swap_flags & SWAP_FLAG_PREFER)
  2876. prio =
  2877. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  2878. enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);
  2879. pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n",
  2880. p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
  2881. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  2882. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  2883. (p->flags & SWP_DISCARDABLE) ? "D" : "",
  2884. (p->flags & SWP_AREA_DISCARD) ? "s" : "",
  2885. (p->flags & SWP_PAGE_DISCARD) ? "c" : "",
  2886. (frontswap_map) ? "FS" : "");
  2887. mutex_unlock(&swapon_mutex);
  2888. atomic_inc(&proc_poll_event);
  2889. wake_up_interruptible(&proc_poll_wait);
  2890. if (S_ISREG(inode->i_mode))
  2891. inode->i_flags |= S_SWAPFILE;
  2892. error = 0;
  2893. goto out;
  2894. bad_swap:
  2895. free_percpu(p->percpu_cluster);
  2896. p->percpu_cluster = NULL;
  2897. if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
  2898. set_blocksize(p->bdev, p->old_block_size);
  2899. blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  2900. }
  2901. destroy_swap_extents(p);
  2902. swap_cgroup_swapoff(p->type);
  2903. spin_lock(&swap_lock);
  2904. p->swap_file = NULL;
  2905. p->flags = 0;
  2906. spin_unlock(&swap_lock);
  2907. vfree(swap_map);
  2908. kvfree(cluster_info);
  2909. kvfree(frontswap_map);
  2910. if (inced_nr_rotate_swap)
  2911. atomic_dec(&nr_rotate_swap);
  2912. if (swap_file) {
  2913. if (inode && S_ISREG(inode->i_mode)) {
  2914. inode_unlock(inode);
  2915. inode = NULL;
  2916. }
  2917. filp_close(swap_file, NULL);
  2918. }
  2919. out:
  2920. if (page && !IS_ERR(page)) {
  2921. kunmap(page);
  2922. put_page(page);
  2923. }
  2924. if (name)
  2925. putname(name);
  2926. if (inode && S_ISREG(inode->i_mode))
  2927. inode_unlock(inode);
  2928. if (!error)
  2929. enable_swap_slots_cache();
  2930. return error;
  2931. }
  2932. void si_swapinfo(struct sysinfo *val)
  2933. {
  2934. unsigned int type;
  2935. unsigned long nr_to_be_unused = 0;
  2936. spin_lock(&swap_lock);
  2937. for (type = 0; type < nr_swapfiles; type++) {
  2938. struct swap_info_struct *si = swap_info[type];
  2939. if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
  2940. nr_to_be_unused += si->inuse_pages;
  2941. }
  2942. val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
  2943. val->totalswap = total_swap_pages + nr_to_be_unused;
  2944. spin_unlock(&swap_lock);
  2945. }
  2946. /*
  2947. * Verify that a swap entry is valid and increment its swap map count.
  2948. *
  2949. * Returns error code in following case.
  2950. * - success -> 0
  2951. * - swp_entry is invalid -> EINVAL
  2952. * - swp_entry is migration entry -> EINVAL
  2953. * - swap-cache reference is requested but there is already one. -> EEXIST
  2954. * - swap-cache reference is requested but the entry is not used. -> ENOENT
  2955. * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
  2956. */
  2957. static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  2958. {
  2959. struct swap_info_struct *p;
  2960. struct swap_cluster_info *ci;
  2961. unsigned long offset;
  2962. unsigned char count;
  2963. unsigned char has_cache;
  2964. int err = -EINVAL;
  2965. if (non_swap_entry(entry))
  2966. goto out;
  2967. p = swp_swap_info(entry);
  2968. if (!p)
  2969. goto bad_file;
  2970. offset = swp_offset(entry);
  2971. if (unlikely(offset >= p->max))
  2972. goto out;
  2973. ci = lock_cluster_or_swap_info(p, offset);
  2974. count = p->swap_map[offset];
  2975. /*
  2976. * swapin_readahead() doesn't check if a swap entry is valid, so the
  2977. * swap entry could be SWAP_MAP_BAD. Check here with lock held.
  2978. */
  2979. if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
  2980. err = -ENOENT;
  2981. goto unlock_out;
  2982. }
  2983. has_cache = count & SWAP_HAS_CACHE;
  2984. count &= ~SWAP_HAS_CACHE;
  2985. err = 0;
  2986. if (usage == SWAP_HAS_CACHE) {
  2987. /* set SWAP_HAS_CACHE if there is no cache and entry is used */
  2988. if (!has_cache && count)
  2989. has_cache = SWAP_HAS_CACHE;
  2990. else if (has_cache) /* someone else added cache */
  2991. err = -EEXIST;
  2992. else /* no users remaining */
  2993. err = -ENOENT;
  2994. } else if (count || has_cache) {
  2995. if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
  2996. count += usage;
  2997. else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
  2998. err = -EINVAL;
  2999. else if (swap_count_continued(p, offset, count))
  3000. count = COUNT_CONTINUED;
  3001. else
  3002. err = -ENOMEM;
  3003. } else
  3004. err = -ENOENT; /* unused swap entry */
  3005. p->swap_map[offset] = count | has_cache;
  3006. unlock_out:
  3007. unlock_cluster_or_swap_info(p, ci);
  3008. out:
  3009. return err;
  3010. bad_file:
  3011. pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val);
  3012. goto out;
  3013. }
  3014. /*
  3015. * Help swapoff by noting that swap entry belongs to shmem/tmpfs
  3016. * (in which case its reference count is never incremented).
  3017. */
  3018. void swap_shmem_alloc(swp_entry_t entry)
  3019. {
  3020. __swap_duplicate(entry, SWAP_MAP_SHMEM);
  3021. }
  3022. /*
  3023. * Increase reference count of swap entry by 1.
  3024. * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
  3025. * but could not be atomically allocated. Returns 0, just as if it succeeded,
  3026. * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
  3027. * might occur if a page table entry has got corrupted.
  3028. */
  3029. int swap_duplicate(swp_entry_t entry)
  3030. {
  3031. int err = 0;
  3032. while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
  3033. err = add_swap_count_continuation(entry, GFP_ATOMIC);
  3034. return err;
  3035. }
  3036. /*
  3037. * @entry: swap entry for which we allocate swap cache.
  3038. *
  3039. * Called when allocating swap cache for existing swap entry,
  3040. * This can return error codes. Returns 0 at success.
  3041. * -EBUSY means there is a swap cache.
  3042. * Note: return code is different from swap_duplicate().
  3043. */
  3044. int swapcache_prepare(swp_entry_t entry)
  3045. {
  3046. return __swap_duplicate(entry, SWAP_HAS_CACHE);
  3047. }
  3048. struct swap_info_struct *swp_swap_info(swp_entry_t entry)
  3049. {
  3050. return swap_type_to_swap_info(swp_type(entry));
  3051. }
  3052. struct swap_info_struct *page_swap_info(struct page *page)
  3053. {
  3054. swp_entry_t entry = { .val = page_private(page) };
  3055. return swp_swap_info(entry);
  3056. }
  3057. /*
  3058. * out-of-line __page_file_ methods to avoid include hell.
  3059. */
  3060. struct address_space *__page_file_mapping(struct page *page)
  3061. {
  3062. return page_swap_info(page)->swap_file->f_mapping;
  3063. }
  3064. EXPORT_SYMBOL_GPL(__page_file_mapping);
  3065. pgoff_t __page_file_index(struct page *page)
  3066. {
  3067. swp_entry_t swap = { .val = page_private(page) };
  3068. return swp_offset(swap);
  3069. }
  3070. EXPORT_SYMBOL_GPL(__page_file_index);
  3071. /*
  3072. * add_swap_count_continuation - called when a swap count is duplicated
  3073. * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  3074. * page of the original vmalloc'ed swap_map, to hold the continuation count
  3075. * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
  3076. * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
  3077. *
  3078. * These continuation pages are seldom referenced: the common paths all work
  3079. * on the original swap_map, only referring to a continuation page when the
  3080. * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
  3081. *
  3082. * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
  3083. * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
  3084. * can be called after dropping locks.
  3085. */
  3086. int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
  3087. {
  3088. struct swap_info_struct *si;
  3089. struct swap_cluster_info *ci;
  3090. struct page *head;
  3091. struct page *page;
  3092. struct page *list_page;
  3093. pgoff_t offset;
  3094. unsigned char count;
  3095. /*
  3096. * When debugging, it's easier to use __GFP_ZERO here; but it's better
  3097. * for latency not to zero a page while GFP_ATOMIC and holding locks.
  3098. */
  3099. page = alloc_page(gfp_mask | __GFP_HIGHMEM);
  3100. si = swap_info_get(entry);
  3101. if (!si) {
  3102. /*
  3103. * An acceptable race has occurred since the failing
  3104. * __swap_duplicate(): the swap entry has been freed,
  3105. * perhaps even the whole swap_map cleared for swapoff.
  3106. */
  3107. goto outer;
  3108. }
  3109. offset = swp_offset(entry);
  3110. ci = lock_cluster(si, offset);
  3111. count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
  3112. if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
  3113. /*
  3114. * The higher the swap count, the more likely it is that tasks
  3115. * will race to add swap count continuation: we need to avoid
  3116. * over-provisioning.
  3117. */
  3118. goto out;
  3119. }
  3120. if (!page) {
  3121. unlock_cluster(ci);
  3122. spin_unlock(&si->lock);
  3123. return -ENOMEM;
  3124. }
  3125. /*
  3126. * We are fortunate that although vmalloc_to_page uses pte_offset_map,
  3127. * no architecture is using highmem pages for kernel page tables: so it
  3128. * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps.
  3129. */
  3130. head = vmalloc_to_page(si->swap_map + offset);
  3131. offset &= ~PAGE_MASK;
  3132. spin_lock(&si->cont_lock);
  3133. /*
  3134. * Page allocation does not initialize the page's lru field,
  3135. * but it does always reset its private field.
  3136. */
  3137. if (!page_private(head)) {
  3138. BUG_ON(count & COUNT_CONTINUED);
  3139. INIT_LIST_HEAD(&head->lru);
  3140. set_page_private(head, SWP_CONTINUED);
  3141. si->flags |= SWP_CONTINUED;
  3142. }
  3143. list_for_each_entry(list_page, &head->lru, lru) {
  3144. unsigned char *map;
  3145. /*
  3146. * If the previous map said no continuation, but we've found
  3147. * a continuation page, free our allocation and use this one.
  3148. */
  3149. if (!(count & COUNT_CONTINUED))
  3150. goto out_unlock_cont;
  3151. map = kmap_atomic(list_page) + offset;
  3152. count = *map;
  3153. kunmap_atomic(map);
  3154. /*
  3155. * If this continuation count now has some space in it,
  3156. * free our allocation and use this one.
  3157. */
  3158. if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
  3159. goto out_unlock_cont;
  3160. }
  3161. list_add_tail(&page->lru, &head->lru);
  3162. page = NULL; /* now it's attached, don't free it */
  3163. out_unlock_cont:
  3164. spin_unlock(&si->cont_lock);
  3165. out:
  3166. unlock_cluster(ci);
  3167. spin_unlock(&si->lock);
  3168. outer:
  3169. if (page)
  3170. __free_page(page);
  3171. return 0;
  3172. }
  3173. /*
  3174. * swap_count_continued - when the original swap_map count is incremented
  3175. * from SWAP_MAP_MAX, check if there is already a continuation page to carry
  3176. * into, carry if so, or else fail until a new continuation page is allocated;
  3177. * when the original swap_map count is decremented from 0 with continuation,
  3178. * borrow from the continuation and report whether it still holds more.
  3179. * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster
  3180. * lock.
  3181. */
  3182. static bool swap_count_continued(struct swap_info_struct *si,
  3183. pgoff_t offset, unsigned char count)
  3184. {
  3185. struct page *head;
  3186. struct page *page;
  3187. unsigned char *map;
  3188. bool ret;
  3189. head = vmalloc_to_page(si->swap_map + offset);
  3190. if (page_private(head) != SWP_CONTINUED) {
  3191. BUG_ON(count & COUNT_CONTINUED);
  3192. return false; /* need to add count continuation */
  3193. }
  3194. spin_lock(&si->cont_lock);
  3195. offset &= ~PAGE_MASK;
  3196. page = list_entry(head->lru.next, struct page, lru);
  3197. map = kmap_atomic(page) + offset;
  3198. if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
  3199. goto init_map; /* jump over SWAP_CONT_MAX checks */
  3200. if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
  3201. /*
  3202. * Think of how you add 1 to 999
  3203. */
  3204. while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
  3205. kunmap_atomic(map);
  3206. page = list_entry(page->lru.next, struct page, lru);
  3207. BUG_ON(page == head);
  3208. map = kmap_atomic(page) + offset;
  3209. }
  3210. if (*map == SWAP_CONT_MAX) {
  3211. kunmap_atomic(map);
  3212. page = list_entry(page->lru.next, struct page, lru);
  3213. if (page == head) {
  3214. ret = false; /* add count continuation */
  3215. goto out;
  3216. }
  3217. map = kmap_atomic(page) + offset;
  3218. init_map: *map = 0; /* we didn't zero the page */
  3219. }
  3220. *map += 1;
  3221. kunmap_atomic(map);
  3222. page = list_entry(page->lru.prev, struct page, lru);
  3223. while (page != head) {
  3224. map = kmap_atomic(page) + offset;
  3225. *map = COUNT_CONTINUED;
  3226. kunmap_atomic(map);
  3227. page = list_entry(page->lru.prev, struct page, lru);
  3228. }
  3229. ret = true; /* incremented */
  3230. } else { /* decrementing */
  3231. /*
  3232. * Think of how you subtract 1 from 1000
  3233. */
  3234. BUG_ON(count != COUNT_CONTINUED);
  3235. while (*map == COUNT_CONTINUED) {
  3236. kunmap_atomic(map);
  3237. page = list_entry(page->lru.next, struct page, lru);
  3238. BUG_ON(page == head);
  3239. map = kmap_atomic(page) + offset;
  3240. }
  3241. BUG_ON(*map == 0);
  3242. *map -= 1;
  3243. if (*map == 0)
  3244. count = 0;
  3245. kunmap_atomic(map);
  3246. page = list_entry(page->lru.prev, struct page, lru);
  3247. while (page != head) {
  3248. map = kmap_atomic(page) + offset;
  3249. *map = SWAP_CONT_MAX | count;
  3250. count = COUNT_CONTINUED;
  3251. kunmap_atomic(map);
  3252. page = list_entry(page->lru.prev, struct page, lru);
  3253. }
  3254. ret = count == COUNT_CONTINUED;
  3255. }
  3256. out:
  3257. spin_unlock(&si->cont_lock);
  3258. return ret;
  3259. }
  3260. /*
  3261. * free_swap_count_continuations - swapoff free all the continuation pages
  3262. * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
  3263. */
  3264. static void free_swap_count_continuations(struct swap_info_struct *si)
  3265. {
  3266. pgoff_t offset;
  3267. for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
  3268. struct page *head;
  3269. head = vmalloc_to_page(si->swap_map + offset);
  3270. if (page_private(head)) {
  3271. struct page *page, *next;
  3272. list_for_each_entry_safe(page, next, &head->lru, lru) {
  3273. list_del(&page->lru);
  3274. __free_page(page);
  3275. }
  3276. }
  3277. }
  3278. }
  3279. #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
  3280. void mem_cgroup_throttle_swaprate(struct mem_cgroup *memcg, int node,
  3281. gfp_t gfp_mask)
  3282. {
  3283. struct swap_info_struct *si, *next;
  3284. if (!(gfp_mask & __GFP_IO) || !memcg)
  3285. return;
  3286. if (!blk_cgroup_congested())
  3287. return;
  3288. /*
  3289. * We've already scheduled a throttle, avoid taking the global swap
  3290. * lock.
  3291. */
  3292. if (current->throttle_queue)
  3293. return;
  3294. spin_lock(&swap_avail_lock);
  3295. plist_for_each_entry_safe(si, next, &swap_avail_heads[node],
  3296. avail_lists[node]) {
  3297. if (si->bdev) {
  3298. blkcg_schedule_throttle(bdev_get_queue(si->bdev),
  3299. true);
  3300. break;
  3301. }
  3302. }
  3303. spin_unlock(&swap_avail_lock);
  3304. }
  3305. #endif
  3306. static int __init swapfile_init(void)
  3307. {
  3308. int nid;
  3309. swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head),
  3310. GFP_KERNEL);
  3311. if (!swap_avail_heads) {
  3312. pr_emerg("Not enough memory for swap heads, swap is disabled\n");
  3313. return -ENOMEM;
  3314. }
  3315. for_each_node(nid)
  3316. plist_head_init(&swap_avail_heads[nid]);
  3317. return 0;
  3318. }
  3319. subsys_initcall(swapfile_init);