swapfile.c 65 KB

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