1f8af4da3SHugh Dickins /* 231dbd01fSIzik Eidus * Memory merging support. 331dbd01fSIzik Eidus * 431dbd01fSIzik Eidus * This code enables dynamic sharing of identical pages found in different 531dbd01fSIzik Eidus * memory areas, even if they are not shared by fork() 631dbd01fSIzik Eidus * 736b2528dSIzik Eidus * Copyright (C) 2008-2009 Red Hat, Inc. 831dbd01fSIzik Eidus * Authors: 931dbd01fSIzik Eidus * Izik Eidus 1031dbd01fSIzik Eidus * Andrea Arcangeli 1131dbd01fSIzik Eidus * Chris Wright 1236b2528dSIzik Eidus * Hugh Dickins 1331dbd01fSIzik Eidus * 1431dbd01fSIzik Eidus * This work is licensed under the terms of the GNU GPL, version 2. 15f8af4da3SHugh Dickins */ 16f8af4da3SHugh Dickins 17f8af4da3SHugh Dickins #include <linux/errno.h> 1831dbd01fSIzik Eidus #include <linux/mm.h> 1931dbd01fSIzik Eidus #include <linux/fs.h> 20f8af4da3SHugh Dickins #include <linux/mman.h> 2131dbd01fSIzik Eidus #include <linux/sched.h> 2231dbd01fSIzik Eidus #include <linux/rwsem.h> 2331dbd01fSIzik Eidus #include <linux/pagemap.h> 2431dbd01fSIzik Eidus #include <linux/rmap.h> 2531dbd01fSIzik Eidus #include <linux/spinlock.h> 2631dbd01fSIzik Eidus #include <linux/jhash.h> 2731dbd01fSIzik Eidus #include <linux/delay.h> 2831dbd01fSIzik Eidus #include <linux/kthread.h> 2931dbd01fSIzik Eidus #include <linux/wait.h> 3031dbd01fSIzik Eidus #include <linux/slab.h> 3131dbd01fSIzik Eidus #include <linux/rbtree.h> 3262b61f61SHugh Dickins #include <linux/memory.h> 3331dbd01fSIzik Eidus #include <linux/mmu_notifier.h> 342c6854fdSIzik Eidus #include <linux/swap.h> 35f8af4da3SHugh Dickins #include <linux/ksm.h> 36d9f8984cSLai Jiangshan #include <linux/hash.h> 37878aee7dSAndrea Arcangeli #include <linux/freezer.h> 38*72788c38SDavid Rientjes #include <linux/oom.h> 39f8af4da3SHugh Dickins 4031dbd01fSIzik Eidus #include <asm/tlbflush.h> 4173848b46SHugh Dickins #include "internal.h" 4231dbd01fSIzik Eidus 4331dbd01fSIzik Eidus /* 4431dbd01fSIzik Eidus * A few notes about the KSM scanning process, 4531dbd01fSIzik Eidus * to make it easier to understand the data structures below: 4631dbd01fSIzik Eidus * 4731dbd01fSIzik Eidus * In order to reduce excessive scanning, KSM sorts the memory pages by their 4831dbd01fSIzik Eidus * contents into a data structure that holds pointers to the pages' locations. 4931dbd01fSIzik Eidus * 5031dbd01fSIzik Eidus * Since the contents of the pages may change at any moment, KSM cannot just 5131dbd01fSIzik Eidus * insert the pages into a normal sorted tree and expect it to find anything. 5231dbd01fSIzik Eidus * Therefore KSM uses two data structures - the stable and the unstable tree. 5331dbd01fSIzik Eidus * 5431dbd01fSIzik Eidus * The stable tree holds pointers to all the merged pages (ksm pages), sorted 5531dbd01fSIzik Eidus * by their contents. Because each such page is write-protected, searching on 5631dbd01fSIzik Eidus * this tree is fully assured to be working (except when pages are unmapped), 5731dbd01fSIzik Eidus * and therefore this tree is called the stable tree. 5831dbd01fSIzik Eidus * 5931dbd01fSIzik Eidus * In addition to the stable tree, KSM uses a second data structure called the 6031dbd01fSIzik Eidus * unstable tree: this tree holds pointers to pages which have been found to 6131dbd01fSIzik Eidus * be "unchanged for a period of time". The unstable tree sorts these pages 6231dbd01fSIzik Eidus * by their contents, but since they are not write-protected, KSM cannot rely 6331dbd01fSIzik Eidus * upon the unstable tree to work correctly - the unstable tree is liable to 6431dbd01fSIzik Eidus * be corrupted as its contents are modified, and so it is called unstable. 6531dbd01fSIzik Eidus * 6631dbd01fSIzik Eidus * KSM solves this problem by several techniques: 6731dbd01fSIzik Eidus * 6831dbd01fSIzik Eidus * 1) The unstable tree is flushed every time KSM completes scanning all 6931dbd01fSIzik Eidus * memory areas, and then the tree is rebuilt again from the beginning. 7031dbd01fSIzik Eidus * 2) KSM will only insert into the unstable tree, pages whose hash value 7131dbd01fSIzik Eidus * has not changed since the previous scan of all memory areas. 7231dbd01fSIzik Eidus * 3) The unstable tree is a RedBlack Tree - so its balancing is based on the 7331dbd01fSIzik Eidus * colors of the nodes and not on their contents, assuring that even when 7431dbd01fSIzik Eidus * the tree gets "corrupted" it won't get out of balance, so scanning time 7531dbd01fSIzik Eidus * remains the same (also, searching and inserting nodes in an rbtree uses 7631dbd01fSIzik Eidus * the same algorithm, so we have no overhead when we flush and rebuild). 7731dbd01fSIzik Eidus * 4) KSM never flushes the stable tree, which means that even if it were to 7831dbd01fSIzik Eidus * take 10 attempts to find a page in the unstable tree, once it is found, 7931dbd01fSIzik Eidus * it is secured in the stable tree. (When we scan a new page, we first 8031dbd01fSIzik Eidus * compare it against the stable tree, and then against the unstable tree.) 8131dbd01fSIzik Eidus */ 8231dbd01fSIzik Eidus 8331dbd01fSIzik Eidus /** 8431dbd01fSIzik Eidus * struct mm_slot - ksm information per mm that is being scanned 8531dbd01fSIzik Eidus * @link: link to the mm_slots hash list 8631dbd01fSIzik Eidus * @mm_list: link into the mm_slots list, rooted in ksm_mm_head 876514d511SHugh Dickins * @rmap_list: head for this mm_slot's singly-linked list of rmap_items 8831dbd01fSIzik Eidus * @mm: the mm that this information is valid for 8931dbd01fSIzik Eidus */ 9031dbd01fSIzik Eidus struct mm_slot { 9131dbd01fSIzik Eidus struct hlist_node link; 9231dbd01fSIzik Eidus struct list_head mm_list; 936514d511SHugh Dickins struct rmap_item *rmap_list; 9431dbd01fSIzik Eidus struct mm_struct *mm; 9531dbd01fSIzik Eidus }; 9631dbd01fSIzik Eidus 9731dbd01fSIzik Eidus /** 9831dbd01fSIzik Eidus * struct ksm_scan - cursor for scanning 9931dbd01fSIzik Eidus * @mm_slot: the current mm_slot we are scanning 10031dbd01fSIzik Eidus * @address: the next address inside that to be scanned 1016514d511SHugh Dickins * @rmap_list: link to the next rmap to be scanned in the rmap_list 10231dbd01fSIzik Eidus * @seqnr: count of completed full scans (needed when removing unstable node) 10331dbd01fSIzik Eidus * 10431dbd01fSIzik Eidus * There is only the one ksm_scan instance of this cursor structure. 10531dbd01fSIzik Eidus */ 10631dbd01fSIzik Eidus struct ksm_scan { 10731dbd01fSIzik Eidus struct mm_slot *mm_slot; 10831dbd01fSIzik Eidus unsigned long address; 1096514d511SHugh Dickins struct rmap_item **rmap_list; 11031dbd01fSIzik Eidus unsigned long seqnr; 11131dbd01fSIzik Eidus }; 11231dbd01fSIzik Eidus 11331dbd01fSIzik Eidus /** 1147b6ba2c7SHugh Dickins * struct stable_node - node of the stable rbtree 1157b6ba2c7SHugh Dickins * @node: rb node of this ksm page in the stable tree 1167b6ba2c7SHugh Dickins * @hlist: hlist head of rmap_items using this ksm page 11762b61f61SHugh Dickins * @kpfn: page frame number of this ksm page 1187b6ba2c7SHugh Dickins */ 1197b6ba2c7SHugh Dickins struct stable_node { 1207b6ba2c7SHugh Dickins struct rb_node node; 1217b6ba2c7SHugh Dickins struct hlist_head hlist; 12262b61f61SHugh Dickins unsigned long kpfn; 1237b6ba2c7SHugh Dickins }; 1247b6ba2c7SHugh Dickins 1257b6ba2c7SHugh Dickins /** 12631dbd01fSIzik Eidus * struct rmap_item - reverse mapping item for virtual addresses 1276514d511SHugh Dickins * @rmap_list: next rmap_item in mm_slot's singly-linked rmap_list 128db114b83SHugh Dickins * @anon_vma: pointer to anon_vma for this mm,address, when in stable tree 12931dbd01fSIzik Eidus * @mm: the memory structure this rmap_item is pointing into 13031dbd01fSIzik Eidus * @address: the virtual address this rmap_item tracks (+ flags in low bits) 13131dbd01fSIzik Eidus * @oldchecksum: previous checksum of the page at that virtual address 1327b6ba2c7SHugh Dickins * @node: rb node of this rmap_item in the unstable tree 1337b6ba2c7SHugh Dickins * @head: pointer to stable_node heading this list in the stable tree 1347b6ba2c7SHugh Dickins * @hlist: link into hlist of rmap_items hanging off that stable_node 13531dbd01fSIzik Eidus */ 13631dbd01fSIzik Eidus struct rmap_item { 1376514d511SHugh Dickins struct rmap_item *rmap_list; 138db114b83SHugh Dickins struct anon_vma *anon_vma; /* when stable */ 13931dbd01fSIzik Eidus struct mm_struct *mm; 14031dbd01fSIzik Eidus unsigned long address; /* + low bits used for flags below */ 14131dbd01fSIzik Eidus unsigned int oldchecksum; /* when unstable */ 14231dbd01fSIzik Eidus union { 1437b6ba2c7SHugh Dickins struct rb_node node; /* when node of unstable tree */ 1447b6ba2c7SHugh Dickins struct { /* when listed from stable tree */ 1457b6ba2c7SHugh Dickins struct stable_node *head; 1467b6ba2c7SHugh Dickins struct hlist_node hlist; 1477b6ba2c7SHugh Dickins }; 14831dbd01fSIzik Eidus }; 14931dbd01fSIzik Eidus }; 15031dbd01fSIzik Eidus 15131dbd01fSIzik Eidus #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */ 1527b6ba2c7SHugh Dickins #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */ 1537b6ba2c7SHugh Dickins #define STABLE_FLAG 0x200 /* is listed from the stable tree */ 15431dbd01fSIzik Eidus 15531dbd01fSIzik Eidus /* The stable and unstable tree heads */ 15631dbd01fSIzik Eidus static struct rb_root root_stable_tree = RB_ROOT; 15731dbd01fSIzik Eidus static struct rb_root root_unstable_tree = RB_ROOT; 15831dbd01fSIzik Eidus 159d9f8984cSLai Jiangshan #define MM_SLOTS_HASH_SHIFT 10 160d9f8984cSLai Jiangshan #define MM_SLOTS_HASH_HEADS (1 << MM_SLOTS_HASH_SHIFT) 161d9f8984cSLai Jiangshan static struct hlist_head mm_slots_hash[MM_SLOTS_HASH_HEADS]; 16231dbd01fSIzik Eidus 16331dbd01fSIzik Eidus static struct mm_slot ksm_mm_head = { 16431dbd01fSIzik Eidus .mm_list = LIST_HEAD_INIT(ksm_mm_head.mm_list), 16531dbd01fSIzik Eidus }; 16631dbd01fSIzik Eidus static struct ksm_scan ksm_scan = { 16731dbd01fSIzik Eidus .mm_slot = &ksm_mm_head, 16831dbd01fSIzik Eidus }; 16931dbd01fSIzik Eidus 17031dbd01fSIzik Eidus static struct kmem_cache *rmap_item_cache; 1717b6ba2c7SHugh Dickins static struct kmem_cache *stable_node_cache; 17231dbd01fSIzik Eidus static struct kmem_cache *mm_slot_cache; 17331dbd01fSIzik Eidus 17431dbd01fSIzik Eidus /* The number of nodes in the stable tree */ 175b4028260SHugh Dickins static unsigned long ksm_pages_shared; 17631dbd01fSIzik Eidus 177e178dfdeSHugh Dickins /* The number of page slots additionally sharing those nodes */ 178b4028260SHugh Dickins static unsigned long ksm_pages_sharing; 17931dbd01fSIzik Eidus 180473b0ce4SHugh Dickins /* The number of nodes in the unstable tree */ 181473b0ce4SHugh Dickins static unsigned long ksm_pages_unshared; 182473b0ce4SHugh Dickins 183473b0ce4SHugh Dickins /* The number of rmap_items in use: to calculate pages_volatile */ 184473b0ce4SHugh Dickins static unsigned long ksm_rmap_items; 185473b0ce4SHugh Dickins 18631dbd01fSIzik Eidus /* Number of pages ksmd should scan in one batch */ 1872c6854fdSIzik Eidus static unsigned int ksm_thread_pages_to_scan = 100; 18831dbd01fSIzik Eidus 18931dbd01fSIzik Eidus /* Milliseconds ksmd should sleep between batches */ 1902ffd8679SHugh Dickins static unsigned int ksm_thread_sleep_millisecs = 20; 19131dbd01fSIzik Eidus 19231dbd01fSIzik Eidus #define KSM_RUN_STOP 0 19331dbd01fSIzik Eidus #define KSM_RUN_MERGE 1 19431dbd01fSIzik Eidus #define KSM_RUN_UNMERGE 2 1952c6854fdSIzik Eidus static unsigned int ksm_run = KSM_RUN_STOP; 19631dbd01fSIzik Eidus 19731dbd01fSIzik Eidus static DECLARE_WAIT_QUEUE_HEAD(ksm_thread_wait); 19831dbd01fSIzik Eidus static DEFINE_MUTEX(ksm_thread_mutex); 19931dbd01fSIzik Eidus static DEFINE_SPINLOCK(ksm_mmlist_lock); 20031dbd01fSIzik Eidus 20131dbd01fSIzik Eidus #define KSM_KMEM_CACHE(__struct, __flags) kmem_cache_create("ksm_"#__struct,\ 20231dbd01fSIzik Eidus sizeof(struct __struct), __alignof__(struct __struct),\ 20331dbd01fSIzik Eidus (__flags), NULL) 20431dbd01fSIzik Eidus 20531dbd01fSIzik Eidus static int __init ksm_slab_init(void) 20631dbd01fSIzik Eidus { 20731dbd01fSIzik Eidus rmap_item_cache = KSM_KMEM_CACHE(rmap_item, 0); 20831dbd01fSIzik Eidus if (!rmap_item_cache) 20931dbd01fSIzik Eidus goto out; 21031dbd01fSIzik Eidus 2117b6ba2c7SHugh Dickins stable_node_cache = KSM_KMEM_CACHE(stable_node, 0); 2127b6ba2c7SHugh Dickins if (!stable_node_cache) 2137b6ba2c7SHugh Dickins goto out_free1; 2147b6ba2c7SHugh Dickins 21531dbd01fSIzik Eidus mm_slot_cache = KSM_KMEM_CACHE(mm_slot, 0); 21631dbd01fSIzik Eidus if (!mm_slot_cache) 2177b6ba2c7SHugh Dickins goto out_free2; 21831dbd01fSIzik Eidus 21931dbd01fSIzik Eidus return 0; 22031dbd01fSIzik Eidus 2217b6ba2c7SHugh Dickins out_free2: 2227b6ba2c7SHugh Dickins kmem_cache_destroy(stable_node_cache); 2237b6ba2c7SHugh Dickins out_free1: 22431dbd01fSIzik Eidus kmem_cache_destroy(rmap_item_cache); 22531dbd01fSIzik Eidus out: 22631dbd01fSIzik Eidus return -ENOMEM; 22731dbd01fSIzik Eidus } 22831dbd01fSIzik Eidus 22931dbd01fSIzik Eidus static void __init ksm_slab_free(void) 23031dbd01fSIzik Eidus { 23131dbd01fSIzik Eidus kmem_cache_destroy(mm_slot_cache); 2327b6ba2c7SHugh Dickins kmem_cache_destroy(stable_node_cache); 23331dbd01fSIzik Eidus kmem_cache_destroy(rmap_item_cache); 23431dbd01fSIzik Eidus mm_slot_cache = NULL; 23531dbd01fSIzik Eidus } 23631dbd01fSIzik Eidus 23731dbd01fSIzik Eidus static inline struct rmap_item *alloc_rmap_item(void) 23831dbd01fSIzik Eidus { 239473b0ce4SHugh Dickins struct rmap_item *rmap_item; 240473b0ce4SHugh Dickins 241473b0ce4SHugh Dickins rmap_item = kmem_cache_zalloc(rmap_item_cache, GFP_KERNEL); 242473b0ce4SHugh Dickins if (rmap_item) 243473b0ce4SHugh Dickins ksm_rmap_items++; 244473b0ce4SHugh Dickins return rmap_item; 24531dbd01fSIzik Eidus } 24631dbd01fSIzik Eidus 24731dbd01fSIzik Eidus static inline void free_rmap_item(struct rmap_item *rmap_item) 24831dbd01fSIzik Eidus { 249473b0ce4SHugh Dickins ksm_rmap_items--; 25031dbd01fSIzik Eidus rmap_item->mm = NULL; /* debug safety */ 25131dbd01fSIzik Eidus kmem_cache_free(rmap_item_cache, rmap_item); 25231dbd01fSIzik Eidus } 25331dbd01fSIzik Eidus 2547b6ba2c7SHugh Dickins static inline struct stable_node *alloc_stable_node(void) 2557b6ba2c7SHugh Dickins { 2567b6ba2c7SHugh Dickins return kmem_cache_alloc(stable_node_cache, GFP_KERNEL); 2577b6ba2c7SHugh Dickins } 2587b6ba2c7SHugh Dickins 2597b6ba2c7SHugh Dickins static inline void free_stable_node(struct stable_node *stable_node) 2607b6ba2c7SHugh Dickins { 2617b6ba2c7SHugh Dickins kmem_cache_free(stable_node_cache, stable_node); 2627b6ba2c7SHugh Dickins } 2637b6ba2c7SHugh Dickins 26431dbd01fSIzik Eidus static inline struct mm_slot *alloc_mm_slot(void) 26531dbd01fSIzik Eidus { 26631dbd01fSIzik Eidus if (!mm_slot_cache) /* initialization failed */ 26731dbd01fSIzik Eidus return NULL; 26831dbd01fSIzik Eidus return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); 26931dbd01fSIzik Eidus } 27031dbd01fSIzik Eidus 27131dbd01fSIzik Eidus static inline void free_mm_slot(struct mm_slot *mm_slot) 27231dbd01fSIzik Eidus { 27331dbd01fSIzik Eidus kmem_cache_free(mm_slot_cache, mm_slot); 27431dbd01fSIzik Eidus } 27531dbd01fSIzik Eidus 27631dbd01fSIzik Eidus static struct mm_slot *get_mm_slot(struct mm_struct *mm) 27731dbd01fSIzik Eidus { 27831dbd01fSIzik Eidus struct mm_slot *mm_slot; 27931dbd01fSIzik Eidus struct hlist_head *bucket; 28031dbd01fSIzik Eidus struct hlist_node *node; 28131dbd01fSIzik Eidus 282d9f8984cSLai Jiangshan bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)]; 28331dbd01fSIzik Eidus hlist_for_each_entry(mm_slot, node, bucket, link) { 28431dbd01fSIzik Eidus if (mm == mm_slot->mm) 28531dbd01fSIzik Eidus return mm_slot; 28631dbd01fSIzik Eidus } 28731dbd01fSIzik Eidus return NULL; 28831dbd01fSIzik Eidus } 28931dbd01fSIzik Eidus 29031dbd01fSIzik Eidus static void insert_to_mm_slots_hash(struct mm_struct *mm, 29131dbd01fSIzik Eidus struct mm_slot *mm_slot) 29231dbd01fSIzik Eidus { 29331dbd01fSIzik Eidus struct hlist_head *bucket; 29431dbd01fSIzik Eidus 295d9f8984cSLai Jiangshan bucket = &mm_slots_hash[hash_ptr(mm, MM_SLOTS_HASH_SHIFT)]; 29631dbd01fSIzik Eidus mm_slot->mm = mm; 29731dbd01fSIzik Eidus hlist_add_head(&mm_slot->link, bucket); 29831dbd01fSIzik Eidus } 29931dbd01fSIzik Eidus 30031dbd01fSIzik Eidus static inline int in_stable_tree(struct rmap_item *rmap_item) 30131dbd01fSIzik Eidus { 30231dbd01fSIzik Eidus return rmap_item->address & STABLE_FLAG; 30331dbd01fSIzik Eidus } 30431dbd01fSIzik Eidus 30531dbd01fSIzik Eidus /* 306a913e182SHugh Dickins * ksmd, and unmerge_and_remove_all_rmap_items(), must not touch an mm's 307a913e182SHugh Dickins * page tables after it has passed through ksm_exit() - which, if necessary, 308a913e182SHugh Dickins * takes mmap_sem briefly to serialize against them. ksm_exit() does not set 309a913e182SHugh Dickins * a special flag: they can just back out as soon as mm_users goes to zero. 310a913e182SHugh Dickins * ksm_test_exit() is used throughout to make this test for exit: in some 311a913e182SHugh Dickins * places for correctness, in some places just to avoid unnecessary work. 312a913e182SHugh Dickins */ 313a913e182SHugh Dickins static inline bool ksm_test_exit(struct mm_struct *mm) 314a913e182SHugh Dickins { 315a913e182SHugh Dickins return atomic_read(&mm->mm_users) == 0; 316a913e182SHugh Dickins } 317a913e182SHugh Dickins 318a913e182SHugh Dickins /* 31931dbd01fSIzik Eidus * We use break_ksm to break COW on a ksm page: it's a stripped down 32031dbd01fSIzik Eidus * 32131dbd01fSIzik Eidus * if (get_user_pages(current, mm, addr, 1, 1, 1, &page, NULL) == 1) 32231dbd01fSIzik Eidus * put_page(page); 32331dbd01fSIzik Eidus * 32431dbd01fSIzik Eidus * but taking great care only to touch a ksm page, in a VM_MERGEABLE vma, 32531dbd01fSIzik Eidus * in case the application has unmapped and remapped mm,addr meanwhile. 32631dbd01fSIzik Eidus * Could a ksm page appear anywhere else? Actually yes, in a VM_PFNMAP 32731dbd01fSIzik Eidus * mmap of /dev/mem or /dev/kmem, where we would not want to touch it. 32831dbd01fSIzik Eidus */ 329d952b791SHugh Dickins static int break_ksm(struct vm_area_struct *vma, unsigned long addr) 33031dbd01fSIzik Eidus { 33131dbd01fSIzik Eidus struct page *page; 332d952b791SHugh Dickins int ret = 0; 33331dbd01fSIzik Eidus 33431dbd01fSIzik Eidus do { 33531dbd01fSIzik Eidus cond_resched(); 33631dbd01fSIzik Eidus page = follow_page(vma, addr, FOLL_GET); 33722eccdd7SDan Carpenter if (IS_ERR_OR_NULL(page)) 33831dbd01fSIzik Eidus break; 33931dbd01fSIzik Eidus if (PageKsm(page)) 34031dbd01fSIzik Eidus ret = handle_mm_fault(vma->vm_mm, vma, addr, 34131dbd01fSIzik Eidus FAULT_FLAG_WRITE); 34231dbd01fSIzik Eidus else 34331dbd01fSIzik Eidus ret = VM_FAULT_WRITE; 34431dbd01fSIzik Eidus put_page(page); 345d952b791SHugh Dickins } while (!(ret & (VM_FAULT_WRITE | VM_FAULT_SIGBUS | VM_FAULT_OOM))); 346d952b791SHugh Dickins /* 347d952b791SHugh Dickins * We must loop because handle_mm_fault() may back out if there's 348d952b791SHugh Dickins * any difficulty e.g. if pte accessed bit gets updated concurrently. 349d952b791SHugh Dickins * 350d952b791SHugh Dickins * VM_FAULT_WRITE is what we have been hoping for: it indicates that 351d952b791SHugh Dickins * COW has been broken, even if the vma does not permit VM_WRITE; 352d952b791SHugh Dickins * but note that a concurrent fault might break PageKsm for us. 353d952b791SHugh Dickins * 354d952b791SHugh Dickins * VM_FAULT_SIGBUS could occur if we race with truncation of the 355d952b791SHugh Dickins * backing file, which also invalidates anonymous pages: that's 356d952b791SHugh Dickins * okay, that truncation will have unmapped the PageKsm for us. 357d952b791SHugh Dickins * 358d952b791SHugh Dickins * VM_FAULT_OOM: at the time of writing (late July 2009), setting 359d952b791SHugh Dickins * aside mem_cgroup limits, VM_FAULT_OOM would only be set if the 360d952b791SHugh Dickins * current task has TIF_MEMDIE set, and will be OOM killed on return 361d952b791SHugh Dickins * to user; and ksmd, having no mm, would never be chosen for that. 362d952b791SHugh Dickins * 363d952b791SHugh Dickins * But if the mm is in a limited mem_cgroup, then the fault may fail 364d952b791SHugh Dickins * with VM_FAULT_OOM even if the current task is not TIF_MEMDIE; and 365d952b791SHugh Dickins * even ksmd can fail in this way - though it's usually breaking ksm 366d952b791SHugh Dickins * just to undo a merge it made a moment before, so unlikely to oom. 367d952b791SHugh Dickins * 368d952b791SHugh Dickins * That's a pity: we might therefore have more kernel pages allocated 369d952b791SHugh Dickins * than we're counting as nodes in the stable tree; but ksm_do_scan 370d952b791SHugh Dickins * will retry to break_cow on each pass, so should recover the page 371d952b791SHugh Dickins * in due course. The important thing is to not let VM_MERGEABLE 372d952b791SHugh Dickins * be cleared while any such pages might remain in the area. 373d952b791SHugh Dickins */ 374d952b791SHugh Dickins return (ret & VM_FAULT_OOM) ? -ENOMEM : 0; 37531dbd01fSIzik Eidus } 37631dbd01fSIzik Eidus 3778dd3557aSHugh Dickins static void break_cow(struct rmap_item *rmap_item) 37831dbd01fSIzik Eidus { 3798dd3557aSHugh Dickins struct mm_struct *mm = rmap_item->mm; 3808dd3557aSHugh Dickins unsigned long addr = rmap_item->address; 38131dbd01fSIzik Eidus struct vm_area_struct *vma; 38231dbd01fSIzik Eidus 3834035c07aSHugh Dickins /* 3844035c07aSHugh Dickins * It is not an accident that whenever we want to break COW 3854035c07aSHugh Dickins * to undo, we also need to drop a reference to the anon_vma. 3864035c07aSHugh Dickins */ 3879e60109fSPeter Zijlstra put_anon_vma(rmap_item->anon_vma); 3884035c07aSHugh Dickins 38981464e30SHugh Dickins down_read(&mm->mmap_sem); 3909ba69294SHugh Dickins if (ksm_test_exit(mm)) 3919ba69294SHugh Dickins goto out; 39231dbd01fSIzik Eidus vma = find_vma(mm, addr); 39331dbd01fSIzik Eidus if (!vma || vma->vm_start > addr) 39481464e30SHugh Dickins goto out; 39531dbd01fSIzik Eidus if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) 39681464e30SHugh Dickins goto out; 39731dbd01fSIzik Eidus break_ksm(vma, addr); 39881464e30SHugh Dickins out: 39931dbd01fSIzik Eidus up_read(&mm->mmap_sem); 40031dbd01fSIzik Eidus } 40131dbd01fSIzik Eidus 40229ad768cSAndrea Arcangeli static struct page *page_trans_compound_anon(struct page *page) 40329ad768cSAndrea Arcangeli { 40429ad768cSAndrea Arcangeli if (PageTransCompound(page)) { 40522e5c47eSAndrea Arcangeli struct page *head = compound_trans_head(page); 40629ad768cSAndrea Arcangeli /* 40722e5c47eSAndrea Arcangeli * head may actually be splitted and freed from under 40822e5c47eSAndrea Arcangeli * us but it's ok here. 40929ad768cSAndrea Arcangeli */ 41029ad768cSAndrea Arcangeli if (PageAnon(head)) 41129ad768cSAndrea Arcangeli return head; 41229ad768cSAndrea Arcangeli } 41329ad768cSAndrea Arcangeli return NULL; 41429ad768cSAndrea Arcangeli } 41529ad768cSAndrea Arcangeli 41631dbd01fSIzik Eidus static struct page *get_mergeable_page(struct rmap_item *rmap_item) 41731dbd01fSIzik Eidus { 41831dbd01fSIzik Eidus struct mm_struct *mm = rmap_item->mm; 41931dbd01fSIzik Eidus unsigned long addr = rmap_item->address; 42031dbd01fSIzik Eidus struct vm_area_struct *vma; 42131dbd01fSIzik Eidus struct page *page; 42231dbd01fSIzik Eidus 42331dbd01fSIzik Eidus down_read(&mm->mmap_sem); 4249ba69294SHugh Dickins if (ksm_test_exit(mm)) 4259ba69294SHugh Dickins goto out; 42631dbd01fSIzik Eidus vma = find_vma(mm, addr); 42731dbd01fSIzik Eidus if (!vma || vma->vm_start > addr) 42831dbd01fSIzik Eidus goto out; 42931dbd01fSIzik Eidus if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) 43031dbd01fSIzik Eidus goto out; 43131dbd01fSIzik Eidus 43231dbd01fSIzik Eidus page = follow_page(vma, addr, FOLL_GET); 43322eccdd7SDan Carpenter if (IS_ERR_OR_NULL(page)) 43431dbd01fSIzik Eidus goto out; 43529ad768cSAndrea Arcangeli if (PageAnon(page) || page_trans_compound_anon(page)) { 43631dbd01fSIzik Eidus flush_anon_page(vma, page, addr); 43731dbd01fSIzik Eidus flush_dcache_page(page); 43831dbd01fSIzik Eidus } else { 43931dbd01fSIzik Eidus put_page(page); 44031dbd01fSIzik Eidus out: page = NULL; 44131dbd01fSIzik Eidus } 44231dbd01fSIzik Eidus up_read(&mm->mmap_sem); 44331dbd01fSIzik Eidus return page; 44431dbd01fSIzik Eidus } 44531dbd01fSIzik Eidus 4464035c07aSHugh Dickins static void remove_node_from_stable_tree(struct stable_node *stable_node) 4474035c07aSHugh Dickins { 4484035c07aSHugh Dickins struct rmap_item *rmap_item; 4494035c07aSHugh Dickins struct hlist_node *hlist; 4504035c07aSHugh Dickins 4514035c07aSHugh Dickins hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { 4524035c07aSHugh Dickins if (rmap_item->hlist.next) 4534035c07aSHugh Dickins ksm_pages_sharing--; 4544035c07aSHugh Dickins else 4554035c07aSHugh Dickins ksm_pages_shared--; 4569e60109fSPeter Zijlstra put_anon_vma(rmap_item->anon_vma); 4574035c07aSHugh Dickins rmap_item->address &= PAGE_MASK; 4584035c07aSHugh Dickins cond_resched(); 4594035c07aSHugh Dickins } 4604035c07aSHugh Dickins 4614035c07aSHugh Dickins rb_erase(&stable_node->node, &root_stable_tree); 4624035c07aSHugh Dickins free_stable_node(stable_node); 4634035c07aSHugh Dickins } 4644035c07aSHugh Dickins 4654035c07aSHugh Dickins /* 4664035c07aSHugh Dickins * get_ksm_page: checks if the page indicated by the stable node 4674035c07aSHugh Dickins * is still its ksm page, despite having held no reference to it. 4684035c07aSHugh Dickins * In which case we can trust the content of the page, and it 4694035c07aSHugh Dickins * returns the gotten page; but if the page has now been zapped, 4704035c07aSHugh Dickins * remove the stale node from the stable tree and return NULL. 4714035c07aSHugh Dickins * 4724035c07aSHugh Dickins * You would expect the stable_node to hold a reference to the ksm page. 4734035c07aSHugh Dickins * But if it increments the page's count, swapping out has to wait for 4744035c07aSHugh Dickins * ksmd to come around again before it can free the page, which may take 4754035c07aSHugh Dickins * seconds or even minutes: much too unresponsive. So instead we use a 4764035c07aSHugh Dickins * "keyhole reference": access to the ksm page from the stable node peeps 4774035c07aSHugh Dickins * out through its keyhole to see if that page still holds the right key, 4784035c07aSHugh Dickins * pointing back to this stable node. This relies on freeing a PageAnon 4794035c07aSHugh Dickins * page to reset its page->mapping to NULL, and relies on no other use of 4804035c07aSHugh Dickins * a page to put something that might look like our key in page->mapping. 4814035c07aSHugh Dickins * 4824035c07aSHugh Dickins * include/linux/pagemap.h page_cache_get_speculative() is a good reference, 4834035c07aSHugh Dickins * but this is different - made simpler by ksm_thread_mutex being held, but 4844035c07aSHugh Dickins * interesting for assuming that no other use of the struct page could ever 4854035c07aSHugh Dickins * put our expected_mapping into page->mapping (or a field of the union which 4864035c07aSHugh Dickins * coincides with page->mapping). The RCU calls are not for KSM at all, but 4874035c07aSHugh Dickins * to keep the page_count protocol described with page_cache_get_speculative. 4884035c07aSHugh Dickins * 4894035c07aSHugh Dickins * Note: it is possible that get_ksm_page() will return NULL one moment, 4904035c07aSHugh Dickins * then page the next, if the page is in between page_freeze_refs() and 4914035c07aSHugh Dickins * page_unfreeze_refs(): this shouldn't be a problem anywhere, the page 4924035c07aSHugh Dickins * is on its way to being freed; but it is an anomaly to bear in mind. 4934035c07aSHugh Dickins */ 4944035c07aSHugh Dickins static struct page *get_ksm_page(struct stable_node *stable_node) 4954035c07aSHugh Dickins { 4964035c07aSHugh Dickins struct page *page; 4974035c07aSHugh Dickins void *expected_mapping; 4984035c07aSHugh Dickins 49962b61f61SHugh Dickins page = pfn_to_page(stable_node->kpfn); 5004035c07aSHugh Dickins expected_mapping = (void *)stable_node + 5014035c07aSHugh Dickins (PAGE_MAPPING_ANON | PAGE_MAPPING_KSM); 5024035c07aSHugh Dickins rcu_read_lock(); 5034035c07aSHugh Dickins if (page->mapping != expected_mapping) 5044035c07aSHugh Dickins goto stale; 5054035c07aSHugh Dickins if (!get_page_unless_zero(page)) 5064035c07aSHugh Dickins goto stale; 5074035c07aSHugh Dickins if (page->mapping != expected_mapping) { 5084035c07aSHugh Dickins put_page(page); 5094035c07aSHugh Dickins goto stale; 5104035c07aSHugh Dickins } 5114035c07aSHugh Dickins rcu_read_unlock(); 5124035c07aSHugh Dickins return page; 5134035c07aSHugh Dickins stale: 5144035c07aSHugh Dickins rcu_read_unlock(); 5154035c07aSHugh Dickins remove_node_from_stable_tree(stable_node); 5164035c07aSHugh Dickins return NULL; 5174035c07aSHugh Dickins } 5184035c07aSHugh Dickins 51931dbd01fSIzik Eidus /* 52031dbd01fSIzik Eidus * Removing rmap_item from stable or unstable tree. 52131dbd01fSIzik Eidus * This function will clean the information from the stable/unstable tree. 52231dbd01fSIzik Eidus */ 52331dbd01fSIzik Eidus static void remove_rmap_item_from_tree(struct rmap_item *rmap_item) 52431dbd01fSIzik Eidus { 5257b6ba2c7SHugh Dickins if (rmap_item->address & STABLE_FLAG) { 5267b6ba2c7SHugh Dickins struct stable_node *stable_node; 5275ad64688SHugh Dickins struct page *page; 52831dbd01fSIzik Eidus 5297b6ba2c7SHugh Dickins stable_node = rmap_item->head; 5304035c07aSHugh Dickins page = get_ksm_page(stable_node); 5314035c07aSHugh Dickins if (!page) 5324035c07aSHugh Dickins goto out; 5335ad64688SHugh Dickins 5344035c07aSHugh Dickins lock_page(page); 5357b6ba2c7SHugh Dickins hlist_del(&rmap_item->hlist); 5365ad64688SHugh Dickins unlock_page(page); 5375ad64688SHugh Dickins put_page(page); 53808beca44SHugh Dickins 5394035c07aSHugh Dickins if (stable_node->hlist.first) 5404035c07aSHugh Dickins ksm_pages_sharing--; 5414035c07aSHugh Dickins else 542b4028260SHugh Dickins ksm_pages_shared--; 54331dbd01fSIzik Eidus 5449e60109fSPeter Zijlstra put_anon_vma(rmap_item->anon_vma); 54593d17715SHugh Dickins rmap_item->address &= PAGE_MASK; 54631dbd01fSIzik Eidus 5477b6ba2c7SHugh Dickins } else if (rmap_item->address & UNSTABLE_FLAG) { 54831dbd01fSIzik Eidus unsigned char age; 54931dbd01fSIzik Eidus /* 5509ba69294SHugh Dickins * Usually ksmd can and must skip the rb_erase, because 55131dbd01fSIzik Eidus * root_unstable_tree was already reset to RB_ROOT. 5529ba69294SHugh Dickins * But be careful when an mm is exiting: do the rb_erase 5539ba69294SHugh Dickins * if this rmap_item was inserted by this scan, rather 5549ba69294SHugh Dickins * than left over from before. 55531dbd01fSIzik Eidus */ 55631dbd01fSIzik Eidus age = (unsigned char)(ksm_scan.seqnr - rmap_item->address); 557cd551f97SHugh Dickins BUG_ON(age > 1); 55831dbd01fSIzik Eidus if (!age) 55931dbd01fSIzik Eidus rb_erase(&rmap_item->node, &root_unstable_tree); 56031dbd01fSIzik Eidus 56193d17715SHugh Dickins ksm_pages_unshared--; 56231dbd01fSIzik Eidus rmap_item->address &= PAGE_MASK; 56393d17715SHugh Dickins } 5644035c07aSHugh Dickins out: 56531dbd01fSIzik Eidus cond_resched(); /* we're called from many long loops */ 56631dbd01fSIzik Eidus } 56731dbd01fSIzik Eidus 56831dbd01fSIzik Eidus static void remove_trailing_rmap_items(struct mm_slot *mm_slot, 5696514d511SHugh Dickins struct rmap_item **rmap_list) 57031dbd01fSIzik Eidus { 5716514d511SHugh Dickins while (*rmap_list) { 5726514d511SHugh Dickins struct rmap_item *rmap_item = *rmap_list; 5736514d511SHugh Dickins *rmap_list = rmap_item->rmap_list; 57431dbd01fSIzik Eidus remove_rmap_item_from_tree(rmap_item); 57531dbd01fSIzik Eidus free_rmap_item(rmap_item); 57631dbd01fSIzik Eidus } 57731dbd01fSIzik Eidus } 57831dbd01fSIzik Eidus 57931dbd01fSIzik Eidus /* 58031dbd01fSIzik Eidus * Though it's very tempting to unmerge in_stable_tree(rmap_item)s rather 58131dbd01fSIzik Eidus * than check every pte of a given vma, the locking doesn't quite work for 58231dbd01fSIzik Eidus * that - an rmap_item is assigned to the stable tree after inserting ksm 58331dbd01fSIzik Eidus * page and upping mmap_sem. Nor does it fit with the way we skip dup'ing 58431dbd01fSIzik Eidus * rmap_items from parent to child at fork time (so as not to waste time 58531dbd01fSIzik Eidus * if exit comes before the next scan reaches it). 58681464e30SHugh Dickins * 58781464e30SHugh Dickins * Similarly, although we'd like to remove rmap_items (so updating counts 58881464e30SHugh Dickins * and freeing memory) when unmerging an area, it's easier to leave that 58981464e30SHugh Dickins * to the next pass of ksmd - consider, for example, how ksmd might be 59081464e30SHugh Dickins * in cmp_and_merge_page on one of the rmap_items we would be removing. 59131dbd01fSIzik Eidus */ 592d952b791SHugh Dickins static int unmerge_ksm_pages(struct vm_area_struct *vma, 59331dbd01fSIzik Eidus unsigned long start, unsigned long end) 59431dbd01fSIzik Eidus { 59531dbd01fSIzik Eidus unsigned long addr; 596d952b791SHugh Dickins int err = 0; 59731dbd01fSIzik Eidus 598d952b791SHugh Dickins for (addr = start; addr < end && !err; addr += PAGE_SIZE) { 5999ba69294SHugh Dickins if (ksm_test_exit(vma->vm_mm)) 6009ba69294SHugh Dickins break; 601d952b791SHugh Dickins if (signal_pending(current)) 602d952b791SHugh Dickins err = -ERESTARTSYS; 603d952b791SHugh Dickins else 604d952b791SHugh Dickins err = break_ksm(vma, addr); 605d952b791SHugh Dickins } 606d952b791SHugh Dickins return err; 60731dbd01fSIzik Eidus } 60831dbd01fSIzik Eidus 6092ffd8679SHugh Dickins #ifdef CONFIG_SYSFS 6102ffd8679SHugh Dickins /* 6112ffd8679SHugh Dickins * Only called through the sysfs control interface: 6122ffd8679SHugh Dickins */ 613d952b791SHugh Dickins static int unmerge_and_remove_all_rmap_items(void) 61431dbd01fSIzik Eidus { 61531dbd01fSIzik Eidus struct mm_slot *mm_slot; 61631dbd01fSIzik Eidus struct mm_struct *mm; 61731dbd01fSIzik Eidus struct vm_area_struct *vma; 618d952b791SHugh Dickins int err = 0; 61931dbd01fSIzik Eidus 620d952b791SHugh Dickins spin_lock(&ksm_mmlist_lock); 6219ba69294SHugh Dickins ksm_scan.mm_slot = list_entry(ksm_mm_head.mm_list.next, 622d952b791SHugh Dickins struct mm_slot, mm_list); 623d952b791SHugh Dickins spin_unlock(&ksm_mmlist_lock); 624d952b791SHugh Dickins 6259ba69294SHugh Dickins for (mm_slot = ksm_scan.mm_slot; 6269ba69294SHugh Dickins mm_slot != &ksm_mm_head; mm_slot = ksm_scan.mm_slot) { 62731dbd01fSIzik Eidus mm = mm_slot->mm; 62831dbd01fSIzik Eidus down_read(&mm->mmap_sem); 62931dbd01fSIzik Eidus for (vma = mm->mmap; vma; vma = vma->vm_next) { 6309ba69294SHugh Dickins if (ksm_test_exit(mm)) 6319ba69294SHugh Dickins break; 63231dbd01fSIzik Eidus if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) 63331dbd01fSIzik Eidus continue; 634d952b791SHugh Dickins err = unmerge_ksm_pages(vma, 635d952b791SHugh Dickins vma->vm_start, vma->vm_end); 6369ba69294SHugh Dickins if (err) 6379ba69294SHugh Dickins goto error; 638d952b791SHugh Dickins } 6399ba69294SHugh Dickins 6406514d511SHugh Dickins remove_trailing_rmap_items(mm_slot, &mm_slot->rmap_list); 64131dbd01fSIzik Eidus 64231dbd01fSIzik Eidus spin_lock(&ksm_mmlist_lock); 6439ba69294SHugh Dickins ksm_scan.mm_slot = list_entry(mm_slot->mm_list.next, 644d952b791SHugh Dickins struct mm_slot, mm_list); 6459ba69294SHugh Dickins if (ksm_test_exit(mm)) { 6469ba69294SHugh Dickins hlist_del(&mm_slot->link); 6479ba69294SHugh Dickins list_del(&mm_slot->mm_list); 64831dbd01fSIzik Eidus spin_unlock(&ksm_mmlist_lock); 6499ba69294SHugh Dickins 6509ba69294SHugh Dickins free_mm_slot(mm_slot); 6519ba69294SHugh Dickins clear_bit(MMF_VM_MERGEABLE, &mm->flags); 6529ba69294SHugh Dickins up_read(&mm->mmap_sem); 6539ba69294SHugh Dickins mmdrop(mm); 6549ba69294SHugh Dickins } else { 6559ba69294SHugh Dickins spin_unlock(&ksm_mmlist_lock); 6569ba69294SHugh Dickins up_read(&mm->mmap_sem); 6579ba69294SHugh Dickins } 65831dbd01fSIzik Eidus } 65931dbd01fSIzik Eidus 660d952b791SHugh Dickins ksm_scan.seqnr = 0; 6619ba69294SHugh Dickins return 0; 6629ba69294SHugh Dickins 6639ba69294SHugh Dickins error: 6649ba69294SHugh Dickins up_read(&mm->mmap_sem); 665d952b791SHugh Dickins spin_lock(&ksm_mmlist_lock); 666d952b791SHugh Dickins ksm_scan.mm_slot = &ksm_mm_head; 667d952b791SHugh Dickins spin_unlock(&ksm_mmlist_lock); 668d952b791SHugh Dickins return err; 669d952b791SHugh Dickins } 6702ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */ 671d952b791SHugh Dickins 67231dbd01fSIzik Eidus static u32 calc_checksum(struct page *page) 67331dbd01fSIzik Eidus { 67431dbd01fSIzik Eidus u32 checksum; 67531dbd01fSIzik Eidus void *addr = kmap_atomic(page, KM_USER0); 67631dbd01fSIzik Eidus checksum = jhash2(addr, PAGE_SIZE / 4, 17); 67731dbd01fSIzik Eidus kunmap_atomic(addr, KM_USER0); 67831dbd01fSIzik Eidus return checksum; 67931dbd01fSIzik Eidus } 68031dbd01fSIzik Eidus 68131dbd01fSIzik Eidus static int memcmp_pages(struct page *page1, struct page *page2) 68231dbd01fSIzik Eidus { 68331dbd01fSIzik Eidus char *addr1, *addr2; 68431dbd01fSIzik Eidus int ret; 68531dbd01fSIzik Eidus 68631dbd01fSIzik Eidus addr1 = kmap_atomic(page1, KM_USER0); 68731dbd01fSIzik Eidus addr2 = kmap_atomic(page2, KM_USER1); 68831dbd01fSIzik Eidus ret = memcmp(addr1, addr2, PAGE_SIZE); 68931dbd01fSIzik Eidus kunmap_atomic(addr2, KM_USER1); 69031dbd01fSIzik Eidus kunmap_atomic(addr1, KM_USER0); 69131dbd01fSIzik Eidus return ret; 69231dbd01fSIzik Eidus } 69331dbd01fSIzik Eidus 69431dbd01fSIzik Eidus static inline int pages_identical(struct page *page1, struct page *page2) 69531dbd01fSIzik Eidus { 69631dbd01fSIzik Eidus return !memcmp_pages(page1, page2); 69731dbd01fSIzik Eidus } 69831dbd01fSIzik Eidus 69931dbd01fSIzik Eidus static int write_protect_page(struct vm_area_struct *vma, struct page *page, 70031dbd01fSIzik Eidus pte_t *orig_pte) 70131dbd01fSIzik Eidus { 70231dbd01fSIzik Eidus struct mm_struct *mm = vma->vm_mm; 70331dbd01fSIzik Eidus unsigned long addr; 70431dbd01fSIzik Eidus pte_t *ptep; 70531dbd01fSIzik Eidus spinlock_t *ptl; 70631dbd01fSIzik Eidus int swapped; 70731dbd01fSIzik Eidus int err = -EFAULT; 70831dbd01fSIzik Eidus 70931dbd01fSIzik Eidus addr = page_address_in_vma(page, vma); 71031dbd01fSIzik Eidus if (addr == -EFAULT) 71131dbd01fSIzik Eidus goto out; 71231dbd01fSIzik Eidus 71329ad768cSAndrea Arcangeli BUG_ON(PageTransCompound(page)); 71431dbd01fSIzik Eidus ptep = page_check_address(page, mm, addr, &ptl, 0); 71531dbd01fSIzik Eidus if (!ptep) 71631dbd01fSIzik Eidus goto out; 71731dbd01fSIzik Eidus 7184e31635cSHugh Dickins if (pte_write(*ptep) || pte_dirty(*ptep)) { 71931dbd01fSIzik Eidus pte_t entry; 72031dbd01fSIzik Eidus 72131dbd01fSIzik Eidus swapped = PageSwapCache(page); 72231dbd01fSIzik Eidus flush_cache_page(vma, addr, page_to_pfn(page)); 72331dbd01fSIzik Eidus /* 72425985edcSLucas De Marchi * Ok this is tricky, when get_user_pages_fast() run it doesn't 72531dbd01fSIzik Eidus * take any lock, therefore the check that we are going to make 72631dbd01fSIzik Eidus * with the pagecount against the mapcount is racey and 72731dbd01fSIzik Eidus * O_DIRECT can happen right after the check. 72831dbd01fSIzik Eidus * So we clear the pte and flush the tlb before the check 72931dbd01fSIzik Eidus * this assure us that no O_DIRECT can happen after the check 73031dbd01fSIzik Eidus * or in the middle of the check. 73131dbd01fSIzik Eidus */ 73231dbd01fSIzik Eidus entry = ptep_clear_flush(vma, addr, ptep); 73331dbd01fSIzik Eidus /* 73431dbd01fSIzik Eidus * Check that no O_DIRECT or similar I/O is in progress on the 73531dbd01fSIzik Eidus * page 73631dbd01fSIzik Eidus */ 73731e855eaSHugh Dickins if (page_mapcount(page) + 1 + swapped != page_count(page)) { 738cb532375SRobin Holt set_pte_at(mm, addr, ptep, entry); 73931dbd01fSIzik Eidus goto out_unlock; 74031dbd01fSIzik Eidus } 7414e31635cSHugh Dickins if (pte_dirty(entry)) 7424e31635cSHugh Dickins set_page_dirty(page); 7434e31635cSHugh Dickins entry = pte_mkclean(pte_wrprotect(entry)); 74431dbd01fSIzik Eidus set_pte_at_notify(mm, addr, ptep, entry); 74531dbd01fSIzik Eidus } 74631dbd01fSIzik Eidus *orig_pte = *ptep; 74731dbd01fSIzik Eidus err = 0; 74831dbd01fSIzik Eidus 74931dbd01fSIzik Eidus out_unlock: 75031dbd01fSIzik Eidus pte_unmap_unlock(ptep, ptl); 75131dbd01fSIzik Eidus out: 75231dbd01fSIzik Eidus return err; 75331dbd01fSIzik Eidus } 75431dbd01fSIzik Eidus 75531dbd01fSIzik Eidus /** 75631dbd01fSIzik Eidus * replace_page - replace page in vma by new ksm page 7578dd3557aSHugh Dickins * @vma: vma that holds the pte pointing to page 7588dd3557aSHugh Dickins * @page: the page we are replacing by kpage 7598dd3557aSHugh Dickins * @kpage: the ksm page we replace page by 76031dbd01fSIzik Eidus * @orig_pte: the original value of the pte 76131dbd01fSIzik Eidus * 76231dbd01fSIzik Eidus * Returns 0 on success, -EFAULT on failure. 76331dbd01fSIzik Eidus */ 7648dd3557aSHugh Dickins static int replace_page(struct vm_area_struct *vma, struct page *page, 7658dd3557aSHugh Dickins struct page *kpage, pte_t orig_pte) 76631dbd01fSIzik Eidus { 76731dbd01fSIzik Eidus struct mm_struct *mm = vma->vm_mm; 76831dbd01fSIzik Eidus pgd_t *pgd; 76931dbd01fSIzik Eidus pud_t *pud; 77031dbd01fSIzik Eidus pmd_t *pmd; 77131dbd01fSIzik Eidus pte_t *ptep; 77231dbd01fSIzik Eidus spinlock_t *ptl; 77331dbd01fSIzik Eidus unsigned long addr; 77431dbd01fSIzik Eidus int err = -EFAULT; 77531dbd01fSIzik Eidus 7768dd3557aSHugh Dickins addr = page_address_in_vma(page, vma); 77731dbd01fSIzik Eidus if (addr == -EFAULT) 77831dbd01fSIzik Eidus goto out; 77931dbd01fSIzik Eidus 78031dbd01fSIzik Eidus pgd = pgd_offset(mm, addr); 78131dbd01fSIzik Eidus if (!pgd_present(*pgd)) 78231dbd01fSIzik Eidus goto out; 78331dbd01fSIzik Eidus 78431dbd01fSIzik Eidus pud = pud_offset(pgd, addr); 78531dbd01fSIzik Eidus if (!pud_present(*pud)) 78631dbd01fSIzik Eidus goto out; 78731dbd01fSIzik Eidus 78831dbd01fSIzik Eidus pmd = pmd_offset(pud, addr); 78929ad768cSAndrea Arcangeli BUG_ON(pmd_trans_huge(*pmd)); 79031dbd01fSIzik Eidus if (!pmd_present(*pmd)) 79131dbd01fSIzik Eidus goto out; 79231dbd01fSIzik Eidus 79331dbd01fSIzik Eidus ptep = pte_offset_map_lock(mm, pmd, addr, &ptl); 79431dbd01fSIzik Eidus if (!pte_same(*ptep, orig_pte)) { 79531dbd01fSIzik Eidus pte_unmap_unlock(ptep, ptl); 79631dbd01fSIzik Eidus goto out; 79731dbd01fSIzik Eidus } 79831dbd01fSIzik Eidus 7998dd3557aSHugh Dickins get_page(kpage); 8005ad64688SHugh Dickins page_add_anon_rmap(kpage, vma, addr); 80131dbd01fSIzik Eidus 80231dbd01fSIzik Eidus flush_cache_page(vma, addr, pte_pfn(*ptep)); 80331dbd01fSIzik Eidus ptep_clear_flush(vma, addr, ptep); 8048dd3557aSHugh Dickins set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); 80531dbd01fSIzik Eidus 8068dd3557aSHugh Dickins page_remove_rmap(page); 807ae52a2adSHugh Dickins if (!page_mapped(page)) 808ae52a2adSHugh Dickins try_to_free_swap(page); 8098dd3557aSHugh Dickins put_page(page); 81031dbd01fSIzik Eidus 81131dbd01fSIzik Eidus pte_unmap_unlock(ptep, ptl); 81231dbd01fSIzik Eidus err = 0; 81331dbd01fSIzik Eidus out: 81431dbd01fSIzik Eidus return err; 81531dbd01fSIzik Eidus } 81631dbd01fSIzik Eidus 81729ad768cSAndrea Arcangeli static int page_trans_compound_anon_split(struct page *page) 81829ad768cSAndrea Arcangeli { 81929ad768cSAndrea Arcangeli int ret = 0; 82029ad768cSAndrea Arcangeli struct page *transhuge_head = page_trans_compound_anon(page); 82129ad768cSAndrea Arcangeli if (transhuge_head) { 82229ad768cSAndrea Arcangeli /* Get the reference on the head to split it. */ 82329ad768cSAndrea Arcangeli if (get_page_unless_zero(transhuge_head)) { 82429ad768cSAndrea Arcangeli /* 82529ad768cSAndrea Arcangeli * Recheck we got the reference while the head 82629ad768cSAndrea Arcangeli * was still anonymous. 82729ad768cSAndrea Arcangeli */ 82829ad768cSAndrea Arcangeli if (PageAnon(transhuge_head)) 82929ad768cSAndrea Arcangeli ret = split_huge_page(transhuge_head); 83029ad768cSAndrea Arcangeli else 83129ad768cSAndrea Arcangeli /* 83229ad768cSAndrea Arcangeli * Retry later if split_huge_page run 83329ad768cSAndrea Arcangeli * from under us. 83429ad768cSAndrea Arcangeli */ 83529ad768cSAndrea Arcangeli ret = 1; 83629ad768cSAndrea Arcangeli put_page(transhuge_head); 83729ad768cSAndrea Arcangeli } else 83829ad768cSAndrea Arcangeli /* Retry later if split_huge_page run from under us. */ 83929ad768cSAndrea Arcangeli ret = 1; 84029ad768cSAndrea Arcangeli } 84129ad768cSAndrea Arcangeli return ret; 84229ad768cSAndrea Arcangeli } 84329ad768cSAndrea Arcangeli 84431dbd01fSIzik Eidus /* 84531dbd01fSIzik Eidus * try_to_merge_one_page - take two pages and merge them into one 8468dd3557aSHugh Dickins * @vma: the vma that holds the pte pointing to page 8478dd3557aSHugh Dickins * @page: the PageAnon page that we want to replace with kpage 84880e14822SHugh Dickins * @kpage: the PageKsm page that we want to map instead of page, 84980e14822SHugh Dickins * or NULL the first time when we want to use page as kpage. 85031dbd01fSIzik Eidus * 85131dbd01fSIzik Eidus * This function returns 0 if the pages were merged, -EFAULT otherwise. 85231dbd01fSIzik Eidus */ 85331dbd01fSIzik Eidus static int try_to_merge_one_page(struct vm_area_struct *vma, 8548dd3557aSHugh Dickins struct page *page, struct page *kpage) 85531dbd01fSIzik Eidus { 85631dbd01fSIzik Eidus pte_t orig_pte = __pte(0); 85731dbd01fSIzik Eidus int err = -EFAULT; 85831dbd01fSIzik Eidus 859db114b83SHugh Dickins if (page == kpage) /* ksm page forked */ 860db114b83SHugh Dickins return 0; 861db114b83SHugh Dickins 86231dbd01fSIzik Eidus if (!(vma->vm_flags & VM_MERGEABLE)) 86331dbd01fSIzik Eidus goto out; 86429ad768cSAndrea Arcangeli if (PageTransCompound(page) && page_trans_compound_anon_split(page)) 86529ad768cSAndrea Arcangeli goto out; 86629ad768cSAndrea Arcangeli BUG_ON(PageTransCompound(page)); 8678dd3557aSHugh Dickins if (!PageAnon(page)) 86831dbd01fSIzik Eidus goto out; 86931dbd01fSIzik Eidus 87031dbd01fSIzik Eidus /* 87131dbd01fSIzik Eidus * We need the page lock to read a stable PageSwapCache in 87231dbd01fSIzik Eidus * write_protect_page(). We use trylock_page() instead of 87331dbd01fSIzik Eidus * lock_page() because we don't want to wait here - we 87431dbd01fSIzik Eidus * prefer to continue scanning and merging different pages, 87531dbd01fSIzik Eidus * then come back to this page when it is unlocked. 87631dbd01fSIzik Eidus */ 8778dd3557aSHugh Dickins if (!trylock_page(page)) 87831e855eaSHugh Dickins goto out; 87931dbd01fSIzik Eidus /* 88031dbd01fSIzik Eidus * If this anonymous page is mapped only here, its pte may need 88131dbd01fSIzik Eidus * to be write-protected. If it's mapped elsewhere, all of its 88231dbd01fSIzik Eidus * ptes are necessarily already write-protected. But in either 88331dbd01fSIzik Eidus * case, we need to lock and check page_count is not raised. 88431dbd01fSIzik Eidus */ 88580e14822SHugh Dickins if (write_protect_page(vma, page, &orig_pte) == 0) { 88680e14822SHugh Dickins if (!kpage) { 88780e14822SHugh Dickins /* 88880e14822SHugh Dickins * While we hold page lock, upgrade page from 88980e14822SHugh Dickins * PageAnon+anon_vma to PageKsm+NULL stable_node: 89080e14822SHugh Dickins * stable_tree_insert() will update stable_node. 89180e14822SHugh Dickins */ 89280e14822SHugh Dickins set_page_stable_node(page, NULL); 89380e14822SHugh Dickins mark_page_accessed(page); 89480e14822SHugh Dickins err = 0; 89580e14822SHugh Dickins } else if (pages_identical(page, kpage)) 8968dd3557aSHugh Dickins err = replace_page(vma, page, kpage, orig_pte); 89780e14822SHugh Dickins } 89831dbd01fSIzik Eidus 89980e14822SHugh Dickins if ((vma->vm_flags & VM_LOCKED) && kpage && !err) { 90073848b46SHugh Dickins munlock_vma_page(page); 9015ad64688SHugh Dickins if (!PageMlocked(kpage)) { 9025ad64688SHugh Dickins unlock_page(page); 9035ad64688SHugh Dickins lock_page(kpage); 9045ad64688SHugh Dickins mlock_vma_page(kpage); 9055ad64688SHugh Dickins page = kpage; /* for final unlock */ 9065ad64688SHugh Dickins } 9075ad64688SHugh Dickins } 90873848b46SHugh Dickins 9098dd3557aSHugh Dickins unlock_page(page); 91031dbd01fSIzik Eidus out: 91131dbd01fSIzik Eidus return err; 91231dbd01fSIzik Eidus } 91331dbd01fSIzik Eidus 91431dbd01fSIzik Eidus /* 91581464e30SHugh Dickins * try_to_merge_with_ksm_page - like try_to_merge_two_pages, 91681464e30SHugh Dickins * but no new kernel page is allocated: kpage must already be a ksm page. 9178dd3557aSHugh Dickins * 9188dd3557aSHugh Dickins * This function returns 0 if the pages were merged, -EFAULT otherwise. 91981464e30SHugh Dickins */ 9208dd3557aSHugh Dickins static int try_to_merge_with_ksm_page(struct rmap_item *rmap_item, 9218dd3557aSHugh Dickins struct page *page, struct page *kpage) 92281464e30SHugh Dickins { 9238dd3557aSHugh Dickins struct mm_struct *mm = rmap_item->mm; 92481464e30SHugh Dickins struct vm_area_struct *vma; 92581464e30SHugh Dickins int err = -EFAULT; 92681464e30SHugh Dickins 9278dd3557aSHugh Dickins down_read(&mm->mmap_sem); 9288dd3557aSHugh Dickins if (ksm_test_exit(mm)) 9298dd3557aSHugh Dickins goto out; 9308dd3557aSHugh Dickins vma = find_vma(mm, rmap_item->address); 9318dd3557aSHugh Dickins if (!vma || vma->vm_start > rmap_item->address) 9329ba69294SHugh Dickins goto out; 9339ba69294SHugh Dickins 9348dd3557aSHugh Dickins err = try_to_merge_one_page(vma, page, kpage); 935db114b83SHugh Dickins if (err) 936db114b83SHugh Dickins goto out; 937db114b83SHugh Dickins 938db114b83SHugh Dickins /* Must get reference to anon_vma while still holding mmap_sem */ 9399e60109fSPeter Zijlstra rmap_item->anon_vma = vma->anon_vma; 9409e60109fSPeter Zijlstra get_anon_vma(vma->anon_vma); 94181464e30SHugh Dickins out: 9428dd3557aSHugh Dickins up_read(&mm->mmap_sem); 94381464e30SHugh Dickins return err; 94481464e30SHugh Dickins } 94581464e30SHugh Dickins 94681464e30SHugh Dickins /* 94731dbd01fSIzik Eidus * try_to_merge_two_pages - take two identical pages and prepare them 94831dbd01fSIzik Eidus * to be merged into one page. 94931dbd01fSIzik Eidus * 9508dd3557aSHugh Dickins * This function returns the kpage if we successfully merged two identical 9518dd3557aSHugh Dickins * pages into one ksm page, NULL otherwise. 95231dbd01fSIzik Eidus * 95380e14822SHugh Dickins * Note that this function upgrades page to ksm page: if one of the pages 95431dbd01fSIzik Eidus * is already a ksm page, try_to_merge_with_ksm_page should be used. 95531dbd01fSIzik Eidus */ 9568dd3557aSHugh Dickins static struct page *try_to_merge_two_pages(struct rmap_item *rmap_item, 9578dd3557aSHugh Dickins struct page *page, 9588dd3557aSHugh Dickins struct rmap_item *tree_rmap_item, 9598dd3557aSHugh Dickins struct page *tree_page) 96031dbd01fSIzik Eidus { 96180e14822SHugh Dickins int err; 96231dbd01fSIzik Eidus 96380e14822SHugh Dickins err = try_to_merge_with_ksm_page(rmap_item, page, NULL); 96431dbd01fSIzik Eidus if (!err) { 9658dd3557aSHugh Dickins err = try_to_merge_with_ksm_page(tree_rmap_item, 96680e14822SHugh Dickins tree_page, page); 96731dbd01fSIzik Eidus /* 96881464e30SHugh Dickins * If that fails, we have a ksm page with only one pte 96981464e30SHugh Dickins * pointing to it: so break it. 97031dbd01fSIzik Eidus */ 9714035c07aSHugh Dickins if (err) 9728dd3557aSHugh Dickins break_cow(rmap_item); 97331dbd01fSIzik Eidus } 97480e14822SHugh Dickins return err ? NULL : page; 97531dbd01fSIzik Eidus } 97631dbd01fSIzik Eidus 97731dbd01fSIzik Eidus /* 9788dd3557aSHugh Dickins * stable_tree_search - search for page inside the stable tree 97931dbd01fSIzik Eidus * 98031dbd01fSIzik Eidus * This function checks if there is a page inside the stable tree 98131dbd01fSIzik Eidus * with identical content to the page that we are scanning right now. 98231dbd01fSIzik Eidus * 9837b6ba2c7SHugh Dickins * This function returns the stable tree node of identical content if found, 98431dbd01fSIzik Eidus * NULL otherwise. 98531dbd01fSIzik Eidus */ 98662b61f61SHugh Dickins static struct page *stable_tree_search(struct page *page) 98731dbd01fSIzik Eidus { 98831dbd01fSIzik Eidus struct rb_node *node = root_stable_tree.rb_node; 9897b6ba2c7SHugh Dickins struct stable_node *stable_node; 99031dbd01fSIzik Eidus 99108beca44SHugh Dickins stable_node = page_stable_node(page); 99208beca44SHugh Dickins if (stable_node) { /* ksm page forked */ 99308beca44SHugh Dickins get_page(page); 99462b61f61SHugh Dickins return page; 99508beca44SHugh Dickins } 99608beca44SHugh Dickins 99731dbd01fSIzik Eidus while (node) { 9984035c07aSHugh Dickins struct page *tree_page; 99931dbd01fSIzik Eidus int ret; 100031dbd01fSIzik Eidus 100131dbd01fSIzik Eidus cond_resched(); 100208beca44SHugh Dickins stable_node = rb_entry(node, struct stable_node, node); 10034035c07aSHugh Dickins tree_page = get_ksm_page(stable_node); 10044035c07aSHugh Dickins if (!tree_page) 10054035c07aSHugh Dickins return NULL; 100631dbd01fSIzik Eidus 10074035c07aSHugh Dickins ret = memcmp_pages(page, tree_page); 100831dbd01fSIzik Eidus 10094035c07aSHugh Dickins if (ret < 0) { 10104035c07aSHugh Dickins put_page(tree_page); 101131dbd01fSIzik Eidus node = node->rb_left; 10124035c07aSHugh Dickins } else if (ret > 0) { 10134035c07aSHugh Dickins put_page(tree_page); 101431dbd01fSIzik Eidus node = node->rb_right; 10154035c07aSHugh Dickins } else 101662b61f61SHugh Dickins return tree_page; 101731dbd01fSIzik Eidus } 101831dbd01fSIzik Eidus 101931dbd01fSIzik Eidus return NULL; 102031dbd01fSIzik Eidus } 102131dbd01fSIzik Eidus 102231dbd01fSIzik Eidus /* 102331dbd01fSIzik Eidus * stable_tree_insert - insert rmap_item pointing to new ksm page 102431dbd01fSIzik Eidus * into the stable tree. 102531dbd01fSIzik Eidus * 10267b6ba2c7SHugh Dickins * This function returns the stable tree node just allocated on success, 10277b6ba2c7SHugh Dickins * NULL otherwise. 102831dbd01fSIzik Eidus */ 10297b6ba2c7SHugh Dickins static struct stable_node *stable_tree_insert(struct page *kpage) 103031dbd01fSIzik Eidus { 103131dbd01fSIzik Eidus struct rb_node **new = &root_stable_tree.rb_node; 103231dbd01fSIzik Eidus struct rb_node *parent = NULL; 10337b6ba2c7SHugh Dickins struct stable_node *stable_node; 103431dbd01fSIzik Eidus 103531dbd01fSIzik Eidus while (*new) { 10364035c07aSHugh Dickins struct page *tree_page; 103731dbd01fSIzik Eidus int ret; 103831dbd01fSIzik Eidus 103931dbd01fSIzik Eidus cond_resched(); 104008beca44SHugh Dickins stable_node = rb_entry(*new, struct stable_node, node); 10414035c07aSHugh Dickins tree_page = get_ksm_page(stable_node); 10424035c07aSHugh Dickins if (!tree_page) 10434035c07aSHugh Dickins return NULL; 104431dbd01fSIzik Eidus 10454035c07aSHugh Dickins ret = memcmp_pages(kpage, tree_page); 10464035c07aSHugh Dickins put_page(tree_page); 104731dbd01fSIzik Eidus 104831dbd01fSIzik Eidus parent = *new; 104931dbd01fSIzik Eidus if (ret < 0) 105031dbd01fSIzik Eidus new = &parent->rb_left; 105131dbd01fSIzik Eidus else if (ret > 0) 105231dbd01fSIzik Eidus new = &parent->rb_right; 105331dbd01fSIzik Eidus else { 105431dbd01fSIzik Eidus /* 105531dbd01fSIzik Eidus * It is not a bug that stable_tree_search() didn't 105631dbd01fSIzik Eidus * find this node: because at that time our page was 105731dbd01fSIzik Eidus * not yet write-protected, so may have changed since. 105831dbd01fSIzik Eidus */ 105931dbd01fSIzik Eidus return NULL; 106031dbd01fSIzik Eidus } 106131dbd01fSIzik Eidus } 106231dbd01fSIzik Eidus 10637b6ba2c7SHugh Dickins stable_node = alloc_stable_node(); 10647b6ba2c7SHugh Dickins if (!stable_node) 10657b6ba2c7SHugh Dickins return NULL; 106631dbd01fSIzik Eidus 10677b6ba2c7SHugh Dickins rb_link_node(&stable_node->node, parent, new); 10687b6ba2c7SHugh Dickins rb_insert_color(&stable_node->node, &root_stable_tree); 10697b6ba2c7SHugh Dickins 10707b6ba2c7SHugh Dickins INIT_HLIST_HEAD(&stable_node->hlist); 10717b6ba2c7SHugh Dickins 107262b61f61SHugh Dickins stable_node->kpfn = page_to_pfn(kpage); 107308beca44SHugh Dickins set_page_stable_node(kpage, stable_node); 107408beca44SHugh Dickins 10757b6ba2c7SHugh Dickins return stable_node; 107631dbd01fSIzik Eidus } 107731dbd01fSIzik Eidus 107831dbd01fSIzik Eidus /* 10798dd3557aSHugh Dickins * unstable_tree_search_insert - search for identical page, 10808dd3557aSHugh Dickins * else insert rmap_item into the unstable tree. 108131dbd01fSIzik Eidus * 108231dbd01fSIzik Eidus * This function searches for a page in the unstable tree identical to the 108331dbd01fSIzik Eidus * page currently being scanned; and if no identical page is found in the 108431dbd01fSIzik Eidus * tree, we insert rmap_item as a new object into the unstable tree. 108531dbd01fSIzik Eidus * 108631dbd01fSIzik Eidus * This function returns pointer to rmap_item found to be identical 108731dbd01fSIzik Eidus * to the currently scanned page, NULL otherwise. 108831dbd01fSIzik Eidus * 108931dbd01fSIzik Eidus * This function does both searching and inserting, because they share 109031dbd01fSIzik Eidus * the same walking algorithm in an rbtree. 109131dbd01fSIzik Eidus */ 10928dd3557aSHugh Dickins static 10938dd3557aSHugh Dickins struct rmap_item *unstable_tree_search_insert(struct rmap_item *rmap_item, 10948dd3557aSHugh Dickins struct page *page, 10958dd3557aSHugh Dickins struct page **tree_pagep) 10968dd3557aSHugh Dickins 109731dbd01fSIzik Eidus { 109831dbd01fSIzik Eidus struct rb_node **new = &root_unstable_tree.rb_node; 109931dbd01fSIzik Eidus struct rb_node *parent = NULL; 110031dbd01fSIzik Eidus 110131dbd01fSIzik Eidus while (*new) { 110231dbd01fSIzik Eidus struct rmap_item *tree_rmap_item; 11038dd3557aSHugh Dickins struct page *tree_page; 110431dbd01fSIzik Eidus int ret; 110531dbd01fSIzik Eidus 1106d178f27fSHugh Dickins cond_resched(); 110731dbd01fSIzik Eidus tree_rmap_item = rb_entry(*new, struct rmap_item, node); 11088dd3557aSHugh Dickins tree_page = get_mergeable_page(tree_rmap_item); 110922eccdd7SDan Carpenter if (IS_ERR_OR_NULL(tree_page)) 111031dbd01fSIzik Eidus return NULL; 111131dbd01fSIzik Eidus 111231dbd01fSIzik Eidus /* 11138dd3557aSHugh Dickins * Don't substitute a ksm page for a forked page. 111431dbd01fSIzik Eidus */ 11158dd3557aSHugh Dickins if (page == tree_page) { 11168dd3557aSHugh Dickins put_page(tree_page); 111731dbd01fSIzik Eidus return NULL; 111831dbd01fSIzik Eidus } 111931dbd01fSIzik Eidus 11208dd3557aSHugh Dickins ret = memcmp_pages(page, tree_page); 112131dbd01fSIzik Eidus 112231dbd01fSIzik Eidus parent = *new; 112331dbd01fSIzik Eidus if (ret < 0) { 11248dd3557aSHugh Dickins put_page(tree_page); 112531dbd01fSIzik Eidus new = &parent->rb_left; 112631dbd01fSIzik Eidus } else if (ret > 0) { 11278dd3557aSHugh Dickins put_page(tree_page); 112831dbd01fSIzik Eidus new = &parent->rb_right; 112931dbd01fSIzik Eidus } else { 11308dd3557aSHugh Dickins *tree_pagep = tree_page; 113131dbd01fSIzik Eidus return tree_rmap_item; 113231dbd01fSIzik Eidus } 113331dbd01fSIzik Eidus } 113431dbd01fSIzik Eidus 11357b6ba2c7SHugh Dickins rmap_item->address |= UNSTABLE_FLAG; 113631dbd01fSIzik Eidus rmap_item->address |= (ksm_scan.seqnr & SEQNR_MASK); 113731dbd01fSIzik Eidus rb_link_node(&rmap_item->node, parent, new); 113831dbd01fSIzik Eidus rb_insert_color(&rmap_item->node, &root_unstable_tree); 113931dbd01fSIzik Eidus 1140473b0ce4SHugh Dickins ksm_pages_unshared++; 114131dbd01fSIzik Eidus return NULL; 114231dbd01fSIzik Eidus } 114331dbd01fSIzik Eidus 114431dbd01fSIzik Eidus /* 114531dbd01fSIzik Eidus * stable_tree_append - add another rmap_item to the linked list of 114631dbd01fSIzik Eidus * rmap_items hanging off a given node of the stable tree, all sharing 114731dbd01fSIzik Eidus * the same ksm page. 114831dbd01fSIzik Eidus */ 114931dbd01fSIzik Eidus static void stable_tree_append(struct rmap_item *rmap_item, 11507b6ba2c7SHugh Dickins struct stable_node *stable_node) 115131dbd01fSIzik Eidus { 11527b6ba2c7SHugh Dickins rmap_item->head = stable_node; 115331dbd01fSIzik Eidus rmap_item->address |= STABLE_FLAG; 11547b6ba2c7SHugh Dickins hlist_add_head(&rmap_item->hlist, &stable_node->hlist); 1155e178dfdeSHugh Dickins 11567b6ba2c7SHugh Dickins if (rmap_item->hlist.next) 1157e178dfdeSHugh Dickins ksm_pages_sharing++; 11587b6ba2c7SHugh Dickins else 11597b6ba2c7SHugh Dickins ksm_pages_shared++; 116031dbd01fSIzik Eidus } 116131dbd01fSIzik Eidus 116231dbd01fSIzik Eidus /* 116381464e30SHugh Dickins * cmp_and_merge_page - first see if page can be merged into the stable tree; 116481464e30SHugh Dickins * if not, compare checksum to previous and if it's the same, see if page can 116581464e30SHugh Dickins * be inserted into the unstable tree, or merged with a page already there and 116681464e30SHugh Dickins * both transferred to the stable tree. 116731dbd01fSIzik Eidus * 116831dbd01fSIzik Eidus * @page: the page that we are searching identical page to. 116931dbd01fSIzik Eidus * @rmap_item: the reverse mapping into the virtual address of this page 117031dbd01fSIzik Eidus */ 117131dbd01fSIzik Eidus static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) 117231dbd01fSIzik Eidus { 117331dbd01fSIzik Eidus struct rmap_item *tree_rmap_item; 11748dd3557aSHugh Dickins struct page *tree_page = NULL; 11757b6ba2c7SHugh Dickins struct stable_node *stable_node; 11768dd3557aSHugh Dickins struct page *kpage; 117731dbd01fSIzik Eidus unsigned int checksum; 117831dbd01fSIzik Eidus int err; 117931dbd01fSIzik Eidus 118031dbd01fSIzik Eidus remove_rmap_item_from_tree(rmap_item); 118131dbd01fSIzik Eidus 118231dbd01fSIzik Eidus /* We first start with searching the page inside the stable tree */ 118362b61f61SHugh Dickins kpage = stable_tree_search(page); 118462b61f61SHugh Dickins if (kpage) { 118508beca44SHugh Dickins err = try_to_merge_with_ksm_page(rmap_item, page, kpage); 118631dbd01fSIzik Eidus if (!err) { 118731dbd01fSIzik Eidus /* 118831dbd01fSIzik Eidus * The page was successfully merged: 118931dbd01fSIzik Eidus * add its rmap_item to the stable tree. 119031dbd01fSIzik Eidus */ 11915ad64688SHugh Dickins lock_page(kpage); 119262b61f61SHugh Dickins stable_tree_append(rmap_item, page_stable_node(kpage)); 11935ad64688SHugh Dickins unlock_page(kpage); 119431dbd01fSIzik Eidus } 11958dd3557aSHugh Dickins put_page(kpage); 119631dbd01fSIzik Eidus return; 119731dbd01fSIzik Eidus } 119831dbd01fSIzik Eidus 119931dbd01fSIzik Eidus /* 12004035c07aSHugh Dickins * If the hash value of the page has changed from the last time 12014035c07aSHugh Dickins * we calculated it, this page is changing frequently: therefore we 12024035c07aSHugh Dickins * don't want to insert it in the unstable tree, and we don't want 12034035c07aSHugh Dickins * to waste our time searching for something identical to it there. 120431dbd01fSIzik Eidus */ 120531dbd01fSIzik Eidus checksum = calc_checksum(page); 120631dbd01fSIzik Eidus if (rmap_item->oldchecksum != checksum) { 120731dbd01fSIzik Eidus rmap_item->oldchecksum = checksum; 120831dbd01fSIzik Eidus return; 120931dbd01fSIzik Eidus } 121031dbd01fSIzik Eidus 12118dd3557aSHugh Dickins tree_rmap_item = 12128dd3557aSHugh Dickins unstable_tree_search_insert(rmap_item, page, &tree_page); 121331dbd01fSIzik Eidus if (tree_rmap_item) { 12148dd3557aSHugh Dickins kpage = try_to_merge_two_pages(rmap_item, page, 12158dd3557aSHugh Dickins tree_rmap_item, tree_page); 12168dd3557aSHugh Dickins put_page(tree_page); 121731dbd01fSIzik Eidus /* 121831dbd01fSIzik Eidus * As soon as we merge this page, we want to remove the 121931dbd01fSIzik Eidus * rmap_item of the page we have merged with from the unstable 122031dbd01fSIzik Eidus * tree, and insert it instead as new node in the stable tree. 122131dbd01fSIzik Eidus */ 12228dd3557aSHugh Dickins if (kpage) { 122393d17715SHugh Dickins remove_rmap_item_from_tree(tree_rmap_item); 1224473b0ce4SHugh Dickins 12255ad64688SHugh Dickins lock_page(kpage); 12267b6ba2c7SHugh Dickins stable_node = stable_tree_insert(kpage); 12277b6ba2c7SHugh Dickins if (stable_node) { 12287b6ba2c7SHugh Dickins stable_tree_append(tree_rmap_item, stable_node); 12297b6ba2c7SHugh Dickins stable_tree_append(rmap_item, stable_node); 12307b6ba2c7SHugh Dickins } 12315ad64688SHugh Dickins unlock_page(kpage); 12327b6ba2c7SHugh Dickins 123331dbd01fSIzik Eidus /* 123431dbd01fSIzik Eidus * If we fail to insert the page into the stable tree, 123531dbd01fSIzik Eidus * we will have 2 virtual addresses that are pointing 123631dbd01fSIzik Eidus * to a ksm page left outside the stable tree, 123731dbd01fSIzik Eidus * in which case we need to break_cow on both. 123831dbd01fSIzik Eidus */ 12397b6ba2c7SHugh Dickins if (!stable_node) { 12408dd3557aSHugh Dickins break_cow(tree_rmap_item); 12418dd3557aSHugh Dickins break_cow(rmap_item); 124231dbd01fSIzik Eidus } 124331dbd01fSIzik Eidus } 124431dbd01fSIzik Eidus } 124531dbd01fSIzik Eidus } 124631dbd01fSIzik Eidus 124731dbd01fSIzik Eidus static struct rmap_item *get_next_rmap_item(struct mm_slot *mm_slot, 12486514d511SHugh Dickins struct rmap_item **rmap_list, 124931dbd01fSIzik Eidus unsigned long addr) 125031dbd01fSIzik Eidus { 125131dbd01fSIzik Eidus struct rmap_item *rmap_item; 125231dbd01fSIzik Eidus 12536514d511SHugh Dickins while (*rmap_list) { 12546514d511SHugh Dickins rmap_item = *rmap_list; 125593d17715SHugh Dickins if ((rmap_item->address & PAGE_MASK) == addr) 125631dbd01fSIzik Eidus return rmap_item; 125731dbd01fSIzik Eidus if (rmap_item->address > addr) 125831dbd01fSIzik Eidus break; 12596514d511SHugh Dickins *rmap_list = rmap_item->rmap_list; 126031dbd01fSIzik Eidus remove_rmap_item_from_tree(rmap_item); 126131dbd01fSIzik Eidus free_rmap_item(rmap_item); 126231dbd01fSIzik Eidus } 126331dbd01fSIzik Eidus 126431dbd01fSIzik Eidus rmap_item = alloc_rmap_item(); 126531dbd01fSIzik Eidus if (rmap_item) { 126631dbd01fSIzik Eidus /* It has already been zeroed */ 126731dbd01fSIzik Eidus rmap_item->mm = mm_slot->mm; 126831dbd01fSIzik Eidus rmap_item->address = addr; 12696514d511SHugh Dickins rmap_item->rmap_list = *rmap_list; 12706514d511SHugh Dickins *rmap_list = rmap_item; 127131dbd01fSIzik Eidus } 127231dbd01fSIzik Eidus return rmap_item; 127331dbd01fSIzik Eidus } 127431dbd01fSIzik Eidus 127531dbd01fSIzik Eidus static struct rmap_item *scan_get_next_rmap_item(struct page **page) 127631dbd01fSIzik Eidus { 127731dbd01fSIzik Eidus struct mm_struct *mm; 127831dbd01fSIzik Eidus struct mm_slot *slot; 127931dbd01fSIzik Eidus struct vm_area_struct *vma; 128031dbd01fSIzik Eidus struct rmap_item *rmap_item; 128131dbd01fSIzik Eidus 128231dbd01fSIzik Eidus if (list_empty(&ksm_mm_head.mm_list)) 128331dbd01fSIzik Eidus return NULL; 128431dbd01fSIzik Eidus 128531dbd01fSIzik Eidus slot = ksm_scan.mm_slot; 128631dbd01fSIzik Eidus if (slot == &ksm_mm_head) { 12872919bfd0SHugh Dickins /* 12882919bfd0SHugh Dickins * A number of pages can hang around indefinitely on per-cpu 12892919bfd0SHugh Dickins * pagevecs, raised page count preventing write_protect_page 12902919bfd0SHugh Dickins * from merging them. Though it doesn't really matter much, 12912919bfd0SHugh Dickins * it is puzzling to see some stuck in pages_volatile until 12922919bfd0SHugh Dickins * other activity jostles them out, and they also prevented 12932919bfd0SHugh Dickins * LTP's KSM test from succeeding deterministically; so drain 12942919bfd0SHugh Dickins * them here (here rather than on entry to ksm_do_scan(), 12952919bfd0SHugh Dickins * so we don't IPI too often when pages_to_scan is set low). 12962919bfd0SHugh Dickins */ 12972919bfd0SHugh Dickins lru_add_drain_all(); 12982919bfd0SHugh Dickins 129931dbd01fSIzik Eidus root_unstable_tree = RB_ROOT; 130031dbd01fSIzik Eidus 130131dbd01fSIzik Eidus spin_lock(&ksm_mmlist_lock); 130231dbd01fSIzik Eidus slot = list_entry(slot->mm_list.next, struct mm_slot, mm_list); 130331dbd01fSIzik Eidus ksm_scan.mm_slot = slot; 130431dbd01fSIzik Eidus spin_unlock(&ksm_mmlist_lock); 130531dbd01fSIzik Eidus next_mm: 130631dbd01fSIzik Eidus ksm_scan.address = 0; 13076514d511SHugh Dickins ksm_scan.rmap_list = &slot->rmap_list; 130831dbd01fSIzik Eidus } 130931dbd01fSIzik Eidus 131031dbd01fSIzik Eidus mm = slot->mm; 131131dbd01fSIzik Eidus down_read(&mm->mmap_sem); 13129ba69294SHugh Dickins if (ksm_test_exit(mm)) 13139ba69294SHugh Dickins vma = NULL; 13149ba69294SHugh Dickins else 13159ba69294SHugh Dickins vma = find_vma(mm, ksm_scan.address); 13169ba69294SHugh Dickins 13179ba69294SHugh Dickins for (; vma; vma = vma->vm_next) { 131831dbd01fSIzik Eidus if (!(vma->vm_flags & VM_MERGEABLE)) 131931dbd01fSIzik Eidus continue; 132031dbd01fSIzik Eidus if (ksm_scan.address < vma->vm_start) 132131dbd01fSIzik Eidus ksm_scan.address = vma->vm_start; 132231dbd01fSIzik Eidus if (!vma->anon_vma) 132331dbd01fSIzik Eidus ksm_scan.address = vma->vm_end; 132431dbd01fSIzik Eidus 132531dbd01fSIzik Eidus while (ksm_scan.address < vma->vm_end) { 13269ba69294SHugh Dickins if (ksm_test_exit(mm)) 13279ba69294SHugh Dickins break; 132831dbd01fSIzik Eidus *page = follow_page(vma, ksm_scan.address, FOLL_GET); 132921ae5b01SAndrea Arcangeli if (IS_ERR_OR_NULL(*page)) { 133021ae5b01SAndrea Arcangeli ksm_scan.address += PAGE_SIZE; 133121ae5b01SAndrea Arcangeli cond_resched(); 133221ae5b01SAndrea Arcangeli continue; 133321ae5b01SAndrea Arcangeli } 133429ad768cSAndrea Arcangeli if (PageAnon(*page) || 133529ad768cSAndrea Arcangeli page_trans_compound_anon(*page)) { 133631dbd01fSIzik Eidus flush_anon_page(vma, *page, ksm_scan.address); 133731dbd01fSIzik Eidus flush_dcache_page(*page); 133831dbd01fSIzik Eidus rmap_item = get_next_rmap_item(slot, 13396514d511SHugh Dickins ksm_scan.rmap_list, ksm_scan.address); 134031dbd01fSIzik Eidus if (rmap_item) { 13416514d511SHugh Dickins ksm_scan.rmap_list = 13426514d511SHugh Dickins &rmap_item->rmap_list; 134331dbd01fSIzik Eidus ksm_scan.address += PAGE_SIZE; 134431dbd01fSIzik Eidus } else 134531dbd01fSIzik Eidus put_page(*page); 134631dbd01fSIzik Eidus up_read(&mm->mmap_sem); 134731dbd01fSIzik Eidus return rmap_item; 134831dbd01fSIzik Eidus } 134931dbd01fSIzik Eidus put_page(*page); 135031dbd01fSIzik Eidus ksm_scan.address += PAGE_SIZE; 135131dbd01fSIzik Eidus cond_resched(); 135231dbd01fSIzik Eidus } 135331dbd01fSIzik Eidus } 135431dbd01fSIzik Eidus 13559ba69294SHugh Dickins if (ksm_test_exit(mm)) { 13569ba69294SHugh Dickins ksm_scan.address = 0; 13576514d511SHugh Dickins ksm_scan.rmap_list = &slot->rmap_list; 13589ba69294SHugh Dickins } 135931dbd01fSIzik Eidus /* 136031dbd01fSIzik Eidus * Nuke all the rmap_items that are above this current rmap: 136131dbd01fSIzik Eidus * because there were no VM_MERGEABLE vmas with such addresses. 136231dbd01fSIzik Eidus */ 13636514d511SHugh Dickins remove_trailing_rmap_items(slot, ksm_scan.rmap_list); 136431dbd01fSIzik Eidus 136531dbd01fSIzik Eidus spin_lock(&ksm_mmlist_lock); 1366cd551f97SHugh Dickins ksm_scan.mm_slot = list_entry(slot->mm_list.next, 1367cd551f97SHugh Dickins struct mm_slot, mm_list); 1368cd551f97SHugh Dickins if (ksm_scan.address == 0) { 1369cd551f97SHugh Dickins /* 1370cd551f97SHugh Dickins * We've completed a full scan of all vmas, holding mmap_sem 1371cd551f97SHugh Dickins * throughout, and found no VM_MERGEABLE: so do the same as 1372cd551f97SHugh Dickins * __ksm_exit does to remove this mm from all our lists now. 13739ba69294SHugh Dickins * This applies either when cleaning up after __ksm_exit 13749ba69294SHugh Dickins * (but beware: we can reach here even before __ksm_exit), 13759ba69294SHugh Dickins * or when all VM_MERGEABLE areas have been unmapped (and 13769ba69294SHugh Dickins * mmap_sem then protects against race with MADV_MERGEABLE). 1377cd551f97SHugh Dickins */ 1378cd551f97SHugh Dickins hlist_del(&slot->link); 1379cd551f97SHugh Dickins list_del(&slot->mm_list); 13809ba69294SHugh Dickins spin_unlock(&ksm_mmlist_lock); 13819ba69294SHugh Dickins 1382cd551f97SHugh Dickins free_mm_slot(slot); 1383cd551f97SHugh Dickins clear_bit(MMF_VM_MERGEABLE, &mm->flags); 13849ba69294SHugh Dickins up_read(&mm->mmap_sem); 13859ba69294SHugh Dickins mmdrop(mm); 13869ba69294SHugh Dickins } else { 138731dbd01fSIzik Eidus spin_unlock(&ksm_mmlist_lock); 1388cd551f97SHugh Dickins up_read(&mm->mmap_sem); 13899ba69294SHugh Dickins } 139031dbd01fSIzik Eidus 139131dbd01fSIzik Eidus /* Repeat until we've completed scanning the whole list */ 1392cd551f97SHugh Dickins slot = ksm_scan.mm_slot; 139331dbd01fSIzik Eidus if (slot != &ksm_mm_head) 139431dbd01fSIzik Eidus goto next_mm; 139531dbd01fSIzik Eidus 139631dbd01fSIzik Eidus ksm_scan.seqnr++; 139731dbd01fSIzik Eidus return NULL; 139831dbd01fSIzik Eidus } 139931dbd01fSIzik Eidus 140031dbd01fSIzik Eidus /** 140131dbd01fSIzik Eidus * ksm_do_scan - the ksm scanner main worker function. 140231dbd01fSIzik Eidus * @scan_npages - number of pages we want to scan before we return. 140331dbd01fSIzik Eidus */ 140431dbd01fSIzik Eidus static void ksm_do_scan(unsigned int scan_npages) 140531dbd01fSIzik Eidus { 140631dbd01fSIzik Eidus struct rmap_item *rmap_item; 140722eccdd7SDan Carpenter struct page *uninitialized_var(page); 140831dbd01fSIzik Eidus 1409878aee7dSAndrea Arcangeli while (scan_npages-- && likely(!freezing(current))) { 141031dbd01fSIzik Eidus cond_resched(); 141131dbd01fSIzik Eidus rmap_item = scan_get_next_rmap_item(&page); 141231dbd01fSIzik Eidus if (!rmap_item) 141331dbd01fSIzik Eidus return; 141431dbd01fSIzik Eidus if (!PageKsm(page) || !in_stable_tree(rmap_item)) 141531dbd01fSIzik Eidus cmp_and_merge_page(page, rmap_item); 141631dbd01fSIzik Eidus put_page(page); 141731dbd01fSIzik Eidus } 141831dbd01fSIzik Eidus } 141931dbd01fSIzik Eidus 14206e158384SHugh Dickins static int ksmd_should_run(void) 14216e158384SHugh Dickins { 14226e158384SHugh Dickins return (ksm_run & KSM_RUN_MERGE) && !list_empty(&ksm_mm_head.mm_list); 14236e158384SHugh Dickins } 14246e158384SHugh Dickins 142531dbd01fSIzik Eidus static int ksm_scan_thread(void *nothing) 142631dbd01fSIzik Eidus { 1427878aee7dSAndrea Arcangeli set_freezable(); 1428339aa624SIzik Eidus set_user_nice(current, 5); 142931dbd01fSIzik Eidus 143031dbd01fSIzik Eidus while (!kthread_should_stop()) { 143131dbd01fSIzik Eidus mutex_lock(&ksm_thread_mutex); 14326e158384SHugh Dickins if (ksmd_should_run()) 143331dbd01fSIzik Eidus ksm_do_scan(ksm_thread_pages_to_scan); 143431dbd01fSIzik Eidus mutex_unlock(&ksm_thread_mutex); 14356e158384SHugh Dickins 1436878aee7dSAndrea Arcangeli try_to_freeze(); 1437878aee7dSAndrea Arcangeli 14386e158384SHugh Dickins if (ksmd_should_run()) { 143931dbd01fSIzik Eidus schedule_timeout_interruptible( 144031dbd01fSIzik Eidus msecs_to_jiffies(ksm_thread_sleep_millisecs)); 144131dbd01fSIzik Eidus } else { 1442878aee7dSAndrea Arcangeli wait_event_freezable(ksm_thread_wait, 14436e158384SHugh Dickins ksmd_should_run() || kthread_should_stop()); 144431dbd01fSIzik Eidus } 144531dbd01fSIzik Eidus } 144631dbd01fSIzik Eidus return 0; 144731dbd01fSIzik Eidus } 144831dbd01fSIzik Eidus 1449f8af4da3SHugh Dickins int ksm_madvise(struct vm_area_struct *vma, unsigned long start, 1450f8af4da3SHugh Dickins unsigned long end, int advice, unsigned long *vm_flags) 1451f8af4da3SHugh Dickins { 1452f8af4da3SHugh Dickins struct mm_struct *mm = vma->vm_mm; 1453d952b791SHugh Dickins int err; 1454f8af4da3SHugh Dickins 1455f8af4da3SHugh Dickins switch (advice) { 1456f8af4da3SHugh Dickins case MADV_MERGEABLE: 1457f8af4da3SHugh Dickins /* 1458f8af4da3SHugh Dickins * Be somewhat over-protective for now! 1459f8af4da3SHugh Dickins */ 1460f8af4da3SHugh Dickins if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE | 1461f8af4da3SHugh Dickins VM_PFNMAP | VM_IO | VM_DONTEXPAND | 1462f8af4da3SHugh Dickins VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE | 14635ad64688SHugh Dickins VM_NONLINEAR | VM_MIXEDMAP | VM_SAO)) 1464f8af4da3SHugh Dickins return 0; /* just ignore the advice */ 1465f8af4da3SHugh Dickins 1466d952b791SHugh Dickins if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) { 1467d952b791SHugh Dickins err = __ksm_enter(mm); 1468d952b791SHugh Dickins if (err) 1469d952b791SHugh Dickins return err; 1470d952b791SHugh Dickins } 1471f8af4da3SHugh Dickins 1472f8af4da3SHugh Dickins *vm_flags |= VM_MERGEABLE; 1473f8af4da3SHugh Dickins break; 1474f8af4da3SHugh Dickins 1475f8af4da3SHugh Dickins case MADV_UNMERGEABLE: 1476f8af4da3SHugh Dickins if (!(*vm_flags & VM_MERGEABLE)) 1477f8af4da3SHugh Dickins return 0; /* just ignore the advice */ 1478f8af4da3SHugh Dickins 1479d952b791SHugh Dickins if (vma->anon_vma) { 1480d952b791SHugh Dickins err = unmerge_ksm_pages(vma, start, end); 1481d952b791SHugh Dickins if (err) 1482d952b791SHugh Dickins return err; 1483d952b791SHugh Dickins } 1484f8af4da3SHugh Dickins 1485f8af4da3SHugh Dickins *vm_flags &= ~VM_MERGEABLE; 1486f8af4da3SHugh Dickins break; 1487f8af4da3SHugh Dickins } 1488f8af4da3SHugh Dickins 1489f8af4da3SHugh Dickins return 0; 1490f8af4da3SHugh Dickins } 1491f8af4da3SHugh Dickins 1492f8af4da3SHugh Dickins int __ksm_enter(struct mm_struct *mm) 1493f8af4da3SHugh Dickins { 14946e158384SHugh Dickins struct mm_slot *mm_slot; 14956e158384SHugh Dickins int needs_wakeup; 14966e158384SHugh Dickins 14976e158384SHugh Dickins mm_slot = alloc_mm_slot(); 149831dbd01fSIzik Eidus if (!mm_slot) 149931dbd01fSIzik Eidus return -ENOMEM; 150031dbd01fSIzik Eidus 15016e158384SHugh Dickins /* Check ksm_run too? Would need tighter locking */ 15026e158384SHugh Dickins needs_wakeup = list_empty(&ksm_mm_head.mm_list); 15036e158384SHugh Dickins 150431dbd01fSIzik Eidus spin_lock(&ksm_mmlist_lock); 150531dbd01fSIzik Eidus insert_to_mm_slots_hash(mm, mm_slot); 150631dbd01fSIzik Eidus /* 150731dbd01fSIzik Eidus * Insert just behind the scanning cursor, to let the area settle 150831dbd01fSIzik Eidus * down a little; when fork is followed by immediate exec, we don't 150931dbd01fSIzik Eidus * want ksmd to waste time setting up and tearing down an rmap_list. 151031dbd01fSIzik Eidus */ 151131dbd01fSIzik Eidus list_add_tail(&mm_slot->mm_list, &ksm_scan.mm_slot->mm_list); 151231dbd01fSIzik Eidus spin_unlock(&ksm_mmlist_lock); 151331dbd01fSIzik Eidus 1514f8af4da3SHugh Dickins set_bit(MMF_VM_MERGEABLE, &mm->flags); 15159ba69294SHugh Dickins atomic_inc(&mm->mm_count); 15166e158384SHugh Dickins 15176e158384SHugh Dickins if (needs_wakeup) 15186e158384SHugh Dickins wake_up_interruptible(&ksm_thread_wait); 15196e158384SHugh Dickins 1520f8af4da3SHugh Dickins return 0; 1521f8af4da3SHugh Dickins } 1522f8af4da3SHugh Dickins 15231c2fb7a4SAndrea Arcangeli void __ksm_exit(struct mm_struct *mm) 1524f8af4da3SHugh Dickins { 1525cd551f97SHugh Dickins struct mm_slot *mm_slot; 15269ba69294SHugh Dickins int easy_to_free = 0; 1527cd551f97SHugh Dickins 152831dbd01fSIzik Eidus /* 15299ba69294SHugh Dickins * This process is exiting: if it's straightforward (as is the 15309ba69294SHugh Dickins * case when ksmd was never running), free mm_slot immediately. 15319ba69294SHugh Dickins * But if it's at the cursor or has rmap_items linked to it, use 15329ba69294SHugh Dickins * mmap_sem to synchronize with any break_cows before pagetables 15339ba69294SHugh Dickins * are freed, and leave the mm_slot on the list for ksmd to free. 15349ba69294SHugh Dickins * Beware: ksm may already have noticed it exiting and freed the slot. 153531dbd01fSIzik Eidus */ 15369ba69294SHugh Dickins 1537cd551f97SHugh Dickins spin_lock(&ksm_mmlist_lock); 1538cd551f97SHugh Dickins mm_slot = get_mm_slot(mm); 15399ba69294SHugh Dickins if (mm_slot && ksm_scan.mm_slot != mm_slot) { 15406514d511SHugh Dickins if (!mm_slot->rmap_list) { 1541cd551f97SHugh Dickins hlist_del(&mm_slot->link); 1542cd551f97SHugh Dickins list_del(&mm_slot->mm_list); 15439ba69294SHugh Dickins easy_to_free = 1; 15449ba69294SHugh Dickins } else { 15459ba69294SHugh Dickins list_move(&mm_slot->mm_list, 15469ba69294SHugh Dickins &ksm_scan.mm_slot->mm_list); 15479ba69294SHugh Dickins } 15489ba69294SHugh Dickins } 1549cd551f97SHugh Dickins spin_unlock(&ksm_mmlist_lock); 1550cd551f97SHugh Dickins 15519ba69294SHugh Dickins if (easy_to_free) { 1552cd551f97SHugh Dickins free_mm_slot(mm_slot); 1553cd551f97SHugh Dickins clear_bit(MMF_VM_MERGEABLE, &mm->flags); 15549ba69294SHugh Dickins mmdrop(mm); 15559ba69294SHugh Dickins } else if (mm_slot) { 15569ba69294SHugh Dickins down_write(&mm->mmap_sem); 15579ba69294SHugh Dickins up_write(&mm->mmap_sem); 15589ba69294SHugh Dickins } 1559f8af4da3SHugh Dickins } 156031dbd01fSIzik Eidus 15615ad64688SHugh Dickins struct page *ksm_does_need_to_copy(struct page *page, 15625ad64688SHugh Dickins struct vm_area_struct *vma, unsigned long address) 15635ad64688SHugh Dickins { 15645ad64688SHugh Dickins struct page *new_page; 15655ad64688SHugh Dickins 15665ad64688SHugh Dickins new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); 15675ad64688SHugh Dickins if (new_page) { 15685ad64688SHugh Dickins copy_user_highpage(new_page, page, address, vma); 15695ad64688SHugh Dickins 15705ad64688SHugh Dickins SetPageDirty(new_page); 15715ad64688SHugh Dickins __SetPageUptodate(new_page); 15725ad64688SHugh Dickins SetPageSwapBacked(new_page); 15735ad64688SHugh Dickins __set_page_locked(new_page); 15745ad64688SHugh Dickins 15755ad64688SHugh Dickins if (page_evictable(new_page, vma)) 15765ad64688SHugh Dickins lru_cache_add_lru(new_page, LRU_ACTIVE_ANON); 15775ad64688SHugh Dickins else 15785ad64688SHugh Dickins add_page_to_unevictable_list(new_page); 15795ad64688SHugh Dickins } 15805ad64688SHugh Dickins 15815ad64688SHugh Dickins return new_page; 15825ad64688SHugh Dickins } 15835ad64688SHugh Dickins 15845ad64688SHugh Dickins int page_referenced_ksm(struct page *page, struct mem_cgroup *memcg, 15855ad64688SHugh Dickins unsigned long *vm_flags) 15865ad64688SHugh Dickins { 15875ad64688SHugh Dickins struct stable_node *stable_node; 15885ad64688SHugh Dickins struct rmap_item *rmap_item; 15895ad64688SHugh Dickins struct hlist_node *hlist; 15905ad64688SHugh Dickins unsigned int mapcount = page_mapcount(page); 15915ad64688SHugh Dickins int referenced = 0; 1592db114b83SHugh Dickins int search_new_forks = 0; 15935ad64688SHugh Dickins 15945ad64688SHugh Dickins VM_BUG_ON(!PageKsm(page)); 15955ad64688SHugh Dickins VM_BUG_ON(!PageLocked(page)); 15965ad64688SHugh Dickins 15975ad64688SHugh Dickins stable_node = page_stable_node(page); 15985ad64688SHugh Dickins if (!stable_node) 15995ad64688SHugh Dickins return 0; 1600db114b83SHugh Dickins again: 16015ad64688SHugh Dickins hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { 1602db114b83SHugh Dickins struct anon_vma *anon_vma = rmap_item->anon_vma; 16035beb4930SRik van Riel struct anon_vma_chain *vmac; 1604db114b83SHugh Dickins struct vm_area_struct *vma; 1605db114b83SHugh Dickins 1606cba48b98SRik van Riel anon_vma_lock(anon_vma); 16075beb4930SRik van Riel list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) { 16085beb4930SRik van Riel vma = vmac->vma; 1609db114b83SHugh Dickins if (rmap_item->address < vma->vm_start || 1610db114b83SHugh Dickins rmap_item->address >= vma->vm_end) 1611db114b83SHugh Dickins continue; 1612db114b83SHugh Dickins /* 1613db114b83SHugh Dickins * Initially we examine only the vma which covers this 1614db114b83SHugh Dickins * rmap_item; but later, if there is still work to do, 1615db114b83SHugh Dickins * we examine covering vmas in other mms: in case they 1616db114b83SHugh Dickins * were forked from the original since ksmd passed. 1617db114b83SHugh Dickins */ 1618db114b83SHugh Dickins if ((rmap_item->mm == vma->vm_mm) == search_new_forks) 16195ad64688SHugh Dickins continue; 16205ad64688SHugh Dickins 1621db114b83SHugh Dickins if (memcg && !mm_match_cgroup(vma->vm_mm, memcg)) 1622db114b83SHugh Dickins continue; 16235ad64688SHugh Dickins 16245ad64688SHugh Dickins referenced += page_referenced_one(page, vma, 16255ad64688SHugh Dickins rmap_item->address, &mapcount, vm_flags); 1626db114b83SHugh Dickins if (!search_new_forks || !mapcount) 1627db114b83SHugh Dickins break; 1628db114b83SHugh Dickins } 1629cba48b98SRik van Riel anon_vma_unlock(anon_vma); 16305ad64688SHugh Dickins if (!mapcount) 16315ad64688SHugh Dickins goto out; 16325ad64688SHugh Dickins } 1633db114b83SHugh Dickins if (!search_new_forks++) 1634db114b83SHugh Dickins goto again; 16355ad64688SHugh Dickins out: 16365ad64688SHugh Dickins return referenced; 16375ad64688SHugh Dickins } 16385ad64688SHugh Dickins 16395ad64688SHugh Dickins int try_to_unmap_ksm(struct page *page, enum ttu_flags flags) 16405ad64688SHugh Dickins { 16415ad64688SHugh Dickins struct stable_node *stable_node; 16425ad64688SHugh Dickins struct hlist_node *hlist; 16435ad64688SHugh Dickins struct rmap_item *rmap_item; 16445ad64688SHugh Dickins int ret = SWAP_AGAIN; 1645db114b83SHugh Dickins int search_new_forks = 0; 16465ad64688SHugh Dickins 16475ad64688SHugh Dickins VM_BUG_ON(!PageKsm(page)); 16485ad64688SHugh Dickins VM_BUG_ON(!PageLocked(page)); 16495ad64688SHugh Dickins 16505ad64688SHugh Dickins stable_node = page_stable_node(page); 16515ad64688SHugh Dickins if (!stable_node) 16525ad64688SHugh Dickins return SWAP_FAIL; 1653db114b83SHugh Dickins again: 16545ad64688SHugh Dickins hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { 1655db114b83SHugh Dickins struct anon_vma *anon_vma = rmap_item->anon_vma; 16565beb4930SRik van Riel struct anon_vma_chain *vmac; 1657db114b83SHugh Dickins struct vm_area_struct *vma; 16585ad64688SHugh Dickins 1659cba48b98SRik van Riel anon_vma_lock(anon_vma); 16605beb4930SRik van Riel list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) { 16615beb4930SRik van Riel vma = vmac->vma; 1662db114b83SHugh Dickins if (rmap_item->address < vma->vm_start || 1663db114b83SHugh Dickins rmap_item->address >= vma->vm_end) 1664db114b83SHugh Dickins continue; 1665db114b83SHugh Dickins /* 1666db114b83SHugh Dickins * Initially we examine only the vma which covers this 1667db114b83SHugh Dickins * rmap_item; but later, if there is still work to do, 1668db114b83SHugh Dickins * we examine covering vmas in other mms: in case they 1669db114b83SHugh Dickins * were forked from the original since ksmd passed. 1670db114b83SHugh Dickins */ 1671db114b83SHugh Dickins if ((rmap_item->mm == vma->vm_mm) == search_new_forks) 1672db114b83SHugh Dickins continue; 1673db114b83SHugh Dickins 1674db114b83SHugh Dickins ret = try_to_unmap_one(page, vma, 1675db114b83SHugh Dickins rmap_item->address, flags); 1676db114b83SHugh Dickins if (ret != SWAP_AGAIN || !page_mapped(page)) { 1677cba48b98SRik van Riel anon_vma_unlock(anon_vma); 16785ad64688SHugh Dickins goto out; 16795ad64688SHugh Dickins } 1680db114b83SHugh Dickins } 1681cba48b98SRik van Riel anon_vma_unlock(anon_vma); 1682db114b83SHugh Dickins } 1683db114b83SHugh Dickins if (!search_new_forks++) 1684db114b83SHugh Dickins goto again; 16855ad64688SHugh Dickins out: 16865ad64688SHugh Dickins return ret; 16875ad64688SHugh Dickins } 16885ad64688SHugh Dickins 1689e9995ef9SHugh Dickins #ifdef CONFIG_MIGRATION 1690e9995ef9SHugh Dickins int rmap_walk_ksm(struct page *page, int (*rmap_one)(struct page *, 1691e9995ef9SHugh Dickins struct vm_area_struct *, unsigned long, void *), void *arg) 1692e9995ef9SHugh Dickins { 1693e9995ef9SHugh Dickins struct stable_node *stable_node; 1694e9995ef9SHugh Dickins struct hlist_node *hlist; 1695e9995ef9SHugh Dickins struct rmap_item *rmap_item; 1696e9995ef9SHugh Dickins int ret = SWAP_AGAIN; 1697e9995ef9SHugh Dickins int search_new_forks = 0; 1698e9995ef9SHugh Dickins 1699e9995ef9SHugh Dickins VM_BUG_ON(!PageKsm(page)); 1700e9995ef9SHugh Dickins VM_BUG_ON(!PageLocked(page)); 1701e9995ef9SHugh Dickins 1702e9995ef9SHugh Dickins stable_node = page_stable_node(page); 1703e9995ef9SHugh Dickins if (!stable_node) 1704e9995ef9SHugh Dickins return ret; 1705e9995ef9SHugh Dickins again: 1706e9995ef9SHugh Dickins hlist_for_each_entry(rmap_item, hlist, &stable_node->hlist, hlist) { 1707e9995ef9SHugh Dickins struct anon_vma *anon_vma = rmap_item->anon_vma; 17085beb4930SRik van Riel struct anon_vma_chain *vmac; 1709e9995ef9SHugh Dickins struct vm_area_struct *vma; 1710e9995ef9SHugh Dickins 1711cba48b98SRik van Riel anon_vma_lock(anon_vma); 17125beb4930SRik van Riel list_for_each_entry(vmac, &anon_vma->head, same_anon_vma) { 17135beb4930SRik van Riel vma = vmac->vma; 1714e9995ef9SHugh Dickins if (rmap_item->address < vma->vm_start || 1715e9995ef9SHugh Dickins rmap_item->address >= vma->vm_end) 1716e9995ef9SHugh Dickins continue; 1717e9995ef9SHugh Dickins /* 1718e9995ef9SHugh Dickins * Initially we examine only the vma which covers this 1719e9995ef9SHugh Dickins * rmap_item; but later, if there is still work to do, 1720e9995ef9SHugh Dickins * we examine covering vmas in other mms: in case they 1721e9995ef9SHugh Dickins * were forked from the original since ksmd passed. 1722e9995ef9SHugh Dickins */ 1723e9995ef9SHugh Dickins if ((rmap_item->mm == vma->vm_mm) == search_new_forks) 1724e9995ef9SHugh Dickins continue; 1725e9995ef9SHugh Dickins 1726e9995ef9SHugh Dickins ret = rmap_one(page, vma, rmap_item->address, arg); 1727e9995ef9SHugh Dickins if (ret != SWAP_AGAIN) { 1728cba48b98SRik van Riel anon_vma_unlock(anon_vma); 1729e9995ef9SHugh Dickins goto out; 1730e9995ef9SHugh Dickins } 1731e9995ef9SHugh Dickins } 1732cba48b98SRik van Riel anon_vma_unlock(anon_vma); 1733e9995ef9SHugh Dickins } 1734e9995ef9SHugh Dickins if (!search_new_forks++) 1735e9995ef9SHugh Dickins goto again; 1736e9995ef9SHugh Dickins out: 1737e9995ef9SHugh Dickins return ret; 1738e9995ef9SHugh Dickins } 1739e9995ef9SHugh Dickins 1740e9995ef9SHugh Dickins void ksm_migrate_page(struct page *newpage, struct page *oldpage) 1741e9995ef9SHugh Dickins { 1742e9995ef9SHugh Dickins struct stable_node *stable_node; 1743e9995ef9SHugh Dickins 1744e9995ef9SHugh Dickins VM_BUG_ON(!PageLocked(oldpage)); 1745e9995ef9SHugh Dickins VM_BUG_ON(!PageLocked(newpage)); 1746e9995ef9SHugh Dickins VM_BUG_ON(newpage->mapping != oldpage->mapping); 1747e9995ef9SHugh Dickins 1748e9995ef9SHugh Dickins stable_node = page_stable_node(newpage); 1749e9995ef9SHugh Dickins if (stable_node) { 175062b61f61SHugh Dickins VM_BUG_ON(stable_node->kpfn != page_to_pfn(oldpage)); 175162b61f61SHugh Dickins stable_node->kpfn = page_to_pfn(newpage); 1752e9995ef9SHugh Dickins } 1753e9995ef9SHugh Dickins } 1754e9995ef9SHugh Dickins #endif /* CONFIG_MIGRATION */ 1755e9995ef9SHugh Dickins 175662b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE 175762b61f61SHugh Dickins static struct stable_node *ksm_check_stable_tree(unsigned long start_pfn, 175862b61f61SHugh Dickins unsigned long end_pfn) 175962b61f61SHugh Dickins { 176062b61f61SHugh Dickins struct rb_node *node; 176162b61f61SHugh Dickins 176262b61f61SHugh Dickins for (node = rb_first(&root_stable_tree); node; node = rb_next(node)) { 176362b61f61SHugh Dickins struct stable_node *stable_node; 176462b61f61SHugh Dickins 176562b61f61SHugh Dickins stable_node = rb_entry(node, struct stable_node, node); 176662b61f61SHugh Dickins if (stable_node->kpfn >= start_pfn && 176762b61f61SHugh Dickins stable_node->kpfn < end_pfn) 176862b61f61SHugh Dickins return stable_node; 176962b61f61SHugh Dickins } 177062b61f61SHugh Dickins return NULL; 177162b61f61SHugh Dickins } 177262b61f61SHugh Dickins 177362b61f61SHugh Dickins static int ksm_memory_callback(struct notifier_block *self, 177462b61f61SHugh Dickins unsigned long action, void *arg) 177562b61f61SHugh Dickins { 177662b61f61SHugh Dickins struct memory_notify *mn = arg; 177762b61f61SHugh Dickins struct stable_node *stable_node; 177862b61f61SHugh Dickins 177962b61f61SHugh Dickins switch (action) { 178062b61f61SHugh Dickins case MEM_GOING_OFFLINE: 178162b61f61SHugh Dickins /* 178262b61f61SHugh Dickins * Keep it very simple for now: just lock out ksmd and 178362b61f61SHugh Dickins * MADV_UNMERGEABLE while any memory is going offline. 1784a0b0f58cSKOSAKI Motohiro * mutex_lock_nested() is necessary because lockdep was alarmed 1785a0b0f58cSKOSAKI Motohiro * that here we take ksm_thread_mutex inside notifier chain 1786a0b0f58cSKOSAKI Motohiro * mutex, and later take notifier chain mutex inside 1787a0b0f58cSKOSAKI Motohiro * ksm_thread_mutex to unlock it. But that's safe because both 1788a0b0f58cSKOSAKI Motohiro * are inside mem_hotplug_mutex. 178962b61f61SHugh Dickins */ 1790a0b0f58cSKOSAKI Motohiro mutex_lock_nested(&ksm_thread_mutex, SINGLE_DEPTH_NESTING); 179162b61f61SHugh Dickins break; 179262b61f61SHugh Dickins 179362b61f61SHugh Dickins case MEM_OFFLINE: 179462b61f61SHugh Dickins /* 179562b61f61SHugh Dickins * Most of the work is done by page migration; but there might 179662b61f61SHugh Dickins * be a few stable_nodes left over, still pointing to struct 179762b61f61SHugh Dickins * pages which have been offlined: prune those from the tree. 179862b61f61SHugh Dickins */ 179962b61f61SHugh Dickins while ((stable_node = ksm_check_stable_tree(mn->start_pfn, 180062b61f61SHugh Dickins mn->start_pfn + mn->nr_pages)) != NULL) 180162b61f61SHugh Dickins remove_node_from_stable_tree(stable_node); 180262b61f61SHugh Dickins /* fallthrough */ 180362b61f61SHugh Dickins 180462b61f61SHugh Dickins case MEM_CANCEL_OFFLINE: 180562b61f61SHugh Dickins mutex_unlock(&ksm_thread_mutex); 180662b61f61SHugh Dickins break; 180762b61f61SHugh Dickins } 180862b61f61SHugh Dickins return NOTIFY_OK; 180962b61f61SHugh Dickins } 181062b61f61SHugh Dickins #endif /* CONFIG_MEMORY_HOTREMOVE */ 181162b61f61SHugh Dickins 18122ffd8679SHugh Dickins #ifdef CONFIG_SYSFS 18132ffd8679SHugh Dickins /* 18142ffd8679SHugh Dickins * This all compiles without CONFIG_SYSFS, but is a waste of space. 18152ffd8679SHugh Dickins */ 18162ffd8679SHugh Dickins 181731dbd01fSIzik Eidus #define KSM_ATTR_RO(_name) \ 181831dbd01fSIzik Eidus static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 181931dbd01fSIzik Eidus #define KSM_ATTR(_name) \ 182031dbd01fSIzik Eidus static struct kobj_attribute _name##_attr = \ 182131dbd01fSIzik Eidus __ATTR(_name, 0644, _name##_show, _name##_store) 182231dbd01fSIzik Eidus 182331dbd01fSIzik Eidus static ssize_t sleep_millisecs_show(struct kobject *kobj, 182431dbd01fSIzik Eidus struct kobj_attribute *attr, char *buf) 182531dbd01fSIzik Eidus { 182631dbd01fSIzik Eidus return sprintf(buf, "%u\n", ksm_thread_sleep_millisecs); 182731dbd01fSIzik Eidus } 182831dbd01fSIzik Eidus 182931dbd01fSIzik Eidus static ssize_t sleep_millisecs_store(struct kobject *kobj, 183031dbd01fSIzik Eidus struct kobj_attribute *attr, 183131dbd01fSIzik Eidus const char *buf, size_t count) 183231dbd01fSIzik Eidus { 183331dbd01fSIzik Eidus unsigned long msecs; 183431dbd01fSIzik Eidus int err; 183531dbd01fSIzik Eidus 183631dbd01fSIzik Eidus err = strict_strtoul(buf, 10, &msecs); 183731dbd01fSIzik Eidus if (err || msecs > UINT_MAX) 183831dbd01fSIzik Eidus return -EINVAL; 183931dbd01fSIzik Eidus 184031dbd01fSIzik Eidus ksm_thread_sleep_millisecs = msecs; 184131dbd01fSIzik Eidus 184231dbd01fSIzik Eidus return count; 184331dbd01fSIzik Eidus } 184431dbd01fSIzik Eidus KSM_ATTR(sleep_millisecs); 184531dbd01fSIzik Eidus 184631dbd01fSIzik Eidus static ssize_t pages_to_scan_show(struct kobject *kobj, 184731dbd01fSIzik Eidus struct kobj_attribute *attr, char *buf) 184831dbd01fSIzik Eidus { 184931dbd01fSIzik Eidus return sprintf(buf, "%u\n", ksm_thread_pages_to_scan); 185031dbd01fSIzik Eidus } 185131dbd01fSIzik Eidus 185231dbd01fSIzik Eidus static ssize_t pages_to_scan_store(struct kobject *kobj, 185331dbd01fSIzik Eidus struct kobj_attribute *attr, 185431dbd01fSIzik Eidus const char *buf, size_t count) 185531dbd01fSIzik Eidus { 185631dbd01fSIzik Eidus int err; 185731dbd01fSIzik Eidus unsigned long nr_pages; 185831dbd01fSIzik Eidus 185931dbd01fSIzik Eidus err = strict_strtoul(buf, 10, &nr_pages); 186031dbd01fSIzik Eidus if (err || nr_pages > UINT_MAX) 186131dbd01fSIzik Eidus return -EINVAL; 186231dbd01fSIzik Eidus 186331dbd01fSIzik Eidus ksm_thread_pages_to_scan = nr_pages; 186431dbd01fSIzik Eidus 186531dbd01fSIzik Eidus return count; 186631dbd01fSIzik Eidus } 186731dbd01fSIzik Eidus KSM_ATTR(pages_to_scan); 186831dbd01fSIzik Eidus 186931dbd01fSIzik Eidus static ssize_t run_show(struct kobject *kobj, struct kobj_attribute *attr, 187031dbd01fSIzik Eidus char *buf) 187131dbd01fSIzik Eidus { 187231dbd01fSIzik Eidus return sprintf(buf, "%u\n", ksm_run); 187331dbd01fSIzik Eidus } 187431dbd01fSIzik Eidus 187531dbd01fSIzik Eidus static ssize_t run_store(struct kobject *kobj, struct kobj_attribute *attr, 187631dbd01fSIzik Eidus const char *buf, size_t count) 187731dbd01fSIzik Eidus { 187831dbd01fSIzik Eidus int err; 187931dbd01fSIzik Eidus unsigned long flags; 188031dbd01fSIzik Eidus 188131dbd01fSIzik Eidus err = strict_strtoul(buf, 10, &flags); 188231dbd01fSIzik Eidus if (err || flags > UINT_MAX) 188331dbd01fSIzik Eidus return -EINVAL; 188431dbd01fSIzik Eidus if (flags > KSM_RUN_UNMERGE) 188531dbd01fSIzik Eidus return -EINVAL; 188631dbd01fSIzik Eidus 188731dbd01fSIzik Eidus /* 188831dbd01fSIzik Eidus * KSM_RUN_MERGE sets ksmd running, and 0 stops it running. 188931dbd01fSIzik Eidus * KSM_RUN_UNMERGE stops it running and unmerges all rmap_items, 1890d0f209f6SHugh Dickins * breaking COW to free the pages_shared (but leaves mm_slots 1891d0f209f6SHugh Dickins * on the list for when ksmd may be set running again). 189231dbd01fSIzik Eidus */ 189331dbd01fSIzik Eidus 189431dbd01fSIzik Eidus mutex_lock(&ksm_thread_mutex); 189531dbd01fSIzik Eidus if (ksm_run != flags) { 189631dbd01fSIzik Eidus ksm_run = flags; 1897d952b791SHugh Dickins if (flags & KSM_RUN_UNMERGE) { 1898*72788c38SDavid Rientjes int oom_score_adj; 1899*72788c38SDavid Rientjes 1900*72788c38SDavid Rientjes oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX); 1901d952b791SHugh Dickins err = unmerge_and_remove_all_rmap_items(); 1902*72788c38SDavid Rientjes test_set_oom_score_adj(oom_score_adj); 1903d952b791SHugh Dickins if (err) { 1904d952b791SHugh Dickins ksm_run = KSM_RUN_STOP; 1905d952b791SHugh Dickins count = err; 1906d952b791SHugh Dickins } 1907d952b791SHugh Dickins } 190831dbd01fSIzik Eidus } 190931dbd01fSIzik Eidus mutex_unlock(&ksm_thread_mutex); 191031dbd01fSIzik Eidus 191131dbd01fSIzik Eidus if (flags & KSM_RUN_MERGE) 191231dbd01fSIzik Eidus wake_up_interruptible(&ksm_thread_wait); 191331dbd01fSIzik Eidus 191431dbd01fSIzik Eidus return count; 191531dbd01fSIzik Eidus } 191631dbd01fSIzik Eidus KSM_ATTR(run); 191731dbd01fSIzik Eidus 1918b4028260SHugh Dickins static ssize_t pages_shared_show(struct kobject *kobj, 1919b4028260SHugh Dickins struct kobj_attribute *attr, char *buf) 1920b4028260SHugh Dickins { 1921b4028260SHugh Dickins return sprintf(buf, "%lu\n", ksm_pages_shared); 1922b4028260SHugh Dickins } 1923b4028260SHugh Dickins KSM_ATTR_RO(pages_shared); 1924b4028260SHugh Dickins 1925b4028260SHugh Dickins static ssize_t pages_sharing_show(struct kobject *kobj, 1926b4028260SHugh Dickins struct kobj_attribute *attr, char *buf) 1927b4028260SHugh Dickins { 1928e178dfdeSHugh Dickins return sprintf(buf, "%lu\n", ksm_pages_sharing); 1929b4028260SHugh Dickins } 1930b4028260SHugh Dickins KSM_ATTR_RO(pages_sharing); 1931b4028260SHugh Dickins 1932473b0ce4SHugh Dickins static ssize_t pages_unshared_show(struct kobject *kobj, 1933473b0ce4SHugh Dickins struct kobj_attribute *attr, char *buf) 1934473b0ce4SHugh Dickins { 1935473b0ce4SHugh Dickins return sprintf(buf, "%lu\n", ksm_pages_unshared); 1936473b0ce4SHugh Dickins } 1937473b0ce4SHugh Dickins KSM_ATTR_RO(pages_unshared); 1938473b0ce4SHugh Dickins 1939473b0ce4SHugh Dickins static ssize_t pages_volatile_show(struct kobject *kobj, 1940473b0ce4SHugh Dickins struct kobj_attribute *attr, char *buf) 1941473b0ce4SHugh Dickins { 1942473b0ce4SHugh Dickins long ksm_pages_volatile; 1943473b0ce4SHugh Dickins 1944473b0ce4SHugh Dickins ksm_pages_volatile = ksm_rmap_items - ksm_pages_shared 1945473b0ce4SHugh Dickins - ksm_pages_sharing - ksm_pages_unshared; 1946473b0ce4SHugh Dickins /* 1947473b0ce4SHugh Dickins * It was not worth any locking to calculate that statistic, 1948473b0ce4SHugh Dickins * but it might therefore sometimes be negative: conceal that. 1949473b0ce4SHugh Dickins */ 1950473b0ce4SHugh Dickins if (ksm_pages_volatile < 0) 1951473b0ce4SHugh Dickins ksm_pages_volatile = 0; 1952473b0ce4SHugh Dickins return sprintf(buf, "%ld\n", ksm_pages_volatile); 1953473b0ce4SHugh Dickins } 1954473b0ce4SHugh Dickins KSM_ATTR_RO(pages_volatile); 1955473b0ce4SHugh Dickins 1956473b0ce4SHugh Dickins static ssize_t full_scans_show(struct kobject *kobj, 1957473b0ce4SHugh Dickins struct kobj_attribute *attr, char *buf) 1958473b0ce4SHugh Dickins { 1959473b0ce4SHugh Dickins return sprintf(buf, "%lu\n", ksm_scan.seqnr); 1960473b0ce4SHugh Dickins } 1961473b0ce4SHugh Dickins KSM_ATTR_RO(full_scans); 1962473b0ce4SHugh Dickins 196331dbd01fSIzik Eidus static struct attribute *ksm_attrs[] = { 196431dbd01fSIzik Eidus &sleep_millisecs_attr.attr, 196531dbd01fSIzik Eidus &pages_to_scan_attr.attr, 196631dbd01fSIzik Eidus &run_attr.attr, 1967b4028260SHugh Dickins &pages_shared_attr.attr, 1968b4028260SHugh Dickins &pages_sharing_attr.attr, 1969473b0ce4SHugh Dickins &pages_unshared_attr.attr, 1970473b0ce4SHugh Dickins &pages_volatile_attr.attr, 1971473b0ce4SHugh Dickins &full_scans_attr.attr, 197231dbd01fSIzik Eidus NULL, 197331dbd01fSIzik Eidus }; 197431dbd01fSIzik Eidus 197531dbd01fSIzik Eidus static struct attribute_group ksm_attr_group = { 197631dbd01fSIzik Eidus .attrs = ksm_attrs, 197731dbd01fSIzik Eidus .name = "ksm", 197831dbd01fSIzik Eidus }; 19792ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */ 198031dbd01fSIzik Eidus 198131dbd01fSIzik Eidus static int __init ksm_init(void) 198231dbd01fSIzik Eidus { 198331dbd01fSIzik Eidus struct task_struct *ksm_thread; 198431dbd01fSIzik Eidus int err; 198531dbd01fSIzik Eidus 198631dbd01fSIzik Eidus err = ksm_slab_init(); 198731dbd01fSIzik Eidus if (err) 198831dbd01fSIzik Eidus goto out; 198931dbd01fSIzik Eidus 199031dbd01fSIzik Eidus ksm_thread = kthread_run(ksm_scan_thread, NULL, "ksmd"); 199131dbd01fSIzik Eidus if (IS_ERR(ksm_thread)) { 199231dbd01fSIzik Eidus printk(KERN_ERR "ksm: creating kthread failed\n"); 199331dbd01fSIzik Eidus err = PTR_ERR(ksm_thread); 1994d9f8984cSLai Jiangshan goto out_free; 199531dbd01fSIzik Eidus } 199631dbd01fSIzik Eidus 19972ffd8679SHugh Dickins #ifdef CONFIG_SYSFS 199831dbd01fSIzik Eidus err = sysfs_create_group(mm_kobj, &ksm_attr_group); 199931dbd01fSIzik Eidus if (err) { 200031dbd01fSIzik Eidus printk(KERN_ERR "ksm: register sysfs failed\n"); 20012ffd8679SHugh Dickins kthread_stop(ksm_thread); 2002d9f8984cSLai Jiangshan goto out_free; 200331dbd01fSIzik Eidus } 2004c73602adSHugh Dickins #else 2005c73602adSHugh Dickins ksm_run = KSM_RUN_MERGE; /* no way for user to start it */ 2006c73602adSHugh Dickins 20072ffd8679SHugh Dickins #endif /* CONFIG_SYSFS */ 200831dbd01fSIzik Eidus 200962b61f61SHugh Dickins #ifdef CONFIG_MEMORY_HOTREMOVE 201062b61f61SHugh Dickins /* 201162b61f61SHugh Dickins * Choose a high priority since the callback takes ksm_thread_mutex: 201262b61f61SHugh Dickins * later callbacks could only be taking locks which nest within that. 201362b61f61SHugh Dickins */ 201462b61f61SHugh Dickins hotplug_memory_notifier(ksm_memory_callback, 100); 201562b61f61SHugh Dickins #endif 201631dbd01fSIzik Eidus return 0; 201731dbd01fSIzik Eidus 2018d9f8984cSLai Jiangshan out_free: 201931dbd01fSIzik Eidus ksm_slab_free(); 202031dbd01fSIzik Eidus out: 202131dbd01fSIzik Eidus return err; 202231dbd01fSIzik Eidus } 202331dbd01fSIzik Eidus module_init(ksm_init) 2024