swapfile.c 68 KB

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