swapfile.c 75 KB

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