Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1119061lqa; Sun, 28 Apr 2024 20:36:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV9ztzeXTSbON8Av7N16AT+YBRLkUk7HLbLx5fmzeQp9TuLbf3NzUofGei67OxxOnKJTD+YtlCs+U44fU6sjUebL45SYfVg2Q5zet8+Gw== X-Google-Smtp-Source: AGHT+IHAkJOoBePb6BHy+vM0FiiFrwRE75wBB8Flvdo0ArV6iPOuQoeeQCxJiFX11w12ho3rhchO X-Received: by 2002:a05:620a:5621:b0:790:794f:9544 with SMTP id vv1-20020a05620a562100b00790794f9544mr11125899qkn.51.1714361798332; Sun, 28 Apr 2024 20:36:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714361798; cv=pass; d=google.com; s=arc-20160816; b=KrvHT7nvueCEY3hPqPMbtFhFbTItoMoFDprn/nvVInkYaAFztMmpWbkTLrmKFuhlZe iTOYtcAb5Uqf8WLPGoEHkT3P+xN33cbuIgq4aGKcHpYHKvj9Xtk1/l+P68B14JZR1ZWJ R60/b51EVXB020Axq2SotdD65F4wH3MqvKTYzdZPHguGaZXrn8WpM/ZdIzspbM051W07 94taLmE4jufBG9BHg7ooRCo42DXvVyXRrfjkCcntnxNmFouSvWsFcwelMXYso1sPrRdd Nz+P4pZYuwheDDdRj7Vq0RCLFs1TG4+F7Mr57pkm2SzU23m+Pq3lrtp+qif5SEY5ET4H JiQg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=AHfVp5cixgnGxnNfe0UZJaSHyRHBx4LbgXQYY/QC6Sw=; fh=rh401YhQDpifXwdrBiUGUs55rVwsM371BxJ9wg7Drz4=; b=FqFR4TtLxbT2hIqcYbPo1v6i5LPPnEL6VhVaEtc0jNsAwAwZCtWAGAGgl0PW1Nk83R ah5BVFZWMHRG4tImXnPJy3+SI3Q11CG3gSyy3tSUDSE9+QnBWLH5jkGHrOuBvb2GBLO7 Fyr0S6n5xzcGH2mwGEpEK0X0W7x3lzT3LlTs3QZpDRieXEFxnMczCx4H89HBUkBPT7d0 2BrZdB9llk2gFDjAWgk+LrVI2YuJtfEwsUrdimT0GGJm+u8hHrtDNyE/Pf1vPCGC9hCV LgY4q4JXGYuAFKvRUw3W66Oh6D2FQLjVlMHgJU/fdG70RN6Lx6AAo9N/0Y1FKpUxlSDp Pkkw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-161699-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161699-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id w5-20020a05620a0e8500b0078ed6d9b474si24813922qkm.450.2024.04.28.20.36.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Apr 2024 20:36:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-161699-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-161699-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-161699-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 06E011C2105C for ; Mon, 29 Apr 2024 03:36:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 353718F5D; Mon, 29 Apr 2024 03:36:32 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DF4D86FD0 for ; Mon, 29 Apr 2024 03:36:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714361791; cv=none; b=HjVz/jjThJ01CuA9Qvx0aH2553ZlOChRyI5ABgCCW3ikU05SQwzrhdX7L8R0lUFQ8XGcAUw6mqVB4htPR+rnovs91hvnAg/d4esKLpycFSXYiQCEsBRM4LF3Iz8HyN5f3lIDFjIzmTqAkXD5qNcYxG27PYktsMI43BsO1bQvAgU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714361791; c=relaxed/simple; bh=OMypMfMPWaoZtX2P8Zb0bC9bAef9DLGboc1N3FIq4Ag=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=i3Beva6ZKvjNJexesqclBnzoETor9q0CjK5EUIIvrPgOTWxCR67hrqZxr/MdqnuGatdZ5ZtcmSNHIk0h2GwDWD1is/KzBSiJz/+vdu2txX1CHjeubWC1u2YrxAyAduHtejZgFmDfUcBRqMkgMFWgbbh7F3cnezmeTo6BHYyriMY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CF93E2F4; Sun, 28 Apr 2024 20:36:54 -0700 (PDT) Received: from [10.162.42.72] (a077893.blr.arm.com [10.162.42.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3FB573F73F; Sun, 28 Apr 2024 20:36:24 -0700 (PDT) Message-ID: <34a9ec83-40ab-4028-8ee3-27438665155f@arm.com> Date: Mon, 29 Apr 2024 09:06:22 +0530 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1] mm: Fix race between __split_huge_pmd_locked() and GUP-fast Content-Language: en-US To: Zi Yan Cc: Ryan Roberts , Andrew Morton , "Aneesh Kumar K.V" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huacai Chen References: <20240425170704.3379492-1-ryan.roberts@arm.com> <922F6794-1C32-4862-8A67-90D4F7DBC474@nvidia.com> <683CC7EC-3FAB-4C63-92F2-636C4DBE7729@nvidia.com> From: Anshuman Khandual In-Reply-To: <683CC7EC-3FAB-4C63-92F2-636C4DBE7729@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 4/26/24 20:03, Zi Yan wrote: > > > -- > Best Regards, > Yan, Zi > > On 26 Apr 2024, at 0:50, Anshuman Khandual wrote: > >> On 4/26/24 00:28, Zi Yan wrote: >>> +Anshuman, who changed pmd_present() semantics. See: >>> https://lore.kernel.org/all/1599627183-14453-2-git-send-email-anshuman.khandual@arm.com/ and commit b65399f6111b ("arm64/mm: Change >>> THP helpers to comply with generic MM semantics") >>> >>> On 25 Apr 2024, at 13:07, Ryan Roberts wrote: >>> >>>> __split_huge_pmd_locked() can be called for a present THP, devmap or >>>> (non-present) migration entry. It calls pmdp_invalidate() >>>> unconditionally on the pmdp and only determines if it is present or not >>>> based on the returned old pmd. This is a problem for the migration entry >>>> case because pmd_mkinvalid(), called by pmdp_invalidate() must only be >>>> called for a present pmd. >>>> >>>> On arm64 at least, pmd_mkinvalid() will mark the pmd such that any >>>> future call to pmd_present() will return true. And therefore any >>> >>> But pmd_mkinvalid() on x86 does not behave so. Maybe we should fix >>> pmd_mkinvalid() on arm64 by not setting PMD_PRESENT_INVALID when the >>> entry is invalid already. And add a test in mm/debug_vm_pgtable.c. >>> >>> I notice that x86, risc-v, mips behave the same. loongarch also >>> has _PAGE_PRESENT_INVALID bit set during pmd_mkinvalid(), but its >>> pmd_present() makes sure _PAGE_HUEG is set before checks _PAGE_PRESENT_INVALID. >>> So it is not a problem for loongarch. Add Huacai to confirm this. >>> >>> Maybe pmd_present() on arm64 can do that too? >> >> pmd_present() should return true even for a splitting PMD which is not >> currently mapped. IIRC in all other architectures, there is a distinct >> identification bit for huge page which stays back, even when the entry >> becomes unmapped. That bit helps pmd_present() return true, during PMD >> splitting process. >> >> But on arm64 platform >> >> #define PTE_VALID (_AT(pteval_t, 1) << 0) >> #define PMD_SECT_VALID (_AT(pmdval_t, 1) << 0) >> #define PMD_TABLE_BIT (_AT(pmdval_t, 1) << 1) >> >> When the PMD entry becomes unmapped, PTE_VALID gets cleared, so does the >> PMD_SECT_VALID, thus erasing its identity as a huge mapping. A software >> bit PMD_PRESENT_INVALID was added which helps in preserving that cleared >> huge page mapping identity once it becomes unmapped. > > OK. PMD_SECT_VALID is just a different name of PTE_VALID. I wonder Non PTE Level descriptor BIT[1:0] = X0 Invalid entry = 01 Block entries (Huge Pages) = 11 Table entries PTE Level descriptor BIT[1:0] = X0 Invalid entry = 01 Reserved invalid entry = 11 Page entries Although PTE_VALID and PMD_SECT_VALID share the same bit position, the huge page also requires the table bit to be cleared. > if ~PMD_TABLE_BIT can be used as _PAGE_HUGE to indicate it is a huge page > PMD, since PMD_TABLE_BIT is unset for PMD huge page already, for swap > entry, since PMD_SECT_VALID is unset, PMD_TABLE_BIT is ignored. But it > will require PTE and PMD have different swap entry encoding on arm64. > It might not be worth the effort. Right and also depending on just clearing of table bit for huge page mapping might be problematic in subtle ways. > >>> >>>> lockless pgtable walker could see the migration entry pmd in this state >>>> and start interpretting the fields as if it were present, leading to >>>> BadThings (TM). GUP-fast appears to be one such lockless pgtable walker. >>>> I suspect the same is possible on other architectures. >>>> >>>> Fix this by only calling pmdp_invalidate() for a present pmd. And for >>>> good measure let's add a warning to the generic implementation of >>>> pmdp_invalidate(). I've manually reviewed all other >>>> pmdp_invalidate[_ad]() call sites and believe all others to be >>>> conformant. >>>> >>>> This is a theoretical bug found during code review. I don't have any >>>> test case to trigger it in practice. >>>> >>>> Fixes: 84c3fc4e9c56 ("mm: thp: check pmd migration entry in common path") >>>> Signed-off-by: Ryan Roberts >>>> --- >>>> >>>> Applies on top of v6.9-rc5. Passes all the mm selftests on arm64. >>>> >>>> Thanks, >>>> Ryan >>>> >>>> >>>> mm/huge_memory.c | 5 +++-- >>>> mm/pgtable-generic.c | 2 ++ >>>> 2 files changed, 5 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/mm/huge_memory.c b/mm/huge_memory.c >>>> index 89f58c7603b2..80939ad00718 100644 >>>> --- a/mm/huge_memory.c >>>> +++ b/mm/huge_memory.c >>>> @@ -2513,12 +2513,12 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, >>>> * for this pmd), then we flush the SMP TLB and finally we write the >>>> * non-huge version of the pmd entry with pmd_populate. >>>> */ >>>> - old_pmd = pmdp_invalidate(vma, haddr, pmd); >>>> >>>> - pmd_migration = is_pmd_migration_entry(old_pmd); >>>> + pmd_migration = is_pmd_migration_entry(*pmd); >>>> if (unlikely(pmd_migration)) { >>>> swp_entry_t entry; >>>> >>>> + old_pmd = *pmd; >>>> entry = pmd_to_swp_entry(old_pmd); >>>> page = pfn_swap_entry_to_page(entry); >>>> write = is_writable_migration_entry(entry); >>>> @@ -2529,6 +2529,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, >>>> soft_dirty = pmd_swp_soft_dirty(old_pmd); >>>> uffd_wp = pmd_swp_uffd_wp(old_pmd); >>>> } else { >>>> + old_pmd = pmdp_invalidate(vma, haddr, pmd); >>>> page = pmd_page(old_pmd); >>>> folio = page_folio(page); >>>> if (pmd_dirty(old_pmd)) { >>>> diff --git a/mm/pgtable-generic.c b/mm/pgtable-generic.c >>>> index 4fcd959dcc4d..74e34ea90656 100644 >>>> --- a/mm/pgtable-generic.c >>>> +++ b/mm/pgtable-generic.c >>>> @@ -198,6 +198,7 @@ pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp) >>>> pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, >>>> pmd_t *pmdp) >>>> { >>>> + VM_WARN_ON(!pmd_present(*pmdp)); >>>> pmd_t old = pmdp_establish(vma, address, pmdp, pmd_mkinvalid(*pmdp)); >>>> flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); >>>> return old; >>>> @@ -208,6 +209,7 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address, >>>> pmd_t pmdp_invalidate_ad(struct vm_area_struct *vma, unsigned long address, >>>> pmd_t *pmdp) >>>> { >>>> + VM_WARN_ON(!pmd_present(*pmdp)); return pmdp_invalidate(vma, address, pmdp); >>>> } >>>> #endif >>>> -- >>>> 2.25.1 >>> >>> >>> -- >>> Best Regards, >>> Yan, Zi