blob: 8b76008fcd3240f5c8aa7a07f1b37d7756bfd101 [file] [log] [blame]
Hugh Dickinsf8af4da2009-09-21 17:01:57 -07001/*
2 * Initial dummy version just to illustrate KSM's interface to other files.
3 */
4
5#include <linux/errno.h>
6#include <linux/mman.h>
7#include <linux/ksm.h>
8
9int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
10 unsigned long end, int advice, unsigned long *vm_flags)
11{
12 struct mm_struct *mm = vma->vm_mm;
13
14 switch (advice) {
15 case MADV_MERGEABLE:
16 /*
17 * Be somewhat over-protective for now!
18 */
19 if (*vm_flags & (VM_MERGEABLE | VM_SHARED | VM_MAYSHARE |
20 VM_PFNMAP | VM_IO | VM_DONTEXPAND |
21 VM_RESERVED | VM_HUGETLB | VM_INSERTPAGE |
22 VM_MIXEDMAP | VM_SAO))
23 return 0; /* just ignore the advice */
24
25 if (!test_bit(MMF_VM_MERGEABLE, &mm->flags))
26 if (__ksm_enter(mm) < 0)
27 return -EAGAIN;
28
29 *vm_flags |= VM_MERGEABLE;
30 break;
31
32 case MADV_UNMERGEABLE:
33 if (!(*vm_flags & VM_MERGEABLE))
34 return 0; /* just ignore the advice */
35
36 /* Unmerge any merged pages here */
37
38 *vm_flags &= ~VM_MERGEABLE;
39 break;
40 }
41
42 return 0;
43}
44
45int __ksm_enter(struct mm_struct *mm)
46{
47 /* Allocate a structure to track mm and link it into KSM's list */
48 set_bit(MMF_VM_MERGEABLE, &mm->flags);
49 return 0;
50}
51
52void __ksm_exit(struct mm_struct *mm)
53{
54 /* Unlink and free all KSM's structures which track this mm */
55 clear_bit(MMF_VM_MERGEABLE, &mm->flags);
56}