ksm.c 55 KB

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