Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1158363rdb; Mon, 2 Oct 2023 00:17:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGLtwSsm2jRhLqn5UcyLH0qCWVFjW+vN69GdFtV5qYvUzH29znXgn9Co92FFXiuunTXv3eO X-Received: by 2002:a17:903:22cd:b0:1c6:2dbb:e5f4 with SMTP id y13-20020a17090322cd00b001c62dbbe5f4mr12125190plg.26.1696231066708; Mon, 02 Oct 2023 00:17:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696231066; cv=none; d=google.com; s=arc-20160816; b=GMlGwxUiJtXs3IHxMXoSWTZtv19fPHL/AabO5qVZ0XoiBLaxRUzWt8gRRf3fUKiQD9 pQ5xZQxad60RiQJrh2Rz8akOfsTjp4TJQzG6zKs77MkbADagVfLcFm2l+K+ZeGg889oc mxhbRhfxubKxTd/oqLaKbALIEkF7dKgosngRS4m1qqDFOJ8pZpQhMK4QgApcj/5eXUcF IdQ3tQqRxvUTK+VBwOxPmg1zwzP7ArlbqvNBL7Vml8nbZCILe7iFo2B1d4D/34vBsmQd lSlg/M8VGOF91co7MLd7iLGJZRjv/igMl0VZnlY5jnMCpgZHtjBpHfJiI25ma0ADPInP uN+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=k884TE7KNx/WnBRBjm/8oTv2AQyhCRgzdEjEHuml8kg=; fh=mNvOpR73OaUnRrr53gRvslddDUjaIrpQi7RNpkp0Veo=; b=G55mZ6RTqsghuxINmoyyckNA3aSoxocBg5saqccZIonEWcJ690Y8lrkp9GYomrWJGI Fcg1Z5HUIZgyXtg/vFuExLBxgSloO45NECxo838VkxRdVXAhVIIgOVfzcoQysQM4Hagw gZwCyZZx8asPIIIrfn27jMX/g7rjWm9y2zTnoXGBYpbBv3KTvdozoDQZzP7LUIGNbqhc WKHWAcU9kqYbjP+AWyaNoHX0okQeVGb2c2Svd4gssKawyHRjh6vUPG0Fs64iROINyoho 9/xEqyWdsJUPB4Ay7FtgNjHzcf/k+uTsssFTN5jmzvj6swOspxcVVcuAyji34aaARk4o lVcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=eC9v2aDJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id l3-20020a170903244300b001c0c79b3869si2096453pls.578.2023.10.02.00.17.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 00:17:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=eC9v2aDJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 16CA08070655; Sun, 1 Oct 2023 09:00:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235206AbjJAQA1 (ORCPT + 99 others); Sun, 1 Oct 2023 12:00:27 -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-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: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Sun, 01 Oct 2023 09:00:52 -0700 (PDT) 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 --- 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, unsigned long addr, struct page *page = get_user_page_vma_remote(mm, addr, gup_flags, &vma); - if (IS_ERR_OR_NULL(page)) { - err = page == NULL ? -EIO : PTR_ERR(page); + if (IS_ERR(page)) { + err = PTR_ERR(page); break; } 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); +/* 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 = get_user_pages_remote(mm, addr, 1, gup_flags, &page, NULL); + int got; + + if (unlikely(gup_flags & FOLL_NOWAIT)) + return ERR_PTR(-EINVAL); + + got = get_user_pages_remote(mm, addr, 1, gup_flags, &page, NULL); if (got < 0) return ERR_PTR(got); - if (got == 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 == 0); vma = 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, struct mm_struct *mm, gup_flags |= FOLL_SPLIT_PMD; /* Read the page with vaddr into memory */ old_page = 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); ret = verify_opcode(old_page, vaddr, &opcode); if (ret <= 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, unsigned long addr, struct page *page = get_user_page_vma_remote(mm, addr, gup_flags, &vma); - if (IS_ERR_OR_NULL(page)) { + if (IS_ERR(page)) { /* We might need to expand the stack to access it */ vma = vma_lookup(mm, addr); if (!vma) { @@ -5919,7 +5919,6 @@ static int __access_remote_vm(struct mm_struct *mm, unsigned long addr, continue; } - /* * Check if this is a VM_IO | VM_PFNMAP VMA, which * we can access using slightly different code. -- 2.42.0