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 85EAEC77B7A for ; Tue, 23 May 2023 03:42:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE5B1900003; Mon, 22 May 2023 23:42:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A95C1900002; Mon, 22 May 2023 23:42:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98413900003; Mon, 22 May 2023 23:42:28 -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 8A2A7900002 for ; Mon, 22 May 2023 23:42:28 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4B025A067A for ; Tue, 23 May 2023 03:42:28 +0000 (UTC) X-FDA: 80820122376.28.CDFCDA6 Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) by imf30.hostedemail.com (Postfix) with ESMTP id 492AB80014 for ; Tue, 23 May 2023 03:42:24 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf30.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.111 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684813346; 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=OUtIm1YFfqc9rPfthkIXrO1GF20IzJtXJEGAnccgo38=; b=S31cCITmRSHt3JIiFhdCB6K1hSc5dinJg9eni6n5wQiLX6dmyI2MruZvR+ahl9mKr03J70 NFMOxCrYffpdBbpvyvIVzesR8Jfu4t6YBqrz5+Icj3LoKE3BoJR+GSBPQowNMSGYu8Xow7 DQUx4nUGx6LTgZmF9N/65HuOutxsGok= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf30.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.111 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684813346; a=rsa-sha256; cv=none; b=VMWAg4OIKryGpiRvFlJ3L0VgYNfG4gHISk+jTw6dDMuEwlzjuzcNqCQECjnDq9pMe3iAo7 KM8b7FOBU1tT5fOdew8jMRUSZ28LijyiX1jSaGMqVdIoyV0Vm84uFEGNIyg7T7QTnxDbWa lFBGuszGcIPcEM2PekOdiVY3ZDOoP6w= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R141e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046056;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0VjIRpOt_1684813339; Received: from 30.97.48.45(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0VjIRpOt_1684813339) by smtp.aliyun-inc.com; Tue, 23 May 2023 11:42:20 +0800 Message-ID: <4453e142-f2c5-7016-acc0-3c861b8d0a0d@linux.alibaba.com> Date: Tue, 23 May 2023 11:42:34 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH v3] mm, compaction: Skip all non-migratable pages during scan To: Khalid Aziz , akpm@linux-foundation.org Cc: willy@infradead.org, steven.sistare@oracle.com, ying.huang@intel.com, mgorman@techsingularity.net, khalid@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20230517161555.84776-1-khalid.aziz@oracle.com> From: Baolin Wang In-Reply-To: <20230517161555.84776-1-khalid.aziz@oracle.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Stat-Signature: 5ie16hacf1pukdk1hhzone6yn9wk7rd5 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 492AB80014 X-HE-Tag: 1684813344-192947 X-HE-Meta: U2FsdGVkX1+660Myw7Dh1C4zLjIAFkNbYOoT3FlYQOFZ9mhn7ZyTVU5pIB+w8j4jH2c8zD//2dR9InnSFXD9CMfsKR6bwIWOt0P90j6RlLs/fmRgxxOyNNWbQKUlRdKy684agW8fJ+cFSMZBcetm9ZuOKJbSQ8FCM2ClYxUzAYhu8oCkcZjeTYmuX7l1Q5zje1dDIVq4BES6clFZAPpE3MN9cZSCPHLNi2uhzJaV6ichuuOcIojUl8zF94xGV2RjxbAuB0v7Ns+Nohtx5e2ysFEyr5i/JXhAA8NsD0Gb7XlfsVDXa5zluIy6iRTzqKNF6qYitcG+rm7AQncCz6nVKlRAU5fB3Mlkvv2JGZSKK8jcAEgR4NMBGOmed6GBvKHvmF/SyrLOPP88XKiD5fcshXNQFCNNIcFaYQAGfFcwFJIlq0M1ep0wmI42alvV896H9IBZNjitLHVE7YNVxkEU4dx+p90p7T+fEo3lFUbi1si0IWrWP+7SdF8CZwtScIIxQ8xLHOqqdW2iqVXrAY7Zr6Xqn8fCS9Jg1ib4KKRSyfmyGId1ik3O1x4Fr5op/mU0aeUR/JaERbWjtcSlRRbAujtES+VJ02sMCNOB33H+z8u0lbcTXGcjsphcqGbzL3QVyc//b50kGmxUAdBVheukPi6hK32ZrkU/qdqvd9f9A20rBaDvrfY9fft6YqV8kJhiMHONRUSGleDNq65nI31QgGTH1zxr7kmG9TKouyvFOmUylJNJkdUe/Z+eawIYbxyhrSblI5MMnMtwuGV4pXT/vrhzOzCQX3SS1+HG65+xmLQQ4+qcMPdLRlocZ2rtivLsY/ydOOPndblolomhI4lmeY22pDVpK0dNnD8WYUasIaw7Q0Mhz93rfRAqE3ScjT51jhS6xUA3Vp4h0OpEHwXaNwnfKu2/9I2qJVYqeNibOryVp0i2NstJ4mr/0bwq5NCDc5uAWBuC9vGF2883ySh w12FjJjH 19JbFfy6FhCxv++MxkSfX2lmE59bWSuAtSScNWbc86XhTQdDCk/iOSV5HDR+0o6YLptWuZNsyfu6eKcS6AcyQRKUt1b1iuD2WYi/8/0oA2hdx9eI= 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: On 5/18/2023 12:15 AM, Khalid Aziz wrote: > Pages pinned in memory through extra refcounts can not be migrated. > Currently as isolate_migratepages_block() scans pages for > compaction, it skips any pinned anonymous pages. All non-migratable > pages should be skipped and not just the anonymous pinned pages. > This patch adds a check for extra refcounts on a page to determine > if the page can be migrated. This was seen as a real issue on a > customer workload where a large number of pages were pinned by vfio > on the host and any attempts to allocate hugepages resulted in > significant amount of cpu time spent in either direct compaction or > in kcompactd scanning vfio pinned pages over and over again that can > not be migrated. > > Signed-off-by: Khalid Aziz > Suggested-by: Steve Sistare > --- > v3: > - Account for extra ref added by get_page_unless_zero() earlier > in isolate_migratepages_block() (Suggested by Huang, Ying) > - Clean up computation of extra refs to be consistent > (Suggested by Huang, Ying) > > v2: > - Update comments in the code (Suggested by Andrew) > - Use PagePrivate() instead of page_has_private() (Suggested > by Matthew) > - Pass mapping to page_has_extrarefs() (Suggested by Matthew) > - Use page_ref_count() (Suggested by Matthew) > - Rename is_pinned_page() to reflect its function more > accurately (Suggested by Matthew) > > mm/compaction.c | 36 ++++++++++++++++++++++++++++++++---- > 1 file changed, 32 insertions(+), 4 deletions(-) > > diff --git a/mm/compaction.c b/mm/compaction.c > index 5a9501e0ae01..f04c00981172 100644 > --- a/mm/compaction.c > +++ b/mm/compaction.c > @@ -764,6 +764,34 @@ static bool too_many_isolated(pg_data_t *pgdat) > return too_many; > } > > +/* > + * Check if this base page should be skipped from isolation because > + * it has extra refcounts that will prevent it from being migrated. > + * This function is called for regular pages only, and not > + * for THP or hugetlbfs pages. This code is inspired by similar code > + * in migrate_vma_check_page(), can_split_folio() and > + * folio_migrate_mapping() > + */ > +static inline bool page_has_extra_refs(struct page *page) > +{ > + /* caller holds a ref already from get_page_unless_zero() */ > + unsigned long extra_refs = 1; > + > + /* anonymous page can have extra ref from swap cache */ > + if (PageAnon(page)) > + extra_refs += PageSwapCache(page) ? 1 : 0; > + else > + extra_refs += 1 + PagePrivate(page); > + > + /* > + * This is an admittedly racy check but good enough to determine > + * if a page is pinned and can not be migrated > + */ > + if ((page_ref_count(page) - extra_refs) > page_mapcount(page)) Could you explain why changing total_mapcount() to page_mapcount()? The original commit 829ae0f81ce0 ("mm: migrate: fix THP's mapcount on isolation") did fix a THP's mapcount issue with converting to total_mapcount(). > + return true; > + return false; > +} > + > /** > * isolate_migratepages_block() - isolate all migrate-able pages within > * a single pageblock > @@ -992,12 +1020,12 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, > goto isolate_fail; > > /* > - * Migration will fail if an anonymous page is pinned in memory, > - * so avoid taking lru_lock and isolating it unnecessarily in an > - * admittedly racy check. > + * Migration will fail if a page has extra refcounts > + * preventing it from migrating, so avoid taking > + * lru_lock and isolating it unnecessarily > */ > mapping = page_mapping(page); > - if (!mapping && (page_count(page) - 1) > total_mapcount(page)) > + if (page_has_extra_refs(page)) > goto isolate_fail_put; > > /*