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 E1AA8C54E49 for ; Mon, 26 Feb 2024 12:57:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 21E09440156; Mon, 26 Feb 2024 07:57:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1CE6E440147; Mon, 26 Feb 2024 07:57:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E464440156; Mon, 26 Feb 2024 07:57:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id F3A54440147 for ; Mon, 26 Feb 2024 07:57:30 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C5E45A085D for ; Mon, 26 Feb 2024 12:57:30 +0000 (UTC) X-FDA: 81833956260.06.28FB3B7 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf19.hostedemail.com (Postfix) with ESMTP id 1D0DA1A0019 for ; Mon, 26 Feb 2024 12:57:28 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708952249; 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; bh=QTeRvZkORPiF6/Mf/A69JhvxVqqT4usTeRFQlATswHI=; b=pdcyQWmTKblHeynpwf53V9P6DRm/+tgKYyuX9bL/0ujBxI4GnvGgWCI+RwFlqTSVCcDzcT wkwk2DQX2G20b7+E7yFvd8T5OW2GCPRTbkyiRQhskIhX0WH+I5/XkpKkkiaJBpeUqvd68U bSclICPZU4jSxMR0lSw5UZJCspgiV5s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708952249; a=rsa-sha256; cv=none; b=pOcqBVGuPRe4YXq/Fa0O7PM2/G+qI1PIIwuwpFiXftWCA+8GzTH8Xtd00aJ2Ag8gp0wHrD SRByfDbehPoKr32n27q5lBfh2p3rAiRPupaYuexhocie7tsVzrXvUTVrpGTN5Z+ld9FHnL bhnve2wOzc+vD6/pSHENpC4jLDU25z0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0EA2DDA7; Mon, 26 Feb 2024 04:58:07 -0800 (PST) Received: from [10.57.67.4] (unknown [10.57.67.4]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 40F243F762; Mon, 26 Feb 2024 04:57:25 -0800 (PST) Message-ID: <8b909691-ca53-43b9-aab1-dba3ef3577cd@arm.com> Date: Mon, 26 Feb 2024 12:57:23 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] mm/madvise: enhance lazyfreeing with mTHP in madvise_free Content-Language: en-GB To: Lance Yang , fengwei.yin@intel.com Cc: akpm@linux-foundation.org, david@redhat.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mhocko@suse.com, minchan@kernel.org, peterx@redhat.com, shy828301@gmail.com, songmuchun@bytedance.com, wangkefeng.wang@huawei.com, zokeefe@google.com, 21cnbao@gmail.com References: <20240226083526.26002-1-ioworker0@gmail.com> From: Ryan Roberts In-Reply-To: <20240226083526.26002-1-ioworker0@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 1D0DA1A0019 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 19ap3wd9e1zy44mcn7abq79e9ihy4xot X-HE-Tag: 1708952248-603947 X-HE-Meta: U2FsdGVkX180z2AitENCYko9gcHiFE4lVFYNK8FEP2io8EgQQ1byN1XsT6aHO2GIsQgScgN5qidz6fO7IJXEOmZKYF6CvLwJw4NMUVUiwVk9U6YCNOrVV3wvRi7Z9ZvfKoi587DXhn0DoIArFlnVwfx0sCMIhAYAVlSifpC+6IAkTR+aK1ZC0qJM/jGIcmebCIG2nSKIw0PPRAeRCCwixjI7zDxgpihvxSCTMn9Gp0rJosmxHNGS342OdqWB06WC8uZrgpRLa83RHTyjfOWOtRdUCp8o1KrQ7gDd7aLiXGQxlADtaZAB7UghGVNQJz71OwLQrBPZ64+w7syOK86u5yifc75vCq6fT/4LcRX1NbMIHINRdLDc8nn+ulSZfUfq0nUNtL188ECpbYZ15GPoklsViXrV47uYCzBwS1paQDEUxGZKuoDHlJw7bLpLziXPi7AsxnnGeOIu3e8faDoIAHbzYnNlxCRNeyvEb0CJMO05vMdPtbDLE1+eseAHmJtj/0EzsFoetko448Pmyfgo+ebEPq4NRNkQTJPra4ADVE8BDou9PHxuwc+q4u49kYpW0VOkhN0x38k4dOwG7iHxu7aBKSA5JV07xBim/xU8dNuK3B0VPX1MvYt5M0ETwGu4iZduIwP8GSWcMRrgr6SdGlZPu4Uy8vDRt6R68/UGgFEFj6o3FRR2nVLmLREjdi379l/ou2SvsEScWSW4KVb/JTBLyjxl0oiJ6Cz5p4TyxCoisod3Bg9WCagzjKbbNf+sn8DAY3EANod1veEzMoJCCM4OiKE9HnmssFUmbQxp3w1qTw6Hj1gFFC8unXqIjPnRSBwM907GBKrpoCQtxhFgI9b42SXbYpU4AoXL3Wd9Avaeej/3p+RA61+OkJm6E4tJnyHvCG7UPgA= 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: On 26/02/2024 08:35, Lance Yang wrote: > Hey Fengwei, > > Thanks for taking time to review! > >> On Mon, Feb 26, 2024 at 10:38 AM Yin Fengwei wrote: >>> On Sun, Feb 25, 2024 at 8:32 PM Lance Yang wrote: > [...] >>> --- a/mm/madvise.c >>> +++ b/mm/madvise.c >>> @@ -676,11 +676,43 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, >>> */ >>> if (folio_test_large(folio)) { >>> int err; >>> + unsigned long next_addr, align; >>> >>> - if (folio_estimated_sharers(folio) != 1) >>> - break; >>> - if (!folio_trylock(folio)) >>> - break; >>> + if (folio_estimated_sharers(folio) != 1 || >>> + !folio_trylock(folio)) >>> + goto skip_large_folio; >>> + >>> + align = folio_nr_pages(folio) * PAGE_SIZE; >>> + next_addr = ALIGN_DOWN(addr + align, align); >> There is a possible corner case: >> If there is a cow folio associated with this folio and the cow folio >> has smaller size than this folio for whatever reason, this change can't >> handle it correctly. > > Thanks for pointing that out; it's very helpful to me! > I made some changes. Could you please check if this corner case is now resolved? > > As a diff against this patch. > > diff --git a/mm/madvise.c b/mm/madvise.c > index bcbf56595a2e..c7aacc9f9536 100644 > --- a/mm/madvise.c > +++ b/mm/madvise.c > @@ -686,10 +686,12 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, > next_addr = ALIGN_DOWN(addr + align, align); > > /* > - * If we mark only the subpages as lazyfree, > - * split the large folio. > + * If we mark only the subpages as lazyfree, or > + * if there is a cow folio associated with this folio, > + * then split the large folio. > */ > - if (next_addr > end || next_addr - addr != align) > + if (next_addr > end || next_addr - addr != align || > + folio_total_mapcount(folio) != folio_nr_pages(folio)) I still don't think this is correct. I think you were previously assuming that if you see a page from a large folio then the whole large folio should be contiguously mapped? This new check doesn't validate that assumption reliably; you need to iterate through every pte to generate a batch, like David does in folio_pte_batch() for this to be safe. An example of when this check is insufficient; let's say you have a 4 page anon folio mapped contiguously in a process (total_mapcount=4). The process is forked (total_mapcount=8). Then each process munmaps the second 2 pages (total_mapcount=4). In place of the munmapped 2 pages, 2 new pages are mapped. Then call madvise. It's probably even easier to trigger for file-backed memory (I think this code path is used for both file and anon?) Thanks, Ryan > goto split_large_folio; > > /* > --- > > Thanks again for your time! > > Best, > Lance