ksm.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  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/hashtable.h>
  36. #include <linux/freezer.h>
  37. #include <linux/oom.h>
  38. #include <linux/numa.h>
  39. #ifdef CONFIG_HAS_EARLYSUSPEND
  40. #include <linux/earlysuspend.h>
  41. #endif
  42. #include <linux/cpumask.h>
  43. #include <linux/fb.h>
  44. #include <asm/tlbflush.h>
  45. #include "internal.h"
  46. #ifdef CONFIG_NUMA
  47. #define NUMA(x) (x)
  48. #define DO_NUMA(x) do { (x); } while (0)
  49. #else
  50. #define NUMA(x) (0)
  51. #define DO_NUMA(x) do { } while (0)
  52. #endif
  53. /*
  54. * A few notes about the KSM scanning process,
  55. * to make it easier to understand the data structures below:
  56. *
  57. * In order to reduce excessive scanning, KSM sorts the memory pages by their
  58. * contents into a data structure that holds pointers to the pages' locations.
  59. *
  60. * Since the contents of the pages may change at any moment, KSM cannot just
  61. * insert the pages into a normal sorted tree and expect it to find anything.
  62. * Therefore KSM uses two data structures - the stable and the unstable tree.
  63. *
  64. * The stable tree holds pointers to all the merged pages (ksm pages), sorted
  65. * by their contents. Because each such page is write-protected, searching on
  66. * this tree is fully assured to be working (except when pages are unmapped),
  67. * and therefore this tree is called the stable tree.
  68. *
  69. * In addition to the stable tree, KSM uses a second data structure called the
  70. * unstable tree: this tree holds pointers to pages which have been found to
  71. * be "unchanged for a period of time". The unstable tree sorts these pages
  72. * by their contents, but since they are not write-protected, KSM cannot rely
  73. * upon the unstable tree to work correctly - the unstable tree is liable to
  74. * be corrupted as its contents are modified, and so it is called unstable.
  75. *
  76. * KSM solves this problem by several techniques:
  77. *
  78. * 1) The unstable tree is flushed every time KSM completes scanning all
  79. * memory areas, and then the tree is rebuilt again from the beginning.
  80. * 2) KSM will only insert into the unstable tree, pages whose hash value
  81. * has not changed since the previous scan of all memory areas.
  82. * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
  83. * colors of the nodes and not on their contents, assuring that even when
  84. * the tree gets "corrupted" it won't get out of balance, so scanning time
  85. * remains the same (also, searching and inserting nodes in an rbtree uses
  86. * the same algorithm, so we have no overhead when we flush and rebuild).
  87. * 4) KSM never flushes the stable tree, which means that even if it were to
  88. * take 10 attempts to find a page in the unstable tree, once it is found,
  89. * it is secured in the stable tree. (When we scan a new page, we first
  90. * compare it against the stable tree, and then against the unstable tree.)
  91. *
  92. * If the merge_across_nodes tunable is unset, then KSM maintains multiple
  93. * stable trees and multiple unstable trees: one of each for each NUMA node.
  94. */
  95. /**
  96. * struct mm_slot - ksm information per mm that is being scanned
  97. * @link: link to the mm_slots hash list
  98. * @mm_list: link into the mm_slots list, rooted in ksm_mm_head
  99. * @rmap_list: head for this mm_slot's singly-linked list of rmap_items
  100. * @mm: the mm that this information is valid for
  101. */
  102. struct mm_slot {
  103. struct hlist_node link;
  104. struct list_head mm_list;
  105. struct rmap_item *rmap_list;
  106. struct mm_struct *mm;
  107. };
  108. /**
  109. * struct ksm_scan - cursor for scanning
  110. * @mm_slot: the current mm_slot we are scanning
  111. * @address: the next address inside that to be scanned
  112. * @rmap_list: link to the next rmap to be scanned in the rmap_list
  113. * @seqnr: count of completed full scans (needed when removing unstable node)
  114. *
  115. * There is only the one ksm_scan instance of this cursor structure.
  116. */
  117. struct ksm_scan {
  118. struct mm_slot *mm_slot;
  119. unsigned long address;
  120. struct rmap_item **rmap_list;
  121. unsigned long seqnr;
  122. };
  123. /**
  124. * struct stable_node - node of the stable rbtree
  125. * @node: rb node of this ksm page in the stable tree
  126. * @head: (overlaying parent) &migrate_nodes indicates temporarily on that list
  127. * @list: linked into migrate_nodes, pending placement in the proper node tree
  128. * @hlist: hlist head of rmap_items using this ksm page
  129. * @kpfn: page frame number of this ksm page (perhaps temporarily on wrong nid)
  130. * @nid: NUMA node id of stable tree in which linked (may not match kpfn)
  131. */
  132. struct stable_node {
  133. union {
  134. struct rb_node node; /* when node of stable tree */
  135. struct { /* when listed for migration */
  136. struct list_head *head;
  137. struct list_head list;
  138. };
  139. };
  140. struct hlist_head hlist;
  141. unsigned long kpfn;
  142. #ifdef CONFIG_NUMA
  143. int nid;
  144. #endif
  145. };
  146. /**
  147. * struct rmap_item - reverse mapping item for virtual addresses
  148. * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
  149. * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree
  150. * @nid: NUMA node id of unstable tree in which linked (may not match page)
  151. * @mm: the memory structure this rmap_item is pointing into
  152. * @address: the virtual address this rmap_item tracks (+ flags in low bits)
  153. * @oldchecksum: previous checksum of the page at that virtual address
  154. * @node: rb node of this rmap_item in the unstable tree
  155. * @head: pointer to stable_node heading this list in the stable tree
  156. * @hlist: link into hlist of rmap_items hanging off that stable_node
  157. */
  158. struct rmap_item {
  159. struct rmap_item *rmap_list;
  160. union {
  161. struct anon_vma *anon_vma; /* when stable */
  162. #ifdef CONFIG_NUMA
  163. int nid; /* when node of unstable tree */
  164. #endif
  165. };
  166. struct mm_struct *mm;
  167. unsigned long address; /* + low bits used for flags below */
  168. unsigned int oldchecksum; /* when unstable */
  169. union {
  170. struct rb_node node; /* when node of unstable tree */
  171. struct { /* when listed from stable tree */
  172. struct stable_node *head;
  173. struct hlist_node hlist;
  174. };
  175. };
  176. };
  177. #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */
  178. #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */
  179. #define STABLE_FLAG 0x200 /* is listed from the stable tree */
  180. /* The stable and unstable tree heads */
  181. static struct rb_root one_stable_tree[1] = { RB_ROOT };
  182. static struct rb_root one_unstable_tree[1] = { RB_ROOT };
  183. static struct rb_root *root_stable_tree = one_stable_tree;
  184. static struct rb_root *root_unstable_tree = one_unstable_tree;
  185. /* Recently migrated nodes of stable tree, pending proper placement */
  186. static LIST_HEAD(migrate_nodes);
  187. #define MM_SLOTS_HASH_BITS 10
  188. static DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
  189. static struct mm_slot ksm_mm_head = {
  190. .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list),
  191. };
  192. static struct ksm_scan ksm_scan = {
  193. .mm_slot = &ksm_mm_head,
  194. };
  195. static struct kmem_cache *rmap_item_cache;
  196. static struct kmem_cache *stable_node_cache;
  197. static struct kmem_cache *mm_slot_cache;
  198. /* The number of nodes in the stable tree */
  199. static unsigned long ksm_pages_shared;
  200. /* The number of page slots additionally sharing those nodes */
  201. static unsigned long ksm_pages_sharing;
  202. /* The number of nodes in the unstable tree */
  203. static unsigned long ksm_pages_unshared;
  204. /* The number of rmap_items in use: to calculate pages_volatile */
  205. static unsigned long ksm_rmap_items;
  206. /* Number of pages ksmd should scan in one batch */
  207. static unsigned int ksm_thread_pages_to_scan = 100;
  208. /* Milliseconds ksmd should sleep between batches */
  209. static unsigned int ksm_thread_sleep_millisecs = 20;
  210. #ifdef CONFIG_NUMA
  211. /* Zeroed when merging across nodes is not allowed */
  212. static unsigned int ksm_merge_across_nodes = 1;
  213. static int ksm_nr_node_ids = 1;
  214. #else
  215. #define ksm_merge_across_nodes 1U
  216. #define ksm_nr_node_ids 1
  217. #endif
  218. #define KSM_RUN_STOP 0
  219. #define KSM_RUN_MERGE 1
  220. #define KSM_RUN_UNMERGE 2
  221. #define KSM_RUN_OFFLINE 4
  222. static unsigned long ksm_run = KSM_RUN_STOP;
  223. static void wait_while_offlining(void);
  224. static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait);
  225. static DEFINE_MUTEX(ksm_thread_mutex);
  226. static DEFINE_SPINLOCK(ksm_mmlist_lock);
  227. #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
  228. sizeof(struct __struct), __alignof__(struct __struct),\
  229. (__flags), NULL)
  230. static int __init ksm_slab_init(void)
  231. {
  232. rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0);
  233. if (!rmap_item_cache)
  234. goto out;
  235. stable_node_cache = KSM_KMEM_CACHE(stable_node, 0);
  236. if (!stable_node_cache)
  237. goto out_free1;
  238. mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0);
  239. if (!mm_slot_cache)
  240. goto out_free2;
  241. return 0;
  242. out_free2:
  243. kmem_cache_destroy(stable_node_cache);
  244. out_free1:
  245. kmem_cache_destroy(rmap_item_cache);
  246. out:
  247. return -ENOMEM;
  248. }
  249. static void __init ksm_slab_free(void)
  250. {
  251. kmem_cache_destroy(mm_slot_cache);
  252. kmem_cache_destroy(stable_node_cache);
  253. kmem_cache_destroy(rmap_item_cache);
  254. mm_slot_cache = NULL;
  255. }
  256. static inline struct rmap_item *alloc_rmap_item(void)
  257. {
  258. struct rmap_item *rmap_item;
  259. rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL);
  260. if (rmap_item)
  261. ksm_rmap_items++;
  262. return rmap_item;
  263. }
  264. static inline void free_rmap_item(struct rmap_item *rmap_item)
  265. {
  266. ksm_rmap_items--;
  267. rmap_item->mm = NULL; /* debug safety */
  268. kmem_cache_free(rmap_item_cache, rmap_item);
  269. }
  270. static inline struct stable_node *alloc_stable_node(void)
  271. {
  272. return kmem_cache_alloc(stable_node_cache, GFP_KERNEL);
  273. }
  274. static inline void free_stable_node(struct stable_node *stable_node)
  275. {
  276. kmem_cache_free(stable_node_cache, stable_node);
  277. }
  278. static inline struct mm_slot *alloc_mm_slot(void)
  279. {
  280. if (!mm_slot_cache) /* initialization failed */
  281. return NULL;
  282. return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
  283. }
  284. static inline void free_mm_slot(struct mm_slot *mm_slot)
  285. {
  286. kmem_cache_free(mm_slot_cache, mm_slot);
  287. }
  288. static struct mm_slot *get_mm_slot(struct mm_struct *mm)
  289. {
  290. struct mm_slot *slot;
  291. hash_for_each_possible(mm_slots_hash, slot, link, (unsigned long)mm)
  292. if (slot->mm == mm)
  293. return slot;
  294. return NULL;
  295. }
  296. static void insert_to_mm_slots_hash(struct mm_struct *mm,
  297. struct mm_slot *mm_slot)
  298. {
  299. mm_slot->mm = mm;
  300. hash_add(mm_slots_hash, &mm_slot->link, (unsigned long)mm);
  301. }
  302. /*
  303. * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
  304. * page tables after it has passed through ksm_exit() - which, if necessary,
  305. * takes mmap_sem briefly to serialize against them. ksm_exit() does not set
  306. * a special flag: they can just back out as soon as mm_users goes to zero.
  307. * ksm_test_exit() is used throughout to make this test for exit: in some
  308. * places for correctness, in some places just to avoid unnecessary work.
  309. */
  310. static inline bool ksm_test_exit(struct mm_struct *mm)
  311. {
  312. return atomic_read(&mm->mm_users) == 0;
  313. }
  314. /*
  315. * We use break_ksm to break COW on a ksm page: it's a stripped down
  316. *
  317. * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1)
  318. * put_page(page);
  319. *
  320. * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
  321. * in case the application has unmapped and remapped mm,addr meanwhile.
  322. * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP
  323. * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
  324. */
  325. static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
  326. {
  327. struct page *page;
  328. int ret = 0;
  329. do {
  330. cond_resched();
  331. page = follow_page(vma, addr, FOLL_GET | FOLL_MIGRATION);
  332. if (IS_ERR_OR_NULL(page))
  333. break;
  334. if (PageKsm(page))
  335. ret = handle_mm_fault(vma->vm_mm, vma, addr,
  336. FAULT_FLAG_WRITE);
  337. else
  338. ret = VM_FAULT_WRITE;
  339. put_page(page);
  340. } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV | VM_FAULT_OOM)));
  341. /*
  342. * We must loop because handle_mm_fault() may back out if there's
  343. * any difficulty e.g. if pte accessed bit gets updated concurrently.
  344. *
  345. * VM_FAULT_WRITE is what we have been hoping for: it indicates that
  346. * COW has been broken, even if the vma does not permit VM_WRITE;
  347. * but note that a concurrent fault might break PageKsm for us.
  348. *
  349. * VM_FAULT_SIGBUS could occur if we race with truncation of the
  350. * backing file, which also invalidates anonymous pages: that's
  351. * okay, that truncation will have unmapped the PageKsm for us.
  352. *
  353. * VM_FAULT_OOM: at the time of writing (late July 2009), setting
  354. * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
  355. * current task has TIF_MEMDIE set, and will be OOM killed on return
  356. * to user; and ksmd, having no mm, would never be chosen for that.
  357. *
  358. * But if the mm is in a limited mem_cgroup, then the fault may fail
  359. * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
  360. * even ksmd can fail in this way - though it's usually breaking ksm
  361. * just to undo a merge it made a moment before, so unlikely to oom.
  362. *
  363. * That's a pity: we might therefore have more kernel pages allocated
  364. * than we're counting as nodes in the stable tree; but ksm_do_scan
  365. * will retry to break_cow on each pass, so should recover the page
  366. * in due course. The important thing is to not let VM_MERGEABLE
  367. * be cleared while any such pages might remain in the area.
  368. */
  369. return (ret & VM_FAULT_OOM) ? -ENOMEM : 0;
  370. }
  371. static struct vm_area_struct *find_mergeable_vma(struct mm_struct *mm,
  372. unsigned long addr)
  373. {
  374. struct vm_area_struct *vma;
  375. if (ksm_test_exit(mm))
  376. return NULL;
  377. vma = find_vma(mm, addr);
  378. if (!vma || vma->vm_start > addr)
  379. return NULL;
  380. if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
  381. return NULL;
  382. return vma;
  383. }
  384. static void break_cow(struct rmap_item *rmap_item)
  385. {
  386. struct mm_struct *mm = rmap_item->mm;
  387. unsigned long addr = rmap_item->address;
  388. struct vm_area_struct *vma;
  389. /*
  390. * It is not an accident that whenever we want to break COW
  391. * to undo, we also need to drop a reference to the anon_vma.
  392. */
  393. put_anon_vma(rmap_item->anon_vma);
  394. down_read(&mm->mmap_sem);
  395. vma = find_mergeable_vma(mm, addr);
  396. if (vma)
  397. break_ksm(vma, addr);
  398. up_read(&mm->mmap_sem);
  399. }
  400. static struct page *page_trans_compound_anon(struct page *page)
  401. {
  402. if (PageTransCompound(page)) {
  403. struct page *head = compound_head(page);
  404. /*
  405. * head may actually be splitted and freed from under
  406. * us but it's ok here.
  407. */
  408. if (PageAnon(head))
  409. return head;
  410. }
  411. return NULL;
  412. }
  413. static struct page *get_mergeable_page(struct rmap_item *rmap_item)
  414. {
  415. struct mm_struct *mm = rmap_item->mm;
  416. unsigned long addr = rmap_item->address;
  417. struct vm_area_struct *vma;
  418. struct page *page;
  419. down_read(&mm->mmap_sem);
  420. vma = find_mergeable_vma(mm, addr);
  421. if (!vma)
  422. goto out;
  423. page = follow_page(vma, addr, FOLL_GET);
  424. if (IS_ERR_OR_NULL(page))
  425. goto out;
  426. if (PageAnon(page) || page_trans_compound_anon(page)) {
  427. flush_anon_page(vma, page, addr);
  428. flush_dcache_page(page);
  429. } else {
  430. put_page(page);
  431. out: page = NULL;
  432. }
  433. up_read(&mm->mmap_sem);
  434. return page;
  435. }
  436. /*
  437. * This helper is used for getting right index into array of tree roots.
  438. * When merge_across_nodes knob is set to 1, there are only two rb-trees for
  439. * stable and unstable pages from all nodes with roots in index 0. Otherwise,
  440. * every node has its own stable and unstable tree.
  441. */
  442. static inline int get_kpfn_nid(unsigned long kpfn)
  443. {
  444. return ksm_merge_across_nodes ? 0 : NUMA(pfn_to_nid(kpfn));
  445. }
  446. static void remove_node_from_stable_tree(struct stable_node *stable_node)
  447. {
  448. struct rmap_item *rmap_item;
  449. hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
  450. if (rmap_item->hlist.next)
  451. ksm_pages_sharing--;
  452. else
  453. ksm_pages_shared--;
  454. put_anon_vma(rmap_item->anon_vma);
  455. rmap_item->address &= PAGE_MASK;
  456. cond_resched();
  457. }
  458. if (stable_node->head == &migrate_nodes)
  459. list_del(&stable_node->list);
  460. else
  461. rb_erase(&stable_node->node,
  462. root_stable_tree + NUMA(stable_node->nid));
  463. free_stable_node(stable_node);
  464. }
  465. /*
  466. * get_ksm_page: checks if the page indicated by the stable node
  467. * is still its ksm page, despite having held no reference to it.
  468. * In which case we can trust the content of the page, and it
  469. * returns the gotten page; but if the page has now been zapped,
  470. * remove the stale node from the stable tree and return NULL.
  471. * But beware, the stable node's page might be being migrated.
  472. *
  473. * You would expect the stable_node to hold a reference to the ksm page.
  474. * But if it increments the page's count, swapping out has to wait for
  475. * ksmd to come around again before it can free the page, which may take
  476. * seconds or even minutes: much too unresponsive. So instead we use a
  477. * "keyhole reference": access to the ksm page from the stable node peeps
  478. * out through its keyhole to see if that page still holds the right key,
  479. * pointing back to this stable node. This relies on freeing a PageAnon
  480. * page to reset its page->mapping to NULL, and relies on no other use of
  481. * a page to put something that might look like our key in page->mapping.
  482. * is on its way to being freed; but it is an anomaly to bear in mind.
  483. */
  484. static struct page *get_ksm_page(struct stable_node *stable_node, bool lock_it)
  485. {
  486. struct page *page;
  487. void *expected_mapping;
  488. unsigned long kpfn;
  489. expected_mapping = (void *)stable_node +
  490. (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM);
  491. again:
  492. kpfn = ACCESS_ONCE(stable_node->kpfn);
  493. page = pfn_to_page(kpfn);
  494. /*
  495. * page is computed from kpfn, so on most architectures reading
  496. * page->mapping is naturally ordered after reading node->kpfn,
  497. * but on Alpha we need to be more careful.
  498. */
  499. smp_read_barrier_depends();
  500. if (ACCESS_ONCE(page->mapping) != expected_mapping)
  501. goto stale;
  502. /*
  503. * We cannot do anything with the page while its refcount is 0.
  504. * Usually 0 means free, or tail of a higher-order page: in which
  505. * case this node is no longer referenced, and should be freed;
  506. * however, it might mean that the page is under page_freeze_refs().
  507. * The __remove_mapping() case is easy, again the node is now stale;
  508. * but if page is swapcache in migrate_page_move_mapping(), it might
  509. * still be our page, in which case it's essential to keep the node.
  510. */
  511. while (!get_page_unless_zero(page)) {
  512. /*
  513. * Another check for page->mapping != expected_mapping would
  514. * work here too. We have chosen the !PageSwapCache test to
  515. * optimize the common case, when the page is or is about to
  516. * be freed: PageSwapCache is cleared (under spin_lock_irq)
  517. * in the freeze_refs section of __remove_mapping(); but Anon
  518. * page->mapping reset to NULL later, in free_pages_prepare().
  519. */
  520. if (!PageSwapCache(page))
  521. goto stale;
  522. cpu_relax();
  523. }
  524. if (ACCESS_ONCE(page->mapping) != expected_mapping) {
  525. put_page(page);
  526. goto stale;
  527. }
  528. if (lock_it) {
  529. lock_page(page);
  530. if (ACCESS_ONCE(page->mapping) != expected_mapping) {
  531. unlock_page(page);
  532. put_page(page);
  533. goto stale;
  534. }
  535. }
  536. return page;
  537. stale:
  538. /*
  539. * We come here from above when page->mapping or !PageSwapCache
  540. * suggests that the node is stale; but it might be under migration.
  541. * We need smp_rmb(), matching the smp_wmb() in ksm_migrate_page(),
  542. * before checking whether node->kpfn has been changed.
  543. */
  544. smp_rmb();
  545. if (ACCESS_ONCE(stable_node->kpfn) != kpfn)
  546. goto again;
  547. remove_node_from_stable_tree(stable_node);
  548. return NULL;
  549. }
  550. /*
  551. * Removing rmap_item from stable or unstable tree.
  552. * This function will clean the information from the stable/unstable tree.
  553. */
  554. static void remove_rmap_item_from_tree(struct rmap_item *rmap_item)
  555. {
  556. if (rmap_item->address & STABLE_FLAG) {
  557. struct stable_node *stable_node;
  558. struct page *page;
  559. stable_node = rmap_item->head;
  560. page = get_ksm_page(stable_node, true);
  561. if (!page)
  562. goto out;
  563. hlist_del(&rmap_item->hlist);
  564. unlock_page(page);
  565. put_page(page);
  566. if (stable_node->hlist.first)
  567. ksm_pages_sharing--;
  568. else
  569. ksm_pages_shared--;
  570. put_anon_vma(rmap_item->anon_vma);
  571. rmap_item->address &= PAGE_MASK;
  572. } else if (rmap_item->address & UNSTABLE_FLAG) {
  573. unsigned char age;
  574. /*
  575. * Usually ksmd can and must skip the rb_erase, because
  576. * root_unstable_tree was already reset to RB_ROOT.
  577. * But be careful when an mm is exiting: do the rb_erase
  578. * if this rmap_item was inserted by this scan, rather
  579. * than left over from before.
  580. */
  581. age = (unsigned char)(ksm_scan.seqnr - rmap_item->address);
  582. BUG_ON(age > 1);
  583. if (!age)
  584. rb_erase(&rmap_item->node,
  585. root_unstable_tree + NUMA(rmap_item->nid));
  586. ksm_pages_unshared--;
  587. rmap_item->address &= PAGE_MASK;
  588. }
  589. out:
  590. cond_resched(); /* we're called from many long loops */
  591. }
  592. static void remove_trailing_rmap_items(struct mm_slot *mm_slot,
  593. struct rmap_item **rmap_list)
  594. {
  595. while (*rmap_list) {
  596. struct rmap_item *rmap_item = *rmap_list;
  597. *rmap_list = rmap_item->rmap_list;
  598. remove_rmap_item_from_tree(rmap_item);
  599. free_rmap_item(rmap_item);
  600. }
  601. }
  602. /*
  603. * Though it's very tempting to unmerge rmap_items from stable tree rather
  604. * than check every pte of a given vma, the locking doesn't quite work for
  605. * that - an rmap_item is assigned to the stable tree after inserting ksm
  606. * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing
  607. * rmap_items from parent to child at fork time (so as not to waste time
  608. * if exit comes before the next scan reaches it).
  609. *
  610. * Similarly, although we'd like to remove rmap_items (so updating counts
  611. * and freeing memory) when unmerging an area, it's easier to leave that
  612. * to the next pass of ksmd - consider, for example, how ksmd might be
  613. * in cmp_and_merge_page on one of the rmap_items we would be removing.
  614. */
  615. static int unmerge_ksm_pages(struct vm_area_struct *vma,
  616. unsigned long start, unsigned long end)
  617. {
  618. unsigned long addr;
  619. int err = 0;
  620. for (addr = start; addr < end && !err; addr += PAGE_SIZE) {
  621. if (ksm_test_exit(vma->vm_mm))
  622. break;
  623. if (signal_pending(current))
  624. err = -ERESTARTSYS;
  625. else
  626. err = break_ksm(vma, addr);
  627. }
  628. return err;
  629. }
  630. #ifdef CONFIG_SYSFS
  631. /*
  632. * Only called through the sysfs control interface:
  633. */
  634. static int remove_stable_node(struct stable_node *stable_node)
  635. {
  636. struct page *page;
  637. int err;
  638. page = get_ksm_page(stable_node, true);
  639. if (!page) {
  640. /*
  641. * get_ksm_page did remove_node_from_stable_tree itself.
  642. */
  643. return 0;
  644. }
  645. if (WARN_ON_ONCE(page_mapped(page))) {
  646. /*
  647. * This should not happen: but if it does, just refuse to let
  648. * merge_across_nodes be switched - there is no need to panic.
  649. */
  650. err = -EBUSY;
  651. } else {
  652. /*
  653. * The stable node did not yet appear stale to get_ksm_page(),
  654. * since that allows for an unmapped ksm page to be recognized
  655. * right up until it is freed; but the node is safe to remove.
  656. * This page might be in a pagevec waiting to be freed,
  657. * or it might be PageSwapCache (perhaps under writeback),
  658. * or it might have been removed from swapcache a moment ago.
  659. */
  660. set_page_stable_node(page, NULL);
  661. remove_node_from_stable_tree(stable_node);
  662. err = 0;
  663. }
  664. unlock_page(page);
  665. put_page(page);
  666. return err;
  667. }
  668. static int remove_all_stable_nodes(void)
  669. {
  670. struct stable_node *stable_node;
  671. struct list_head *this, *next;
  672. int nid;
  673. int err = 0;
  674. for (nid = 0; nid < ksm_nr_node_ids; nid++) {
  675. while (root_stable_tree[nid].rb_node) {
  676. stable_node = rb_entry(root_stable_tree[nid].rb_node,
  677. struct stable_node, node);
  678. if (remove_stable_node(stable_node)) {
  679. err = -EBUSY;
  680. break; /* proceed to next nid */
  681. }
  682. cond_resched();
  683. }
  684. }
  685. list_for_each_safe(this, next, &migrate_nodes) {
  686. stable_node = list_entry(this, struct stable_node, list);
  687. if (remove_stable_node(stable_node))
  688. err = -EBUSY;
  689. cond_resched();
  690. }
  691. return err;
  692. }
  693. static int unmerge_and_remove_all_rmap_items(void)
  694. {
  695. struct mm_slot *mm_slot;
  696. struct mm_struct *mm;
  697. struct vm_area_struct *vma;
  698. int err = 0;
  699. spin_lock(&ksm_mmlist_lock);
  700. ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next,
  701. struct mm_slot, mm_list);
  702. spin_unlock(&ksm_mmlist_lock);
  703. for (mm_slot = ksm_scan.mm_slot;
  704. mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) {
  705. mm = mm_slot->mm;
  706. down_read(&mm->mmap_sem);
  707. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  708. if (ksm_test_exit(mm))
  709. break;
  710. if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma)
  711. continue;
  712. err = unmerge_ksm_pages(vma,
  713. vma->vm_start, vma->vm_end);
  714. if (err)
  715. goto error;
  716. }
  717. remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list);
  718. spin_lock(&ksm_mmlist_lock);
  719. ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next,
  720. struct mm_slot, mm_list);
  721. if (ksm_test_exit(mm)) {
  722. hash_del(&mm_slot->link);
  723. list_del(&mm_slot->mm_list);
  724. spin_unlock(&ksm_mmlist_lock);
  725. free_mm_slot(mm_slot);
  726. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  727. up_read(&mm->mmap_sem);
  728. mmdrop(mm);
  729. } else {
  730. spin_unlock(&ksm_mmlist_lock);
  731. up_read(&mm->mmap_sem);
  732. }
  733. }
  734. /* Clean up stable nodes, but don't worry if some are still busy */
  735. remove_all_stable_nodes();
  736. ksm_scan.seqnr = 0;
  737. return 0;
  738. error:
  739. up_read(&mm->mmap_sem);
  740. spin_lock(&ksm_mmlist_lock);
  741. ksm_scan.mm_slot = &ksm_mm_head;
  742. spin_unlock(&ksm_mmlist_lock);
  743. return err;
  744. }
  745. #endif /* CONFIG_SYSFS */
  746. static u32 calc_checksum(struct page *page)
  747. {
  748. u32 checksum;
  749. void *addr = kmap_atomic(page);
  750. checksum = jhash2(addr, PAGE_SIZE / 4, 17);
  751. kunmap_atomic(addr);
  752. return checksum;
  753. }
  754. static int memcmp_pages(struct page *page1, struct page *page2)
  755. {
  756. char *addr1, *addr2;
  757. int ret;
  758. addr1 = kmap_atomic(page1);
  759. addr2 = kmap_atomic(page2);
  760. ret = memcmp(addr1, addr2, PAGE_SIZE);
  761. kunmap_atomic(addr2);
  762. kunmap_atomic(addr1);
  763. return ret;
  764. }
  765. static inline int pages_identical(struct page *page1, struct page *page2)
  766. {
  767. return !memcmp_pages(page1, page2);
  768. }
  769. static int write_protect_page(struct vm_area_struct *vma, struct page *page,
  770. pte_t *orig_pte)
  771. {
  772. struct mm_struct *mm = vma->vm_mm;
  773. unsigned long addr;
  774. pte_t *ptep;
  775. spinlock_t *ptl;
  776. int swapped;
  777. int err = -EFAULT;
  778. unsigned long mmun_start; /* For mmu_notifiers */
  779. unsigned long mmun_end; /* For mmu_notifiers */
  780. addr = page_address_in_vma(page, vma);
  781. if (addr == -EFAULT)
  782. goto out;
  783. BUG_ON(PageTransCompound(page));
  784. mmun_start = addr;
  785. mmun_end = addr + PAGE_SIZE;
  786. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  787. ptep = page_check_address(page, mm, addr, &ptl, 0);
  788. if (!ptep)
  789. goto out_mn;
  790. if (pte_write(*ptep) || pte_dirty(*ptep)) {
  791. pte_t entry;
  792. swapped = PageSwapCache(page);
  793. flush_cache_page(vma, addr, page_to_pfn(page));
  794. /*
  795. * Ok this is tricky, when get_user_pages_fast() run it doesn't
  796. * take any lock, therefore the check that we are going to make
  797. * with the pagecount against the mapcount is racey and
  798. * O_DIRECT can happen right after the check.
  799. * So we clear the pte and flush the tlb before the check
  800. * this assure us that no O_DIRECT can happen after the check
  801. * or in the middle of the check.
  802. */
  803. entry = ptep_clear_flush(vma, addr, ptep);
  804. /*
  805. * Check that no O_DIRECT or similar I/O is in progress on the
  806. * page
  807. */
  808. if (page_mapcount(page) + 1 + swapped != page_count(page)) {
  809. set_pte_at(mm, addr, ptep, entry);
  810. goto out_unlock;
  811. }
  812. if (pte_dirty(entry))
  813. set_page_dirty(page);
  814. entry = pte_mkclean(pte_wrprotect(entry));
  815. set_pte_at_notify(mm, addr, ptep, entry);
  816. }
  817. *orig_pte = *ptep;
  818. err = 0;
  819. out_unlock:
  820. pte_unmap_unlock(ptep, ptl);
  821. out_mn:
  822. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  823. out:
  824. return err;
  825. }
  826. /**
  827. * replace_page - replace page in vma by new ksm page
  828. * @vma: vma that holds the pte pointing to page
  829. * @page: the page we are replacing by kpage
  830. * @kpage: the ksm page we replace page by
  831. * @orig_pte: the original value of the pte
  832. *
  833. * Returns 0 on success, -EFAULT on failure.
  834. */
  835. static int replace_page(struct vm_area_struct *vma, struct page *page,
  836. struct page *kpage, pte_t orig_pte)
  837. {
  838. struct mm_struct *mm = vma->vm_mm;
  839. pmd_t *pmd;
  840. pte_t *ptep;
  841. spinlock_t *ptl;
  842. unsigned long addr;
  843. int err = -EFAULT;
  844. unsigned long mmun_start; /* For mmu_notifiers */
  845. unsigned long mmun_end; /* For mmu_notifiers */
  846. addr = page_address_in_vma(page, vma);
  847. if (addr == -EFAULT)
  848. goto out;
  849. pmd = mm_find_pmd(mm, addr);
  850. if (!pmd)
  851. goto out;
  852. mmun_start = addr;
  853. mmun_end = addr + PAGE_SIZE;
  854. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  855. ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
  856. if (!pte_same(*ptep, orig_pte)) {
  857. pte_unmap_unlock(ptep, ptl);
  858. goto out_mn;
  859. }
  860. get_page(kpage);
  861. page_add_anon_rmap(kpage, vma, addr);
  862. flush_cache_page(vma, addr, pte_pfn(*ptep));
  863. ptep_clear_flush(vma, addr, ptep);
  864. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  865. page_remove_rmap(page);
  866. if (!page_mapped(page))
  867. try_to_free_swap(page);
  868. put_page(page);
  869. pte_unmap_unlock(ptep, ptl);
  870. err = 0;
  871. out_mn:
  872. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  873. out:
  874. return err;
  875. }
  876. static int page_trans_compound_anon_split(struct page *page)
  877. {
  878. int ret = 0;
  879. struct page *transhuge_head = page_trans_compound_anon(page);
  880. if (transhuge_head) {
  881. /* Get the reference on the head to split it. */
  882. if (get_page_unless_zero(transhuge_head)) {
  883. /*
  884. * Recheck we got the reference while the head
  885. * was still anonymous.
  886. */
  887. if (PageAnon(transhuge_head))
  888. ret = split_huge_page(transhuge_head);
  889. else
  890. /*
  891. * Retry later if split_huge_page run
  892. * from under us.
  893. */
  894. ret = 1;
  895. put_page(transhuge_head);
  896. } else
  897. /* Retry later if split_huge_page run from under us. */
  898. ret = 1;
  899. }
  900. return ret;
  901. }
  902. /*
  903. * try_to_merge_one_page - take two pages and merge them into one
  904. * @vma: the vma that holds the pte pointing to page
  905. * @page: the PageAnon page that we want to replace with kpage
  906. * @kpage: the PageKsm page that we want to map instead of page,
  907. * or NULL the first time when we want to use page as kpage.
  908. *
  909. * This function returns 0 if the pages were merged, -EFAULT otherwise.
  910. */
  911. static int try_to_merge_one_page(struct vm_area_struct *vma,
  912. struct page *page, struct page *kpage)
  913. {
  914. pte_t orig_pte = __pte(0);
  915. int err = -EFAULT;
  916. if (page == kpage) /* ksm page forked */
  917. return 0;
  918. if (!(vma->vm_flags & VM_MERGEABLE))
  919. goto out;
  920. if (PageTransCompound(page) && page_trans_compound_anon_split(page))
  921. goto out;
  922. BUG_ON(PageTransCompound(page));
  923. if (!PageAnon(page))
  924. goto out;
  925. /*
  926. * We need the page lock to read a stable PageSwapCache in
  927. * write_protect_page(). We use trylock_page() instead of
  928. * lock_page() because we don't want to wait here - we
  929. * prefer to continue scanning and merging different pages,
  930. * then come back to this page when it is unlocked.
  931. */
  932. if (!trylock_page(page))
  933. goto out;
  934. /*
  935. * If this anonymous page is mapped only here, its pte may need
  936. * to be write-protected. If it's mapped elsewhere, all of its
  937. * ptes are necessarily already write-protected. But in either
  938. * case, we need to lock and check page_count is not raised.
  939. */
  940. if (write_protect_page(vma, page, &orig_pte) == 0) {
  941. if (!kpage) {
  942. /*
  943. * While we hold page lock, upgrade page from
  944. * PageAnon+anon_vma to PageKsm+NULL stable_node:
  945. * stable_tree_insert() will update stable_node.
  946. */
  947. set_page_stable_node(page, NULL);
  948. mark_page_accessed(page);
  949. err = 0;
  950. } else if (pages_identical(page, kpage))
  951. err = replace_page(vma, page, kpage, orig_pte);
  952. }
  953. if ((vma->vm_flags & VM_LOCKED) && kpage && !err) {
  954. munlock_vma_page(page);
  955. if (!PageMlocked(kpage)) {
  956. unlock_page(page);
  957. lock_page(kpage);
  958. mlock_vma_page(kpage);
  959. page = kpage; /* for final unlock */
  960. }
  961. }
  962. unlock_page(page);
  963. out:
  964. return err;
  965. }
  966. /*
  967. * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
  968. * but no new kernel page is allocated: kpage must already be a ksm page.
  969. *
  970. * This function returns 0 if the pages were merged, -EFAULT otherwise.
  971. */
  972. static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item,
  973. struct page *page, struct page *kpage)
  974. {
  975. struct mm_struct *mm = rmap_item->mm;
  976. struct vm_area_struct *vma;
  977. int err = -EFAULT;
  978. down_read(&mm->mmap_sem);
  979. if (ksm_test_exit(mm))
  980. goto out;
  981. vma = find_vma(mm, rmap_item->address);
  982. if (!vma || vma->vm_start > rmap_item->address)
  983. goto out;
  984. err = try_to_merge_one_page(vma, page, kpage);
  985. if (err)
  986. goto out;
  987. /* Unstable nid is in union with stable anon_vma: remove first */
  988. remove_rmap_item_from_tree(rmap_item);
  989. /* Must get reference to anon_vma while still holding mmap_sem */
  990. rmap_item->anon_vma = vma->anon_vma;
  991. get_anon_vma(vma->anon_vma);
  992. out:
  993. up_read(&mm->mmap_sem);
  994. return err;
  995. }
  996. /*
  997. * try_to_merge_two_pages - take two identical pages and prepare them
  998. * to be merged into one page.
  999. *
  1000. * This function returns the kpage if we successfully merged two identical
  1001. * pages into one ksm page, NULL otherwise.
  1002. *
  1003. * Note that this function upgrades page to ksm page: if one of the pages
  1004. * is already a ksm page, try_to_merge_with_ksm_page should be used.
  1005. */
  1006. static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item,
  1007. struct page *page,
  1008. struct rmap_item *tree_rmap_item,
  1009. struct page *tree_page)
  1010. {
  1011. int err;
  1012. err = try_to_merge_with_ksm_page(rmap_item, page, NULL);
  1013. if (!err) {
  1014. err = try_to_merge_with_ksm_page(tree_rmap_item,
  1015. tree_page, page);
  1016. /*
  1017. * If that fails, we have a ksm page with only one pte
  1018. * pointing to it: so break it.
  1019. */
  1020. if (err)
  1021. break_cow(rmap_item);
  1022. }
  1023. return err ? NULL : page;
  1024. }
  1025. /*
  1026. * stable_tree_search - search for page inside the stable tree
  1027. *
  1028. * This function checks if there is a page inside the stable tree
  1029. * with identical content to the page that we are scanning right now.
  1030. *
  1031. * This function returns the stable tree node of identical content if found,
  1032. * NULL otherwise.
  1033. */
  1034. static struct page *stable_tree_search(struct page *page)
  1035. {
  1036. int nid;
  1037. struct rb_root *root;
  1038. struct rb_node **new;
  1039. struct rb_node *parent;
  1040. struct stable_node *stable_node;
  1041. struct stable_node *page_node;
  1042. page_node = page_stable_node(page);
  1043. if (page_node && page_node->head != &migrate_nodes) {
  1044. /* ksm page forked */
  1045. get_page(page);
  1046. return page;
  1047. }
  1048. nid = get_kpfn_nid(page_to_pfn(page));
  1049. root = root_stable_tree + nid;
  1050. again:
  1051. new = &root->rb_node;
  1052. parent = NULL;
  1053. while (*new) {
  1054. struct page *tree_page;
  1055. int ret;
  1056. cond_resched();
  1057. stable_node = rb_entry(*new, struct stable_node, node);
  1058. tree_page = get_ksm_page(stable_node, false);
  1059. if (!tree_page)
  1060. return NULL;
  1061. ret = memcmp_pages(page, tree_page);
  1062. put_page(tree_page);
  1063. parent = *new;
  1064. if (ret < 0)
  1065. new = &parent->rb_left;
  1066. else if (ret > 0)
  1067. new = &parent->rb_right;
  1068. else {
  1069. /*
  1070. * Lock and unlock the stable_node's page (which
  1071. * might already have been migrated) so that page
  1072. * migration is sure to notice its raised count.
  1073. * It would be more elegant to return stable_node
  1074. * than kpage, but that involves more changes.
  1075. */
  1076. tree_page = get_ksm_page(stable_node, true);
  1077. if (tree_page) {
  1078. unlock_page(tree_page);
  1079. if (get_kpfn_nid(stable_node->kpfn) !=
  1080. NUMA(stable_node->nid)) {
  1081. put_page(tree_page);
  1082. goto replace;
  1083. }
  1084. return tree_page;
  1085. }
  1086. /*
  1087. * There is now a place for page_node, but the tree may
  1088. * have been rebalanced, so re-evaluate parent and new.
  1089. */
  1090. if (page_node)
  1091. goto again;
  1092. return NULL;
  1093. }
  1094. }
  1095. if (!page_node)
  1096. return NULL;
  1097. list_del(&page_node->list);
  1098. DO_NUMA(page_node->nid = nid);
  1099. rb_link_node(&page_node->node, parent, new);
  1100. rb_insert_color(&page_node->node, root);
  1101. get_page(page);
  1102. return page;
  1103. replace:
  1104. if (page_node) {
  1105. list_del(&page_node->list);
  1106. DO_NUMA(page_node->nid = nid);
  1107. rb_replace_node(&stable_node->node, &page_node->node, root);
  1108. get_page(page);
  1109. } else {
  1110. rb_erase(&stable_node->node, root);
  1111. page = NULL;
  1112. }
  1113. stable_node->head = &migrate_nodes;
  1114. list_add(&stable_node->list, stable_node->head);
  1115. return page;
  1116. }
  1117. /*
  1118. * stable_tree_insert - insert stable tree node pointing to new ksm page
  1119. * into the stable tree.
  1120. *
  1121. * This function returns the stable tree node just allocated on success,
  1122. * NULL otherwise.
  1123. */
  1124. static struct stable_node *stable_tree_insert(struct page *kpage)
  1125. {
  1126. int nid;
  1127. unsigned long kpfn;
  1128. struct rb_root *root;
  1129. struct rb_node **new;
  1130. struct rb_node *parent = NULL;
  1131. struct stable_node *stable_node;
  1132. kpfn = page_to_pfn(kpage);
  1133. nid = get_kpfn_nid(kpfn);
  1134. root = root_stable_tree + nid;
  1135. new = &root->rb_node;
  1136. while (*new) {
  1137. struct page *tree_page;
  1138. int ret;
  1139. cond_resched();
  1140. stable_node = rb_entry(*new, struct stable_node, node);
  1141. tree_page = get_ksm_page(stable_node, false);
  1142. if (!tree_page)
  1143. return NULL;
  1144. ret = memcmp_pages(kpage, tree_page);
  1145. put_page(tree_page);
  1146. parent = *new;
  1147. if (ret < 0)
  1148. new = &parent->rb_left;
  1149. else if (ret > 0)
  1150. new = &parent->rb_right;
  1151. else {
  1152. /*
  1153. * It is not a bug that stable_tree_search() didn't
  1154. * find this node: because at that time our page was
  1155. * not yet write-protected, so may have changed since.
  1156. */
  1157. return NULL;
  1158. }
  1159. }
  1160. stable_node = alloc_stable_node();
  1161. if (!stable_node)
  1162. return NULL;
  1163. INIT_HLIST_HEAD(&stable_node->hlist);
  1164. stable_node->kpfn = kpfn;
  1165. set_page_stable_node(kpage, stable_node);
  1166. DO_NUMA(stable_node->nid = nid);
  1167. rb_link_node(&stable_node->node, parent, new);
  1168. rb_insert_color(&stable_node->node, root);
  1169. return stable_node;
  1170. }
  1171. /*
  1172. * unstable_tree_search_insert - search for identical page,
  1173. * else insert rmap_item into the unstable tree.
  1174. *
  1175. * This function searches for a page in the unstable tree identical to the
  1176. * page currently being scanned; and if no identical page is found in the
  1177. * tree, we insert rmap_item as a new object into the unstable tree.
  1178. *
  1179. * This function returns pointer to rmap_item found to be identical
  1180. * to the currently scanned page, NULL otherwise.
  1181. *
  1182. * This function does both searching and inserting, because they share
  1183. * the same walking algorithm in an rbtree.
  1184. */
  1185. static
  1186. struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item,
  1187. struct page *page,
  1188. struct page **tree_pagep)
  1189. {
  1190. struct rb_node **new;
  1191. struct rb_root *root;
  1192. struct rb_node *parent = NULL;
  1193. int nid;
  1194. nid = get_kpfn_nid(page_to_pfn(page));
  1195. root = root_unstable_tree + nid;
  1196. new = &root->rb_node;
  1197. while (*new) {
  1198. struct rmap_item *tree_rmap_item;
  1199. struct page *tree_page;
  1200. int ret;
  1201. cond_resched();
  1202. tree_rmap_item = rb_entry(*new, struct rmap_item, node);
  1203. tree_page = get_mergeable_page(tree_rmap_item);
  1204. if (IS_ERR_OR_NULL(tree_page))
  1205. return NULL;
  1206. /*
  1207. * Don't substitute a ksm page for a forked page.
  1208. */
  1209. if (page == tree_page) {
  1210. put_page(tree_page);
  1211. return NULL;
  1212. }
  1213. ret = memcmp_pages(page, tree_page);
  1214. parent = *new;
  1215. if (ret < 0) {
  1216. put_page(tree_page);
  1217. new = &parent->rb_left;
  1218. } else if (ret > 0) {
  1219. put_page(tree_page);
  1220. new = &parent->rb_right;
  1221. } else if (!ksm_merge_across_nodes &&
  1222. page_to_nid(tree_page) != nid) {
  1223. /*
  1224. * If tree_page has been migrated to another NUMA node,
  1225. * it will be flushed out and put in the right unstable
  1226. * tree next time: only merge with it when across_nodes.
  1227. */
  1228. put_page(tree_page);
  1229. return NULL;
  1230. } else {
  1231. *tree_pagep = tree_page;
  1232. return tree_rmap_item;
  1233. }
  1234. }
  1235. rmap_item->address |= UNSTABLE_FLAG;
  1236. rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK);
  1237. DO_NUMA(rmap_item->nid = nid);
  1238. rb_link_node(&rmap_item->node, parent, new);
  1239. rb_insert_color(&rmap_item->node, root);
  1240. ksm_pages_unshared++;
  1241. return NULL;
  1242. }
  1243. /*
  1244. * stable_tree_append - add another rmap_item to the linked list of
  1245. * rmap_items hanging off a given node of the stable tree, all sharing
  1246. * the same ksm page.
  1247. */
  1248. static void stable_tree_append(struct rmap_item *rmap_item,
  1249. struct stable_node *stable_node)
  1250. {
  1251. rmap_item->head = stable_node;
  1252. rmap_item->address |= STABLE_FLAG;
  1253. hlist_add_head(&rmap_item->hlist, &stable_node->hlist);
  1254. if (rmap_item->hlist.next)
  1255. ksm_pages_sharing++;
  1256. else
  1257. ksm_pages_shared++;
  1258. }
  1259. /*
  1260. * cmp_and_merge_page - first see if page can be merged into the stable tree;
  1261. * if not, compare checksum to previous and if it's the same, see if page can
  1262. * be inserted into the unstable tree, or merged with a page already there and
  1263. * both transferred to the stable tree.
  1264. *
  1265. * @page: the page that we are searching identical page to.
  1266. * @rmap_item: the reverse mapping into the virtual address of this page
  1267. */
  1268. static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item)
  1269. {
  1270. struct rmap_item *tree_rmap_item;
  1271. struct page *tree_page = NULL;
  1272. struct stable_node *stable_node;
  1273. struct page *kpage;
  1274. unsigned int checksum;
  1275. int err;
  1276. stable_node = page_stable_node(page);
  1277. if (stable_node) {
  1278. if (stable_node->head != &migrate_nodes &&
  1279. get_kpfn_nid(stable_node->kpfn) != NUMA(stable_node->nid)) {
  1280. rb_erase(&stable_node->node,
  1281. root_stable_tree + NUMA(stable_node->nid));
  1282. stable_node->head = &migrate_nodes;
  1283. list_add(&stable_node->list, stable_node->head);
  1284. }
  1285. if (stable_node->head != &migrate_nodes &&
  1286. rmap_item->head == stable_node)
  1287. return;
  1288. }
  1289. /* We first start with searching the page inside the stable tree */
  1290. kpage = stable_tree_search(page);
  1291. if (kpage == page && rmap_item->head == stable_node) {
  1292. put_page(kpage);
  1293. return;
  1294. }
  1295. remove_rmap_item_from_tree(rmap_item);
  1296. if (kpage) {
  1297. err = try_to_merge_with_ksm_page(rmap_item, page, kpage);
  1298. if (!err) {
  1299. /*
  1300. * The page was successfully merged:
  1301. * add its rmap_item to the stable tree.
  1302. */
  1303. lock_page(kpage);
  1304. stable_tree_append(rmap_item, page_stable_node(kpage));
  1305. unlock_page(kpage);
  1306. }
  1307. put_page(kpage);
  1308. return;
  1309. }
  1310. /*
  1311. * If the hash value of the page has changed from the last time
  1312. * we calculated it, this page is changing frequently: therefore we
  1313. * don't want to insert it in the unstable tree, and we don't want
  1314. * to waste our time searching for something identical to it there.
  1315. */
  1316. checksum = calc_checksum(page);
  1317. if (rmap_item->oldchecksum != checksum) {
  1318. rmap_item->oldchecksum = checksum;
  1319. return;
  1320. }
  1321. tree_rmap_item =
  1322. unstable_tree_search_insert(rmap_item, page, &tree_page);
  1323. if (tree_rmap_item) {
  1324. kpage = try_to_merge_two_pages(rmap_item, page,
  1325. tree_rmap_item, tree_page);
  1326. put_page(tree_page);
  1327. if (kpage) {
  1328. /*
  1329. * The pages were successfully merged: insert new
  1330. * node in the stable tree and add both rmap_items.
  1331. */
  1332. lock_page(kpage);
  1333. stable_node = stable_tree_insert(kpage);
  1334. if (stable_node) {
  1335. stable_tree_append(tree_rmap_item, stable_node);
  1336. stable_tree_append(rmap_item, stable_node);
  1337. }
  1338. unlock_page(kpage);
  1339. /*
  1340. * If we fail to insert the page into the stable tree,
  1341. * we will have 2 virtual addresses that are pointing
  1342. * to a ksm page left outside the stable tree,
  1343. * in which case we need to break_cow on both.
  1344. */
  1345. if (!stable_node) {
  1346. break_cow(tree_rmap_item);
  1347. break_cow(rmap_item);
  1348. }
  1349. }
  1350. }
  1351. }
  1352. static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot,
  1353. struct rmap_item **rmap_list,
  1354. unsigned long addr)
  1355. {
  1356. struct rmap_item *rmap_item;
  1357. while (*rmap_list) {
  1358. rmap_item = *rmap_list;
  1359. if ((rmap_item->address & PAGE_MASK) == addr)
  1360. return rmap_item;
  1361. if (rmap_item->address > addr)
  1362. break;
  1363. *rmap_list = rmap_item->rmap_list;
  1364. remove_rmap_item_from_tree(rmap_item);
  1365. free_rmap_item(rmap_item);
  1366. }
  1367. rmap_item = alloc_rmap_item();
  1368. if (rmap_item) {
  1369. /* It has already been zeroed */
  1370. rmap_item->mm = mm_slot->mm;
  1371. rmap_item->address = addr;
  1372. rmap_item->rmap_list = *rmap_list;
  1373. *rmap_list = rmap_item;
  1374. }
  1375. return rmap_item;
  1376. }
  1377. static struct rmap_item *scan_get_next_rmap_item(struct page **page)
  1378. {
  1379. struct mm_struct *mm;
  1380. struct mm_slot *slot;
  1381. struct vm_area_struct *vma;
  1382. struct rmap_item *rmap_item;
  1383. int nid;
  1384. if (list_empty(&ksm_mm_head.mm_list))
  1385. return NULL;
  1386. slot = ksm_scan.mm_slot;
  1387. if (slot == &ksm_mm_head) {
  1388. /*
  1389. * A number of pages can hang around indefinitely on per-cpu
  1390. * pagevecs, raised page count preventing write_protect_page
  1391. * from merging them. Though it doesn't really matter much,
  1392. * it is puzzling to see some stuck in pages_volatile until
  1393. * other activity jostles them out, and they also prevented
  1394. * LTP's KSM test from succeeding deterministically; so drain
  1395. * them here (here rather than on entry to ksm_do_scan(),
  1396. * so we don't IPI too often when pages_to_scan is set low).
  1397. */
  1398. lru_add_drain_all();
  1399. /*
  1400. * Whereas stale stable_nodes on the stable_tree itself
  1401. * get pruned in the regular course of stable_tree_search(),
  1402. * those moved out to the migrate_nodes list can accumulate:
  1403. * so prune them once before each full scan.
  1404. */
  1405. if (!ksm_merge_across_nodes) {
  1406. struct stable_node *stable_node;
  1407. struct list_head *this, *next;
  1408. struct page *page;
  1409. list_for_each_safe(this, next, &migrate_nodes) {
  1410. stable_node = list_entry(this,
  1411. struct stable_node, list);
  1412. page = get_ksm_page(stable_node, false);
  1413. if (page)
  1414. put_page(page);
  1415. cond_resched();
  1416. }
  1417. }
  1418. for (nid = 0; nid < ksm_nr_node_ids; nid++)
  1419. root_unstable_tree[nid] = RB_ROOT;
  1420. spin_lock(&ksm_mmlist_lock);
  1421. slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list);
  1422. ksm_scan.mm_slot = slot;
  1423. spin_unlock(&ksm_mmlist_lock);
  1424. /*
  1425. * Although we tested list_empty() above, a racing __ksm_exit
  1426. * of the last mm on the list may have removed it since then.
  1427. */
  1428. if (slot == &ksm_mm_head)
  1429. return NULL;
  1430. next_mm:
  1431. ksm_scan.address = 0;
  1432. ksm_scan.rmap_list = &slot->rmap_list;
  1433. }
  1434. mm = slot->mm;
  1435. down_read(&mm->mmap_sem);
  1436. if (ksm_test_exit(mm))
  1437. vma = NULL;
  1438. else
  1439. vma = find_vma(mm, ksm_scan.address);
  1440. for (; vma; vma = vma->vm_next) {
  1441. if (!(vma->vm_flags & VM_MERGEABLE))
  1442. continue;
  1443. if (ksm_scan.address < vma->vm_start)
  1444. ksm_scan.address = vma->vm_start;
  1445. if (!vma->anon_vma)
  1446. ksm_scan.address = vma->vm_end;
  1447. while (ksm_scan.address < vma->vm_end) {
  1448. if (ksm_test_exit(mm))
  1449. break;
  1450. *page = follow_page(vma, ksm_scan.address, FOLL_GET);
  1451. if (IS_ERR_OR_NULL(*page)) {
  1452. ksm_scan.address += PAGE_SIZE;
  1453. cond_resched();
  1454. continue;
  1455. }
  1456. if (PageAnon(*page) ||
  1457. page_trans_compound_anon(*page)) {
  1458. flush_anon_page(vma, *page, ksm_scan.address);
  1459. flush_dcache_page(*page);
  1460. rmap_item = get_next_rmap_item(slot,
  1461. ksm_scan.rmap_list, ksm_scan.address);
  1462. if (rmap_item) {
  1463. ksm_scan.rmap_list =
  1464. &rmap_item->rmap_list;
  1465. ksm_scan.address += PAGE_SIZE;
  1466. } else
  1467. put_page(*page);
  1468. up_read(&mm->mmap_sem);
  1469. return rmap_item;
  1470. }
  1471. put_page(*page);
  1472. ksm_scan.address += PAGE_SIZE;
  1473. cond_resched();
  1474. }
  1475. }
  1476. if (ksm_test_exit(mm)) {
  1477. ksm_scan.address = 0;
  1478. ksm_scan.rmap_list = &slot->rmap_list;
  1479. }
  1480. /*
  1481. * Nuke all the rmap_items that are above this current rmap:
  1482. * because there were no VM_MERGEABLE vmas with such addresses.
  1483. */
  1484. remove_trailing_rmap_items(slot, ksm_scan.rmap_list);
  1485. spin_lock(&ksm_mmlist_lock);
  1486. ksm_scan.mm_slot = list_entry(slot->mm_list.next,
  1487. struct mm_slot, mm_list);
  1488. if (ksm_scan.address == 0) {
  1489. /*
  1490. * We've completed a full scan of all vmas, holding mmap_sem
  1491. * throughout, and found no VM_MERGEABLE: so do the same as
  1492. * __ksm_exit does to remove this mm from all our lists now.
  1493. * This applies either when cleaning up after __ksm_exit
  1494. * (but beware: we can reach here even before __ksm_exit),
  1495. * or when all VM_MERGEABLE areas have been unmapped (and
  1496. * mmap_sem then protects against race with MADV_MERGEABLE).
  1497. */
  1498. hash_del(&slot->link);
  1499. list_del(&slot->mm_list);
  1500. spin_unlock(&ksm_mmlist_lock);
  1501. free_mm_slot(slot);
  1502. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  1503. up_read(&mm->mmap_sem);
  1504. mmdrop(mm);
  1505. } else {
  1506. spin_unlock(&ksm_mmlist_lock);
  1507. up_read(&mm->mmap_sem);
  1508. }
  1509. /* Repeat until we've completed scanning the whole list */
  1510. slot = ksm_scan.mm_slot;
  1511. if (slot != &ksm_mm_head)
  1512. goto next_mm;
  1513. ksm_scan.seqnr++;
  1514. return NULL;
  1515. }
  1516. /**
  1517. * ksm_do_scan - the ksm scanner main worker function.
  1518. * @scan_npages - number of pages we want to scan before we return.
  1519. */
  1520. static void ksm_do_scan(unsigned int scan_npages)
  1521. {
  1522. struct rmap_item *rmap_item;
  1523. struct page *uninitialized_var(page);
  1524. while (scan_npages-- && likely(!freezing(current))) {
  1525. cond_resched();
  1526. rmap_item = scan_get_next_rmap_item(&page);
  1527. if (!rmap_item)
  1528. return;
  1529. cmp_and_merge_page(page, rmap_item);
  1530. put_page(page);
  1531. }
  1532. }
  1533. /*
  1534. * LCH_ADD: ksm kernel control interface for run or stop
  1535. * flags: 1(KSM_RUN_MERGE) sets ksmd running
  1536. * 0 sets ksmd stop running
  1537. * return: 0 success
  1538. * others error
  1539. */
  1540. #define KSM_KCTL_INTERFACE
  1541. #ifdef KSM_KCTL_INTERFACE
  1542. static ssize_t ksm_run_change(unsigned long flags)
  1543. {
  1544. int err = 0;
  1545. if (flags > KSM_RUN_UNMERGE)
  1546. return -EINVAL;
  1547. /*
  1548. * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
  1549. * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
  1550. * breaking COW to free the pages_shared (but leaves mm_slots
  1551. * on the list for when ksmd may be set running again).
  1552. */
  1553. mutex_lock(&ksm_thread_mutex);
  1554. wait_while_offlining();
  1555. if (ksm_run != flags) {
  1556. ksm_run = flags;
  1557. if (flags & KSM_RUN_UNMERGE) {
  1558. set_current_oom_origin();
  1559. err = unmerge_and_remove_all_rmap_items();
  1560. clear_current_oom_origin();
  1561. if (err)
  1562. ksm_run = KSM_RUN_STOP;
  1563. }
  1564. }
  1565. mutex_unlock(&ksm_thread_mutex);
  1566. if (flags & KSM_RUN_MERGE)
  1567. wake_up_interruptible(&ksm_thread_wait);
  1568. return err;
  1569. }
  1570. static void ksm_tuning_pressure(void)
  1571. {
  1572. #if NR_CPUS > 1
  1573. if (bat_is_charger_exist() == KAL_TRUE) {
  1574. if (ksm_thread_sleep_millisecs == 20 &&
  1575. ksm_thread_pages_to_scan == 100)
  1576. return;
  1577. /*set to default value */
  1578. ksm_thread_sleep_millisecs = 20;
  1579. ksm_thread_pages_to_scan = 100;
  1580. } else {
  1581. int num_cpus = num_online_cpus();
  1582. int three_quater_cpus = ((3 * num_possible_cpus() * 10)/4 + 5)/10;
  1583. int one_half_cpus = num_possible_cpus() >> 1;
  1584. if (num_cpus >= three_quater_cpus) {
  1585. ksm_thread_sleep_millisecs = 20;
  1586. ksm_thread_pages_to_scan = 100;
  1587. } else if (num_cpus >= one_half_cpus) {
  1588. ksm_thread_sleep_millisecs = 3000;
  1589. ksm_thread_pages_to_scan = 200;
  1590. } else {
  1591. ksm_thread_sleep_millisecs = 10000;
  1592. ksm_thread_pages_to_scan = 200;
  1593. }
  1594. }
  1595. #endif
  1596. }
  1597. #ifdef CONFIG_HAS_EARLYSUSPEND
  1598. static void ksm_early_suspend(struct early_suspend *h)
  1599. {
  1600. ksm_run_change(KSM_RUN_STOP);
  1601. }
  1602. static void ksm_late_resume(struct early_suspend *h)
  1603. {
  1604. ksm_run_change(KSM_RUN_MERGE);
  1605. }
  1606. static struct early_suspend ksm_early_suspend_handler = {
  1607. .suspend = ksm_early_suspend,
  1608. .resume = ksm_late_resume,
  1609. };
  1610. #else /* no CONFIG_HAS_EARLYSUSPEND*/
  1611. static int ksm_fb_notifier_callback(struct notifier_block *p,
  1612. unsigned long event, void *data)
  1613. {
  1614. int blank;
  1615. if (event != FB_EVENT_BLANK)
  1616. return 0;
  1617. blank = *(int *)((struct fb_event *)data)->data;
  1618. if (blank == FB_BLANK_UNBLANK) { /*LCD ON*/
  1619. ksm_run_change(KSM_RUN_MERGE);
  1620. } else if (blank == FB_BLANK_POWERDOWN) { /*LCD OFF*/
  1621. ksm_run_change(KSM_RUN_STOP);
  1622. }
  1623. return 0;
  1624. }
  1625. static struct notifier_block ksm_fb_notifier = {
  1626. .notifier_call = ksm_fb_notifier_callback,
  1627. }
  1628. #endif
  1629. #else /* no KSM_KCTL_INTERFACE*/
  1630. static ssize_t ksm_run_change(unsigned long flags)
  1631. {
  1632. }
  1633. #endif
  1634. EXPORT_SYMBOL(ksm_run_change);
  1635. static int ksmd_should_run(void)
  1636. {
  1637. return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list);
  1638. }
  1639. static int ksm_scan_thread(void *nothing)
  1640. {
  1641. set_freezable();
  1642. /* M: set KSMD's priority to the lowest value */
  1643. set_user_nice(current, 19);
  1644. /* set_user_nice(current, 5); */
  1645. while (!kthread_should_stop()) {
  1646. mutex_lock(&ksm_thread_mutex);
  1647. wait_while_offlining();
  1648. if (ksmd_should_run()) {
  1649. #ifdef KSM_KCTL_INTERFACE
  1650. ksm_tuning_pressure();
  1651. #endif
  1652. ksm_do_scan(ksm_thread_pages_to_scan);
  1653. }
  1654. mutex_unlock(&ksm_thread_mutex);
  1655. try_to_freeze();
  1656. if (ksmd_should_run()) {
  1657. schedule_timeout_interruptible(
  1658. msecs_to_jiffies(ksm_thread_sleep_millisecs));
  1659. } else {
  1660. wait_event_freezable(ksm_thread_wait,
  1661. ksmd_should_run() || kthread_should_stop());
  1662. }
  1663. }
  1664. return 0;
  1665. }
  1666. int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
  1667. unsigned long end, int advice, unsigned long *vm_flags)
  1668. {
  1669. struct mm_struct *mm = vma->vm_mm;
  1670. int err;
  1671. switch (advice) {
  1672. case MADV_MERGEABLE:
  1673. /*
  1674. * Be somewhat over-protective for now!
  1675. */
  1676. if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
  1677. VM_PFNMAP | VM_IO | VM_DONTEXPAND |
  1678. VM_HUGETLB | VM_NONLINEAR | VM_MIXEDMAP))
  1679. return 0; /* just ignore the advice */
  1680. #ifdef VM_SAO
  1681. if (*vm_flags & VM_SAO)
  1682. return 0;
  1683. #endif
  1684. if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) {
  1685. err = __ksm_enter(mm);
  1686. if (err)
  1687. return err;
  1688. }
  1689. *vm_flags |= VM_MERGEABLE;
  1690. break;
  1691. case MADV_UNMERGEABLE:
  1692. if (!(*vm_flags & VM_MERGEABLE))
  1693. return 0; /* just ignore the advice */
  1694. if (vma->anon_vma) {
  1695. err = unmerge_ksm_pages(vma, start, end);
  1696. if (err)
  1697. return err;
  1698. }
  1699. *vm_flags &= ~VM_MERGEABLE;
  1700. break;
  1701. }
  1702. return 0;
  1703. }
  1704. int __ksm_enter(struct mm_struct *mm)
  1705. {
  1706. struct mm_slot *mm_slot;
  1707. int needs_wakeup;
  1708. mm_slot = alloc_mm_slot();
  1709. if (!mm_slot)
  1710. return -ENOMEM;
  1711. /* Check ksm_run too? Would need tighter locking */
  1712. needs_wakeup = list_empty(&ksm_mm_head.mm_list);
  1713. spin_lock(&ksm_mmlist_lock);
  1714. insert_to_mm_slots_hash(mm, mm_slot);
  1715. /*
  1716. * When KSM_RUN_MERGE (or KSM_RUN_STOP),
  1717. * insert just behind the scanning cursor, to let the area settle
  1718. * down a little; when fork is followed by immediate exec, we don't
  1719. * want ksmd to waste time setting up and tearing down an rmap_list.
  1720. *
  1721. * But when KSM_RUN_UNMERGE, it's important to insert ahead of its
  1722. * scanning cursor, otherwise KSM pages in newly forked mms will be
  1723. * missed: then we might as well insert at the end of the list.
  1724. */
  1725. if (ksm_run & KSM_RUN_UNMERGE)
  1726. list_add_tail(&mm_slot->mm_list, &ksm_mm_head.mm_list);
  1727. else
  1728. list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list);
  1729. spin_unlock(&ksm_mmlist_lock);
  1730. set_bit(MMF_VM_MERGEABLE, &mm->flags);
  1731. atomic_inc(&mm->mm_count);
  1732. if (needs_wakeup)
  1733. wake_up_interruptible(&ksm_thread_wait);
  1734. return 0;
  1735. }
  1736. void __ksm_exit(struct mm_struct *mm)
  1737. {
  1738. struct mm_slot *mm_slot;
  1739. int easy_to_free = 0;
  1740. /*
  1741. * This process is exiting: if it's straightforward (as is the
  1742. * case when ksmd was never running), free mm_slot immediately.
  1743. * But if it's at the cursor or has rmap_items linked to it, use
  1744. * mmap_sem to synchronize with any break_cows before pagetables
  1745. * are freed, and leave the mm_slot on the list for ksmd to free.
  1746. * Beware: ksm may already have noticed it exiting and freed the slot.
  1747. */
  1748. spin_lock(&ksm_mmlist_lock);
  1749. mm_slot = get_mm_slot(mm);
  1750. if (mm_slot && ksm_scan.mm_slot != mm_slot) {
  1751. if (!mm_slot->rmap_list) {
  1752. hash_del(&mm_slot->link);
  1753. list_del(&mm_slot->mm_list);
  1754. easy_to_free = 1;
  1755. } else {
  1756. list_move(&mm_slot->mm_list,
  1757. &ksm_scan.mm_slot->mm_list);
  1758. }
  1759. }
  1760. spin_unlock(&ksm_mmlist_lock);
  1761. if (easy_to_free) {
  1762. free_mm_slot(mm_slot);
  1763. clear_bit(MMF_VM_MERGEABLE, &mm->flags);
  1764. mmdrop(mm);
  1765. } else if (mm_slot) {
  1766. down_write(&mm->mmap_sem);
  1767. up_write(&mm->mmap_sem);
  1768. }
  1769. }
  1770. struct page *ksm_might_need_to_copy(struct page *page,
  1771. struct vm_area_struct *vma, unsigned long address)
  1772. {
  1773. struct anon_vma *anon_vma = page_anon_vma(page);
  1774. struct page *new_page;
  1775. if (PageKsm(page)) {
  1776. if (page_stable_node(page) &&
  1777. !(ksm_run & KSM_RUN_UNMERGE))
  1778. return page; /* no need to copy it */
  1779. } else if (!anon_vma) {
  1780. return page; /* no need to copy it */
  1781. } else if (anon_vma->root == vma->anon_vma->root &&
  1782. page->index == linear_page_index(vma, address)) {
  1783. return page; /* still no need to copy it */
  1784. }
  1785. if (!PageUptodate(page))
  1786. return page; /* let do_swap_page report the error */
  1787. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  1788. if (new_page) {
  1789. copy_user_highpage(new_page, page, address, vma);
  1790. SetPageDirty(new_page);
  1791. __SetPageUptodate(new_page);
  1792. __set_page_locked(new_page);
  1793. }
  1794. return new_page;
  1795. }
  1796. int rmap_walk_ksm(struct page *page, struct rmap_walk_control *rwc)
  1797. {
  1798. struct stable_node *stable_node;
  1799. struct rmap_item *rmap_item;
  1800. int ret = SWAP_AGAIN;
  1801. int search_new_forks = 0;
  1802. VM_BUG_ON_PAGE(!PageKsm(page), page);
  1803. /*
  1804. * Rely on the page lock to protect against concurrent modifications
  1805. * to that page's node of the stable tree.
  1806. */
  1807. VM_BUG_ON_PAGE(!PageLocked(page), page);
  1808. stable_node = page_stable_node(page);
  1809. if (!stable_node)
  1810. return ret;
  1811. again:
  1812. hlist_for_each_entry(rmap_item, &stable_node->hlist, hlist) {
  1813. struct anon_vma *anon_vma = rmap_item->anon_vma;
  1814. struct anon_vma_chain *vmac;
  1815. struct vm_area_struct *vma;
  1816. anon_vma_lock_read(anon_vma);
  1817. anon_vma_interval_tree_foreach(vmac, &anon_vma->rb_root,
  1818. 0, ULONG_MAX) {
  1819. vma = vmac->vma;
  1820. if (rmap_item->address < vma->vm_start ||
  1821. rmap_item->address >= vma->vm_end)
  1822. continue;
  1823. /*
  1824. * Initially we examine only the vma which covers this
  1825. * rmap_item; but later, if there is still work to do,
  1826. * we examine covering vmas in other mms: in case they
  1827. * were forked from the original since ksmd passed.
  1828. */
  1829. if ((rmap_item->mm == vma->vm_mm) == search_new_forks)
  1830. continue;
  1831. if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
  1832. continue;
  1833. ret = rwc->rmap_one(page, vma,
  1834. rmap_item->address, rwc->arg);
  1835. if (ret != SWAP_AGAIN) {
  1836. anon_vma_unlock_read(anon_vma);
  1837. goto out;
  1838. }
  1839. if (rwc->done && rwc->done(page)) {
  1840. anon_vma_unlock_read(anon_vma);
  1841. goto out;
  1842. }
  1843. }
  1844. anon_vma_unlock_read(anon_vma);
  1845. }
  1846. if (!search_new_forks++)
  1847. goto again;
  1848. out:
  1849. return ret;
  1850. }
  1851. #ifdef CONFIG_MIGRATION
  1852. void ksm_migrate_page(struct page *newpage, struct page *oldpage)
  1853. {
  1854. struct stable_node *stable_node;
  1855. VM_BUG_ON_PAGE(!PageLocked(oldpage), oldpage);
  1856. VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
  1857. VM_BUG_ON_PAGE(newpage->mapping != oldpage->mapping, newpage);
  1858. stable_node = page_stable_node(newpage);
  1859. if (stable_node) {
  1860. VM_BUG_ON_PAGE(stable_node->kpfn != page_to_pfn(oldpage), oldpage);
  1861. stable_node->kpfn = page_to_pfn(newpage);
  1862. /*
  1863. * newpage->mapping was set in advance; now we need smp_wmb()
  1864. * to make sure that the new stable_node->kpfn is visible
  1865. * to get_ksm_page() before it can see that oldpage->mapping
  1866. * has gone stale (or that PageSwapCache has been cleared).
  1867. */
  1868. smp_wmb();
  1869. set_page_stable_node(oldpage, NULL);
  1870. }
  1871. }
  1872. #endif /* CONFIG_MIGRATION */
  1873. #ifdef CONFIG_MEMORY_HOTREMOVE
  1874. static void wait_while_offlining(void)
  1875. {
  1876. while (ksm_run & KSM_RUN_OFFLINE) {
  1877. mutex_unlock(&ksm_thread_mutex);
  1878. wait_on_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE),
  1879. TASK_UNINTERRUPTIBLE);
  1880. mutex_lock(&ksm_thread_mutex);
  1881. }
  1882. }
  1883. static void ksm_check_stable_tree(unsigned long start_pfn,
  1884. unsigned long end_pfn)
  1885. {
  1886. struct stable_node *stable_node;
  1887. struct list_head *this, *next;
  1888. struct rb_node *node;
  1889. int nid;
  1890. for (nid = 0; nid < ksm_nr_node_ids; nid++) {
  1891. node = rb_first(root_stable_tree + nid);
  1892. while (node) {
  1893. stable_node = rb_entry(node, struct stable_node, node);
  1894. if (stable_node->kpfn >= start_pfn &&
  1895. stable_node->kpfn < end_pfn) {
  1896. /*
  1897. * Don't get_ksm_page, page has already gone:
  1898. * which is why we keep kpfn instead of page*
  1899. */
  1900. remove_node_from_stable_tree(stable_node);
  1901. node = rb_first(root_stable_tree + nid);
  1902. } else
  1903. node = rb_next(node);
  1904. cond_resched();
  1905. }
  1906. }
  1907. list_for_each_safe(this, next, &migrate_nodes) {
  1908. stable_node = list_entry(this, struct stable_node, list);
  1909. if (stable_node->kpfn >= start_pfn &&
  1910. stable_node->kpfn < end_pfn)
  1911. remove_node_from_stable_tree(stable_node);
  1912. cond_resched();
  1913. }
  1914. }
  1915. static int ksm_memory_callback(struct notifier_block *self,
  1916. unsigned long action, void *arg)
  1917. {
  1918. struct memory_notify *mn = arg;
  1919. switch (action) {
  1920. case MEM_GOING_OFFLINE:
  1921. /*
  1922. * Prevent ksm_do_scan(), unmerge_and_remove_all_rmap_items()
  1923. * and remove_all_stable_nodes() while memory is going offline:
  1924. * it is unsafe for them to touch the stable tree at this time.
  1925. * But unmerge_ksm_pages(), rmap lookups and other entry points
  1926. * which do not need the ksm_thread_mutex are all safe.
  1927. */
  1928. mutex_lock(&ksm_thread_mutex);
  1929. ksm_run |= KSM_RUN_OFFLINE;
  1930. mutex_unlock(&ksm_thread_mutex);
  1931. break;
  1932. case MEM_OFFLINE:
  1933. /*
  1934. * Most of the work is done by page migration; but there might
  1935. * be a few stable_nodes left over, still pointing to struct
  1936. * pages which have been offlined: prune those from the tree,
  1937. * otherwise get_ksm_page() might later try to access a
  1938. * non-existent struct page.
  1939. */
  1940. ksm_check_stable_tree(mn->start_pfn,
  1941. mn->start_pfn + mn->nr_pages);
  1942. /* fallthrough */
  1943. case MEM_CANCEL_OFFLINE:
  1944. mutex_lock(&ksm_thread_mutex);
  1945. ksm_run &= ~KSM_RUN_OFFLINE;
  1946. mutex_unlock(&ksm_thread_mutex);
  1947. smp_mb(); /* wake_up_bit advises this */
  1948. wake_up_bit(&ksm_run, ilog2(KSM_RUN_OFFLINE));
  1949. break;
  1950. }
  1951. return NOTIFY_OK;
  1952. }
  1953. #else
  1954. static void wait_while_offlining(void)
  1955. {
  1956. }
  1957. #endif /* CONFIG_MEMORY_HOTREMOVE */
  1958. #ifdef CONFIG_SYSFS
  1959. /*
  1960. * This all compiles without CONFIG_SYSFS, but is a waste of space.
  1961. */
  1962. #define KSM_ATTR_RO(_name) \
  1963. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1964. #define KSM_ATTR(_name) \
  1965. static struct kobj_attribute _name##_attr = \
  1966. __ATTR(_name, 0644, _name##_show, _name##_store)
  1967. static ssize_t sleep_millisecs_show(struct kobject *kobj,
  1968. struct kobj_attribute *attr, char *buf)
  1969. {
  1970. return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs);
  1971. }
  1972. static ssize_t sleep_millisecs_store(struct kobject *kobj,
  1973. struct kobj_attribute *attr,
  1974. const char *buf, size_t count)
  1975. {
  1976. unsigned long msecs;
  1977. int err;
  1978. err = kstrtoul(buf, 10, &msecs);
  1979. if (err || msecs > UINT_MAX)
  1980. return -EINVAL;
  1981. ksm_thread_sleep_millisecs = msecs;
  1982. return count;
  1983. }
  1984. KSM_ATTR(sleep_millisecs);
  1985. static ssize_t pages_to_scan_show(struct kobject *kobj,
  1986. struct kobj_attribute *attr, char *buf)
  1987. {
  1988. return sprintf(buf, "%u\n", ksm_thread_pages_to_scan);
  1989. }
  1990. static ssize_t pages_to_scan_store(struct kobject *kobj,
  1991. struct kobj_attribute *attr,
  1992. const char *buf, size_t count)
  1993. {
  1994. int err;
  1995. unsigned long nr_pages;
  1996. err = kstrtoul(buf, 10, &nr_pages);
  1997. if (err || nr_pages > UINT_MAX)
  1998. return -EINVAL;
  1999. ksm_thread_pages_to_scan = nr_pages;
  2000. return count;
  2001. }
  2002. KSM_ATTR(pages_to_scan);
  2003. static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr,
  2004. char *buf)
  2005. {
  2006. return sprintf(buf, "%lu\n", ksm_run);
  2007. }
  2008. static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr,
  2009. const char *buf, size_t count)
  2010. {
  2011. int err;
  2012. unsigned long flags;
  2013. err = kstrtoul(buf, 10, &flags);
  2014. if (err || flags > UINT_MAX)
  2015. return -EINVAL;
  2016. if (flags > KSM_RUN_UNMERGE)
  2017. return -EINVAL;
  2018. /*
  2019. * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
  2020. * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
  2021. * breaking COW to free the pages_shared (but leaves mm_slots
  2022. * on the list for when ksmd may be set running again).
  2023. */
  2024. mutex_lock(&ksm_thread_mutex);
  2025. wait_while_offlining();
  2026. if (ksm_run != flags) {
  2027. ksm_run = flags;
  2028. if (flags & KSM_RUN_UNMERGE) {
  2029. set_current_oom_origin();
  2030. err = unmerge_and_remove_all_rmap_items();
  2031. clear_current_oom_origin();
  2032. if (err) {
  2033. ksm_run = KSM_RUN_STOP;
  2034. count = err;
  2035. }
  2036. }
  2037. }
  2038. mutex_unlock(&ksm_thread_mutex);
  2039. if (flags & KSM_RUN_MERGE)
  2040. wake_up_interruptible(&ksm_thread_wait);
  2041. return count;
  2042. }
  2043. KSM_ATTR(run);
  2044. #ifdef CONFIG_NUMA
  2045. static ssize_t merge_across_nodes_show(struct kobject *kobj,
  2046. struct kobj_attribute *attr, char *buf)
  2047. {
  2048. return sprintf(buf, "%u\n", ksm_merge_across_nodes);
  2049. }
  2050. static ssize_t merge_across_nodes_store(struct kobject *kobj,
  2051. struct kobj_attribute *attr,
  2052. const char *buf, size_t count)
  2053. {
  2054. int err;
  2055. unsigned long knob;
  2056. err = kstrtoul(buf, 10, &knob);
  2057. if (err)
  2058. return err;
  2059. if (knob > 1)
  2060. return -EINVAL;
  2061. mutex_lock(&ksm_thread_mutex);
  2062. wait_while_offlining();
  2063. if (ksm_merge_across_nodes != knob) {
  2064. if (ksm_pages_shared || remove_all_stable_nodes())
  2065. err = -EBUSY;
  2066. else if (root_stable_tree == one_stable_tree) {
  2067. struct rb_root *buf;
  2068. /*
  2069. * This is the first time that we switch away from the
  2070. * default of merging across nodes: must now allocate
  2071. * a buffer to hold as many roots as may be needed.
  2072. * Allocate stable and unstable together:
  2073. * MAXSMP NODES_SHIFT 10 will use 16kB.
  2074. */
  2075. buf = kcalloc(nr_node_ids + nr_node_ids, sizeof(*buf),
  2076. GFP_KERNEL);
  2077. /* Let us assume that RB_ROOT is NULL is zero */
  2078. if (!buf)
  2079. err = -ENOMEM;
  2080. else {
  2081. root_stable_tree = buf;
  2082. root_unstable_tree = buf + nr_node_ids;
  2083. /* Stable tree is empty but not the unstable */
  2084. root_unstable_tree[0] = one_unstable_tree[0];
  2085. }
  2086. }
  2087. if (!err) {
  2088. ksm_merge_across_nodes = knob;
  2089. ksm_nr_node_ids = knob ? 1 : nr_node_ids;
  2090. }
  2091. }
  2092. mutex_unlock(&ksm_thread_mutex);
  2093. return err ? err : count;
  2094. }
  2095. KSM_ATTR(merge_across_nodes);
  2096. #endif
  2097. static ssize_t pages_shared_show(struct kobject *kobj,
  2098. struct kobj_attribute *attr, char *buf)
  2099. {
  2100. return sprintf(buf, "%lu\n", ksm_pages_shared);
  2101. }
  2102. KSM_ATTR_RO(pages_shared);
  2103. static ssize_t pages_sharing_show(struct kobject *kobj,
  2104. struct kobj_attribute *attr, char *buf)
  2105. {
  2106. return sprintf(buf, "%lu\n", ksm_pages_sharing);
  2107. }
  2108. KSM_ATTR_RO(pages_sharing);
  2109. static ssize_t pages_unshared_show(struct kobject *kobj,
  2110. struct kobj_attribute *attr, char *buf)
  2111. {
  2112. return sprintf(buf, "%lu\n", ksm_pages_unshared);
  2113. }
  2114. KSM_ATTR_RO(pages_unshared);
  2115. static ssize_t pages_volatile_show(struct kobject *kobj,
  2116. struct kobj_attribute *attr, char *buf)
  2117. {
  2118. long ksm_pages_volatile;
  2119. ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared
  2120. - ksm_pages_sharing - ksm_pages_unshared;
  2121. /*
  2122. * It was not worth any locking to calculate that statistic,
  2123. * but it might therefore sometimes be negative: conceal that.
  2124. */
  2125. if (ksm_pages_volatile < 0)
  2126. ksm_pages_volatile = 0;
  2127. return sprintf(buf, "%ld\n", ksm_pages_volatile);
  2128. }
  2129. KSM_ATTR_RO(pages_volatile);
  2130. static ssize_t full_scans_show(struct kobject *kobj,
  2131. struct kobj_attribute *attr, char *buf)
  2132. {
  2133. return sprintf(buf, "%lu\n", ksm_scan.seqnr);
  2134. }
  2135. KSM_ATTR_RO(full_scans);
  2136. static struct attribute *ksm_attrs[] = {
  2137. &sleep_millisecs_attr.attr,
  2138. &pages_to_scan_attr.attr,
  2139. &run_attr.attr,
  2140. &pages_shared_attr.attr,
  2141. &pages_sharing_attr.attr,
  2142. &pages_unshared_attr.attr,
  2143. &pages_volatile_attr.attr,
  2144. &full_scans_attr.attr,
  2145. #ifdef CONFIG_NUMA
  2146. &merge_across_nodes_attr.attr,
  2147. #endif
  2148. NULL,
  2149. };
  2150. static struct attribute_group ksm_attr_group = {
  2151. .attrs = ksm_attrs,
  2152. .name = "ksm",
  2153. };
  2154. #endif /* CONFIG_SYSFS */
  2155. static int __init ksm_init(void)
  2156. {
  2157. struct task_struct *ksm_thread;
  2158. int err;
  2159. err = ksm_slab_init();
  2160. if (err)
  2161. goto out;
  2162. ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd");
  2163. if (IS_ERR(ksm_thread)) {
  2164. pr_err("ksm: creating kthread failed\n");
  2165. err = PTR_ERR(ksm_thread);
  2166. goto out_free;
  2167. }
  2168. #ifdef CONFIG_SYSFS
  2169. err = sysfs_create_group(mm_kobj, &ksm_attr_group);
  2170. if (err) {
  2171. pr_err("ksm: register sysfs failed\n");
  2172. kthread_stop(ksm_thread);
  2173. goto out_free;
  2174. }
  2175. #else
  2176. ksm_run = KSM_RUN_MERGE; /* no way for user to start it */
  2177. #endif /* CONFIG_SYSFS */
  2178. #ifdef CONFIG_MEMORY_HOTREMOVE
  2179. /* There is no significance to this priority 100 */
  2180. hotplug_memory_notifier(ksm_memory_callback, 100);
  2181. #endif
  2182. #ifdef KSM_KCTL_INTERFACE
  2183. #ifdef CONFIG_HAS_EARLYSUSPEND
  2184. register_early_suspend(&ksm_early_suspend_handler);
  2185. #else
  2186. err = fb_register_client(&ksm_fb_notifier);
  2187. if (err) {
  2188. pr_err("ksm: unable to register fb_notifier\n");
  2189. kthread_stop(ksm_thread);
  2190. sysfs_remove_group(mm_kobj, &ksm_attr_group);
  2191. goto out_free;
  2192. }
  2193. #endif
  2194. #endif
  2195. return 0;
  2196. out_free:
  2197. ksm_slab_free();
  2198. out:
  2199. return err;
  2200. }
  2201. subsys_initcall(ksm_init);