From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F0E20606A680 for ; Thu, 3 Dec 2026 11:39:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=DZMfw62f Received: by kanga.kvack.org (Postfix) id EA52C6B05BE; Thu, 3 Dec 2026 06:39:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E54936B05BF; Thu, 3 Dec 2026 06:39:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1D246B05C0; Thu, 3 Dec 2026 06:39:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 2A9846B05BE for ; Thu, 3 Dec 2026 06:39:08 -0500 (EST) Received: from smtpin04.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A93024048A for ; Thu, 3 Dec 2026 11:39:07 +0000 (UTC) X-FDA: 85502475534.04.5900386 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf10.hostedemail.com (Postfix) with ESMTP id DBA48C0013 for ; Thu, 3 Dec 2026 11:39:05 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=DZMfw62f; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796297946; b=n3GvA/B87y9mOEm4z7C3jjptAG8oVN+2LZ+889lQcNsRlBcNZC4UTg4L87JnIR1viAKJLP IFWjVQ0l97vfQeehQzDU29GMcsqG2iMgY47Bts/P/xduRZuI7NWQxSQkGO7OszRNp2HjPn brz1ByYroiqcgTj29tCDlvqvUu0Zl90= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796297946; 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=akpQwuAlG1rC0IswKUJ903aOYKCkGoeN/9B+v48euOs=; b=jPAXjhmmOgMjHqNrTMRGHlrJTA00Lx0fu09kvQGAqM6fy3NKf4R7pd6D0Oib/dBVCEnhFJ dYQ8BVXFrefOm/HjZAlNHW7ENXrDszwAUzOo4J/EHfGg/LR4CYXSD3SjTsaxalXMdvyoOQ A4gh6cYIfel8upXA6G0WqReAtK2ZAqs= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=DZMfw62f; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of david@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=david@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 31E93402E1; Thu, 3 Dec 2026 11:39:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DDE8C1F00893; Thu, 3 Dec 2026 11:38:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796297945; bh=akpQwuAlG1rC0IswKUJ903aOYKCkGoeN/9B+v48euOs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=DZMfw62foUdMvq31hoKx8G99oTYPrPcehRgnDlsMVlet1vcNJEWxY0TkIK3Z1wfHx V0DC1ubKbGaiEuKhwmpSe3ffBqqLPPbB9HxfEs/3vSv1TWhu6HYozcjaWUSnd/YBHy aQcsrbtkRCW7OzLeFLVQTMoNpI5lFiCtirlvNax20Je2/qgtB/Y0d67cLVIxbN7ygr F9+2PkCKdNsj9NDTJcRxY1yMUHiNOf//t1L1Uy3krzGL4RJAzbQA3sojrgeyCLjKMP yjtN3Tbd3j1vrQ0Axs86WRgy8DoWxbzg6/UslgV7d2fGaz/EZikYMxwydvPqX6VrM5 YoWm4esjRUdCQ== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:28 +0100 Subject: [PATCH v2 05/35] mm/hugetlb: report reservations that we actually added from hugetlb_reserve_pages() X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-5-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , Sashiko , stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3089; i=david@kernel.org; h=from:subject:message-id; bh=/1JVq9wtIacWdIHyiF8Bpmv3iPUgfF7BkvF1pdEHJD0=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ6piH9vODHfhifoyUejMyxtFxxd+cg5ueud/gOGcm 4mJXfqXjlIWBjEOBlkxRZag4PsGL1+uq61242OGmcPKBDKEgYtTACYi+YKRYcs5u2kLe2+YM879 xh/SeW/NrfqwXZO27K6ffm86/zxx/VuMDBOiE6z2nZJ6P3FB4YlbwjZO3Gv0vayENgauMnrodEu DgwcA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: DBA48C0013 X-Rspam-User: X-Stat-Signature: frsifwun5eo5ygeruuhuoqhkx8itk8p3 X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796297945-799049 X-HE-Meta: U2FsdGVkX1/5kC2ZSRBuvKn6+/PpDhKwFDz1rEOQjqJuGPbHlJvZyIsAsJCW7iwe4iQfAYUJwCA80QkBscMAmlcFOqJzAIhZuDyHNmlDlZWNfSXyC3yLyGp0hLMThVA/XqP4LhHlN1EvuwMRG9lTUFX9fwpAOwyD0cIk0MQQHaFky36bmGwVoTJ+tdYvI4E/AmYuij4F0dtVg/tc9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BBQvdScHUiGlpv/nP+NHECdKALHRM6mvck0gFZSXOpYB0Sq5FPzTZIIgZekxMemWvbJ0XyRiwcxwJSBDux0BA0NEJ1kuQiadQETnJqgtfwnA4hsGlHLaGftPYpA0M5J1ynUinD7kUgotSfXOy0+Iw6bp2uN+q5GrX670HIpdGsePE3DF/EUA2xQedwpvQmeWDfRm/4YsFmPjy3sXybEp8PGj72iyGE32jyaFJqktBl37tmoeNQOc24DkoumBfkui/XBHIevyupnENcWr+qKU46Wwv8s+keKibvxk62RgBJkqSLn7gBS3AtehNtGC0cA== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 memfd_alloc_folio() uses hugetlb_reserve_pages() to reserve the index of the folio it is about to allocate. If allocating the folio or adding it to the page cache fails, it undoes that reservation, but only if hugetlb_reserve_pages() reported that it actually performed the reservation. If the index was already reserved, for example through an mmap() of the same file, memfd_alloc_folio() must not undo that reservation. However, if hugetlb_reserve_pages() races with another reservation for the same index, region_add_limited() can reserve fewer pages than region_chg() counted. hugetlb_reserve_pages() will then hand back the surplus charges, but still report the number of pages that region_chg() counted. Consequently, after racing with hugetlbfs_file_mmap() reserving the same index, memfd_alloc_folio() will undo the reservation of the mmap() caller on error, and a later fault at that index can fail with SIGBUS. Reproduced by artificially delaying hugetlb_reserve_pages() after region_chg() during IOMMU_IOAS_MAP_FILE of a hugetlb memfd, while another process mmap()s the memfd, and failing the page cache insertion with fault injection. Once all unreserved huge pages are in use, the mmap() caller gets SIGBUS on fault. Let's fix it by only reporting the reservations that we actually added. Other callers are not affected, as they only care about error vs. success, not the actual number of reserved pages. This has low urgency, as it requires an allocation failure after racing with an mmap() of the same file. Fixes: 717cf9357325 ("mm/memfd: reserve hugetlb folios before allocation") Reported-by: Sashiko Cc: stable@vger.kernel.org Signed-off-by: David Hildenbrand (Arm) --- mm/hugetlb.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 025aae6d0b72e..68d90c7297921 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6907,9 +6907,9 @@ long hugetlb_change_protection(struct vm_area_struct *vma, /* * Update the reservation map for the range [from, to]. * - * Returns the number of entries that would be added to the reservation map - * associated with the range [from, to]. This number is greater or equal to - * zero. -EINVAL or -ENOMEM is returned in case of any errors. + * Returns the number of huge pages that we reserved in the range [from, to], + * or a negative error code. On a later error, callers must only undo + * these reservations, not reservations that already existed in the range. */ long hugetlb_reserve_pages(struct inode *inode, @@ -7052,6 +7052,11 @@ long hugetlb_reserve_pages(struct inode *inode, rsv_adjust = hugepage_subpool_put_pages(spool, chg - add); hugetlb_acct_memory(h, -rsv_adjust); + /* + * Only report the reservations that we added, such that + * callers will not undo the racing reservations. + */ + chg = add; } else if (h_cg) { /* * The file_regions will hold their own reference to -- 2.56.1