From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384987; b=sm6LpqrnP99pxs6KiiUPGecR+YbVzvuLFlW/gep1laRf5LJG2GdDsBjXOxlLcaWdy+7Y UJmZoeSuQbs5KOQj25eoYGMPI35QG4//YDaMUGa4j2pQJGUI3e52dCAJl2RgKhuFmAwF/ jNMc4YtmJL99RHvO9vVy3CZ2L9/Aek+IHA9XW/ONG2LC9yBkouheoXtaGSL7gnqcberzO pXTiLLjdnP9hNIB+5pR6hsvOSG8g88iiPcb2Pw770CWkU1yP0Ql1A11jedaFyu0lxpiFV /WkIfKfcuKNWZYpdvCLnfrSZ8u6cgJc+iK5SNJqaVYIbWUpFPFxNn0Blwwr8t1AyVgQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384987; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=OcDmCyn2TTMOW1lpEjMCBP9BkoMYHgJtujbBEKb1ZlY=; b=O99gQYzoXDOKl4PMa5AoE0bJ8mtWi2Xg4yobjsTRvJkfgt9/DqeBrnJHxRgNFChoR1Z3 fSDdeHSrrrNt7bYO1PQI7KqO80X7uM4+nKsH0kW3bZJiCFstk39Tt5AqN0cRmxteOwAGA IggF6T8dGx9foK2KJ5xWSbWPok+AwfhrTE7d/SWB2GabHDoL6W5KWKDzO4MD/uI8ruJxT nOy3DG4T9JNTRLT+z7kdV7n52Rn4gqukaP/0dpZEDwGLvCwDESuyMriztoLwktUZzZQND 81yZ4ymAkkmPz/Es8LzWJBttlaEJnzVQDNqS23l8GVfEAxSaApwlzsgnACQh7wN3dAw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uaaYI7Nj; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A88F606A32D; Fri, 4 Dec 2026 11:49:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=uaaYI7Nj Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17C291F000CE; Fri, 4 Dec 2026 11:49:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384987; bh=OcDmCyn2TTMOW1lpEjMCBP9BkoMYHgJtujbBEKb1ZlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=uaaYI7NjwQjA+yweM1hYmBJKmatI/ORTzSypflyhzfzmuxftWZrufb7ARnCn2EZiP v/+ebvRc7OYG+yx3xec5Yox4BSGUbVdho104BD4OMMh1N1vveEG/mNdZvVqPhg1pyW kBeXTcYoERgJIRqy1vpXWObJHf1xU9IxrAL4NfDo= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Andrew Morton , David Hildenbrand , Jason Gunthorpe , John Hubbard , Peter Xu , Lance Yang , Sasha Levin Subject: [PATCH 6.6 0332/1167] mm/gup: honour FOLL_PIN in NOMMU __get_user_pages_locked() Date: Fri, 4 Dec 2026 18:55:59 +0900 Message-ID: <20261204100710.650680258@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Greg Kroah-Hartman [ Upstream commit da01399b90a73cd00b502f7903fa5acbfac4a103 ] The !CONFIG_MMU implementation of __get_user_pages_locked() takes a bare get_page() reference for each page regardless of foll_flags: if (pages[i]) get_page(pages[i]); This is reached from pin_user_pages*() with FOLL_PIN set. unpin_user_page() is shared between MMU and NOMMU configurations and unconditionally calls gup_put_folio(..., FOLL_PIN), which subtracts GUP_PIN_COUNTING_BIAS (1024) from the folio refcount. This means that pin adds 1, and then unpin will subtract 1024. If a user maps a page (refcount 1), registers it 1023 times as an io_uring fixed buffer (1023 pin_user_pages calls -> refcount 1024), then unregisters: the first unpin_user_page subtracts 1024, refcount hits 0, the page is freed and returned to the buddy allocator. The remaining 1022 unpins write into whatever was reallocated, and the user's VMA still maps the freed page (NOMMU has no MMU to invalidate it). Reallocating the page for an io_uring pbuf_ring then lets userspace corrupt the new owner's data through the stale mapping. Use try_grab_folio() which adds GUP_PIN_COUNTING_BIAS for FOLL_PIN and 1 for FOLL_GET, mirroring the CONFIG_MMU path so pin and unpin are symmetric. Keep supporting the traditional behavior where users specify a pages array but don't set FOLL_GET. While at it, don't return NULL pointers in the page array, as this is really not expected for GUP users; instead, just fail and return -EFAULT. [ david: support traditional behavior with no FOLL_GET, extend description ] Cc: Andrew Morton Cc: David Hildenbrand Cc: Jason Gunthorpe Cc: John Hubbard Cc: Peter Xu Cc: Lance Yang Reported-by: Anthropic Fixes: 3faa52c03f44 ("mm/gup: track FOLL_PIN pages") Assisted-by: gkh_clanker_t1000 Signed-off-by: Greg Kroah-Hartman Tested-by: Lance Yang Link: https://patch.msgid.link/20260921-nommu_gup_pin-v4-1-c410041cc479@kernel.org Signed-off-by: David Hildenbrand (Arm) [ sashal: Kept unsigned long vm_flags; instead of upstream's vm_flags_t vm_flags; in the NOMMU __get_user_pages_locked() declarations context, since the vm_flags_t type is missing here. ] Signed-off-by: Sasha Levin --- mm/gup.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index af2435cd73d66..05b0a06623dac 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1716,6 +1716,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, struct vm_area_struct *vma; bool must_unlock = false; unsigned long vm_flags; + int ret, err = -EFAULT; long i; if (!nr_pages) @@ -1732,6 +1733,10 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, *locked = 1; } + /* See the MMU variant: support the traditional behavior. */ + if (pages && !(foll_flags & FOLL_PIN)) + foll_flags |= FOLL_GET; + /* calculate required read or write permissions. * If FOLL_FORCE is set, we only require the "MAY" flags. */ @@ -1752,8 +1757,14 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, if (pages) { pages[i] = virt_to_page((void *)start); - if (pages[i]) - get_page(pages[i]); + if (!pages[i]) + break; + ret = try_grab_folio(page_folio(pages[i]), 1, foll_flags); + if (unlikely(ret)) { + pages[i] = NULL; + err = ret; + break; + } } start = (start + PAGE_SIZE) & PAGE_MASK; @@ -1764,7 +1775,7 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, *locked = 0; } - return i ? : -EFAULT; + return i ? : err; } #endif /* !CONFIG_MMU */ -- 2.53.0