2 * Memory merging support.
4 * This code enables dynamic sharing of identical pages found in different
5 * memory areas, even if they are not shared by fork()
7 * Copyright (C) 2008-2009 Red Hat, Inc.
14 * This work is licensed under the terms of the GNU GPL, version 2.
17 #include <linux/errno.h>
20 #include <linux/mman.h>
21 #include <linux/sched.h>
22 #include <linux/rwsem.h>
23 #include <linux/pagemap.h>
24 #include <linux/rmap.h>
25 #include <linux/spinlock.h>
26 #include <linux/jhash.h>
27 #include <linux/delay.h>
28 #include <linux/kthread.h>
29 #include <linux/wait.h>
30 #include <linux/slab.h>
31 #include <linux/rbtree.h>
32 #include <linux/mmu_notifier.h>
33 #include <linux/swap.h>
34 #include <linux/ksm.h>
36 #include <asm/tlbflush.h>
39 * A few notes about the KSM scanning process,
40 * to make it easier to understand the data structures below:
42 * In order to reduce excessive scanning, KSM sorts the memory pages by their
43 * contents into a data structure that holds pointers to the pages' locations.
45 * Since the contents of the pages may change at any moment, KSM cannot just
46 * insert the pages into a normal sorted tree and expect it to find anything.
47 * Therefore KSM uses two data structures - the stable and the unstable tree.
49 * The stable tree holds pointers to all the merged pages (ksm pages), sorted
50 * by their contents. Because each such page is write-protected, searching on
51 * this tree is fully assured to be working (except when pages are unmapped),
52 * and therefore this tree is called the stable tree.
54 * In addition to the stable tree, KSM uses a second data structure called the
55 * unstable tree: this tree holds pointers to pages which have been found to
56 * be "unchanged for a period of time". The unstable tree sorts these pages
57 * by their contents, but since they are not write-protected, KSM cannot rely
58 * upon the unstable tree to work correctly - the unstable tree is liable to
59 * be corrupted as its contents are modified, and so it is called unstable.
61 * KSM solves this problem by several techniques:
63 * 1) The unstable tree is flushed every time KSM completes scanning all
64 * memory areas, and then the tree is rebuilt again from the beginning.
65 * 2) KSM will only insert into the unstable tree, pages whose hash value
66 * has not changed since the previous scan of all memory areas.
67 * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the
68 * colors of the nodes and not on their contents, assuring that even when
69 * the tree gets "corrupted" it won't get out of balance, so scanning time
70 * remains the same (also, searching and inserting nodes in an rbtree uses
71 * the same algorithm, so we have no overhead when we flush and rebuild).
72 * 4) KSM never flushes the stable tree, which means that even if it were to
73 * take 10 attempts to find a page in the unstable tree, once it is found,
74 * it is secured in the stable tree. (When we scan a new page, we first
75 * compare it against the stable tree, and then against the unstable tree.)
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
86 struct hlist_node link
;
87 struct list_head mm_list
;
88 struct rmap_item
*rmap_list
;
93 * struct ksm_scan - cursor for scanning
94 * @mm_slot: the current mm_slot we are scanning
95 * @address: the next address inside that to be scanned
96 * @rmap_list: link to the next rmap to be scanned in the rmap_list
97 * @seqnr: count of completed full scans (needed when removing unstable node)
99 * There is only the one ksm_scan instance of this cursor structure.
102 struct mm_slot
*mm_slot
;
103 unsigned long address
;
104 struct rmap_item
**rmap_list
;
109 * struct stable_node - node of the stable rbtree
110 * @page: pointer to struct page of the ksm page
111 * @node: rb node of this ksm page in the stable tree
112 * @hlist: hlist head of rmap_items using this ksm page
117 struct hlist_head hlist
;
121 * struct rmap_item - reverse mapping item for virtual addresses
122 * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list
123 * @filler: unused space we're making available in this patch
124 * @mm: the memory structure this rmap_item is pointing into
125 * @address: the virtual address this rmap_item tracks (+ flags in low bits)
126 * @oldchecksum: previous checksum of the page at that virtual address
127 * @node: rb node of this rmap_item in the unstable tree
128 * @head: pointer to stable_node heading this list in the stable tree
129 * @hlist: link into hlist of rmap_items hanging off that stable_node
132 struct rmap_item
*rmap_list
;
133 unsigned long filler
;
134 struct mm_struct
*mm
;
135 unsigned long address
; /* + low bits used for flags below */
136 unsigned int oldchecksum
; /* when unstable */
138 struct rb_node node
; /* when node of unstable tree */
139 struct { /* when listed from stable tree */
140 struct stable_node
*head
;
141 struct hlist_node hlist
;
146 #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */
147 #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */
148 #define STABLE_FLAG 0x200 /* is listed from the stable tree */
150 /* The stable and unstable tree heads */
151 static struct rb_root root_stable_tree
= RB_ROOT
;
152 static struct rb_root root_unstable_tree
= RB_ROOT
;
154 #define MM_SLOTS_HASH_HEADS 1024
155 static struct hlist_head
*mm_slots_hash
;
157 static struct mm_slot ksm_mm_head
= {
158 .mm_list
= LIST_HEAD_INIT(ksm_mm_head
.mm_list
),
160 static struct ksm_scan ksm_scan
= {
161 .mm_slot
= &ksm_mm_head
,
164 static struct kmem_cache
*rmap_item_cache
;
165 static struct kmem_cache
*stable_node_cache
;
166 static struct kmem_cache
*mm_slot_cache
;
168 /* The number of nodes in the stable tree */
169 static unsigned long ksm_pages_shared
;
171 /* The number of page slots additionally sharing those nodes */
172 static unsigned long ksm_pages_sharing
;
174 /* The number of nodes in the unstable tree */
175 static unsigned long ksm_pages_unshared
;
177 /* The number of rmap_items in use: to calculate pages_volatile */
178 static unsigned long ksm_rmap_items
;
180 /* Limit on the number of unswappable pages used */
181 static unsigned long ksm_max_kernel_pages
;
183 /* Number of pages ksmd should scan in one batch */
184 static unsigned int ksm_thread_pages_to_scan
= 100;
186 /* Milliseconds ksmd should sleep between batches */
187 static unsigned int ksm_thread_sleep_millisecs
= 20;
189 #define KSM_RUN_STOP 0
190 #define KSM_RUN_MERGE 1
191 #define KSM_RUN_UNMERGE 2
192 static unsigned int ksm_run
= KSM_RUN_STOP
;
194 static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait
);
195 static DEFINE_MUTEX(ksm_thread_mutex
);
196 static DEFINE_SPINLOCK(ksm_mmlist_lock
);
198 #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\
199 sizeof(struct __struct), __alignof__(struct __struct),\
202 static int __init
ksm_slab_init(void)
204 rmap_item_cache
= KSM_KMEM_CACHE(rmap_item
, 0);
205 if (!rmap_item_cache
)
208 stable_node_cache
= KSM_KMEM_CACHE(stable_node
, 0);
209 if (!stable_node_cache
)
212 mm_slot_cache
= KSM_KMEM_CACHE(mm_slot
, 0);
219 kmem_cache_destroy(stable_node_cache
);
221 kmem_cache_destroy(rmap_item_cache
);
226 static void __init
ksm_slab_free(void)
228 kmem_cache_destroy(mm_slot_cache
);
229 kmem_cache_destroy(stable_node_cache
);
230 kmem_cache_destroy(rmap_item_cache
);
231 mm_slot_cache
= NULL
;
234 static inline struct rmap_item
*alloc_rmap_item(void)
236 struct rmap_item
*rmap_item
;
238 rmap_item
= kmem_cache_zalloc(rmap_item_cache
, GFP_KERNEL
);
244 static inline void free_rmap_item(struct rmap_item
*rmap_item
)
247 rmap_item
->mm
= NULL
; /* debug safety */
248 kmem_cache_free(rmap_item_cache
, rmap_item
);
251 static inline struct stable_node
*alloc_stable_node(void)
253 return kmem_cache_alloc(stable_node_cache
, GFP_KERNEL
);
256 static inline void free_stable_node(struct stable_node
*stable_node
)
258 kmem_cache_free(stable_node_cache
, stable_node
);
261 static inline struct mm_slot
*alloc_mm_slot(void)
263 if (!mm_slot_cache
) /* initialization failed */
265 return kmem_cache_zalloc(mm_slot_cache
, GFP_KERNEL
);
268 static inline void free_mm_slot(struct mm_slot
*mm_slot
)
270 kmem_cache_free(mm_slot_cache
, mm_slot
);
273 static int __init
mm_slots_hash_init(void)
275 mm_slots_hash
= kzalloc(MM_SLOTS_HASH_HEADS
* sizeof(struct hlist_head
),
282 static void __init
mm_slots_hash_free(void)
284 kfree(mm_slots_hash
);
287 static struct mm_slot
*get_mm_slot(struct mm_struct
*mm
)
289 struct mm_slot
*mm_slot
;
290 struct hlist_head
*bucket
;
291 struct hlist_node
*node
;
293 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
294 % MM_SLOTS_HASH_HEADS
];
295 hlist_for_each_entry(mm_slot
, node
, bucket
, link
) {
296 if (mm
== mm_slot
->mm
)
302 static void insert_to_mm_slots_hash(struct mm_struct
*mm
,
303 struct mm_slot
*mm_slot
)
305 struct hlist_head
*bucket
;
307 bucket
= &mm_slots_hash
[((unsigned long)mm
/ sizeof(struct mm_struct
))
308 % MM_SLOTS_HASH_HEADS
];
310 hlist_add_head(&mm_slot
->link
, bucket
);
313 static inline int in_stable_tree(struct rmap_item
*rmap_item
)
315 return rmap_item
->address
& STABLE_FLAG
;
319 * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's
320 * page tables after it has passed through ksm_exit() - which, if necessary,
321 * takes mmap_sem briefly to serialize against them. ksm_exit() does not set
322 * a special flag: they can just back out as soon as mm_users goes to zero.
323 * ksm_test_exit() is used throughout to make this test for exit: in some
324 * places for correctness, in some places just to avoid unnecessary work.
326 static inline bool ksm_test_exit(struct mm_struct
*mm
)
328 return atomic_read(&mm
->mm_users
) == 0;
332 * We use break_ksm to break COW on a ksm page: it's a stripped down
334 * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1)
337 * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma,
338 * in case the application has unmapped and remapped mm,addr meanwhile.
339 * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP
340 * mmap of /dev/mem or /dev/kmem, where we would not want to touch it.
342 static int break_ksm(struct vm_area_struct
*vma
, unsigned long addr
)
349 page
= follow_page(vma
, addr
, FOLL_GET
);
353 ret
= handle_mm_fault(vma
->vm_mm
, vma
, addr
,
356 ret
= VM_FAULT_WRITE
;
358 } while (!(ret
& (VM_FAULT_WRITE
| VM_FAULT_SIGBUS
| VM_FAULT_OOM
)));
360 * We must loop because handle_mm_fault() may back out if there's
361 * any difficulty e.g. if pte accessed bit gets updated concurrently.
363 * VM_FAULT_WRITE is what we have been hoping for: it indicates that
364 * COW has been broken, even if the vma does not permit VM_WRITE;
365 * but note that a concurrent fault might break PageKsm for us.
367 * VM_FAULT_SIGBUS could occur if we race with truncation of the
368 * backing file, which also invalidates anonymous pages: that's
369 * okay, that truncation will have unmapped the PageKsm for us.
371 * VM_FAULT_OOM: at the time of writing (late July 2009), setting
372 * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the
373 * current task has TIF_MEMDIE set, and will be OOM killed on return
374 * to user; and ksmd, having no mm, would never be chosen for that.
376 * But if the mm is in a limited mem_cgroup, then the fault may fail
377 * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and
378 * even ksmd can fail in this way - though it's usually breaking ksm
379 * just to undo a merge it made a moment before, so unlikely to oom.
381 * That's a pity: we might therefore have more kernel pages allocated
382 * than we're counting as nodes in the stable tree; but ksm_do_scan
383 * will retry to break_cow on each pass, so should recover the page
384 * in due course. The important thing is to not let VM_MERGEABLE
385 * be cleared while any such pages might remain in the area.
387 return (ret
& VM_FAULT_OOM
) ? -ENOMEM
: 0;
390 static void break_cow(struct rmap_item
*rmap_item
)
392 struct mm_struct
*mm
= rmap_item
->mm
;
393 unsigned long addr
= rmap_item
->address
;
394 struct vm_area_struct
*vma
;
396 down_read(&mm
->mmap_sem
);
397 if (ksm_test_exit(mm
))
399 vma
= find_vma(mm
, addr
);
400 if (!vma
|| vma
->vm_start
> addr
)
402 if (!(vma
->vm_flags
& VM_MERGEABLE
) || !vma
->anon_vma
)
404 break_ksm(vma
, addr
);
406 up_read(&mm
->mmap_sem
);
409 static struct page
*get_mergeable_page(struct rmap_item
*rmap_item
)
411 struct mm_struct
*mm
= rmap_item
->mm
;
412 unsigned long addr
= rmap_item
->address
;
413 struct vm_area_struct
*vma
;
416 down_read(&mm
->mmap_sem
);
417 if (ksm_test_exit(mm
))
419 vma
= find_vma(mm
, addr
);
420 if (!vma
|| vma
->vm_start
> addr
)
422 if (!(vma
->vm_flags
& VM_MERGEABLE
) || !vma
->anon_vma
)
425 page
= follow_page(vma
, addr
, FOLL_GET
);
428 if (PageAnon(page
)) {
429 flush_anon_page(vma
, page
, addr
);
430 flush_dcache_page(page
);
435 up_read(&mm
->mmap_sem
);
440 * Removing rmap_item from stable or unstable tree.
441 * This function will clean the information from the stable/unstable tree.
443 static void remove_rmap_item_from_tree(struct rmap_item
*rmap_item
)
445 if (rmap_item
->address
& STABLE_FLAG
) {
446 struct stable_node
*stable_node
;
448 stable_node
= rmap_item
->head
;
449 hlist_del(&rmap_item
->hlist
);
450 if (stable_node
->hlist
.first
)
453 set_page_stable_node(stable_node
->page
, NULL
);
454 put_page(stable_node
->page
);
456 rb_erase(&stable_node
->node
, &root_stable_tree
);
457 free_stable_node(stable_node
);
461 rmap_item
->address
&= PAGE_MASK
;
463 } else if (rmap_item
->address
& UNSTABLE_FLAG
) {
466 * Usually ksmd can and must skip the rb_erase, because
467 * root_unstable_tree was already reset to RB_ROOT.
468 * But be careful when an mm is exiting: do the rb_erase
469 * if this rmap_item was inserted by this scan, rather
470 * than left over from before.
472 age
= (unsigned char)(ksm_scan
.seqnr
- rmap_item
->address
);
475 rb_erase(&rmap_item
->node
, &root_unstable_tree
);
477 ksm_pages_unshared
--;
478 rmap_item
->address
&= PAGE_MASK
;
481 cond_resched(); /* we're called from many long loops */
484 static void remove_trailing_rmap_items(struct mm_slot
*mm_slot
,
485 struct rmap_item
**rmap_list
)
488 struct rmap_item
*rmap_item
= *rmap_list
;
489 *rmap_list
= rmap_item
->rmap_list
;
490 remove_rmap_item_from_tree(rmap_item
);
491 free_rmap_item(rmap_item
);
496 * Though it's very tempting to unmerge in_stable_tree(rmap_item)s rather
497 * than check every pte of a given vma, the locking doesn't quite work for
498 * that - an rmap_item is assigned to the stable tree after inserting ksm
499 * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing
500 * rmap_items from parent to child at fork time (so as not to waste time
501 * if exit comes before the next scan reaches it).
503 * Similarly, although we'd like to remove rmap_items (so updating counts
504 * and freeing memory) when unmerging an area, it's easier to leave that
505 * to the next pass of ksmd - consider, for example, how ksmd might be
506 * in cmp_and_merge_page on one of the rmap_items we would be removing.
508 static int unmerge_ksm_pages(struct vm_area_struct
*vma
,
509 unsigned long start
, unsigned long end
)
514 for (addr
= start
; addr
< end
&& !err
; addr
+= PAGE_SIZE
) {
515 if (ksm_test_exit(vma
->vm_mm
))
517 if (signal_pending(current
))
520 err
= break_ksm(vma
, addr
);
527 * Only called through the sysfs control interface:
529 static int unmerge_and_remove_all_rmap_items(void)
531 struct mm_slot
*mm_slot
;
532 struct mm_struct
*mm
;
533 struct vm_area_struct
*vma
;
536 spin_lock(&ksm_mmlist_lock
);
537 ksm_scan
.mm_slot
= list_entry(ksm_mm_head
.mm_list
.next
,
538 struct mm_slot
, mm_list
);
539 spin_unlock(&ksm_mmlist_lock
);
541 for (mm_slot
= ksm_scan
.mm_slot
;
542 mm_slot
!= &ksm_mm_head
; mm_slot
= ksm_scan
.mm_slot
) {
544 down_read(&mm
->mmap_sem
);
545 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
546 if (ksm_test_exit(mm
))
548 if (!(vma
->vm_flags
& VM_MERGEABLE
) || !vma
->anon_vma
)
550 err
= unmerge_ksm_pages(vma
,
551 vma
->vm_start
, vma
->vm_end
);
556 remove_trailing_rmap_items(mm_slot
, &mm_slot
->rmap_list
);
558 spin_lock(&ksm_mmlist_lock
);
559 ksm_scan
.mm_slot
= list_entry(mm_slot
->mm_list
.next
,
560 struct mm_slot
, mm_list
);
561 if (ksm_test_exit(mm
)) {
562 hlist_del(&mm_slot
->link
);
563 list_del(&mm_slot
->mm_list
);
564 spin_unlock(&ksm_mmlist_lock
);
566 free_mm_slot(mm_slot
);
567 clear_bit(MMF_VM_MERGEABLE
, &mm
->flags
);
568 up_read(&mm
->mmap_sem
);
571 spin_unlock(&ksm_mmlist_lock
);
572 up_read(&mm
->mmap_sem
);
580 up_read(&mm
->mmap_sem
);
581 spin_lock(&ksm_mmlist_lock
);
582 ksm_scan
.mm_slot
= &ksm_mm_head
;
583 spin_unlock(&ksm_mmlist_lock
);
586 #endif /* CONFIG_SYSFS */
588 static u32
calc_checksum(struct page
*page
)
591 void *addr
= kmap_atomic(page
, KM_USER0
);
592 checksum
= jhash2(addr
, PAGE_SIZE
/ 4, 17);
593 kunmap_atomic(addr
, KM_USER0
);
597 static int memcmp_pages(struct page
*page1
, struct page
*page2
)
602 addr1
= kmap_atomic(page1
, KM_USER0
);
603 addr2
= kmap_atomic(page2
, KM_USER1
);
604 ret
= memcmp(addr1
, addr2
, PAGE_SIZE
);
605 kunmap_atomic(addr2
, KM_USER1
);
606 kunmap_atomic(addr1
, KM_USER0
);
610 static inline int pages_identical(struct page
*page1
, struct page
*page2
)
612 return !memcmp_pages(page1
, page2
);
615 static int write_protect_page(struct vm_area_struct
*vma
, struct page
*page
,
618 struct mm_struct
*mm
= vma
->vm_mm
;
625 addr
= page_address_in_vma(page
, vma
);
629 ptep
= page_check_address(page
, mm
, addr
, &ptl
, 0);
633 if (pte_write(*ptep
)) {
636 swapped
= PageSwapCache(page
);
637 flush_cache_page(vma
, addr
, page_to_pfn(page
));
639 * Ok this is tricky, when get_user_pages_fast() run it doesnt
640 * take any lock, therefore the check that we are going to make
641 * with the pagecount against the mapcount is racey and
642 * O_DIRECT can happen right after the check.
643 * So we clear the pte and flush the tlb before the check
644 * this assure us that no O_DIRECT can happen after the check
645 * or in the middle of the check.
647 entry
= ptep_clear_flush(vma
, addr
, ptep
);
649 * Check that no O_DIRECT or similar I/O is in progress on the
652 if (page_mapcount(page
) + 1 + swapped
!= page_count(page
)) {
653 set_pte_at_notify(mm
, addr
, ptep
, entry
);
656 entry
= pte_wrprotect(entry
);
657 set_pte_at_notify(mm
, addr
, ptep
, entry
);
663 pte_unmap_unlock(ptep
, ptl
);
669 * replace_page - replace page in vma by new ksm page
670 * @vma: vma that holds the pte pointing to page
671 * @page: the page we are replacing by kpage
672 * @kpage: the ksm page we replace page by
673 * @orig_pte: the original value of the pte
675 * Returns 0 on success, -EFAULT on failure.
677 static int replace_page(struct vm_area_struct
*vma
, struct page
*page
,
678 struct page
*kpage
, pte_t orig_pte
)
680 struct mm_struct
*mm
= vma
->vm_mm
;
689 addr
= page_address_in_vma(page
, vma
);
693 pgd
= pgd_offset(mm
, addr
);
694 if (!pgd_present(*pgd
))
697 pud
= pud_offset(pgd
, addr
);
698 if (!pud_present(*pud
))
701 pmd
= pmd_offset(pud
, addr
);
702 if (!pmd_present(*pmd
))
705 ptep
= pte_offset_map_lock(mm
, pmd
, addr
, &ptl
);
706 if (!pte_same(*ptep
, orig_pte
)) {
707 pte_unmap_unlock(ptep
, ptl
);
712 page_add_ksm_rmap(kpage
);
714 flush_cache_page(vma
, addr
, pte_pfn(*ptep
));
715 ptep_clear_flush(vma
, addr
, ptep
);
716 set_pte_at_notify(mm
, addr
, ptep
, mk_pte(kpage
, vma
->vm_page_prot
));
718 page_remove_rmap(page
);
721 pte_unmap_unlock(ptep
, ptl
);
728 * try_to_merge_one_page - take two pages and merge them into one
729 * @vma: the vma that holds the pte pointing to page
730 * @page: the PageAnon page that we want to replace with kpage
731 * @kpage: the PageKsm page that we want to map instead of page
733 * This function returns 0 if the pages were merged, -EFAULT otherwise.
735 static int try_to_merge_one_page(struct vm_area_struct
*vma
,
736 struct page
*page
, struct page
*kpage
)
738 pte_t orig_pte
= __pte(0);
741 if (!(vma
->vm_flags
& VM_MERGEABLE
))
747 * We need the page lock to read a stable PageSwapCache in
748 * write_protect_page(). We use trylock_page() instead of
749 * lock_page() because we don't want to wait here - we
750 * prefer to continue scanning and merging different pages,
751 * then come back to this page when it is unlocked.
753 if (!trylock_page(page
))
756 * If this anonymous page is mapped only here, its pte may need
757 * to be write-protected. If it's mapped elsewhere, all of its
758 * ptes are necessarily already write-protected. But in either
759 * case, we need to lock and check page_count is not raised.
761 if (write_protect_page(vma
, page
, &orig_pte
) == 0 &&
762 pages_identical(page
, kpage
))
763 err
= replace_page(vma
, page
, kpage
, orig_pte
);
771 * try_to_merge_with_ksm_page - like try_to_merge_two_pages,
772 * but no new kernel page is allocated: kpage must already be a ksm page.
774 * This function returns 0 if the pages were merged, -EFAULT otherwise.
776 static int try_to_merge_with_ksm_page(struct rmap_item
*rmap_item
,
777 struct page
*page
, struct page
*kpage
)
779 struct mm_struct
*mm
= rmap_item
->mm
;
780 struct vm_area_struct
*vma
;
783 if (page
== kpage
) /* ksm page forked */
786 down_read(&mm
->mmap_sem
);
787 if (ksm_test_exit(mm
))
789 vma
= find_vma(mm
, rmap_item
->address
);
790 if (!vma
|| vma
->vm_start
> rmap_item
->address
)
793 err
= try_to_merge_one_page(vma
, page
, kpage
);
795 up_read(&mm
->mmap_sem
);
800 * try_to_merge_two_pages - take two identical pages and prepare them
801 * to be merged into one page.
803 * This function returns the kpage if we successfully merged two identical
804 * pages into one ksm page, NULL otherwise.
806 * Note that this function allocates a new kernel page: if one of the pages
807 * is already a ksm page, try_to_merge_with_ksm_page should be used.
809 static struct page
*try_to_merge_two_pages(struct rmap_item
*rmap_item
,
811 struct rmap_item
*tree_rmap_item
,
812 struct page
*tree_page
)
814 struct mm_struct
*mm
= rmap_item
->mm
;
815 struct vm_area_struct
*vma
;
820 * The number of nodes in the stable tree
821 * is the number of kernel pages that we hold.
823 if (ksm_max_kernel_pages
&&
824 ksm_max_kernel_pages
<= ksm_pages_shared
)
827 kpage
= alloc_page(GFP_HIGHUSER
);
831 down_read(&mm
->mmap_sem
);
832 if (ksm_test_exit(mm
))
834 vma
= find_vma(mm
, rmap_item
->address
);
835 if (!vma
|| vma
->vm_start
> rmap_item
->address
)
838 copy_user_highpage(kpage
, page
, rmap_item
->address
, vma
);
840 set_page_stable_node(kpage
, NULL
); /* mark it PageKsm */
842 err
= try_to_merge_one_page(vma
, page
, kpage
);
844 up_read(&mm
->mmap_sem
);
847 err
= try_to_merge_with_ksm_page(tree_rmap_item
,
850 * If that fails, we have a ksm page with only one pte
851 * pointing to it: so break it.
854 break_cow(rmap_item
);
864 * stable_tree_search - search for page inside the stable tree
866 * This function checks if there is a page inside the stable tree
867 * with identical content to the page that we are scanning right now.
869 * This function returns the stable tree node of identical content if found,
872 static struct stable_node
*stable_tree_search(struct page
*page
)
874 struct rb_node
*node
= root_stable_tree
.rb_node
;
875 struct stable_node
*stable_node
;
877 stable_node
= page_stable_node(page
);
878 if (stable_node
) { /* ksm page forked */
887 stable_node
= rb_entry(node
, struct stable_node
, node
);
889 ret
= memcmp_pages(page
, stable_node
->page
);
892 node
= node
->rb_left
;
894 node
= node
->rb_right
;
896 get_page(stable_node
->page
);
905 * stable_tree_insert - insert rmap_item pointing to new ksm page
906 * into the stable tree.
908 * This function returns the stable tree node just allocated on success,
911 static struct stable_node
*stable_tree_insert(struct page
*kpage
)
913 struct rb_node
**new = &root_stable_tree
.rb_node
;
914 struct rb_node
*parent
= NULL
;
915 struct stable_node
*stable_node
;
921 stable_node
= rb_entry(*new, struct stable_node
, node
);
923 ret
= memcmp_pages(kpage
, stable_node
->page
);
927 new = &parent
->rb_left
;
929 new = &parent
->rb_right
;
932 * It is not a bug that stable_tree_search() didn't
933 * find this node: because at that time our page was
934 * not yet write-protected, so may have changed since.
940 stable_node
= alloc_stable_node();
944 rb_link_node(&stable_node
->node
, parent
, new);
945 rb_insert_color(&stable_node
->node
, &root_stable_tree
);
947 INIT_HLIST_HEAD(&stable_node
->hlist
);
950 stable_node
->page
= kpage
;
951 set_page_stable_node(kpage
, stable_node
);
957 * unstable_tree_search_insert - search for identical page,
958 * else insert rmap_item into the unstable tree.
960 * This function searches for a page in the unstable tree identical to the
961 * page currently being scanned; and if no identical page is found in the
962 * tree, we insert rmap_item as a new object into the unstable tree.
964 * This function returns pointer to rmap_item found to be identical
965 * to the currently scanned page, NULL otherwise.
967 * This function does both searching and inserting, because they share
968 * the same walking algorithm in an rbtree.
971 struct rmap_item
*unstable_tree_search_insert(struct rmap_item
*rmap_item
,
973 struct page
**tree_pagep
)
976 struct rb_node
**new = &root_unstable_tree
.rb_node
;
977 struct rb_node
*parent
= NULL
;
980 struct rmap_item
*tree_rmap_item
;
981 struct page
*tree_page
;
985 tree_rmap_item
= rb_entry(*new, struct rmap_item
, node
);
986 tree_page
= get_mergeable_page(tree_rmap_item
);
991 * Don't substitute a ksm page for a forked page.
993 if (page
== tree_page
) {
998 ret
= memcmp_pages(page
, tree_page
);
1002 put_page(tree_page
);
1003 new = &parent
->rb_left
;
1004 } else if (ret
> 0) {
1005 put_page(tree_page
);
1006 new = &parent
->rb_right
;
1008 *tree_pagep
= tree_page
;
1009 return tree_rmap_item
;
1013 rmap_item
->address
|= UNSTABLE_FLAG
;
1014 rmap_item
->address
|= (ksm_scan
.seqnr
& SEQNR_MASK
);
1015 rb_link_node(&rmap_item
->node
, parent
, new);
1016 rb_insert_color(&rmap_item
->node
, &root_unstable_tree
);
1018 ksm_pages_unshared
++;
1023 * stable_tree_append - add another rmap_item to the linked list of
1024 * rmap_items hanging off a given node of the stable tree, all sharing
1025 * the same ksm page.
1027 static void stable_tree_append(struct rmap_item
*rmap_item
,
1028 struct stable_node
*stable_node
)
1030 rmap_item
->head
= stable_node
;
1031 rmap_item
->address
|= STABLE_FLAG
;
1032 hlist_add_head(&rmap_item
->hlist
, &stable_node
->hlist
);
1034 if (rmap_item
->hlist
.next
)
1035 ksm_pages_sharing
++;
1041 * cmp_and_merge_page - first see if page can be merged into the stable tree;
1042 * if not, compare checksum to previous and if it's the same, see if page can
1043 * be inserted into the unstable tree, or merged with a page already there and
1044 * both transferred to the stable tree.
1046 * @page: the page that we are searching identical page to.
1047 * @rmap_item: the reverse mapping into the virtual address of this page
1049 static void cmp_and_merge_page(struct page
*page
, struct rmap_item
*rmap_item
)
1051 struct rmap_item
*tree_rmap_item
;
1052 struct page
*tree_page
= NULL
;
1053 struct stable_node
*stable_node
;
1055 unsigned int checksum
;
1058 remove_rmap_item_from_tree(rmap_item
);
1060 /* We first start with searching the page inside the stable tree */
1061 stable_node
= stable_tree_search(page
);
1063 kpage
= stable_node
->page
;
1064 err
= try_to_merge_with_ksm_page(rmap_item
, page
, kpage
);
1067 * The page was successfully merged:
1068 * add its rmap_item to the stable tree.
1070 stable_tree_append(rmap_item
, stable_node
);
1077 * A ksm page might have got here by fork, but its other
1078 * references have already been removed from the stable tree.
1079 * Or it might be left over from a break_ksm which failed
1080 * when the mem_cgroup had reached its limit: try again now.
1083 break_cow(rmap_item
);
1086 * In case the hash value of the page was changed from the last time we
1087 * have calculated it, this page to be changed frequely, therefore we
1088 * don't want to insert it to the unstable tree, and we don't want to
1089 * waste our time to search if there is something identical to it there.
1091 checksum
= calc_checksum(page
);
1092 if (rmap_item
->oldchecksum
!= checksum
) {
1093 rmap_item
->oldchecksum
= checksum
;
1098 unstable_tree_search_insert(rmap_item
, page
, &tree_page
);
1099 if (tree_rmap_item
) {
1100 kpage
= try_to_merge_two_pages(rmap_item
, page
,
1101 tree_rmap_item
, tree_page
);
1102 put_page(tree_page
);
1104 * As soon as we merge this page, we want to remove the
1105 * rmap_item of the page we have merged with from the unstable
1106 * tree, and insert it instead as new node in the stable tree.
1109 remove_rmap_item_from_tree(tree_rmap_item
);
1111 stable_node
= stable_tree_insert(kpage
);
1113 stable_tree_append(tree_rmap_item
, stable_node
);
1114 stable_tree_append(rmap_item
, stable_node
);
1119 * If we fail to insert the page into the stable tree,
1120 * we will have 2 virtual addresses that are pointing
1121 * to a ksm page left outside the stable tree,
1122 * in which case we need to break_cow on both.
1125 break_cow(tree_rmap_item
);
1126 break_cow(rmap_item
);
1132 static struct rmap_item
*get_next_rmap_item(struct mm_slot
*mm_slot
,
1133 struct rmap_item
**rmap_list
,
1136 struct rmap_item
*rmap_item
;
1138 while (*rmap_list
) {
1139 rmap_item
= *rmap_list
;
1140 if ((rmap_item
->address
& PAGE_MASK
) == addr
)
1142 if (rmap_item
->address
> addr
)
1144 *rmap_list
= rmap_item
->rmap_list
;
1145 remove_rmap_item_from_tree(rmap_item
);
1146 free_rmap_item(rmap_item
);
1149 rmap_item
= alloc_rmap_item();
1151 /* It has already been zeroed */
1152 rmap_item
->mm
= mm_slot
->mm
;
1153 rmap_item
->address
= addr
;
1154 rmap_item
->rmap_list
= *rmap_list
;
1155 *rmap_list
= rmap_item
;
1160 static struct rmap_item
*scan_get_next_rmap_item(struct page
**page
)
1162 struct mm_struct
*mm
;
1163 struct mm_slot
*slot
;
1164 struct vm_area_struct
*vma
;
1165 struct rmap_item
*rmap_item
;
1167 if (list_empty(&ksm_mm_head
.mm_list
))
1170 slot
= ksm_scan
.mm_slot
;
1171 if (slot
== &ksm_mm_head
) {
1172 root_unstable_tree
= RB_ROOT
;
1174 spin_lock(&ksm_mmlist_lock
);
1175 slot
= list_entry(slot
->mm_list
.next
, struct mm_slot
, mm_list
);
1176 ksm_scan
.mm_slot
= slot
;
1177 spin_unlock(&ksm_mmlist_lock
);
1179 ksm_scan
.address
= 0;
1180 ksm_scan
.rmap_list
= &slot
->rmap_list
;
1184 down_read(&mm
->mmap_sem
);
1185 if (ksm_test_exit(mm
))
1188 vma
= find_vma(mm
, ksm_scan
.address
);
1190 for (; vma
; vma
= vma
->vm_next
) {
1191 if (!(vma
->vm_flags
& VM_MERGEABLE
))
1193 if (ksm_scan
.address
< vma
->vm_start
)
1194 ksm_scan
.address
= vma
->vm_start
;
1196 ksm_scan
.address
= vma
->vm_end
;
1198 while (ksm_scan
.address
< vma
->vm_end
) {
1199 if (ksm_test_exit(mm
))
1201 *page
= follow_page(vma
, ksm_scan
.address
, FOLL_GET
);
1202 if (*page
&& PageAnon(*page
)) {
1203 flush_anon_page(vma
, *page
, ksm_scan
.address
);
1204 flush_dcache_page(*page
);
1205 rmap_item
= get_next_rmap_item(slot
,
1206 ksm_scan
.rmap_list
, ksm_scan
.address
);
1208 ksm_scan
.rmap_list
=
1209 &rmap_item
->rmap_list
;
1210 ksm_scan
.address
+= PAGE_SIZE
;
1213 up_read(&mm
->mmap_sem
);
1218 ksm_scan
.address
+= PAGE_SIZE
;
1223 if (ksm_test_exit(mm
)) {
1224 ksm_scan
.address
= 0;
1225 ksm_scan
.rmap_list
= &slot
->rmap_list
;
1228 * Nuke all the rmap_items that are above this current rmap:
1229 * because there were no VM_MERGEABLE vmas with such addresses.
1231 remove_trailing_rmap_items(slot
, ksm_scan
.rmap_list
);
1233 spin_lock(&ksm_mmlist_lock
);
1234 ksm_scan
.mm_slot
= list_entry(slot
->mm_list
.next
,
1235 struct mm_slot
, mm_list
);
1236 if (ksm_scan
.address
== 0) {
1238 * We've completed a full scan of all vmas, holding mmap_sem
1239 * throughout, and found no VM_MERGEABLE: so do the same as
1240 * __ksm_exit does to remove this mm from all our lists now.
1241 * This applies either when cleaning up after __ksm_exit
1242 * (but beware: we can reach here even before __ksm_exit),
1243 * or when all VM_MERGEABLE areas have been unmapped (and
1244 * mmap_sem then protects against race with MADV_MERGEABLE).
1246 hlist_del(&slot
->link
);
1247 list_del(&slot
->mm_list
);
1248 spin_unlock(&ksm_mmlist_lock
);
1251 clear_bit(MMF_VM_MERGEABLE
, &mm
->flags
);
1252 up_read(&mm
->mmap_sem
);
1255 spin_unlock(&ksm_mmlist_lock
);
1256 up_read(&mm
->mmap_sem
);
1259 /* Repeat until we've completed scanning the whole list */
1260 slot
= ksm_scan
.mm_slot
;
1261 if (slot
!= &ksm_mm_head
)
1269 * ksm_do_scan - the ksm scanner main worker function.
1270 * @scan_npages - number of pages we want to scan before we return.
1272 static void ksm_do_scan(unsigned int scan_npages
)
1274 struct rmap_item
*rmap_item
;
1277 while (scan_npages
--) {
1279 rmap_item
= scan_get_next_rmap_item(&page
);
1282 if (!PageKsm(page
) || !in_stable_tree(rmap_item
))
1283 cmp_and_merge_page(page
, rmap_item
);
1284 else if (page_mapcount(page
) == 1) {
1286 * Replace now-unshared ksm page by ordinary page.
1288 break_cow(rmap_item
);
1289 remove_rmap_item_from_tree(rmap_item
);
1290 rmap_item
->oldchecksum
= calc_checksum(page
);
1296 static int ksmd_should_run(void)
1298 return (ksm_run
& KSM_RUN_MERGE
) && !list_empty(&ksm_mm_head
.mm_list
);
1301 static int ksm_scan_thread(void *nothing
)
1303 set_user_nice(current
, 5);
1305 while (!kthread_should_stop()) {
1306 mutex_lock(&ksm_thread_mutex
);
1307 if (ksmd_should_run())
1308 ksm_do_scan(ksm_thread_pages_to_scan
);
1309 mutex_unlock(&ksm_thread_mutex
);
1311 if (ksmd_should_run()) {
1312 schedule_timeout_interruptible(
1313 msecs_to_jiffies(ksm_thread_sleep_millisecs
));
1315 wait_event_interruptible(ksm_thread_wait
,
1316 ksmd_should_run() || kthread_should_stop());
1322 int ksm_madvise(struct vm_area_struct
*vma
, unsigned long start
,
1323 unsigned long end
, int advice
, unsigned long *vm_flags
)
1325 struct mm_struct
*mm
= vma
->vm_mm
;
1329 case MADV_MERGEABLE
:
1331 * Be somewhat over-protective for now!
1333 if (*vm_flags
& (VM_MERGEABLE
| VM_SHARED
| VM_MAYSHARE
|
1334 VM_PFNMAP
| VM_IO
| VM_DONTEXPAND
|
1335 VM_RESERVED
| VM_HUGETLB
| VM_INSERTPAGE
|
1336 VM_MIXEDMAP
| VM_SAO
))
1337 return 0; /* just ignore the advice */
1339 if (!test_bit(MMF_VM_MERGEABLE
, &mm
->flags
)) {
1340 err
= __ksm_enter(mm
);
1345 *vm_flags
|= VM_MERGEABLE
;
1348 case MADV_UNMERGEABLE
:
1349 if (!(*vm_flags
& VM_MERGEABLE
))
1350 return 0; /* just ignore the advice */
1352 if (vma
->anon_vma
) {
1353 err
= unmerge_ksm_pages(vma
, start
, end
);
1358 *vm_flags
&= ~VM_MERGEABLE
;
1365 int __ksm_enter(struct mm_struct
*mm
)
1367 struct mm_slot
*mm_slot
;
1370 mm_slot
= alloc_mm_slot();
1374 /* Check ksm_run too? Would need tighter locking */
1375 needs_wakeup
= list_empty(&ksm_mm_head
.mm_list
);
1377 spin_lock(&ksm_mmlist_lock
);
1378 insert_to_mm_slots_hash(mm
, mm_slot
);
1380 * Insert just behind the scanning cursor, to let the area settle
1381 * down a little; when fork is followed by immediate exec, we don't
1382 * want ksmd to waste time setting up and tearing down an rmap_list.
1384 list_add_tail(&mm_slot
->mm_list
, &ksm_scan
.mm_slot
->mm_list
);
1385 spin_unlock(&ksm_mmlist_lock
);
1387 set_bit(MMF_VM_MERGEABLE
, &mm
->flags
);
1388 atomic_inc(&mm
->mm_count
);
1391 wake_up_interruptible(&ksm_thread_wait
);
1396 void __ksm_exit(struct mm_struct
*mm
)
1398 struct mm_slot
*mm_slot
;
1399 int easy_to_free
= 0;
1402 * This process is exiting: if it's straightforward (as is the
1403 * case when ksmd was never running), free mm_slot immediately.
1404 * But if it's at the cursor or has rmap_items linked to it, use
1405 * mmap_sem to synchronize with any break_cows before pagetables
1406 * are freed, and leave the mm_slot on the list for ksmd to free.
1407 * Beware: ksm may already have noticed it exiting and freed the slot.
1410 spin_lock(&ksm_mmlist_lock
);
1411 mm_slot
= get_mm_slot(mm
);
1412 if (mm_slot
&& ksm_scan
.mm_slot
!= mm_slot
) {
1413 if (!mm_slot
->rmap_list
) {
1414 hlist_del(&mm_slot
->link
);
1415 list_del(&mm_slot
->mm_list
);
1418 list_move(&mm_slot
->mm_list
,
1419 &ksm_scan
.mm_slot
->mm_list
);
1422 spin_unlock(&ksm_mmlist_lock
);
1425 free_mm_slot(mm_slot
);
1426 clear_bit(MMF_VM_MERGEABLE
, &mm
->flags
);
1428 } else if (mm_slot
) {
1429 down_write(&mm
->mmap_sem
);
1430 up_write(&mm
->mmap_sem
);
1436 * This all compiles without CONFIG_SYSFS, but is a waste of space.
1439 #define KSM_ATTR_RO(_name) \
1440 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1441 #define KSM_ATTR(_name) \
1442 static struct kobj_attribute _name##_attr = \
1443 __ATTR(_name, 0644, _name##_show, _name##_store)
1445 static ssize_t
sleep_millisecs_show(struct kobject
*kobj
,
1446 struct kobj_attribute
*attr
, char *buf
)
1448 return sprintf(buf
, "%u\n", ksm_thread_sleep_millisecs
);
1451 static ssize_t
sleep_millisecs_store(struct kobject
*kobj
,
1452 struct kobj_attribute
*attr
,
1453 const char *buf
, size_t count
)
1455 unsigned long msecs
;
1458 err
= strict_strtoul(buf
, 10, &msecs
);
1459 if (err
|| msecs
> UINT_MAX
)
1462 ksm_thread_sleep_millisecs
= msecs
;
1466 KSM_ATTR(sleep_millisecs
);
1468 static ssize_t
pages_to_scan_show(struct kobject
*kobj
,
1469 struct kobj_attribute
*attr
, char *buf
)
1471 return sprintf(buf
, "%u\n", ksm_thread_pages_to_scan
);
1474 static ssize_t
pages_to_scan_store(struct kobject
*kobj
,
1475 struct kobj_attribute
*attr
,
1476 const char *buf
, size_t count
)
1479 unsigned long nr_pages
;
1481 err
= strict_strtoul(buf
, 10, &nr_pages
);
1482 if (err
|| nr_pages
> UINT_MAX
)
1485 ksm_thread_pages_to_scan
= nr_pages
;
1489 KSM_ATTR(pages_to_scan
);
1491 static ssize_t
run_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1494 return sprintf(buf
, "%u\n", ksm_run
);
1497 static ssize_t
run_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
1498 const char *buf
, size_t count
)
1501 unsigned long flags
;
1503 err
= strict_strtoul(buf
, 10, &flags
);
1504 if (err
|| flags
> UINT_MAX
)
1506 if (flags
> KSM_RUN_UNMERGE
)
1510 * KSM_RUN_MERGE sets ksmd running, and 0 stops it running.
1511 * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items,
1512 * breaking COW to free the unswappable pages_shared (but leaves
1513 * mm_slots on the list for when ksmd may be set running again).
1516 mutex_lock(&ksm_thread_mutex
);
1517 if (ksm_run
!= flags
) {
1519 if (flags
& KSM_RUN_UNMERGE
) {
1520 current
->flags
|= PF_OOM_ORIGIN
;
1521 err
= unmerge_and_remove_all_rmap_items();
1522 current
->flags
&= ~PF_OOM_ORIGIN
;
1524 ksm_run
= KSM_RUN_STOP
;
1529 mutex_unlock(&ksm_thread_mutex
);
1531 if (flags
& KSM_RUN_MERGE
)
1532 wake_up_interruptible(&ksm_thread_wait
);
1538 static ssize_t
max_kernel_pages_store(struct kobject
*kobj
,
1539 struct kobj_attribute
*attr
,
1540 const char *buf
, size_t count
)
1543 unsigned long nr_pages
;
1545 err
= strict_strtoul(buf
, 10, &nr_pages
);
1549 ksm_max_kernel_pages
= nr_pages
;
1554 static ssize_t
max_kernel_pages_show(struct kobject
*kobj
,
1555 struct kobj_attribute
*attr
, char *buf
)
1557 return sprintf(buf
, "%lu\n", ksm_max_kernel_pages
);
1559 KSM_ATTR(max_kernel_pages
);
1561 static ssize_t
pages_shared_show(struct kobject
*kobj
,
1562 struct kobj_attribute
*attr
, char *buf
)
1564 return sprintf(buf
, "%lu\n", ksm_pages_shared
);
1566 KSM_ATTR_RO(pages_shared
);
1568 static ssize_t
pages_sharing_show(struct kobject
*kobj
,
1569 struct kobj_attribute
*attr
, char *buf
)
1571 return sprintf(buf
, "%lu\n", ksm_pages_sharing
);
1573 KSM_ATTR_RO(pages_sharing
);
1575 static ssize_t
pages_unshared_show(struct kobject
*kobj
,
1576 struct kobj_attribute
*attr
, char *buf
)
1578 return sprintf(buf
, "%lu\n", ksm_pages_unshared
);
1580 KSM_ATTR_RO(pages_unshared
);
1582 static ssize_t
pages_volatile_show(struct kobject
*kobj
,
1583 struct kobj_attribute
*attr
, char *buf
)
1585 long ksm_pages_volatile
;
1587 ksm_pages_volatile
= ksm_rmap_items
- ksm_pages_shared
1588 - ksm_pages_sharing
- ksm_pages_unshared
;
1590 * It was not worth any locking to calculate that statistic,
1591 * but it might therefore sometimes be negative: conceal that.
1593 if (ksm_pages_volatile
< 0)
1594 ksm_pages_volatile
= 0;
1595 return sprintf(buf
, "%ld\n", ksm_pages_volatile
);
1597 KSM_ATTR_RO(pages_volatile
);
1599 static ssize_t
full_scans_show(struct kobject
*kobj
,
1600 struct kobj_attribute
*attr
, char *buf
)
1602 return sprintf(buf
, "%lu\n", ksm_scan
.seqnr
);
1604 KSM_ATTR_RO(full_scans
);
1606 static struct attribute
*ksm_attrs
[] = {
1607 &sleep_millisecs_attr
.attr
,
1608 &pages_to_scan_attr
.attr
,
1610 &max_kernel_pages_attr
.attr
,
1611 &pages_shared_attr
.attr
,
1612 &pages_sharing_attr
.attr
,
1613 &pages_unshared_attr
.attr
,
1614 &pages_volatile_attr
.attr
,
1615 &full_scans_attr
.attr
,
1619 static struct attribute_group ksm_attr_group
= {
1623 #endif /* CONFIG_SYSFS */
1625 static int __init
ksm_init(void)
1627 struct task_struct
*ksm_thread
;
1630 ksm_max_kernel_pages
= totalram_pages
/ 4;
1632 err
= ksm_slab_init();
1636 err
= mm_slots_hash_init();
1640 ksm_thread
= kthread_run(ksm_scan_thread
, NULL
, "ksmd");
1641 if (IS_ERR(ksm_thread
)) {
1642 printk(KERN_ERR
"ksm: creating kthread failed\n");
1643 err
= PTR_ERR(ksm_thread
);
1648 err
= sysfs_create_group(mm_kobj
, &ksm_attr_group
);
1650 printk(KERN_ERR
"ksm: register sysfs failed\n");
1651 kthread_stop(ksm_thread
);
1655 ksm_run
= KSM_RUN_MERGE
; /* no way for user to start it */
1657 #endif /* CONFIG_SYSFS */
1662 mm_slots_hash_free();
1668 module_init(ksm_init
)