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 BA618C433FE for ; Wed, 23 Nov 2022 08:56:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 55EBA8E0001; Wed, 23 Nov 2022 03:56:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 50CC06B0073; Wed, 23 Nov 2022 03:56:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AD638E0001; Wed, 23 Nov 2022 03:56:44 -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 28A546B0071 for ; Wed, 23 Nov 2022 03:56:44 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E71F81C6A0F for ; Wed, 23 Nov 2022 08:56:43 +0000 (UTC) X-FDA: 80164101486.29.B1A3AFB Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id A21551A0011 for ; Wed, 23 Nov 2022 08:56:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669193803; 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=AUOAjXZ0hC028KtT/Qg2yvhat3lOPxnG8sqKDVCvQ84s7gfq1NH+V9Uj3J5EIzqXmdxAWc gfTWF9AjXwFq+OFtKtwoacCgKq2tjbFNZAhn/Kt5+h2PACJ+q0pbIjl3l4eXR9BmY/+0jz THXT1Fp+q/SYdPOHuYA/xJKTe++HpJA= 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-434-oGCCjaBcMtGmMBG8zpPpOg-1; Wed, 23 Nov 2022 03:56:41 -0500 X-MC-Unique: oGCCjaBcMtGmMBG8zpPpOg-1 Received: by mail-wm1-f71.google.com with SMTP id 187-20020a1c02c4000000b003d016c210f7so764002wmc.6 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=ddDP3CzXgdQxEiF44oPR/Xpqd/LAWZXfF1f3uLu/+fus8T6NxpGzUNdH6hfhg9Id95 yrS9lt7XHxi0dGSnQ9UmQgLxJCJYwQTVv9FYr0TKRRnzee2881IqSiFWH5lLjjouYlYB TtuMbwnV+dqCp2Q6ijysw435bhoayPVB39dmg1j5w1o8BcOHw7P9ekNJLXO5YFV0Wq9I QpFcar7e5o3OhoDxABMAVeVVHmICZn+4l7qbM3obUffXVvIDdRdheKONhXlgXtKS30M2 BKOMSFI3fFfHtPPgbewbqHFTPbKionzkcvap1dSqx3PEyAgGzvi1E7ajumm00x0jL2fK 2k1A== X-Gm-Message-State: ANoB5pkSoqNi2hAIjQqrH8UiaEEtwvcMocVzMyDTi0NjkbYaqnCL37dR J4NdMhDGCvjv5GHSzsufnwx4U31dGCyttm2qLdL/ZGDTgJTqnvMSCBjE7IGdWu2lPuqRn1PbDFv QDlokPKCeIZk= X-Received: by 2002:a5d:6486:0:b0:241:ba9c:cc65 with SMTP id o6-20020a5d6486000000b00241ba9ccc65mr15889246wri.443.1669193800375; Wed, 23 Nov 2022 00:56:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf4uDU75kInYeD9FSmhw8R0V45qBc7elklNLWlIvr+0tClc6/K1z63WfVyYYsckxcZZ6H9Aq8Q== 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 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: 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; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AUOAjXZ0; spf=pass (imf19.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=1669193803; a=rsa-sha256; cv=none; b=Mm0oHdi4ffBRtis7GbEb8v2jN0MIg19x1R6VMMVo5FwHSSLEHuqVDdV/zfgELUWeUWFHGg v4vyoy7omqAqnJEFQvERRvGz1FfiLKrziDCIe1o5Jita+XaEDmI052QFBv6BZknoj4+T3Z u1vlydaChSxopQWQZmKD0F5uUW9ifUY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669193803; 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=XWu6+GwCnxbfZ8amHPAOZGcnSFr/fgzIG5mmAP249rA=; b=PQXMmaR1rbAgx1zxKXa287i/mvWIDZSzCPJezbw9qCRkWc3J5RkIhLauqFJ+DeMCbr7Cm5 ca/C81qI+mgAJxaELi+sLdi5gDDRzfyiHZguJEO96vwk2CJqbkBHweOba/tIfM9J3s1wtw 23M8veC65QH9WFfKOwb++vtaIMk2TP0= X-Rspamd-Queue-Id: A21551A0011 X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AUOAjXZ0; spf=pass (imf19.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: q3wsu7n6hkj46mqr5azspsykdbahsnn4 X-HE-Tag: 1669193803-19703 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 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