From: Kairui Song lruvec_page_state_local() clamps negative values to zero on SMP. That is right for readers reporting page counts, but can be wrong for reparenting once MGLRU updates the counters without the lruvec lock. Reparenting runs after the forced stats flush, so a negative is a real balance that has to move to the parent, not be discarded. We will introduce lockless MGLRU promotion which may cause following problem: 1. A refs CAS on a folio on the child's lruvec moves it from inactive to active, and its active/inactive counter update follows as a separate step. 2. A concurrent isolation of the folio reads the folio as active, debits the child's NR_ACTIVE_* by delta before CSS_DYING is visible to it. 3. The child is killed, and the two __update_lru_size() calls from step 1 land on the parent: NR_INACTIVE_* -delta, then NR_ACTIVE_* +delta. The child is left with NR_ACTIVE_* -delta while the parent holds the matching +delta, and reparenting the child's raw value is what cancels the parent's credit. However, if the child happens to hold few enough folios for that counter to be negative, lruvec_page_state_local() clamps the child's negative to zero, which drops the debit instead, so the parent keeps NR_ACTIVE_* overcounted by delta, and its active + inactive total overcounted as well, permanently: the child's local counters are gone once reparenting is done. So use a raw signed helper and reparent with that. Ordinary readers still keep the clamp. Reported-by: Kunwu Chan Closes: https://lore.kernel.org/linux-mm/CAN_Opa-+nb0vxQ+HsENCG2fEEMZoxG9Sy_DBC4CLkWjNLzDWKw@mail.gmail.com/ Reviewed-by: Kunwu Chan Reviewed-by: SJ Park Reviewed-by: Ridong Chen Signed-off-by: Kairui Song --- mm/memcontrol.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 475201bf762e..b75a61ddeded 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -569,11 +569,10 @@ unsigned long lruvec_page_state_monotonic(const struct lruvec *lruvec, return (unsigned long)READ_ONCE(pn->lruvec_stats->state[i]); } -unsigned long lruvec_page_state_local(const struct lruvec *lruvec, +static long __lruvec_page_state_local(const struct lruvec *lruvec, enum node_stat_item idx) { const struct mem_cgroup_per_node *pn; - long x; int i; if (mem_cgroup_disabled()) @@ -584,7 +583,14 @@ unsigned long lruvec_page_state_local(const struct lruvec *lruvec, return 0; pn = container_of_const(lruvec, struct mem_cgroup_per_node, lruvec); - x = READ_ONCE(pn->lruvec_stats->state_local[i]); + return READ_ONCE(pn->lruvec_stats->state_local[i]); +} + +unsigned long lruvec_page_state_local(const struct lruvec *lruvec, + enum node_stat_item idx) +{ + long x = __lruvec_page_state_local(lruvec, idx); + #ifdef CONFIG_SMP if (x < 0) x = 0; @@ -602,7 +608,8 @@ void reparent_memcg_lruvec_state_local(struct mem_cgroup *memcg, for_each_node(nid) { struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(nid)); - unsigned long value = lruvec_page_state_local(lruvec, idx); + /* Reparent the raw value: the clamp drops the child's debit. */ + long value = __lruvec_page_state_local(lruvec, idx); __mod_memcg_lruvec_state(memcg, nid, idx, -value); __mod_memcg_lruvec_state(parent, nid, idx, value); -- 2.56.1