Received: by 2002:ab2:3c46:0:b0:1f5:f2ab:c469 with SMTP id x6csp112014lqf; Fri, 26 Apr 2024 00:50:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXk9FqdM4/OUVofa+9sMBSN+ROOFGUA/kztweFWqtGdUP6bYfrd5lhvUAJ4tMQJZT6jr+oBHeA6cnjwm1UD5NOIeO2bWUbMF4IUIt5+1Q== X-Google-Smtp-Source: AGHT+IHvdbNaSN3QQFixjjxwJnGkwEA6UeFkbsT0FG+SHkG6zo3VCgK1jwC8za4WAFQNS9NOYsrM X-Received: by 2002:a17:902:7803:b0:1e6:114c:2e54 with SMTP id p3-20020a170902780300b001e6114c2e54mr1855230pll.69.1714117852080; Fri, 26 Apr 2024 00:50:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714117852; cv=pass; d=google.com; s=arc-20160816; b=YtjM1G3b9o4St3yc+fMCWiBlsLi3TUQ04B5xxQc3H804XWKMqQR07JsfV/0lXr6wrI FHl+B15SjVZCBuwkpMpBeXSwpyAZZsFT6UeRTvuNr6dmnv8cA8UKg1GqkdZ6N+9nhj+0 5SjbZa5/MlkFhmLUB78//0jIsVxm/vQlHysckuL5wUSrC5WCaH9q7m3umYFbmNNY3S0k 4k/TETxHQNaPGhhY0oWjnlLfCu5UUmPo91AO7L4i77wgUgnqOM9SZlLE5oTAb/20gqlT ba4nm9yQZQ5jEhUTaNh+HGJC5Lbn2ydfts98AeNA8maEWYKWsQV8yrXtsGu85WWO44yY wn1A== 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=V+cySVAcDaBtP3xCBymauMiq/8oy5WPgaD1+PaMY1fY=; fh=6Zd7/HvDPa7ksP/hh/sjK6zA0IkZdNDCDlEz5fGec1Y=; b=ZoP8RS2yq7xQhw4MVq/KlGdFDgENV2Af+Zjdj8PCiupzzGYbcknjwLfraCvEmx3X+A Bkuopb1cwzvF3x56hP9Dx9zxgalcM9/ygEOi8UihgMPzT5YoYiaVwRyxCk8Ac9n/U9f3 IbsuuJdY/wiNqkyb9dp2doxPHT0ru9y2NZcri0Is8PtkGCY9IGZFZ7pi17x4D1CXVE3w LtId6O6YB7EGaAaCw4PJLXsiqmCLBC+0scBwKBLC4WUOIunmWEHr9CQZMByGwI2bfGeJ NAXvpbhKKgZT0UBQnGiwRkOUHs2b5jdCsyhonrehz8iNI+/W+lyuHSHMokOv8alsXxMk aQ3Q==; 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-159660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159660-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id az2-20020a170902a58200b001e444a6cd8bsi14091825plb.145.2024.04.26.00.50.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Apr 2024 00:50:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-159660-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159660-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id E36592876F7 for ; Fri, 26 Apr 2024 07:48:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A3A5413C9D1; Fri, 26 Apr 2024 07:48:12 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F16B13C9A1 for ; Fri, 26 Apr 2024 07:48:10 +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=1714117692; cv=none; b=RopQojvloQii8l5eaIi9M3gZ0oR7nAnL4uCvOOl4y/CUsKwgdp3C5gIBUJJRgOWOsaZ0klakgw90RLBpGCvxSG3X00fUKiaKFm4QC74sUIpaMVFrRTzx2CaspMVuwOwjC2rB1Wm4+q2bd5m8nIo2+ZK5gSlaPIpzTE6a8oIT+9Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714117692; c=relaxed/simple; bh=708DFfk87yyJ5OvirFxuGM3SRGCohNCdIYHuOt/WdS8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=GszOwFsIHS08JRPaJKboU4J7MfYhsEeHt8cRzXqDYJua5UEMBolfFH7mTTzDEtEboPForKMSJsfjKGFOA9qkc+G0BrrBr/enQoVvgxeuBfTbYTRKEGwpsZX0Treab2KvK7vaJIfYgZ+xjO5BCyBzvBg2vFfj0oSok+VU9LsDNUk= 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 ACEAD2F4; Fri, 26 Apr 2024 00:48:37 -0700 (PDT) Received: from [10.57.64.176] (unknown [10.57.64.176]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8798F3F793; Fri, 26 Apr 2024 00:48:08 -0700 (PDT) Message-ID: <35210542-1309-4330-9367-e4a597be59fc@arm.com> Date: Fri, 26 Apr 2024 08:48:07 +0100 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-GB To: Zi Yan Cc: Andrew Morton , "Aneesh Kumar K.V" , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Anshuman Khandual , Huacai Chen References: <20240425170704.3379492-1-ryan.roberts@arm.com> <922F6794-1C32-4862-8A67-90D4F7DBC474@nvidia.com> From: Ryan Roberts In-Reply-To: <922F6794-1C32-4862-8A67-90D4F7DBC474@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 25/04/2024 19:58, 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. Yes, we *could* make pmd_mkinvalid() defensive. But we don't do that for the other getters/setters (e.g. pte_mkwrite()). So not sure why we would want that here. Ultimately it makes no semantic sense to invalidate a non-present pmd. See my other mail for excessive detail. Thanks, Ryan > > 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?> >> 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