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 518B9C43219 for ; Thu, 24 Nov 2022 10:09:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0CC86B0071; Thu, 24 Nov 2022 05:09:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DBC646B0072; Thu, 24 Nov 2022 05:09:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C5D546B0074; Thu, 24 Nov 2022 05:09:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B576C6B0071 for ; Thu, 24 Nov 2022 05:09:45 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8ED99120665 for ; Thu, 24 Nov 2022 10:09:45 +0000 (UTC) X-FDA: 80167914330.12.0D4AA91 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf29.hostedemail.com (Postfix) with ESMTP id B66A2120011 for ; Thu, 24 Nov 2022 10:09:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669284584; h=from:from: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=3kAYquvyXCoEwzo0E+846zCjz/I/oFJHnxyQt8aQxnE=; b=CZ0k5cyC/4fESQvmDVkX+yXUSL1tCDwzWdtlDL8JtIN7avTIklpMuDWGQPi7TybI47H2ZZ oid1KiY9mZfCz/s8EbpDLMCVp5zrwjP/U/jfx4KEVTfxy+IfgMKIRvdYyX7CXdOWDj+jgO wQgyCacr6KUwmR7yazD6N5bXtwLxfYI= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-612-mlLeNL1WOwKZb5tOyOc2nQ-1; Thu, 24 Nov 2022 05:09:43 -0500 X-MC-Unique: mlLeNL1WOwKZb5tOyOc2nQ-1 Received: by mail-wm1-f70.google.com with SMTP id l42-20020a05600c1d2a00b003cf8e70c1ecso2401424wms.4 for ; Thu, 24 Nov 2022 02:09:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3kAYquvyXCoEwzo0E+846zCjz/I/oFJHnxyQt8aQxnE=; b=LmMx3LoojsJJKmd8AtNmm5jBg6XLkowdKogHAndSmB00ZrJZrVLVegTsRQE17Wa1o+ YCNoXUWLTsBYA1FjVdvjTKbxZvshOVZSUxHY1lkaSD3LxdY248xhX8qvJVDniJuVJbLl 0dRvHlthRFKxosiZKpLVMX/eHJdiuugAlLTW3wq17PPY/oDpGH7Yfw+4oHBtWJ0wOV71 jcnWGUJiQOJ4YC7cKNnYgWSaU8/apDgbnFY9FAJtzKvlGj/yn5l6awNN0rxHZ8wNjKx+ 8HtUeUJ+57RhObqMehPdPiFVi1IMTAwUXrFJbPjOATZpD7HSSPbveiVsUzZe5uUtoZTS vtIA== X-Gm-Message-State: ANoB5pmvLlFHOOB6UjQFCwKgo93KUFnxrIbSwg+KVAFZrv7iHOHp7yFk ZfPwDmZpPwG+AKukef4KYCeevHm8dKfujhrh6/PoucO3TvZa1xK9fRIry7/mibQZOOylTmlNXMf sfQKFdSRQGL0= X-Received: by 2002:a05:600c:5541:b0:3cf:8896:e1d0 with SMTP id iz1-20020a05600c554100b003cf8896e1d0mr12515849wmb.177.1669284581735; Thu, 24 Nov 2022 02:09:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf5IiZfBe8A2j1NQ2KOLuiJeEg4fjtIovyFu3MMuo6cT8u7xmm5WMD3j+Z35QMr44JBCleOkOA== X-Received: by 2002:a05:600c:5541:b0:3cf:8896:e1d0 with SMTP id iz1-20020a05600c554100b003cf8896e1d0mr12515823wmb.177.1669284581266; Thu, 24 Nov 2022 02:09:41 -0800 (PST) Received: from ?IPV6:2003:cb:c704:2200:bfcb:7212:1370:de13? (p200300cbc7042200bfcb72121370de13.dip0.t-ipconnect.de. [2003:cb:c704:2200:bfcb:7212:1370:de13]) by smtp.gmail.com with ESMTPSA id k17-20020a05600c1c9100b003c6b874a0dfsm1582715wms.14.2022.11.24.02.09.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Nov 2022 02:09:40 -0800 (PST) Message-ID: <3c584ce6-dc8c-e0e4-c78f-b59dfff1fc13@redhat.com> Date: Thu, 24 Nov 2022 11:09:39 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 To: Gavin Shan , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, william.kucharski@oracle.com, ziy@nvidia.com, kirill.shutemov@linux.intel.com, zhenyzha@redhat.com, apopple@nvidia.com, hughd@google.com, willy@infradead.org, shan.gavin@gmail.com References: <20221124095523.31061-1-gshan@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2] mm: migrate: Fix THP's mapcount on isolation In-Reply-To: <20221124095523.31061-1-gshan@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669284585; a=rsa-sha256; cv=none; b=20z9VgttWJrMUIlCPaBbKbxFhtc6C/xaePnhmNct9rDgPEYFm6wEk5aUsPnC3nWmzsvAoC zLoiINCyBWPt2A8STFvJkxIK+fRSSkzwk1zzgReqBUSdpr3Z/HcTD7TxR57ZIjlM1kBUvu zfsBVo7sAc0KYSBboD3qW8TuqK0q2BE= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CZ0k5cyC; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669284585; 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=3kAYquvyXCoEwzo0E+846zCjz/I/oFJHnxyQt8aQxnE=; b=dGKPbhQg5dThnB2sePunZaQ1X3RsT6GxBKhpP+tSP3lnBPfqssIg7a3rGEznfy+pW+E5jp ieVZcp4nrjdThR9SINatvcAfMNfe+orfBudFZsr16J+KQ3Z8WN0Ps2JFU+7kKxIB6TXxn5 gZitnxT0+AupbQ9l7xtPgQDTmjZ+Kls= X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B66A2120011 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CZ0k5cyC; spf=pass (imf29.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspam-User: X-Stat-Signature: 6pbea7g393d8k4335r6bua1a19p3ytaf X-HE-Tag: 1669284584-20336 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 24.11.22 10:55, Gavin Shan wrote: > The issue is reported when removing memory through virtio_mem device. > The transparent huge page, experienced copy-on-write fault, is wrongly > regarded as pinned. The transparent huge page is escaped from being > isolated in isolate_migratepages_block(). The transparent huge page > can't be migrated and the corresponding memory block can't be put > into offline state. > > Fix it by replacing page_mapcount() with total_mapcount(). With this, > the transparent huge page can be isolated and migrated, and the memory > block can be put into offline state. Besides, The page's refcount is > increased a bit earlier to avoid the page is released when the check > is executed. Did you look into handling pages that are in the swapcache case as well? See is_refcount_suitable() in mm/khugepaged.c. Should be easy to reproduce, let me know if you need inspiration. > > Fixes: 1da2f328fa64 ("mm,thp,compaction,cma: allow THP migration for CMA allocations") > Cc: stable@vger.kernel.org # v5.7+ > Reported-by: Zhenyu Zhang > Suggested-by: David Hildenbrand > Signed-off-by: Gavin Shan > --- > v2: Corrected fix tag and increase page's refcount before the check > --- > mm/compaction.c | 22 +++++++++++----------- > 1 file changed, 11 insertions(+), 11 deletions(-) > > diff --git a/mm/compaction.c b/mm/compaction.c > index c51f7f545afe..1f6da31dd9a5 100644 > --- a/mm/compaction.c > +++ b/mm/compaction.c > @@ -984,29 +984,29 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, > goto isolate_fail; > } > > + /* > + * Be careful not to clear PageLRU until after we're > + * sure the page is not being freed elsewhere -- the > + * page release code relies on it. > + */ > + if (unlikely(!get_page_unless_zero(page))) > + 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. > */ > mapping = page_mapping(page); > - if (!mapping && page_count(page) > page_mapcount(page)) > - goto isolate_fail; > + if (!mapping && (page_count(page) - 1) > total_mapcount(page)) > + goto isolate_fail_put; > > /* > * Only allow to migrate anonymous pages in GFP_NOFS context > * because those do not depend on fs locks. > */ > if (!(cc->gfp_mask & __GFP_FS) && mapping) > - goto isolate_fail; > - > - /* > - * Be careful not to clear PageLRU until after we're > - * sure the page is not being freed elsewhere -- the > - * page release code relies on it. > - */ > - if (unlikely(!get_page_unless_zero(page))) > - goto isolate_fail; > + goto isolate_fail_put; > > /* Only take pages on LRU: a check now makes later tests safe */ > if (!PageLRU(page)) -- Thanks, David / dhildenb