From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7D79322B74 for ; Fri, 20 Mar 2026 06:57:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773989827; cv=none; b=DcIC3s8MZ532G5Ma1mE+PCaawDZ4fs5I/4Dn/X03CAhYt03IC5zuvXSAfLIBHwJ8nmGiw8AF8vbQ+ki+hFlO9r1pvN+0cxK+7dlt385MOQb7dix9RTOJak+hEwUJuF5RzH/bZM1Qy8Hg3cSlXV3nukgzOejMdEMyEtzYK+hgdgw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773989827; c=relaxed/simple; bh=VXUgKjck2i7SgfNeDlFFGz5vd71E1DyPDYhbwvi3bIY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=EWAsOhZuBBDJs8+cwhnQ74SmO3Uz10veEh5zRP2uNlpEQzRjkhmcKvjnAQRd5bhs4IuTFZZA1pQzt7+7tVr+ohOfrq3U460SpkMrdxrhisHVEj1L2spcIGhd+85nIyMzlkIKHmTluSRU4vIU3p4hS04STA0DF8putoohcT/8eOg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=np4KfTp3; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="np4KfTp3" Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-35bb7afdc38so1060124a91.1 for ; Thu, 19 Mar 2026 23:57:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773989826; x=1774594626; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=uB3o3s0T22p//pHJUM9GCy7rvolCUexjSjOz2UT6Tjw=; b=np4KfTp3pExLjUWAoY81VK6MOA9lqbec0Ih/X6xYcxCnsHynYxKBtsYqwBs1ZAyjHs SSsMw2bn4/wwH09oTSEbT/UIdprOjd8HCBepaAGIZFeEV+ivIe/5hHDCE/fLNycPvGcv TIoHHEU1majfU/imUF4Na3X12AxfkiW+aWbkvSqLYZk6+AleXFS38fbze/MoTKGZlLB0 +Lw3kMMtQayPUP2vMrFPP/gc8XTzJX7m76EAQw1zGcrEkCwCjam0ZQ8Gs8IS44hBLDjJ qNCVB5DtP/QW6AmxeGxOD82nZKfVZ4YDTWTiCIlpiBhF15HrUNyTn4S44Fn1DAeHfXC6 x0FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773989826; x=1774594626; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uB3o3s0T22p//pHJUM9GCy7rvolCUexjSjOz2UT6Tjw=; b=GLRbE4dkVWbgNHoZBrbn547CbLQAHZuaGPeuUbmIg7d1ReDvVzA8knurz8X9IRjGs9 O0pjEwN7PaTZN68KH41uSm7MgfcllUJ9skLbEQ22Zy1VGRjXxx1lFPOnXzhWhTDJvGCw KnEsaeRjx0namgXnucvwXyzULnmFb8g7cj8RkrvAjpwnyw55F+uhxJntlOrxw3biHZAB K4OqH5WAEezxtbDnPiv6uEszf99gCRUhZtDhTexUhtmub3B7ees5slkDG8KuWQb8ODwK tYsgq1Gm7SI7788lISbMLqbKii3EbbSpuJqjY8RJGXFxjB2kJqSDegpwk99SB+2QH0Dg jTDQ== X-Forwarded-Encrypted: i=1; AJvYcCUDastFU39K4994D/1KjzRdsRHoVgasOXYqd/N+J2RjcC9FHG8PDRUVfS4gjBlA57tbgh75/4+P8O60F/M=@vger.kernel.org X-Gm-Message-State: AOJu0Yxyt1AAqYYtIg8rOendsgcznbRTbtkqzVJZmudGIMAuxyruHaHa 4v6a1nnmuIhOWrinlf3so7R2aR626GpACIstI83COnCTgfZ3oTAoAXi/ X-Gm-Gg: ATEYQzyda4zY3bPyyQlFkohYEIdlE5KLuFwaxbiY7Cav3mBZGrH2UxWfSTMQUZVlSb5 Bj4oygzugW8+mJEQ12uuFf0+6SNzAeVQIbDnUw6AHbYe15jf6DCIDZRJt78jBsSsCiqkEU/eTm4 LanvAqlb97aJXnkJ1xj8Lv92ZkE561rtBGyCBeZquxHR4RJTdcTQDoUR4bkCnUVHK95cUHeixTm MjgwUuoSScn4dNtLLlxju8xDPeLfcQ+SEPjjPSB3QeTxNpF38Xv4F/6iKRN3ldDokSsT2zkvk6A T3eK+FabGjuj7FNXTcX3/zb+W7tWO0gLxEvgAv0fkBRFJgG0lRecQreMjxryan6W2Uy24k7wQCk VMBh9iwlZuQTt6MqclNR/aHLD4iIrIfuObZucKVlHL1m6vxshLmT9gjGPKhpyFA54PtOqOecsRa MQvC9u6o/DfEeUn9tU61pk9aUuqyzXCCMlyZFlhDmFxlB6slGjnwcFPMRAxPJA89tzplpS X-Received: by 2002:a17:90a:ec8e:b0:35b:9777:8bb1 with SMTP id 98e67ed59e1d1-35bd2cb2acbmr1551565a91.19.1773989826091; Thu, 19 Mar 2026 23:57:06 -0700 (PDT) Received: from KASONG-MC4 ([43.132.141.25]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-35bd41347f0sm948262a91.17.2026.03.19.23.57.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Mar 2026 23:57:05 -0700 (PDT) Date: Fri, 20 Mar 2026 14:57:00 +0800 From: Kairui Song To: Alexandre Ghiti Cc: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , hannes@cmpxchg.org, kernel-team@meta.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/swap: fix swap cache memcg accounting Message-ID: References: <20260320050601.1833108-1-alex@ghiti.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260320050601.1833108-1-alex@ghiti.fr> On Fri, Mar 20, 2026 at 06:05:59AM +0800, Alexandre Ghiti wrote: > The swap readahead path was recently refactored and while doing this, > the order between the charging of the folio in the memcg and the addition > of the folio in the swap cache was inverted. > > Since the accounting of the folio is done while adding the folio to the > swap cache and the folio is not charged in the memcg yet, the accounting > is then done at the node level, which is wrong. > > Fix this by charging the folio in the memcg before adding it to the swap cache. > > Fixes: 2732acda82c9 ("mm, swap: use swap cache as the swap in synchronize layer") > Signed-off-by: Alexandre Ghiti > --- > mm/swap_state.c | 9 ++++----- > 1 file changed, 4 insertions(+), 5 deletions(-) > > diff --git a/mm/swap_state.c b/mm/swap_state.c > index 6d0eef7470be..48aff2c917c0 100644 > --- a/mm/swap_state.c > +++ b/mm/swap_state.c > @@ -494,6 +494,10 @@ static struct folio *__swap_cache_prepare_and_add(swp_entry_t entry, > > __folio_set_locked(folio); > __folio_set_swapbacked(folio); > + > + if (!charged && mem_cgroup_swapin_charge_folio(folio, NULL, gfp, entry)) > + goto failed; > + Thanks! I was worrying charging it first could cause thrashing, raced swapin will cause multiple charge and failed one will uncharge, so I put the charge after insert into cache, completely forgot about the lruvec static info part. For a quick fix this this is clean and good enough, and it doesn't effect the most performance sensitive part, mTHP and ZRAM would have `charged == true` here so they are not effected. Later we can move the lruvec static info out of __swap_cache_add_folio after the charge, maybe I'll do this with someother follow up optimzation together, then everything should be perfect. And I think we need Cc stable here. Acked-by: Kairui Song