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 inline 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. SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
  1393. {
  1394. struct swap_info_struct *p = NULL;
  1395. unsigned char *swap_map;
  1396. struct file *swap_file, *victim;
  1397. struct address_space *mapping;
  1398. struct inode *inode;
  1399. char *pathname;
  1400. int i, type, prev;
  1401. int err;
  1402. if (!capable(CAP_SYS_ADMIN))
  1403. return -EPERM;
  1404. pathname = getname(specialfile);
  1405. err = PTR_ERR(pathname);
  1406. if (IS_ERR(pathname))
  1407. goto out;
  1408. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1409. putname(pathname);
  1410. err = PTR_ERR(victim);
  1411. if (IS_ERR(victim))
  1412. goto out;
  1413. mapping = victim->f_mapping;
  1414. prev = -1;
  1415. spin_lock(&swap_lock);
  1416. for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
  1417. p = swap_info[type];
  1418. if (p->flags & SWP_WRITEOK) {
  1419. if (p->swap_file->f_mapping == mapping)
  1420. break;
  1421. }
  1422. prev = type;
  1423. }
  1424. if (type < 0) {
  1425. err = -EINVAL;
  1426. spin_unlock(&swap_lock);
  1427. goto out_dput;
  1428. }
  1429. if (!security_vm_enough_memory(p->pages))
  1430. vm_unacct_memory(p->pages);
  1431. else {
  1432. err = -ENOMEM;
  1433. spin_unlock(&swap_lock);
  1434. goto out_dput;
  1435. }
  1436. if (prev < 0)
  1437. swap_list.head = p->next;
  1438. else
  1439. swap_info[prev]->next = p->next;
  1440. if (type == swap_list.next) {
  1441. /* just pick something that's safe... */
  1442. swap_list.next = swap_list.head;
  1443. }
  1444. if (p->prio < 0) {
  1445. for (i = p->next; i >= 0; i = swap_info[i]->next)
  1446. swap_info[i]->prio = p->prio--;
  1447. least_priority++;
  1448. }
  1449. nr_swap_pages -= p->pages;
  1450. total_swap_pages -= p->pages;
  1451. p->flags &= ~SWP_WRITEOK;
  1452. spin_unlock(&swap_lock);
  1453. current->flags |= PF_OOM_ORIGIN;
  1454. err = try_to_unuse(type);
  1455. current->flags &= ~PF_OOM_ORIGIN;
  1456. if (err) {
  1457. /* re-insert swap space back into swap_list */
  1458. spin_lock(&swap_lock);
  1459. if (p->prio < 0)
  1460. p->prio = --least_priority;
  1461. prev = -1;
  1462. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1463. if (p->prio >= swap_info[i]->prio)
  1464. break;
  1465. prev = i;
  1466. }
  1467. p->next = i;
  1468. if (prev < 0)
  1469. swap_list.head = swap_list.next = type;
  1470. else
  1471. swap_info[prev]->next = type;
  1472. nr_swap_pages += p->pages;
  1473. total_swap_pages += p->pages;
  1474. p->flags |= SWP_WRITEOK;
  1475. spin_unlock(&swap_lock);
  1476. goto out_dput;
  1477. }
  1478. /* wait for any unplug function to finish */
  1479. down_write(&swap_unplug_sem);
  1480. up_write(&swap_unplug_sem);
  1481. destroy_swap_extents(p);
  1482. if (p->flags & SWP_CONTINUED)
  1483. free_swap_count_continuations(p);
  1484. mutex_lock(&swapon_mutex);
  1485. spin_lock(&swap_lock);
  1486. drain_mmlist();
  1487. /* wait for anyone still in scan_swap_map */
  1488. p->highest_bit = 0; /* cuts scans short */
  1489. while (p->flags >= SWP_SCANNING) {
  1490. spin_unlock(&swap_lock);
  1491. schedule_timeout_uninterruptible(1);
  1492. spin_lock(&swap_lock);
  1493. }
  1494. swap_file = p->swap_file;
  1495. p->swap_file = NULL;
  1496. p->max = 0;
  1497. swap_map = p->swap_map;
  1498. p->swap_map = NULL;
  1499. p->flags = 0;
  1500. spin_unlock(&swap_lock);
  1501. mutex_unlock(&swapon_mutex);
  1502. vfree(swap_map);
  1503. /* Destroy swap account informatin */
  1504. swap_cgroup_swapoff(type);
  1505. inode = mapping->host;
  1506. if (S_ISBLK(inode->i_mode)) {
  1507. struct block_device *bdev = I_BDEV(inode);
  1508. set_blocksize(bdev, p->old_block_size);
  1509. blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1510. } else {
  1511. mutex_lock(&inode->i_mutex);
  1512. inode->i_flags &= ~S_SWAPFILE;
  1513. mutex_unlock(&inode->i_mutex);
  1514. }
  1515. filp_close(swap_file, NULL);
  1516. err = 0;
  1517. atomic_inc(&proc_poll_event);
  1518. wake_up_interruptible(&proc_poll_wait);
  1519. out_dput:
  1520. filp_close(victim, NULL);
  1521. out:
  1522. return err;
  1523. }
  1524. #ifdef CONFIG_PROC_FS
  1525. struct proc_swaps {
  1526. struct seq_file seq;
  1527. int event;
  1528. };
  1529. static unsigned swaps_poll(struct file *file, poll_table *wait)
  1530. {
  1531. struct proc_swaps *s = file->private_data;
  1532. poll_wait(file, &proc_poll_wait, wait);
  1533. if (s->event != atomic_read(&proc_poll_event)) {
  1534. s->event = atomic_read(&proc_poll_event);
  1535. return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
  1536. }
  1537. return POLLIN | POLLRDNORM;
  1538. }
  1539. /* iterator */
  1540. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1541. {
  1542. struct swap_info_struct *si;
  1543. int type;
  1544. loff_t l = *pos;
  1545. mutex_lock(&swapon_mutex);
  1546. if (!l)
  1547. return SEQ_START_TOKEN;
  1548. for (type = 0; type < nr_swapfiles; type++) {
  1549. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1550. si = swap_info[type];
  1551. if (!(si->flags & SWP_USED) || !si->swap_map)
  1552. continue;
  1553. if (!--l)
  1554. return si;
  1555. }
  1556. return NULL;
  1557. }
  1558. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1559. {
  1560. struct swap_info_struct *si = v;
  1561. int type;
  1562. if (v == SEQ_START_TOKEN)
  1563. type = 0;
  1564. else
  1565. type = si->type + 1;
  1566. for (; type < nr_swapfiles; type++) {
  1567. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1568. si = swap_info[type];
  1569. if (!(si->flags & SWP_USED) || !si->swap_map)
  1570. continue;
  1571. ++*pos;
  1572. return si;
  1573. }
  1574. return NULL;
  1575. }
  1576. static void swap_stop(struct seq_file *swap, void *v)
  1577. {
  1578. mutex_unlock(&swapon_mutex);
  1579. }
  1580. static int swap_show(struct seq_file *swap, void *v)
  1581. {
  1582. struct swap_info_struct *si = v;
  1583. struct file *file;
  1584. int len;
  1585. if (si == SEQ_START_TOKEN) {
  1586. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1587. return 0;
  1588. }
  1589. file = si->swap_file;
  1590. len = seq_path(swap, &file->f_path, " \t\n\\");
  1591. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1592. len < 40 ? 40 - len : 1, " ",
  1593. S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
  1594. "partition" : "file\t",
  1595. si->pages << (PAGE_SHIFT - 10),
  1596. si->inuse_pages << (PAGE_SHIFT - 10),
  1597. si->prio);
  1598. return 0;
  1599. }
  1600. static const struct seq_operations swaps_op = {
  1601. .start = swap_start,
  1602. .next = swap_next,
  1603. .stop = swap_stop,
  1604. .show = swap_show
  1605. };
  1606. static int swaps_open(struct inode *inode, struct file *file)
  1607. {
  1608. struct proc_swaps *s;
  1609. int ret;
  1610. s = kmalloc(sizeof(struct proc_swaps), GFP_KERNEL);
  1611. if (!s)
  1612. return -ENOMEM;
  1613. file->private_data = s;
  1614. ret = seq_open(file, &swaps_op);
  1615. if (ret) {
  1616. kfree(s);
  1617. return ret;
  1618. }
  1619. s->seq.private = s;
  1620. s->event = atomic_read(&proc_poll_event);
  1621. return ret;
  1622. }
  1623. static const struct file_operations proc_swaps_operations = {
  1624. .open = swaps_open,
  1625. .read = seq_read,
  1626. .llseek = seq_lseek,
  1627. .release = seq_release,
  1628. .poll = swaps_poll,
  1629. };
  1630. static int __init procswaps_init(void)
  1631. {
  1632. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  1633. return 0;
  1634. }
  1635. __initcall(procswaps_init);
  1636. #endif /* CONFIG_PROC_FS */
  1637. #ifdef MAX_SWAPFILES_CHECK
  1638. static int __init max_swapfiles_check(void)
  1639. {
  1640. MAX_SWAPFILES_CHECK();
  1641. return 0;
  1642. }
  1643. late_initcall(max_swapfiles_check);
  1644. #endif
  1645. static struct swap_info_struct *alloc_swap_info(void)
  1646. {
  1647. struct swap_info_struct *p;
  1648. unsigned int type;
  1649. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1650. if (!p)
  1651. return ERR_PTR(-ENOMEM);
  1652. spin_lock(&swap_lock);
  1653. for (type = 0; type < nr_swapfiles; type++) {
  1654. if (!(swap_info[type]->flags & SWP_USED))
  1655. break;
  1656. }
  1657. if (type >= MAX_SWAPFILES) {
  1658. spin_unlock(&swap_lock);
  1659. kfree(p);
  1660. return ERR_PTR(-EPERM);
  1661. }
  1662. if (type >= nr_swapfiles) {
  1663. p->type = type;
  1664. swap_info[type] = p;
  1665. /*
  1666. * Write swap_info[type] before nr_swapfiles, in case a
  1667. * racing procfs swap_start() or swap_next() is reading them.
  1668. * (We never shrink nr_swapfiles, we never free this entry.)
  1669. */
  1670. smp_wmb();
  1671. nr_swapfiles++;
  1672. } else {
  1673. kfree(p);
  1674. p = swap_info[type];
  1675. /*
  1676. * Do not memset this entry: a racing procfs swap_next()
  1677. * would be relying on p->type to remain valid.
  1678. */
  1679. }
  1680. INIT_LIST_HEAD(&p->first_swap_extent.list);
  1681. p->flags = SWP_USED;
  1682. p->next = -1;
  1683. spin_unlock(&swap_lock);
  1684. return p;
  1685. }
  1686. static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
  1687. {
  1688. int error;
  1689. if (S_ISBLK(inode->i_mode)) {
  1690. p->bdev = bdgrab(I_BDEV(inode));
  1691. error = blkdev_get(p->bdev,
  1692. FMODE_READ | FMODE_WRITE | FMODE_EXCL,
  1693. sys_swapon);
  1694. if (error < 0) {
  1695. p->bdev = NULL;
  1696. return -EINVAL;
  1697. }
  1698. p->old_block_size = block_size(p->bdev);
  1699. error = set_blocksize(p->bdev, PAGE_SIZE);
  1700. if (error < 0)
  1701. return error;
  1702. p->flags |= SWP_BLKDEV;
  1703. } else if (S_ISREG(inode->i_mode)) {
  1704. p->bdev = inode->i_sb->s_bdev;
  1705. mutex_lock(&inode->i_mutex);
  1706. if (IS_SWAPFILE(inode))
  1707. return -EBUSY;
  1708. } else
  1709. return -EINVAL;
  1710. return 0;
  1711. }
  1712. static unsigned long read_swap_header(struct swap_info_struct *p,
  1713. union swap_header *swap_header,
  1714. struct inode *inode)
  1715. {
  1716. int i;
  1717. unsigned long maxpages;
  1718. unsigned long swapfilepages;
  1719. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  1720. printk(KERN_ERR "Unable to find swap-space signature\n");
  1721. return 0;
  1722. }
  1723. /* swap partition endianess hack... */
  1724. if (swab32(swap_header->info.version) == 1) {
  1725. swab32s(&swap_header->info.version);
  1726. swab32s(&swap_header->info.last_page);
  1727. swab32s(&swap_header->info.nr_badpages);
  1728. for (i = 0; i < swap_header->info.nr_badpages; i++)
  1729. swab32s(&swap_header->info.badpages[i]);
  1730. }
  1731. /* Check the swap header's sub-version */
  1732. if (swap_header->info.version != 1) {
  1733. printk(KERN_WARNING
  1734. "Unable to handle swap header version %d\n",
  1735. swap_header->info.version);
  1736. return 0;
  1737. }
  1738. p->lowest_bit = 1;
  1739. p->cluster_next = 1;
  1740. p->cluster_nr = 0;
  1741. /*
  1742. * Find out how many pages are allowed for a single swap
  1743. * device. There are two limiting factors: 1) the number of
  1744. * bits for the swap offset in the swp_entry_t type and
  1745. * 2) the number of bits in the a swap pte as defined by
  1746. * the different architectures. In order to find the
  1747. * largest possible bit mask a swap entry with swap type 0
  1748. * and swap offset ~0UL is created, encoded to a swap pte,
  1749. * decoded to a swp_entry_t again and finally the swap
  1750. * offset is extracted. This will mask all the bits from
  1751. * the initial ~0UL mask that can't be encoded in either
  1752. * the swp_entry_t or the architecture definition of a
  1753. * swap pte.
  1754. */
  1755. maxpages = swp_offset(pte_to_swp_entry(
  1756. swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
  1757. if (maxpages > swap_header->info.last_page) {
  1758. maxpages = swap_header->info.last_page + 1;
  1759. /* p->max is an unsigned int: don't overflow it */
  1760. if ((unsigned int)maxpages == 0)
  1761. maxpages = UINT_MAX;
  1762. }
  1763. p->highest_bit = maxpages - 1;
  1764. if (!maxpages)
  1765. return 0;
  1766. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  1767. if (swapfilepages && maxpages > swapfilepages) {
  1768. printk(KERN_WARNING
  1769. "Swap area shorter than signature indicates\n");
  1770. return 0;
  1771. }
  1772. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1773. return 0;
  1774. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1775. return 0;
  1776. return maxpages;
  1777. }
  1778. static int setup_swap_map_and_extents(struct swap_info_struct *p,
  1779. union swap_header *swap_header,
  1780. unsigned char *swap_map,
  1781. unsigned long maxpages,
  1782. sector_t *span)
  1783. {
  1784. int i;
  1785. unsigned int nr_good_pages;
  1786. int nr_extents;
  1787. nr_good_pages = maxpages - 1; /* omit header page */
  1788. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1789. unsigned int page_nr = swap_header->info.badpages[i];
  1790. if (page_nr == 0 || page_nr > swap_header->info.last_page)
  1791. return -EINVAL;
  1792. if (page_nr < maxpages) {
  1793. swap_map[page_nr] = SWAP_MAP_BAD;
  1794. nr_good_pages--;
  1795. }
  1796. }
  1797. if (nr_good_pages) {
  1798. swap_map[0] = SWAP_MAP_BAD;
  1799. p->max = maxpages;
  1800. p->pages = nr_good_pages;
  1801. nr_extents = setup_swap_extents(p, span);
  1802. if (nr_extents < 0)
  1803. return nr_extents;
  1804. nr_good_pages = p->pages;
  1805. }
  1806. if (!nr_good_pages) {
  1807. printk(KERN_WARNING "Empty swap-file\n");
  1808. return -EINVAL;
  1809. }
  1810. return nr_extents;
  1811. }
  1812. SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
  1813. {
  1814. struct swap_info_struct *p;
  1815. char *name;
  1816. struct file *swap_file = NULL;
  1817. struct address_space *mapping;
  1818. int i, prev;
  1819. int error;
  1820. union swap_header *swap_header;
  1821. unsigned int nr_good_pages;
  1822. int nr_extents;
  1823. sector_t span;
  1824. unsigned long maxpages;
  1825. unsigned char *swap_map = NULL;
  1826. struct page *page = NULL;
  1827. struct inode *inode = NULL;
  1828. if (!capable(CAP_SYS_ADMIN))
  1829. return -EPERM;
  1830. p = alloc_swap_info();
  1831. if (IS_ERR(p))
  1832. return PTR_ERR(p);
  1833. name = getname(specialfile);
  1834. if (IS_ERR(name)) {
  1835. error = PTR_ERR(name);
  1836. name = NULL;
  1837. goto bad_swap;
  1838. }
  1839. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1840. if (IS_ERR(swap_file)) {
  1841. error = PTR_ERR(swap_file);
  1842. swap_file = NULL;
  1843. goto bad_swap;
  1844. }
  1845. p->swap_file = swap_file;
  1846. mapping = swap_file->f_mapping;
  1847. inode = mapping->host;
  1848. for (i = 0; i < nr_swapfiles; i++) {
  1849. struct swap_info_struct *q = swap_info[i];
  1850. if (q == p || !q->swap_file)
  1851. continue;
  1852. if (mapping == q->swap_file->f_mapping) {
  1853. error = -EBUSY;
  1854. goto bad_swap;
  1855. }
  1856. }
  1857. error = claim_swapfile(p, inode);
  1858. if (unlikely(error))
  1859. goto bad_swap;
  1860. /*
  1861. * Read the swap header.
  1862. */
  1863. if (!mapping->a_ops->readpage) {
  1864. error = -EINVAL;
  1865. goto bad_swap;
  1866. }
  1867. page = read_mapping_page(mapping, 0, swap_file);
  1868. if (IS_ERR(page)) {
  1869. error = PTR_ERR(page);
  1870. goto bad_swap;
  1871. }
  1872. swap_header = kmap(page);
  1873. maxpages = read_swap_header(p, swap_header, inode);
  1874. if (unlikely(!maxpages)) {
  1875. error = -EINVAL;
  1876. goto bad_swap;
  1877. }
  1878. /* OK, set up the swap map and apply the bad block list */
  1879. swap_map = vzalloc(maxpages);
  1880. if (!swap_map) {
  1881. error = -ENOMEM;
  1882. goto bad_swap;
  1883. }
  1884. error = swap_cgroup_swapon(p->type, maxpages);
  1885. if (error)
  1886. goto bad_swap;
  1887. nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
  1888. maxpages, &span);
  1889. if (unlikely(nr_extents < 0)) {
  1890. error = nr_extents;
  1891. goto bad_swap;
  1892. }
  1893. nr_good_pages = p->pages;
  1894. if (p->bdev) {
  1895. if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  1896. p->flags |= SWP_SOLIDSTATE;
  1897. p->cluster_next = 1 + (random32() % p->highest_bit);
  1898. }
  1899. if (discard_swap(p) == 0 && (swap_flags & SWAP_FLAG_DISCARD))
  1900. p->flags |= SWP_DISCARDABLE;
  1901. }
  1902. mutex_lock(&swapon_mutex);
  1903. spin_lock(&swap_lock);
  1904. if (swap_flags & SWAP_FLAG_PREFER)
  1905. p->prio =
  1906. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  1907. else
  1908. p->prio = --least_priority;
  1909. p->swap_map = swap_map;
  1910. p->flags |= SWP_WRITEOK;
  1911. nr_swap_pages += nr_good_pages;
  1912. total_swap_pages += nr_good_pages;
  1913. printk(KERN_INFO "Adding %uk swap on %s. "
  1914. "Priority:%d extents:%d across:%lluk %s%s\n",
  1915. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1916. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  1917. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  1918. (p->flags & SWP_DISCARDABLE) ? "D" : "");
  1919. /* insert swap space into swap_list: */
  1920. prev = -1;
  1921. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1922. if (p->prio >= swap_info[i]->prio)
  1923. break;
  1924. prev = i;
  1925. }
  1926. p->next = i;
  1927. if (prev < 0)
  1928. swap_list.head = swap_list.next = p->type;
  1929. else
  1930. swap_info[prev]->next = p->type;
  1931. spin_unlock(&swap_lock);
  1932. mutex_unlock(&swapon_mutex);
  1933. atomic_inc(&proc_poll_event);
  1934. wake_up_interruptible(&proc_poll_wait);
  1935. if (S_ISREG(inode->i_mode))
  1936. inode->i_flags |= S_SWAPFILE;
  1937. error = 0;
  1938. goto out;
  1939. bad_swap:
  1940. if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
  1941. set_blocksize(p->bdev, p->old_block_size);
  1942. blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  1943. }
  1944. destroy_swap_extents(p);
  1945. swap_cgroup_swapoff(p->type);
  1946. spin_lock(&swap_lock);
  1947. p->swap_file = NULL;
  1948. p->flags = 0;
  1949. spin_unlock(&swap_lock);
  1950. vfree(swap_map);
  1951. if (swap_file) {
  1952. if (inode && S_ISREG(inode->i_mode))
  1953. mutex_unlock(&inode->i_mutex);
  1954. filp_close(swap_file, NULL);
  1955. }
  1956. out:
  1957. if (page && !IS_ERR(page)) {
  1958. kunmap(page);
  1959. page_cache_release(page);
  1960. }
  1961. if (name)
  1962. putname(name);
  1963. if (inode && S_ISREG(inode->i_mode))
  1964. mutex_unlock(&inode->i_mutex);
  1965. return error;
  1966. }
  1967. void si_swapinfo(struct sysinfo *val)
  1968. {
  1969. unsigned int type;
  1970. unsigned long nr_to_be_unused = 0;
  1971. spin_lock(&swap_lock);
  1972. for (type = 0; type < nr_swapfiles; type++) {
  1973. struct swap_info_struct *si = swap_info[type];
  1974. if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
  1975. nr_to_be_unused += si->inuse_pages;
  1976. }
  1977. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1978. val->totalswap = total_swap_pages + nr_to_be_unused;
  1979. spin_unlock(&swap_lock);
  1980. }
  1981. /*
  1982. * Verify that a swap entry is valid and increment its swap map count.
  1983. *
  1984. * Returns error code in following case.
  1985. * - success -> 0
  1986. * - swp_entry is invalid -> EINVAL
  1987. * - swp_entry is migration entry -> EINVAL
  1988. * - swap-cache reference is requested but there is already one. -> EEXIST
  1989. * - swap-cache reference is requested but the entry is not used. -> ENOENT
  1990. * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
  1991. */
  1992. static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  1993. {
  1994. struct swap_info_struct *p;
  1995. unsigned long offset, type;
  1996. unsigned char count;
  1997. unsigned char has_cache;
  1998. int err = -EINVAL;
  1999. if (non_swap_entry(entry))
  2000. goto out;
  2001. type = swp_type(entry);
  2002. if (type >= nr_swapfiles)
  2003. goto bad_file;
  2004. p = swap_info[type];
  2005. offset = swp_offset(entry);
  2006. spin_lock(&swap_lock);
  2007. if (unlikely(offset >= p->max))
  2008. goto unlock_out;
  2009. count = p->swap_map[offset];
  2010. has_cache = count & SWAP_HAS_CACHE;
  2011. count &= ~SWAP_HAS_CACHE;
  2012. err = 0;
  2013. if (usage == SWAP_HAS_CACHE) {
  2014. /* set SWAP_HAS_CACHE if there is no cache and entry is used */
  2015. if (!has_cache && count)
  2016. has_cache = SWAP_HAS_CACHE;
  2017. else if (has_cache) /* someone else added cache */
  2018. err = -EEXIST;
  2019. else /* no users remaining */
  2020. err = -ENOENT;
  2021. } else if (count || has_cache) {
  2022. if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
  2023. count += usage;
  2024. else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
  2025. err = -EINVAL;
  2026. else if (swap_count_continued(p, offset, count))
  2027. count = COUNT_CONTINUED;
  2028. else
  2029. err = -ENOMEM;
  2030. } else
  2031. err = -ENOENT; /* unused swap entry */
  2032. p->swap_map[offset] = count | has_cache;
  2033. unlock_out:
  2034. spin_unlock(&swap_lock);
  2035. out:
  2036. return err;
  2037. bad_file:
  2038. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  2039. goto out;
  2040. }
  2041. /*
  2042. * Help swapoff by noting that swap entry belongs to shmem/tmpfs
  2043. * (in which case its reference count is never incremented).
  2044. */
  2045. void swap_shmem_alloc(swp_entry_t entry)
  2046. {
  2047. __swap_duplicate(entry, SWAP_MAP_SHMEM);
  2048. }
  2049. /*
  2050. * Increase reference count of swap entry by 1.
  2051. * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
  2052. * but could not be atomically allocated. Returns 0, just as if it succeeded,
  2053. * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
  2054. * might occur if a page table entry has got corrupted.
  2055. */
  2056. int swap_duplicate(swp_entry_t entry)
  2057. {
  2058. int err = 0;
  2059. while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
  2060. err = add_swap_count_continuation(entry, GFP_ATOMIC);
  2061. return err;
  2062. }
  2063. /*
  2064. * @entry: swap entry for which we allocate swap cache.
  2065. *
  2066. * Called when allocating swap cache for existing swap entry,
  2067. * This can return error codes. Returns 0 at success.
  2068. * -EBUSY means there is a swap cache.
  2069. * Note: return code is different from swap_duplicate().
  2070. */
  2071. int swapcache_prepare(swp_entry_t entry)
  2072. {
  2073. return __swap_duplicate(entry, SWAP_HAS_CACHE);
  2074. }
  2075. /*
  2076. * swap_lock prevents swap_map being freed. Don't grab an extra
  2077. * reference on the swaphandle, it doesn't matter if it becomes unused.
  2078. */
  2079. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  2080. {
  2081. struct swap_info_struct *si;
  2082. int our_page_cluster = page_cluster;
  2083. pgoff_t target, toff;
  2084. pgoff_t base, end;
  2085. int nr_pages = 0;
  2086. if (!our_page_cluster) /* no readahead */
  2087. return 0;
  2088. si = swap_info[swp_type(entry)];
  2089. target = swp_offset(entry);
  2090. base = (target >> our_page_cluster) << our_page_cluster;
  2091. end = base + (1 << our_page_cluster);
  2092. if (!base) /* first page is swap header */
  2093. base++;
  2094. spin_lock(&swap_lock);
  2095. if (end > si->max) /* don't go beyond end of map */
  2096. end = si->max;
  2097. /* Count contiguous allocated slots above our target */
  2098. for (toff = target; ++toff < end; nr_pages++) {
  2099. /* Don't read in free or bad pages */
  2100. if (!si->swap_map[toff])
  2101. break;
  2102. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2103. break;
  2104. }
  2105. /* Count contiguous allocated slots below our target */
  2106. for (toff = target; --toff >= base; nr_pages++) {
  2107. /* Don't read in free or bad pages */
  2108. if (!si->swap_map[toff])
  2109. break;
  2110. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2111. break;
  2112. }
  2113. spin_unlock(&swap_lock);
  2114. /*
  2115. * Indicate starting offset, and return number of pages to get:
  2116. * if only 1, say 0, since there's then no readahead to be done.
  2117. */
  2118. *offset = ++toff;
  2119. return nr_pages? ++nr_pages: 0;
  2120. }
  2121. /*
  2122. * add_swap_count_continuation - called when a swap count is duplicated
  2123. * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  2124. * page of the original vmalloc'ed swap_map, to hold the continuation count
  2125. * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
  2126. * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
  2127. *
  2128. * These continuation pages are seldom referenced: the common paths all work
  2129. * on the original swap_map, only referring to a continuation page when the
  2130. * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
  2131. *
  2132. * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
  2133. * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
  2134. * can be called after dropping locks.
  2135. */
  2136. int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
  2137. {
  2138. struct swap_info_struct *si;
  2139. struct page *head;
  2140. struct page *page;
  2141. struct page *list_page;
  2142. pgoff_t offset;
  2143. unsigned char count;
  2144. /*
  2145. * When debugging, it's easier to use __GFP_ZERO here; but it's better
  2146. * for latency not to zero a page while GFP_ATOMIC and holding locks.
  2147. */
  2148. page = alloc_page(gfp_mask | __GFP_HIGHMEM);
  2149. si = swap_info_get(entry);
  2150. if (!si) {
  2151. /*
  2152. * An acceptable race has occurred since the failing
  2153. * __swap_duplicate(): the swap entry has been freed,
  2154. * perhaps even the whole swap_map cleared for swapoff.
  2155. */
  2156. goto outer;
  2157. }
  2158. offset = swp_offset(entry);
  2159. count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
  2160. if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
  2161. /*
  2162. * The higher the swap count, the more likely it is that tasks
  2163. * will race to add swap count continuation: we need to avoid
  2164. * over-provisioning.
  2165. */
  2166. goto out;
  2167. }
  2168. if (!page) {
  2169. spin_unlock(&swap_lock);
  2170. return -ENOMEM;
  2171. }
  2172. /*
  2173. * We are fortunate that although vmalloc_to_page uses pte_offset_map,
  2174. * no architecture is using highmem pages for kernel pagetables: so it
  2175. * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
  2176. */
  2177. head = vmalloc_to_page(si->swap_map + offset);
  2178. offset &= ~PAGE_MASK;
  2179. /*
  2180. * Page allocation does not initialize the page's lru field,
  2181. * but it does always reset its private field.
  2182. */
  2183. if (!page_private(head)) {
  2184. BUG_ON(count & COUNT_CONTINUED);
  2185. INIT_LIST_HEAD(&head->lru);
  2186. set_page_private(head, SWP_CONTINUED);
  2187. si->flags |= SWP_CONTINUED;
  2188. }
  2189. list_for_each_entry(list_page, &head->lru, lru) {
  2190. unsigned char *map;
  2191. /*
  2192. * If the previous map said no continuation, but we've found
  2193. * a continuation page, free our allocation and use this one.
  2194. */
  2195. if (!(count & COUNT_CONTINUED))
  2196. goto out;
  2197. map = kmap_atomic(list_page, KM_USER0) + offset;
  2198. count = *map;
  2199. kunmap_atomic(map, KM_USER0);
  2200. /*
  2201. * If this continuation count now has some space in it,
  2202. * free our allocation and use this one.
  2203. */
  2204. if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
  2205. goto out;
  2206. }
  2207. list_add_tail(&page->lru, &head->lru);
  2208. page = NULL; /* now it's attached, don't free it */
  2209. out:
  2210. spin_unlock(&swap_lock);
  2211. outer:
  2212. if (page)
  2213. __free_page(page);
  2214. return 0;
  2215. }
  2216. /*
  2217. * swap_count_continued - when the original swap_map count is incremented
  2218. * from SWAP_MAP_MAX, check if there is already a continuation page to carry
  2219. * into, carry if so, or else fail until a new continuation page is allocated;
  2220. * when the original swap_map count is decremented from 0 with continuation,
  2221. * borrow from the continuation and report whether it still holds more.
  2222. * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
  2223. */
  2224. static bool swap_count_continued(struct swap_info_struct *si,
  2225. pgoff_t offset, unsigned char count)
  2226. {
  2227. struct page *head;
  2228. struct page *page;
  2229. unsigned char *map;
  2230. head = vmalloc_to_page(si->swap_map + offset);
  2231. if (page_private(head) != SWP_CONTINUED) {
  2232. BUG_ON(count & COUNT_CONTINUED);
  2233. return false; /* need to add count continuation */
  2234. }
  2235. offset &= ~PAGE_MASK;
  2236. page = list_entry(head->lru.next, struct page, lru);
  2237. map = kmap_atomic(page, KM_USER0) + offset;
  2238. if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
  2239. goto init_map; /* jump over SWAP_CONT_MAX checks */
  2240. if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
  2241. /*
  2242. * Think of how you add 1 to 999
  2243. */
  2244. while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
  2245. kunmap_atomic(map, KM_USER0);
  2246. page = list_entry(page->lru.next, struct page, lru);
  2247. BUG_ON(page == head);
  2248. map = kmap_atomic(page, KM_USER0) + offset;
  2249. }
  2250. if (*map == SWAP_CONT_MAX) {
  2251. kunmap_atomic(map, KM_USER0);
  2252. page = list_entry(page->lru.next, struct page, lru);
  2253. if (page == head)
  2254. return false; /* add count continuation */
  2255. map = kmap_atomic(page, KM_USER0) + offset;
  2256. init_map: *map = 0; /* we didn't zero the page */
  2257. }
  2258. *map += 1;
  2259. kunmap_atomic(map, KM_USER0);
  2260. page = list_entry(page->lru.prev, struct page, lru);
  2261. while (page != head) {
  2262. map = kmap_atomic(page, KM_USER0) + offset;
  2263. *map = COUNT_CONTINUED;
  2264. kunmap_atomic(map, KM_USER0);
  2265. page = list_entry(page->lru.prev, struct page, lru);
  2266. }
  2267. return true; /* incremented */
  2268. } else { /* decrementing */
  2269. /*
  2270. * Think of how you subtract 1 from 1000
  2271. */
  2272. BUG_ON(count != COUNT_CONTINUED);
  2273. while (*map == COUNT_CONTINUED) {
  2274. kunmap_atomic(map, KM_USER0);
  2275. page = list_entry(page->lru.next, struct page, lru);
  2276. BUG_ON(page == head);
  2277. map = kmap_atomic(page, KM_USER0) + offset;
  2278. }
  2279. BUG_ON(*map == 0);
  2280. *map -= 1;
  2281. if (*map == 0)
  2282. count = 0;
  2283. kunmap_atomic(map, KM_USER0);
  2284. page = list_entry(page->lru.prev, struct page, lru);
  2285. while (page != head) {
  2286. map = kmap_atomic(page, KM_USER0) + offset;
  2287. *map = SWAP_CONT_MAX | count;
  2288. count = COUNT_CONTINUED;
  2289. kunmap_atomic(map, KM_USER0);
  2290. page = list_entry(page->lru.prev, struct page, lru);
  2291. }
  2292. return count == COUNT_CONTINUED;
  2293. }
  2294. }
  2295. /*
  2296. * free_swap_count_continuations - swapoff free all the continuation pages
  2297. * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
  2298. */
  2299. static void free_swap_count_continuations(struct swap_info_struct *si)
  2300. {
  2301. pgoff_t offset;
  2302. for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
  2303. struct page *head;
  2304. head = vmalloc_to_page(si->swap_map + offset);
  2305. if (page_private(head)) {
  2306. struct list_head *this, *next;
  2307. list_for_each_safe(this, next, &head->lru) {
  2308. struct page *page;
  2309. page = list_entry(this, struct page, lru);
  2310. list_del(this);
  2311. __free_page(page);
  2312. }
  2313. }
  2314. }
  2315. }