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=1796387543; b=Ovvp+amPcvBYxuY2xCLnCxL1KoME91SmpA01uURFSLa9eti6cesJjtcLuwnmVgLY5q2T r8hTGgHb7WW9cXcwLNAjPa3oTXlJqsZ6gvakZ565wGHeES1nI7XgMkwyWNCDvSsE/WVpX x/unj7tPvHfrHDRLvR+Egnuae/3FwnHqXmcOshlCWk7pyfGmSDt0tQNO2+LEBW+c9y2Pb IS4bHT4oRhppI6WUZv/n3+ZUt5l/AiUkYvykIMQMT54EaMD5mOry5HTmIYvA7yngjNISa lBIj/lvqKCc6kZoEQTR2E0jZtOMg8KY253jxfC7fm/UPO+2Mt6APd6yeA2mwVsynDOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387543; 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=Jr4DUpAp4GXf+eJDfB1igoP5hYnHdWxguEUUcD5+Eu8=; b=P+QKiELEshnpWAEzN/xRESRTttjpDyX2sPD90i0i+OUOmkrMu1JTsXYSa+ImGfC9upvZ AVvuft7VCYErnq6QuawTDFIfpF1JlazNEl7JUB2C4PBhd+k233FOwD7EwqZYZNd08/s1V 0/HvNwE4Hi3Sh4mMlpBhF+ZViZynpysE04tVYkqmwlEj201jQq8NrIJDZ7MvdkUsl/fqI W4E2yDexJDRgNrw/pWXVL4avmz0C506FWCIFJGyVqrIiTFgHp7WSXgXAwOSCXeWdtiIxX apd9cnrjDy6HpKKlJL4tuOJ/+WN9CPl5UyPOZsQg/5Z0rfACHuvPpGTAJG6pXyRvPEA== 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=o0I3h2Hz; 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 C0C7D606A322; Fri, 4 Dec 2026 12:32:23 +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=o0I3h2Hz Received: by smtp.kernel.org (Postfix) with ESMTPSA id 428ED1F000C2; Fri, 4 Dec 2026 12:32:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387543; bh=Jr4DUpAp4GXf+eJDfB1igoP5hYnHdWxguEUUcD5+Eu8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=o0I3h2Hzyvwi6rQMWdQu8h26GrjfFmlxeYG1DupLT+veAKr1tvNqIYAmURvj1Rr85 2DZSguCit5KamDs153507tr0hr5/ni46ahsrOsjNw+sePh1BNvQL8EUvfZytlqecgb 7mRw5IARq+zi1TYqaoCE/rWZTEbZ1L39cJiZKeok= 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.1 0290/1059] mm/gup: honour FOLL_PIN in NOMMU __get_user_pages_locked() Date: Fri, 4 Dec 2026 18:57:01 +0900 Message-ID: <20261204100651.790117607@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@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.1-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: Replaced upstream's try_grab_folio() with try_grab_page(), keeping the FOLL_GET default, and dropped the ret/err locals, using the existing finish_or_fault label for failures. ] Signed-off-by: Sasha Levin --- mm/gup.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mm/gup.c b/mm/gup.c index 8629b2e145460..f209cfcb62add 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1743,6 +1743,10 @@ static long __get_user_pages_locked(struct mm_struct *mm, unsigned long start, unsigned long vm_flags; long i; + /* 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. */ @@ -1763,8 +1767,12 @@ 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]) + goto finish_or_fault; + if (unlikely(!try_grab_page(pages[i], foll_flags))) { + pages[i] = NULL; + goto finish_or_fault; + } } if (vmas) vmas[i] = vma; -- 2.53.0