Skip to content

Commit 77bb499

Browse files
koct9itorvalds
authored andcommitted
pagemap: add mmap-exclusive bit for marking pages mapped only here
This patch sets bit 56 in pagemap if this page is mapped only once. It allows to detect exclusively used pages without exposing PFN: present file exclusive state 0 0 0 non-present 1 1 0 file page mapped somewhere else 1 1 1 file page mapped only here 1 0 0 anon non-CoWed page (shared with parent/child) 1 0 1 anon CoWed page (or never forked) CoWed pages in (MAP_FILE | MAP_PRIVATE) areas are anon in this context. MMap-exclusive bit doesn't reflect potential page-sharing via swapcache: page could be mapped once but has several swap-ptes which point to it. Application could detect that by swap bit in pagemap entry and touch that pte via /proc/pid/mem to get real information. See http://lkml.kernel.org/r/CAEVpBa+_RyACkhODZrRvQLs80iy0sqpdrd0AaP_-tgnX3Y9yNQ@mail.gmail.com Requested by Mark Williamson. [akpm@linux-foundation.org: fix spello] Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru> Reviewed-by: Mark Williamson <mwilliamson@undo-software.com> Tested-by: Mark Williamson <mwilliamson@undo-software.com> Reviewed-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1c90308 commit 77bb499

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎Documentation/vm/pagemap.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ There are three components to pagemap:
1616
* Bits 0-4 swap type if swapped
1717
* Bits 5-54 swap offset if swapped
1818
* Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
19-
* Bits 56-60 zero
19+
* Bit 56 page exclusively mapped
20+
* Bits 57-60 zero
2021
* Bit 61 page is file-page or shared-anon
2122
* Bit 62 page swapped
2223
* Bit 63 page present

‎fs/proc/task_mmu.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ struct pagemapread {
949949
#define PM_PFRAME_BITS 55
950950
#define PM_PFRAME_MASK GENMASK_ULL(PM_PFRAME_BITS - 1, 0)
951951
#define PM_SOFT_DIRTY BIT_ULL(55)
952+
#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
952953
#define PM_FILE BIT_ULL(61)
953954
#define PM_SWAP BIT_ULL(62)
954955
#define PM_PRESENT BIT_ULL(63)
@@ -1036,6 +1037,8 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
10361037

10371038
if (page && !PageAnon(page))
10381039
flags |= PM_FILE;
1040+
if (page && page_mapcount(page) == 1)
1041+
flags |= PM_MMAP_EXCLUSIVE;
10391042
if (vma->vm_flags & VM_SOFTDIRTY)
10401043
flags |= PM_SOFT_DIRTY;
10411044

@@ -1066,6 +1069,11 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
10661069
* This if-check is just to prepare for future implementation.
10671070
*/
10681071
if (pmd_present(pmd)) {
1072+
struct page *page = pmd_page(pmd);
1073+
1074+
if (page_mapcount(page) == 1)
1075+
flags |= PM_MMAP_EXCLUSIVE;
1076+
10691077
flags |= PM_PRESENT;
10701078
if (pm->show_pfn)
10711079
frame = pmd_pfn(pmd) +
@@ -1131,6 +1139,9 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
11311139
if (!PageAnon(page))
11321140
flags |= PM_FILE;
11331141

1142+
if (page_mapcount(page) == 1)
1143+
flags |= PM_MMAP_EXCLUSIVE;
1144+
11341145
flags |= PM_PRESENT;
11351146
if (pm->show_pfn)
11361147
frame = pte_pfn(pte) +
@@ -1163,7 +1174,8 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
11631174
* Bits 0-4 swap type if swapped
11641175
* Bits 5-54 swap offset if swapped
11651176
* Bit 55 pte is soft-dirty (see Documentation/vm/soft-dirty.txt)
1166-
* Bits 56-60 zero
1177+
* Bit 56 page exclusively mapped
1178+
* Bits 57-60 zero
11671179
* Bit 61 page is file-page or shared-anon
11681180
* Bit 62 page swapped
11691181
* Bit 63 page present

‎tools/vm/page-types.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define PM_PFRAME_MASK ((1LL << PM_PFRAME_BITS) - 1)
6363
#define PM_PFRAME(x) ((x) & PM_PFRAME_MASK)
6464
#define PM_SOFT_DIRTY (1ULL << 55)
65+
#define PM_MMAP_EXCLUSIVE (1ULL << 56)
6566
#define PM_FILE (1ULL << 61)
6667
#define PM_SWAP (1ULL << 62)
6768
#define PM_PRESENT (1ULL << 63)
@@ -91,6 +92,8 @@
9192
#define KPF_SLOB_FREE 49
9293
#define KPF_SLUB_FROZEN 50
9394
#define KPF_SLUB_DEBUG 51
95+
#define KPF_FILE 62
96+
#define KPF_MMAP_EXCLUSIVE 63
9497

9598
#define KPF_ALL_BITS ((uint64_t)~0ULL)
9699
#define KPF_HACKERS_BITS (0xffffULL << 32)
@@ -140,6 +143,9 @@ static const char * const page_flag_names[] = {
140143
[KPF_SLOB_FREE] = "P:slob_free",
141144
[KPF_SLUB_FROZEN] = "A:slub_frozen",
142145
[KPF_SLUB_DEBUG] = "E:slub_debug",
146+
147+
[KPF_FILE] = "F:file",
148+
[KPF_MMAP_EXCLUSIVE] = "1:mmap_exclusive",
143149
};
144150

145151

@@ -443,6 +449,10 @@ static uint64_t expand_overloaded_flags(uint64_t flags, uint64_t pme)
443449

444450
if (pme & PM_SOFT_DIRTY)
445451
flags |= BIT(SOFTDIRTY);
452+
if (pme & PM_FILE)
453+
flags |= BIT(FILE);
454+
if (pme & PM_MMAP_EXCLUSIVE)
455+
flags |= BIT(MMAP_EXCLUSIVE);
446456

447457
return flags;
448458
}

0 commit comments

Comments
 (0)