linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Lincheng Yang <lincheng.yang@transsion.corp-partner.google.com>
To: akpm@linux-foundation.org, rostedt@goodmis.org,
	mhiramat@kernel.org, willy@infradead.org, hughd@google.com,
	peterx@redhat.com, mike.kravetz@oracle.com, jgg@ziepe.ca,
	surenb@google.com, steven.price@arm.com,
	pasha.tatashin@soleen.com, kirill.shutemov@linux.intel.com,
	yuanchu@google.com, david@redhat.com,
	mathieu.desnoyers@efficios.com, dhowells@redhat.com,
	shakeelb@google.com, pcc@google.com, tytso@mit.edu,
	42.hyeyoo@gmail.com, vbabka@suse.cz, catalin.marinas@arm.com,
	lrh2000@pku.edu.cn, ying.huang@intel.com, mhocko@suse.com,
	vishal.moola@gmail.com, yosryahmed@google.com,
	findns94@gmail.com, neilb@suse.de
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	wanbin.wang@transsion.com, chunlei.zhuang@transsion.com,
	jinsheng.zhao@transsion.com, jiajun.ling@transsion.com,
	dongyun.liu@transsion.com,
	Lincheng Yang <lincheng.yang@transsion.com>
Subject: [RFC PATCH 4/5] mm: add page implyreclaim flag
Date: Sun,  8 Oct 2023 17:59:23 +0800	[thread overview]
Message-ID: <20231008095924.1165106-5-lincheng.yang@transsion.com> (raw)
In-Reply-To: <20231008095924.1165106-1-lincheng.yang@transsion.com>

Add implyrecalim flag means that the page is reclaim from the user advise.
If the number of restore times for these implyrecalim pages exceeds
workingset_restore_limit, it means they are frequently used and are hot
pages. Otherwise, continue to determine whether it has been restored. If
so, it means that it is frequently used also and belongs to the hot page.

Signed-off-by: Lincheng Yang <lincheng.yang@transsion.com>
---
 include/linux/mmzone.h         |  1 +
 include/linux/page-flags.h     |  3 ++
 include/trace/events/mmflags.h |  3 +-
 mm/madvise.c                   |  1 +
 mm/migrate.c                   |  2 ++
 mm/swapfile.c                  | 64 +++++++++++++++++++++++++++++++++-
 mm/vmscan.c                    |  3 ++
 7 files changed, 75 insertions(+), 2 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 5e50b78d58ea..b280e6b0015a 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -626,6 +626,7 @@ struct lruvec {
 	atomic_long_t			nonresident_age;
 	/* Refaults at the time of last reclaim cycle */
 	unsigned long			refaults[ANON_AND_FILE];
+	unsigned long			restores[ANON_AND_FILE];
 	/* Various lruvec state flags (enum lruvec_flags) */
 	unsigned long			flags;
 #ifdef CONFIG_LRU_GEN
diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index a2c83c0100aa..4a1278851d4b 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -138,6 +138,7 @@ enum pageflags {
 #endif
 	PG_hot,
 	PG_cold,
+	PG_implyreclaim,
 	__NR_PAGEFLAGS,
 
 	PG_readahead = PG_reclaim,
@@ -482,6 +483,8 @@ PAGEFLAG(Hot, hot, PF_HEAD)
 	TESTCLEARFLAG(Hot, hot, PF_HEAD)
 PAGEFLAG(Cold, cold, PF_HEAD)
 	TESTCLEARFLAG(Cold, cold, PF_HEAD)
+PAGEFLAG(Implyreclaim, implyreclaim, PF_HEAD)
+	TESTCLEARFLAG(Implyreclaim, implyreclaim, PF_HEAD)
 __PAGEFLAG(Slab, slab, PF_NO_TAIL)
 PAGEFLAG(Checked, checked, PF_NO_COMPOUND)	   /* Used by some filesystems */
 
diff --git a/include/trace/events/mmflags.h b/include/trace/events/mmflags.h
index f266f92c41c6..ee014f955aef 100644
--- a/include/trace/events/mmflags.h
+++ b/include/trace/events/mmflags.h
@@ -120,7 +120,8 @@
 	DEF_PAGEFLAG_NAME(swapbacked),					\
 	DEF_PAGEFLAG_NAME(unevictable),					\
 	DEF_PAGEFLAG_NAME(hot),						\
-	DEF_PAGEFLAG_NAME(cold)						\
+	DEF_PAGEFLAG_NAME(cold),					\
+	DEF_PAGEFLAG_NAME(implyreclaim)					\
 IF_HAVE_PG_MLOCK(mlocked)						\
 IF_HAVE_PG_UNCACHED(uncached)						\
 IF_HAVE_PG_HWPOISON(hwpoison)						\
diff --git a/mm/madvise.c b/mm/madvise.c
index a5c19bb3f392..199b48dfa8c5 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -518,6 +518,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 				} else {
 					list_add(&folio->lru, &folio_list);
 					folio_set_cold(folio);
+					folio_set_implyreclaim(folio);
 				}
 			}
 		} else
