swapfile.c 66 KB

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