Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8578317rwb; Thu, 24 Nov 2022 01:12:52 -0800 (PST) X-Google-Smtp-Source: AA0mqf7DhpdsXhN2PYEREGSTk/lhx9v1krDxcBktKo11tbi6smkNbIoYt3cMY9kuCc51HFCf9WGH X-Received: by 2002:aa7:dbc3:0:b0:461:6b61:81ae with SMTP id v3-20020aa7dbc3000000b004616b6181aemr28966625edt.62.1669281172465; Thu, 24 Nov 2022 01:12:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669281172; cv=none; d=google.com; s=arc-20160816; b=iLxlb33K6qA0a8+ExkBwjeg2IeR5IYHwnX+oPI/MQ5mNJSjQ8awhu4GbmuyKNitib+ AJSlDYqPVXAZmF11B+kRGK8wnHX95b2A77wYqM8dKLI5+EMeguqwU4BwvKkgtfaCyIhh 4qpoCo1xwRzleW35JMC+p0vy9jNm83zVlo68BN4iwqvpq6VUMsJadiLoNY6q6lFM06Qb M5eS83/3a/rAwWsEmL3S9zSVIva0nRr96iYOtsXDkF6aveog6TPTr/hVl2BWB2CtvEAG UuNxYcYni3dAiHiKIOg4s/gmAqnEODnNsApn2Y53178DTeR5t3LuSLi/7Bi/QrX1sxt0 /o3w== 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=bouCjiccMbkBph0CwILHyMEk13c9R9ulH7Nu8uPZIlo=; b=QP/PS6rYV8nPO+qaRx33v7Zx6D7hqKvocUeoBHnLCTbgGYWHQfo+eIyQt3S4C6ZGwk 5U8pXN0DRCFhGinnO1vUnmR1B7coYha/kAm8gB+WN/iVANf1GD3jrfsID5xnpO5BVC/B PO/12LdfjxlVm9YiLdvXnOf+gh9/lCVe2nit1+kK7tDwCvfqIssriV+OLroIQOdlZ9IF Wf0GV0yArRRbJywbW2tu61iMmzrNaKYtaXYBXheb0b3YD51znMG0HaN8z993njGX91/m LO6Y9lrZjMqAUWsJfV9LYbMRRGO0QmfMNTwH1DtjQpX51X5EVbgsZXAfyWnEmwoM7xJG 1i+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=db992nWD; 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 t28-20020a50d71c000000b0046a31ab6a3bsi555099edi.334.2022.11.24.01.12.31; Thu, 24 Nov 2022 01:12:52 -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=db992nWD; 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 S229694AbiKXIur (ORCPT + 87 others); Thu, 24 Nov 2022 03:50:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229497AbiKXIuq (ORCPT ); Thu, 24 Nov 2022 03:50: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 8FE71275EA for ; Thu, 24 Nov 2022 00:49:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669279791; 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=bouCjiccMbkBph0CwILHyMEk13c9R9ulH7Nu8uPZIlo=; b=db992nWDebjm2iqF7vrQBr4AD0OMfbef5oiuE+inI1ssLoT9GxajOTpZfEf7RNtxrK/oOO aZQIKqK4itqjxm+uyT4F61f/97pbJJbW3VT652qR4jfFYh6zY4IAcrYpVHwOUOjCnw08Na OLAFGcERp+1pPJrJzBJuTByaShKn0SE= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-493-23jeoreUMASs6R7pawJuoA-1; Thu, 24 Nov 2022 03:49:47 -0500 X-MC-Unique: 23jeoreUMASs6R7pawJuoA-1 Received: by mail-wm1-f71.google.com with SMTP id x10-20020a05600c420a00b003cfa33f2e7cso659520wmh.2 for ; Thu, 24 Nov 2022 00:49:47 -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=bouCjiccMbkBph0CwILHyMEk13c9R9ulH7Nu8uPZIlo=; b=QCjQEiwG42JnduL4soiolw4+y8yS8NAV64JTr+JRmt0k2PKPkMFP0daUqAADHwqo3F JNsypU5x0aE86ZqrRY8jqG5NmycaJ1wFh4iaTuTBVUn//KlGfAIgfIt+GhWI9/LcBKDZ FyCHdj2ENK60L77V4KtWHv+sacjoLxt6usmumXdpAoiro70CitgFzBB/iKbzyK0Ao2Of dq5cnBpFslegO3asFFY9nqkScFEjmF0zFeBxtmUPwCuAEM5cIQdd0A2yXsBwIdbwNFt/ aLnzUwu6+Zh6NsXSTxkTUBmZkK3DkZLlncJrZTmvoa74vCb/C800O11RPqADHk5OKa8x MREQ== X-Gm-Message-State: ANoB5pk1GMI1aldhAQF65aGRnK2/EC0OwfN2a9nwJX4ZvPee3KxS1Ct4 4P0ns9hA4DWmPmWH6pFbxrQx4db8HcN0J2sPXLwrcpuMFr8TlMn8E5bFR3nr3x3eRk5mgc4ZjF3 JMNY9c4LTD1JVW6ZifA6c//LI X-Received: by 2002:adf:e4ca:0:b0:241:d5a7:8bf7 with SMTP id v10-20020adfe4ca000000b00241d5a78bf7mr8728563wrm.440.1669279786656; Thu, 24 Nov 2022 00:49:46 -0800 (PST) X-Received: by 2002:adf:e4ca:0:b0:241:d5a7:8bf7 with SMTP id v10-20020adfe4ca000000b00241d5a78bf7mr8728542wrm.440.1669279786352; Thu, 24 Nov 2022 00:49:46 -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 dn7-20020a05600c654700b003cf6e1df4a8sm988382wmb.15.2022.11.24.00.49.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 24 Nov 2022 00:49:45 -0800 (PST) Message-ID: <51ffd399-7fa3-b2f2-b6e5-61a8b609e350@redhat.com> Date: Thu, 24 Nov 2022 09:49:44 +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: Matthew Wilcox , Alistair Popple Cc: Hugh Dickins , Gavin Shan , 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 References: <20221123005752.161003-1-gshan@redhat.com> <871qptrvsw.fsf@nvidia.com> From: David Hildenbrand Organization: Red Hat 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 24.11.22 04:33, Matthew Wilcox wrote: > On Thu, Nov 24, 2022 at 12:06:56PM +1100, Alistair Popple wrote: >> >> David Hildenbrand writes: >> >>> 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. >> >> I think you're right. Without a page reference I don't think it is even >> safe to look at struct page, at least not without synchronisation >> against memory hot unplug which could remove the struct page. From a >> quick glance I didn't see anything here that obviously did that though. > > Memory hotplug is the offending party here. It has to make sure that > everything else is definitely quiescent before removing the struct pages. > Otherwise you can't even try_get a refcount. At least alloc_contig_range() and memory offlining are mutually exclusive due to MIGRATE_ISOLTAE. I recall that ordinary memory compaction similarly deals with isolated pageblocks (or some other mechanism I forgot) to not race with memory offlining. Wouldn't worry about that for now. -- Thanks, David / dhildenb