swapfile.c 65 KB

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