Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8597431rwb; Thu, 24 Nov 2022 01:33:38 -0800 (PST) X-Google-Smtp-Source: AA0mqf5NklBVhT7upMRt92jggGOEgkgcKdkngRAtBavcg5z6OkZcErm31+2uY9hDmVb6pVaiOWWq X-Received: by 2002:a17:903:1203:b0:189:30cd:8fa4 with SMTP id l3-20020a170903120300b0018930cd8fa4mr14432533plh.103.1669282418018; Thu, 24 Nov 2022 01:33:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669282418; cv=none; d=google.com; s=arc-20160816; b=jLes0vi/c9rFJxyT006oyLO73VDP75IC4nGtwZY1b3aMPGOcsM40twti7AtOA35LAp QpJ47/VbfxPxDeH+ukPxKr/3hlDaLF1Wo6BwV85qpdF2PMihaT69prM/uE55/bB5bn1j p8GCliiX6UT4o86O8nWBMIVpESRSuvxzCfmGWrdN21LAgkgqh3Gy22ztpO+vmmV2IQfT E1DxzLFIX5XEXgvW3zUdQw/NgSSYRHrgkFKURdz9mkGvW1NrWd7cLzrPQQ7/wjXhBmD0 pUZZsV9oNTymVplq3KldmWZiY6C2mlF+YaCuStLpxMTTY5EKbq3NACW7DpLOms/n3h1R 4+dQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=4n/Mj6FX5RcMoXU1Ro8lJ+6enfIFo7SixFfBkNMSQ1w=; b=JPQs1poKoZucGK58Q2yt4aHiHK65fogoHvWU5kYtK7TRdFLsyD+o1qnJfXFLHo9pq+ 6Sf6J++r1c4gwOfLDPC2CbhML0lATLlcqS7PaEHnb7RT9xQt9QzDB3d7QheHUcvTnqZS TqKU08bdl1QJlDiYjraKvEONnEbFA4jJug+Lf10XKuBGQ1ozwICXnXsfus7kWqsMxTDP KmM90jtok7X9WXLvsa0b/CVWulki67QGWfFmqeuQy6pgwyEZxScnNRQT4K8yS28ISt0O hJ4KPRstVboCLM80uEkqXA8zz6tP6J6uLevuH+YuhBF7LNW7GCO/Fg0Nis9a5ScmzIK7 G1sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=MuO+edGx; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 33-20020a631161000000b0047688fc8acdsi848264pgr.474.2022.11.24.01.33.26; Thu, 24 Nov 2022 01:33:38 -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=MuO+edGx; 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 S229635AbiKXIsE (ORCPT + 87 others); Thu, 24 Nov 2022 03:48:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbiKXIr6 (ORCPT ); Thu, 24 Nov 2022 03:47:58 -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 4C7BEFAE94 for ; Thu, 24 Nov 2022 00:47:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669279622; 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=4n/Mj6FX5RcMoXU1Ro8lJ+6enfIFo7SixFfBkNMSQ1w=; b=MuO+edGx+/IVev5Ri7etNZp6yK1Gx32cCUezjOZcWj6uRvtTnwAnKKk9l0gXxkU/cANohN LpuJyJYKdnJ8BbPR+Fvii65e3CMqJ1riobo6yP7hCzxiWvAsBaSY/VAcBu+XSMFGRsBLv/ EBwI8g82vfmqCwj6wH4W66khtuVfxh4= 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-177-EOgIVf8MOlSZwVhFDNtk5A-1; Thu, 24 Nov 2022 03:47:00 -0500 X-MC-Unique: EOgIVf8MOlSZwVhFDNtk5A-1 Received: by mail-wm1-f70.google.com with SMTP id ay40-20020a05600c1e2800b003cf8aa16377so644844wmb.7 for ; Thu, 24 Nov 2022 00:47:00 -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:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4n/Mj6FX5RcMoXU1Ro8lJ+6enfIFo7SixFfBkNMSQ1w=; b=a4NGoS1KHzecxt0vp41h42POO9Fe+pP0gs96jzgHTl06iCDk8mHJJwwoPR4QHAecB6 YyuJyFpmR9i942B+VQTFm5XS4a8iJg48bQDLTmFNFy7nZz+UGLNT7zeHyFFHZrRaf0Z2 CcDqmmmos0JYDfVGNuoAjAjug6KXxyZfD3ATxzhmO0gl80/kHg+R1vraUwgttHfWS/Vd t6KjAu3mGJrFa3yCDZTxAEtfNfua6VVWf/Yvd32DdYjARK3hRzIBQcAgyQ9lc+Wbb+Lv cMxqvJImqcZ8E2CtBvc0yG47cfbaOAARxvfULqKSQd/gm8XW6dKdn/IrH1beW9agUCLx CCdw== X-Gm-Message-State: ANoB5pn163bCebd3gHiLgZ10bvlWoA1jChbK4SQdo5NHbWO6yHgzg45G PZoqXE3imC5ZrNz1tw8u203zrlMWHiaMecEE5QH+QoWt51VWNfca+lyYjTXoay2EQQ4qQEPkQ3f w0Tj85WrwezjDDaaldWQ+MGdV X-Received: by 2002:a05:600c:1f0f:b0:3d0:2a82:1cb2 with SMTP id bd15-20020a05600c1f0f00b003d02a821cb2mr8863486wmb.206.1669279619657; Thu, 24 Nov 2022 00:46:59 -0800 (PST) X-Received: by 2002:a05:600c:1f0f:b0:3d0:2a82:1cb2 with SMTP id bd15-20020a05600c1f0f00b003d02a821cb2mr8863464wmb.206.1669279619366; Thu, 24 Nov 2022 00:46:59 -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 w8-20020a5d6088000000b0024165454262sm751245wrt.11.2022.11.24.00.46.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Nov 2022 00:46:58 -0800 (PST) Message-ID: <0bfbcb03-fad4-92db-0a19-167a3a37ab50@redhat.com> Date: Thu, 24 Nov 2022 09:46:57 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH] mm: migrate: Fix THP's mapcount on isolation Content-Language: en-US To: Gavin Shan , Hugh Dickins Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, william.kucharski@oracle.com, ziy@nvidia.com, kirill.shutemov@linux.intel.com, zhenyzha@redhat.com, shan.gavin@gmail.com, riel@surriel.com, willy@infradead.org, apopple@nvidia.com References: <20221123005752.161003-1-gshan@redhat.com> <9d2ce3f4-9f5d-ff09-c28f-fc28ee6f2817@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <9d2ce3f4-9f5d-ff09-c28f-fc28ee6f2817@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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,NICE_REPLY_A, 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 On 24.11.22 01:14, Gavin Shan wrote: > On 11/23/22 4:56 PM, David Hildenbrand wrote: >> On 23.11.22 06:14, Hugh Dickins wrote: >>> On Wed, 23 Nov 2022, 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. >>>> >>>> Fixes: 3917c80280c9 ("thp: change CoW semantics for anon-THP") >>>> Cc: stable@vger.kernel.org   # v5.8+ >>>> Reported-by: Zhenyu Zhang >>>> Suggested-by: David Hildenbrand >>>> Signed-off-by: Gavin Shan >>> >>> Interesting, good catch, looked right to me: except for the Fixes line >>> and mention of v5.8.  That CoW change may have added a case which easily >>> demonstrates the problem, but it would have been the wrong test on a THP >>> for long before then - but only in v5.7 were compound pages allowed >>> through at all to reach that test, so I think it should be >>> >>> Fixes: 1da2f328fa64 ("mm,thp,compaction,cma: allow THP migration for CMA allocations") >>> Cc: stable@vger.kernel.org   # v5.7+ >>> > > Right, commit 1da2f328fa64 looks more accurate in this particular > case, I will fix it up in next revision. > >>> Oh, no, stop: this is not so easy, even in the latest tree. >>> >>> Because at the time of that "admittedly racy check", we have no hold >>> at all on the page in question: and if it's PageLRU or PageCompound >>> at one instant, it may be different the next instant.  Which leaves it >>> vulnerable to whatever BUG_ON()s there may be in the total_mapcount() >>> path - needs research.  *Perhaps* there are no more BUG_ON()s in the >>> total_mapcount() path than in the existing page_mapcount() path. >>> >>> I suspect that for this to be safe (before your patch and more so after), >>> it will be necessary to shift the "admittedly racy check" down after the >>> get_page_unless_zero() (and check the sequence of operations when a >>> compound page is initialized). >> >> Grabbing a reference first sounds like the right approach to me. >> > > Yeah, it sounds reasonable to me to grab a page->__refcount in the > first place. Looking at isolate_migratepages_block(), the page's refcount > is increased by get_page_unless_zero(), but it's too late. To increase > the page's refcount at the first place in the function will be conflicting > with hugetlb page and non-LRU page. I mean there will be a series to refactor > the code so that the page's refcount can be grabbed in the first place. > > So I plan to post a followup series to refactor the code and grab > the page's refcount in the first place. In this way, the fix can be > merged as soon as possible. David and Hugh, please let me know if > it's reasonable plan? :) Can't you just temporarily grab the refcount and drop it again? I mean, it's all racy either way and the code has to be able to cope with such races. -- Thanks, David / dhildenb