swapfile.c 76 KB

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