swapfile.c 65 KB

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