From nobody Sun Nov 24 03:11:29 2024 Received: from mail-il1-f172.google.com (mail-il1-f172.google.com [209.85.166.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B2351E9065 for ; Mon, 18 Nov 2024 23:01:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970891; cv=none; b=G5/tCMKvgc+SESRTKLwZnVcQPOWM/wiyk5K/z43L0r+MfplkN0O0qzwq+x5gm8Y9mz+DIvo5RLwf9BHc8IL+yRqxPmbBNL+GRTfv9cnP0L9nwbJ7KlfBFT29m4XmZO3JGR7c99cTJW7skbzp6xF9RXA2KDN6HLGaa4sSzW7o0K8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970891; c=relaxed/simple; bh=ws7tXnWf+U6DHt9H3UXfoIBpPUQt9MU+Vr1IUARhf/s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c8U7ijEP5WEbeuPAwhOITLkmrzS9z/lKXhKlVST9e4qX7hL9JB/Y6tj4rQUJLRiyxqdsBHWxYZkTEClur3eBtyl0W52qNS9qGPqrtIAvDaP5PwWniRCnu+UMl1RtcNTZU5f74EeQ1y1qTuT03CgwkZZHmlYJG6FrfY1ue4xymnM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com; spf=pass smtp.mailfrom=tenstorrent.com; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b=IE8FrBqp; arc=none smtp.client-ip=209.85.166.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b="IE8FrBqp" Received: by mail-il1-f172.google.com with SMTP id e9e14a558f8ab-3a761e21ddeso8479995ab.3 for ; Mon, 18 Nov 2024 15:01:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tenstorrent.com; s=google; t=1731970889; x=1732575689; 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=YFqjIC8plnN7ShAhD1wilwVRTqQH/DMgtflcfXOVYkY=; b=IE8FrBqpP0gpBSaRh6vVvHW7DBQpULtsMi+mkcKcrLcNPMhlFLYDafriz8nUUNYj2c U8vdZu46HvLBux5mnAQ3XsoM8tcUP2wK+jPvW2h5DLg/e2f4sCwsWQ4UgpxRFrfvk6Td TCVuuvqkMLV7BvrttC7138HJCiBvVNJsmBG2PbtKwdqpS36Gk29XmT6njDveXd/5ty9U E2fnIeskGUBXzqATlsqL+A1Uq5nnxP1ArVvbfCWZjrnx8MogaTusszih9KDmELa7XDS1 82cYeQ6VUxyurVycbOXQPtv2lJ0vx4T6G9AHMDkEGq5by+xZ4Hg+CPXwHgsC05oFfTuz cNLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731970889; x=1732575689; 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=YFqjIC8plnN7ShAhD1wilwVRTqQH/DMgtflcfXOVYkY=; b=eLX4NEEEfWhyKarZJeQY4q+uIuzUI2MbH3W3r5lfaz2p1w/SnfbDqBtVzsfBHFxUCS g0XX+HZbpsBV8uGuTjtkQjZF3eQMPBtYMfG47OqSU5Y+g57WVnlmRNz/z5Q35W7oF8Ad Hmz5d2+iAY7dtTR0C/wwXv+puSCkp5pShd8LmhP007ISB/K5YMz5w/dmC+bgy6+ipgXu 0SSU1qdmPBeMgMPl7AR0bhTRUt7vsk/Am8GAzHHrlFq2Lcewvs+N1Qnn5hvelettm1to L7qpkEDw+oBCwvAtkh3gugYOsvQa9nXENVw5IOdHMGNKNqMHeK2gNNTWWL/XkQNROV3O //Qw== X-Forwarded-Encrypted: i=1; AJvYcCVHJJMEAvDOIZ3mfl/5Q6ONqYIzS+uFssB2MkjKjQlPKsR3jpz0myFwQpXRclEpH5XliNSDfcZwvlrPl/A=@vger.kernel.org X-Gm-Message-State: AOJu0YzOMyVa8DRymCLlbw3y/tKYOqicwRQrAatFYttFE4sZ3+U7pIeS UpzrNbjPA4YaCE+kA/waREZYG+ULGEC9AeU/f2Kk6Y6gpg0WopUbUAg9Xcse/Q== X-Google-Smtp-Source: AGHT+IFvCHXxvHQVpV+bK5VdC3xSTjSvs5zHAHvFSeEEYYdE9811W5YkCAlHdEbeZSHxbdsnrW17KA== X-Received: by 2002:a05:6e02:1949:b0:3a0:5642:c78 with SMTP id e9e14a558f8ab-3a748082c35mr142193715ab.15.1731970888729; Mon, 18 Nov 2024 15:01:28 -0800 (PST) Received: from aus-ird.local.tenstorrent.com ([38.104.49.66]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b37a897eebsm35344185a.94.2024.11.18.15.01.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 15:01:27 -0800 (PST) From: Cyril Bur To: palmer@dabbelt.com, aou@eecs.berkeley.edu, paul.walmsley@sifive.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Jisheng Zhang Subject: [PATCH v2 1/4] riscv: implement user_access_begin and families Date: Mon, 18 Nov 2024 23:01:09 +0000 Message-Id: <20241118230112.2872978-2-cyrilbur@tenstorrent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> References: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jisheng Zhang Currently, when a function like strncpy_from_user() is called, the userspace access protection is disabled and enabled for every word read. By implementing user_access_begin and families, the protection is disabled at the beginning of the copy and enabled at the end. The __inttype macro is borrowed from x86 implementation. Signed-off-by: Jisheng Zhang Signed-off-by: Cyril Bur --- arch/riscv/include/asm/uaccess.h | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uacc= ess.h index 72ec1d9bd3f3..09d4ca37522c 100644 --- a/arch/riscv/include/asm/uaccess.h +++ b/arch/riscv/include/asm/uaccess.h @@ -28,6 +28,19 @@ #define __disable_user_access() \ __asm__ __volatile__ ("csrc sstatus, %0" : : "r" (SR_SUM) : "memory") =20 +/* + * This is the smallest unsigned integer type that can fit a value + * (up to 'long long') + */ +#define __inttype(x) __typeof__( \ + __typefits(x,char, \ + __typefits(x,short, \ + __typefits(x,int, \ + __typefits(x,long,0ULL))))) + +#define __typefits(x,type,not) \ + __builtin_choose_expr(sizeof(x)<=3Dsizeof(type),(unsigned type)0,not) + /* * The exception table consists of pairs of addresses: the first is the * address of an instruction that is allowed to fault, and the second is @@ -335,6 +348,56 @@ do { \ goto err_label; \ } while (0) =20 +static __must_check __always_inline bool user_access_begin(const void __us= er *ptr, size_t len) +{ + if (unlikely(!access_ok(ptr,len))) + return 0; + __enable_user_access(); + return 1; +} +#define user_access_begin(a,b) user_access_begin(a,b) +#define user_access_end() __disable_user_access(); + +static inline unsigned long user_access_save(void) { return 0UL; } +static inline void user_access_restore(unsigned long enabled) { } + +#define unsafe_put_user(x, ptr, label) do { \ + long __kr_err =3D 0; \ + __put_user_nocheck(x, (ptr), __kr_err); \ + if (__kr_err) goto label; \ +} while (0) + +#define unsafe_get_user(x, ptr, label) do { \ + long __kr_err =3D 0; \ + __inttype(*(ptr)) __gu_val; \ + __get_user_nocheck(__gu_val, (ptr), __kr_err); \ + (x) =3D (__force __typeof__(*(ptr)))__gu_val; \ + if (__kr_err) goto label; \ +} while (0) + +/* + * We want the unsafe accessors to always be inlined and use + * the error labels - thus the macro games. + */ +#define unsafe_copy_loop(dst, src, len, type, label) \ + while (len >=3D sizeof(type)) { \ + unsafe_put_user(*(type *)(src),(type __user *)(dst),label); \ + dst +=3D sizeof(type); \ + src +=3D sizeof(type); \ + len -=3D sizeof(type); \ + } + +#define unsafe_copy_to_user(_dst,_src,_len,label) \ +do { \ + char __user *__ucu_dst =3D (_dst); \ + const char *__ucu_src =3D (_src); \ + size_t __ucu_len =3D (_len); \ + unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, label); \ + unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, label); \ + unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, label); \ + unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label); \ +} while (0) + #else /* CONFIG_MMU */ #include #endif /* CONFIG_MMU */ --=20 2.34.1 From nobody Sun Nov 24 03:11:29 2024 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5CE601E909E for ; Mon, 18 Nov 2024 23:01:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970893; cv=none; b=Mc6Iw6tFx3Pk1NZUiCZVU4rU0eByYiezzSfg0CZnLgV6dEFxla5NQZOcchh15GfZ83CPMYkJpdENWs9gGzc0YHqpIx5jEROdL2GPKoFqMiIvUx1RXupoTKbnxsztdV+KvmrnFahDZgBvGLkcda8X16Q7IF5Hg8aF2jEf5ksn8Bk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970893; c=relaxed/simple; bh=5sq2RndBvjwbPDbRstQdV6Pr5uT7zETeUtlaWtTno4I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=F0pfoOwF4hSceZQP/7wygUXqoD6LKZpfIfRjFQZcLOspBTJ7ulApEdujL6tJllHSH7yNfUIvWrptMIvxQtHj2zOBTZ4lpIr1vah0w1CMLBhpH0AsY4DojWV4Isp1AAV3e81ummSWLXvvRWdfttvc9AMrzeoB+7RrjVWbwkCNUI4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com; spf=pass smtp.mailfrom=tenstorrent.com; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b=Ob8+ePIX; arc=none smtp.client-ip=209.85.222.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b="Ob8+ePIX" Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-7b1457ba751so13899285a.3 for ; Mon, 18 Nov 2024 15:01:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tenstorrent.com; s=google; t=1731970890; x=1732575690; 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=PToKMxR8SXs7rV9viWVleXFNB1vBa7oV5mNalHrcMMc=; b=Ob8+ePIXEkT+R8HcZg6cZbb1xYZpXSP6JJLiZ1RmJGqRV3voR8l2jbM6gW608/BfAY Wfz/P1mKkTY1Y/NyNKz06pGVy2/oV3kKjCmjNISyzDOf9zPAykl3rGcmYbGrBhuniD/+ Vnnvb9LLculrhXLAEmFSLW90Ia0TT/+k081KTa22Vb7wJK6muNXMPfkZ17sKPhV4FXkc eEvoWhLbI1HJ1H9DlMLv6Z7akDA1SX0c/k4+SNRPlRjaov1dw2+tmugBFJxw1n7qnosg zeNpHL1tv9liA8DUzUh7UH+dhHDraXPvHsvQUHQKFX3GG4AUVdUh3CLwOc23XoaSurP5 wjRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731970890; x=1732575690; 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=PToKMxR8SXs7rV9viWVleXFNB1vBa7oV5mNalHrcMMc=; b=pGtzgHckUfgWEMHz0T/ipJ4ZWrpBHcnX4eY2KwhmQnwYZwYWA1WRpuwGQmYXZGyBTD NyboP3COksmAKNr95DR+ytE+k7xSXNEMUJL+USriPx0sAdRv7jliVjJT3onP9dTYRjjd 1XaRLbpHJfDveAx/y+vVSXtwDVIRGVl5sTUBDJAYEyLfQQfTR0sS5VqoyhgcLRqTI9kl XoQy5Bud5pWkMZwWAFF6zo8TtCafRa7I3bmyiepySsOtGr3mT6BO8LWdt4mDiu6Ra74B e0Mt/WnW1nDTSImgnbT39XXw/a0Z/RD6d3xcHmhq+dCg7+HMejDcBapxiYEtLAXIKEBe qI3Q== X-Forwarded-Encrypted: i=1; AJvYcCX3x6MlN4zo3W1xRvFdHNb4YH0u3CgeKDaQtd0ok5BIqc5bUE0MeO18uutoGjEw1DkIaWSC01xb2gzBMZY=@vger.kernel.org X-Gm-Message-State: AOJu0YyS7nQwwGmx84S0uc/yd241sSSvyPkOg32dgS0cuC7REJ0eKJB/ K67uuoqFsHvdmaXLfKlKsEMHIEfkP8aPsUzvvhSK+izOnrt2xPW43PPbrJ3lIQ== X-Google-Smtp-Source: AGHT+IEdKYZYKIfiFMuOvuSqJaFWovb+3yVp8wkhP6JmHG9WTJacACUFPkjQt/TtG4UdQbsLVQzgYQ== X-Received: by 2002:a05:620a:45a7:b0:7ad:832a:4063 with SMTP id af79cd13be357-7b36229a32fmr1994256685a.7.1731970890344; Mon, 18 Nov 2024 15:01:30 -0800 (PST) Received: from aus-ird.local.tenstorrent.com ([38.104.49.66]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b37a897eebsm35344185a.94.2024.11.18.15.01.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 15:01:29 -0800 (PST) From: Cyril Bur To: palmer@dabbelt.com, aou@eecs.berkeley.edu, paul.walmsley@sifive.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Jisheng Zhang Subject: [PATCH v2 2/4] riscv: uaccess: use input constraints for ptr of __put_user Date: Mon, 18 Nov 2024 23:01:10 +0000 Message-Id: <20241118230112.2872978-3-cyrilbur@tenstorrent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> References: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jisheng Zhang I believe the output constraints "=3Dm" is not necessary, because the instruction itself is "write", we don't need the compiler to "write" for us. So tell compiler we read from memory instead of writing. Signed-off-by: Jisheng Zhang Signed-off-by: Cyril Bur --- arch/riscv/include/asm/uaccess.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uacc= ess.h index 09d4ca37522c..84b084e388a7 100644 --- a/arch/riscv/include/asm/uaccess.h +++ b/arch/riscv/include/asm/uaccess.h @@ -186,11 +186,11 @@ do { \ __typeof__(*(ptr)) __x =3D x; \ __asm__ __volatile__ ( \ "1:\n" \ - " " insn " %z2, %1\n" \ + " " insn " %z1, %2\n" \ "2:\n" \ _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %0) \ - : "+r" (err), "=3Dm" (*(ptr)) \ - : "rJ" (__x)); \ + : "+r" (err) \ + : "rJ" (__x), "m"(*(ptr))); \ } while (0) =20 #ifdef CONFIG_64BIT @@ -203,16 +203,16 @@ do { \ u64 __x =3D (__typeof__((x)-(x)))(x); \ __asm__ __volatile__ ( \ "1:\n" \ - " sw %z3, %1\n" \ + " sw %z1, %3\n" \ "2:\n" \ - " sw %z4, %2\n" \ + " sw %z2, %4\n" \ "3:\n" \ _ASM_EXTABLE_UACCESS_ERR(1b, 3b, %0) \ _ASM_EXTABLE_UACCESS_ERR(2b, 3b, %0) \ - : "+r" (err), \ - "=3Dm" (__ptr[__LSW]), \ - "=3Dm" (__ptr[__MSW]) \ - : "rJ" (__x), "rJ" (__x >> 32)); \ + : "+r" (err) \ + : "rJ" (__x), "rJ" (__x >> 32), \ + "m" (__ptr[__LSW]), \ + "m" (__ptr[__MSW])); \ } while (0) #endif /* CONFIG_64BIT */ =20 --=20 2.34.1 From nobody Sun Nov 24 03:11:29 2024 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC4E61EABC0 for ; Mon, 18 Nov 2024 23:01:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970894; cv=none; b=q5y0xU7HUC4vYhsJXP1lnsY9fMVmpEngxKPxRM8QYe27BNOWJfAblLyriopc7O0Gylz+jaT3m/GyT9mBRrBGs1aYBvvcpI/6xCVcCNW7SKub4ek0d2l3dfBsTAqGZtxFJrWGi/uu3ugH/sbU+V47iKAl2HOgNxdcQTWzd7F/9bE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970894; c=relaxed/simple; bh=vz/CsLoB3Axx776+m0RBpN204Gas1MIZUEQJapwqFKo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k+HJ4QJ23J9RxKHACiVDvQgIx2FXPooq6qIo2hR5pGou0mIfuT2Q+5oaD+JqbzucvNsfGoAC4ys9fiMtBL7yK2wFM0w1IZB7VKrUdWGViPMjlkS3/B+nDa2APu69YAsgCaBZ4dfdTj31YIxFPj5mOh0lbwpOtmT7d9gzlgU/SIQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com; spf=pass smtp.mailfrom=tenstorrent.com; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b=VnK2OiCA; arc=none smtp.client-ip=209.85.222.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b="VnK2OiCA" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-7b152a923a3so252608885a.3 for ; Mon, 18 Nov 2024 15:01:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tenstorrent.com; s=google; t=1731970892; x=1732575692; 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=fwX5scsTVfAnPzy5utpE8/PcuRTXCsQPKfCiUnW9jEk=; b=VnK2OiCAqFu41HyLpthT+P55AV1O8pCNfvWXgGD42X7tHdTcNdG0avnJUWRwfH35kA pPALPLpTvGrslUi29nuOB45OgOaIQeNh1YpuvQrFRKInPjXyUf5RYQpGRLRbu5/S/6WM e3y7cHSn3i0OGa3vOdfHp4VJ9M/p0JBls8UucvJCSSXdBKZsbiFGyY4n1nLg+eqow0Nd /09uiRn96GMZRnkQZdpQAw+r9krCtukOYSVqXdTJmww8i4kTk4gUIDCgBAxXQT7dUWzC OynjMg/wTkMsDr4ippZDN50C7swNL20XL9jVoNOhF0fQbwFXHeRiqzoBLtjp+RkM0ERt 0XTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731970892; x=1732575692; 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=fwX5scsTVfAnPzy5utpE8/PcuRTXCsQPKfCiUnW9jEk=; b=H219V5tq5YFXjLOajYvfBEtYWHg7w3l/uff9Cl3eFyK1TOS/AunzTJgPElaUXrCnHt zrq412+pf9UeZXei5JhW4ydoYcU10WfyKFhui6AygYS2uAXeMaj1UYRvUBu4f/y1rJKW 4heL+m2XzcC4ua290Noob0xcVouE4FqHYGy5daBQ0UqopLI+tu/jmiKdIMrdpktxMnBb NE99rdf5PtMt7XDJkz2xHiWRbZ/ndYnIdHS7JH6XIuVNxtql1+zIwxzYDPaIu2lqtDNp dPk1NVIUQudFGbtflgKxMr9YicDPiC+0Sj5ED9ybNQPHn95B8zM9GWJgrObFBvth+S5g z2hw== X-Forwarded-Encrypted: i=1; AJvYcCUjnqNcpNO63g21Sjp4HNARUHHPlZiQc8zlK9yjBwSGmq/vU3obgdbxaW0ZvpLq+46np96GrDpjeoK5ON0=@vger.kernel.org X-Gm-Message-State: AOJu0YyL9ZoM8SJ99Jn1F61Ytk4nM5qm7sJCgmzUev4apHmTS9AKz/tU uhrxPXGGX4e1TIRMJnW8QAhxKEuwMF84L/PNbdIKfmgcPNZf+hNZ5GD0kjbZCg== X-Google-Smtp-Source: AGHT+IFmUQ6Alw6TgMZvuOtrCvlZCP/drMIJBLXEykYvSL6Eocpr3m7d3KptR1usfQOxg1R4KAxCyg== X-Received: by 2002:a05:620a:4714:b0:7b1:8e09:5119 with SMTP id af79cd13be357-7b3622a57e4mr1938760285a.2.1731970891735; Mon, 18 Nov 2024 15:01:31 -0800 (PST) Received: from aus-ird.local.tenstorrent.com ([38.104.49.66]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b37a897eebsm35344185a.94.2024.11.18.15.01.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 15:01:31 -0800 (PST) From: Cyril Bur To: palmer@dabbelt.com, aou@eecs.berkeley.edu, paul.walmsley@sifive.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Jisheng Zhang Subject: [PATCH v2 3/4] riscv: uaccess: use 'asm goto' for put_user() Date: Mon, 18 Nov 2024 23:01:11 +0000 Message-Id: <20241118230112.2872978-4-cyrilbur@tenstorrent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> References: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jisheng Zhang 'asm goto' generates noticeably better code since we don't need to test the error etc, the exception just jumps to the error handling directly. Signed-off-by: Jisheng Zhang Signed-off-by: Cyril Bur --- arch/riscv/include/asm/uaccess.h | 70 +++++++++++++++----------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uacc= ess.h index 84b084e388a7..916df594ef3c 100644 --- a/arch/riscv/include/asm/uaccess.h +++ b/arch/riscv/include/asm/uaccess.h @@ -181,61 +181,66 @@ do { \ ((x) =3D (__force __typeof__(x))0, -EFAULT); \ }) =20 -#define __put_user_asm(insn, x, ptr, err) \ +#define __put_user_asm(insn, x, ptr, label) \ do { \ __typeof__(*(ptr)) __x =3D x; \ - __asm__ __volatile__ ( \ + asm goto( \ "1:\n" \ - " " insn " %z1, %2\n" \ - "2:\n" \ - _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %0) \ - : "+r" (err) \ - : "rJ" (__x), "m"(*(ptr))); \ + " " insn " %z0, %1\n" \ + _ASM_EXTABLE(1b, %l2) \ + : : "rJ" (__x), "m"(*(ptr)) : : label); \ } while (0) =20 #ifdef CONFIG_64BIT -#define __put_user_8(x, ptr, err) \ - __put_user_asm("sd", x, ptr, err) +#define __put_user_8(x, ptr, label) \ + __put_user_asm("sd", x, ptr, label) #else /* !CONFIG_64BIT */ -#define __put_user_8(x, ptr, err) \ +#define __put_user_8(x, ptr, label) \ do { \ u32 __user *__ptr =3D (u32 __user *)(ptr); \ u64 __x =3D (__typeof__((x)-(x)))(x); \ - __asm__ __volatile__ ( \ + asm goto( \ "1:\n" \ - " sw %z1, %3\n" \ + " sw %z0, %2\n" \ "2:\n" \ - " sw %z2, %4\n" \ - "3:\n" \ - _ASM_EXTABLE_UACCESS_ERR(1b, 3b, %0) \ - _ASM_EXTABLE_UACCESS_ERR(2b, 3b, %0) \ - : "+r" (err) \ - : "rJ" (__x), "rJ" (__x >> 32), \ + " sw %z1, %3\n" \ + _ASM_EXTABLE(1b, %l4) \ + _ASM_EXTABLE(2b, %l4) \ + : : "rJ" (__x), "rJ" (__x >> 32), \ "m" (__ptr[__LSW]), \ - "m" (__ptr[__MSW])); \ + "m" (__ptr[__MSW]) : : label); \ } while (0) #endif /* CONFIG_64BIT */ =20 -#define __put_user_nocheck(x, __gu_ptr, __pu_err) \ +#define __put_user_nocheck(x, __gu_ptr, label) \ do { \ switch (sizeof(*__gu_ptr)) { \ case 1: \ - __put_user_asm("sb", (x), __gu_ptr, __pu_err); \ + __put_user_asm("sb", (x), __gu_ptr, label); \ break; \ case 2: \ - __put_user_asm("sh", (x), __gu_ptr, __pu_err); \ + __put_user_asm("sh", (x), __gu_ptr, label); \ break; \ case 4: \ - __put_user_asm("sw", (x), __gu_ptr, __pu_err); \ + __put_user_asm("sw", (x), __gu_ptr, label); \ break; \ case 8: \ - __put_user_8((x), __gu_ptr, __pu_err); \ + __put_user_8((x), __gu_ptr, label); \ break; \ default: \ BUILD_BUG(); \ } \ } while (0) =20 +#define __put_user_error(x, ptr, err) \ +do { \ + __label__ err_label; \ + __put_user_nocheck(x, ptr, err_label); \ + break; \ +err_label: \ + (err) =3D -EFAULT; \ +} while (0) + /** * __put_user: - Write a simple value into user space, with less checking. * @x: Value to copy to user space. @@ -266,7 +271,7 @@ do { \ __chk_user_ptr(__gu_ptr); \ \ __enable_user_access(); \ - __put_user_nocheck(__val, __gu_ptr, __pu_err); \ + __put_user_error(__val, __gu_ptr, __pu_err); \ __disable_user_access(); \ \ __pu_err; \ @@ -340,13 +345,7 @@ do { \ } while (0) =20 #define __put_kernel_nofault(dst, src, type, err_label) \ -do { \ - long __kr_err =3D 0; \ - \ - __put_user_nocheck(*((type *)(src)), (type *)(dst), __kr_err); \ - if (unlikely(__kr_err)) \ - goto err_label; \ -} while (0) + __put_user_nocheck(*((type *)(src)), (type *)(dst), err_label); =20 static __must_check __always_inline bool user_access_begin(const void __us= er *ptr, size_t len) { @@ -361,11 +360,8 @@ static __must_check __always_inline bool user_access_b= egin(const void __user *pt static inline unsigned long user_access_save(void) { return 0UL; } static inline void user_access_restore(unsigned long enabled) { } =20 -#define unsafe_put_user(x, ptr, label) do { \ - long __kr_err =3D 0; \ - __put_user_nocheck(x, (ptr), __kr_err); \ - if (__kr_err) goto label; \ -} while (0) +#define unsafe_put_user(x, ptr, label) \ + __put_user_nocheck(x, (ptr), label); =20 #define unsafe_get_user(x, ptr, label) do { \ long __kr_err =3D 0; \ --=20 2.34.1 From nobody Sun Nov 24 03:11:29 2024 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B04DE1EABDD for ; Mon, 18 Nov 2024 23:01:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970895; cv=none; b=PO/BaHgJETvSu2GjonOtZIO56Dn5XZ/i4/s+KA39+OlSi4EUg96KxYFO2WDOtfN7nxTqn1knBXCSRDSPosYXqDOqvvP7ycgYGVmFpzGi1R2ueP16dA6Ox0M0QXHXF0bncKLFoT6qTIKdGXnikDH24gEIgEy/uASl0kXAyoc+uMQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731970895; c=relaxed/simple; bh=3S7sE4A0QXcirClOQ2vojdJdjKT+8NfmgYnWPtv+jH4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LA8tIicbLojFOCGSZq5Ytod2vTaDEw2ArgeECysI6Y3SoWO7PNmiKdOZPi8XsypDfxJY3ZjBqyweFHug+N/Z5zrIbrkMniAYQDLiExEdTUwqCxsj8MRenS3KDJL+Ieeezarr0ui7Q7A+pWZh8+PM+Vv1m932MQXk9VRg1uuSBOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com; spf=pass smtp.mailfrom=tenstorrent.com; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b=ZIrQpp1f; arc=none smtp.client-ip=209.85.222.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b="ZIrQpp1f" Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-7b155cca097so377817185a.1 for ; Mon, 18 Nov 2024 15:01:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tenstorrent.com; s=google; t=1731970893; x=1732575693; 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=xRVaI3mI4q3ET8HD94GPC1Sw1Gna7OqKIYTjZxZiUMo=; b=ZIrQpp1f/onlCGnZgN1041Vpie4+1LPfHpJhmt/43O4Hdu9I5sNlrHxyB23WiI/aaF DU2K0SAeUEUpUm3Uef+Bv7scS0+gFmvIzkvwcC/jW5qEY3MApGtpTqlW69MNPea3/TRA XuQERDZmwUpAW987c0bMH/Ssf+hWurhJRFbCfCHKJyZQ0GRxO/N3fVCZSfVY/FxnoKu0 CipSshe8EbfTBU2DLsLS7VMbUB5BJO5DIquRYunS4zazRrcr9CRyd4FvN2CBEL414Nye RYdnQEF04kqG7lv1H6RJtAXv+Z9ST8xL8uM+VlnGt27wLBv+AEmRHAUUM9UXhs/qNcVE sxBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731970893; x=1732575693; 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=xRVaI3mI4q3ET8HD94GPC1Sw1Gna7OqKIYTjZxZiUMo=; b=VSfRhhu2sYjTCDkzuyl9B+ZxHeOFmdHZxzprR34aaJFdujJMZ4kGOo7taaRUCaOxbQ fEWlYXqZ6YbVAbBmoIlsUAxXJo2iEj3znQX2wshgpE6SkLwJYa7kF0t1wRZ7oiJgVB/v gdIwNNMOZkTQqWKUd7EiKRqTQqq9LPCIxGHFaSkR6zXAz+LE2xF5u/3mfAnZHy3xDC8l 0ch5sGfiGZfx1evcn4jVFkJ829P8b8LRh7F2wkJ/CiYf3J8SBjXI6Lx8dJGcDjiTIufC A3sTXyOB4sQRWEUFbQ379dN+oqm+/lYxelX6rO4GIe8DZ3/gIjKnIiEDoa9mEOf07pMn ai2Q== X-Forwarded-Encrypted: i=1; AJvYcCWQBy7K657Wj2x+ci3PWrXMk+DpmE6ZVJ40jbeQuA8nlUiQi6D+A75sbes97BjRrWMsOzUhbh+Dcsc5sN4=@vger.kernel.org X-Gm-Message-State: AOJu0Yz1K03z2ZvqU4aPdIt+4Pe8bxZUNQxRYf7M8Y9XUeUAjAUcwNmh 8ZAOXEV+PJnNKklWLsO6i2ygTwIMY00/QvROw7+TRn2N+CG74jJzbhPTU56mqQ== X-Google-Smtp-Source: AGHT+IFgOZgADD9g2Yv8LNg2iDmTl9fW41NRsj+KmA2TQmL8rOOG21P3+sAEG4+q3wXTJbLeh7WvWQ== X-Received: by 2002:a05:620a:8006:b0:7b2:fa17:f7d7 with SMTP id af79cd13be357-7b37aa0dc7amr185728085a.6.1731970892669; Mon, 18 Nov 2024 15:01:32 -0800 (PST) Received: from aus-ird.local.tenstorrent.com ([38.104.49.66]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7b37a897eebsm35344185a.94.2024.11.18.15.01.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 15:01:32 -0800 (PST) From: Cyril Bur To: palmer@dabbelt.com, aou@eecs.berkeley.edu, paul.walmsley@sifive.com Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Jisheng Zhang Subject: [PATCH v2 4/4] riscv: uaccess: use 'asm goto output' for get_user Date: Mon, 18 Nov 2024 23:01:12 +0000 Message-Id: <20241118230112.2872978-5-cyrilbur@tenstorrent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> References: <20241118230112.2872978-1-cyrilbur@tenstorrent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jisheng Zhang 'asm goto output' generates noticeably better code since we don't need to test the error etc, the exception just jumps to the error handling directly. Signed-off-by: Jisheng Zhang Signed-off-by: Cyril Bur --- arch/riscv/include/asm/uaccess.h | 90 +++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/arch/riscv/include/asm/uaccess.h b/arch/riscv/include/asm/uacc= ess.h index 916df594ef3c..62a2ddf8c542 100644 --- a/arch/riscv/include/asm/uaccess.h +++ b/arch/riscv/include/asm/uaccess.h @@ -63,27 +63,56 @@ * call. */ =20 -#define __get_user_asm(insn, x, ptr, err) \ +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +#define __get_user_asm(insn, x, ptr, label) \ + asm_goto_output( \ + "1:\n" \ + " " insn " %0, %1\n" \ + _ASM_EXTABLE_UACCESS_ERR(1b, %l2, %0) \ + : "=3D&r" (x) \ + : "m" (*(ptr)) : : label) +#else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */ +#define __get_user_asm(insn, x, ptr, label) \ do { \ - __typeof__(x) __x; \ + long __gua_err =3D 0; \ __asm__ __volatile__ ( \ "1:\n" \ " " insn " %1, %2\n" \ "2:\n" \ _ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 2b, %0, %1) \ - : "+r" (err), "=3D&r" (__x) \ + : "+r" (__gua_err), "=3D&r" (x) \ : "m" (*(ptr))); \ - (x) =3D __x; \ + if (__gua_err) \ + goto label; \ } while (0) +#endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */ =20 #ifdef CONFIG_64BIT -#define __get_user_8(x, ptr, err) \ - __get_user_asm("ld", x, ptr, err) +#define __get_user_8(x, ptr, label) \ + __get_user_asm("ld", x, ptr, label) #else /* !CONFIG_64BIT */ -#define __get_user_8(x, ptr, err) \ + +#ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT +#define __get_user_8(x, ptr, label) \ + u32 __user *__ptr =3D (u32 __user *)(ptr); \ + u32 __lo, __hi; \ + asm_goto_output( \ + "1:\n" \ + " lw %0, %2\n" \ + "2:\n" \ + " lw %1, %3\n" \ + _ASM_EXTABLE_UACCESS_ERR(1b, %l4, %0) \ + _ASM_EXTABLE_UACCESS_ERR(2b, %l4, %0) \ + : "=3D&r" (__lo), "=3Dr" (__hi) \ + : "m" (__ptr[__LSW]), "m" (__ptr[__MSW]) \ + : : label) + +#else /* !CONFIG_CC_HAS_ASM_GOTO_OUTPUT */ +#define __get_user_8(x, ptr, label) \ do { \ u32 __user *__ptr =3D (u32 __user *)(ptr); \ u32 __lo, __hi; \ + long __gu8_err =3D 0; \ __asm__ __volatile__ ( \ "1:\n" \ " lw %1, %3\n" \ @@ -92,35 +121,51 @@ do { \ "3:\n" \ _ASM_EXTABLE_UACCESS_ERR_ZERO(1b, 3b, %0, %1) \ _ASM_EXTABLE_UACCESS_ERR_ZERO(2b, 3b, %0, %1) \ - : "+r" (err), "=3D&r" (__lo), "=3Dr" (__hi) \ + : "+r" (__gu8_err), "=3D&r" (__lo), "=3Dr" (__hi) \ : "m" (__ptr[__LSW]), "m" (__ptr[__MSW])); \ - if (err) \ + if (__gu8_err) { \ __hi =3D 0; \ + goto label; \ + } \ (x) =3D (__typeof__(x))((__typeof__((x)-(x)))( \ (((u64)__hi << 32) | __lo))); \ } while (0) +#endif /* CONFIG_CC_HAS_ASM_GOTO_OUTPUT */ + #endif /* CONFIG_64BIT */ =20 -#define __get_user_nocheck(x, __gu_ptr, __gu_err) \ +#define __get_user_nocheck(x, __gu_ptr, label) \ do { \ switch (sizeof(*__gu_ptr)) { \ case 1: \ - __get_user_asm("lb", (x), __gu_ptr, __gu_err); \ + __get_user_asm("lb", (x), __gu_ptr, label); \ break; \ case 2: \ - __get_user_asm("lh", (x), __gu_ptr, __gu_err); \ + __get_user_asm("lh", (x), __gu_ptr, label); \ break; \ case 4: \ - __get_user_asm("lw", (x), __gu_ptr, __gu_err); \ + __get_user_asm("lw", (x), __gu_ptr, label); \ break; \ case 8: \ - __get_user_8((x), __gu_ptr, __gu_err); \ + __get_user_8((x), __gu_ptr, label); \ break; \ default: \ BUILD_BUG(); \ } \ } while (0) =20 +#define __get_user_error(x, ptr, err) \ +do { \ + __label__ __gu_failed; \ + \ + __get_user_nocheck(x, ptr, __gu_failed); \ + err =3D 0; \ + break; \ +__gu_failed: \ + x =3D 0; \ + err =3D -EFAULT; \ +} while (0) + /** * __get_user: - Get a simple variable from user space, with less checking. * @x: Variable to store result. @@ -145,13 +190,16 @@ do { \ ({ \ const __typeof__(*(ptr)) __user *__gu_ptr =3D (ptr); \ long __gu_err =3D 0; \ + __typeof__(x) __gu_val; \ \ __chk_user_ptr(__gu_ptr); \ \ __enable_user_access(); \ - __get_user_nocheck(x, __gu_ptr, __gu_err); \ + __get_user_error(__gu_val, __gu_ptr, __gu_err); \ __disable_user_access(); \ \ + (x) =3D __gu_val; \ + \ __gu_err; \ }) =20 @@ -336,13 +384,7 @@ unsigned long __must_check clear_user(void __user *to,= unsigned long n) } =20 #define __get_kernel_nofault(dst, src, type, err_label) \ -do { \ - long __kr_err =3D 0; \ - \ - __get_user_nocheck(*((type *)(dst)), (type *)(src), __kr_err); \ - if (unlikely(__kr_err)) \ - goto err_label; \ -} while (0) + __get_user_nocheck(*((type *)(dst)), (type *)(src), err_label); =20 #define __put_kernel_nofault(dst, src, type, err_label) \ __put_user_nocheck(*((type *)(src)), (type *)(dst), err_label); @@ -364,11 +406,9 @@ static inline void user_access_restore(unsigned long e= nabled) { } __put_user_nocheck(x, (ptr), label); =20 #define unsafe_get_user(x, ptr, label) do { \ - long __kr_err =3D 0; \ __inttype(*(ptr)) __gu_val; \ - __get_user_nocheck(__gu_val, (ptr), __kr_err); \ + __get_user_nocheck(__gu_val, (ptr), label); \ (x) =3D (__force __typeof__(*(ptr)))__gu_val; \ - if (__kr_err) goto label; \ } while (0) =20 /* --=20 2.34.1