Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp220495lqq; Thu, 13 Jun 2024 00:34:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVjwfyzDUrtD4OdVlJJ6dNfZk7K2FyX7QTXgD1B0DvypETXOTjQgqs5uzFQogoKwZus7PlW9X7tyZkSgYwbla7XshR3Y/Kx7eyyjMTx0g== X-Google-Smtp-Source: AGHT+IGMFybRWZddbaUmJYxbprYNR23RPh2z/DX8dzmXH0ralFcMtF8X9sfokQQiodrAaf7n3RWL X-Received: by 2002:a17:906:6d14:b0:a6f:51d8:1963 with SMTP id a640c23a62f3a-a6f51d819a1mr111455666b.43.1718264058352; Thu, 13 Jun 2024 00:34:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718264058; cv=pass; d=google.com; s=arc-20160816; b=w8TqsyYGqkQ8RLEsGHhsXtySYf/qevjJ9FrRvPwQyr2ptCh/ONUjF0pSPdET0NPHfB w1yCc98R7050copdWJAQ9jjSSTNF/tf3RAgsMl21JrGQbTUfG5t3AbqcRQSYbN7vrhor zIS55Ys1yHuwwQ/MVq9SMa1yhteMmNv1DzP1gbpLxbIexIEdeulam0Fxzb/Sy/Us477C FhhxQ1pGh911gDV1GRpX6fxGQ4pkGBqtvz0muWDEY18FTV9eZ1RDnPcv6VM5PqdZK99z 0koVMANJ33t/bZHkthpDD8doc3n+3N4MZLtnxuqseK+gSsR9AYs1EPcIUZt3c+Rjaeo6 WBvQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=xpwccXm6VSga0glYezh9Lcoo9mdtNlokYTZmh5MRn+k=; fh=sk+mzqRazPg860wCmEhqd3iFFYgfgtqueZVmtjgqcqo=; b=b8z0y4QEulfnTBH7BUkz77N9WFkDS4Y1kUNHVPAu051KTx8dS+u01hNwivPMMeuZgQ 6Hb6ckGFRQ1280f40kRqNVjBsr9dM6JwE02WYMzLeEowQYFjFUU1rkWbSwzZeQ0PkmkE OotcDvz7WOG+KDkgC01GyoBc3l39gkkJGkguitU5XEvKVOb2Ev1N9QLCz1kms24DCkWv 5dyusaMlMKjfImJtzGl6o6GPQIwBMAtIJq57Ha+7IBrDTcsblbNbI8OT7FFu5peQ37iX HDpMoIx9HbtRpIOnHf+9D5/au72rPxNnPlKnvEqNkiBfy9g83PPd5o8sjnGtaxrcooaU bVZg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=FcdI8T+8; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-212714-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212714-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f56dd66aasi40539466b.306.2024.06.13.00.34.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 00:34:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212714-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=FcdI8T+8; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-212714-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212714-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DD7071F2129F for ; Thu, 13 Jun 2024 07:34:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5330613D259; Thu, 13 Jun 2024 07:33:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="FcdI8T+8" Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) (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 9DD4013D258 for ; Thu, 13 Jun 2024 07:33:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718263982; cv=none; b=Cb0abAqKzBuQLRHkh6ogvPIcOGjMi0F+l+HIIn+QC4qkqWUuf/1W7h3Nghq0lrsrCj2rJpXd7/Y6wM3bsRME/Edj8OuFJfnJQ1cTUzmstXYYlXhCet7I0jrAjDgQLoHT6RENLCymjG6lWc4JU3OxgTKo/H3OgHng/mYopB39Lvs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718263982; c=relaxed/simple; bh=tupMDt20s7S7PlhgqDhuVkvtJDEBbx3ofqpMD3PiZ2k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=D/uvXeKYOtEmhPFtUyPhXkziuZuB7L/LRs5MshCUEmyRbNOnSS5H3l3Zw4npoZd9SpxfGm/IlnPqvy+eSfXk29bMJEntuhNLnr9yr7qQeFoMMS7e1FJkKtwgwkHuYJoEz/zxPcdmpOEJMHHaEZN9a+5ZuOh5pP9bmkxHbSpnzf8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=FcdI8T+8; arc=none smtp.client-ip=209.85.167.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-oi1-f181.google.com with SMTP id 5614622812f47-3d220039bc6so340918b6e.2 for ; Thu, 13 Jun 2024 00:33:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1718263980; x=1718868780; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=xpwccXm6VSga0glYezh9Lcoo9mdtNlokYTZmh5MRn+k=; b=FcdI8T+80T80YU+ZQFMin0zapvV8rJZJ2673hnqQVSA4yfnH1wF0GtzLY8GFUjI5wn /Qop6ysDY0Zyfbm85odiJJy4DZIlxifiGdy6t8H1cXs6dcBTxvpzG7PMOtqlmyZmkxv/ FrB5urjcZktyYh4CpHURWCGNhGcAYVelDTQIPJEczdYfsHt2Kv4ReyGb9Lk6QrIhYR46 1B+1HZlKu0Iae80el5r4j50aFXDJ2zqEP9gv1PqO3T4qH/NNAEm4Ydx6A0tIMfaG881F ad3IleVEHQ8Qlqq1kTnptM1NBfnjp9PPRzGLn9ozRW7EDuIiO3yytScnlxIVIyDGXGcl V3/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718263980; x=1718868780; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xpwccXm6VSga0glYezh9Lcoo9mdtNlokYTZmh5MRn+k=; b=lQ4YCC+HrKcWFwhSFVcoB105jtCZ/upcvk8X0QWIRPbP615MGgxv2v2J/syOVux3kv 7pi/c8qaGv3IXLCYudc6ZS6a58gs9+0SGJvA9PNJYRAfYX8/HZ0rxvpwHrqiGjGm755+ zOoFPxQiG7QI208Ywfdet/IJva3RA7c1FFLn88xU70Lz2hWqvuWmzfxWX0bkdfm3p9sI V3eXVWPULfSQUeAxY9RI8FyJnKZ3n1Nva5rERF1Csj4+aCLDCHbVfHzIzydl8cFeP3ro 9Z85axXI74taK/gegTCaQIvE5R6bgghPo6Attk0KiLh0Wdr91Xi81jrkhHv2yEIm9nxK gS5A== X-Forwarded-Encrypted: i=1; AJvYcCWAfY52IIhQWW3Z27zppkwukVJ9Da8NggjnnbXuBT3IWTaiSqEgyfRxYsEnn80phIUB6Ri7LvnebgCEqUgVhkgFkR/mVaQhIE+sjU1H X-Gm-Message-State: AOJu0YySe0OOsZ+rawcycHrmB9M6f4CtaDmIVHuBGKwGX91WlEFEivYG EtRNctb/a+9g+0/5q5K4zjia16mzTMNyN6pr1H9VdgHJDaT+/+p9+/Dlxf3+p9k= X-Received: by 2002:a05:6808:1a24:b0:3d2:4a08:2c99 with SMTP id 5614622812f47-3d24a0832b0mr183168b6e.23.1718263979694; Thu, 13 Jun 2024 00:32:59 -0700 (PDT) Received: from [127.0.1.1] (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-705cc78a1a0sm730028b3a.0.2024.06.13.00.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 00:32:59 -0700 (PDT) From: Andy Chiu Date: Thu, 13 Jun 2024 15:11:10 +0800 Subject: [PATCH 5/8] riscv: ftrace: prepare ftrace for atomic code patching Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240613-dev-andyc-dyn-ftrace-v4-v1-5-1a538e12c01e@sifive.com> References: <20240613-dev-andyc-dyn-ftrace-v4-v1-0-1a538e12c01e@sifive.com> In-Reply-To: <20240613-dev-andyc-dyn-ftrace-v4-v1-0-1a538e12c01e@sifive.com> To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Zong Li , Steven Rostedt , Masami Hiramatsu , Mark Rutland , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Puranjay Mohan Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, llvm@lists.linux.dev, Andy Chiu X-Mailer: b4 0.12.4 We use an AUIPC+JALR pair to jump into a ftrace trampoline. Since instruction fetch can break down to 4 byte at a time, it is impossible to update two instructions without a race. In order to mitigate it, we initialize the patchable entry to AUIPC + NOP4. Then, the run-time code patching can change NOP4 to JALR to eable/disable ftrcae from a function. This limits the reach of each ftrace entry to +-2KB displacing from ftrace_caller. Starting from the trampoline, we add a level of indirection for it to reach ftrace caller target. Now, it loads the target address from a memory location, then perform the jump. This enable the kernel to update the target atomically. The ordering of reading/updating the targert address should be guarded by generic ftrace code, where it sends smp_rmb ipi. Signed-off-by: Andy Chiu --- arch/riscv/include/asm/ftrace.h | 4 +++ arch/riscv/kernel/ftrace.c | 80 ++++++++++++++++++++++++++--------------- arch/riscv/kernel/mcount-dyn.S | 9 +++-- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/arch/riscv/include/asm/ftrace.h b/arch/riscv/include/asm/ftrace.h index 5f81c53dbfd9..7199383f8c02 100644 --- a/arch/riscv/include/asm/ftrace.h +++ b/arch/riscv/include/asm/ftrace.h @@ -81,6 +81,7 @@ struct dyn_arch_ftrace { #define JALR_T0 (0x000282e7) #define AUIPC_T0 (0x00000297) #define NOP4 (0x00000013) +#define JALR_RANGE (JALR_SIGN_MASK - 1) #define to_jalr_t0(offset) \ (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_T0) @@ -118,6 +119,9 @@ do { \ * Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here. */ #define MCOUNT_INSN_SIZE 8 +#define MCOUNT_AUIPC_SIZE 4 +#define MCOUNT_JALR_SIZE 4 +#define MCOUNT_NOP4_SIZE 4 #ifndef __ASSEMBLY__ struct dyn_ftrace; diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index 87cbd86576b2..f3b09f2d3ecc 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -64,42 +64,64 @@ static int ftrace_check_current_call(unsigned long hook_pos, return 0; } -static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target, - bool enable, bool ra) +static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target, bool validate) { unsigned int call[2]; - unsigned int nops[2] = {NOP4, NOP4}; + unsigned int replaced[2]; + + make_call_t0(hook_pos, target, call); - if (ra) - make_call_ra(hook_pos, target, call); - else - make_call_t0(hook_pos, target, call); + if (validate) { + /* + * Read the text we want to modify; + * return must be -EFAULT on read error + */ + if (copy_from_kernel_nofault(replaced, (void *)hook_pos, + MCOUNT_INSN_SIZE)) + return -EFAULT; + + if (replaced[0] != call[0]) { + pr_err("%p: expected (%08x) but got (%08x)\n", + (void *)hook_pos, call[0], replaced[0]); + return -EINVAL; + } + } - /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */ - if (patch_insn_write((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE)) + /* Replace the jalr at once. Return -EPERM on write error. */ + if (patch_insn_write((void *)(hook_pos + MCOUNT_AUIPC_SIZE), call + 1, MCOUNT_JALR_SIZE)) return -EPERM; return 0; } -int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) +static int __ftrace_modify_call_site(ftrace_func_t *hook_pos, ftrace_func_t target, bool enable) { - unsigned int call[2]; + ftrace_func_t call = target; + ftrace_func_t nops = &ftrace_stub; - make_call_t0(rec->ip, addr, call); - - if (patch_insn_write((void *)rec->ip, call, MCOUNT_INSN_SIZE)) - return -EPERM; + WRITE_ONCE(*hook_pos, enable ? call : nops); return 0; } +int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) +{ + unsigned long distance, orig_addr; + + orig_addr = (unsigned long)&ftrace_caller; + distance = addr > orig_addr ? addr - orig_addr : orig_addr - addr; + if (distance > JALR_RANGE) + return -EINVAL; + + return __ftrace_modify_call(rec->ip, addr, false); +} + int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr) { - unsigned int nops[2] = {NOP4, NOP4}; + unsigned int nops[1] = {NOP4}; - if (patch_insn_write((void *)rec->ip, nops, MCOUNT_INSN_SIZE)) + if (patch_insn_write((void *)(rec->ip + MCOUNT_AUIPC_SIZE), nops, MCOUNT_NOP4_SIZE)) return -EPERM; return 0; @@ -114,10 +136,14 @@ int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, */ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec) { + unsigned int nops[2]; int out; + make_call_t0(rec->ip, &ftrace_caller, nops); + nops[1] = NOP4; + mutex_lock(&text_mutex); - out = ftrace_make_nop(mod, rec, MCOUNT_ADDR); + out = patch_insn_write((void *)rec->ip, nops, MCOUNT_INSN_SIZE); mutex_unlock(&text_mutex); if (!mod) @@ -126,12 +152,10 @@ int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec) return out; } +ftrace_func_t ftrace_call_dest = ftrace_stub; int ftrace_update_ftrace_func(ftrace_func_t func) { - int ret = __ftrace_modify_call((unsigned long)&ftrace_call, - (unsigned long)func, true, true); - - return ret; + return __ftrace_modify_call_site(&ftrace_call_dest, func, true); } struct ftrace_modify_param { @@ -185,7 +209,7 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, if (ret) return ret; - return __ftrace_modify_call(caller, addr, true, false); + return __ftrace_modify_call(caller, addr, true); } #endif @@ -220,17 +244,17 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, prepare_ftrace_return(&fregs->ra, ip, fregs->s0); } #else /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */ -extern void ftrace_graph_call(void); +ftrace_func_t ftrace_graph_call_dest = ftrace_stub; int ftrace_enable_ftrace_graph_caller(void) { - return __ftrace_modify_call((unsigned long)&ftrace_graph_call, - (unsigned long)&prepare_ftrace_return, true, true); + return __ftrace_modify_call_site(&ftrace_graph_call_dest, + &prepare_ftrace_return, true); } int ftrace_disable_ftrace_graph_caller(void) { - return __ftrace_modify_call((unsigned long)&ftrace_graph_call, - (unsigned long)&prepare_ftrace_return, false, true); + return __ftrace_modify_call_site(&ftrace_graph_call_dest, + &prepare_ftrace_return, false); } #endif /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */ #endif /* CONFIG_DYNAMIC_FTRACE */ diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mcount-dyn.S index e988bd26b28b..bc06e8ab81cf 100644 --- a/arch/riscv/kernel/mcount-dyn.S +++ b/arch/riscv/kernel/mcount-dyn.S @@ -162,7 +162,8 @@ SYM_FUNC_START(ftrace_caller) mv a3, sp SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL) - call ftrace_stub + REG_L ra, ftrace_call_dest + jalr 0(ra) #ifdef CONFIG_FUNCTION_GRAPH_TRACER addi a0, sp, ABI_RA @@ -172,7 +173,8 @@ SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL) mv a2, s0 #endif SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL) - call ftrace_stub + REG_L ra, ftrace_graph_call_dest + jalr 0(ra) #endif RESTORE_ABI jr t0 @@ -185,7 +187,8 @@ SYM_FUNC_START(ftrace_caller) PREPARE_ARGS SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL) - call ftrace_stub + REG_L ra, ftrace_call_dest + jalr 0(ra) RESTORE_ABI_REGS bnez t1, .Ldirect -- 2.43.0