[RFC 2/6] mm, thp: add a global interface for zero subapges reclaim

Ning Zhang
2021-10-28T11:57:02Z

Add a global interface to configure zero subpages reclaim global:

/sys/kernel/mm/transparent_hugepage/reclaim

It has three modes:

memcg, means every memory cgroup will use their own configure.
enable, means every mem cgroup will enable reclaim.
disable, means every mem cgroup will disable reclaim.

The default mode is memcg.

Signed-off-by: Ning Zhang <ningzhang@linux.alibaba.com>
---
include/linux/huge_mm.h | 1 +
include/linux/memcontrol.h | 8 ++++++++
mm/huge_memory.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
mm/memcontrol.c | 2 +-
mm/vmscan.c | 2 +-
5 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index e1b3bf9..04607b1 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -186,6 +186,7 @@ unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
bool is_transparent_hugepage(struct page *page);

#ifdef CONFIG_MEMCG
+extern int global_thp_reclaim;
int zsr_get_hpage(struct hpage_reclaim *hr_queue, struct page **reclaim_page);
unsigned long zsr_reclaim_hpage(struct lruvec *lruvec, struct page *page);
static inline struct list_head *hpage_reclaim_list(struct page *page)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 502a6ab..f99f13f 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -1123,6 +1123,14 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
void del_hpage_from_queue(struct page *page);
+
+static inline int get_thp_reclaim_mode(struct mem_cgroup *memcg)
+{
+ int reclaim = READ_ONCE(global_thp_reclaim);
+
+ return (reclaim != THP_RECLAIM_MEMCG) ? reclaim :
+ READ_ONCE(memcg->thp_reclaim);
+}
#endif

#else /* CONFIG_MEMCG */
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 21e3c01..84fd738 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -60,6 +60,10 @@

static struct shrinker deferred_split_shrinker;

+#ifdef CONFIG_MEMCG
+int global_thp_reclaim = THP_RECLAIM_MEMCG;
+#endif
+
static atomic_t huge_zero_refcount;
struct page *huge_zero_page __read_mostly;
unsigned long huge_zero_pfn __read_mostly = ~0UL;
@@ -330,6 +334,43 @@ static ssize_t hpage_pmd_size_show(struct kobject *kobj,
static struct kobj_attribute hpage_pmd_size_attr =
__ATTR_RO(hpage_pmd_size);

+#ifdef CONFIG_MEMCG
+static ssize_t reclaim_show(struct kobject *kobj,
+ struct kobj_attribute *attr, char *buf)
+{
+ int thp_reclaim = READ_ONCE(global_thp_reclaim);
+
+ if (thp_reclaim == THP_RECLAIM_MEMCG)
+ return sprintf(buf, "[memcg] enable disable\n");
+ else if (thp_reclaim == THP_RECLAIM_ENABLE)
+ return sprintf(buf, "memcg [enable] disable\n");
+ else
+ return sprintf(buf, "memcg enable [disable]\n");
+}
+
+static ssize_t reclaim_store(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ const char *buf, size_t count)
+{
+ if (!memcmp("memcg", buf,
+ min(sizeof("memcg")-1, count)))
+ WRITE_ONCE(global_thp_reclaim, THP_RECLAIM_MEMCG);
+ else if (!memcmp("enable", buf,
+ min(sizeof("enable")-1, count)))
+ WRITE_ONCE(global_thp_reclaim, THP_RECLAIM_ENABLE);
+ else if (!memcmp("disable", buf,
+ min(sizeof("disable")-1, count)))
+ WRITE_ONCE(global_thp_reclaim, THP_RECLAIM_DISABLE);
+ else
+ return -EINVAL;
+
+ return count;
+}
+
+static struct kobj_attribute reclaim_attr =
+ __ATTR(reclaim, 0644, reclaim_show, reclaim_store);
+#endif
+
static struct attribute *hugepage_attr[] = {
&enabled_attr.attr,
&defrag_attr.attr,
@@ -338,6 +379,9 @@ static ssize_t hpage_pmd_size_show(struct kobject *kobj,
#ifdef CONFIG_SHMEM
&shmem_enabled_attr.attr,
#endif
+#ifdef CONFIG_MEMCG
+ &reclaim_attr.attr,
+#endif
NULL,
};

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 5df1cdd..ae96781 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2746,7 +2746,7 @@ static void add_hpage_to_queue(struct page *page, struct mem_cgroup *memcg)
struct hpage_reclaim *hr_queue;
unsigned long flags;

- if (READ_ONCE(memcg->thp_reclaim) == THP_RECLAIM_DISABLE)
+ if (get_thp_reclaim_mode(memcg) == THP_RECLAIM_DISABLE)
return;

page = compound_head(page);
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9be136f..f4ff14d 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -2801,7 +2801,7 @@ static unsigned long reclaim_hpage_zero_subpages(struct lruvec *lruvec,
goto out;

hr_queue = &memcg->nodeinfo[nid]->hpage_reclaim_queue;
- if (!READ_ONCE(memcg->thp_reclaim))
+ if (get_thp_reclaim_mode(memcg) == THP_RECLAIM_DISABLE)
goto out;

/* The last scan loop will scan all the huge pages.*/
--
1.8.3.1