Skip to content

Commit acc067d

Browse files
ebruAkagunduzhnaz
authored andcommitted
mm: make optimistic check for swapin readahead
Introduce a new sysfs integer knob /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_swap which makes optimistic check for swapin readahead to increase thp collapse rate. Before getting swapped out pages to memory, checks them and allows up to a certain number. It also prints out using tracepoints amount of unmapped ptes. Signed-off-by: Ebru Akagunduz <[email protected]> Acked-by: Rik van Riel <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Naoya Horiguchi <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Joonsoo Kim <[email protected]> Cc: Xie XiuQi <[email protected]> Cc: Cyrill Gorcunov <[email protected]> Cc: Mel Gorman <[email protected]> Cc: David Rientjes <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: Aneesh Kumar K.V <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Michal Hocko <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 7d302a9 commit acc067d

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

include/trace/events/huge_memory.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
EM( SCAN_SWAP_CACHE_PAGE, "page_swap_cache") \
3030
EM( SCAN_DEL_PAGE_LRU, "could_not_delete_page_from_lru")\
3131
EM( SCAN_ALLOC_HUGE_PAGE_FAIL, "alloc_huge_page_failed") \
32-
EMe( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed")
32+
EM( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed") \
33+
EMe( SCAN_EXCEED_SWAP_PTE, "exceed_swap_pte")
3334

3435
#undef EM
3536
#undef EMe
@@ -46,9 +47,9 @@ SCAN_STATUS
4647
TRACE_EVENT(mm_khugepaged_scan_pmd,
4748

4849
TP_PROTO(struct mm_struct *mm, unsigned long pfn, bool writable,
49-
bool referenced, int none_or_zero, int status),
50+
bool referenced, int none_or_zero, int status, int unmapped),
5051

51-
TP_ARGS(mm, pfn, writable, referenced, none_or_zero, status),
52+
TP_ARGS(mm, pfn, writable, referenced, none_or_zero, status, unmapped),
5253

5354
TP_STRUCT__entry(
5455
__field(struct mm_struct *, mm)
@@ -57,6 +58,7 @@ TRACE_EVENT(mm_khugepaged_scan_pmd,
5758
__field(bool, referenced)
5859
__field(int, none_or_zero)
5960
__field(int, status)
61+
__field(int, unmapped)
6062
),
6163

6264
TP_fast_assign(
@@ -66,15 +68,17 @@ TRACE_EVENT(mm_khugepaged_scan_pmd,
6668
__entry->referenced = referenced;
6769
__entry->none_or_zero = none_or_zero;
6870
__entry->status = status;
71+
__entry->unmapped = unmapped;
6972
),
7073

71-
TP_printk("mm=%p, scan_pfn=0x%lx, writable=%d, referenced=%d, none_or_zero=%d, status=%s",
74+
TP_printk("mm=%p, scan_pfn=0x%lx, writable=%d, referenced=%d, none_or_zero=%d, status=%s, unmapped=%d",
7275
__entry->mm,
7376
__entry->pfn,
7477
__entry->writable,
7578
__entry->referenced,
7679
__entry->none_or_zero,
77-
__print_symbolic(__entry->status, SCAN_STATUS))
80+
__print_symbolic(__entry->status, SCAN_STATUS),
81+
__entry->unmapped)
7882
);
7983

8084
TRACE_EVENT(mm_collapse_huge_page,

mm/huge_memory.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/hashtable.h>
2727
#include <linux/userfaultfd_k.h>
2828
#include <linux/page_idle.h>
29+
#include <linux/swapops.h>
2930

3031
#include <asm/tlb.h>
3132
#include <asm/pgalloc.h>
@@ -52,7 +53,8 @@ enum scan_result {
5253
SCAN_SWAP_CACHE_PAGE,
5354
SCAN_DEL_PAGE_LRU,
5455
SCAN_ALLOC_HUGE_PAGE_FAIL,
55-
SCAN_CGROUP_CHARGE_FAIL
56+
SCAN_CGROUP_CHARGE_FAIL,
57+
SCAN_EXCEED_SWAP_PTE
5658
};
5759

5860
#define CREATE_TRACE_POINTS
@@ -94,6 +96,7 @@ static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
9496
* fault.
9597
*/
9698
static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
99+
static unsigned int khugepaged_max_ptes_swap __read_mostly = HPAGE_PMD_NR/8;
97100

98101
static int khugepaged(void *none);
99102
static int khugepaged_slab_init(void);
@@ -580,6 +583,33 @@ static struct kobj_attribute khugepaged_max_ptes_none_attr =
580583
__ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
581584
khugepaged_max_ptes_none_store);
582585

586+
static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
587+
struct kobj_attribute *attr,
588+
char *buf)
589+
{
590+
return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
591+
}
592+
593+
static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
594+
struct kobj_attribute *attr,
595+
const char *buf, size_t count)
596+
{
597+
int err;
598+
unsigned long max_ptes_swap;
599+
600+
err = kstrtoul(buf, 10, &max_ptes_swap);
601+
if (err || max_ptes_swap > HPAGE_PMD_NR-1)
602+
return -EINVAL;
603+
604+
khugepaged_max_ptes_swap = max_ptes_swap;
605+
606+
return count;
607+
}
608+
609+
static struct kobj_attribute khugepaged_max_ptes_swap_attr =
610+
__ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
611+
khugepaged_max_ptes_swap_store);
612+
583613
static struct attribute *khugepaged_attr[] = {
584614
&khugepaged_defrag_attr.attr,
585615
&khugepaged_max_ptes_none_attr.attr,
@@ -588,6 +618,7 @@ static struct attribute *khugepaged_attr[] = {
588618
&full_scans_attr.attr,
589619
&scan_sleep_millisecs_attr.attr,
590620
&alloc_sleep_millisecs_attr.attr,
621+
&khugepaged_max_ptes_swap_attr.attr,
591622
NULL,
592623
};
593624

@@ -2720,7 +2751,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
27202751
struct page *page = NULL;
27212752
unsigned long _address;
27222753
spinlock_t *ptl;
2723-
int node = NUMA_NO_NODE;
2754+
int node = NUMA_NO_NODE, unmapped = 0;
27242755
bool writable = false, referenced = false;
27252756

27262757
VM_BUG_ON(address & ~HPAGE_PMD_MASK);
@@ -2736,6 +2767,14 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
27362767
for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
27372768
_pte++, _address += PAGE_SIZE) {
27382769
pte_t pteval = *_pte;
2770+
if (is_swap_pte(pteval)) {
2771+
if (++unmapped <= khugepaged_max_ptes_swap) {
2772+
continue;
2773+
} else {
2774+
ret = SCAN_EXCEED_SWAP_PTE;
2775+
goto out_unmap;
2776+
}
2777+
}
27392778
if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
27402779
if (!userfaultfd_armed(vma) &&
27412780
++none_or_zero <= khugepaged_max_ptes_none) {
@@ -2816,7 +2855,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
28162855
}
28172856
out:
28182857
trace_mm_khugepaged_scan_pmd(mm, page_to_pfn(page), writable, referenced,
2819-
none_or_zero, result);
2858+
none_or_zero, result, unmapped);
28202859
return ret;
28212860
}
28222861

0 commit comments

Comments
 (0)