swapfile.c 62 KB

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