diff mbox series

[v2] mm: memcontrol: Simplify the mem_cgroup_page_lruvec

Message ID 20201028035013.99711-4-songmuchun@bytedance.com (mailing list archive)
State New, archived
Headers show
Series [v2] mm: memcontrol: Simplify the mem_cgroup_page_lruvec | expand

Commit Message

Muchun Song Oct. 28, 2020, 3:50 a.m. UTC
We can reuse the code of mem_cgroup_lruvec() to simplify the code
of the mem_cgroup_page_lruvec().

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
 changelog in v2:
 1. Move mem_cgroup_node_lruvec to memcontrol.c to avoid abuse.

 include/linux/memcontrol.h | 41 ++++-------------------------
 mm/memcontrol.c            | 53 ++++++++++++++++++++++++++------------
 2 files changed, 42 insertions(+), 52 deletions(-)

Comments

Michal Hocko Oct. 29, 2020, 9:08 a.m. UTC | #1
On Wed 28-10-20 11:50:13, Muchun Song wrote:
[...]
> -struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat)
> +static struct lruvec *
> +__mem_cgroup_node_lruvec(struct mem_cgroup *memcg, struct pglist_data *pgdat,
> +			 int nid)

I thought I have made it clear that this is not a good approach. Please
do not repost new version without that being addressed. If there are any
questions then feel free to ask for details.
Shakeel Butt Oct. 29, 2020, 4:01 p.m. UTC | #2
On Thu, Oct 29, 2020 at 2:08 AM Michal Hocko <mhocko@suse.com> wrote:
>
> On Wed 28-10-20 11:50:13, Muchun Song wrote:
> [...]
> > -struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat)
> > +static struct lruvec *
> > +__mem_cgroup_node_lruvec(struct mem_cgroup *memcg, struct pglist_data *pgdat,
> > +                      int nid)
>
> I thought I have made it clear that this is not a good approach. Please
> do not repost new version without that being addressed. If there are any
> questions then feel free to ask for details.

You can get nid from pgdat (pgdat->node_id) and also pgdat from nid
(NODE_DATA(nid)), so, __mem_cgroup_node_lruvec() only need one of them
as parameter.
Michal Hocko Oct. 29, 2020, 4:13 p.m. UTC | #3
On Thu 29-10-20 09:01:37, Shakeel Butt wrote:
> On Thu, Oct 29, 2020 at 2:08 AM Michal Hocko <mhocko@suse.com> wrote:
> >
> > On Wed 28-10-20 11:50:13, Muchun Song wrote:
> > [...]
> > > -struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat)
> > > +static struct lruvec *
> > > +__mem_cgroup_node_lruvec(struct mem_cgroup *memcg, struct pglist_data *pgdat,
> > > +                      int nid)
> >
> > I thought I have made it clear that this is not a good approach. Please
> > do not repost new version without that being addressed. If there are any
> > questions then feel free to ask for details.
> 
> You can get nid from pgdat (pgdat->node_id) and also pgdat from nid
> (NODE_DATA(nid)), so, __mem_cgroup_node_lruvec() only need one of them
> as parameter.

Exactly what I've said in the previous version review. I suspect that
the issue is that mem_cgroup_page_nodeinfo (based on page's node_id)
and the given pgdat can mismatch in the existing code but that shouldn't
be a real problem because the mismatch can only happen for lruvec->pgdat
== NULL unless I am missing something.
diff mbox series

Patch

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 95807bf6be64..bbdc694d26b1 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -445,48 +445,17 @@  void mem_cgroup_uncharge_list(struct list_head *page_list);
 
 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage);
 
-static struct mem_cgroup_per_node *
+static inline struct mem_cgroup_per_node *
 mem_cgroup_nodeinfo(struct mem_cgroup *memcg, int nid)
 {
 	return memcg->nodeinfo[nid];
 }
 
