swapfile.c 55 KB

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