swapfile.c 62 KB

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