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 F1EE7C43219 for ; Thu, 24 Nov 2022 08:49:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EC696B0071; Thu, 24 Nov 2022 03:49:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19C6C6B0072; Thu, 24 Nov 2022 03:49:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 065C36B0074; Thu, 24 Nov 2022 03:49:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E750C6B0071 for ; Thu, 24 Nov 2022 03:49:50 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AA8648094E for ; Thu, 24 Nov 2022 08:49:50 +0000 (UTC) X-FDA: 80167712940.20.FFCA4A9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 544C9180004 for ; Thu, 24 Nov 2022 08:49:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669279789; 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=AC0KXLp8Fb9eqGPH1l0AC4R+eVJpSC7aMgnB39v1SEoa0mUIdW7CrdZkipjzQjFKnoBGg5 Es1BM4pD2T+bB4Pd/8C9b+nD4i17NgR8h1XtvsvjL0l826vEJB12KpJrgcC51KsuUVcNNa d1u0lLX+3//Ie+pbnkQM0/7LbPX4Lpc= 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-122-0D2lYWUrMLatC7APs7m4jQ-1; Thu, 24 Nov 2022 03:49:47 -0500 X-MC-Unique: 0D2lYWUrMLatC7APs7m4jQ-1 Received: by mail-wm1-f70.google.com with SMTP id m17-20020a05600c3b1100b003cf9cc47da5so642971wms.9 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=XXmwfmxR9E1psP8YmrOZ+GdDq5KfjwAptIZcwVUOlKFsAnqPMF/RKY1+d1u65Opky4 RKrhIZo5Rv/N5bQ760ooc1cxH3D8dgCAL5b1Rr8WMCipRnVXtzBD6Of3NGv7DdiL0sYF Px29gxTb17rRoiZga3pqjzTm63+Yuz5A2U//eNcDwwM1NFvG/1lv0GXXnDc8m8H6zCW2 en9XeY1M7YEgxMLRepx806axyBcFgMAltKbU4KMbK8CfCbNk0J2tkWixZBX/01WsSofj YSE+HF2933DCjqpY9IeA34OqxRGx04OS7vec566aZHC0l6FnXV1ixocDkKEAl6Vgfwa0 l12Q== X-Gm-Message-State: ANoB5pls/su6vRwtv6+89XURuGnBGZmyNUPxNC7iCreWwdfdU3jK/IPW BRDCaQchGawPgLiy1mhx2YBJLY3ja5PuCQOoWP3AvMjhW5ymWc5/ygs+LdrSgioOjYGwyXEItxv ++NCG80c+cls= X-Received: by 2002:adf:e4ca:0:b0:241:d5a7:8bf7 with SMTP id v10-20020adfe4ca000000b00241d5a78bf7mr8728560wrm.440.1669279786655; Thu, 24 Nov 2022 00:49:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf7lMi+A4R7Nx1q9fQ8HSx91nzD4aqkEi0jFq0mNdcSOZNVpH2q4UoP8cXz383M5CERZD4uQbg== 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 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: 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-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AC0KXLp8; spf=pass (imf06.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669279790; a=rsa-sha256; cv=none; b=P4L+fv09Zz9zA8V7Mp4gGtqEAjeR35zXE7ebA3vGMxTUSPmGtngo3UEWufwzlWdCLYDsxD ZuBpo9e35UJL/Jv5x9vh7rL8UmuopRT2Yye1TBmzwtEi/zND8bfbmhz83gJ1SRUuhfreb+ rJ9fDmB8SkiFPBZ9ucqEq8BOFOweAZw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669279790; 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=bouCjiccMbkBph0CwILHyMEk13c9R9ulH7Nu8uPZIlo=; b=0wurcpnroppF5u1OgCPR3mAQRWVQMyMi4+YXEeVKzM3Y4eKcRmK6RWS5+JpoSDFtWoCSgW yg135DSCWuVf3nc32a+jA5HawyrKPXnFC/gGb2bRGvqcM9YsjizjA1R2yauXqqHm0inxUM s9HPvYBI0vDgIAyO3EQ7u6T9PkoB+5k= X-Rspamd-Queue-Id: 544C9180004 X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AC0KXLp8; spf=pass (imf06.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-Rspamd-Server: rspam06 X-Stat-Signature: 5w6bkk63ea3mgp1iysqxr3q5hmurqdr1 X-HE-Tag: 1669279790-215854 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 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