Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp7150098rwb; Wed, 23 Nov 2022 02:56:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf4CwajdbbC+v7HNbKNIg2xk/VzXzd/lp9ZfkT2dECXgw9chqxIY4t73X9ikd82IHUzYP4RU X-Received: by 2002:a17:906:c40b:b0:7ae:1e53:95b2 with SMTP id u11-20020a170906c40b00b007ae1e5395b2mr22971598ejz.333.1669201007404; Wed, 23 Nov 2022 02:56:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669201007; cv=none; d=google.com; s=arc-20160816; b=UNGSPWauSXIyx5LYhql8hEmLZsEwICizad22zlnAgtEvNbYZLi/fjELs7r3Gqqmjms ggTCOqS31XPmT8J9WL8WcUE/k+w4eh4H8tXJz0CXOtUZuWmKIqhhY0EwR/4wxcCyk6WP yL8jWasERPhhUeKHGFunCZlGwstVz2jHERmkqP3GXoIIA7ASVLjOHyWXUn5+EcdxRJgz wRhH5oKCMwydN6tVT4d8DOdF/CkcDnn3neMv23XLWTmm2lxPe7Ac+AenYk19o754Ey7m RSmh961zDhVsGucXUR5KDbw8YJvCcNhreXJyMYyA01fx7wjNIdRUnCqx+Vfsdn+wjgmd Cwew== 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:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=XWu6+GwCnxbfZ8amHPAOZGcnSFr/fgzIG5mmAP249rA=; b=iH2Bd2paojvp0dFR/DRpTeORDi+Ug0qiIjqHW5nUP2NzHcueD6mNr9/wvCtc/FOMtp yJjDfF7CD0r4wif7+fHL9lwV9/Nv5Hr5+QVq0tNkW4wrr9nwwo02SmchCpbhtNu62J+M YEGCkHljGjovBWERLPPol3ajyJHI4TkIkNNfA3VG42lsVV6cyuXWaeeuhw6XeaUVAyXv +6MS5Q4ccu+LgEw10CvvauU3v0+ClUyS/UaOMt7r7r2zUaSvgINsIhz8IqoX5QgWuQu9 ViiJGaosGbBRxCTzY6mlTgdl9TzSor3WaCfuXHjNcKubL2iJ9wP4DRYnVNENp7SNArKs mANQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iUo48WjO; 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 b8-20020a056402350800b0043dfc949d31si14983152edd.25.2022.11.23.02.56.24; Wed, 23 Nov 2022 02:56:47 -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=iUo48WjO; 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 S229563AbiKWI5l (ORCPT + 88 others); Wed, 23 Nov 2022 03:57:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236777AbiKWI5k (ORCPT ); Wed, 23 Nov 2022 03:57:40 -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 BDFFB1001CA for ; Wed, 23 Nov 2022 00:56:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669193802; 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=XWu6+GwCnxbfZ8amHPAOZGcnSFr/fgzIG5mmAP249rA=; b=iUo48WjOsO8uH493AC4xUDd0tgqKtvfnicaqCkWQO24n0rsk53uSQSrdJfuuLOwAAuIwGW A7aefp91lVAdhoN43f+I0r+UocZlXrjqMfPvxNNVffQhx5Q8rE4MatH0QUR6gWaJKN+Bd4 pFDtsXkNGFXvIL+3h9Sv3XMQR/p9ACo= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-537-Um504RLVOAStI3FOH3Q3zQ-1; Wed, 23 Nov 2022 03:56:41 -0500 X-MC-Unique: Um504RLVOAStI3FOH3Q3zQ-1 Received: by mail-wm1-f69.google.com with SMTP id m34-20020a05600c3b2200b003cf549cb32bso782136wms.1 for ; Wed, 23 Nov 2022 00:56:41 -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=XWu6+GwCnxbfZ8amHPAOZGcnSFr/fgzIG5mmAP249rA=; b=f/x2ioqOTaSEG0M7BLfQUZzJmeziejuXNlAXJsRWgaPTvGYWuJHDCzJkV7bbFJpN3T vL4EXSZ/Un6iiC2gQlSFkITVDGVNaEYgtYsGleY1a/+uZrbsi1hDkMFQElrydCwgwg/c 3U44dQoZqFaz5fiBbhRsxaPi03SVg5abE4m4Fr8nxL4Tk0nFsqi9vqcvE5iUEonpa3Dj CXHVhY68sMDXxlh5LLr6dgSKhEYsoxkiAvMofjOIeK/bdxt270p4Zm5D5zUN0yRdz6B3 Xd3CATUvaxs4QQHmJQtuth4B3ibr62YsGci211OtULVipxQB1XB+i9ilSdOElOu3AET4 7TTw== X-Gm-Message-State: ANoB5plOP3aPLwklgUORtYs54ZTvRDUkRhkK8VikDaa/Yzzu81ySOLnL h7gtfhtzGi507FGBwM5hKXRBFSKal5FxsJQZLSI32DanqLSM37dy1MbCvcHmxgW+iw/B0SJFd2m mjjOhIvXaLwNLnheWoUcBH2qU X-Received: by 2002:a5d:6486:0:b0:241:ba9c:cc65 with SMTP id o6-20020a5d6486000000b00241ba9ccc65mr15889255wri.443.1669193800382; Wed, 23 Nov 2022 00:56:40 -0800 (PST) X-Received: by 2002:a5d:6486:0:b0:241:ba9c:cc65 with SMTP id o6-20020a5d6486000000b00241ba9ccc65mr15889233wri.443.1669193800065; Wed, 23 Nov 2022 00:56:40 -0800 (PST) Received: from ?IPV6:2003:cb:c704:9f00:a98d:4026:7c44:40fd? (p200300cbc7049f00a98d40267c4440fd.dip0.t-ipconnect.de. [2003:cb:c704:9f00:a98d:4026:7c44:40fd]) by smtp.gmail.com with ESMTPSA id r19-20020a05600c35d300b003c6b874a0dfsm1910509wmq.14.2022.11.23.00.56.38 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 23 Nov 2022 00:56:39 -0800 (PST) Message-ID: Date: Wed, 23 Nov 2022 09:56:38 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Content-Language: en-US To: Hugh Dickins , Gavin Shan 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> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH] mm: migrate: Fix THP's mapcount on isolation In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 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+ > > 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. > > The races I'm talking about are much much rarer than the condition you > are trying to avoid, so it's frustrating; but such races are real, > and increasing stable's exposure to them is not so good. Such checks are always racy and the code has to be able to deal with false negatives/postives (we're not even holding the page lock); as you state, we just don't want to trigger undefined behavior/BUG. I'm also curious how that migration code handles a THP that's in the swapcache. It better should handle such pages correctly, for example, by removing them from the swapcache first, otherwise that could block migration. For example, in mm/ksm.c:write_protect_page() we have "page_mapcount(page) + 1 + swapped != page_count(page)" page_mapcount() and "swapped==0/1" makes sense to me, because KSM only cares about order-0 pages, so no need for THP games. But we do have an even better helper in place already: mm/huge_memory.c:can_split_folio() Which cares about a) Swapcache for THP: each subpage could be in the swapcache b) Requires the caller to hold one reference to be safe But I am a bit confused about the "extra_pins" for !anon. Where do the folio_nr_pages() references come from? So *maybe* it makes sense to factor out can_split_folio() and call it something like: "folio_maybe_additionally_referenced" [to clearly distinguish it from "folio_maybe_dma_pinned" that cares about actual page pinning (read/write page content)]. Such a function could return false positives/negatives due to races and the caller would have to hold one reference and be able to deal with the semantics. -- Thanks, David / dhildenb