ksm.c 53 KB

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