swapfile.c 66 KB

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