swapfile.c 65 KB

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