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 F11C0C4332F for ; Thu, 24 Nov 2022 08:47:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFD526B0071; Thu, 24 Nov 2022 03:47:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D86CF6B0072; Thu, 24 Nov 2022 03:47:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFF426B0074; Thu, 24 Nov 2022 03:47:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id AE3D16B0071 for ; Thu, 24 Nov 2022 03:47:03 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 792FE160111 for ; Thu, 24 Nov 2022 08:47:03 +0000 (UTC) X-FDA: 80167705926.25.42C89A6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 0CA4A1C000B for ; Thu, 24 Nov 2022 08:47:02 +0000 (UTC) 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-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-628-tFYUMDaBP96oJVkOsKnAlA-1; Thu, 24 Nov 2022 03:47:00 -0500 X-MC-Unique: tFYUMDaBP96oJVkOsKnAlA-1 Received: by mail-wm1-f71.google.com with SMTP id o5-20020a05600c510500b003cfca1a327fso642737wms.8 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=tTBVg6EDCiFd5XfLapR7DmKydqacj47Y4J+UnIbGvE0HXNrI6RWFmYY0K3YDv3IlSg OrSVyzHvJKW3+Z7M0JG1SpBgE2DTWz/XImVxkioyET+ZBNPFrag9HicNPyUuK256B+XS TlPI+0Jhf80u/xS6M/PWHlH+M/IcTqyO03I0M6UPQW+mVIWURccUcqv1q1OkuQbZH4Q8 1mkxyRAIay2G2k3d+3lX+NuBWsvpO8bZQNL5p2ldFA6C9GnBUuYpC85ihVw9LsyvhFT5 aLoOCXm/OeI9mU2eGOYAh0cSCgJJ1pr+u2QhvUHHf5rsF26K57cCaVGz3rTvQi/gTNp8 gpLw== X-Gm-Message-State: ANoB5plPuhqBBP/9A52KA+t4+zs+sAtCZq5GkY0kLlpdojQcg88eGW9P O9ynZEX2XtKJJDHW59qpUkprHiR2FUZPjYIWlK7kJ6CP1vz2lw+TF6tXCoCT+Ftkg5HoSIAVf8i 0jAuU/fh7a9E= X-Received: by 2002:a05:600c:1f0f:b0:3d0:2a82:1cb2 with SMTP id bd15-20020a05600c1f0f00b003d02a821cb2mr8863479wmb.206.1669279619655; Thu, 24 Nov 2022 00:46:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf6bn2nLT8jXz5jZ8OGFq5hnA5XQ409+mb3jJ/xNk+VXCYJY0M8GPrwD2IZxwaIPUXLO789B/A== 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 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> 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: 8bit ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MuO+edGx; spf=pass (imf18.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=1669279623; a=rsa-sha256; cv=none; b=F9NA4icxBj9h4HcVB60vmZmpc8CpZkqmeIA5Kai1z36zy3MTmbyv9K6PzZ834DwpwC2oq8 WUKQ5sVFxFyDFbctwWu0X65ASmHKlEaMnGBfFfKOhkHQQhXvwSgnl+aVWXOjqwuSPz1mRK /nslWuE+bYxw2mi7IHbkh2p4SjwKLWw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669279623; 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=4n/Mj6FX5RcMoXU1Ro8lJ+6enfIFo7SixFfBkNMSQ1w=; b=CqYU5VAjNgvVhe/TU9XIYYDJl3kkZKu9iMQk8jCzZVyb20xSsl55ccBN2O9a6mYHHTbjWp IwSqbaZhzQB0WYgiIsQkBQqCLzGGpDwUSwM1FuUUUFwaxtkWS7sZhqKtgUXjtcZvEr3gVR aIw3CuCj9X4L30YXs3Yu07PV8DG4FHw= X-Stat-Signature: mkwxdpywreibz1auixu7zzhtzw3xhpz1 X-Rspamd-Server: rspam08 X-Rspam-User: Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=MuO+edGx; spf=pass (imf18.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-Queue-Id: 0CA4A1C000B X-HE-Tag: 1669279622-175791 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 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