swapfile.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489
  1. /*
  2. * linux/mm/swapfile.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. * Swap reorganised 29.12.95, Stephen Tweedie
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/hugetlb.h>
  9. #include <linux/mman.h>
  10. #include <linux/slab.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/swap.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/namei.h>
  16. #include <linux/shm.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/random.h>
  19. #include <linux/writeback.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/ksm.h>
  25. #include <linux/rmap.h>
  26. #include <linux/security.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/mutex.h>
  29. #include <linux/capability.h>
  30. #include <linux/syscalls.h>
  31. #include <linux/memcontrol.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/tlbflush.h>
  34. #include <linux/swapops.h>
  35. #include <linux/page_cgroup.h>
  36. static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
  37. unsigned char);
  38. static void free_swap_count_continuations(struct swap_info_struct *);
  39. static sector_t map_swap_entry(swp_entry_t, struct block_device**);
  40. static DEFINE_SPINLOCK(swap_lock);
  41. static unsigned int nr_swapfiles;
  42. long nr_swap_pages;
  43. long total_swap_pages;
  44. static int least_priority;
  45. static const char Bad_file[] = "Bad swap file entry ";
  46. static const char Unused_file[] = "Unused swap file entry ";
  47. static const char Bad_offset[] = "Bad swap offset entry ";
  48. static const char Unused_offset[] = "Unused swap offset entry ";
  49. static struct swap_list_t swap_list = {-1, -1};
  50. static struct swap_info_struct *swap_info[MAX_SWAPFILES];
  51. static DEFINE_MUTEX(swapon_mutex);
  52. static inline unsigned char swap_count(unsigned char ent)
  53. {
  54. return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */
  55. }
  56. /* returns 1 if swap entry is freed */
  57. static int
  58. __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
  59. {
  60. swp_entry_t entry = swp_entry(si->type, offset);
  61. struct page *page;
  62. int ret = 0;
  63. page = find_get_page(&swapper_space, entry.val);
  64. if (!page)
  65. return 0;
  66. /*
  67. * This function is called from scan_swap_map() and it's called
  68. * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
  69. * We have to use trylock for avoiding deadlock. This is a special
  70. * case and you should use try_to_free_swap() with explicit lock_page()
  71. * in usual operations.
  72. */
  73. if (trylock_page(page)) {
  74. ret = try_to_free_swap(page);
  75. unlock_page(page);
  76. }
  77. page_cache_release(page);
  78. return ret;
  79. }
  80. /*
  81. * We need this because the bdev->unplug_fn can sleep and we cannot
  82. * hold swap_lock while calling the unplug_fn. And swap_lock
  83. * cannot be turned into a mutex.
  84. */
  85. static DECLARE_RWSEM(swap_unplug_sem);
  86. void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
  87. {
  88. swp_entry_t entry;
  89. down_read(&swap_unplug_sem);
  90. entry.val = page_private(page);
  91. if (PageSwapCache(page)) {
  92. struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
  93. struct backing_dev_info *bdi;
  94. /*
  95. * If the page is removed from swapcache from under us (with a
  96. * racy try_to_unuse/swapoff) we need an additional reference
  97. * count to avoid reading garbage from page_private(page) above.
  98. * If the WARN_ON triggers during a swapoff it maybe the race
  99. * condition and it's harmless. However if it triggers without
  100. * swapoff it signals a problem.
  101. */
  102. WARN_ON(page_count(page) <= 1);
  103. bdi = bdev->bd_inode->i_mapping->backing_dev_info;
  104. blk_run_backing_dev(bdi, page);
  105. }
  106. up_read(&swap_unplug_sem);
  107. }
  108. /*
  109. * swapon tell device that all the old swap contents can be discarded,
  110. * to allow the swap device to optimize its wear-levelling.
  111. */
  112. static int discard_swap(struct swap_info_struct *si)
  113. {
  114. struct swap_extent *se;
  115. sector_t start_block;
  116. sector_t nr_blocks;
  117. int err = 0;
  118. /* Do not discard the swap header page! */
  119. se = &si->first_swap_extent;
  120. start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
  121. nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
  122. if (nr_blocks) {
  123. err = blkdev_issue_discard(si->bdev, start_block,
  124. nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
  125. if (err)
  126. return err;
  127. cond_resched();
  128. }
  129. list_for_each_entry(se, &si->first_swap_extent.list, list) {
  130. start_block = se->start_block << (PAGE_SHIFT - 9);
  131. nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
  132. err = blkdev_issue_discard(si->bdev, start_block,
  133. nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
  134. if (err)
  135. break;
  136. cond_resched();
  137. }
  138. return err; /* That will often be -EOPNOTSUPP */
  139. }
  140. /*
  141. * swap allocation tell device that a cluster of swap can now be discarded,
  142. * to allow the swap device to optimize its wear-levelling.
  143. */
  144. static void discard_swap_cluster(struct swap_info_struct *si,
  145. pgoff_t start_page, pgoff_t nr_pages)
  146. {
  147. struct swap_extent *se = si->curr_swap_extent;
  148. int found_extent = 0;
  149. while (nr_pages) {
  150. struct list_head *lh;
  151. if (se->start_page <= start_page &&
  152. start_page < se->start_page + se->nr_pages) {
  153. pgoff_t offset = start_page - se->start_page;
  154. sector_t start_block = se->start_block + offset;
  155. sector_t nr_blocks = se->nr_pages - offset;
  156. if (nr_blocks > nr_pages)
  157. nr_blocks = nr_pages;
  158. start_page += nr_blocks;
  159. nr_pages -= nr_blocks;
  160. if (!found_extent++)
  161. si->curr_swap_extent = se;
  162. start_block <<= PAGE_SHIFT - 9;
  163. nr_blocks <<= PAGE_SHIFT - 9;
  164. if (blkdev_issue_discard(si->bdev, start_block,
  165. nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER))
  166. break;
  167. }
  168. lh = se->list.next;
  169. se = list_entry(lh, struct swap_extent, list);
  170. }
  171. }
  172. static int wait_for_discard(void *word)
  173. {
  174. schedule();
  175. return 0;
  176. }
  177. #define SWAPFILE_CLUSTER 256
  178. #define LATENCY_LIMIT 256
  179. static inline unsigned long scan_swap_map(struct swap_info_struct *si,
  180. unsigned char usage)
  181. {
  182. unsigned long offset;
  183. unsigned long scan_base;
  184. unsigned long last_in_cluster = 0;
  185. int latency_ration = LATENCY_LIMIT;
  186. int found_free_cluster = 0;
  187. /*
  188. * We try to cluster swap pages by allocating them sequentially
  189. * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
  190. * way, however, we resort to first-free allocation, starting
  191. * a new cluster. This prevents us from scattering swap pages
  192. * all over the entire swap partition, so that we reduce
  193. * overall disk seek times between swap pages. -- sct
  194. * But we do now try to find an empty cluster. -Andrea
  195. * And we let swap pages go all over an SSD partition. Hugh
  196. */
  197. si->flags += SWP_SCANNING;
  198. scan_base = offset = si->cluster_next;
  199. if (unlikely(!si->cluster_nr--)) {
  200. if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
  201. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  202. goto checks;
  203. }
  204. if (si->flags & SWP_DISCARDABLE) {
  205. /*
  206. * Start range check on racing allocations, in case
  207. * they overlap the cluster we eventually decide on
  208. * (we scan without swap_lock to allow preemption).
  209. * It's hardly conceivable that cluster_nr could be
  210. * wrapped during our scan, but don't depend on it.
  211. */
  212. if (si->lowest_alloc)
  213. goto checks;
  214. si->lowest_alloc = si->max;
  215. si->highest_alloc = 0;
  216. }
  217. spin_unlock(&swap_lock);
  218. /*
  219. * If seek is expensive, start searching for new cluster from
  220. * start of partition, to minimize the span of allocated swap.
  221. * But if seek is cheap, search from our current position, so
  222. * that swap is allocated from all over the partition: if the
  223. * Flash Translation Layer only remaps within limited zones,
  224. * we don't want to wear out the first zone too quickly.
  225. */
  226. if (!(si->flags & SWP_SOLIDSTATE))
  227. scan_base = offset = si->lowest_bit;
  228. last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
  229. /* Locate the first empty (unaligned) cluster */
  230. for (; last_in_cluster <= si->highest_bit; offset++) {
  231. if (si->swap_map[offset])
  232. last_in_cluster = offset + SWAPFILE_CLUSTER;
  233. else if (offset == last_in_cluster) {
  234. spin_lock(&swap_lock);
  235. offset -= SWAPFILE_CLUSTER - 1;
  236. si->cluster_next = offset;
  237. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  238. found_free_cluster = 1;
  239. goto checks;
  240. }
  241. if (unlikely(--latency_ration < 0)) {
  242. cond_resched();
  243. latency_ration = LATENCY_LIMIT;
  244. }
  245. }
  246. offset = si->lowest_bit;
  247. last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
  248. /* Locate the first empty (unaligned) cluster */
  249. for (; last_in_cluster < scan_base; offset++) {
  250. if (si->swap_map[offset])
  251. last_in_cluster = offset + SWAPFILE_CLUSTER;
  252. else if (offset == last_in_cluster) {
  253. spin_lock(&swap_lock);
  254. offset -= SWAPFILE_CLUSTER - 1;
  255. si->cluster_next = offset;
  256. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  257. found_free_cluster = 1;
  258. goto checks;
  259. }
  260. if (unlikely(--latency_ration < 0)) {
  261. cond_resched();
  262. latency_ration = LATENCY_LIMIT;
  263. }
  264. }
  265. offset = scan_base;
  266. spin_lock(&swap_lock);
  267. si->cluster_nr = SWAPFILE_CLUSTER - 1;
  268. si->lowest_alloc = 0;
  269. }
  270. checks:
  271. if (!(si->flags & SWP_WRITEOK))
  272. goto no_page;
  273. if (!si->highest_bit)
  274. goto no_page;
  275. if (offset > si->highest_bit)
  276. scan_base = offset = si->lowest_bit;
  277. /* reuse swap entry of cache-only swap if not busy. */
  278. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  279. int swap_was_freed;
  280. spin_unlock(&swap_lock);
  281. swap_was_freed = __try_to_reclaim_swap(si, offset);
  282. spin_lock(&swap_lock);
  283. /* entry was freed successfully, try to use this again */
  284. if (swap_was_freed)
  285. goto checks;
  286. goto scan; /* check next one */
  287. }
  288. if (si->swap_map[offset])
  289. goto scan;
  290. if (offset == si->lowest_bit)
  291. si->lowest_bit++;
  292. if (offset == si->highest_bit)
  293. si->highest_bit--;
  294. si->inuse_pages++;
  295. if (si->inuse_pages == si->pages) {
  296. si->lowest_bit = si->max;
  297. si->highest_bit = 0;
  298. }
  299. si->swap_map[offset] = usage;
  300. si->cluster_next = offset + 1;
  301. si->flags -= SWP_SCANNING;
  302. if (si->lowest_alloc) {
  303. /*
  304. * Only set when SWP_DISCARDABLE, and there's a scan
  305. * for a free cluster in progress or just completed.
  306. */
  307. if (found_free_cluster) {
  308. /*
  309. * To optimize wear-levelling, discard the
  310. * old data of the cluster, taking care not to
  311. * discard any of its pages that have already
  312. * been allocated by racing tasks (offset has
  313. * already stepped over any at the beginning).
  314. */
  315. if (offset < si->highest_alloc &&
  316. si->lowest_alloc <= last_in_cluster)
  317. last_in_cluster = si->lowest_alloc - 1;
  318. si->flags |= SWP_DISCARDING;
  319. spin_unlock(&swap_lock);
  320. if (offset < last_in_cluster)
  321. discard_swap_cluster(si, offset,
  322. last_in_cluster - offset + 1);
  323. spin_lock(&swap_lock);
  324. si->lowest_alloc = 0;
  325. si->flags &= ~SWP_DISCARDING;
  326. smp_mb(); /* wake_up_bit advises this */
  327. wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
  328. } else if (si->flags & SWP_DISCARDING) {
  329. /*
  330. * Delay using pages allocated by racing tasks
  331. * until the whole discard has been issued. We
  332. * could defer that delay until swap_writepage,
  333. * but it's easier to keep this self-contained.
  334. */
  335. spin_unlock(&swap_lock);
  336. wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
  337. wait_for_discard, TASK_UNINTERRUPTIBLE);
  338. spin_lock(&swap_lock);
  339. } else {
  340. /*
  341. * Note pages allocated by racing tasks while
  342. * scan for a free cluster is in progress, so
  343. * that its final discard can exclude them.
  344. */
  345. if (offset < si->lowest_alloc)
  346. si->lowest_alloc = offset;
  347. if (offset > si->highest_alloc)
  348. si->highest_alloc = offset;
  349. }
  350. }
  351. return offset;
  352. scan:
  353. spin_unlock(&swap_lock);
  354. while (++offset <= si->highest_bit) {
  355. if (!si->swap_map[offset]) {
  356. spin_lock(&swap_lock);
  357. goto checks;
  358. }
  359. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  360. spin_lock(&swap_lock);
  361. goto checks;
  362. }
  363. if (unlikely(--latency_ration < 0)) {
  364. cond_resched();
  365. latency_ration = LATENCY_LIMIT;
  366. }
  367. }
  368. offset = si->lowest_bit;
  369. while (++offset < scan_base) {
  370. if (!si->swap_map[offset]) {
  371. spin_lock(&swap_lock);
  372. goto checks;
  373. }
  374. if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
  375. spin_lock(&swap_lock);
  376. goto checks;
  377. }
  378. if (unlikely(--latency_ration < 0)) {
  379. cond_resched();
  380. latency_ration = LATENCY_LIMIT;
  381. }
  382. }
  383. spin_lock(&swap_lock);
  384. no_page:
  385. si->flags -= SWP_SCANNING;
  386. return 0;
  387. }
  388. swp_entry_t get_swap_page(void)
  389. {
  390. struct swap_info_struct *si;
  391. pgoff_t offset;
  392. int type, next;
  393. int wrapped = 0;
  394. spin_lock(&swap_lock);
  395. if (nr_swap_pages <= 0)
  396. goto noswap;
  397. nr_swap_pages--;
  398. for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
  399. si = swap_info[type];
  400. next = si->next;
  401. if (next < 0 ||
  402. (!wrapped && si->prio != swap_info[next]->prio)) {
  403. next = swap_list.head;
  404. wrapped++;
  405. }
  406. if (!si->highest_bit)
  407. continue;
  408. if (!(si->flags & SWP_WRITEOK))
  409. continue;
  410. swap_list.next = next;
  411. /* This is called for allocating swap entry for cache */
  412. offset = scan_swap_map(si, SWAP_HAS_CACHE);
  413. if (offset) {
  414. spin_unlock(&swap_lock);
  415. return swp_entry(type, offset);
  416. }
  417. next = swap_list.next;
  418. }
  419. nr_swap_pages++;
  420. noswap:
  421. spin_unlock(&swap_lock);
  422. return (swp_entry_t) {0};
  423. }
  424. /* The only caller of this function is now susupend routine */
  425. swp_entry_t get_swap_page_of_type(int type)
  426. {
  427. struct swap_info_struct *si;
  428. pgoff_t offset;
  429. spin_lock(&swap_lock);
  430. si = swap_info[type];
  431. if (si && (si->flags & SWP_WRITEOK)) {
  432. nr_swap_pages--;
  433. /* This is called for allocating swap entry, not cache */
  434. offset = scan_swap_map(si, 1);
  435. if (offset) {
  436. spin_unlock(&swap_lock);
  437. return swp_entry(type, offset);
  438. }
  439. nr_swap_pages++;
  440. }
  441. spin_unlock(&swap_lock);
  442. return (swp_entry_t) {0};
  443. }
  444. static struct swap_info_struct *swap_info_get(swp_entry_t entry)
  445. {
  446. struct swap_info_struct *p;
  447. unsigned long offset, type;
  448. if (!entry.val)
  449. goto out;
  450. type = swp_type(entry);
  451. if (type >= nr_swapfiles)
  452. goto bad_nofile;
  453. p = swap_info[type];
  454. if (!(p->flags & SWP_USED))
  455. goto bad_device;
  456. offset = swp_offset(entry);
  457. if (offset >= p->max)
  458. goto bad_offset;
  459. if (!p->swap_map[offset])
  460. goto bad_free;
  461. spin_lock(&swap_lock);
  462. return p;
  463. bad_free:
  464. printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
  465. goto out;
  466. bad_offset:
  467. printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
  468. goto out;
  469. bad_device:
  470. printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
  471. goto out;
  472. bad_nofile:
  473. printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
  474. out:
  475. return NULL;
  476. }
  477. static unsigned char swap_entry_free(struct swap_info_struct *p,
  478. swp_entry_t entry, unsigned char usage)
  479. {
  480. unsigned long offset = swp_offset(entry);
  481. unsigned char count;
  482. unsigned char has_cache;
  483. count = p->swap_map[offset];
  484. has_cache = count & SWAP_HAS_CACHE;
  485. count &= ~SWAP_HAS_CACHE;
  486. if (usage == SWAP_HAS_CACHE) {
  487. VM_BUG_ON(!has_cache);
  488. has_cache = 0;
  489. } else if (count == SWAP_MAP_SHMEM) {
  490. /*
  491. * Or we could insist on shmem.c using a special
  492. * swap_shmem_free() and free_shmem_swap_and_cache()...
  493. */
  494. count = 0;
  495. } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
  496. if (count == COUNT_CONTINUED) {
  497. if (swap_count_continued(p, offset, count))
  498. count = SWAP_MAP_MAX | COUNT_CONTINUED;
  499. else
  500. count = SWAP_MAP_MAX;
  501. } else
  502. count--;
  503. }
  504. if (!count)
  505. mem_cgroup_uncharge_swap(entry);
  506. usage = count | has_cache;
  507. p->swap_map[offset] = usage;
  508. /* free if no reference */
  509. if (!usage) {
  510. if (offset < p->lowest_bit)
  511. p->lowest_bit = offset;
  512. if (offset > p->highest_bit)
  513. p->highest_bit = offset;
  514. if (swap_list.next >= 0 &&
  515. p->prio > swap_info[swap_list.next]->prio)
  516. swap_list.next = p->type;
  517. nr_swap_pages++;
  518. p->inuse_pages--;
  519. }
  520. return usage;
  521. }
  522. /*
  523. * Caller has made sure that the swapdevice corresponding to entry
  524. * is still around or has not been recycled.
  525. */
  526. void swap_free(swp_entry_t entry)
  527. {
  528. struct swap_info_struct *p;
  529. p = swap_info_get(entry);
  530. if (p) {
  531. swap_entry_free(p, entry, 1);
  532. spin_unlock(&swap_lock);
  533. }
  534. }
  535. /*
  536. * Called after dropping swapcache to decrease refcnt to swap entries.
  537. */
  538. void swapcache_free(swp_entry_t entry, struct page *page)
  539. {
  540. struct swap_info_struct *p;
  541. unsigned char count;
  542. p = swap_info_get(entry);
  543. if (p) {
  544. count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
  545. if (page)
  546. mem_cgroup_uncharge_swapcache(page, entry, count != 0);
  547. spin_unlock(&swap_lock);
  548. }
  549. }
  550. /*
  551. * How many references to page are currently swapped out?
  552. * This does not give an exact answer when swap count is continued,
  553. * but does include the high COUNT_CONTINUED flag to allow for that.
  554. */
  555. static inline int page_swapcount(struct page *page)
  556. {
  557. int count = 0;
  558. struct swap_info_struct *p;
  559. swp_entry_t entry;
  560. entry.val = page_private(page);
  561. p = swap_info_get(entry);
  562. if (p) {
  563. count = swap_count(p->swap_map[swp_offset(entry)]);
  564. spin_unlock(&swap_lock);
  565. }
  566. return count;
  567. }
  568. /*
  569. * We can write to an anon page without COW if there are no other references
  570. * to it. And as a side-effect, free up its swap: because the old content
  571. * on disk will never be read, and seeking back there to write new content
  572. * later would only waste time away from clustering.
  573. */
  574. int reuse_swap_page(struct page *page)
  575. {
  576. int count;
  577. VM_BUG_ON(!PageLocked(page));
  578. if (unlikely(PageKsm(page)))
  579. return 0;
  580. count = page_mapcount(page);
  581. if (count <= 1 && PageSwapCache(page)) {
  582. count += page_swapcount(page);
  583. if (count == 1 && !PageWriteback(page)) {
  584. delete_from_swap_cache(page);
  585. SetPageDirty(page);
  586. }
  587. }
  588. return count <= 1;
  589. }
  590. /*
  591. * If swap is getting full, or if there are no more mappings of this page,
  592. * then try_to_free_swap is called to free its swap space.
  593. */
  594. int try_to_free_swap(struct page *page)
  595. {
  596. VM_BUG_ON(!PageLocked(page));
  597. if (!PageSwapCache(page))
  598. return 0;
  599. if (PageWriteback(page))
  600. return 0;
  601. if (page_swapcount(page))
  602. return 0;
  603. delete_from_swap_cache(page);
  604. SetPageDirty(page);
  605. return 1;
  606. }
  607. /*
  608. * Free the swap entry like above, but also try to
  609. * free the page cache entry if it is the last user.
  610. */
  611. int free_swap_and_cache(swp_entry_t entry)
  612. {
  613. struct swap_info_struct *p;
  614. struct page *page = NULL;
  615. if (non_swap_entry(entry))
  616. return 1;
  617. p = swap_info_get(entry);
  618. if (p) {
  619. if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
  620. page = find_get_page(&swapper_space, entry.val);
  621. if (page && !trylock_page(page)) {
  622. page_cache_release(page);
  623. page = NULL;
  624. }
  625. }
  626. spin_unlock(&swap_lock);
  627. }
  628. if (page) {
  629. /*
  630. * Not mapped elsewhere, or swap space full? Free it!
  631. * Also recheck PageSwapCache now page is locked (above).
  632. */
  633. if (PageSwapCache(page) && !PageWriteback(page) &&
  634. (!page_mapped(page) || vm_swap_full())) {
  635. delete_from_swap_cache(page);
  636. SetPageDirty(page);
  637. }
  638. unlock_page(page);
  639. page_cache_release(page);
  640. }
  641. return p != NULL;
  642. }
  643. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  644. /**
  645. * mem_cgroup_count_swap_user - count the user of a swap entry
  646. * @ent: the swap entry to be checked
  647. * @pagep: the pointer for the swap cache page of the entry to be stored
  648. *
  649. * Returns the number of the user of the swap entry. The number is valid only
  650. * for swaps of anonymous pages.
  651. * If the entry is found on swap cache, the page is stored to pagep with
  652. * refcount of it being incremented.
  653. */
  654. int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
  655. {
  656. struct page *page;
  657. struct swap_info_struct *p;
  658. int count = 0;
  659. page = find_get_page(&swapper_space, ent.val);
  660. if (page)
  661. count += page_mapcount(page);
  662. p = swap_info_get(ent);
  663. if (p) {
  664. count += swap_count(p->swap_map[swp_offset(ent)]);
  665. spin_unlock(&swap_lock);
  666. }
  667. *pagep = page;
  668. return count;
  669. }
  670. #endif
  671. #ifdef CONFIG_HIBERNATION
  672. /*
  673. * Find the swap type that corresponds to given device (if any).
  674. *
  675. * @offset - number of the PAGE_SIZE-sized block of the device, starting
  676. * from 0, in which the swap header is expected to be located.
  677. *
  678. * This is needed for the suspend to disk (aka swsusp).
  679. */
  680. int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
  681. {
  682. struct block_device *bdev = NULL;
  683. int type;
  684. if (device)
  685. bdev = bdget(device);
  686. spin_lock(&swap_lock);
  687. for (type = 0; type < nr_swapfiles; type++) {
  688. struct swap_info_struct *sis = swap_info[type];
  689. if (!(sis->flags & SWP_WRITEOK))
  690. continue;
  691. if (!bdev) {
  692. if (bdev_p)
  693. *bdev_p = bdgrab(sis->bdev);
  694. spin_unlock(&swap_lock);
  695. return type;
  696. }
  697. if (bdev == sis->bdev) {
  698. struct swap_extent *se = &sis->first_swap_extent;
  699. if (se->start_block == offset) {
  700. if (bdev_p)
  701. *bdev_p = bdgrab(sis->bdev);
  702. spin_unlock(&swap_lock);
  703. bdput(bdev);
  704. return type;
  705. }
  706. }
  707. }
  708. spin_unlock(&swap_lock);
  709. if (bdev)
  710. bdput(bdev);
  711. return -ENODEV;
  712. }
  713. /*
  714. * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
  715. * corresponding to given index in swap_info (swap type).
  716. */
  717. sector_t swapdev_block(int type, pgoff_t offset)
  718. {
  719. struct block_device *bdev;
  720. if ((unsigned int)type >= nr_swapfiles)
  721. return 0;
  722. if (!(swap_info[type]->flags & SWP_WRITEOK))
  723. return 0;
  724. return map_swap_entry(swp_entry(type, offset), &bdev);
  725. }
  726. /*
  727. * Return either the total number of swap pages of given type, or the number
  728. * of free pages of that type (depending on @free)
  729. *
  730. * This is needed for software suspend
  731. */
  732. unsigned int count_swap_pages(int type, int free)
  733. {
  734. unsigned int n = 0;
  735. spin_lock(&swap_lock);
  736. if ((unsigned int)type < nr_swapfiles) {
  737. struct swap_info_struct *sis = swap_info[type];
  738. if (sis->flags & SWP_WRITEOK) {
  739. n = sis->pages;
  740. if (free)
  741. n -= sis->inuse_pages;
  742. }
  743. }
  744. spin_unlock(&swap_lock);
  745. return n;
  746. }
  747. #endif /* CONFIG_HIBERNATION */
  748. /*
  749. * No need to decide whether this PTE shares the swap entry with others,
  750. * just let do_wp_page work it out if a write is requested later - to
  751. * force COW, vm_page_prot omits write permission from any private vma.
  752. */
  753. static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
  754. unsigned long addr, swp_entry_t entry, struct page *page)
  755. {
  756. struct mem_cgroup *ptr = NULL;
  757. spinlock_t *ptl;
  758. pte_t *pte;
  759. int ret = 1;
  760. if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
  761. ret = -ENOMEM;
  762. goto out_nolock;
  763. }
  764. pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
  765. if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
  766. if (ret > 0)
  767. mem_cgroup_cancel_charge_swapin(ptr);
  768. ret = 0;
  769. goto out;
  770. }
  771. dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
  772. inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
  773. get_page(page);
  774. set_pte_at(vma->vm_mm, addr, pte,
  775. pte_mkold(mk_pte(page, vma->vm_page_prot)));
  776. page_add_anon_rmap(page, vma, addr);
  777. mem_cgroup_commit_charge_swapin(page, ptr);
  778. swap_free(entry);
  779. /*
  780. * Move the page to the active list so it is not
  781. * immediately swapped out again after swapon.
  782. */
  783. activate_page(page);
  784. out:
  785. pte_unmap_unlock(pte, ptl);
  786. out_nolock:
  787. return ret;
  788. }
  789. static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
  790. unsigned long addr, unsigned long end,
  791. swp_entry_t entry, struct page *page)
  792. {
  793. pte_t swp_pte = swp_entry_to_pte(entry);
  794. pte_t *pte;
  795. int ret = 0;
  796. /*
  797. * We don't actually need pte lock while scanning for swp_pte: since
  798. * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
  799. * page table while we're scanning; though it could get zapped, and on
  800. * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
  801. * of unmatched parts which look like swp_pte, so unuse_pte must
  802. * recheck under pte lock. Scanning without pte lock lets it be
  803. * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
  804. */
  805. pte = pte_offset_map(pmd, addr);
  806. do {
  807. /*
  808. * swapoff spends a _lot_ of time in this loop!
  809. * Test inline before going to call unuse_pte.
  810. */
  811. if (unlikely(pte_same(*pte, swp_pte))) {
  812. pte_unmap(pte);
  813. ret = unuse_pte(vma, pmd, addr, entry, page);
  814. if (ret)
  815. goto out;
  816. pte = pte_offset_map(pmd, addr);
  817. }
  818. } while (pte++, addr += PAGE_SIZE, addr != end);
  819. pte_unmap(pte - 1);
  820. out:
  821. return ret;
  822. }
  823. static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
  824. unsigned long addr, unsigned long end,
  825. swp_entry_t entry, struct page *page)
  826. {
  827. pmd_t *pmd;
  828. unsigned long next;
  829. int ret;
  830. pmd = pmd_offset(pud, addr);
  831. do {
  832. next = pmd_addr_end(addr, end);
  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. SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
  1366. {
  1367. struct swap_info_struct *p = NULL;
  1368. unsigned char *swap_map;
  1369. struct file *swap_file, *victim;
  1370. struct address_space *mapping;
  1371. struct inode *inode;
  1372. char *pathname;
  1373. int i, type, prev;
  1374. int err;
  1375. if (!capable(CAP_SYS_ADMIN))
  1376. return -EPERM;
  1377. pathname = getname(specialfile);
  1378. err = PTR_ERR(pathname);
  1379. if (IS_ERR(pathname))
  1380. goto out;
  1381. victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
  1382. putname(pathname);
  1383. err = PTR_ERR(victim);
  1384. if (IS_ERR(victim))
  1385. goto out;
  1386. mapping = victim->f_mapping;
  1387. prev = -1;
  1388. spin_lock(&swap_lock);
  1389. for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
  1390. p = swap_info[type];
  1391. if (p->flags & SWP_WRITEOK) {
  1392. if (p->swap_file->f_mapping == mapping)
  1393. break;
  1394. }
  1395. prev = type;
  1396. }
  1397. if (type < 0) {
  1398. err = -EINVAL;
  1399. spin_unlock(&swap_lock);
  1400. goto out_dput;
  1401. }
  1402. if (!security_vm_enough_memory(p->pages))
  1403. vm_unacct_memory(p->pages);
  1404. else {
  1405. err = -ENOMEM;
  1406. spin_unlock(&swap_lock);
  1407. goto out_dput;
  1408. }
  1409. if (prev < 0)
  1410. swap_list.head = p->next;
  1411. else
  1412. swap_info[prev]->next = p->next;
  1413. if (type == swap_list.next) {
  1414. /* just pick something that's safe... */
  1415. swap_list.next = swap_list.head;
  1416. }
  1417. if (p->prio < 0) {
  1418. for (i = p->next; i >= 0; i = swap_info[i]->next)
  1419. swap_info[i]->prio = p->prio--;
  1420. least_priority++;
  1421. }
  1422. nr_swap_pages -= p->pages;
  1423. total_swap_pages -= p->pages;
  1424. p->flags &= ~SWP_WRITEOK;
  1425. spin_unlock(&swap_lock);
  1426. current->flags |= PF_OOM_ORIGIN;
  1427. err = try_to_unuse(type);
  1428. current->flags &= ~PF_OOM_ORIGIN;
  1429. if (err) {
  1430. /* re-insert swap space back into swap_list */
  1431. spin_lock(&swap_lock);
  1432. if (p->prio < 0)
  1433. p->prio = --least_priority;
  1434. prev = -1;
  1435. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1436. if (p->prio >= swap_info[i]->prio)
  1437. break;
  1438. prev = i;
  1439. }
  1440. p->next = i;
  1441. if (prev < 0)
  1442. swap_list.head = swap_list.next = type;
  1443. else
  1444. swap_info[prev]->next = type;
  1445. nr_swap_pages += p->pages;
  1446. total_swap_pages += p->pages;
  1447. p->flags |= SWP_WRITEOK;
  1448. spin_unlock(&swap_lock);
  1449. goto out_dput;
  1450. }
  1451. /* wait for any unplug function to finish */
  1452. down_write(&swap_unplug_sem);
  1453. up_write(&swap_unplug_sem);
  1454. destroy_swap_extents(p);
  1455. if (p->flags & SWP_CONTINUED)
  1456. free_swap_count_continuations(p);
  1457. mutex_lock(&swapon_mutex);
  1458. spin_lock(&swap_lock);
  1459. drain_mmlist();
  1460. /* wait for anyone still in scan_swap_map */
  1461. p->highest_bit = 0; /* cuts scans short */
  1462. while (p->flags >= SWP_SCANNING) {
  1463. spin_unlock(&swap_lock);
  1464. schedule_timeout_uninterruptible(1);
  1465. spin_lock(&swap_lock);
  1466. }
  1467. swap_file = p->swap_file;
  1468. p->swap_file = NULL;
  1469. p->max = 0;
  1470. swap_map = p->swap_map;
  1471. p->swap_map = NULL;
  1472. p->flags = 0;
  1473. spin_unlock(&swap_lock);
  1474. mutex_unlock(&swapon_mutex);
  1475. vfree(swap_map);
  1476. /* Destroy swap account informatin */
  1477. swap_cgroup_swapoff(type);
  1478. inode = mapping->host;
  1479. if (S_ISBLK(inode->i_mode)) {
  1480. struct block_device *bdev = I_BDEV(inode);
  1481. set_blocksize(bdev, p->old_block_size);
  1482. bd_release(bdev);
  1483. } else {
  1484. mutex_lock(&inode->i_mutex);
  1485. inode->i_flags &= ~S_SWAPFILE;
  1486. mutex_unlock(&inode->i_mutex);
  1487. }
  1488. filp_close(swap_file, NULL);
  1489. err = 0;
  1490. out_dput:
  1491. filp_close(victim, NULL);
  1492. out:
  1493. return err;
  1494. }
  1495. #ifdef CONFIG_PROC_FS
  1496. /* iterator */
  1497. static void *swap_start(struct seq_file *swap, loff_t *pos)
  1498. {
  1499. struct swap_info_struct *si;
  1500. int type;
  1501. loff_t l = *pos;
  1502. mutex_lock(&swapon_mutex);
  1503. if (!l)
  1504. return SEQ_START_TOKEN;
  1505. for (type = 0; type < nr_swapfiles; type++) {
  1506. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1507. si = swap_info[type];
  1508. if (!(si->flags & SWP_USED) || !si->swap_map)
  1509. continue;
  1510. if (!--l)
  1511. return si;
  1512. }
  1513. return NULL;
  1514. }
  1515. static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
  1516. {
  1517. struct swap_info_struct *si = v;
  1518. int type;
  1519. if (v == SEQ_START_TOKEN)
  1520. type = 0;
  1521. else
  1522. type = si->type + 1;
  1523. for (; type < nr_swapfiles; type++) {
  1524. smp_rmb(); /* read nr_swapfiles before swap_info[type] */
  1525. si = swap_info[type];
  1526. if (!(si->flags & SWP_USED) || !si->swap_map)
  1527. continue;
  1528. ++*pos;
  1529. return si;
  1530. }
  1531. return NULL;
  1532. }
  1533. static void swap_stop(struct seq_file *swap, void *v)
  1534. {
  1535. mutex_unlock(&swapon_mutex);
  1536. }
  1537. static int swap_show(struct seq_file *swap, void *v)
  1538. {
  1539. struct swap_info_struct *si = v;
  1540. struct file *file;
  1541. int len;
  1542. if (si == SEQ_START_TOKEN) {
  1543. seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
  1544. return 0;
  1545. }
  1546. file = si->swap_file;
  1547. len = seq_path(swap, &file->f_path, " \t\n\\");
  1548. seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
  1549. len < 40 ? 40 - len : 1, " ",
  1550. S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
  1551. "partition" : "file\t",
  1552. si->pages << (PAGE_SHIFT - 10),
  1553. si->inuse_pages << (PAGE_SHIFT - 10),
  1554. si->prio);
  1555. return 0;
  1556. }
  1557. static const struct seq_operations swaps_op = {
  1558. .start = swap_start,
  1559. .next = swap_next,
  1560. .stop = swap_stop,
  1561. .show = swap_show
  1562. };
  1563. static int swaps_open(struct inode *inode, struct file *file)
  1564. {
  1565. return seq_open(file, &swaps_op);
  1566. }
  1567. static const struct file_operations proc_swaps_operations = {
  1568. .open = swaps_open,
  1569. .read = seq_read,
  1570. .llseek = seq_lseek,
  1571. .release = seq_release,
  1572. };
  1573. static int __init procswaps_init(void)
  1574. {
  1575. proc_create("swaps", 0, NULL, &proc_swaps_operations);
  1576. return 0;
  1577. }
  1578. __initcall(procswaps_init);
  1579. #endif /* CONFIG_PROC_FS */
  1580. #ifdef MAX_SWAPFILES_CHECK
  1581. static int __init max_swapfiles_check(void)
  1582. {
  1583. MAX_SWAPFILES_CHECK();
  1584. return 0;
  1585. }
  1586. late_initcall(max_swapfiles_check);
  1587. #endif
  1588. /*
  1589. * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
  1590. *
  1591. * The swapon system call
  1592. */
  1593. SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
  1594. {
  1595. struct swap_info_struct *p;
  1596. char *name = NULL;
  1597. struct block_device *bdev = NULL;
  1598. struct file *swap_file = NULL;
  1599. struct address_space *mapping;
  1600. unsigned int type;
  1601. int i, prev;
  1602. int error;
  1603. union swap_header *swap_header;
  1604. unsigned int nr_good_pages;
  1605. int nr_extents = 0;
  1606. sector_t span;
  1607. unsigned long maxpages;
  1608. unsigned long swapfilepages;
  1609. unsigned char *swap_map = NULL;
  1610. struct page *page = NULL;
  1611. struct inode *inode = NULL;
  1612. int did_down = 0;
  1613. if (!capable(CAP_SYS_ADMIN))
  1614. return -EPERM;
  1615. p = kzalloc(sizeof(*p), GFP_KERNEL);
  1616. if (!p)
  1617. return -ENOMEM;
  1618. spin_lock(&swap_lock);
  1619. for (type = 0; type < nr_swapfiles; type++) {
  1620. if (!(swap_info[type]->flags & SWP_USED))
  1621. break;
  1622. }
  1623. error = -EPERM;
  1624. if (type >= MAX_SWAPFILES) {
  1625. spin_unlock(&swap_lock);
  1626. kfree(p);
  1627. goto out;
  1628. }
  1629. if (type >= nr_swapfiles) {
  1630. p->type = type;
  1631. swap_info[type] = p;
  1632. /*
  1633. * Write swap_info[type] before nr_swapfiles, in case a
  1634. * racing procfs swap_start() or swap_next() is reading them.
  1635. * (We never shrink nr_swapfiles, we never free this entry.)
  1636. */
  1637. smp_wmb();
  1638. nr_swapfiles++;
  1639. } else {
  1640. kfree(p);
  1641. p = swap_info[type];
  1642. /*
  1643. * Do not memset this entry: a racing procfs swap_next()
  1644. * would be relying on p->type to remain valid.
  1645. */
  1646. }
  1647. INIT_LIST_HEAD(&p->first_swap_extent.list);
  1648. p->flags = SWP_USED;
  1649. p->next = -1;
  1650. spin_unlock(&swap_lock);
  1651. name = getname(specialfile);
  1652. error = PTR_ERR(name);
  1653. if (IS_ERR(name)) {
  1654. name = NULL;
  1655. goto bad_swap_2;
  1656. }
  1657. swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
  1658. error = PTR_ERR(swap_file);
  1659. if (IS_ERR(swap_file)) {
  1660. swap_file = NULL;
  1661. goto bad_swap_2;
  1662. }
  1663. p->swap_file = swap_file;
  1664. mapping = swap_file->f_mapping;
  1665. inode = mapping->host;
  1666. error = -EBUSY;
  1667. for (i = 0; i < nr_swapfiles; i++) {
  1668. struct swap_info_struct *q = swap_info[i];
  1669. if (i == type || !q->swap_file)
  1670. continue;
  1671. if (mapping == q->swap_file->f_mapping)
  1672. goto bad_swap;
  1673. }
  1674. error = -EINVAL;
  1675. if (S_ISBLK(inode->i_mode)) {
  1676. bdev = I_BDEV(inode);
  1677. error = bd_claim(bdev, sys_swapon);
  1678. if (error < 0) {
  1679. bdev = NULL;
  1680. error = -EINVAL;
  1681. goto bad_swap;
  1682. }
  1683. p->old_block_size = block_size(bdev);
  1684. error = set_blocksize(bdev, PAGE_SIZE);
  1685. if (error < 0)
  1686. goto bad_swap;
  1687. p->bdev = bdev;
  1688. } else if (S_ISREG(inode->i_mode)) {
  1689. p->bdev = inode->i_sb->s_bdev;
  1690. mutex_lock(&inode->i_mutex);
  1691. did_down = 1;
  1692. if (IS_SWAPFILE(inode)) {
  1693. error = -EBUSY;
  1694. goto bad_swap;
  1695. }
  1696. } else {
  1697. goto bad_swap;
  1698. }
  1699. swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
  1700. /*
  1701. * Read the swap header.
  1702. */
  1703. if (!mapping->a_ops->readpage) {
  1704. error = -EINVAL;
  1705. goto bad_swap;
  1706. }
  1707. page = read_mapping_page(mapping, 0, swap_file);
  1708. if (IS_ERR(page)) {
  1709. error = PTR_ERR(page);
  1710. goto bad_swap;
  1711. }
  1712. swap_header = kmap(page);
  1713. if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
  1714. printk(KERN_ERR "Unable to find swap-space signature\n");
  1715. error = -EINVAL;
  1716. goto bad_swap;
  1717. }
  1718. /* swap partition endianess hack... */
  1719. if (swab32(swap_header->info.version) == 1) {
  1720. swab32s(&swap_header->info.version);
  1721. swab32s(&swap_header->info.last_page);
  1722. swab32s(&swap_header->info.nr_badpages);
  1723. for (i = 0; i < swap_header->info.nr_badpages; i++)
  1724. swab32s(&swap_header->info.badpages[i]);
  1725. }
  1726. /* Check the swap header's sub-version */
  1727. if (swap_header->info.version != 1) {
  1728. printk(KERN_WARNING
  1729. "Unable to handle swap header version %d\n",
  1730. swap_header->info.version);
  1731. error = -EINVAL;
  1732. goto bad_swap;
  1733. }
  1734. p->lowest_bit = 1;
  1735. p->cluster_next = 1;
  1736. p->cluster_nr = 0;
  1737. /*
  1738. * Find out how many pages are allowed for a single swap
  1739. * device. There are two limiting factors: 1) the number of
  1740. * bits for the swap offset in the swp_entry_t type and
  1741. * 2) the number of bits in the a swap pte as defined by
  1742. * the different architectures. In order to find the
  1743. * largest possible bit mask a swap entry with swap type 0
  1744. * and swap offset ~0UL is created, encoded to a swap pte,
  1745. * decoded to a swp_entry_t again and finally the swap
  1746. * offset is extracted. This will mask all the bits from
  1747. * the initial ~0UL mask that can't be encoded in either
  1748. * the swp_entry_t or the architecture definition of a
  1749. * swap pte.
  1750. */
  1751. maxpages = swp_offset(pte_to_swp_entry(
  1752. swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
  1753. if (maxpages > swap_header->info.last_page) {
  1754. maxpages = swap_header->info.last_page + 1;
  1755. /* p->max is an unsigned int: don't overflow it */
  1756. if ((unsigned int)maxpages == 0)
  1757. maxpages = UINT_MAX;
  1758. }
  1759. p->highest_bit = maxpages - 1;
  1760. error = -EINVAL;
  1761. if (!maxpages)
  1762. goto bad_swap;
  1763. if (swapfilepages && maxpages > swapfilepages) {
  1764. printk(KERN_WARNING
  1765. "Swap area shorter than signature indicates\n");
  1766. goto bad_swap;
  1767. }
  1768. if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
  1769. goto bad_swap;
  1770. if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
  1771. goto bad_swap;
  1772. /* OK, set up the swap map and apply the bad block list */
  1773. swap_map = vmalloc(maxpages);
  1774. if (!swap_map) {
  1775. error = -ENOMEM;
  1776. goto bad_swap;
  1777. }
  1778. memset(swap_map, 0, maxpages);
  1779. nr_good_pages = maxpages - 1; /* omit header page */
  1780. for (i = 0; i < swap_header->info.nr_badpages; i++) {
  1781. unsigned int page_nr = swap_header->info.badpages[i];
  1782. if (page_nr == 0 || page_nr > swap_header->info.last_page) {
  1783. error = -EINVAL;
  1784. goto bad_swap;
  1785. }
  1786. if (page_nr < maxpages) {
  1787. swap_map[page_nr] = SWAP_MAP_BAD;
  1788. nr_good_pages--;
  1789. }
  1790. }
  1791. error = swap_cgroup_swapon(type, maxpages);
  1792. if (error)
  1793. goto bad_swap;
  1794. if (nr_good_pages) {
  1795. swap_map[0] = SWAP_MAP_BAD;
  1796. p->max = maxpages;
  1797. p->pages = nr_good_pages;
  1798. nr_extents = setup_swap_extents(p, &span);
  1799. if (nr_extents < 0) {
  1800. error = nr_extents;
  1801. goto bad_swap;
  1802. }
  1803. nr_good_pages = p->pages;
  1804. }
  1805. if (!nr_good_pages) {
  1806. printk(KERN_WARNING "Empty swap-file\n");
  1807. error = -EINVAL;
  1808. goto bad_swap;
  1809. }
  1810. if (p->bdev) {
  1811. if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
  1812. p->flags |= SWP_SOLIDSTATE;
  1813. p->cluster_next = 1 + (random32() % p->highest_bit);
  1814. }
  1815. if (discard_swap(p) == 0)
  1816. p->flags |= SWP_DISCARDABLE;
  1817. }
  1818. mutex_lock(&swapon_mutex);
  1819. spin_lock(&swap_lock);
  1820. if (swap_flags & SWAP_FLAG_PREFER)
  1821. p->prio =
  1822. (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
  1823. else
  1824. p->prio = --least_priority;
  1825. p->swap_map = swap_map;
  1826. p->flags |= SWP_WRITEOK;
  1827. nr_swap_pages += nr_good_pages;
  1828. total_swap_pages += nr_good_pages;
  1829. printk(KERN_INFO "Adding %uk swap on %s. "
  1830. "Priority:%d extents:%d across:%lluk %s%s\n",
  1831. nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
  1832. nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
  1833. (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
  1834. (p->flags & SWP_DISCARDABLE) ? "D" : "");
  1835. /* insert swap space into swap_list: */
  1836. prev = -1;
  1837. for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
  1838. if (p->prio >= swap_info[i]->prio)
  1839. break;
  1840. prev = i;
  1841. }
  1842. p->next = i;
  1843. if (prev < 0)
  1844. swap_list.head = swap_list.next = type;
  1845. else
  1846. swap_info[prev]->next = type;
  1847. spin_unlock(&swap_lock);
  1848. mutex_unlock(&swapon_mutex);
  1849. error = 0;
  1850. goto out;
  1851. bad_swap:
  1852. if (bdev) {
  1853. set_blocksize(bdev, p->old_block_size);
  1854. bd_release(bdev);
  1855. }
  1856. destroy_swap_extents(p);
  1857. swap_cgroup_swapoff(type);
  1858. bad_swap_2:
  1859. spin_lock(&swap_lock);
  1860. p->swap_file = NULL;
  1861. p->flags = 0;
  1862. spin_unlock(&swap_lock);
  1863. vfree(swap_map);
  1864. if (swap_file)
  1865. filp_close(swap_file, NULL);
  1866. out:
  1867. if (page && !IS_ERR(page)) {
  1868. kunmap(page);
  1869. page_cache_release(page);
  1870. }
  1871. if (name)
  1872. putname(name);
  1873. if (did_down) {
  1874. if (!error)
  1875. inode->i_flags |= S_SWAPFILE;
  1876. mutex_unlock(&inode->i_mutex);
  1877. }
  1878. return error;
  1879. }
  1880. void si_swapinfo(struct sysinfo *val)
  1881. {
  1882. unsigned int type;
  1883. unsigned long nr_to_be_unused = 0;
  1884. spin_lock(&swap_lock);
  1885. for (type = 0; type < nr_swapfiles; type++) {
  1886. struct swap_info_struct *si = swap_info[type];
  1887. if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
  1888. nr_to_be_unused += si->inuse_pages;
  1889. }
  1890. val->freeswap = nr_swap_pages + nr_to_be_unused;
  1891. val->totalswap = total_swap_pages + nr_to_be_unused;
  1892. spin_unlock(&swap_lock);
  1893. }
  1894. /*
  1895. * Verify that a swap entry is valid and increment its swap map count.
  1896. *
  1897. * Returns error code in following case.
  1898. * - success -> 0
  1899. * - swp_entry is invalid -> EINVAL
  1900. * - swp_entry is migration entry -> EINVAL
  1901. * - swap-cache reference is requested but there is already one. -> EEXIST
  1902. * - swap-cache reference is requested but the entry is not used. -> ENOENT
  1903. * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
  1904. */
  1905. static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
  1906. {
  1907. struct swap_info_struct *p;
  1908. unsigned long offset, type;
  1909. unsigned char count;
  1910. unsigned char has_cache;
  1911. int err = -EINVAL;
  1912. if (non_swap_entry(entry))
  1913. goto out;
  1914. type = swp_type(entry);
  1915. if (type >= nr_swapfiles)
  1916. goto bad_file;
  1917. p = swap_info[type];
  1918. offset = swp_offset(entry);
  1919. spin_lock(&swap_lock);
  1920. if (unlikely(offset >= p->max))
  1921. goto unlock_out;
  1922. count = p->swap_map[offset];
  1923. has_cache = count & SWAP_HAS_CACHE;
  1924. count &= ~SWAP_HAS_CACHE;
  1925. err = 0;
  1926. if (usage == SWAP_HAS_CACHE) {
  1927. /* set SWAP_HAS_CACHE if there is no cache and entry is used */
  1928. if (!has_cache && count)
  1929. has_cache = SWAP_HAS_CACHE;
  1930. else if (has_cache) /* someone else added cache */
  1931. err = -EEXIST;
  1932. else /* no users remaining */
  1933. err = -ENOENT;
  1934. } else if (count || has_cache) {
  1935. if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
  1936. count += usage;
  1937. else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
  1938. err = -EINVAL;
  1939. else if (swap_count_continued(p, offset, count))
  1940. count = COUNT_CONTINUED;
  1941. else
  1942. err = -ENOMEM;
  1943. } else
  1944. err = -ENOENT; /* unused swap entry */
  1945. p->swap_map[offset] = count | has_cache;
  1946. unlock_out:
  1947. spin_unlock(&swap_lock);
  1948. out:
  1949. return err;
  1950. bad_file:
  1951. printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
  1952. goto out;
  1953. }
  1954. /*
  1955. * Help swapoff by noting that swap entry belongs to shmem/tmpfs
  1956. * (in which case its reference count is never incremented).
  1957. */
  1958. void swap_shmem_alloc(swp_entry_t entry)
  1959. {
  1960. __swap_duplicate(entry, SWAP_MAP_SHMEM);
  1961. }
  1962. /*
  1963. * Increase reference count of swap entry by 1.
  1964. * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
  1965. * but could not be atomically allocated. Returns 0, just as if it succeeded,
  1966. * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
  1967. * might occur if a page table entry has got corrupted.
  1968. */
  1969. int swap_duplicate(swp_entry_t entry)
  1970. {
  1971. int err = 0;
  1972. while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
  1973. err = add_swap_count_continuation(entry, GFP_ATOMIC);
  1974. return err;
  1975. }
  1976. /*
  1977. * @entry: swap entry for which we allocate swap cache.
  1978. *
  1979. * Called when allocating swap cache for existing swap entry,
  1980. * This can return error codes. Returns 0 at success.
  1981. * -EBUSY means there is a swap cache.
  1982. * Note: return code is different from swap_duplicate().
  1983. */
  1984. int swapcache_prepare(swp_entry_t entry)
  1985. {
  1986. return __swap_duplicate(entry, SWAP_HAS_CACHE);
  1987. }
  1988. /*
  1989. * swap_lock prevents swap_map being freed. Don't grab an extra
  1990. * reference on the swaphandle, it doesn't matter if it becomes unused.
  1991. */
  1992. int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
  1993. {
  1994. struct swap_info_struct *si;
  1995. int our_page_cluster = page_cluster;
  1996. pgoff_t target, toff;
  1997. pgoff_t base, end;
  1998. int nr_pages = 0;
  1999. if (!our_page_cluster) /* no readahead */
  2000. return 0;
  2001. si = swap_info[swp_type(entry)];
  2002. target = swp_offset(entry);
  2003. base = (target >> our_page_cluster) << our_page_cluster;
  2004. end = base + (1 << our_page_cluster);
  2005. if (!base) /* first page is swap header */
  2006. base++;
  2007. spin_lock(&swap_lock);
  2008. if (end > si->max) /* don't go beyond end of map */
  2009. end = si->max;
  2010. /* Count contiguous allocated slots above our target */
  2011. for (toff = target; ++toff < end; nr_pages++) {
  2012. /* Don't read in free or bad pages */
  2013. if (!si->swap_map[toff])
  2014. break;
  2015. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2016. break;
  2017. }
  2018. /* Count contiguous allocated slots below our target */
  2019. for (toff = target; --toff >= base; nr_pages++) {
  2020. /* Don't read in free or bad pages */
  2021. if (!si->swap_map[toff])
  2022. break;
  2023. if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
  2024. break;
  2025. }
  2026. spin_unlock(&swap_lock);
  2027. /*
  2028. * Indicate starting offset, and return number of pages to get:
  2029. * if only 1, say 0, since there's then no readahead to be done.
  2030. */
  2031. *offset = ++toff;
  2032. return nr_pages? ++nr_pages: 0;
  2033. }
  2034. /*
  2035. * add_swap_count_continuation - called when a swap count is duplicated
  2036. * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
  2037. * page of the original vmalloc'ed swap_map, to hold the continuation count
  2038. * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
  2039. * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
  2040. *
  2041. * These continuation pages are seldom referenced: the common paths all work
  2042. * on the original swap_map, only referring to a continuation page when the
  2043. * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
  2044. *
  2045. * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
  2046. * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
  2047. * can be called after dropping locks.
  2048. */
  2049. int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
  2050. {
  2051. struct swap_info_struct *si;
  2052. struct page *head;
  2053. struct page *page;
  2054. struct page *list_page;
  2055. pgoff_t offset;
  2056. unsigned char count;
  2057. /*
  2058. * When debugging, it's easier to use __GFP_ZERO here; but it's better
  2059. * for latency not to zero a page while GFP_ATOMIC and holding locks.
  2060. */
  2061. page = alloc_page(gfp_mask | __GFP_HIGHMEM);
  2062. si = swap_info_get(entry);
  2063. if (!si) {
  2064. /*
  2065. * An acceptable race has occurred since the failing
  2066. * __swap_duplicate(): the swap entry has been freed,
  2067. * perhaps even the whole swap_map cleared for swapoff.
  2068. */
  2069. goto outer;
  2070. }
  2071. offset = swp_offset(entry);
  2072. count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
  2073. if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
  2074. /*
  2075. * The higher the swap count, the more likely it is that tasks
  2076. * will race to add swap count continuation: we need to avoid
  2077. * over-provisioning.
  2078. */
  2079. goto out;
  2080. }
  2081. if (!page) {
  2082. spin_unlock(&swap_lock);
  2083. return -ENOMEM;
  2084. }
  2085. /*
  2086. * We are fortunate that although vmalloc_to_page uses pte_offset_map,
  2087. * no architecture is using highmem pages for kernel pagetables: so it
  2088. * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
  2089. */
  2090. head = vmalloc_to_page(si->swap_map + offset);
  2091. offset &= ~PAGE_MASK;
  2092. /*
  2093. * Page allocation does not initialize the page's lru field,
  2094. * but it does always reset its private field.
  2095. */
  2096. if (!page_private(head)) {
  2097. BUG_ON(count & COUNT_CONTINUED);
  2098. INIT_LIST_HEAD(&head->lru);
  2099. set_page_private(head, SWP_CONTINUED);
  2100. si->flags |= SWP_CONTINUED;
  2101. }
  2102. list_for_each_entry(list_page, &head->lru, lru) {
  2103. unsigned char *map;
  2104. /*
  2105. * If the previous map said no continuation, but we've found
  2106. * a continuation page, free our allocation and use this one.
  2107. */
  2108. if (!(count & COUNT_CONTINUED))
  2109. goto out;
  2110. map = kmap_atomic(list_page, KM_USER0) + offset;
  2111. count = *map;
  2112. kunmap_atomic(map, KM_USER0);
  2113. /*
  2114. * If this continuation count now has some space in it,
  2115. * free our allocation and use this one.
  2116. */
  2117. if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
  2118. goto out;
  2119. }
  2120. list_add_tail(&page->lru, &head->lru);
  2121. page = NULL; /* now it's attached, don't free it */
  2122. out:
  2123. spin_unlock(&swap_lock);
  2124. outer:
  2125. if (page)
  2126. __free_page(page);
  2127. return 0;
  2128. }
  2129. /*
  2130. * swap_count_continued - when the original swap_map count is incremented
  2131. * from SWAP_MAP_MAX, check if there is already a continuation page to carry
  2132. * into, carry if so, or else fail until a new continuation page is allocated;
  2133. * when the original swap_map count is decremented from 0 with continuation,
  2134. * borrow from the continuation and report whether it still holds more.
  2135. * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
  2136. */
  2137. static bool swap_count_continued(struct swap_info_struct *si,
  2138. pgoff_t offset, unsigned char count)
  2139. {
  2140. struct page *head;
  2141. struct page *page;
  2142. unsigned char *map;
  2143. head = vmalloc_to_page(si->swap_map + offset);
  2144. if (page_private(head) != SWP_CONTINUED) {
  2145. BUG_ON(count & COUNT_CONTINUED);
  2146. return false; /* need to add count continuation */
  2147. }
  2148. offset &= ~PAGE_MASK;
  2149. page = list_entry(head->lru.next, struct page, lru);
  2150. map = kmap_atomic(page, KM_USER0) + offset;
  2151. if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
  2152. goto init_map; /* jump over SWAP_CONT_MAX checks */
  2153. if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
  2154. /*
  2155. * Think of how you add 1 to 999
  2156. */
  2157. while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
  2158. kunmap_atomic(map, KM_USER0);
  2159. page = list_entry(page->lru.next, struct page, lru);
  2160. BUG_ON(page == head);
  2161. map = kmap_atomic(page, KM_USER0) + offset;
  2162. }
  2163. if (*map == SWAP_CONT_MAX) {
  2164. kunmap_atomic(map, KM_USER0);
  2165. page = list_entry(page->lru.next, struct page, lru);
  2166. if (page == head)
  2167. return false; /* add count continuation */
  2168. map = kmap_atomic(page, KM_USER0) + offset;
  2169. init_map: *map = 0; /* we didn't zero the page */
  2170. }
  2171. *map += 1;
  2172. kunmap_atomic(map, KM_USER0);
  2173. page = list_entry(page->lru.prev, struct page, lru);
  2174. while (page != head) {
  2175. map = kmap_atomic(page, KM_USER0) + offset;
  2176. *map = COUNT_CONTINUED;
  2177. kunmap_atomic(map, KM_USER0);
  2178. page = list_entry(page->lru.prev, struct page, lru);
  2179. }
  2180. return true; /* incremented */
  2181. } else { /* decrementing */
  2182. /*
  2183. * Think of how you subtract 1 from 1000
  2184. */
  2185. BUG_ON(count != COUNT_CONTINUED);
  2186. while (*map == COUNT_CONTINUED) {
  2187. kunmap_atomic(map, KM_USER0);
  2188. page = list_entry(page->lru.next, struct page, lru);
  2189. BUG_ON(page == head);
  2190. map = kmap_atomic(page, KM_USER0) + offset;
  2191. }
  2192. BUG_ON(*map == 0);
  2193. *map -= 1;
  2194. if (*map == 0)
  2195. count = 0;
  2196. kunmap_atomic(map, KM_USER0);
  2197. page = list_entry(page->lru.prev, struct page, lru);
  2198. while (page != head) {
  2199. map = kmap_atomic(page, KM_USER0) + offset;
  2200. *map = SWAP_CONT_MAX | count;
  2201. count = COUNT_CONTINUED;
  2202. kunmap_atomic(map, KM_USER0);
  2203. page = list_entry(page->lru.prev, struct page, lru);
  2204. }
  2205. return count == COUNT_CONTINUED;
  2206. }
  2207. }
  2208. /*
  2209. * free_swap_count_continuations - swapoff free all the continuation pages
  2210. * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
  2211. */
  2212. static void free_swap_count_continuations(struct swap_info_struct *si)
  2213. {
  2214. pgoff_t offset;
  2215. for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
  2216. struct page *head;
  2217. head = vmalloc_to_page(si->swap_map + offset);
  2218. if (page_private(head)) {
  2219. struct list_head *this, *next;
  2220. list_for_each_safe(this, next, &head->lru) {
  2221. struct page *page;
  2222. page = list_entry(this, struct page, lru);
  2223. list_del(this);
  2224. __free_page(page);
  2225. }
  2226. }
  2227. }
  2228. }