From patchwork Thu Jul 25 18:39:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13742091 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 DBBCAC3DA49 for ; Thu, 25 Jul 2024 18:40:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 688EF6B0083; Thu, 25 Jul 2024 14:40:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6101D6B0088; Thu, 25 Jul 2024 14:40:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D8726B0089; Thu, 25 Jul 2024 14:40:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2C8506B0083 for ; Thu, 25 Jul 2024 14:40:15 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D0AD51C2235 for ; Thu, 25 Jul 2024 18:40:14 +0000 (UTC) X-FDA: 82379139948.23.1756407 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 2256CA0028 for ; Thu, 25 Jul 2024 18:40:12 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=N5Uyqllc; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1721932764; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=825KmH6PFbzryItWTiXJBmfjya/+p9AIE9EWqywGW+c=; b=gKCGNIJ4fnGEVL9WN+Y0cTfdj2DvhnGzBJC/14k6JBRrvvyTNXYDfb2PImWRFsJGQPlhwd AkulmhgFEyjNX8A4Lxkly2ZV+kgSKQQAroufU541mFUunx3b1j8WKaLsNNHA8Q1JjpREAo z01FU+kDW2SDaBGWuFKGUw8Irayqv2I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1721932764; a=rsa-sha256; cv=none; b=FKWjEIV3uib74l6k1hFXlO1WlWqCK5qvOnQayMWzW1OhURyLlF65eBmh/xMYybsUxbXfeY y3gj1BGqCc72eFKIYcG1njDXch1NU+MOYLNdTfMDgowfrJntSJDTY4zuPt59wrNQPXyIOe 89V/wfZMSTTa/3H2yJgjj8czvdSm5G8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=N5Uyqllc; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721932812; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=825KmH6PFbzryItWTiXJBmfjya/+p9AIE9EWqywGW+c=; b=N5UyqllcEKBKb51EAUWoZnN3KR7C6qdVcz6JvKj5W9QfZflONX1yvDDvVopnrtlA8tlS8D spmX2bcxbIy0ws79L7MtDwMsRJcFBLYameW8pMRKXGTe1PbmUDJl1tveF0vNsTFGeb1CkW s7L/C5ua+XOTnExHAhGeMBY6jC8Uj58= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-457-s1hCBgoxNB6O3FQHM27urw-1; Thu, 25 Jul 2024 14:40:08 -0400 X-MC-Unique: s1hCBgoxNB6O3FQHM27urw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CB20F1955D4B; Thu, 25 Jul 2024 18:40:06 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.192.30]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BE23F1955D42; Thu, 25 Jul 2024 18:40:03 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Muchun Song , Peter Xu , Oscar Salvador , stable@vger.kernel.org Subject: [PATCH v1 2/2] mm/hugetlb: fix hugetlb vs. core-mm PT locking Date: Thu, 25 Jul 2024 20:39:55 +0200 Message-ID: <20240725183955.2268884-3-david@redhat.com> In-Reply-To: <20240725183955.2268884-1-david@redhat.com> References: <20240725183955.2268884-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 2256CA0028 X-Stat-Signature: ybz8xacace537z8qom679nanmsbp4348 X-HE-Tag: 1721932812-565825 X-HE-Meta: U2FsdG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PHfRtWkiGSnXfvHDGV+Xa/hJtcyWw4eVngzTxz7fs0L7xwY1RM+l7Rrx08472yUcSem6pvalvU+PZKjz1JfYYXoHXI+7+IjGDFTRNk3To9w9oLNykBxl7fXtU8gHbE8r2nULXOHVCxnWQKuBI6SdVUNzc1C1b719xXT0MkZket9zliw2FYBKPdu2j8C7CpSevVS96Im1L8ZWrH/1PgVyKXiSZTupYkl2BAimRpzfZNbXLBXHUTkvOFubuYiI3VbY9Xesy2C4WV90= 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: List-Subscribe: List-Unsubscribe: We recently made GUP's common page table walking code to also walk hugetlb VMAs without most hugetlb special-casing, preparing for the future of having less hugetlb-specific page table walking code in the codebase. Turns out that we missed one page table locking detail: page table locking for hugetlb folios that are not mapped using a single PMD/PUD. Assume we have hugetlb folio that spans multiple PTEs (e.g., 64 KiB hugetlb folios on arm64 with 4 KiB base page size). GUP, as it walks the page tables, will perform a pte_offset_map_lock() to grab the PTE table lock. However, hugetlb that concurrently modifies these page tables would actually grab the mm->page_table_lock: with USE_SPLIT_PTE_PTLOCKS, the locks would differ. Something similar can happen right now with hugetlb folios that span multiple PMDs when USE_SPLIT_PMD_PTLOCKS. Let's make huge_pte_lockptr() effectively uses the same PT locks as any core-mm page table walker would. There is one ugly case: powerpc 8xx, whereby we have an 8 MiB hugetlb folio being mapped using two PTE page tables. While hugetlb wants to take the PMD table lock, core-mm would grab the PTE table lock of one of both PTE page tables. In such corner cases, we have to make sure that both locks match, which is (fortunately!) currently guaranteed for 8xx as it does not support SMP. Fixes: 9cb28da54643 ("mm/gup: handle hugetlb in the generic follow_page_mask code") Cc: Signed-off-by: David Hildenbrand Reviewed-by: Baolin Wang Acked-by: Muchun Song Reviewed-by: Peter Xu Reviewed-by: Oscar Salvador --- include/linux/hugetlb.h | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index c9bf68c239a01..da800e56fe590 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -944,10 +944,29 @@ static inline bool htlb_allow_alloc_fallback(int reason) static inline spinlock_t *huge_pte_lockptr(struct hstate *h, struct mm_struct *mm, pte_t *pte) { - if (huge_page_size(h) == PMD_SIZE) + VM_WARN_ON(huge_page_size(h) == PAGE_SIZE); + VM_WARN_ON(huge_page_size(h) >= P4D_SIZE); + + /* + * hugetlb must use the exact same PT locks as core-mm page table + * walkers would. When modifying a PTE table, hugetlb must take the + * PTE PT lock, when modifying a PMD table, hugetlb must take the PMD + * PT lock etc. + * + * The expectation is that any hugetlb folio smaller than a PMD is + * always mapped into a single PTE table and that any hugetlb folio + * smaller than a PUD (but at least as big as a PMD) is always mapped + * into a single PMD table. + * + * If that does not hold for an architecture, then that architecture + * must disable split PT locks such that all *_lockptr() functions + * will give us the same result: the per-MM PT lock. + */ + if (huge_page_size(h) < PMD_SIZE) + return pte_lockptr(mm, pte); + else if (huge_page_size(h) < PUD_SIZE) return pmd_lockptr(mm, (pmd_t *) pte); - VM_BUG_ON(huge_page_size(h) == PAGE_SIZE); - return &mm->page_table_lock; + return pud_lockptr(mm, (pud_t *) pte); } #ifndef hugepages_supported