Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp674447pxv; Wed, 14 Jul 2021 12:44:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1IIixucOvqgBEBYKeip5AzU4x9jVzmAVabgvhb7dJ63eIat+OoMKRrmOurQq+Ihj3Q7XL X-Received: by 2002:a92:3009:: with SMTP id x9mr7462551ile.49.1626291876033; Wed, 14 Jul 2021 12:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626291876; cv=none; d=google.com; s=arc-20160816; b=bOR2bvruqG8h83Tq48IswlgjD8q7FrSR1HXwtoq7foH71A9svv6GUepi15kgToZDx+ Xla67um36mnC8K8N6OZtLkS/7IELo/4hsQz4OkUY0QsZttpO0ueBDYZ4D7xZIkf0wokB NaVETM+71/yktZMWPgT7vpTMqc4c/X2FHuWumiksHQLbYllj3sSyBG+cFwc+GyGI0VnN 56SzlIQpjL2CgKNWZnf5pxnfDI4unfbiXNBm+UoKMdDzKGCAYNqaEDh0IftgpGmyYx0/ J4YoM4GO+zpMGU+Tw3NqKoElRrcoCdHMWA9rkySduN0KIY4RgK8C0wDVmJAkhqfToLwd 1FiQ== 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=JbY3B6JgEwArKLsgNvLO69uvWYXgnCfWyYHwqWO80SA=; b=LV5vvBbwYZRyghE2tUFkonD2BRsDiIXA1k9GC2nsbUigCRrPdlxmmJzO0QtmP8nAM8 iv3gx6VZ8rM2EK0GePvOnFPEKpM4A5tHBrANswqO+DSbcO2qf9rN8btlIHdvCHlh427e CFc+BRmtf9NvF006naKxo26eUBRdfs7ybKAx9Z2UgojeBY9PKU784N+aeQm9Q/B75QJO fLYr/rRbI+8Arb8XBZ15u5DWjQRD/X0Lp2+85NJzp7SE2E5abrSYmvAVplCU3a0e4/d9 nBqHo6TpjFyNPVDbuFB4ZZhkhukmOGK1g3bOGlqx7bD0mwFxxrZBpRIMe9mZhuYcTI9r mliw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TFL7AlNA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x6si3234797ilj.25.2021.07.14.12.44.23; Wed, 14 Jul 2021 12:44:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=TFL7AlNA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237577AbhGNTqJ (ORCPT + 99 others); Wed, 14 Jul 2021 15:46:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:38740 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236143AbhGNTnb (ORCPT ); Wed, 14 Jul 2021 15:43:31 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 11F9460FF2; Wed, 14 Jul 2021 19:40:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1626291632; bh=xu4a09RyxjHjcQgsyVfU+AasF6ya7IX+aHbh4Bb+IP8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TFL7AlNA9rfyxjqsRyQb1uKpzbKU4EBRcJTdTrS6OBxm3upkIObE1+38rYGuuvZMS Z/3d3/0spQlGuZCO3eCwigGVaK6TA6s9sSfHBZ3EkapPPxLAYAzw/aytqE06fcoVsT k/wjhn5yhvO/r/N/7RM38QSLWk8fBSq5+5TMacrT3b2Y8rUnzRJlw1hOjCgaU4eP5S igk1i0WQC9Skg1Of6TrbqAchMl87s/lR9AzedLH8zhi9E0pGFySeChVUX+iA+/YoWf m4KI35xnOCsb+/nc5GEcZQ7C+DFvCzsJCxYLNlDzCvaqC189GLf+5BhqEl40rEpWF6 J0G+gGbH2LEAA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Heiko Carstens , Sven Schnelle , Vasily Gorbik , Sasha Levin , linux-s390@vger.kernel.org Subject: [PATCH AUTOSEL 5.13 107/108] s390: introduce proper type handling call_on_stack() macro Date: Wed, 14 Jul 2021 15:37:59 -0400 Message-Id: <20210714193800.52097-107-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210714193800.52097-1-sashal@kernel.org> References: <20210714193800.52097-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Heiko Carstens [ Upstream commit 41d71fe59cce41237f24f3b7bdc1b414069a34ed ] The existing CALL_ON_STACK() macro allows for subtle bugs: - There is no type checking of the function that is being called. That is: missing or too many arguments do not cause any compile error or warning. The same is true if the return type of the called function changes. This can lead to quite random bugs. - Sign and zero extension of arguments is missing. Given that the s390 C ABI requires that the caller of a function performs proper sign and zero extension this can also lead to subtle bugs. - If arguments to the CALL_ON_STACK() macros contain functions calls register corruption can happen due to register asm constructs being used. Therefore introduce a new call_on_stack() macro which is supposed to fix all these problems. Reviewed-by: Sven Schnelle Signed-off-by: Heiko Carstens Signed-off-by: Vasily Gorbik Signed-off-by: Sasha Levin --- arch/s390/include/asm/stacktrace.h | 97 ++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/arch/s390/include/asm/stacktrace.h b/arch/s390/include/asm/stacktrace.h index 76c6034428be..b4d936580fbf 100644 --- a/arch/s390/include/asm/stacktrace.h +++ b/arch/s390/include/asm/stacktrace.h @@ -129,6 +129,103 @@ struct stack_frame { r2; \ }) +#define CALL_LARGS_0(...) \ + long dummy = 0 +#define CALL_LARGS_1(t1, a1) \ + long arg1 = (long)(t1)(a1) +#define CALL_LARGS_2(t1, a1, t2, a2) \ + CALL_LARGS_1(t1, a1); \ + long arg2 = (long)(t2)(a2) +#define CALL_LARGS_3(t1, a1, t2, a2, t3, a3) \ + CALL_LARGS_2(t1, a1, t2, a2); \ + long arg3 = (long)(t3)(a3) +#define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4) \ + CALL_LARGS_3(t1, a1, t2, a2, t3, a3); \ + long arg4 = (long)(t4)(a4) +#define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \ + CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4); \ + long arg5 = (long)(t5)(a5) + +#define CALL_REGS_0 \ + register long r2 asm("2") = dummy +#define CALL_REGS_1 \ + register long r2 asm("2") = arg1 +#define CALL_REGS_2 \ + CALL_REGS_1; \ + register long r3 asm("3") = arg2 +#define CALL_REGS_3 \ + CALL_REGS_2; \ + register long r4 asm("4") = arg3 +#define CALL_REGS_4 \ + CALL_REGS_3; \ + register long r5 asm("5") = arg4 +#define CALL_REGS_5 \ + CALL_REGS_4; \ + register long r6 asm("6") = arg5 + +#define CALL_TYPECHECK_0(...) +#define CALL_TYPECHECK_1(t, a, ...) \ + typecheck(t, a) +#define CALL_TYPECHECK_2(t, a, ...) \ + CALL_TYPECHECK_1(__VA_ARGS__); \ + typecheck(t, a) +#define CALL_TYPECHECK_3(t, a, ...) \ + CALL_TYPECHECK_2(__VA_ARGS__); \ + typecheck(t, a) +#define CALL_TYPECHECK_4(t, a, ...) \ + CALL_TYPECHECK_3(__VA_ARGS__); \ + typecheck(t, a) +#define CALL_TYPECHECK_5(t, a, ...) \ + CALL_TYPECHECK_4(__VA_ARGS__); \ + typecheck(t, a) + +#define CALL_PARM_0(...) void +#define CALL_PARM_1(t, a, ...) t +#define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__) +#define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__) +#define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__) +#define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__) +#define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__) + +/* + * Use call_on_stack() to call a function switching to a specified + * stack. Proper sign and zero extension of function arguments is + * done. Usage: + * + * rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...) + * + * - nr specifies the number of function arguments of fn. + * - stack specifies the stack to be used. + * - fn is the function to be called. + * - rettype is the return type of fn. + * - t1, a1, ... are pairs, where t1 must match the type of the first + * argument of fn, t2 the second, etc. a1 is the corresponding + * first function argument (not name), etc. + */ +#define call_on_stack(nr, stack, rettype, fn, ...) \ +({ \ + rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn; \ + unsigned long frame = current_frame_address(); \ + unsigned long __stack = stack; \ + unsigned long prev; \ + CALL_LARGS_##nr(__VA_ARGS__); \ + CALL_REGS_##nr; \ + \ + CALL_TYPECHECK_##nr(__VA_ARGS__); \ + asm volatile( \ + " lgr %[_prev],15\n" \ + " lg 15,%[_stack]\n" \ + " stg %[_frame],%[_bc](15)\n" \ + " brasl 14,%[_fn]\n" \ + " lgr 15,%[_prev]\n" \ + : [_prev] "=&d" (prev), CALL_FMT_##nr \ + : [_stack] "R" (__stack), \ + [_bc] "i" (offsetof(struct stack_frame, back_chain)), \ + [_frame] "d" (frame), \ + [_fn] "X" (__fn) : CALL_CLOBBER_##nr); \ + (rettype)r2; \ +}) + #define CALL_ON_STACK_NORETURN(fn, stack) \ ({ \ asm volatile( \ -- 2.30.2