swapfile.c 74 KB

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