From nobody Thu Jan 8 10:55:50 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A5F4E748E9 for ; Sun, 1 Oct 2023 16:00:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235222AbjJAQA2 (ORCPT ); Sun, 1 Oct 2023 12:00:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235155AbjJAQAY (ORCPT ); Sun, 1 Oct 2023 12:00:24 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBF87E1 for ; Sun, 1 Oct 2023 09:00:16 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-32157c8e4c7so15624050f8f.1 for ; Sun, 01 Oct 2023 09:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696176015; x=1696780815; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=k884TE7KNx/WnBRBjm/8oTv2AQyhCRgzdEjEHuml8kg=; b=eC9v2aDJav36tUfYswA4Ze3QQ6KNhS2sPi3X+0XZvGEt6RiOGnHEFN7ks5WJKS5GVK 8RD9V2mf2jzuPrCFcosWNXr2/J3xsXoKC62g/7LHBWYPoFF0CN4/rGKNwD6f5bbSLWOm Hd8EKWgLBo124NZERmoyUeVzdsWMsqZI2N6ggQPptylgZ0mqqn1eey6aH+kqcBpFDQ77 0DttRbbNSUNaflltsL/j3ukJEaQ085f4JCF3tgTMvA2XQI02BOVBy2iUF9dzKLlNhObp 3FjPgeAqz3UoRHKdyKVpK0X30L8lpFxA8BEiH5Ms5b9EB3apVrK20scqaWg/1iBRg2V9 /W7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696176015; x=1696780815; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=k884TE7KNx/WnBRBjm/8oTv2AQyhCRgzdEjEHuml8kg=; b=Cc37zSFpMSfPlBb8aNtT9fH391un6/U6bggvzUvkKNFZIfEN6DDMg9QsWu623nDyM4 qClonr+xPwd6Y8H9PRdSkJrY5efhAZy8Ftz/MxQ9Fdf24VOm4sObHbFDtjbVh9Sn5f1+ 3VQbFSPLydJTNU/0Ua1M19vHqn64ZIbw57M7fhaAP2DOCzf9mKqnRckQUzPh4tmuZlF8 B9jGLkmX11YnB/FdqXrsCvclaKnxIkJf4oEdHRxYw5qJELpLDD4DD/9HhX8lJGj827Sy nhtTdWYR9uIQn37uRgvIn2p3c3iYgU6UCc+DBmAcJ/WUj05kkS4ICZzNvEUuTk/Z2rx/ xB1Q== X-Gm-Message-State: AOJu0YwHxlnjgIqzoR5MMZwViK2mewEQ+7uT+cUrgOpNNgIVA7v+aCA4 Q8DGBIZ/ME0SgnVSyME9kxI= X-Google-Smtp-Source: AGHT+IFz6TUSlKf+sA6AqE1HJa6bCLgZ7TQdxdFdV0t++vM23Tcc6lgRka6dafHVYFayTJul4FVAXw== X-Received: by 2002:a5d:6e53:0:b0:321:5971:23a5 with SMTP id j19-20020a5d6e53000000b00321597123a5mr7951114wrz.20.1696176014911; Sun, 01 Oct 2023 09:00:14 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id s16-20020a05600c045000b0040536dcec17sm5487695wmb.27.2023.10.01.09.00.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 09:00:14 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Catalin Marinas , Will Deacon , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland Cc: Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Oleg Nesterov , Richard Cochran , Jason Gunthorpe , John Hubbard , Arnd Bergmann , Lorenzo Stoakes Subject: [PATCH 4/4] mm/gup: adapt get_user_page_vma_remote() to never return NULL Date: Sun, 1 Oct 2023 17:00:05 +0100 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" get_user_pages_remote() will never return 0 except in the case of FOLL_NOWAIT being specified, which we explicitly disallow. This simplifies error handling for the caller and avoids the awkwardness of dealing with both errors and failing to pin. Failing to pin here is an error. Suggested-by: Arnd Bergmann Signed-off-by: Lorenzo Stoakes Acked-by: Catalin Marinas Reviewed-by: Jason Gunthorpe --- arch/arm64/kernel/mte.c | 4 ++-- include/linux/mm.h | 16 +++++++++++++--- kernel/events/uprobes.c | 4 ++-- mm/memory.c | 3 +-- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 4edecaac8f91..8878b392df58 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -411,8 +411,8 @@ static int __access_remote_tags(struct mm_struct *mm, u= nsigned long addr, struct page *page =3D get_user_page_vma_remote(mm, addr, gup_flags, &vma); =20 - if (IS_ERR_OR_NULL(page)) { - err =3D page =3D=3D NULL ? -EIO : PTR_ERR(page); + if (IS_ERR(page)) { + err =3D PTR_ERR(page); break; } =20 diff --git a/include/linux/mm.h b/include/linux/mm.h index 7b89f7bd420d..da9631683d38 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2425,6 +2425,7 @@ long pin_user_pages_remote(struct mm_struct *mm, unsigned int gup_flags, struct page **pages, int *locked); =20 +/* Either retrieve a single VMA and page, or an error. */ static inline struct page *get_user_page_vma_remote(struct mm_struct *mm, unsigned long addr, int gup_flags, @@ -2432,12 +2433,21 @@ static inline struct page *get_user_page_vma_remote= (struct mm_struct *mm, { struct page *page; struct vm_area_struct *vma; - int got =3D get_user_pages_remote(mm, addr, 1, gup_flags, &page, NULL); + int got; + + if (unlikely(gup_flags & FOLL_NOWAIT)) + return ERR_PTR(-EINVAL); + + got =3D get_user_pages_remote(mm, addr, 1, gup_flags, &page, NULL); =20 if (got < 0) return ERR_PTR(got); - if (got =3D=3D 0) - return NULL; + + /* + * get_user_pages_remote() is guaranteed to not return 0 for + * non-FOLL_NOWAIT contexts, so this should never happen. + */ + VM_WARN_ON(got =3D=3D 0); =20 vma =3D vma_lookup(mm, addr); if (WARN_ON_ONCE(!vma)) { diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c index 3048589e2e85..435aac1d8c27 100644 --- a/kernel/events/uprobes.c +++ b/kernel/events/uprobes.c @@ -474,8 +474,8 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, st= ruct mm_struct *mm, gup_flags |=3D FOLL_SPLIT_PMD; /* Read the page with vaddr into memory */ old_page =3D get_user_page_vma_remote(mm, vaddr, gup_flags, &vma); - if (IS_ERR_OR_NULL(old_page)) - return old_page ? PTR_ERR(old_page) : 0; + if (IS_ERR(old_page)) + return PTR_ERR(old_page); =20 ret =3D verify_opcode(old_page, vaddr, &opcode); if (ret <=3D 0) diff --git a/mm/memory.c b/mm/memory.c index e2743aa95b56..f2eef3d1cf58 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5905,7 +5905,7 @@ static int __access_remote_vm(struct mm_struct *mm, u= nsigned long addr, struct page *page =3D get_user_page_vma_remote(mm, addr, gup_flags, &vma); =20 - if (IS_ERR_OR_NULL(page)) { + if (IS_ERR(page)) { /* We might need to expand the stack to access it */ vma =3D vma_lookup(mm, addr); if (!vma) { @@ -5919,7 +5919,6 @@ static int __access_remote_vm(struct mm_struct *mm, u= nsigned long addr, continue; } =20 - /* * Check if this is a VM_IO | VM_PFNMAP VMA, which * we can access using slightly different code. --=20 2.42.0