From patchwork Sun Oct 1 16:00:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13405433 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61FC3E748E9 for ; Sun, 1 Oct 2023 16:00:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AE5A96B0192; Sun, 1 Oct 2023 12:00:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A93CD6B0194; Sun, 1 Oct 2023 12:00:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 90EEB6B0195; Sun, 1 Oct 2023 12:00:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 7EF186B0192 for ; Sun, 1 Oct 2023 12:00:19 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3DFCAB35F4 for ; Sun, 1 Oct 2023 16:00:19 +0000 (UTC) X-FDA: 81297354558.14.910FE44 Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) by imf11.hostedemail.com (Postfix) with ESMTP id 4D7B540013 for ; Sun, 1 Oct 2023 16:00:17 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FdkoEOJK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.53 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696176017; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=k884TE7KNx/WnBRBjm/8oTv2AQyhCRgzdEjEHuml8kg=; b=UXEd+7ZulPxBX3ff5H+yJCiAA7/2IQydyDXCBaV4ehMNMd90QZchMQjO/52VlakCmQu7dB aCbj8jzI5ga2WFXiHredFHMNEam9RDmGyrSi6REPpJNTYgeImWSXZbE4dgty9/mEKsAcx7 b+qQZffo/t0It3p8F8QnjnwVl71fST4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FdkoEOJK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.221.53 as permitted sender) smtp.mailfrom=lstoakes@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696176017; a=rsa-sha256; cv=none; b=AV2dNC+cfd37l3jozIvJkHJFj3XHykqQi9XCqaEhDnmPlQ5WUEowEfHoJce5wS5gLgZnDz 5lqjfieuiXb63e4popfenDTCBWyt/oVbpU++G/QfZEVxnlT5I6hgG6vK5nQjR2dLtcJ12m cE/z6MTdSNtgGoY91rzCR2CRB830EK8= Received: by mail-wr1-f53.google.com with SMTP id ffacd0b85a97d-3215f19a13aso15619665f8f.3 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=kvack.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=FdkoEOJKygWJUkHWHN7kvekbcHXcPpVm1lGTWafGbdOQGNCnq6qb1Tcbbg8oGkifxq LafuzSdja5ZUcC+f47tb+vr26Ikey2Hfl08wDtyyZY02NKf1KFdMdgdMroNicuK1oW3x j8kUYF/GLx1cCkpKadTwRXbUBEwO8ND7ImLnZw2DOKAEDCrK/q8Gt1/QyQ+ZgY0E+aqH Rh2aVWBKLmqtSgptz8JOEDbD164Mtz1ZqHN8rpOEDHtkzUjcYSZGGE83+CniuEDo6tkO M3fpP4ylc7o0sRTyial7kgm4wSJNgGTiGYPGE0+aAATstmtK9vkJW3gSNup/7Vuz3a5V h7IQ== 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=N+F3VOQHYNu6q6VN96zhbS3qRZnlEZgfGTn4+yQOAAH0QjlyLdKZcp4ieFZYCI9v1D kXX9j9rVkXHPboUMbW+REYTc4r3/afud/EU3bI13UBqhfHnDOWOklFZ6cLkp/ybeHsve G33PZGguV4D5dDti04phpdSdp68rAxcXPXNbAmrROP2eokdkNF4FB44YVKBKMp4YexcK wq8HR1ChvxiQymcVd1aAF9Hw6hfGvQwIWPVMhR19rm0duMuqiQy5/h8QHWy+aV2ytkNg JOcWlUC2FhpeO6Ssn9w/KNlkQUT4y0xPoQPec1cd+GyKGCptOrs/L0kboOfFOaHHgfAo //hg== X-Gm-Message-State: AOJu0YyM+IOuZHeE/RdYelerRWNrdnjVc9yM5OHwrxxCURKYJLMqQ+y2 oZcjxeUMvsQRPXxsrxveaXNTS8MGvgU= 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 X-Rspam-User: X-Stat-Signature: 1ufm6i7jjffxuek9ghe9w83x4iqyyx9j X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4D7B540013 X-HE-Tag: 1696176017-811020 X-HE-Meta: U2FsdGVkX1/mhpSNIQTnd/FSoY44avpAxDvJyS7L/LwxqsRvbcPDDcfDmIndH1u1cnHbxK5foByaWkbmUmSvq0NiGGZ3rPF50lVkzXKFlY8YcIuqdb4AdqY7ja16G5ANuGABMqHVBXOQgEkLdIpP4kuHwCfrnsHSasg4uSHPNBkX9z317siI2qxT6Eety2oIJQEkGcSiPrXMdzKBZG8gADtnrSN5AxxWshjpT/w8Av4Es+6Wlhi6N3ogaRKoeMnXseSTviRV7pWBiWOvnMYXhR60TDvnyz3hwRrj4AtzMlM46INchCaTvjhyxP+pPSe4o6Wz2hdcc9Hykf5DJXoMCbwlZiPlDizEQxqGQ/p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WGpTmD7eVNY//GIRRNK1XQ0H4+lvgufxp6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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, 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.