swapfile.c 61 KB

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