swapfile.c 63 KB

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