ksm.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  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) || pte_dirty(*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. if (pte_dirty(entry))
  656. set_page_dirty(page);
  657. entry = pte_mkclean(pte_wrprotect(entry));
  658. set_pte_at_notify(mm, addr, ptep, entry);
  659. }
  660. *orig_pte = *ptep;
  661. err = 0;
  662. out_unlock:
  663. pte_unmap_unlock(ptep, ptl);
  664. out:
  665. return err;
  666. }
  667. /**
  668. * replace_page - replace page in vma by new ksm page
  669. * @vma: vma that holds the pte pointing to page
  670. * @page: the page we are replacing by kpage
  671. * @kpage: the ksm page we replace page by
  672. * @orig_pte: the original value of the pte
  673. *
  674. * Returns 0 on success, -EFAULT on failure.
  675. */
  676. static int replace_page(struct vm_area_struct *vma, struct page *page,
  677. struct page *kpage, pte_t orig_pte)
  678. {
  679. struct mm_struct *mm = vma->vm_mm;
  680. pgd_t *pgd;
  681. pud_t *pud;
  682. pmd_t *pmd;
  683. pte_t *ptep;
  684. spinlock_t *ptl;
  685. unsigned long addr;
  686. int err = -EFAULT;
  687. addr = page_address_in_vma(page, vma);
  688. if (addr == -EFAULT)
  689. goto out;
  690. pgd = pgd_offset(mm, addr);
  691. if (!pgd_present(*pgd))
  692. goto out;
  693. pud = pud_offset(pgd, addr);
  694. if (!pud_present(*pud))
  695. goto out;
  696. pmd = pmd_offset(pud, addr);
  697. if (!pmd_present(*pmd))
  698. goto out;
  699. ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
  700. if (!pte_same(*ptep, orig_pte)) {
  701. pte_unmap_unlock(ptep, ptl);
  702. goto out;
  703. }
  704. get_page(kpage);
  705. page_add_anon_rmap(kpage, vma, addr);
  706. flush_cache_page(vma, addr, pte_pfn(*ptep));
  707. ptep_clear_flush(vma, addr, ptep);
  708. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  709. page_remove_rmap(page);
  710. put_page(page);
  711. pte_unmap_unlock(ptep, ptl);
  712. err = 0;
  713. out:
  714. return err;
  715. }
  716. /*
  717. * try_to_merge_one_page - take two pages and merge them into one
  718. * @vma: the vma that holds the pte pointing to page
  719. * @page: the PageAnon page that we want to replace with kpage
  720. * @kpage: the PageKsm page that we want to map instead of page,
  721. * or NULL the first time when we want to use page as kpage.
  722. *
  723. * This function returns 0 if the pages were merged, -EFAULT otherwise.
  724. */
  725. static int try_to_merge_one_page(struct vm_area_struct *vma,
  726. struct page *page, struct page *kpage)
  727. {
  728. pte_t orig_pte = __pte(0);
  729. int err = -EFAULT;
  730. if (page == kpage) /* ksm page forked */
  731. return 0;
  732. if (!(vma->vm_flags & VM_MERGEABLE))
  733. goto out;
  734. if (!PageAnon(page))
  735. goto out;
  736. /*
  737. * We need the page lock to read a stable PageSwapCache in
  738. * write_protect_page(). We use trylock_page() instead of
  739. * lock_page() because we don't want to wait here - we
  740. * prefer to continue scanning and merging different pages,
  741. * then come back to this page when it is unlocked.
  742. */
  743. if (!trylock_page(page))
  744. goto out;
  745. /*
  746. * If this anonymous page is mapped only here, its pte may need
  747. * to be write-protected. If it's mapped elsewhere, all of its
  748. * ptes are necessarily already write-protected. But in either
  749. * case, we need to lock and check page_count is not raised.
  750. */
  751. if (write_protect_page(vma, page, &orig_pte) == 0) {
  752. if (!kpage) {
  753. /*
  754. * While we hold page lock, upgrade page from
  755. * PageAnon+anon_vma to PageKsm+NULL stable_node:
  756. * stable_tree_insert() will update stable_node.
  757. */
  758. set_page_stable_node(page, NULL);
  759. mark_page_accessed(page);
  760. err = 0;
  761. } else if (pages_identical(page, kpage))
  762. err = replace_page(vma, page, kpage, orig_pte);
  763. }
  764. if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
  765. munlock_vma_page(page);
  766. if (!PageMlocked(kpage)) {
  767. unlock_page(page);
  768. lock_page(kpage);
  769. mlock_vma_page(kpage);
  770. page = kpage; /* for final unlock */
  771. }
  772. }
  773. unlock_page(page);
  774. out:
  775. return err;
  776. }
  777. /*
  778. * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
  779. * but no new kernel page is allocated: kpage must already be a ksm page.
  780. *
  781. * This function returns 0 if the pages were merged, -EFAULT otherwise.
  782. */
  783. static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
  784. struct page *page, struct page *kpage)
  785. {
  786. struct mm_struct *mm = rmap_item->mm;
  787. struct vm_area_struct *vma;
  788. int err = -EFAULT;
  789. down_read(&mm->mmap_sem);
  790. if (ksm_test_exit(mm))
  791. goto out;
  792. vma = find_vma(mm, rmap_item->address);
  793. if (!vma || vma->vm_start > rmap_item->address)
  794. goto out;
  795. err = try_to_merge_one_page(vma, page, kpage);
  796. if (err)
  797. goto out;
  798. /* Must get reference to anon_vma while still holding mmap_sem */
  799. hold_anon_vma(rmap_item, vma->anon_vma);
  800. out:
  801. up_read(&mm->mmap_sem);
  802. return err;
  803. }
  804. /*
  805. * try_to_merge_two_pages - take two identical pages and prepare them
  806. * to be merged into one page.
  807. *
  808. * This function returns the kpage if we successfully merged two identical
  809. * pages into one ksm page, NULL otherwise.
  810. *
  811. * Note that this function upgrades page to ksm page: if one of the pages
  812. * is already a ksm page, try_to_merge_with_ksm_page should be used.
  813. */
  814. static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
  815. struct page *page,
  816. struct rmap_item *tree_rmap_item,
  817. struct page *tree_page)
  818. {
  819. int err;
  820. err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
  821. if (!err) {
  822. err = try_to_merge_with_ksm_page(tree_rmap_item,
  823. tree_page, page);
  824. /*
  825. * If that fails, we have a ksm page with only one pte
  826. * pointing to it: so break it.
  827. */
  828. if (err)
  829. break_cow(rmap_item);
  830. }
  831. return err ? NULL : page;
  832. }
  833. /*
  834. * stable_tree_search - search for page inside the stable tree
  835. *
  836. * This function checks if there is a page inside the stable tree
  837. * with identical content to the page that we are scanning right now.
  838. *
  839. * This function returns the stable tree node of identical content if found,
  840. * NULL otherwise.
  841. */
  842. static struct page *stable_tree_search(struct page *page)
  843. {
  844. struct rb_node *node = root_stable_tree.rb_node;
  845. struct stable_node *stable_node;
  846. stable_node = page_stable_node(page);
  847. if (stable_node) { /* ksm page forked */
  848. get_page(page);
  849. return page;
  850. }
  851. while (node) {
  852. struct page *tree_page;
  853. int ret;
  854. cond_resched();
  855. stable_node = rb_entry(node, struct stable_node, node);
  856. tree_page = get_ksm_page(stable_node);
  857. if (!tree_page)
  858. return NULL;
  859. ret = memcmp_pages(page, tree_page);
  860. if (ret < 0) {
  861. put_page(tree_page);
  862. node = node->rb_left;
  863. } else if (ret > 0) {
  864. put_page(tree_page);
  865. node = node->rb_right;
  866. } else
  867. return tree_page;
  868. }
  869. return NULL;
  870. }
  871. /*
  872. * stable_tree_insert - insert rmap_item pointing to new ksm page
  873. * into the stable tree.
  874. *
  875. * This function returns the stable tree node just allocated on success,
  876. * NULL otherwise.
  877. */
  878. static struct stable_node *stable_tree_insert(struct page *kpage)
  879. {
  880. struct rb_node **new = &root_stable_tree.rb_node;
  881. struct rb_node *parent = NULL;
  882. struct stable_node *stable_node;
  883. while (*new) {
  884. struct page *tree_page;
  885. int ret;
  886. cond_resched();
  887. stable_node = rb_entry(*new, struct stable_node, node);
  888. tree_page = get_ksm_page(stable_node);
  889. if (!tree_page)
  890. return NULL;
  891. ret = memcmp_pages(kpage, tree_page);
  892. put_page(tree_page);
  893. parent = *new;
  894. if (ret < 0)
  895. new = &parent->rb_left;
  896. else if (ret > 0)
  897. new = &parent->rb_right;
  898. else {
  899. /*
  900. * It is not a bug that stable_tree_search() didn't
  901. * find this node: because at that time our page was
  902. * not yet write-protected, so may have changed since.
  903. */
  904. return NULL;
  905. }
  906. }
  907. stable_node = alloc_stable_node();
  908. if (!stable_node)
  909. return NULL;
  910. rb_link_node(&stable_node->node, parent, new);
  911. rb_insert_color(&stable_node->node, &root_stable_tree);
  912. INIT_HLIST_HEAD(&stable_node->hlist);
  913. stable_node->kpfn = page_to_pfn(kpage);
  914. set_page_stable_node(kpage, stable_node);
  915. return stable_node;
  916. }
  917. /*
  918. * unstable_tree_search_insert - search for identical page,
  919. * else insert rmap_item into the unstable tree.
  920. *
  921. * This function searches for a page in the unstable tree identical to the
  922. * page currently being scanned; and if no identical page is found in the
  923. * tree, we insert rmap_item as a new object into the unstable tree.
  924. *
  925. * This function returns pointer to rmap_item found to be identical
  926. * to the currently scanned page, NULL otherwise.
  927. *
  928. * This function does both searching and inserting, because they share
  929. * the same walking algorithm in an rbtree.
  930. */
  931. static
  932. struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
  933. struct page *page,
  934. struct page **tree_pagep)
  935. {
  936. struct rb_node **new = &root_unstable_tree.rb_node;
  937. struct rb_node *parent = NULL;
  938. while (*new) {
  939. struct rmap_item *tree_rmap_item;
  940. struct page *tree_page;
  941. int ret;
  942. cond_resched();
  943. tree_rmap_item = rb_entry(*new, struct rmap_item, node);
  944. tree_page = get_mergeable_page(tree_rmap_item);
  945. if (IS_ERR_OR_NULL(tree_page))
  946. return NULL;
  947. /*
  948. * Don't substitute a ksm page for a forked page.
  949. */
  950. if (page == tree_page) {
  951. put_page(tree_page);
  952. return NULL;
  953. }
  954. ret = memcmp_pages(page, tree_page);
  955. parent = *new;
  956. if (ret < 0) {
  957. put_page(tree_page);
  958. new = &parent->rb_left;
  959. } else if (ret > 0) {
  960. put_page(tree_page);
  961. new = &parent->rb_right;
  962. } else {
  963. *tree_pagep = tree_page;
  964. return tree_rmap_item;
  965. }
  966. }
  967. rmap_item->address |= UNSTABLE_FLAG;
  968. rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
  969. rb_link_node(&rmap_item->node, parent, new);
  970. rb_insert_color(&rmap_item->node, &root_unstable_tree);
  971. ksm_pages_unshared++;
  972. return NULL;
  973. }
  974. /*
  975. * stable_tree_append - add another rmap_item to the linked list of
  976. * rmap_items hanging off a given node of the stable tree, all sharing
  977. * the same ksm page.
  978. */
  979. static void stable_tree_append(struct rmap_item *rmap_item,
  980. struct stable_node *stable_node)
  981. {
  982. rmap_item->head = stable_node;
  983. rmap_item->address |= STABLE_FLAG;
  984. hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
  985. if (rmap_item->hlist.next)
  986. ksm_pages_sharing++;
  987. else
  988. ksm_pages_shared++;
  989. }
  990. /*
  991. * cmp_and_merge_page - first see if page can be merged into the stable tree;
  992. * if not, compare checksum to previous and if it's the same, see if page can
  993. * be inserted into the unstable tree, or merged with a page already there and
  994. * both transferred to the stable tree.
  995. *
  996. * @page: the page that we are searching identical page to.
  997. * @rmap_item: the reverse mapping into the virtual address of this page
  998. */
  999. static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
  1000. {
  1001. struct rmap_item *tree_rmap_item;
  1002. struct page *tree_page = NULL;
  1003. struct stable_node *stable_node;
  1004. struct page *kpage;
  1005. unsigned int checksum;
  1006. int err;
  1007. remove_rmap_item_from_tree(rmap_item);
  1008. /* We first start with searching the page inside the stable tree */
  1009. kpage = stable_tree_search(page);
  1010. if (kpage) {
  1011. err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
  1012. if (!err) {
  1013. /*
  1014. * The page was successfully merged:
  1015. * add its rmap_item to the stable tree.
  1016. */
  1017. lock_page(kpage);
  1018. stable_tree_append(rmap_item, page_stable_node(kpage));
  1019. unlock_page(kpage);
  1020. }
  1021. put_page(kpage);
  1022. return;
  1023. }
  1024. /*
  1025. * If the hash value of the page has changed from the last time
  1026. * we calculated it, this page is changing frequently: therefore we
  1027. * don't want to insert it in the unstable tree, and we don't want
  1028. * to waste our time searching for something identical to it there.
  1029. */
  1030. checksum = calc_checksum(page);
  1031. if (rmap_item->oldchecksum != checksum) {
  1032. rmap_item->oldchecksum = checksum;
  1033. return;
  1034. }
  1035. tree_rmap_item =
  1036. unstable_tree_search_insert(rmap_item, page, &tree_page);
  1037. if (tree_rmap_item) {
  1038. kpage = try_to_merge_two_pages(rmap_item, page,
  1039. tree_rmap_item, tree_page);
  1040. put_page(tree_page);
  1041. /*
  1042. * As soon as we merge this page, we want to remove the
  1043. * rmap_item of the page we have merged with from the unstable
  1044. * tree, and insert it instead as new node in the stable tree.
  1045. */
  1046. if (kpage) {
  1047. remove_rmap_item_from_tree(tree_rmap_item);
  1048. lock_page(kpage);
  1049. stable_node = stable_tree_insert(kpage);
  1050. if (stable_node) {
  1051. stable_tree_append(tree_rmap_item, stable_node);
  1052. stable_tree_append(rmap_item, stable_node);
  1053. }
  1054. unlock_page(kpage);
  1055. /*
  1056. * If we fail to insert the page into the stable tree,
  1057. * we will have 2 virtual addresses that are pointing
  1058. * to a ksm page left outside the stable tree,
  1059. * in which case we need to break_cow on both.
  1060. */
  1061. if (!stable_node) {
  1062. break_cow(tree_rmap_item);
  1063. break_cow(rmap_item);
  1064. }
  1065. }
  1066. }
  1067. }
  1068. static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
  1069. struct rmap_item **rmap_list,
  1070. unsigned long addr)
  1071. {
  1072. struct rmap_item *rmap_item;
  1073. while (*rmap_list) {
  1074. rmap_item = *rmap_list;
  1075. if ((rmap_item->address & PAGE_MASK) == addr)
  1076. return rmap_item;
  1077. if (rmap_item->address > addr)
  1078. break;
  1079. *rmap_list = rmap_item->rmap_list;
  1080. remove_rmap_item_from_tree(rmap_item);
  1081. free_rmap_item(rmap_item);
  1082. }
  1083. rmap_item = alloc_rmap_item();
  1084. if (rmap_item) {
  1085. /* It has already been zeroed */
  1086. rmap_item->mm = mm_slot->mm;
  1087. rmap_item->address = addr;
  1088. rmap_item->rmap_list = *rmap_list;
  1089. *rmap_list = rmap_item;
  1090. }
  1091. return rmap_item;
  1092. }
  1093. static struct rmap_item *scan_get_next_rmap_item(struct page **page)
  1094. {
  1095. struct mm_struct *mm;
  1096. struct mm_slot *slot;
  1097. struct vm_area_struct *vma;
  1098. struct rmap_item *rmap_item;
  1099. if (list_empty(&ksm_mm_head.mm_list))
  1100. return NULL;
  1101. slot = ksm_scan.mm_slot;
  1102. if (slot == &ksm_mm_head) {
  1103. root_unstable_tree = RB_ROOT;
  1104. spin_lock(&ksm_mmlist_lock);
  1105. slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
  1106. ksm_scan.mm_slot = slot;
  1107. spin_unlock(&ksm_mmlist_lock);
  1108. next_mm:
  1109. ksm_scan.address = 0;
  1110. ksm_scan.rmap_list = &slot->rmap_list;
  1111. }
  1112. mm = slot->mm;
  1113. down_read(&mm->mmap_sem);
  1114. if (ksm_test_exit(mm))
  1115. vma = NULL;
  1116. else
  1117. vma = find_vma(mm, ksm_scan.address);
  1118. for (; vma; vma = vma->vm_next) {
  1119. if (!(vma->vm_flags & VM_MERGEABLE))
  1120. continue;
  1121. if (ksm_scan.address < vma->vm_start)
  1122. ksm_scan.address = vma->vm_start;
  1123. if (!vma->anon_vma)
  1124. ksm_scan.address = vma->vm_end;
  1125. while (ksm_scan.address < vma->vm_end) {
  1126. if (ksm_test_exit(mm))
  1127. break;
  1128. *page = follow_page(vma, ksm_scan.address, FOLL_GET);
  1129. if (!IS_ERR_OR_NULL(*page) && PageAnon(*page)) {
  1130. flush_anon_page(vma, *page, ksm_scan.address);
  1131. flush_dcache_page(*page);
  1132. rmap_item = get_next_rmap_item(slot,
  1133. ksm_scan.rmap_list, ksm_scan.address);
  1134. if (rmap_item) {
  1135. ksm_scan.rmap_list =
  1136. &rmap_item->rmap_list;
  1137. ksm_scan.address += PAGE_SIZE;
  1138. } else
  1139. put_page(*page);
  1140. up_read(&mm->mmap_sem);
  1141. return rmap_item;
  1142. }
  1143. if (!IS_ERR_OR_NULL(*page))
  1144. put_page(*page);
  1145. ksm_scan.address += PAGE_SIZE;
  1146. cond_resched();
  1147. }
  1148. }
  1149. if (ksm_test_exit(mm)) {
  1150. ksm_scan.address = 0;
  1151. ksm_scan.rmap_list = &slot->rmap_list;
  1152. }
  1153. /*
  1154. * Nuke all the rmap_items that are above this current rmap:
  1155. * because there were no VM_MERGEABLE vmas with such addresses.
  1156. */
  1157. remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
  1158. spin_lock(&ksm_mmlist_lock);
  1159. ksm_scan.mm_slot = list_entry(slot->mm_list.next,
  1160. struct mm_slot, mm_list);
  1161. if (ksm_scan.address == 0) {
  1162. /*
  1163. * We've completed a full scan of all vmas, holding mmap_sem
  1164. * throughout, and found no VM_MERGEABLE: so do the same as
  1165. * __ksm_exit does to remove this mm from all our lists now.
  1166. * This applies either when cleaning up after __ksm_exit
  1167. * (but beware: we can reach here even before __ksm_exit),
  1168. * or when all VM_MERGEABLE areas have been unmapped (and
  1169. * mmap_sem then protects against race with MADV_MERGEABLE).
  1170. */
  1171. hlist_del(&slot->link);
  1172. list_del(&slot->mm_list);
  1173. spin_unlock(&ksm_mmlist_lock);
  1174. free_mm_slot(slot);
  1175. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  1176. up_read(&mm->mmap_sem);
  1177. mmdrop(mm);
  1178. } else {
  1179. spin_unlock(&ksm_mmlist_lock);
  1180. up_read(&mm->mmap_sem);
  1181. }
  1182. /* Repeat until we've completed scanning the whole list */
  1183. slot = ksm_scan.mm_slot;
  1184. if (slot != &ksm_mm_head)
  1185. goto next_mm;
  1186. ksm_scan.seqnr++;
  1187. return NULL;
  1188. }
  1189. /**
  1190. * ksm_do_scan - the ksm scanner main worker function.
  1191. * @scan_npages - number of pages we want to scan before we return.
  1192. */
  1193. static void ksm_do_scan(unsigned int scan_npages)
  1194. {
  1195. struct rmap_item *rmap_item;
  1196. struct page *uninitialized_var(page);
  1197. while (scan_npages--) {
  1198. cond_resched();
  1199. rmap_item = scan_get_next_rmap_item(&page);
  1200. if (!rmap_item)
  1201. return;
  1202. if (!PageKsm(page) || !in_stable_tree(rmap_item))
  1203. cmp_and_merge_page(page, rmap_item);
  1204. put_page(page);
  1205. }
  1206. }
  1207. static int ksmd_should_run(void)
  1208. {
  1209. return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
  1210. }
  1211. static int ksm_scan_thread(void *nothing)
  1212. {
  1213. set_user_nice(current, 5);
  1214. while (!kthread_should_stop()) {
  1215. mutex_lock(&ksm_thread_mutex);
  1216. if (ksmd_should_run())
  1217. ksm_do_scan(ksm_thread_pages_to_scan);
  1218. mutex_unlock(&ksm_thread_mutex);
  1219. if (ksmd_should_run()) {
  1220. schedule_timeout_interruptible(
  1221. msecs_to_jiffies(ksm_thread_sleep_millisecs));
  1222. } else {
  1223. wait_event_interruptible(ksm_thread_wait,
  1224. ksmd_should_run() || kthread_should_stop());
  1225. }
  1226. }
  1227. return 0;
  1228. }
  1229. int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  1230. unsigned long end, int advice, unsigned long *vm_flags)
  1231. {
  1232. struct mm_struct *mm = vma->vm_mm;
  1233. int err;
  1234. switch (advice) {
  1235. case MADV_MERGEABLE:
  1236. /*
  1237. * Be somewhat over-protective for now!
  1238. */
  1239. if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
  1240. VM_PFNMAP | VM_IO | VM_DONTEXPAND |
  1241. VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE |
  1242. VM_NONLINEAR | VM_MIXEDMAP | VM_SAO))
  1243. return 0; /* just ignore the advice */
  1244. if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
  1245. err = __ksm_enter(mm);
  1246. if (err)
  1247. return err;
  1248. }
  1249. *vm_flags |= VM_MERGEABLE;
  1250. break;
  1251. case MADV_UNMERGEABLE:
  1252. if (!(*vm_flags & VM_MERGEABLE))
  1253. return 0; /* just ignore the advice */
  1254. if (vma->anon_vma) {
  1255. err = unmerge_ksm_pages(vma, start, end);
  1256. if (err)
  1257. return err;
  1258. }
  1259. *vm_flags &= ~VM_MERGEABLE;
  1260. break;
  1261. }
  1262. return 0;
  1263. }
  1264. int __ksm_enter(struct mm_struct *mm)
  1265. {
  1266. struct mm_slot *mm_slot;
  1267. int needs_wakeup;
  1268. mm_slot = alloc_mm_slot();
  1269. if (!mm_slot)
  1270. return -ENOMEM;
  1271. /* Check ksm_run too? Would need tighter locking */
  1272. needs_wakeup = list_empty(&ksm_mm_head.mm_list);
  1273. spin_lock(&ksm_mmlist_lock);
  1274. insert_to_mm_slots_hash(mm, mm_slot);
  1275. /*
  1276. * Insert just behind the scanning cursor, to let the area settle
  1277. * down a little; when fork is followed by immediate exec, we don't
  1278. * want ksmd to waste time setting up and tearing down an rmap_list.
  1279. */
  1280. list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
  1281. spin_unlock(&ksm_mmlist_lock);
  1282. set_bit(MMF_VM_MERGEABLE, &mm->flags);
  1283. atomic_inc(&mm->mm_count);
  1284. if (needs_wakeup)
  1285. wake_up_interruptible(&ksm_thread_wait);
  1286. return 0;
  1287. }
  1288. void __ksm_exit(struct mm_struct *mm)
  1289. {
  1290. struct mm_slot *mm_slot;
  1291. int easy_to_free = 0;
  1292. /*
  1293. * This process is exiting: if it's straightforward (as is the
  1294. * case when ksmd was never running), free mm_slot immediately.
  1295. * But if it's at the cursor or has rmap_items linked to it, use
  1296. * mmap_sem to synchronize with any break_cows before pagetables
  1297. * are freed, and leave the mm_slot on the list for ksmd to free.
  1298. * Beware: ksm may already have noticed it exiting and freed the slot.
  1299. */
  1300. spin_lock(&ksm_mmlist_lock);
  1301. mm_slot = get_mm_slot(mm);
  1302. if (mm_slot && ksm_scan.mm_slot != mm_slot) {
  1303. if (!mm_slot->rmap_list) {
  1304. hlist_del(&mm_slot->link);
  1305. list_del(&mm_slot->mm_list);
  1306. easy_to_free = 1;
  1307. } else {
  1308. list_move(&mm_slot->mm_list,
  1309. &ksm_scan.mm_slot->mm_list);
  1310. }
  1311. }
  1312. spin_unlock(&ksm_mmlist_lock);
  1313. if (easy_to_free) {
  1314. free_mm_slot(mm_slot);
  1315. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  1316. mmdrop(mm);
  1317. } else if (mm_slot) {
  1318. down_write(&mm->mmap_sem);
  1319. up_write(&mm->mmap_sem);
  1320. }
  1321. }
  1322. struct page *ksm_does_need_to_copy(struct page *page,
  1323. struct vm_area_struct *vma, unsigned long address)
  1324. {
  1325. struct page *new_page;
  1326. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  1327. if (new_page) {
  1328. copy_user_highpage(new_page, page, address, vma);
  1329. SetPageDirty(new_page);
  1330. __SetPageUptodate(new_page);
  1331. SetPageSwapBacked(new_page);
  1332. __set_page_locked(new_page);
  1333. if (page_evictable(new_page, vma))
  1334. lru_cache_add_lru(new_page, LRU_ACTIVE_ANON);
  1335. else
  1336. add_page_to_unevictable_list(new_page);
  1337. }
  1338. return new_page;
  1339. }
  1340. int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg,
  1341. unsigned long *vm_flags)
  1342. {
  1343. struct stable_node *stable_node;
  1344. struct rmap_item *rmap_item;
  1345. struct hlist_node *hlist;
  1346. unsigned int mapcount = page_mapcount(page);
  1347. int referenced = 0;
  1348. int search_new_forks = 0;
  1349. VM_BUG_ON(!PageKsm(page));
  1350. VM_BUG_ON(!PageLocked(page));
  1351. stable_node = page_stable_node(page);
  1352. if (!stable_node)
  1353. return 0;
  1354. again:
  1355. hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
  1356. struct anon_vma *anon_vma = rmap_item->anon_vma;
  1357. struct anon_vma_chain *vmac;
  1358. struct vm_area_struct *vma;
  1359. anon_vma_lock(anon_vma);
  1360. list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
  1361. vma = vmac->vma;
  1362. if (rmap_item->address < vma->vm_start ||
  1363. rmap_item->address >= vma->vm_end)
  1364. continue;
  1365. /*
  1366. * Initially we examine only the vma which covers this
  1367. * rmap_item; but later, if there is still work to do,
  1368. * we examine covering vmas in other mms: in case they
  1369. * were forked from the original since ksmd passed.
  1370. */
  1371. if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
  1372. continue;
  1373. if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
  1374. continue;
  1375. referenced += page_referenced_one(page, vma,
  1376. rmap_item->address, &mapcount, vm_flags);
  1377. if (!search_new_forks || !mapcount)
  1378. break;
  1379. }
  1380. anon_vma_unlock(anon_vma);
  1381. if (!mapcount)
  1382. goto out;
  1383. }
  1384. if (!search_new_forks++)
  1385. goto again;
  1386. out:
  1387. return referenced;
  1388. }
  1389. int try_to_unmap_ksm(struct page *page, enum ttu_flags flags)
  1390. {
  1391. struct stable_node *stable_node;
  1392. struct hlist_node *hlist;
  1393. struct rmap_item *rmap_item;
  1394. int ret = SWAP_AGAIN;
  1395. int search_new_forks = 0;
  1396. VM_BUG_ON(!PageKsm(page));
  1397. VM_BUG_ON(!PageLocked(page));
  1398. stable_node = page_stable_node(page);
  1399. if (!stable_node)
  1400. return SWAP_FAIL;
  1401. again:
  1402. hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
  1403. struct anon_vma *anon_vma = rmap_item->anon_vma;
  1404. struct anon_vma_chain *vmac;
  1405. struct vm_area_struct *vma;
  1406. anon_vma_lock(anon_vma);
  1407. list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
  1408. vma = vmac->vma;
  1409. if (rmap_item->address < vma->vm_start ||
  1410. rmap_item->address >= vma->vm_end)
  1411. continue;
  1412. /*
  1413. * Initially we examine only the vma which covers this
  1414. * rmap_item; but later, if there is still work to do,
  1415. * we examine covering vmas in other mms: in case they
  1416. * were forked from the original since ksmd passed.
  1417. */
  1418. if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
  1419. continue;
  1420. ret = try_to_unmap_one(page, vma,
  1421. rmap_item->address, flags);
  1422. if (ret != SWAP_AGAIN || !page_mapped(page)) {
  1423. anon_vma_unlock(anon_vma);
  1424. goto out;
  1425. }
  1426. }
  1427. anon_vma_unlock(anon_vma);
  1428. }
  1429. if (!search_new_forks++)
  1430. goto again;
  1431. out:
  1432. return ret;
  1433. }
  1434. #ifdef CONFIG_MIGRATION
  1435. int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *,
  1436. struct vm_area_struct *, unsigned long, void *), void *arg)
  1437. {
  1438. struct stable_node *stable_node;
  1439. struct hlist_node *hlist;
  1440. struct rmap_item *rmap_item;
  1441. int ret = SWAP_AGAIN;
  1442. int search_new_forks = 0;
  1443. VM_BUG_ON(!PageKsm(page));
  1444. VM_BUG_ON(!PageLocked(page));
  1445. stable_node = page_stable_node(page);
  1446. if (!stable_node)
  1447. return ret;
  1448. again:
  1449. hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) {
  1450. struct anon_vma *anon_vma = rmap_item->anon_vma;
  1451. struct anon_vma_chain *vmac;
  1452. struct vm_area_struct *vma;
  1453. anon_vma_lock(anon_vma);
  1454. list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) {
  1455. vma = vmac->vma;
  1456. if (rmap_item->address < vma->vm_start ||
  1457. rmap_item->address >= vma->vm_end)
  1458. continue;
  1459. /*
  1460. * Initially we examine only the vma which covers this
  1461. * rmap_item; but later, if there is still work to do,
  1462. * we examine covering vmas in other mms: in case they
  1463. * were forked from the original since ksmd passed.
  1464. */
  1465. if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
  1466. continue;
  1467. ret = rmap_one(page, vma, rmap_item->address, arg);
  1468. if (ret != SWAP_AGAIN) {
  1469. anon_vma_unlock(anon_vma);
  1470. goto out;
  1471. }
  1472. }
  1473. anon_vma_unlock(anon_vma);
  1474. }
  1475. if (!search_new_forks++)
  1476. goto again;
  1477. out:
  1478. return ret;
  1479. }
  1480. void ksm_migrate_page(struct page *newpage, struct page *oldpage)
  1481. {
  1482. struct stable_node *stable_node;
  1483. VM_BUG_ON(!PageLocked(oldpage));
  1484. VM_BUG_ON(!PageLocked(newpage));
  1485. VM_BUG_ON(newpage->mapping != oldpage->mapping);
  1486. stable_node = page_stable_node(newpage);
  1487. if (stable_node) {
  1488. VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage));
  1489. stable_node->kpfn = page_to_pfn(newpage);
  1490. }
  1491. }
  1492. #endif /* CONFIG_MIGRATION */
  1493. #ifdef CONFIG_MEMORY_HOTREMOVE
  1494. static struct stable_node *ksm_check_stable_tree(unsigned long start_pfn,
  1495. unsigned long end_pfn)
  1496. {
  1497. struct rb_node *node;
  1498. for (node = rb_first(&root_stable_tree); node; node = rb_next(node)) {
  1499. struct stable_node *stable_node;
  1500. stable_node = rb_entry(node, struct stable_node, node);
  1501. if (stable_node->kpfn >= start_pfn &&
  1502. stable_node->kpfn < end_pfn)
  1503. return stable_node;
  1504. }
  1505. return NULL;
  1506. }
  1507. static int ksm_memory_callback(struct notifier_block *self,
  1508. unsigned long action, void *arg)
  1509. {
  1510. struct memory_notify *mn = arg;
  1511. struct stable_node *stable_node;
  1512. switch (action) {
  1513. case MEM_GOING_OFFLINE:
  1514. /*
  1515. * Keep it very simple for now: just lock out ksmd and
  1516. * MADV_UNMERGEABLE while any memory is going offline.
  1517. */
  1518. mutex_lock(&ksm_thread_mutex);
  1519. break;
  1520. case MEM_OFFLINE:
  1521. /*
  1522. * Most of the work is done by page migration; but there might
  1523. * be a few stable_nodes left over, still pointing to struct
  1524. * pages which have been offlined: prune those from the tree.
  1525. */
  1526. while ((stable_node = ksm_check_stable_tree(mn->start_pfn,
  1527. mn->start_pfn + mn->nr_pages)) != NULL)
  1528. remove_node_from_stable_tree(stable_node);
  1529. /* fallthrough */
  1530. case MEM_CANCEL_OFFLINE:
  1531. mutex_unlock(&ksm_thread_mutex);
  1532. break;
  1533. }
  1534. return NOTIFY_OK;
  1535. }
  1536. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1537. #ifdef CONFIG_SYSFS
  1538. /*
  1539. * This all compiles without CONFIG_SYSFS, but is a waste of space.
  1540. */
  1541. #define KSM_ATTR_RO(_name) \
  1542. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1543. #define KSM_ATTR(_name) \
  1544. static struct kobj_attribute _name##_attr = \
  1545. __ATTR(_name, 0644, _name##_show, _name##_store)
  1546. static ssize_t sleep_millisecs_show(struct kobject *kobj,
  1547. struct kobj_attribute *attr, char *buf)
  1548. {
  1549. return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
  1550. }
  1551. static ssize_t sleep_millisecs_store(struct kobject *kobj,
  1552. struct kobj_attribute *attr,
  1553. const char *buf, size_t count)
  1554. {
  1555. unsigned long msecs;
  1556. int err;
  1557. err = strict_strtoul(buf, 10, &msecs);
  1558. if (err || msecs > UINT_MAX)
  1559. return -EINVAL;
  1560. ksm_thread_sleep_millisecs = msecs;
  1561. return count;
  1562. }
  1563. KSM_ATTR(sleep_millisecs);
  1564. static ssize_t pages_to_scan_show(struct kobject *kobj,
  1565. struct kobj_attribute *attr, char *buf)
  1566. {
  1567. return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
  1568. }
  1569. static ssize_t pages_to_scan_store(struct kobject *kobj,
  1570. struct kobj_attribute *attr,
  1571. const char *buf, size_t count)
  1572. {
  1573. int err;
  1574. unsigned long nr_pages;
  1575. err = strict_strtoul(buf, 10, &nr_pages);
  1576. if (err || nr_pages > UINT_MAX)
  1577. return -EINVAL;
  1578. ksm_thread_pages_to_scan = nr_pages;
  1579. return count;
  1580. }
  1581. KSM_ATTR(pages_to_scan);
  1582. static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
  1583. char *buf)
  1584. {
  1585. return sprintf(buf, "%u\n", ksm_run);
  1586. }
  1587. static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
  1588. const char *buf, size_t count)
  1589. {
  1590. int err;
  1591. unsigned long flags;
  1592. err = strict_strtoul(buf, 10, &flags);
  1593. if (err || flags > UINT_MAX)
  1594. return -EINVAL;
  1595. if (flags > KSM_RUN_UNMERGE)
  1596. return -EINVAL;
  1597. /*
  1598. * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
  1599. * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
  1600. * breaking COW to free the pages_shared (but leaves mm_slots
  1601. * on the list for when ksmd may be set running again).
  1602. */
  1603. mutex_lock(&ksm_thread_mutex);
  1604. if (ksm_run != flags) {
  1605. ksm_run = flags;
  1606. if (flags & KSM_RUN_UNMERGE) {
  1607. current->flags |= PF_OOM_ORIGIN;
  1608. err = unmerge_and_remove_all_rmap_items();
  1609. current->flags &= ~PF_OOM_ORIGIN;
  1610. if (err) {
  1611. ksm_run = KSM_RUN_STOP;
  1612. count = err;
  1613. }
  1614. }
  1615. }
  1616. mutex_unlock(&ksm_thread_mutex);
  1617. if (flags & KSM_RUN_MERGE)
  1618. wake_up_interruptible(&ksm_thread_wait);
  1619. return count;
  1620. }
  1621. KSM_ATTR(run);
  1622. static ssize_t pages_shared_show(struct kobject *kobj,
  1623. struct kobj_attribute *attr, char *buf)
  1624. {
  1625. return sprintf(buf, "%lu\n", ksm_pages_shared);
  1626. }
  1627. KSM_ATTR_RO(pages_shared);
  1628. static ssize_t pages_sharing_show(struct kobject *kobj,
  1629. struct kobj_attribute *attr, char *buf)
  1630. {
  1631. return sprintf(buf, "%lu\n", ksm_pages_sharing);
  1632. }
  1633. KSM_ATTR_RO(pages_sharing);
  1634. static ssize_t pages_unshared_show(struct kobject *kobj,
  1635. struct kobj_attribute *attr, char *buf)
  1636. {
  1637. return sprintf(buf, "%lu\n", ksm_pages_unshared);
  1638. }
  1639. KSM_ATTR_RO(pages_unshared);
  1640. static ssize_t pages_volatile_show(struct kobject *kobj,
  1641. struct kobj_attribute *attr, char *buf)
  1642. {
  1643. long ksm_pages_volatile;
  1644. ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
  1645. - ksm_pages_sharing - ksm_pages_unshared;
  1646. /*
  1647. * It was not worth any locking to calculate that statistic,
  1648. * but it might therefore sometimes be negative: conceal that.
  1649. */
  1650. if (ksm_pages_volatile < 0)
  1651. ksm_pages_volatile = 0;
  1652. return sprintf(buf, "%ld\n", ksm_pages_volatile);
  1653. }
  1654. KSM_ATTR_RO(pages_volatile);
  1655. static ssize_t full_scans_show(struct kobject *kobj,
  1656. struct kobj_attribute *attr, char *buf)
  1657. {
  1658. return sprintf(buf, "%lu\n", ksm_scan.seqnr);
  1659. }
  1660. KSM_ATTR_RO(full_scans);
  1661. static struct attribute *ksm_attrs[] = {
  1662. &sleep_millisecs_attr.attr,
  1663. &pages_to_scan_attr.attr,
  1664. &run_attr.attr,
  1665. &pages_shared_attr.attr,
  1666. &pages_sharing_attr.attr,
  1667. &pages_unshared_attr.attr,
  1668. &pages_volatile_attr.attr,
  1669. &full_scans_attr.attr,
  1670. NULL,
  1671. };
  1672. static struct attribute_group ksm_attr_group = {
  1673. .attrs = ksm_attrs,
  1674. .name = "ksm",
  1675. };
  1676. #endif /* CONFIG_SYSFS */
  1677. static int __init ksm_init(void)
  1678. {
  1679. struct task_struct *ksm_thread;
  1680. int err;
  1681. err = ksm_slab_init();
  1682. if (err)
  1683. goto out;
  1684. ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
  1685. if (IS_ERR(ksm_thread)) {
  1686. printk(KERN_ERR "ksm: creating kthread failed\n");
  1687. err = PTR_ERR(ksm_thread);
  1688. goto out_free;
  1689. }
  1690. #ifdef CONFIG_SYSFS
  1691. err = sysfs_create_group(mm_kobj, &ksm_attr_group);
  1692. if (err) {
  1693. printk(KERN_ERR "ksm: register sysfs failed\n");
  1694. kthread_stop(ksm_thread);
  1695. goto out_free;
  1696. }
  1697. #else
  1698. ksm_run = KSM_RUN_MERGE; /* no way for user to start it */
  1699. #endif /* CONFIG_SYSFS */
  1700. #ifdef CONFIG_MEMORY_HOTREMOVE
  1701. /*
  1702. * Choose a high priority since the callback takes ksm_thread_mutex:
  1703. * later callbacks could only be taking locks which nest within that.
  1704. */
  1705. hotplug_memory_notifier(ksm_memory_callback, 100);
  1706. #endif
  1707. return 0;
  1708. out_free:
  1709. ksm_slab_free();
  1710. out:
  1711. return err;
  1712. }
  1713. module_init(ksm_init)