From patchwork Mon Feb 26 08:19:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Shi X-Patchwork-Id: 10241553 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 60DC860386 for ; Mon, 26 Feb 2018 08:33:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4BBCA29BD5 for ; Mon, 26 Feb 2018 08:33:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4061729BFC; Mon, 26 Feb 2018 08:33:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 992C929BD5 for ; Mon, 26 Feb 2018 08:33:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:List-Subscribe:List-Help: List-Post:List-Archive:List-Unsubscribe:List-Id:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=d/3fPGZYHIPoJlFub2wY74syudZkXcF6MhnCPXP9F5A=; b=jnZZZcb702dbko cTqM1Ri6xEoIXSYjCi7vrLxyR6MRUms5f43wxjaCTWfM3kOkpqTv5fFf072TzMn6CV5CespXZpIXO OsR0LFAd70ajVZHM5Ris+R98gMB3Fk9ARO+NML3O58117vwZp/ykKOBFKRjEYULAS+nGkVvI5hcgk mqj+QubTyLCqtAq0mH53lGMASlm+/vuMoAdq+FH7r6TBBxGz9BeDF2BWZ5+OJlawsrCscLxBqyZYj fFn7B7ASVL8PXsprgzE6aw0Ia+539xcYKgVgoCQattynJatbKbRfr7vfZUmLEbkDwx27DJ315JZ5a WZOFRAKiEzCD65Nf1GSA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1eqEE3-00016h-3Y; Mon, 26 Feb 2018 08:33:35 +0000 Received: from mail-pl0-x241.google.com ([2607:f8b0:400e:c01::241]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1eqE3q-00010y-Ar for linux-arm-kernel@lists.infradead.org; Mon, 26 Feb 2018 08:23:50 +0000 Received: by mail-pl0-x241.google.com with SMTP id 31so8855188ple.9 for ; Mon, 26 Feb 2018 00:22:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=JeYitV85vYUZHZtgTIJwAzhhdYjQpmFJh73a4N4z6FM=; b=WuEnGHaWFEDfKx3hNgfDxwVUV612UXDp7O3vkuvzzYqTTTrRDssZ4a0LQ5AbaWbII2 hzmuQlibDWAxkE6coVIxiK06vBbRzCmkQ2mfCW0sEhnlQG+lR4WUzlEmRlj1dMSBCngG q5Sd4quYSTPM2z5cEOKOj/QcT793ca0IgWZtQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=JeYitV85vYUZHZtgTIJwAzhhdYjQpmFJh73a4N4z6FM=; b=jwjaMO9QkXwNz7SxlhRSfysxPVkcwvnBUnq25osjIktL0+2dxD24MF/7fSnMTSnNUY gej9Vwd4y6Ngs6T1EYiE4xOEQlzlgdUmA6MfBeEgFK+91ixyd8ZEJz5OsRUesom1Rfxg sxjoldRZO0Fc7T9C+c0KULOmhuzM0TxkSk7yQGyuNeFbWUA28u0H45uuM88vJcsOKUsx 1k8BvcYRII9xqRzpf3aOfNO+agKE8Ic6CI3fQNE+pTs8gUS7+AVZWrEuBEMIPfLD4eC6 jqShfUMN0Ike5+g+8Wa6X8MaPMK3YuNrVg4K7aCJhx6+v2/tT7muvmHECkvs0RCBgC+S e8mg== X-Gm-Message-State: APf1xPCZBix1cmgK86m5hpYSR9LE4Yxsgu6gzwXupRud64Msk+LspE3X BTH2JgEi1JCwySa78nkMlO5xIw== X-Google-Smtp-Source: AH8x226w/9Wsbte96bAfrpbikBlonv3gmr9I338q9sxKwmjAdlYJUcr4VADDoYfpBH9cV//IZpXvPA== X-Received: by 2002:a17:902:34f:: with SMTP id 73-v6mr9561276pld.55.1519633370036; Mon, 26 Feb 2018 00:22:50 -0800 (PST) Received: from localhost.localdomain (176.122.172.82.16clouds.com. [176.122.172.82]) by smtp.gmail.com with ESMTPSA id o86sm1422706pfi.87.2018.02.26.00.22.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Feb 2018 00:22:49 -0800 (PST) From: Alex Shi To: Marc Zyngier , Will Deacon , Ard Biesheuvel , Catalin Marinas , stable@vger.kernel.org, linux-arm-kernel@lists.infradead.org (moderated list:ARM64 PORT (AARCH64 ARCHITECTURE)), linux-kernel@vger.kernel.org (open list) Subject: [PATCH 14/52] arm64: uaccess: Don't bother eliding access_ok checks in __{get, put}_user Date: Mon, 26 Feb 2018 16:19:48 +0800 Message-Id: <1519633227-29832-15-git-send-email-alex.shi@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519633227-29832-1-git-send-email-alex.shi@linaro.org> References: <1519633227-29832-1-git-send-email-alex.shi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180226_002303_016801_D1F98975 X-CRM114-Status: GOOD ( 13.28 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Will Deacon commit 84624087dd7e upstream. access_ok isn't an expensive operation once the addr_limit for the current thread has been loaded into the cache. Given that the initial access_ok check preceding a sequence of __{get,put}_user operations will take the brunt of the miss, we can make the __* variants identical to the full-fat versions, which brings with it the benefits of address masking. The likely cost in these sequences will be from toggling PAN/UAO, which we can address later by implementing the *_unsafe versions. Reviewed-by: Robin Murphy Signed-off-by: Will Deacon Signed-off-by: Catalin Marinas Signed-off-by: Alex Shi Conflicts: keep __{get/put}_user_unaligned in arch/arm64/include/asm/uaccess.h --- arch/arm64/include/asm/uaccess.h | 54 ++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/arch/arm64/include/asm/uaccess.h b/arch/arm64/include/asm/uaccess.h index 00025c5..ffa4e39 100644 --- a/arch/arm64/include/asm/uaccess.h +++ b/arch/arm64/include/asm/uaccess.h @@ -248,30 +248,35 @@ do { \ (x) = (__force __typeof__(*(ptr)))__gu_val; \ } while (0) -#define __get_user(x, ptr) \ +#define __get_user_check(x, ptr, err) \ ({ \ - int __gu_err = 0; \ - __get_user_err((x), (ptr), __gu_err); \ - __gu_err; \ + __typeof__(*(ptr)) __user *__p = (ptr); \ + might_fault(); \ + if (access_ok(VERIFY_READ, __p, sizeof(*__p))) { \ + __p = uaccess_mask_ptr(__p); \ + __get_user_err((x), __p, (err)); \ + } else { \ + (x) = 0; (err) = -EFAULT; \ + } \ }) #define __get_user_error(x, ptr, err) \ ({ \ - __get_user_err((x), (ptr), (err)); \ + __get_user_check((x), (ptr), (err)); \ (void)0; \ }) #define __get_user_unaligned __get_user -#define get_user(x, ptr) \ +#define __get_user(x, ptr) \ ({ \ - __typeof__(*(ptr)) __user *__p = (ptr); \ - might_fault(); \ - access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \ - __p = uaccess_mask_ptr(__p), __get_user((x), __p) : \ - ((x) = 0, -EFAULT); \ + int __gu_err = 0; \ + __get_user_check((x), (ptr), __gu_err); \ + __gu_err; \ }) +#define get_user __get_user + #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \ asm volatile( \ "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \ @@ -314,30 +319,35 @@ do { \ uaccess_disable_not_uao(); \ } while (0) -#define __put_user(x, ptr) \ +#define __put_user_check(x, ptr, err) \ ({ \ - int __pu_err = 0; \ - __put_user_err((x), (ptr), __pu_err); \ - __pu_err; \ + __typeof__(*(ptr)) __user *__p = (ptr); \ + might_fault(); \ + if (access_ok(VERIFY_WRITE, __p, sizeof(*__p))) { \ + __p = uaccess_mask_ptr(__p); \ + __put_user_err((x), __p, (err)); \ + } else { \ + (err) = -EFAULT; \ + } \ }) #define __put_user_error(x, ptr, err) \ ({ \ - __put_user_err((x), (ptr), (err)); \ + __put_user_check((x), (ptr), (err)); \ (void)0; \ }) #define __put_user_unaligned __put_user -#define put_user(x, ptr) \ +#define __put_user(x, ptr) \ ({ \ - __typeof__(*(ptr)) __user *__p = (ptr); \ - might_fault(); \ - access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \ - __p = uaccess_mask_ptr(__p), __put_user((x), __p) : \ - -EFAULT; \ + int __pu_err = 0; \ + __put_user_check((x), (ptr), __pu_err); \ + __pu_err; \ }) +#define put_user __put_user + extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n); extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n); extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);