From nobody Fri Dec 27 15:01:59 2024 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E96CE221D8D for ; Tue, 10 Dec 2024 04:08:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.186 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733803697; cv=none; b=owf0WBsqhagGJqfb6hYzozHLaaA2E8KAMza1/R+rzckn4+1mvC0UUaj3wffUBgjcjuV+pFZk6MtrJu5hH9mXuuUQh78ncy+ZQKIT7RGDqQCvMai9dKb/QmYq6IjtJkiVj9ihLt7GbSGkku6f3GOsJ4HMYqO7HmB/s1JK/8Uq2V8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733803697; c=relaxed/simple; bh=2EreavHoP96YSHO6gB3dHcCFTOGvdN4giySUOzF+fd4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YxyNM2QokrvAia3XbnbqdBEVAqPIgJtl+o+tbbQd8hnZ2uCVtSZCCcduhFIp9N9FLDumbxn2NVvOQI4DnC5q6DaW9aGMzOW9ZuaYOFa9qMcqFtpppgcLY0LDgl7XdCmu4EburyDfFWjB8SNnoImwwyfTjAUqV7JXMyflqauODDI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=bodIUL0I; arc=none smtp.client-ip=91.218.175.186 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="bodIUL0I" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1733803690; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=8RMk/oHGm+u3KEDKg9lygnIdO4nQMUWy6C7jj8RjEic=; b=bodIUL0Is6mb8MAJO4BUgh+VmZQ1HWYkUTPb5XGpHvdTiL2C4DqIMW6OveUiG7JPULqauw uJNMAMGq1i70blX9TuyhgiHWniO8RMsDlboslPqyUBCTZqos8yilqPx1mVA8Qm83iHk/lp F6B8f37ALq2bO1FVzaJDtuVe0V3GOFw= From: Shakeel Butt To: Andrew Morton , Vlastimil Babka Cc: Yosry Ahmed , Roman Gushchin , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Meta kernel team Subject: [PATCH] memcg: slub: fix SUnreclaim for post charged objects Date: Mon, 9 Dec 2024 20:06:57 -0800 Message-ID: <20241210040657.3441287-1-shakeel.butt@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Large kmalloc directly allocates from the page allocator and then use lruvec_stat_mod_folio() to increment the unreclaimable slab stats for global and memcg. However when post memcg charging of slab objects was added in commit 9028cdeb38e1 ("memcg: add charging of already allocated slab objects"), it missed to correctly handle the unreclaimable slab stats for memcg. One user visisble effect of that bug is that the node level unreclaimable slab stat will work correctly but the memcg level stat can underflow as kernel correctly handles the free path but the charge path missed to increment the memcg level unreclaimable slab stat. Let's fix by correctly handle in the post charge code path. Fixes: 9028cdeb38e1 ("memcg: add charging of already allocated slab objects= ") Signed-off-by: Shakeel Butt --- mm/slub.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index f62c829b7b6b..88bf2bf51bd6 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2189,9 +2189,24 @@ bool memcg_slab_post_charge(void *p, gfp_t flags) =20 folio =3D virt_to_folio(p); if (!folio_test_slab(folio)) { - return folio_memcg_kmem(folio) || - (__memcg_kmem_charge_page(folio_page(folio, 0), flags, - folio_order(folio)) =3D=3D 0); + int size; + + if (folio_memcg_kmem(folio)) + return true; + + if (__memcg_kmem_charge_page(folio_page(folio, 0), flags, + folio_order(folio))) + return false; + + /* + * This folio has already been accounted in the global stats but + * not in the memcg stats. So, subtract from the global and use + * the interface which adds to both global and memcg stats. + */ + size =3D folio_size(folio); + node_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, -size); + lruvec_stat_mod_folio(folio, NR_SLAB_UNRECLAIMABLE_B, size); + return true; } =20 slab =3D folio_slab(folio); --=20 2.43.5