ksm.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. /*
  2. * Memory merging support.
  3. *
  4. * This code enables dynamic sharing of identical pages found in different
  5. * memory areas, even if they are not shared by fork()
  6. *
  7. * Copyright (C) 2008-2009 Red Hat, Inc.
  8. * Authors:
  9. * Izik Eidus
  10. * Andrea Arcangeli
  11. * Chris Wright
  12. * Hugh Dickins
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2.
  15. */
  16. #include <linux/errno.h>
  17. #include <linux/mm.h>
  18. #include <linux/fs.h>
  19. #include <linux/mman.h>
  20. #include <linux/sched.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/rmap.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/kthread.h>
  28. #include <linux/wait.h>
  29. #include <linux/slab.h>
  30. #include <linux/rbtree.h>
  31. #include <linux/memory.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <linux/swap.h>
  34. #include <linux/ksm.h>
  35. #include <linux/hash.h>
  36. #include <asm/tlbflush.h>
  37. #include "internal.h"
  38. /*
  39. * A few notes about the KSM scanning process,
  40. * to make it easier to understand the data structures below:
  41. *
  42. * In order to reduce excessive scanning, KSM sorts the memory pages by their
  43. * contents into a data structure that holds pointers to the pages' locations.
  44. *
  45. * Since the contents of the pages may change at any moment, KSM cannot just
  46. * insert the pages into a normal sorted tree and expect it to find anything.
  47. * Therefore KSM uses two data structures - the stable and the unstable tree.
  48. *
  49. * The stable tree holds pointers to all the merged pages (ksm pages), sorted
  50. * by their contents. Because each such page is write-protected, searching on
  51. * this tree is fully assured to be working (except when pages are unmapped),
  52. * and therefore this tree is called the stable tree.
  53. *
  54. * In addition to the stable tree, KSM uses a second data structure called the
  55. * unstable tree: this tree holds pointers to pages which have been found to
  56. * be "unchanged for a period of time". The unstable tree sorts these pages
  57. * by their contents, but since they are not write-protected, KSM cannot rely
  58. * upon the unstable tree to work correctly - the unstable tree is liable to
  59. * be corrupted as its contents are modified, and so it is called unstable.
  60. *
  61. * KSM solves this problem by several techniques:
  62. *
  63. * 1) The unstable tree is flushed every time KSM completes scanning all
  64. * memory areas, and then the tree is rebuilt again from the beginning.
  65. * 2) KSM will only insert into the unstable tree, pages whose hash value
  66. * has not changed since the previous scan of all memory areas.
  67. * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
  68. * colors of the nodes and not on their contents, assuring that even when
  69. * the tree gets "corrupted" it won't get out of balance, so scanning time
  70. * remains the same (also, searching and inserting nodes in an rbtree uses
  71. * the same algorithm, so we have no overhead when we flush and rebuild).
  72. * 4) KSM never flushes the stable tree, which means that even if it were to
  73. * take 10 attempts to find a page in the unstable tree, once it is found,
  74. * it is secured in the stable tree. (When we scan a new page, we first
  75. * compare it against the stable tree, and then against the unstable tree.)
  76. */
  77. /**
  78. * struct mm_slot - ksm information per mm that is being scanned
  79. * @link: link to the mm_slots hash list
  80. * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
  81. * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
  82. * @mm: the mm that this information is valid for
  83. */
  84. struct mm_slot {
  85. struct hlist_node link;
  86. struct list_head mm_list;
  87. struct rmap_item *rmap_list;
  88. struct mm_struct *mm;
  89. };
  90. /**
  91. * struct ksm_scan - cursor for scanning
  92. * @mm_slot: the current mm_slot we are scanning
  93. * @address: the next address inside that to be scanned
  94. * @rmap_list: link to the next rmap to be scanned in the rmap_list
  95. * @seqnr: count of completed full scans (needed when removing unstable node)
  96. *
  97. * There is only the one ksm_scan instance of this cursor structure.
  98. */
  99. struct ksm_scan {
  100. struct mm_slot *mm_slot;
  101. unsigned long address;
  102. struct rmap_item **rmap_list;
  103. unsigned long seqnr;
  104. };
  105. /**
  106. * struct stable_node - node of the stable rbtree
  107. * @node: rb node of this ksm page in the stable tree
  108. * @hlist: hlist head of rmap_items using this ksm page
  109. * @kpfn: page frame number of this ksm page
  110. */
  111. struct stable_node {
  112. struct rb_node node;
  113. struct hlist_head hlist;
  114. unsigned long kpfn;
  115. };
  116. /**
  117. * struct rmap_item - reverse mapping item for virtual addresses
  118. * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
  119. * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
  120. * @mm: the memory structure this rmap_item is pointing into
  121. * @address: the virtual address this rmap_item tracks (+ flags in low bits)
  122. * @oldchecksum: previous checksum of the page at that virtual address
  123. * @node: rb node of this rmap_item in the unstable tree
  124. * @head: pointer to stable_node heading this list in the stable tree
  125. * @hlist: link into hlist of rmap_items hanging off that stable_node
  126. */
  127. struct rmap_item {
  128. struct rmap_item *rmap_list;
  129. struct anon_vma *anon_vma; /* when stable */
  130. struct mm_struct *mm;
  131. unsigned long address; /* + low bits used for flags below */
  132. unsigned int oldchecksum; /* when unstable */
  133. union {
  134. struct rb_node node; /* when node of unstable tree */
  135. struct { /* when listed from stable tree */
  136. struct stable_node *head;
  137. struct hlist_node hlist;
  138. };
  139. };
  140. };
  141. #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */
  142. #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */
  143. #define STABLE_FLAG 0x200 /* is listed from the stable tree */
  144. /* The stable and unstable tree heads */
  145. static struct rb_root root_stable_tree = RB_ROOT;
  146. static struct rb_root root_unstable_tree = RB_ROOT;
  147. #define MM_SLOTS_HASH_SHIFT 10
  148. #define MM_SLOTS_HASH_HEADS (1 << MM_SLOTS_HASH_SHIFT)
  149. static struct hlist_head mm_slots_hash[MM_SLOTS_HASH_HEADS];
  150. static struct mm_slot ksm_mm_head = {
  151. .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
  152. };
  153. static struct ksm_scan ksm_scan = {
  154. .mm_slot = &ksm_mm_head,
  155. };
  156. static struct kmem_cache *rmap_item_cache;
  157. static struct kmem_cache *stable_node_cache;
  158. static struct kmem_cache *mm_slot_cache;
  159. /* The number of nodes in the stable tree */
  160. static unsigned long ksm_pages_shared;
  161. /* The number of page slots additionally sharing those nodes */
  162. static unsigned long ksm_pages_sharing;
  163. /* The number of nodes in the unstable tree */
  164. static unsigned long ksm_pages_unshared;
  165. /* The number of rmap_items in use: to calculate pages_volatile */
  166. static unsigned long ksm_rmap_items;
  167. /* Number of pages ksmd should scan in one batch */
  168. static unsigned int ksm_thread_pages_to_scan = 100;
  169. /* Milliseconds ksmd should sleep between batches */
  170. static unsigned int ksm_thread_sleep_millisecs = 20;
  171. #define KSM_RUN_STOP 0
  172. #define KSM_RUN_MERGE 1
  173. #define KSM_RUN_UNMERGE 2
  174. static unsigned int ksm_run = KSM_RUN_STOP;
  175. static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
  176. static DEFINE_MUTEX(ksm_thread_mutex);
  177. static DEFINE_SPINLOCK(ksm_mmlist_lock);
  178. #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
  179. sizeof(struct __struct), __alignof__(struct __struct),\
  180. (__flags), NULL)
  181. static int __init ksm_slab_init(void)
  182. {
  183. rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
  184. if (!rmap_item_cache)
  185. goto out;
  186. stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
  187. if (!stable_node_cache)
  188. goto out_free1;
  189. mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
  190. if (!mm_slot_cache)
  191. goto out_free2;
  192. return 0;
  193. out_free2:
  194. kmem_cache_destroy(stable_node_cache);
  195. out_free1:
  196. kmem_cache_destroy(rmap_item_cache);
  197. out:
  198. return -ENOMEM;
  199. }
  200. static void __init ksm_slab_free(void)
  201. {
  202. kmem_cache_destroy(mm_slot_cache);
  203. kmem_cache_destroy(stable_node_cache);
  204. kmem_cache_destroy(rmap_item_cache);
  205. mm_slot_cache = NULL;
  206. }
  207. static inline struct rmap_item *alloc_rmap_item(void)
  208. {
  209. struct rmap_item *rmap_item;
  210. rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL);
  211. if (rmap_item)
  212. ksm_rmap_items++;
  213. return rmap_item;
  214. }
  215. static inline void free_rmap_item(struct rmap_item *rmap_item)
  216. {
  217. ksm_rmap_items--;
  218. rmap_item->mm = NULL; /* debug safety */
  219. kmem_cache_free(rmap_item_cache, rmap_item);
  220. }
  221. static inline struct stable_node *alloc_stable_node(void)
  222. {
  223. return kmem_cache_alloc(stable_node_cache, GFP_KERNEL);
  224. }
  225. static inline void free_stable_node(struct stable_node *stable_node)
  226. {
  227. kmem_cache_free(stable_node_cache, stable_node);
  228. }
  229. static inline struct mm_slot *alloc_mm_slot(void)
  230. {
  231. if (!mm_slot_cache) /* initialization failed */
  232. return NULL;
  233. return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
  234. }
  235. static inline void free_mm_slot(struct mm_slot *mm_slot)
  236. {
  237. kmem_cache_free(mm_slot_cache, mm_slot);
  238. }
  239. static struct mm_slot *get_mm_slot(struct mm_struct *mm)
  240. {
  241. struct mm_slot *mm_slot;
  242. struct hlist_head *bucket;
  243. struct hlist_node *node;
  244. bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)];
  245. hlist_for_each_entry(mm_slot, node, bucket, link) {
  246. if (mm == mm_slot->mm)
  247. return mm_slot;
  248. }
  249. return NULL;
  250. }
  251. static void insert_to_mm_slots_hash(struct mm_struct *mm,
  252. struct mm_slot *mm_slot)
  253. {
  254. struct hlist_head *bucket;
  255. bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)];
  256. mm_slot->mm = mm;
  257. hlist_add_head(&mm_slot->link, bucket);
  258. }
  259. static inline int in_stable_tree(struct rmap_item *rmap_item)
  260. {
  261. return rmap_item->address & STABLE_FLAG;
  262. }
  263. static void hold_anon_vma(struct rmap_item *rmap_item,
  264. struct anon_vma *anon_vma)
  265. {
  266. rmap_item->anon_vma = anon_vma;
  267. get_anon_vma(anon_vma);
  268. }
  269. static void ksm_drop_anon_vma(struct rmap_item *rmap_item)
  270. {
  271. struct anon_vma *anon_vma = rmap_item->anon_vma;
  272. drop_anon_vma(anon_vma);
  273. }
  274. /*
  275. * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
  276. * page tables after it has passed through ksm_exit() - which, if necessary,
  277. * takes mmap_sem briefly to serialize against them. ksm_exit() does not set
  278. * a special flag: they can just back out as soon as mm_users goes to zero.
  279. * ksm_test_exit() is used throughout to make this test for exit: in some
  280. * places for correctness, in some places just to avoid unnecessary work.
  281. */
  282. static inline bool ksm_test_exit(struct mm_struct *mm)
  283. {
  284. return atomic_read(&mm->mm_users) == 0;
  285. }
  286. /*
  287. * We use break_ksm to break COW on a ksm page: it's a stripped down
  288. *
  289. * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1)
  290. * put_page(page);
  291. *
  292. * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
  293. * in case the application has unmapped and remapped mm,addr meanwhile.
  294. * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP
  295. * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
  296. */
  297. static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
  298. {
  299. struct page *page;
  300. int ret = 0;
  301. do {
  302. cond_resched();
  303. page = follow_page(vma, addr, FOLL_GET);
  304. if (IS_ERR_OR_NULL(page))
  305. break;
  306. if (PageKsm(page))
  307. ret = handle_mm_fault(vma->vm_mm, vma, addr,
  308. FAULT_FLAG_WRITE);
  309. else
  310. ret = VM_FAULT_WRITE;
  311. put_page(page);
  312. } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM)));
  313. /*
  314. * We must loop because handle_mm_fault() may back out if there's
  315. * any difficulty e.g. if pte accessed bit gets updated concurrently.
  316. *
  317. * VM_FAULT_WRITE is what we have been hoping for: it indicates that
  318. * COW has been broken, even if the vma does not permit VM_WRITE;
  319. * but note that a concurrent fault might break PageKsm for us.
  320. *
  321. * VM_FAULT_SIGBUS could occur if we race with truncation of the
  322. * backing file, which also invalidates anonymous pages: that's
  323. * okay, that truncation will have unmapped the PageKsm for us.
  324. *
  325. * VM_FAULT_OOM: at the time of writing (late July 2009), setting
  326. * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
  327. * current task has TIF_MEMDIE set, and will be OOM killed on return
  328. * to user; and ksmd, having no mm, would never be chosen for that.
  329. *
  330. * But if the mm is in a limited mem_cgroup, then the fault may fail
  331. * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
  332. * even ksmd can fail in this way - though it's usually breaking ksm
  333. * just to undo a merge it made a moment before, so unlikely to oom.
  334. *
  335. * That's a pity: we might therefore have more kernel pages allocated
  336. * than we're counting as nodes in the stable tree; but ksm_do_scan
  337. * will retry to break_cow on each pass, so should recover the page
  338. * in due course. The important thing is to not let VM_MERGEABLE
  339. * be cleared while any such pages might remain in the area.
  340. */
  341. return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
  342. }
  343. static void break_cow(struct rmap_item *rmap_item)
  344. {
  345. struct mm_struct *mm = rmap_item->mm;
  346. unsigned long addr = rmap_item->address;
  347. struct vm_area_struct *vma;
  348. /*
  349. * It is not an accident that whenever we want to break COW
  350. * to undo, we also need to drop a reference to the anon_vma.
  351. */
  352. ksm_drop_anon_vma(rmap_item);
  353. down_read(&mm->mmap_sem);
  354. if (ksm_test_exit(mm))
  355. goto out;
  356. vma = find_vma(mm, addr);
  357. if (!vma || vma->vm_start > addr)
  358. goto out;
  359. if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
  360. goto out;
  361. break_ksm(vma, addr);
  362. out:
  363. up_read(&mm->mmap_sem);
  364. }
  365. static struct page *get_mergeable_page(struct rmap_item *rmap_item)
  366. {
  367. struct mm_struct *mm = rmap_item->mm;
  368. unsigned long addr = rmap_item->address;
  369. struct vm_area_struct *vma;
  370. struct page *page;
  371. down_read(&mm->mmap_sem);
  372. if (ksm_test_exit(mm))
  373. goto out;
  374. vma = find_vma(mm, addr);
  375. if (!vma || vma->vm_start > addr)
  376. goto out;
  377. if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
  378. goto out;
  379. page = follow_page(vma, addr, FOLL_GET);
  380. if (IS_ERR_OR_NULL(page))
  381. goto out;
  382. if (PageAnon(page)) {
  383. flush_anon_page(vma, page, addr);
  384. flush_dcache_page(page);
  385. } else {
  386. put_page(page);
  387. out: page = NULL;
  388. }
  389. up_read(&mm->mmap_sem);
  390. return page;
  391. }
  392. static void remove_node_from_stable_tree(struct stable_node *stable_node)
  393. {
  394. struct rmap_item *rmap_item;
  395. struct hlist_node *hlist;
  396. hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
  397. if (rmap_item->hlist.next)
  398. ksm_pages_sharing--;
  399. else
  400. ksm_pages_shared--;
  401. ksm_drop_anon_vma(rmap_item);
  402. rmap_item->address &= PAGE_MASK;
  403. cond_resched();
  404. }
  405. rb_erase(&stable_node->node, &root_stable_tree);
  406. free_stable_node(stable_node);
  407. }
  408. /*
  409. * get_ksm_page: checks if the page indicated by the stable node
  410. * is still its ksm page, despite having held no reference to it.
  411. * In which case we can trust the content of the page, and it
  412. * returns the gotten page; but if the page has now been zapped,
  413. * remove the stale node from the stable tree and return NULL.
  414. *
  415. * You would expect the stable_node to hold a reference to the ksm page.
  416. * But if it increments the page's count, swapping out has to wait for
  417. * ksmd to come around again before it can free the page, which may take
  418. * seconds or even minutes: much too unresponsive. So instead we use a
  419. * "keyhole reference": access to the ksm page from the stable node peeps
  420. * out through its keyhole to see if that page still holds the right key,
  421. * pointing back to this stable node. This relies on freeing a PageAnon
  422. * page to reset its page->mapping to NULL, and relies on no other use of
  423. * a page to put something that might look like our key in page->mapping.
  424. *
  425. * include/linux/pagemap.h page_cache_get_speculative() is a good reference,
  426. * but this is different - made simpler by ksm_thread_mutex being held, but
  427. * interesting for assuming that no other use of the struct page could ever
  428. * put our expected_mapping into page->mapping (or a field of the union which
  429. * coincides with page->mapping). The RCU calls are not for KSM at all, but
  430. * to keep the page_count protocol described with page_cache_get_speculative.
  431. *
  432. * Note: it is possible that get_ksm_page() will return NULL one moment,
  433. * then page the next, if the page is in between page_freeze_refs() and
  434. * page_unfreeze_refs(): this shouldn't be a problem anywhere, the page
  435. * is on its way to being freed; but it is an anomaly to bear in mind.
  436. */
  437. static struct page *get_ksm_page(struct stable_node *stable_node)
  438. {
  439. struct page *page;
  440. void *expected_mapping;
  441. page = pfn_to_page(stable_node->kpfn);
  442. expected_mapping = (void *)stable_node +
  443. (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
  444. rcu_read_lock();
  445. if (page->mapping != expected_mapping)
  446. goto stale;
  447. if (!get_page_unless_zero(page))
  448. goto stale;
  449. if (page->mapping != expected_mapping) {
  450. put_page(page);
  451. goto stale;
  452. }
  453. rcu_read_unlock();
  454. return page;
  455. stale:
  456. rcu_read_unlock();
  457. remove_node_from_stable_tree(stable_node);
  458. return NULL;
  459. }
  460. /*
  461. * Removing rmap_item from stable or unstable tree.
  462. * This function will clean the information from the stable/unstable tree.
  463. */
  464. static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
  465. {
  466. if (rmap_item->address & STABLE_FLAG) {
  467. struct stable_node *stable_node;
  468. struct page *page;
  469. stable_node = rmap_item->head;
  470. page = get_ksm_page(stable_node);
  471. if (!page)
  472. goto out;
  473. lock_page(page);
  474. hlist_del(&rmap_item->hlist);
  475. unlock_page(page);
  476. put_page(page);
  477. if (stable_node->hlist.first)
  478. ksm_pages_sharing--;
  479. else
  480. ksm_pages_shared--;
  481. ksm_drop_anon_vma(rmap_item);
  482. rmap_item->address &= PAGE_MASK;
  483. } else if (rmap_item->address & UNSTABLE_FLAG) {
  484. unsigned char age;
  485. /*
  486. * Usually ksmd can and must skip the rb_erase, because
  487. * root_unstable_tree was already reset to RB_ROOT.
  488. * But be careful when an mm is exiting: do the rb_erase
  489. * if this rmap_item was inserted by this scan, rather
  490. * than left over from before.
  491. */
  492. age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
  493. BUG_ON(age > 1);
  494. if (!age)
  495. rb_erase(&rmap_item->node, &root_unstable_tree);
  496. ksm_pages_unshared--;
  497. rmap_item->address &= PAGE_MASK;
  498. }
  499. out:
  500. cond_resched(); /* we're called from many long loops */
  501. }
  502. static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
  503. struct rmap_item **rmap_list)
  504. {
  505. while (*rmap_list) {
  506. struct rmap_item *rmap_item = *rmap_list;
  507. *rmap_list = rmap_item->rmap_list;
  508. remove_rmap_item_from_tree(rmap_item);
  509. free_rmap_item(rmap_item);
  510. }
  511. }
  512. /*
  513. * Though it's very tempting to unmerge in_stable_tree(rmap_item)s rather
  514. * than check every pte of a given vma, the locking doesn't quite work for
  515. * that - an rmap_item is assigned to the stable tree after inserting ksm
  516. * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing
  517. * rmap_items from parent to child at fork time (so as not to waste time
  518. * if exit comes before the next scan reaches it).
  519. *
  520. * Similarly, although we'd like to remove rmap_items (so updating counts
  521. * and freeing memory) when unmerging an area, it's easier to leave that
  522. * to the next pass of ksmd - consider, for example, how ksmd might be
  523. * in cmp_and_merge_page on one of the rmap_items we would be removing.
  524. */
  525. static int unmerge_ksm_pages(struct vm_area_struct *vma,
  526. unsigned long start, unsigned long end)
  527. {
  528. unsigned long addr;
  529. int err = 0;
  530. for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
  531. if (ksm_test_exit(vma->vm_mm))
  532. break;
  533. if (signal_pending(current))
  534. err = -ERESTARTSYS;
  535. else
  536. err = break_ksm(vma, addr);
  537. }
  538. return err;
  539. }
  540. #ifdef CONFIG_SYSFS
  541. /*
  542. * Only called through the sysfs control interface:
  543. */
  544. static int unmerge_and_remove_all_rmap_items(void)
  545. {
  546. struct mm_slot *mm_slot;
  547. struct mm_struct *mm;
  548. struct vm_area_struct *vma;
  549. int err = 0;
  550. spin_lock(&ksm_mmlist_lock);
  551. ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
  552. struct mm_slot, mm_list);
  553. spin_unlock(&ksm_mmlist_lock);
  554. for (mm_slot = ksm_scan.mm_slot;
  555. mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
  556. mm = mm_slot->mm;
  557. down_read(&mm->mmap_sem);
  558. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  559. if (ksm_test_exit(mm))
  560. break;
  561. if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
  562. continue;
  563. err = unmerge_ksm_pages(vma,
  564. vma->vm_start, vma->vm_end);
  565. if (err)
  566. goto error;
  567. }
  568. remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
  569. spin_lock(&ksm_mmlist_lock);
  570. ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
  571. struct mm_slot, mm_list);
  572. if (ksm_test_exit(mm)) {
  573. hlist_del(&mm_slot->link);
  574. list_del(&mm_slot->mm_list);
  575. spin_unlock(&ksm_mmlist_lock);
  576. free_mm_slot(mm_slot);
  577. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  578. up_read(&mm->mmap_sem);
  579. mmdrop(mm);
  580. } else {
  581. spin_unlock(&ksm_mmlist_lock);
  582. up_read(&mm->mmap_sem);
  583. }
  584. }
  585. ksm_scan.seqnr = 0;
  586. return 0;
  587. error:
  588. up_read(&mm->mmap_sem);
  589. spin_lock(&ksm_mmlist_lock);
  590. ksm_scan.mm_slot = &ksm_mm_head;
  591. spin_unlock(&ksm_mmlist_lock);
  592. return err;
  593. }
  594. #endif /* CONFIG_SYSFS */
  595. static u32 calc_checksum(struct page *page)
  596. {
  597. u32 checksum;
  598. void *addr = kmap_atomic(page, KM_USER0);
  599. checksum = jhash2(addr, PAGE_SIZE / 4, 17);
  600. kunmap_atomic(addr, KM_USER0);
  601. return checksum;
  602. }
  603. static int memcmp_pages(struct page *page1, struct page *page2)
  604. {
  605. char *addr1, *addr2;
  606. int ret;
  607. addr1 = kmap_atomic(page1, KM_USER0);
  608. addr2 = kmap_atomic(page2, KM_USER1);
  609. ret = memcmp(addr1, addr2, PAGE_SIZE);
  610. kunmap_atomic(addr2, KM_USER1);
  611. kunmap_atomic(addr1, KM_USER0);
  612. return ret;
  613. }
  614. static inline int pages_identical(struct page *page1, struct page *page2)
  615. {
  616. return !memcmp_pages(page1, page2);
  617. }
  618. static int write_protect_page(struct vm_area_struct *vma, struct page *page,
  619. pte_t *orig_pte)
  620. {
  621. struct mm_struct *mm = vma->vm_mm;
  622. unsigned long addr;
  623. pte_t *ptep;
  624. spinlock_t *ptl;
  625. int swapped;
  626. int err = -EFAULT;
  627. addr = page_address_in_vma(page, vma);
  628. if (addr == -EFAULT)
  629. goto out;
  630. ptep = page_check_address(page, mm, addr, &ptl, 0);
  631. if (!ptep)
  632. goto out;
  633. if (pte_write(*ptep)) {
  634. pte_t entry;
  635. swapped = PageSwapCache(page);
  636. flush_cache_page(vma, addr, page_to_pfn(page));
  637. /*
  638. * Ok this is tricky, when get_user_pages_fast() run it doesnt
  639. * take any lock, therefore the check that we are going to make
  640. * with the pagecount against the mapcount is racey and
  641. * O_DIRECT can happen right after the check.
  642. * So we clear the pte and flush the tlb before the check
  643. * this assure us that no O_DIRECT can happen after the check
  644. * or in the middle of the check.
  645. */
  646. entry = ptep_clear_flush(vma, addr, ptep);
  647. /*
  648. * Check that no O_DIRECT or similar I/O is in progress on the
  649. * page
  650. */
  651. if (page_mapcount(page) + 1 + swapped != page_count(page)) {
  652. set_pte_at(mm, addr, ptep, entry);
  653. goto out_unlock;
  654. }
  655. entry = pte_wrprotect(entry);
  656. set_pte_at_notify(mm, addr, ptep, entry);
  657. }
  658. *orig_pte = *ptep;
  659. err = 0;
  660. out_unlock:
  661. pte_unmap_unlock(ptep, ptl);
  662. out:
  663. return err;
  664. }
  665. /**
  666. * replace_page - replace page in vma by new ksm page
  667. * @vma: vma that holds the pte pointing to page
  668. * @page: the page we are replacing by kpage
  669. * @kpage: the ksm page we replace page by
  670. * @orig_pte: the original value of the pte
  671. *
  672. * Returns 0 on success, -EFAULT on failure.
  673. */
  674. static int replace_page(struct vm_area_struct *vma, struct page *page,
  675. struct page *kpage, pte_t orig_pte)
  676. {
  677. struct mm_struct *mm = vma->vm_mm;
  678. pgd_t *pgd;
  679. pud_t *pud;
  680. pmd_t *pmd;
  681. pte_t *ptep;
  682. spinlock_t *ptl;
  683. unsigned long addr;
  684. int err = -EFAULT;
  685. addr = page_address_in_vma(page, vma);
  686. if (addr == -EFAULT)
  687. goto out;
  688. pgd = pgd_offset(mm, addr);
  689. if (!pgd_present(*pgd))
  690. goto out;
  691. pud = pud_offset(pgd, addr);
  692. if (!pud_present(*pud))
  693. goto out;
  694. pmd = pmd_offset(pud, addr);
  695. if (!pmd_present(*pmd))
  696. goto out;
  697. ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
  698. if (!pte_same(*ptep, orig_pte)) {
  699. pte_unmap_unlock(ptep, ptl);
  700. goto out;
  701. }
  702. get_page(kpage);
  703. page_add_anon_rmap(kpage, vma, addr);
  704. flush_cache_page(vma, addr, pte_pfn(*ptep));
  705. ptep_clear_flush(vma, addr, ptep);
  706. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  707. page_remove_rmap(page);
  708. put_page(page);
  709. pte_unmap_unlock(ptep, ptl);
  710. err = 0;
  711. out:
  712. return err;
  713. }
  714. /*
  715. * try_to_merge_one_page - take two pages and merge them into one
  716. * @vma: the vma that holds the pte pointing to page
  717. * @page: the PageAnon page that we want to replace with kpage
  718. * @kpage: the PageKsm page that we want to map instead of page,
  719. * or NULL the first time when we want to use page as kpage.
  720. *
  721. * This function returns 0 if the pages were merged, -EFAULT otherwise.
  722. */
  723. static int try_to_merge_one_page(struct vm_area_struct *vma,
  724. struct page *page, struct page *kpage)
  725. {
  726. pte_t orig_pte = __pte(0);
  727. int err = -EFAULT;
  728. if (page == kpage) /* ksm page forked */
  729. return 0;
  730. if (!(vma->vm_flags & VM_MERGEABLE))
  731. goto out;
  732. if (!PageAnon(page))
  733. goto out;
  734. /*
  735. * We need the page lock to read a stable PageSwapCache in
  736. * write_protect_page(). We use trylock_page() instead of
  737. * lock_page() because we don't want to wait here - we
  738. * prefer to continue scanning and merging different pages,
  739. * then come back to this page when it is unlocked.
  740. */
  741. if (!trylock_page(page))
  742. goto out;
  743. /*
  744. * If this anonymous page is mapped only here, its pte may need
  745. * to be write-protected. If it's mapped elsewhere, all of its
  746. * ptes are necessarily already write-protected. But in either
  747. * case, we need to lock and check page_count is not raised.
  748. */
  749. if (write_protect_page(vma, page, &orig_pte) == 0) {
  750. if (!kpage) {
  751. /*
  752. * While we hold page lock, upgrade page from
  753. * PageAnon+anon_vma to PageKsm+NULL stable_node:
  754. * stable_tree_insert() will update stable_node.
  755. */
  756. set_page_stable_node(page, NULL);
  757. mark_page_accessed(page);
  758. err = 0;
  759. } else if (pages_identical(page, kpage))
  760. err = replace_page(vma, page, kpage, orig_pte);
  761. }
  762. if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
  763. munlock_vma_page(page);
  764. if (!PageMlocked(kpage)) {
  765. unlock_page(page);
  766. lock_page(kpage);
  767. mlock_vma_page(kpage);
  768. page = kpage; /* for final unlock */
  769. }
  770. }
  771. unlock_page(page);
  772. out:
  773. return err;
  774. }
  775. /*
  776. * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
  777. * but no new kernel page is allocated: kpage must already be a ksm page.
  778. *
  779. * This function returns 0 if the pages were merged, -EFAULT otherwise.
  780. */
  781. static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
  782. struct page *page, struct page *kpage)
  783. {
  784. struct mm_struct *mm = rmap_item->mm;
  785. struct vm_area_struct *vma;
  786. int err = -EFAULT;
  787. down_read(&mm->mmap_sem);
  788. if (ksm_test_exit(mm))
  789. goto out;
  790. vma = find_vma(mm, rmap_item->address);
  791. if (!vma || vma->vm_start > rmap_item->address)
  792. goto out;
  793. err = try_to_merge_one_page(vma, page, kpage);
  794. if (err)
  795. goto out;
  796. /* Must get reference to anon_vma while still holding mmap_sem */
  797. hold_anon_vma(rmap_item, vma->anon_vma);
  798. out:
  799. up_read(&mm->mmap_sem);
  800. return err;
  801. }
  802. /*
  803. * try_to_merge_two_pages - take two identical pages and prepare them
  804. * to be merged into one page.
  805. *
  806. * This function returns the kpage if we successfully merged two identical
  807. * pages into one ksm page, NULL otherwise.
  808. *
  809. * Note that this function upgrades page to ksm page: if one of the pages
  810. * is already a ksm page, try_to_merge_with_ksm_page should be used.
  811. */
  812. static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
  813. struct page *page,
  814. struct rmap_item *tree_rmap_item,
  815. struct page *tree_page)
  816. {
  817. int err;
  818. err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
  819. if (!err) {
  820. err = try_to_merge_with_ksm_page(tree_rmap_item,
  821. tree_page, page);
  822. /*
  823. * If that fails, we have a ksm page with only one pte
  824. * pointing to it: so break it.
  825. */
  826. if (err)
  827. break_cow(rmap_item);
  828. }
  829. return err ? NULL : page;
  830. }
  831. /*
  832. * stable_tree_search - search for page inside the stable tree
  833. *
  834. * This function checks if there is a page inside the stable tree
  835. * with identical content to the page that we are scanning right now.
  836. *
  837. * This function returns the stable tree node of identical content if found,
  838. * NULL otherwise.
  839. */
  840. static struct page *stable_tree_search(struct page *page)
  841. {
  842. struct rb_node *node = root_stable_tree.rb_node;
  843. struct stable_node *stable_node;
  844. stable_node = page_stable_node(page);
  845. if (stable_node) { /* ksm page forked */
  846. get_page(page);
  847. return page;
  848. }
  849. while (node) {
  850. struct page *tree_page;
  851. int ret;
  852. cond_resched();
  853. stable_node = rb_entry(node, struct stable_node, node);
  854. tree_page = get_ksm_page(stable_node);
  855. if (!tree_page)
  856. return NULL;
  857. ret = memcmp_pages(page, tree_page);
  858. if (ret < 0) {
  859. put_page(tree_page);
  860. node = node->rb_left;
  861. } else if (ret > 0) {
  862. put_page(tree_page);
  863. node = node->rb_right;
  864. } else
  865. return tree_page;
  866. }
  867. return NULL;
  868. }
  869. /*
  870. * stable_tree_insert - insert rmap_item pointing to new ksm page
  871. * into the stable tree.
  872. *
  873. * This function returns the stable tree node just allocated on success,
  874. * NULL otherwise.
  875. */
  876. static struct stable_node *stable_tree_insert(struct page *kpage)
  877. {
  878. struct rb_node **new = &root_stable_tree.rb_node;
  879. struct rb_node *parent = NULL;
  880. struct stable_node *stable_node;
  881. while (*new) {
  882. struct page *tree_page;
  883. int ret;
  884. cond_resched();
  885. stable_node = rb_entry(*new, struct stable_node, node);
  886. tree_page = get_ksm_page(stable_node);
  887. if (!tree_page)
  888. return NULL;
  889. ret = memcmp_pages(kpage, tree_page);
  890. put_page(tree_page);
  891. parent = *new;
  892. if (ret < 0)
  893. new = &parent->rb_left;
  894. else if (ret > 0)
  895. new = &parent->rb_right;
  896. else {
  897. /*
  898. * It is not a bug that stable_tree_search() didn't
  899. * find this node: because at that time our page was
  900. * not yet write-protected, so may have changed since.
  901. */
  902. return NULL;
  903. }
  904. }
  905. stable_node = alloc_stable_node();
  906. if (!stable_node)
  907. return NULL;
  908. rb_link_node(&stable_node->node, parent, new);
  909. rb_insert_color(&stable_node->node, &root_stable_tree);
  910. INIT_HLIST_HEAD(&stable_node->hlist);
  911. stable_node->kpfn = page_to_pfn(kpage);
  912. set_page_stable_node(kpage, stable_node);
  913. return stable_node;
  914. }
  915. /*
  916. * unstable_tree_search_insert - search for identical page,
  917. * else insert rmap_item into the unstable tree.
  918. *
  919. * This function searches for a page in the unstable tree identical to the
  920. * page currently being scanned; and if no identical page is found in the
  921. * tree, we insert rmap_item as a new object into the unstable tree.
  922. *
  923. * This function returns pointer to rmap_item found to be identical
  924. * to the currently scanned page, NULL otherwise.
  925. *
  926. * This function does both searching and inserting, because they share
  927. * the same walking algorithm in an rbtree.
  928. */
  929. static
  930. struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
  931. struct page *page,
  932. struct page **tree_pagep)
  933. {
  934. struct rb_node **new = &root_unstable_tree.rb_node;
  935. struct rb_node *parent = NULL;
  936. while (*new) {
  937. struct rmap_item *tree_rmap_item;
  938. struct page *tree_page;
  939. int ret;
  940. cond_resched();
  941. tree_rmap_item = rb_entry(*new, struct rmap_item, node);
  942. tree_page = get_mergeable_page(tree_rmap_item);
  943. if (IS_ERR_OR_NULL(tree_page))
  944. return NULL;
  945. /*
  946. * Don't substitute a ksm page for a forked page.
  947. */
  948. if (page == tree_page) {
  949. put_page(tree_page);
  950. return NULL;
  951. }
  952. ret = memcmp_pages(page, tree_page);
  953. parent = *new;
  954. if (ret < 0) {
  955. put_page(tree_page);
  956. new = &parent->rb_left;
  957. } else if (ret > 0) {
  958. put_page(tree_page);
  959. new = &parent->rb_right;
  960. } else {
  961. *tree_pagep = tree_page;
  962. return tree_rmap_item;
  963. }
  964. }
  965. rmap_item->address |= UNSTABLE_FLAG;
  966. rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
  967. rb_link_node(&rmap_item->node, parent, new);
  968. rb_insert_color(&rmap_item->node, &root_unstable_tree);
  969. ksm_pages_unshared++;
  970. return NULL;
  971. }
  972. /*
  973. * stable_tree_append - add another rmap_item to the linked list of
  974. * rmap_items hanging off a given node of the stable tree, all sharing
  975. * the same ksm page.
  976. */
  977. static void stable_tree_append(struct rmap_item *rmap_item,
  978. struct stable_node *stable_node)
  979. {
  980. rmap_item->head = stable_node;
  981. rmap_item->address |= STABLE_FLAG;
  982. hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
  983. if (rmap_item->hlist.next)
  984. ksm_pages_sharing++;
  985. else
  986. ksm_pages_shared++;
  987. }
  988. /*
  989. * cmp_and_merge_page - first see if page can be merged into the stable tree;
  990. * if not, compare checksum to previous and if it's the same, see if page can
  991. * be inserted into the unstable tree, or merged with a page already there and
  992. * both transferred to the stable tree.
  993. *
  994. * @page: the page that we are searching identical page to.
  995. * @rmap_item: the reverse mapping into the virtual address of this page
  996. */
  997. static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
  998. {
  999. struct rmap_item *tree_rmap_item;
  1000. struct page *tree_page = NULL;
  1001. struct stable_node *stable_node;
  1002. struct page *kpage;
  1003. unsigned int checksum;
  1004. int err;
  1005. remove_rmap_item_from_tree(rmap_item);
  1006. /* We first start with searching the page inside the stable tree */
  1007. kpage = stable_tree_search(page);
  1008. if (kpage) {
  1009. err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
  1010. if (!err) {
  1011. /*
  1012. * The page was successfully merged:
  1013. * add its rmap_item to the stable tree.
  1014. */
  1015. lock_page(kpage);
  1016. stable_tree_append(rmap_item, page_stable_node(kpage));
  1017. unlock_page(kpage);
  1018. }
  1019. put_page(kpage);
  1020. return;
  1021. }
  1022. /*
  1023. * If the hash value of the page has changed from the last time
  1024. * we calculated it, this page is changing frequently: therefore we
  1025. * don't want to insert it in the unstable tree, and we don't want
  1026. * to waste our time searching for something identical to it there.
  1027. */
  1028. checksum = calc_checksum(page);
  1029. if (rmap_item->oldchecksum != checksum) {
  1030. rmap_item->oldchecksum = checksum;
  1031. return;
  1032. }
  1033. tree_rmap_item =
  1034. unstable_tree_search_insert(rmap_item, page, &tree_page);
  1035. if (tree_rmap_item) {
  1036. kpage = try_to_merge_two_pages(rmap_item, page,
  1037. tree_rmap_item, tree_page);
  1038. put_page(tree_page);
  1039. /*
  1040. * As soon as we merge this page, we want to remove the
  1041. * rmap_item of the page we have merged with from the unstable
  1042. * tree, and insert it instead as new node in the stable tree.
  1043. */
  1044. if (kpage) {
  1045. remove_rmap_item_from_tree(tree_rmap_item);
  1046. lock_page(kpage);
  1047. stable_node = stable_tree_insert(kpage);
  1048. if (stable_node) {
  1049. stable_tree_append(tree_rmap_item, stable_node);
  1050. stable_tree_append(rmap_item, stable_node);
  1051. }
  1052. unlock_page(kpage);
  1053. /*
  1054. * If we fail to insert the page into the stable tree,
  1055. * we will have 2 virtual addresses that are pointing
  1056. * to a ksm page left outside the stable tree,
  1057. * in which case we need to break_cow on both.
  1058. */
  1059. if (!stable_node) {
  1060. break_cow(tree_rmap_item);
  1061. break_cow(rmap_item);
  1062. }
  1063. }
  1064. }
  1065. }
  1066. static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
  1067. struct rmap_item **rmap_list,
  1068. unsigned long addr)
  1069. {
  1070. struct rmap_item *rmap_item;
  1071. while (*rmap_list) {
  1072. rmap_item = *rmap_list;
  1073. if ((rmap_item->address & PAGE_MASK) == addr)
  1074. return rmap_item;
  1075. if (rmap_item->address > addr)
  1076. break;
  1077. *rmap_list = rmap_item->rmap_list;
  1078. remove_rmap_item_from_tree(rmap_item);
  1079. free_rmap_item(rmap_item);
  1080. }
  1081. rmap_item = alloc_rmap_item();
  1082. if (rmap_item) {
  1083. /* It has already been zeroed */
  1084. rmap_item->mm = mm_slot->mm;
  1085. rmap_item->address = addr;
  1086. rmap_item->rmap_list = *rmap_list;
  1087. *rmap_list = rmap_item;
  1088. }
  1089. return rmap_item;
  1090. }
  1091. static struct rmap_item *scan_get_next_rmap_item(struct page **page)
  1092. {
  1093. struct mm_struct *mm;
  1094. struct mm_slot *slot;
  1095. struct vm_area_struct *vma;
  1096. struct rmap_item *rmap_item;
  1097. if (list_empty(&ksm_mm_head.mm_list))
  1098. return NULL;
  1099. slot = ksm_scan.mm_slot;
  1100. if (slot == &ksm_mm_head) {
  1101. root_unstable_tree = RB_ROOT;
  1102. spin_lock(&ksm_mmlist_lock);
  1103. slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
  1104. ksm_scan.mm_slot = slot;
  1105. spin_unlock(&ksm_mmlist_lock);
  1106. next_mm:
  1107. ksm_scan.address = 0;
  1108. ksm_scan.rmap_list = &slot->rmap_list;
  1109. }
  1110. mm = slot->mm;
  1111. down_read(&mm->mmap_sem);
  1112. if (ksm_test_exit(mm))
  1113. vma = NULL;
  1114. else
  1115. vma = find_vma(mm, ksm_scan.address);
  1116. for (; vma; vma = vma->vm_next) {
  1117. if (!(vma->vm_flags & VM_MERGEABLE))
  1118. continue;
  1119. if (ksm_scan.address < vma->vm_start)
  1120. ksm_scan.address = vma->vm_start;
  1121. if (!vma->anon_vma)
  1122. ksm_scan.address = vma->vm_end;
  1123. while (ksm_scan.address < vma->vm_end) {
  1124. if (ksm_test_exit(mm))
  1125. break;
  1126. *page = follow_page(vma, ksm_scan.address, FOLL_GET);
  1127. if (!IS_ERR_OR_NULL(*page) && PageAnon(*page)) {
  1128. flush_anon_page(vma, *page, ksm_scan.address);
  1129. flush_dcache_page(*page);
  1130. rmap_item = get_next_rmap_item(slot,
  1131. ksm_scan.rmap_list, ksm_scan.address);
  1132. if (rmap_item) {
  1133. ksm_scan.rmap_list =
  1134. &rmap_item->rmap_list;
  1135. ksm_scan.address += PAGE_SIZE;
  1136. } else
  1137. put_page(*page);
  1138. up_read(&mm->mmap_sem);
  1139. return rmap_item;
  1140. }
  1141. if (!IS_ERR_OR_NULL(*page))
  1142. put_page(*page);
  1143. ksm_scan.address += PAGE_SIZE;
  1144. cond_resched();
  1145. }
  1146. }
  1147. if (ksm_test_exit(mm)) {
  1148. ksm_scan.address = 0;
  1149. ksm_scan.rmap_list = &slot->rmap_list;
  1150. }
  1151. /*
  1152. * Nuke all the rmap_items that are above this current rmap:
  1153. * because there were no VM_MERGEABLE vmas with such addresses.
  1154. */
  1155. remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
  1156. spin_lock(&ksm_mmlist_lock);
  1157. ksm_scan.mm_slot = list_entry(slot->mm_list.next,
  1158. struct mm_slot, mm_list);
  1159. if (ksm_scan.address == 0) {
  1160. /*
  1161. * We've completed a full scan of all vmas, holding mmap_sem
  1162. * throughout, and found no VM_MERGEABLE: so do the same as
  1163. * __ksm_exit does to remove this mm from all our lists now.
  1164. * This applies either when cleaning up after __ksm_exit
  1165. * (but beware: we can reach here even before __ksm_exit),
  1166. * or when all VM_MERGEABLE areas have been unmapped (and
  1167. * mmap_sem then protects against race with MADV_MERGEABLE).
  1168. */
  1169. hlist_del(&slot->link);
  1170. list_del(&slot->mm_list);
  1171. spin_unlock(&ksm_mmlist_lock);
  1172. free_mm_slot(slot);
  1173. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  1174. up_read(&mm->mmap_sem);
  1175. mmdrop(mm);
  1176. } else {
  1177. spin_unlock(&ksm_mmlist_lock);
  1178. up_read(&mm->mmap_sem);
  1179. }
  1180. /* Repeat until we've completed scanning the whole list */
  1181. slot = ksm_scan.mm_slot;
  1182. if (slot != &ksm_mm_head)
  1183. goto next_mm;
  1184. ksm_scan.seqnr++;
  1185. return NULL;
  1186. }
  1187. /**
  1188. * ksm_do_scan - the ksm scanner main worker function.
  1189. * @scan_npages - number of pages we want to scan before we return.
  1190. */
  1191. static void ksm_do_scan(unsigned int scan_npages)
  1192. {
  1193. struct rmap_item *rmap_item;
  1194. struct page *uninitialized_var(page);
  1195. while (scan_npages--) {
  1196. cond_resched();
  1197. rmap_item = scan_get_next_rmap_item(&page);
  1198. if (!rmap_item)
  1199. return;
  1200. if (!PageKsm(page) || !in_stable_tree(rmap_item))
  1201. cmp_and_merge_page(page, rmap_item);
  1202. put_page(page);
  1203. }
  1204. }
  1205. static int ksmd_should_run(void)
  1206. {
  1207. return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
  1208. }
  1209. static int ksm_scan_thread(void *nothing)
  1210. {
  1211. set_user_nice(current, 5);
  1212. while (!kthread_should_stop()) {
  1213. mutex_lock(&ksm_thread_mutex);
  1214. if (ksmd_should_run())
  1215. ksm_do_scan(ksm_thread_pages_to_scan);
  1216. mutex_unlock(&ksm_thread_mutex);
  1217. if (ksmd_should_run()) {
  1218. schedule_timeout_interruptible(
  1219. msecs_to_jiffies(ksm_thread_sleep_millisecs));
  1220. } else {
  1221. wait_event_interruptible(ksm_thread_wait,
  1222. ksmd_should_run() || kthread_should_stop());
  1223. }
  1224. }
  1225. return 0;
  1226. }
  1227. int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  1228. unsigned long end, int advice, unsigned long *vm_flags)
  1229. {
  1230. struct mm_struct *mm = vma->vm_mm;
  1231. int err;
  1232. switch (advice) {
  1233. case MADV_MERGEABLE:
  1234. /*
  1235. * Be somewhat over-protective for now!
  1236. */
  1237. if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
  1238. VM_PFNMAP | VM_IO | VM_DONTEXPAND |
  1239. VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE |
  1240. VM_NONLINEAR | VM_MIXEDMAP | VM_SAO))
  1241. return 0; /* just ignore the advice */
  1242. if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
  1243. err = __ksm_enter(mm);
  1244. if (err)
  1245. return err;
  1246. }
  1247. *vm_flags |= VM_MERGEABLE;
  1248. break;
  1249. case MADV_UNMERGEABLE:
  1250. if (!(*vm_flags & VM_MERGEABLE))
  1251. return 0; /* just ignore the advice */
  1252. if (vma->anon_vma) {
  1253. err = unmerge_ksm_pages(vma, start, end);
  1254. if (err)
  1255. return err;
  1256. }
  1257. *vm_flags &= ~VM_MERGEABLE;
  1258. break;
  1259. }
  1260. return 0;
  1261. }
  1262. int __ksm_enter(struct mm_struct *mm)
  1263. {
  1264. struct mm_slot *mm_slot;
  1265. int needs_wakeup;
  1266. mm_slot = alloc_mm_slot();
  1267. if (!mm_slot)
  1268. return -ENOMEM;
  1269. /* Check ksm_run too? Would need tighter locking */
  1270. needs_wakeup = list_empty(&ksm_mm_head.mm_list);
  1271. spin_lock(&ksm_mmlist_lock);
  1272. insert_to_mm_slots_hash(mm, mm_slot);
  1273. /*
  1274. * Insert just behind the scanning cursor, to let the area settle
  1275. * down a little; when fork is followed by immediate exec, we don't
  1276. * want ksmd to waste time setting up and tearing down an rmap_list.
  1277. */
  1278. list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
  1279. spin_unlock(&ksm_mmlist_lock);
  1280. set_bit(MMF_VM_MERGEABLE, &mm->flags);
  1281. atomic_inc(&mm->mm_count);
  1282. if (needs_wakeup)
  1283. wake_up_interruptible(&ksm_thread_wait);
  1284. return 0;
  1285. }
  1286. void __ksm_exit(struct mm_struct *mm)
  1287. {
  1288. struct mm_slot *mm_slot;
  1289. int easy_to_free = 0;
  1290. /*
  1291. * This process is exiting: if it's straightforward (as is the
  1292. * case when ksmd was never running), free mm_slot immediately.
  1293. * But if it's at the cursor or has rmap_items linked to it, use
  1294. * mmap_sem to synchronize with any break_cows before pagetables
  1295. * are freed, and leave the mm_slot on the list for ksmd to free.
  1296. * Beware: ksm may already have noticed it exiting and freed the slot.
  1297. */
  1298. spin_lock(&ksm_mmlist_lock);
  1299. mm_slot = get_mm_slot(mm);
  1300. if (mm_slot && ksm_scan.mm_slot != mm_slot) {
  1301. if (!mm_slot->rmap_list) {
  1302. hlist_del(&mm_slot->link);
  1303. list_del(&mm_slot->mm_list);
  1304. easy_to_free = 1;
  1305. } else {
  1306. list_move(&mm_slot->mm_list,
  1307. &ksm_scan.mm_slot->mm_list);
  1308. }
  1309. }
  1310. spin_unlock(&ksm_mmlist_lock);
  1311. if (easy_to_free) {
  1312. free_mm_slot(mm_slot);
  1313. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  1314. mmdrop(mm);
  1315. } else if (mm_slot) {
  1316. down_write(&mm->mmap_sem);
  1317. up_write(&mm->mmap_sem);
  1318. }
  1319. }
  1320. struct page *ksm_does_need_to_copy(struct page *page,
  1321. struct vm_area_struct *vma, unsigned long address)
  1322. {
  1323. struct page *new_page;
  1324. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  1325. if (new_page) {
  1326. copy_user_highpage(new_page, page, address, vma);
  1327. SetPageDirty(new_page);
  1328. __SetPageUptodate(new_page);
  1329. SetPageSwapBacked(new_page);
  1330. __set_page_locked(new_page);
  1331. if (page_evictable(new_page, vma))
  1332. lru_cache_add_lru(new_page, LRU_ACTIVE_ANON);
  1333. else
  1334. add_page_to_unevictable_list(new_page);
  1335. }
  1336. return new_page;
  1337. }
  1338. int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg,
  1339. unsigned long *vm_flags)
  1340. {
  1341. struct stable_node *stable_node;
  1342. struct rmap_item *rmap_item;
  1343. struct hlist_node *hlist;
  1344. unsigned int mapcount = page_mapcount(page);
  1345. int referenced = 0;
  1346. int search_new_forks = 0;
  1347. VM_BUG_ON(!PageKsm(page));
  1348. VM_BUG_ON(!PageLocked(page));
  1349. stable_node = page_stable_node(page);
  1350. if (!stable_node)
  1351. return 0;
  1352. again:
  1353. hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
  1354. struct anon_vma *anon_vma = rmap_item->anon_vma;
  1355. struct anon_vma_chain *vmac;
  1356. struct vm_area_struct *vma;
  1357. anon_vma_lock(anon_vma);
  1358. list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
  1359. vma = vmac->vma;
  1360. if (rmap_item->address < vma->vm_start ||
  1361. rmap_item->address >= vma->vm_end)
  1362. continue;
  1363. /*
  1364. * Initially we examine only the vma which covers this
  1365. * rmap_item; but later, if there is still work to do,
  1366. * we examine covering vmas in other mms: in case they
  1367. * were forked from the original since ksmd passed.
  1368. */
  1369. if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
  1370. continue;
  1371. if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
  1372. continue;
  1373. referenced += page_referenced_one(page, vma,
  1374. rmap_item->address, &mapcount, vm_flags);
  1375. if (!search_new_forks || !mapcount)
  1376. break;
  1377. }
  1378. anon_vma_unlock(anon_vma);
  1379. if (!mapcount)
  1380. goto out;
  1381. }
  1382. if (!search_new_forks++)
  1383. goto again;
  1384. out:
  1385. return referenced;
  1386. }
  1387. int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
  1388. {
  1389. struct stable_node *stable_node;
  1390. struct hlist_node *hlist;
  1391. struct rmap_item *rmap_item;
  1392. int ret = SWAP_AGAIN;
  1393. int search_new_forks = 0;
  1394. VM_BUG_ON(!PageKsm(page));
  1395. VM_BUG_ON(!PageLocked(page));
  1396. stable_node = page_stable_node(page);
  1397. if (!stable_node)
  1398. return SWAP_FAIL;
  1399. again:
  1400. hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
  1401. struct anon_vma *anon_vma = rmap_item->anon_vma;
  1402. struct anon_vma_chain *vmac;
  1403. struct vm_area_struct *vma;
  1404. anon_vma_lock(anon_vma);
  1405. list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
  1406. vma = vmac->vma;
  1407. if (rmap_item->address < vma->vm_start ||
  1408. rmap_item->address >= vma->vm_end)
  1409. continue;
  1410. /*
  1411. * Initially we examine only the vma which covers this
  1412. * rmap_item; but later, if there is still work to do,
  1413. * we examine covering vmas in other mms: in case they
  1414. * were forked from the original since ksmd passed.
  1415. */
  1416. if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
  1417. continue;
  1418. ret = try_to_unmap_one(page, vma,
  1419. rmap_item->address, flags);
  1420. if (ret != SWAP_AGAIN || !page_mapped(page)) {
  1421. anon_vma_unlock(anon_vma);
  1422. goto out;
  1423. }
  1424. }
  1425. anon_vma_unlock(anon_vma);
  1426. }
  1427. if (!search_new_forks++)
  1428. goto again;
  1429. out:
  1430. return ret;
  1431. }
  1432. #ifdef CONFIG_MIGRATION
  1433. int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
  1434. struct vm_area_struct *, unsigned long, void *), void *arg)
  1435. {
  1436. struct stable_node *stable_node;
  1437. struct hlist_node *hlist;
  1438. struct rmap_item *rmap_item;
  1439. int ret = SWAP_AGAIN;
  1440. int search_new_forks = 0;
  1441. VM_BUG_ON(!PageKsm(page));
  1442. VM_BUG_ON(!PageLocked(page));
  1443. stable_node = page_stable_node(page);
  1444. if (!stable_node)
  1445. return ret;
  1446. again:
  1447. hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
  1448. struct anon_vma *anon_vma = rmap_item->anon_vma;
  1449. struct anon_vma_chain *vmac;
  1450. struct vm_area_struct *vma;
  1451. anon_vma_lock(anon_vma);
  1452. list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
  1453. vma = vmac->vma;
  1454. if (rmap_item->address < vma->vm_start ||
  1455. rmap_item->address >= vma->vm_end)
  1456. continue;
  1457. /*
  1458. * Initially we examine only the vma which covers this
  1459. * rmap_item; but later, if there is still work to do,
  1460. * we examine covering vmas in other mms: in case they
  1461. * were forked from the original since ksmd passed.
  1462. */
  1463. if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
  1464. continue;
  1465. ret = rmap_one(page, vma, rmap_item->address, arg);
  1466. if (ret != SWAP_AGAIN) {
  1467. anon_vma_unlock(anon_vma);
  1468. goto out;
  1469. }
  1470. }
  1471. anon_vma_unlock(anon_vma);
  1472. }
  1473. if (!search_new_forks++)
  1474. goto again;
  1475. out:
  1476. return ret;
  1477. }
  1478. void ksm_migrate_page(struct page *newpage, struct page *oldpage)
  1479. {
  1480. struct stable_node *stable_node;
  1481. VM_BUG_ON(!PageLocked(oldpage));
  1482. VM_BUG_ON(!PageLocked(newpage));
  1483. VM_BUG_ON(newpage->mapping != oldpage->mapping);
  1484. stable_node = page_stable_node(newpage);
  1485. if (stable_node) {
  1486. VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage));
  1487. stable_node->kpfn = page_to_pfn(newpage);
  1488. }
  1489. }
  1490. #endif /* CONFIG_MIGRATION */
  1491. #ifdef CONFIG_MEMORY_HOTREMOVE
  1492. static struct stable_node *ksm_check_stable_tree(unsigned long start_pfn,
  1493. unsigned long end_pfn)
  1494. {
  1495. struct rb_node *node;
  1496. for (node = rb_first(&root_stable_tree); node; node = rb_next(node)) {
  1497. struct stable_node *stable_node;
  1498. stable_node = rb_entry(node, struct stable_node, node);
  1499. if (stable_node->kpfn >= start_pfn &&
  1500. stable_node->kpfn < end_pfn)
  1501. return stable_node;
  1502. }
  1503. return NULL;
  1504. }
  1505. static int ksm_memory_callback(struct notifier_block *self,
  1506. unsigned long action, void *arg)
  1507. {
  1508. struct memory_notify *mn = arg;
  1509. struct stable_node *stable_node;
  1510. switch (action) {
  1511. case MEM_GOING_OFFLINE:
  1512. /*
  1513. * Keep it very simple for now: just lock out ksmd and
  1514. * MADV_UNMERGEABLE while any memory is going offline.
  1515. */
  1516. mutex_lock(&ksm_thread_mutex);
  1517. break;
  1518. case MEM_OFFLINE:
  1519. /*
  1520. * Most of the work is done by page migration; but there might
  1521. * be a few stable_nodes left over, still pointing to struct
  1522. * pages which have been offlined: prune those from the tree.
  1523. */
  1524. while ((stable_node = ksm_check_stable_tree(mn->start_pfn,
  1525. mn->start_pfn + mn->nr_pages)) != NULL)
  1526. remove_node_from_stable_tree(stable_node);
  1527. /* fallthrough */
  1528. case MEM_CANCEL_OFFLINE:
  1529. mutex_unlock(&ksm_thread_mutex);
  1530. break;
  1531. }
  1532. return NOTIFY_OK;
  1533. }
  1534. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1535. #ifdef CONFIG_SYSFS
  1536. /*
  1537. * This all compiles without CONFIG_SYSFS, but is a waste of space.
  1538. */
  1539. #define KSM_ATTR_RO(_name) \
  1540. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1541. #define KSM_ATTR(_name) \
  1542. static struct kobj_attribute _name##_attr = \
  1543. __ATTR(_name, 0644, _name##_show, _name##_store)
  1544. static ssize_t sleep_millisecs_show(struct kobject *kobj,
  1545. struct kobj_attribute *attr, char *buf)
  1546. {
  1547. return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
  1548. }
  1549. static ssize_t sleep_millisecs_store(struct kobject *kobj,
  1550. struct kobj_attribute *attr,
  1551. const char *buf, size_t count)
  1552. {
  1553. unsigned long msecs;
  1554. int err;
  1555. err = strict_strtoul(buf, 10, &msecs);
  1556. if (err || msecs > UINT_MAX)
  1557. return -EINVAL;
  1558. ksm_thread_sleep_millisecs = msecs;
  1559. return count;
  1560. }
  1561. KSM_ATTR(sleep_millisecs);
  1562. static ssize_t pages_to_scan_show(struct kobject *kobj,
  1563. struct kobj_attribute *attr, char *buf)
  1564. {
  1565. return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
  1566. }
  1567. static ssize_t pages_to_scan_store(struct kobject *kobj,
  1568. struct kobj_attribute *attr,
  1569. const char *buf, size_t count)
  1570. {
  1571. int err;
  1572. unsigned long nr_pages;
  1573. err = strict_strtoul(buf, 10, &nr_pages);
  1574. if (err || nr_pages > UINT_MAX)
  1575. return -EINVAL;
  1576. ksm_thread_pages_to_scan = nr_pages;
  1577. return count;
  1578. }
  1579. KSM_ATTR(pages_to_scan);
  1580. static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
  1581. char *buf)
  1582. {
  1583. return sprintf(buf, "%u\n", ksm_run);
  1584. }
  1585. static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
  1586. const char *buf, size_t count)
  1587. {
  1588. int err;
  1589. unsigned long flags;
  1590. err = strict_strtoul(buf, 10, &flags);
  1591. if (err || flags > UINT_MAX)
  1592. return -EINVAL;
  1593. if (flags > KSM_RUN_UNMERGE)
  1594. return -EINVAL;
  1595. /*
  1596. * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
  1597. * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
  1598. * breaking COW to free the pages_shared (but leaves mm_slots
  1599. * on the list for when ksmd may be set running again).
  1600. */
  1601. mutex_lock(&ksm_thread_mutex);
  1602. if (ksm_run != flags) {
  1603. ksm_run = flags;
  1604. if (flags & KSM_RUN_UNMERGE) {
  1605. current->flags |= PF_OOM_ORIGIN;
  1606. err = unmerge_and_remove_all_rmap_items();
  1607. current->flags &= ~PF_OOM_ORIGIN;
  1608. if (err) {
  1609. ksm_run = KSM_RUN_STOP;
  1610. count = err;
  1611. }
  1612. }
  1613. }
  1614. mutex_unlock(&ksm_thread_mutex);
  1615. if (flags & KSM_RUN_MERGE)
  1616. wake_up_interruptible(&ksm_thread_wait);
  1617. return count;
  1618. }
  1619. KSM_ATTR(run);
  1620. static ssize_t pages_shared_show(struct kobject *kobj,
  1621. struct kobj_attribute *attr, char *buf)
  1622. {
  1623. return sprintf(buf, "%lu\n", ksm_pages_shared);
  1624. }
  1625. KSM_ATTR_RO(pages_shared);
  1626. static ssize_t pages_sharing_show(struct kobject *kobj,
  1627. struct kobj_attribute *attr, char *buf)
  1628. {
  1629. return sprintf(buf, "%lu\n", ksm_pages_sharing);
  1630. }
  1631. KSM_ATTR_RO(pages_sharing);
  1632. static ssize_t pages_unshared_show(struct kobject *kobj,
  1633. struct kobj_attribute *attr, char *buf)
  1634. {
  1635. return sprintf(buf, "%lu\n", ksm_pages_unshared);
  1636. }
  1637. KSM_ATTR_RO(pages_unshared);
  1638. static ssize_t pages_volatile_show(struct kobject *kobj,
  1639. struct kobj_attribute *attr, char *buf)
  1640. {
  1641. long ksm_pages_volatile;
  1642. ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
  1643. - ksm_pages_sharing - ksm_pages_unshared;
  1644. /*
  1645. * It was not worth any locking to calculate that statistic,
  1646. * but it might therefore sometimes be negative: conceal that.
  1647. */
  1648. if (ksm_pages_volatile < 0)
  1649. ksm_pages_volatile = 0;
  1650. return sprintf(buf, "%ld\n", ksm_pages_volatile);
  1651. }
  1652. KSM_ATTR_RO(pages_volatile);
  1653. static ssize_t full_scans_show(struct kobject *kobj,
  1654. struct kobj_attribute *attr, char *buf)
  1655. {
  1656. return sprintf(buf, "%lu\n", ksm_scan.seqnr);
  1657. }
  1658. KSM_ATTR_RO(full_scans);
  1659. static struct attribute *ksm_attrs[] = {
  1660. &sleep_millisecs_attr.attr,
  1661. &pages_to_scan_attr.attr,
  1662. &run_attr.attr,
  1663. &pages_shared_attr.attr,
  1664. &pages_sharing_attr.attr,
  1665. &pages_unshared_attr.attr,
  1666. &pages_volatile_attr.attr,
  1667. &full_scans_attr.attr,
  1668. NULL,
  1669. };
  1670. static struct attribute_group ksm_attr_group = {
  1671. .attrs = ksm_attrs,
  1672. .name = "ksm",
  1673. };
  1674. #endif /* CONFIG_SYSFS */
  1675. static int __init ksm_init(void)
  1676. {
  1677. struct task_struct *ksm_thread;
  1678. int err;
  1679. err = ksm_slab_init();
  1680. if (err)
  1681. goto out;
  1682. ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
  1683. if (IS_ERR(ksm_thread)) {
  1684. printk(KERN_ERR "ksm: creating kthread failed\n");
  1685. err = PTR_ERR(ksm_thread);
  1686. goto out_free;
  1687. }
  1688. #ifdef CONFIG_SYSFS
  1689. err = sysfs_create_group(mm_kobj, &ksm_attr_group);
  1690. if (err) {
  1691. printk(KERN_ERR "ksm: register sysfs failed\n");
  1692. kthread_stop(ksm_thread);
  1693. goto out_free;
  1694. }
  1695. #else
  1696. ksm_run = KSM_RUN_MERGE; /* no way for user to start it */
  1697. #endif /* CONFIG_SYSFS */
  1698. #ifdef CONFIG_MEMORY_HOTREMOVE
  1699. /*
  1700. * Choose a high priority since the callback takes ksm_thread_mutex:
  1701. * later callbacks could only be taking locks which nest within that.
  1702. */
  1703. hotplug_memory_notifier(ksm_memory_callback, 100);
  1704. #endif
  1705. return 0;
  1706. out_free:
  1707. ksm_slab_free();
  1708. out:
  1709. return err;
  1710. }
  1711. module_init(ksm_init)