diff --git a/mm/migrate.c b/mm/migrate.c
index 9f97744bb0a8..691b4f7bf1ae 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -565,6 +565,8 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 		folio_set_hot(newfolio);
 	if (folio_test_cold(folio))
 		folio_set_cold(newfolio);
+	if (folio_test_implyreclaim(folio))
+		folio_set_implyreclaim(newfolio);
 	if (folio_test_checked(folio))
 		folio_set_checked(newfolio);
 	/*
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 5378f70d330d..629e6a291e9b 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -105,6 +105,8 @@ static atomic_t proc_poll_event = ATOMIC_INIT(0);
 
 atomic_t nr_rotate_swap = ATOMIC_INIT(0);
 
+static unsigned int workingset_restore_limit;
+
 static struct swap_info_struct *swap_type_to_swap_info(int type)
 {
 	if (type >= MAX_SWAPFILES)
@@ -120,11 +122,33 @@ static inline bool swap_info_hot(struct swap_info_struct *si)
 
 bool swap_folio_hot(struct folio *folio, bool hotness)
 {
+	struct lruvec *lruvec;
+	struct mem_cgroup *memcg;
+	unsigned long restores;
+	int delta;
+
 	if (hotness)
 		return true;
 
-	if (folio_test_swapbacked(folio) && folio_test_hot(folio))
+	if (folio_test_swapbacked(folio) && folio_test_hot(folio)) {
+		folio_clear_implyreclaim(folio);
+		return true;
+	}
+
+	rcu_read_lock(); // prevent writing from delaying reading
+	memcg = folio_memcg_rcu(folio);
+	rcu_read_unlock();
+
+	lruvec = mem_cgroup_lruvec(memcg, folio_pgdat(folio));
+	restores = lruvec_page_state(lruvec, WORKINGSET_RESTORE_ANON);
+	delta = restores - lruvec->restores[WORKINGSET_ANON];
+
+	if (folio_test_clear_implyreclaim(folio)) {
+		if (delta > workingset_restore_limit)
+			return true;
+	} else if (delta) {
 		return true;
+	}
 
 	if (folio_test_cold(folio))
 		folio_clear_cold(folio);
@@ -2715,9 +2739,47 @@ static const struct proc_ops swaps_proc_ops = {
 	.proc_poll	= swaps_poll,
 };
 
+static ssize_t workingset_restore_limit_write(struct file *file,
+					      const char __user *ubuf,
+					      size_t count, loff_t *pos)
+{
+	unsigned int val;
+	int ret;
+
+	ret = kstrtouint_from_user(ubuf, count, 10, &val);
+	if (ret)
+		return ret;
+
+	workingset_restore_limit = val;
+
+	return count;
+}
+
+static int workingset_restore_limit_show(struct seq_file *m, void *v)
+{
+	seq_printf(m, "%d\n", workingset_restore_limit);
+
+	return 0;
+}
+
+static int workingset_restore_limit_open(struct inode *inode, struct file *file)
+{
+	return single_open(file, workingset_restore_limit_show, NULL);
+}
+
+const struct proc_ops workingset_restore_limit_fops = {
+	.proc_open = workingset_restore_limit_open,
+	.proc_read = seq_read,
+	.proc_lseek = seq_lseek,
+	.proc_release = seq_release,
+	.proc_write = workingset_restore_limit_write,
+};
+
 static int __init procswaps_init(void)
 {
 	proc_create("swaps", 0, NULL, &swaps_proc_ops);
+	proc_create("workingset_restore_limit", S_IALLUGO, NULL, &workingset_restore_limit_fops);
+
 	return 0;
 }
 __initcall(procswaps_init);
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 11d175d9fe0c..8107f8d86d7f 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -6805,6 +6805,9 @@ static void snapshot_refaults(struct mem_cgroup *target_memcg, pg_data_t *pgdat)
 	target_lruvec->refaults[WORKINGSET_ANON] = refaults;
 	refaults = lruvec_page_state(target_lruvec, WORKINGSET_ACTIVATE_FILE);
 	target_lruvec->refaults[WORKINGSET_FILE] = refaults;
+
+	refaults = lruvec_page_state(target_lruvec, WORKINGSET_RESTORE_ANON);
+	target_lruvec->restores[WORKINGSET_ANON] = refaults;
 }
 
 /*
-- 
2.34.1



  parent reply	other threads:[~2023-10-08 10:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-08  9:59 [RFC PATCH 0/5] hot page swap to zram, cold page swap to swapfile directly Lincheng Yang
2023-10-08  9:59 ` [RFC PATCH 1/5] mm/swap_slots: cleanup swap slot cache Lincheng Yang
2023-10-08  9:59 ` [RFC PATCH 2/5] mm: introduce hot and cold anon page flags Lincheng Yang
2023-10-08  9:59 ` [RFC PATCH 3/5] mm: add VMA hot flag Lincheng Yang
2023-10-08  9:59 ` Lincheng Yang [this message]
2023-10-08 11:07   ` [RFC PATCH 4/5] mm: add page implyreclaim flag Matthew Wilcox
2023-10-10  3:27     ` Lincheng Yang
2023-10-08  9:59 ` [RFC PATCH 5/5] mm/swapfile: add swapfile_write_enable interface Lincheng Yang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231008095924.1165106-5-lincheng.yang@transsion.com \
    --to=lincheng.yang@transsion.corp-partner.google.com \
    --cc=42.hyeyoo@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=chunlei.zhuang@transsion.com \
    --cc=david@redhat.com \
    --cc=dhowells@redhat.com \
    --cc=dongyun.liu@transsion.com \
    --cc=findns94@gmail.com \
    --cc=hughd@google.com \
    --cc=jgg@ziepe.ca \
    --cc=jiajun.ling@transsion.com \
    --cc=jinsheng.zhao@transsion.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=lincheng.yang@transsion.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lrh2000@pku.edu.cn \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mike.kravetz@oracle.com \
    --cc=neilb@suse.de \
    --cc=pasha.tatashin@soleen.com \
    --cc=pcc@google.com \
    --cc=peterx@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=shakeelb@google.com \
    --cc=steven.price@arm.com \
    --cc=surenb@google.com \
    --cc=tytso@mit.edu \
    --cc=vbabka@suse.cz \
    --cc=vishal.moola@gmail.com \
    --cc=wanbin.wang@transsion.com \
    --cc=willy@infradead.org \
    --cc=ying.huang@intel.com \
    --cc=yosryahmed@google.com \
    --cc=yuanchu@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).