From patchwork Thu Nov 24 09:55:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gavin Shan X-Patchwork-Id: 25415 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3298013wrr; Thu, 24 Nov 2022 02:03:33 -0800 (PST) X-Google-Smtp-Source: AA0mqf5iw8bVxorSAhcZWJo6qEDvXj5FLchYyE3hilSlbV6T6hJ5NsFhC16Kjpl7b88HygBML0hj X-Received: by 2002:a17:90a:f0c2:b0:218:ac70:fdd5 with SMTP id fa2-20020a17090af0c200b00218ac70fdd5mr19640198pjb.123.1669284213408; Thu, 24 Nov 2022 02:03:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669284213; cv=none; d=google.com; s=arc-20160816; b=hZwmYrbo3i2V5TicruCSCi1IBQfcCjV1oXrOXp9/jvLyvp4nEtt1ALaMq0k8qiGP2A s7mIEqxJ4d+Cdgh9M2RQ3SeqFHiQcPr5FobGD/to2lhMox7v3CNEEfpCiYyh7YxZd1qT Iu9tp3evZYwRLJCRiCraARgvKWTu/Ceuk09FyyOQJMyKHuGgWceS8CParUWAjOo2jZxK OC6hkrlggAUdFivF3IFzRSHOyJ72a25iSnRSjdM95rER/BnGokOFCYtL/MiXP2GOWbze ClvO/2GQmn43ew/RI48e90KaGZBPWnrinRnli9rdjFo9GI8wprxfDj2l5vemfoHhRg9E BRsQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=60QDRRHbYW5CIGKF/Qr6kydVK9e2+lWs3YQKxRzQMXc=; b=Ar2cjihtbSY0X3GiNaqRMa60jcbbHfJgOdYUPgM8vNg2qwgBeHKwk5YXf1HZcH2B6/ cBiTfjJpC7ul6sqzXkSJBXrPLJQ8jIJnHO1DyccVDbU0pZ5n68iKA37wdJpUF539Fg/6 ITTx5M9StUVbFcG/DS7qoS1X05woA5UVBS4My1AtdhlavyL8jv8UKeUEWC7c5/4TJ8YC axzjivuOGs6jJHokX2orPzISr1WLVGRQMxtjsk6Uq+Zen0teHtlXCPZbFA3dbSiEqH4R FMqKyTnZhKUd3X1Aru56rIAzSs02oqTdQfsIiFTSUrNe12OfIn4lMyj7tBq+ZT3RRrq7 9fTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S9wj6D3X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o12-20020a1709026b0c00b001893e9d712dsi507018plk.557.2022.11.24.02.03.20; Thu, 24 Nov 2022 02:03:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S9wj6D3X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229671AbiKXJ4s (ORCPT + 99 others); Thu, 24 Nov 2022 04:56:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229555AbiKXJ4q (ORCPT ); Thu, 24 Nov 2022 04:56:46 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C677912EBE5 for ; Thu, 24 Nov 2022 01:55:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669283749; 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; bh=60QDRRHbYW5CIGKF/Qr6kydVK9e2+lWs3YQKxRzQMXc=; b=S9wj6D3XPLSoazx33TkWn8VTdWPEXHFZmi2kDwuPOfedqqSlXSjthL2+Kymy6GqM34jYEK NlStzKQ0GCU39MtBzvV11Jj5STTZtjsZvvN92JDWhIQYgtPPQcqmxOLhuVF8NgfByYLJkM qsu3by9FtmtYbPyMcNUGaJWTwxmiU+Q= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-499-By9IKOSyMZuvRh8Ile7czA-1; Thu, 24 Nov 2022 04:55:44 -0500 X-MC-Unique: By9IKOSyMZuvRh8Ile7czA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C8C2B29AB3E5; Thu, 24 Nov 2022 09:55:43 +0000 (UTC) Received: from gshan.redhat.com (vpn2-54-95.bne.redhat.com [10.64.54.95]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6303140C6DC7; Thu, 24 Nov 2022 09:55:39 +0000 (UTC) From: Gavin Shan To: 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, david@redhat.com, zhenyzha@redhat.com, apopple@nvidia.com, hughd@google.com, willy@infradead.org, shan.gavin@gmail.com Subject: [PATCH v2] mm: migrate: Fix THP's mapcount on isolation Date: Thu, 24 Nov 2022 17:55:23 +0800 Message-Id: <20221124095523.31061-1-gshan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750246827300294072?= X-GMAIL-MSGID: =?utf-8?q?1750371363224019534?= 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. 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 Acked-by: David Hildenbrand Tested-by: Zhenyu Zhang --- 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))