From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7B2DC54E58 for ; Wed, 13 Mar 2024 09:24:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BD948001A; Wed, 13 Mar 2024 05:24:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46D01940010; Wed, 13 Mar 2024 05:24:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3360D8001A; Wed, 13 Mar 2024 05:24:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1D50F940010 for ; Wed, 13 Mar 2024 05:24:49 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E98D2140EB6 for ; Wed, 13 Mar 2024 09:24:48 +0000 (UTC) X-FDA: 81891481056.05.2CB204B Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.45]) by imf13.hostedemail.com (Postfix) with ESMTP id 4332820008 for ; Wed, 13 Mar 2024 09:24:47 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hnadUPl2; spf=pass (imf13.hostedemail.com: domain of chuanhuahan@gmail.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=chuanhuahan@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710321887; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PPD+cxv+0qUBoucY+ZqtBX2TszFOHU0DupeB2/oO4k0=; b=EYs0NPzyU6FUV8Rzy2n8Lqf/4P4E4cNUel8WPY1jEz/USMGgrC9JJRYxhO4v6DxVg5QB52 IoM1lFwpT8BGbsMrKtxN4U0MPUNwBwAzLrm9PofriTZ9kHv2d3JzZLJ0Q2NBqveIYT5BQQ 3/bIVZnx7gDooYzzLR2Nc0A1o5YYVy4= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hnadUPl2; spf=pass (imf13.hostedemail.com: domain of chuanhuahan@gmail.com designates 209.85.208.45 as permitted sender) smtp.mailfrom=chuanhuahan@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710321887; a=rsa-sha256; cv=none; b=OVkfSv0pPTrNOlnstRzwdQfPdx3eNw5xxzurQ6WzBj/+YtSJ++pHLm2Ao7wZ+fAmjY4RnC sIY+v/TLT2EnqwfEY2N+7kaGbk0uDpjvyfQ5b+lhPvCwCoFneUlCw8XIfGXlTMt3wT8F3F h+kxAudZPvHbFwe0K3GK+aaPhIv3U04= Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5687feeb1feso971997a12.2 for ; Wed, 13 Mar 2024 02:24:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710321886; x=1710926686; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=PPD+cxv+0qUBoucY+ZqtBX2TszFOHU0DupeB2/oO4k0=; b=hnadUPl2RlcgHUwuiAKG075fE88LH0Fe8+DCZEKOYckZi6oakdkO19zOKbP/YZ5sHs ufGuAcG3QFs/0nwzzrYxPE7c7ul8g6tbLwwz5oCdb6RkJRpDH2Clvfmwl4zNAjTr2YM+ GFdHaY2wndgya4iwqaONKkNhN04ILZGLzR1vPtIcTgH93b1Tk80vMGkSfWNeyvIGMUCz ap7rXafJBTr0oyNVkbOyyWbiWJR2JRdibHL5s7+fP2qi3rukqDDqOqYAbbEdw/brUa2O mpEtN4qq30E/6QGPKgqtfrz34EjhzQeGdHZOxl+WUef0M/i1w0C42NmE33U4XEUzhR5y hjng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710321886; x=1710926686; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PPD+cxv+0qUBoucY+ZqtBX2TszFOHU0DupeB2/oO4k0=; b=oa8je1xJhQLyfAR0u0qgw0xtnmhAREgLW8GqSojHYkaA6f55MvIg5KiZdNtEybpRQ9 +QN/3qE8+j2kLvxxKjfW+IRaTKzUUkn09lj37UCamOKReSExFwY+HRLE/steRw8UqksQ XjnumYLMFDF2a3cAhB09NTeb4NENY++aXeRsWORAx8R1UprjoY8pinuACSftFFtiiOOB AjwkOkgTrqgVUjCjyR9y0HUV3WRlYdcVXk+nQVgSZGaRPYKI6ylvgF5EQtJOwqqbJUnE WIrWcIXyAtmD22Q+FDh+OEUnfgl/A5YDQKV7Lax1LrjeRjnfe23VwZetLxvJkBGdTAU4 mGOQ== X-Forwarded-Encrypted: i=1; AJvYcCVN1YL+cKSXhoQCvG1WUrIj4kG7cZwskBeqavYm0Y7lWVg50OlDQKEa8j4HRhXhqRwP9PlsqkpXWbADooeJzQmO0Kg= X-Gm-Message-State: AOJu0YyBcyHeTltNEFQNHahbEl2oeLjMSFFsZu97sHB9WmIFk4ygXdi5 PQ6JYUmRwpo0lns+weu4PuadY4IHrRugD3WeTz/hauuxXv6E1Thry3FS2rN0ZLXxr9tp0je76x/ G+ZiX4bHjW4U1EQ30bDIqQ4oii9E= X-Google-Smtp-Source: AGHT+IFD54pe1kRDZc3skthbk+PAXH90rU1buFJKLdAnOrx/jHLKY+yJZGAokd4mO7aP2jrPKgE4eb/8Fb3Vx4SnwGA= X-Received: by 2002:a50:c01b:0:b0:566:43ab:8b78 with SMTP id r27-20020a50c01b000000b0056643ab8b78mr7925871edb.30.1710321885387; Wed, 13 Mar 2024 02:24:45 -0700 (PDT) MIME-Version: 1.0 References: <20240304081348.197341-1-21cnbao@gmail.com> <20240304081348.197341-4-21cnbao@gmail.com> <24dc6251-8582-790f-bbd3-465deed946f5@oppo.com> In-Reply-To: From: Chuanhua Han Date: Wed, 13 Mar 2024 17:24:33 +0800 Message-ID: Subject: Re: [RFC PATCH v3 3/5] mm: swap: make should_try_to_free_swap() support large-folio To: Ryan Roberts Cc: Chuanhua Han , Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org, chengming.zhou@linux.dev, chrisl@kernel.org, david@redhat.com, hannes@cmpxchg.org, kasong@tencent.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, mhocko@suse.com, nphamcs@gmail.com, shy828301@gmail.com, steven.price@arm.com, surenb@google.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yosryahmed@google.com, yuzhao@google.com, Barry Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4332820008 X-Rspam-User: X-Stat-Signature: oqhhq9ef1fi7c7hjox15pfbufqojzihm X-Rspamd-Server: rspam01 X-HE-Tag: 1710321887-497818 X-HE-Meta: U2FsdGVkX19NPDs3MIfhxbmSCy4Ym4NybSIAv93GddDkF3tvjNvdrfIpgWMm6LwD4dB4nu53Zuon+kJf4VSc9x0wNedo+LlXSIZBHqM/SkJKIEBo4GUdx3Corla0oByPSkcORg8nOV5aNDGKkaS9xoTon4Te8gm8Y1p7jEfzabrk6e1SNvn7Vik+e+R6zhQF/GOMfV7wCJZFugrXdjyZORou5k67wBkSJ+r0bKzQtWUcqOozIDrC7ZOBsHZwFKfNsb5QD+Q80WfGYGfRaIrqdh8vL7yisvapWOJtx6l+9wLxjzCooIIjyJruaSD/IvtP79y0OmtEucLlYyIql+nErTNlg1aO9pl0lX+81xHnuxi3APtuQWR5cA6at+kSR2thhxu4WCGGsdgxKeipjyfzaU5cgqRhmQh1IWCC5TLNZ1EdqyR/d0G3CmpLDZ58jfGX6uQP4LC0EiyPhnMifTinaSc2nHSLIGetN9/B8GMkm5yOD9T7fiuBFFGxGEMNBUNyJpupuNK9KbmWo2sI99mWNXHWOkBxyqA5n1m23EvjGedmFtPvHCyxlv5tWPa+I5/09vlBq5OEYJzGY5HrjSVUakcP23jB+1t32lbACqzUAW90+/yV3KJqDuGRBFW2XaimiimJmSW2lgbfXpVySr+ZqtWH6q44tcp94GCyt4uF2+/6h9sa+FgeZZKytXYvWCoZXmiAO+SDXV2I6FT5UqJSUCl+ABVhjgUkhAUf/8mbV/r5Mo6NjC5wz0TzNy8fWDIUu6myRUIiIxlAsjRPDVDKYWTgu6P9swhiah3MArb2uPj96h8v1aPOB8iFWtE/PglqKVCNkB86BLfR0Fk5GXFH+LqzESPqGx3Gc85bTIeUNYokqKg1Vml6BmemY2Ee4NxZMFMvCPjIlRWhmnEbR1XVJBLUqGdguoa2zIyKKLm6r9MBY3J+oxxUJHkYCntzc3+dHfUbVTma+hpY36kSRzL ZBcZAIPK SnB+iWaudgX5iZts+IAMY1HEW+pnoofD5nATEYnv16J2Qx7nQQ5mxdjUBn/hxC9ENM2Um/DpCxXATrjf0/ho1yjp1MQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Hi Ryan, Ryan Roberts =E4=BA=8E2024=E5=B9=B43=E6=9C=8813=E6= =97=A5=E5=91=A8=E4=B8=89 17:10=E5=86=99=E9=81=93=EF=BC=9A > > On 13/03/2024 02:21, Chuanhua Han wrote: > > hi, Ryan Roberts > > > > =E5=9C=A8 2024/3/12 20:34, Ryan Roberts =E5=86=99=E9=81=93: > >> On 04/03/2024 08:13, Barry Song wrote: > >>> From: Chuanhua Han > >>> > >>> should_try_to_free_swap() works with an assumption that swap-in is al= ways done > >>> at normal page granularity, aka, folio_nr_pages =3D 1. To support lar= ge folio > >>> swap-in, this patch removes the assumption. > >>> > >>> Signed-off-by: Chuanhua Han > >>> Co-developed-by: Barry Song > >>> Signed-off-by: Barry Song > >>> Acked-by: Chris Li > >>> --- > >>> mm/memory.c | 2 +- > >>> 1 file changed, 1 insertion(+), 1 deletion(-) > >>> > >>> diff --git a/mm/memory.c b/mm/memory.c > >>> index abd4f33d62c9..e0d34d705e07 100644 > >>> --- a/mm/memory.c > >>> +++ b/mm/memory.c > >>> @@ -3837,7 +3837,7 @@ static inline bool should_try_to_free_swap(stru= ct folio *folio, > >>> * reference only in case it's likely that we'll be the exlusive = user. > >>> */ > >>> return (fault_flags & FAULT_FLAG_WRITE) && !folio_test_ksm(folio)= && > >>> - folio_ref_count(folio) =3D=3D 2; > >>> + folio_ref_count(folio) =3D=3D (1 + folio_nr_pages(folio))= ; > >> I don't think this is correct; one reference has just been added to th= e folio in > >> do_swap_page(), either by getting from swapcache (swap_cache_get_folio= ()) or by > >> allocating. If it came from the swapcache, it could be a large folio, = because we > >> swapped out a large folio and never removed it from swapcache. But in = that case, > >> others may have partially mapped it, so the refcount could legitimatel= y equal > >> the number of pages while still not being exclusively mapped. > >> > >> I'm guessing this logic is trying to estimate when we are likely exclu= sive so > >> that we remove from swapcache (release ref) and can then reuse rather = than CoW > >> the folio? The main CoW path currently CoWs page-by-page even for larg= e folios, > >> and with Barry's recent patch, even the last page gets copied. So not = sure what > >> this change is really trying to achieve? > >> > > First, if it is a large folio in the swap cache, then its refcont is at > > least folio_nr_pages(folio) : > > Ahh! Sorry, I had it backwards - was thinking there would be 1 ref for th= e swap > cache, and you were assuming 1 ref per page taken by do_swap_page(). I > understand now. On this basis: > > Reviewed-by: Ryan Roberts Thank you for your review! > > > > > > > For example, in add_to_swap_cache path: > > > > int add_to_swap_cache(struct folio *folio, swp_entry_t entry, > > gfp_t gfp, void **shadowp) > > { > > struct address_space *address_space =3D swap_address_space(entr= y); > > pgoff_t idx =3D swp_offset(entry); > > XA_STATE_ORDER(xas, &address_space->i_pages, idx, > > folio_order(folio)); > > unsigned long i, nr =3D folio_nr_pages(folio); <--- > > void *old; > > ... > > folio_ref_add(folio, nr); <--- > > folio_set_swapcache(folio); > > ... > > } > > > > > > * > > > > Then in the do_swap_page path: > > > > * if (should_try_to_free_swap(folio, vma, vmf->flags)) > > folio_free_swap(folio); > > * > > > > * It also indicates that only folio in the swap cache will call > > folio_free_swap > > * to delete it from the swap cache, So I feel like this patch is > > necessary!? =F0=9F=98=81 > > > >>> } > >>> > >>> static vm_fault_t pte_marker_clear(struct vm_fault *vmf) > > > > Thanks, > > > > Chuanhua > > > > Thanks, Chuanhua