-/**
- * mem_cgroup_lruvec - get the lru list vector for a memcg & node
- * @memcg: memcg of the wanted lruvec
- *
- * Returns the lru list vector holding pages for a given @memcg &
- * @node combination. This can be the node lruvec, if the memory
- * controller is disabled.
- */
-static inline struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
-					       struct pglist_data *pgdat)
-{
-	struct mem_cgroup_per_node *mz;
-	struct lruvec *lruvec;
-
-	if (mem_cgroup_disabled()) {
-		lruvec = &pgdat->__lruvec;
-		goto out;
-	}
-
-	if (!memcg)
-		memcg = root_mem_cgroup;
-
-	mz = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
-	lruvec = &mz->lruvec;
-out:
-	/*
-	 * Since a node can be onlined after the mem_cgroup was created,
-	 * we have to be prepared to initialize lruvec->pgdat here;
-	 * and if offlined then reonlined, we need to reinitialize it.
-	 */
-	if (unlikely(lruvec->pgdat != pgdat))
-		lruvec->pgdat = pgdat;
-	return lruvec;
-}
+struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
+				 struct pglist_data *pgdat);
 
-struct lruvec *mem_cgroup_page_lruvec(struct page *, struct pglist_data *);
+struct lruvec *mem_cgroup_page_lruvec(struct page *page,
+				      struct pglist_data *pgdat);
 
 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
 
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index bbd40c5af61e..28095a1711aa 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1332,18 +1332,15 @@  int mem_cgroup_scan_tasks(struct mem_cgroup *memcg,
 	return ret;
 }
 
-/**
- * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
- * @page: the page
- * @pgdat: pgdat of the page
- *
- * This function relies on page->mem_cgroup being stable - see the
- * access rules in commit_charge().
+/*
+ * Note: Do not use this function directly. Please use mem_cgroup_lruvec()
+ * or mem_cgroup_page_lruvec() instead.
  */
-struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgdat)
+static struct lruvec *
+__mem_cgroup_node_lruvec(struct mem_cgroup *memcg, struct pglist_data *pgdat,
+			 int nid)
 {
 	struct mem_cgroup_per_node *mz;
-	struct mem_cgroup *memcg;
 	struct lruvec *lruvec;
 
 	if (mem_cgroup_disabled()) {
@@ -1351,20 +1348,15 @@  struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgd
 		goto out;
 	}
 
-	memcg = page->mem_cgroup;
-	/*
-	 * Swapcache readahead pages are added to the LRU - and
-	 * possibly migrated - before they are charged.
-	 */
 	if (!memcg)
 		memcg = root_mem_cgroup;
 
-	mz = mem_cgroup_page_nodeinfo(memcg, page);
+	mz = mem_cgroup_nodeinfo(memcg, nid);
 	lruvec = &mz->lruvec;
 out:
 	/*
 	 * Since a node can be onlined after the mem_cgroup was created,
-	 * we have to be prepared to initialize lruvec->zone here;
+	 * we have to be prepared to initialize lruvec->pgdat here;
 	 * and if offlined then reonlined, we need to reinitialize it.
 	 */
 	if (unlikely(lruvec->pgdat != pgdat))
@@ -1372,6 +1364,35 @@  struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct pglist_data *pgd
 	return lruvec;
 }
 
+/**
+ * mem_cgroup_lruvec - get the lru list vector for a memcg & node
+ * @memcg: memcg of the wanted lruvec
+ *
+ * Returns the lru list vector holding pages for a given @memcg &
+ * @node combination. This can be the node lruvec, if the memory
+ * controller is disabled.
+ */
+struct lruvec *mem_cgroup_lruvec(struct mem_cgroup *memcg,
+				 struct pglist_data *pgdat)
+{
+	return __mem_cgroup_node_lruvec(memcg, pgdat, pgdat->node_id);
+}
+
+/**
+ * mem_cgroup_page_lruvec - return lruvec for isolating/putting an LRU page
+ * @page: the page
+ * @pgdat: pgdat of the page
+ *
+ * This function relies on page->mem_cgroup being stable - see the
+ * access rules in commit_charge().
+ */
+struct lruvec *mem_cgroup_page_lruvec(struct page *page,
+				      struct pglist_data *pgdat)
+{
+	return __mem_cgroup_node_lruvec(page->mem_cgroup, pgdat,
+					page_to_nid(page));
+}
+
 /**
  * mem_cgroup_update_lru_size - account for adding or removing an lru page
  * @lruvec: mem_cgroup per zone lru vector