From patchwork Thu Mar 1 12:53:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Shi X-Patchwork-Id: 10251409 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 9F84660211 for ; Thu, 1 Mar 2018 13:32:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8DDF5201B1 for ; Thu, 1 Mar 2018 13:32:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8267B201F5; Thu, 1 Mar 2018 13:32:07 +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 50D6E201B1 for ; Thu, 1 Mar 2018 13:32:06 +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:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=nwoj0WvsFWnAFv/YWu5VeT1MrsZrGoAfX5vgUQQf0oc=; b=gK1AmlZuFSipeHKEoXyNf1Qc2Y 6DXY6m51fO6NOP9mDNDNedsNIsR4d9P2J5Av5YdPYEzssl3ggrbiAdYEHzYPlYds2fr17z1d7/gJ4 RQWUig4WVoZKNIxacOR+MMZVrMyGtjw4nB9rE76AOJik+LUBg3dNf8L/2Aqyp2Q9cnvykOPIsG16w hNS1Vx4xYZ2Sy/yI/5Akwcu51pJmt/CXfFyickASiq4vhR/Ff9QmmAnRd4MGSWfE98NKVQhEiOmif E0YJqRi3SB4dWUc/g9Kdh+WlhO4yZebAGj4TWP5HWUQJGZRMi09hxcc5oup8wSLKIsT2ITJb9Y6kt XqnHqmVQ==; 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 1erOJP-0004QQ-TY; Thu, 01 Mar 2018 13:31:55 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1erO0Z-0005Ub-DV for linux-arm-kernel@bombadil.infradead.org; Thu, 01 Mar 2018 13:12:27 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=B06UrOpFOhDhk8CXgd0QbaeXaNIGUHvFPN317fRlFQ4=; b=SYvMihbqCtT42O9ObL668Ckof EUYk6oV4NIU5sXr4XE+yQpRGAP24BLJpRKxDCskGuAtUZjc4v3q0k19cbnNcxqr2UWSQC41DB+T0h 3vMBbOBw64GrRKjCUTqeET8eF1liLsAlv8PBP3butF9/cwZedRNy1JWIpAz0+IYgfHeadGr0LVg4O jIT9R+Myow2JAeYziKAsBNnflKxH/xJj7Kc0gts0vHsMIdELN/sJdUK5si9Ij7u1X0Dv8BqVXvYOA ecKq47KI8z+RKj/D4gfxumXwv1FhvdfARI6ENXLHJCpcjXFeQc/0eHPxgbkjGfBT7kiaqIq8ttdk/ cibQbVVNQ==; Received: from mail-pg0-x243.google.com ([2607:f8b0:400e:c05::243]) by casper.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1erNlp-00051v-Rr for linux-arm-kernel@lists.infradead.org; Thu, 01 Mar 2018 12:57:16 +0000 Received: by mail-pg0-x243.google.com with SMTP id l24so2273246pgc.5 for ; Thu, 01 Mar 2018 04:57:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=B06UrOpFOhDhk8CXgd0QbaeXaNIGUHvFPN317fRlFQ4=; b=D/+FhXt5iKbRmF6usXiWGMZw6TVW3dhAcUiL4cOj1ExLjn8byPNEftelFZB6NgiL6m hDnmkPfXNAEK64ROsIPjHe3v4lDxFzNZOUU+ZAQLu/W69DNNMJx3CFpu5CyaGi6vQ0qG NeiU3VeNURVbqPLyJIEQ/wfKw/7LULtU41Qlk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=B06UrOpFOhDhk8CXgd0QbaeXaNIGUHvFPN317fRlFQ4=; b=X/lREyhljMtx9VqaLTP82FXG10hWAUb1VRN+STVJ+Wcl3qof9mF0ulSpJsuow86EMe gd9oOwRYFeElTwt1IR0TYTW39MJrX6+dPWCTkOOHPBIUVwPk97Q24aIdKnqf6vaJHvWp AVr9F6a+im2B6h3b7rS9GdXdyTJIEQegB2r4RwHTMdaTXcrvmz9GcTCoQQf7dkqG5rSl QdSD7NQQMCiM7N/r4xofvwvgzHb7N2SEl7e6EPstBtn3TUtVGXlYeSi3EHYeHjAy8/J5 ODfP0Ag6E7KHjJ+40WE96j/nOPXfBAXP55J08LYtnnJdzQtYSgTcnvXl6jc10wHrxkhR JiTQ== X-Gm-Message-State: APf1xPC29cNUBcQgBBpai7uwEtoyEQJNIcyJ0Ync0HisQvTym3Aw/M3J upNQzSs1N9vnbDujDquHEo6jAA== X-Google-Smtp-Source: AG47ELsj8/N9BmPe04eqb9FPvC24xYKDpVP8wgcHlRMGFnwC+82inZWq/GwfEgDId/anSFRAIo99yA== X-Received: by 10.101.69.4 with SMTP id n4mr1510244pgq.184.1519909022161; Thu, 01 Mar 2018 04:57:02 -0800 (PST) Received: from localhost.localdomain (176.122.172.82.16clouds.com. [176.122.172.82]) by smtp.gmail.com with ESMTPSA id x4sm2289655pfb.46.2018.03.01.04.56.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 01 Mar 2018 04:57:01 -0800 (PST) From: Alex Shi To: Marc Zyngier , Will Deacon , Ard Biesheuvel , Catalin Marinas , stable@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 14/45] arm64: uaccess: Don't bother eliding access_ok checks in __{get, put}_user Date: Thu, 1 Mar 2018 20:53:51 +0800 Message-Id: <1519908862-11425-15-git-send-email-alex.shi@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519908862-11425-1-git-send-email-alex.shi@linaro.org> References: <1519908862-11425-1-git-send-email-alex.shi@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180301_125713_890201_473CA09B X-CRM114-Status: GOOD ( 15.20 ) 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: , Cc: Alex Shi 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 --- 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);