swapfile.c 63 KB

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