swapfile.c 77 KB

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