Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1451106lql; Tue, 12 Mar 2024 19:22:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVFS9/dBKAm5d7tUVSp6n+3cborQpPAwWxfDUE5hcuwzCo3CnkyjuwjnEHyzdrpUCHXHM0U+EUpM4cUrXIFY+DrIGv1I9mfuTY71XRE3A== X-Google-Smtp-Source: AGHT+IHcb0h7TrjyTMwjkCXDy042E57mntsjQxX476PVS7+AL/WcFRJzw3DqaIl6Tay2xfwIp37o X-Received: by 2002:ad4:4430:0:b0:68f:e5ee:5225 with SMTP id e16-20020ad44430000000b0068fe5ee5225mr2051100qvt.45.1710296520115; Tue, 12 Mar 2024 19:22:00 -0700 (PDT) Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id a1-20020a05620a16c100b0078820cca87fsi468156qkn.320.2024.03.12.19.21.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 19:22:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-101047-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@oppo.com header.s=selector1 header.b=BPLAt505; arc=fail (signature failed); spf=pass (google.com: domain of linux-kernel+bounces-101047-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101047-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=oppo.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BA2961C21B2D for ; Wed, 13 Mar 2024 02:21:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0038CBE68; Wed, 13 Mar 2024 02:21:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=oppo.com header.i=@oppo.com header.b="BPLAt505" Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2114.outbound.protection.outlook.com [40.107.117.114]) (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 B3FB2A93C for ; Wed, 13 Mar 2024 02:21:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.117.114 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710296512; cv=fail; b=au4pr662LqTg6y1fK1/9hJdMl/Ok2sqlx3IDr302g+oBlIlmRiAgUswxHrf2eKtRw5CMS5wqM2IWIigimwBrd4LCSLGMFfvExmmPR/zbGlgn0XZWBO4ED1iySmg5NvnUNBLeFPGMT1XM1OqGLY8kK8UAIQLknwh3bm2qQf9xFM8= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710296512; c=relaxed/simple; bh=LSrM7qcc8Ya2eWifx7L/EfQ3MSGfb4GRJWsacN9s2lY=; h=Message-ID:Date:Subject:To:Cc:References:From:In-Reply-To: Content-Type:MIME-Version; b=PAEHO4IGCJPhbI8TFZZEOzEt9dRayqaiAW4NwLrXyz/UX9HME312El8hQq2JgQRb2fR631lnrGRRl1hZyUPvtDpdsKAmkWeIWXoSi176IMh5ZGmxnmgkIRjWqH75f5E0RBvBFb7rhDJL0jXtV+H7MFORJvk8/4eZYzEordwTLQA= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=oppo.com; spf=pass smtp.mailfrom=oppo.com; dkim=pass (1024-bit key) header.d=oppo.com header.i=@oppo.com header.b=BPLAt505; arc=fail smtp.client-ip=40.107.117.114 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=oppo.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oppo.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C4OtPPwhW316t3HVWJ68w0+nhhXvx5K2MCqGBiqtv/VS1WuVuOGlMfoxz/gH2FESIR+EX42GqEWiLNxEnqt15Pir5oC8SYeEQYjYNx0Wo3qUuTUoe2+nLLsEhXSRGLGIR9YdU7GY0IV3fBirXpDJmo/sXhdGST6ZgztmpYURuLsT8eMOW4E828JGvk4neagikE8Th2tXLELI5d1BsDE5/O+8akj8OYVyyfuqFv0b69jE5rR+SGNPf2N2SP8W7XEMddp1ks8OCc1lsFz8pmrTSX3WGFRHYFP5vTonF3RYdBBoCtTKNIhg5WDlqR1IeTngtjUNpT3yay22eLmZEJXtug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0vaZukkDp9co5SInJKdHik64MwM4f/k/Kl1Mu3M6RjU=; b=nULSovSBD1H695hjMnofFg3L7jk6byLIBk7zotc+tP+TA5bFNOFi+pQxPYNH6VpgB9Amhaxo9gfBCCHEI/4454kEvU7ZK1s+gbc3U0Dfrg40KAaIkzAt5EFQnpam3vgJ/0EGMFqDRCEsxQr2l2kyV4isW4dQOiztrw7/k9lUv9GpAR85zshix+A9Q9FLEROgpTWstAXje7SElic/hm4huMx6eM6ETUY3ZxXjI4DE8pbam3FpMuyJL9cUo+gnAbCDfQAzdrVJHF3vobqajgnRdsL0YuDBlixVE9DYJsOIDUc/0vWWf/f0fQ2mWQq6uEfvJYcHCeh6F/JYfMejk1qXQQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oppo.com; dmarc=pass action=none header.from=oppo.com; dkim=pass header.d=oppo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oppo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0vaZukkDp9co5SInJKdHik64MwM4f/k/Kl1Mu3M6RjU=; b=BPLAt505pYpkqzz71nmFfZrXVjgz++TAqLiPxKWlGrDdNzNdOO89A7uT48XxNaObrk3LABehmN6z1OsJwk311EhcOAlJljp9JahA8/5gG8b4Q+hswd5ZrE6e1FcRAdH16E7pbZlM8rPUn3+OpujeLATGCx5uirmQWVqqtnNZsrQ= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oppo.com; Received: from TYZPR02MB7988.apcprd02.prod.outlook.com (2603:1096:405:aa::5) by SEZPR02MB5614.apcprd02.prod.outlook.com (2603:1096:101:39::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7362.35; Wed, 13 Mar 2024 02:21:47 +0000 Received: from TYZPR02MB7988.apcprd02.prod.outlook.com ([fe80::9aeb:5c84:e751:1bad]) by TYZPR02MB7988.apcprd02.prod.outlook.com ([fe80::9aeb:5c84:e751:1bad%5]) with mapi id 15.20.7362.031; Wed, 13 Mar 2024 02:21:47 +0000 Message-ID: <24dc6251-8582-790f-bbd3-465deed946f5@oppo.com> Date: Wed, 13 Mar 2024 10:21:42 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [RFC PATCH v3 3/5] mm: swap: make should_try_to_free_swap() support large-folio To: Ryan Roberts , Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org Cc: 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 References: <20240304081348.197341-1-21cnbao@gmail.com> <20240304081348.197341-4-21cnbao@gmail.com> From: Chuanhua Han In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ClientProxiedBy: SI1PR02CA0001.apcprd02.prod.outlook.com (2603:1096:4:1f7::15) To TYZPR02MB7988.apcprd02.prod.outlook.com (2603:1096:405:aa::5) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TYZPR02MB7988:EE_|SEZPR02MB5614:EE_ X-MS-Office365-Filtering-Correlation-Id: 6b548685-16f1-427f-9b66-08dc4304559b X-LD-Processed: f1905eb1-c353-41c5-9516-62b4a54b5ee6,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YHleEcUbHnfPNjsdxFnc69X/Vlf1i6Bqam8+e3QxrvBtaoMAhOJJN1f3zirruShEeeZZIBz4bNj5nRwiR9zjdPa9klJPexaqcSjKL2sdclb0zcsXrWcE97q+Dw0kamHjaD5BRHPfB9VDEBX1CgyxmaCBiYB2gEpdDpZH2GoFtVjPBEd4zwKOhJZD2vqFq//NsAxv9qAvGowSq0vAkE6cBs5lAKH8VNrfjOM29kiD9qRQ45P1NXg7AIRiXLZSjZbtuOwTs6v5caW8Z0IWcZK0/lhcJ9wXG6+E3wUzJq9sDLmIsd0EvrDVX8t3g6/621Fez9clk6urRl0sbAp95eUKyuPN6mt1otBt8McYwQ1aZSi7qUPOwREXNRv8qnehmFqcWBuLosOFg0lNEwBHn/0AKNimtEbmQ2Zg6FuH66+Ydg3FhZM4nT76TefAA+7jeYdlAJVE8vdKdYWaR9owfzy2uTcay90rsIOmKq8YFoV51hbCflpW4JC+/BuPcmte1cL/RBI7DIDyO0l3Ed90seC9ltfMSf3KWSmisOPxhKlJ0MitvnjXMEw423ZP9THo8rj+TX3nCxGvRzmowE9VxPmzigdtz6DXRzypr1VIDoFWb465KC3zdnKHRG8SuXmXvMZKQ5UBHq4Fcytl0MvyHZ+HGlelf++52afFzZJo/pYWon8= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TYZPR02MB7988.apcprd02.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376005)(7416005)(1800799015);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?dmlTcGYwOFIwc01xazNwb096cXZYZkJ2emVzSEc1WjQ3K29Mb2E3V0lJeXhP?= =?utf-8?B?UE4ydmY1aFBqZjJpMkx5RC9jWXJ4SXRsWnlndzlxTXc5bDFvRk9YY010cWpL?= =?utf-8?B?UERkNWV0Mk9PSjQ0UkRUSU9XMmxsY1BEWU5oSnEyUXR0aUpIaTh2Sy9tNUxs?= =?utf-8?B?dHJTMkNaRlhiaDNKMjh4cGF1dmFtNnoyZUpyWnlPaTh4R2NoNHZPNE5rdHRM?= =?utf-8?B?VVB3WlU1ZmhWNndjT0VTUFhNQ3o3SFk4K2ZrTFBBaWpkYUVOVSt0UE5heThV?= =?utf-8?B?RTNuTDBpNlNoWllqZEFraEFtRHduaWdBVXQwU1N2RTZsSU9NNWZ1UGhFS3FC?= =?utf-8?B?Vno5NlJONVNUMmxxS1NMTEc1ZHNROFJX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B?UXc9PQ==?= X-OriginatorOrg: oppo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6b548685-16f1-427f-9b66-08dc4304559b X-MS-Exchange-CrossTenant-AuthSource: TYZPR02MB7988.apcprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Mar 2024 02:21:47.7028 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f1905eb1-c353-41c5-9516-62b4a54b5ee6 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: WepdixPbOqA1MWeonBIFx3Klz6sYfZiTEIp7haz4PC60o0IVBEqUD2HrLZOnh+UwUCp4sLVz0UnayNFxixpC1w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEZPR02MB5614 hi, Ryan Roberts 在 2024/3/12 20:34, Ryan Roberts 写道: > 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 always done >> at normal page granularity, aka, folio_nr_pages = 1. To support large 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(struct 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) == 2; >> + folio_ref_count(folio) == (1 + folio_nr_pages(folio)); > I don't think this is correct; one reference has just been added to the 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 legitimately equal > the number of pages while still not being exclusively mapped. > > I'm guessing this logic is trying to estimate when we are likely exclusive 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 large 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) :   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 = swap_address_space(entry);         pgoff_t idx = swp_offset(entry);         XA_STATE_ORDER(xas, &address_space->i_pages, idx, folio_order(folio));         unsigned long i, nr = 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!? ???? >> } >> >> static vm_fault_t pte_marker_clear(struct vm_fault *vmf) Thanks, Chuanhua