swapfile.c 65 KB

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