swapfile.c 84 KB

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