Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp26212pxb; Fri, 19 Feb 2021 16:43:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJyKmTDTfNDnzoTmbuwcz6cBWwtWFl2Cl6FcJjL2TOuqc0xWkIxgWTXkksmY/kVD8Sbh1eMR X-Received: by 2002:a17:906:4c85:: with SMTP id q5mr10849942eju.375.1613781795399; Fri, 19 Feb 2021 16:43:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613781795; cv=none; d=google.com; s=arc-20160816; b=zGze45E9tH630E5EfIl+cPSr+FScsfEbv8fBCChCeW21sOd9TJ97Hj2sJKscm0uWxj IC5VOSSg1pnkshQjYDY7QcUPPj7wjMu3ZS9phCJDjAONJjZ8rK3bEBAO9XfkvTjYiisI 8Z/0ZGIqKuaZ/1sz9qabTd5RnHhNGmo9ucrgdTwR/4kom4/EZIwwn186n4iBKY5sMdwE 4A2gZ6B3sRkHEjmytRyRImUnoQfL4tc0Bp67gB4oCTRRHi+p3LdsqGxuT2Izn3DEyDwq t3GevQZvzwHuUcwz6j7l0qtl1U1ODmDpDlqiM1uMWxpT8PCMa1liIBVtSFabGCX9/pWG 3P3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=dddqDG5OH+l1ND7BjX6aGGlaSzT3Qqk6VPLYVIkB8Zk=; b=ZTFuYgJvyZDotCvSdDtA8JVc2TvBK4S/7JMFN5jeeLmXV/Q4U5gRToV9iT7iAUZwUF 7kUaRZLl4MVyXbvh8RJlS0tnfySIKXRzOLqmH4svFWPkNVFJ2bL/OADUM9wSNFwoMy4d UGd2FuV9aH+K3HgCJYwSj2LAWeKbFd9j9XGTY1zgVsyw1atgMQOXUmcJjG0oax0Ly9vF VdyzYTLuxYFmChgqvNCGfjOxbHMuj67CYVJzFbb1VE04t1chDugJFtAaxUhvO88eNpeF uf9t/fR7buuHHCmrY66PQj0jUrqj/Jb+DtTH6ctZ2uIa2zstF8+w3HBEb2qbJQGBytg7 AF3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=j3hoLdIc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id du8si6651646ejc.659.2021.02.19.16.42.29; Fri, 19 Feb 2021 16:43:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cmpxchg-org.20150623.gappssmtp.com header.s=20150623 header.b=j3hoLdIc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=cmpxchg.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229765AbhBTAft (ORCPT + 99 others); Fri, 19 Feb 2021 19:35:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229658AbhBTAfs (ORCPT ); Fri, 19 Feb 2021 19:35:48 -0500 Received: from mail-qv1-xf2f.google.com (mail-qv1-xf2f.google.com [IPv6:2607:f8b0:4864:20::f2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B0E3C061794 for ; Fri, 19 Feb 2021 16:34:31 -0800 (PST) Received: by mail-qv1-xf2f.google.com with SMTP id dr7so3527164qvb.1 for ; Fri, 19 Feb 2021 16:34:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=dddqDG5OH+l1ND7BjX6aGGlaSzT3Qqk6VPLYVIkB8Zk=; b=j3hoLdIcaOoMzPPzi5cxfKzQ6iy0+AxlZBenkTInEAaFtLcL7CAtbl/oK5f/dMNtoU +wzNPu7pcJjBgE5T+v/+W/Z094r37wQbBGmS1/kYm+DgOsw+36T7wGKBlwApDIK+VA3X 3/o7H8q8f4SmFsjr+R9Q6kPeVToT8SBYBaPpBRFrATTV6AToiYmxPWkmzhf71RcHVHU4 QZZfcHEMxDHzTj2lg3wGzIe4j3yX2rvNk0L/sB9iTk9mfdmQbjFexGECkA4vF/ixM7Hu G0fAWXTfnaIe5vEhXkeleTdZQRORgGrA9GYOxnX/TyDLoUF2GtgLWJdRTzDnXve8LS/W 5NXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=dddqDG5OH+l1ND7BjX6aGGlaSzT3Qqk6VPLYVIkB8Zk=; b=ncqrPkU87CSCGgD5UJKsb684ISopEryG3e+sUGkGCpaVp2wF5KfpJY/lyJL0m9wVSp OKJKTgAalFYN850r6Q+luVclqT8Er5QaAB7tTG2OCW8wDSNFPQ5gWm0g2XUf9lQjR7U4 O34mRTwsGSm2UA2oPrUNZRT7YZ1R9AwEZEHkHZaeL4CUqhFBSnLh+x/maO7bQ2vraPQp 8/RnHTjub1e7bnlcFW4bDRKHPduMOl+l0o2jSup4ZDuuQdWoDjbfIskXTGjmxqA2TbuF CNur86kXiSmPYYCkCfSg2+X+tDPtEt5BXIEJapdk7W/OYnmFlIb9YBlLJOHddnudmNA5 ynEQ== X-Gm-Message-State: AOAM531VmiP/2ztF4Jm+WEyIUUkOVO5PY3wN+V6MRzNduFXgpWQR+mQm bztuh+KOYGEjCK5d6SU7RngtZYmoO2djfg== X-Received: by 2002:a0c:aece:: with SMTP id n14mr2253955qvd.52.1613781270232; Fri, 19 Feb 2021 16:34:30 -0800 (PST) Received: from localhost (70.44.39.90.res-cmts.bus.ptd.net. [70.44.39.90]) by smtp.gmail.com with ESMTPSA id r2sm6445507qti.4.2021.02.19.16.34.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Feb 2021 16:34:29 -0800 (PST) Date: Fri, 19 Feb 2021 19:34:29 -0500 From: Johannes Weiner To: Shakeel Butt Cc: Hugh Dickins , Roman Gushchin , Michal Hocko , Andrew Morton , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] memcg: charge before adding to swapcache on swapin Message-ID: References: <20210219224405.1544597-1-shakeelb@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210219224405.1544597-1-shakeelb@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 19, 2021 at 02:44:05PM -0800, Shakeel Butt wrote: > Currently the kernel adds the page, allocated for swapin, to the > swapcache before charging the page. This is fine but now we want a > per-memcg swapcache stat which is essential for folks who wants to > transparently migrate from cgroup v1's memsw to cgroup v2's memory and > swap counters. > > To correctly maintain the per-memcg swapcache stat, one option which > this patch has adopted is to charge the page before adding it to > swapcache. One challenge in this option is the failure case of > add_to_swap_cache() on which we need to undo the mem_cgroup_charge(). > Specifically undoing mem_cgroup_uncharge_swap() is not simple. > > This patch circumvent this specific issue by removing the failure path > of add_to_swap_cache() by providing __GFP_NOFAIL. Please note that in > this specific situation ENOMEM was the only possible failure of > add_to_swap_cache() which is removed by using __GFP_NOFAIL. > > Another option was to use __mod_memcg_lruvec_state(NR_SWAPCACHE) in > mem_cgroup_charge() but then we need to take of the do_swap_page() case > where synchronous swap devices bypass the swapcache. The do_swap_page() > already does hackery to set and reset PageSwapCache bit to make > mem_cgroup_charge() execute the swap accounting code and then we would > need to add additional parameter to tell to not touch NR_SWAPCACHE stat > as that code patch bypass swapcache. > > This patch added memcg charging API explicitly foe swapin pages and > cleaned up do_swap_page() to not set and reset PageSwapCache bit. > > Signed-off-by: Shakeel Butt The patch makes sense to me. While it extends the charge interface, I actually quite like that it charges the page earlier - before putting it into wider circulation. It's a step in the right direction. But IMO the semantics of mem_cgroup_charge_swapin_page() are a bit too fickle: the __GFP_NOFAIL in add_to_swap_cache() works around it, but having a must-not-fail-after-this line makes the code tricky to work on and error prone. It would be nicer to do a proper transaction sequence. > @@ -497,16 +497,15 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, > __SetPageLocked(page); > __SetPageSwapBacked(page); > > - /* May fail (-ENOMEM) if XArray node allocation failed. */ > - if (add_to_swap_cache(page, entry, gfp_mask & GFP_RECLAIM_MASK, &shadow)) { > - put_swap_page(page, entry); > + if (mem_cgroup_charge_swapin_page(page, NULL, gfp_mask, entry)) > goto fail_unlock; > - } > > - if (mem_cgroup_charge(page, NULL, gfp_mask)) { > - delete_from_swap_cache(page); > - goto fail_unlock; > - } > + /* > + * Use __GFP_NOFAIL to not worry about undoing the changes done by > + * mem_cgroup_charge_swapin_page() on failure of add_to_swap_cache(). > + */ > + add_to_swap_cache(page, entry, > + (gfp_mask|__GFP_NOFAIL) & GFP_RECLAIM_MASK, &shadow); How about: mem_cgroup_charge_swapin_page() add_to_swap_cache() mem_cgroup_finish_swapin_page() where finish_swapin_page() only uncharges the swap entry (on cgroup1) once the swap->memory transition is complete? Otherwise the patch looks good to me.