Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4453504ybb; Tue, 7 Apr 2020 07:48:51 -0700 (PDT) X-Google-Smtp-Source: APiQypJX33U8Okpb5hp0LX5tmVkrawLyk8oWyP88JOUa8BUTcfc0lbWvtZPU+u6NJvq+gRhfWrZ+ X-Received: by 2002:aca:5d87:: with SMTP id r129mr1872075oib.144.1586270931239; Tue, 07 Apr 2020 07:48:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586270931; cv=none; d=google.com; s=arc-20160816; b=bwpSOjlFs+6MQUm1f1aFUuxyKmv5PEcyf71Y5L+aS8ZCoS30hKhWgfYHptWd3Si4b3 ZcAee2jT5+jEdUbKIq6YfsgXXpgT6Ot7MQ1DhcjEyxahOoJjMx7P1SczKczgJR1K37GX 51Dv7hBnrs9xLlqdJvi/R7sJ6V5/sQ7SoEdgAnMj/+gg3eB7Yg0m3el24IpBGvlzdzEh UVfheuANiFpBrXT4DEzS6XwypqUrUv4ASeEDKMWkqmiy7KHZpxQVNYemyfqQMTfmYxMF x3ePbI+o1oCN6+6pgdCRUThDniPlUrCvIyV95jTbsJ0+i1gXS3YBm3UE2Pt1MpPttf4w 0fkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kYuVFCggRo9I8GvVGPv9n9TMuc3y+S8enCWZ0r9b2Rg=; b=csrYulyLY5BDACLjQW/0rj0fi1oCZCO8Vn0uhKkhXR3z77P2xQ40IwdiUdK5R0eshc WNLt1YS10iHufOQgsDJc0drTzjg1sllCSH7Wizo+OU5y6vXz9gAXa9/bwrVOFxsipcOZ tbkfbXqC7zsPwYKP3yFDzcxqeIBqw2koeg6a2toYiPLtdPcslTtSRo2MleoG16e6O+oV eWh5h6lMs2JxBVQjbxlA6p+fy4YEJ0znGcdHJAi3WqNDAg5Ea6YuPGmgSBakCOY2mpQS 3EugP45MAGDmlWq+RSqmcwhcRlXzeyA3ZVD9cMSfh2nb/pjc9RRBCavveQutfZt56YAt rYtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=Dp2cyuiV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d62si747492oib.137.2020.04.07.07.48.39; Tue, 07 Apr 2020 07:48:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=Dp2cyuiV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729290AbgDGOrK (ORCPT + 99 others); Tue, 7 Apr 2020 10:47:10 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:42902 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728855AbgDGOrF (ORCPT ); Tue, 7 Apr 2020 10:47:05 -0400 Received: by mail-pl1-f193.google.com with SMTP id e1so1321329plt.9 for ; Tue, 07 Apr 2020 07:47:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kYuVFCggRo9I8GvVGPv9n9TMuc3y+S8enCWZ0r9b2Rg=; b=Dp2cyuiV3Zq2urQAMh7yiuZ3tl4kdNTj0CnDofGx5CYcevhuKy6HPSG4E7W9b0Q7Y0 2uRjZtKohnI7E8ZgkiEw8px7x+qMhXPYizZnM37q5PEOBdTSXVfkFm1rceB9zklVQOkX 1LPAhRh1htLQX11lcxKXodjYAR/tQZSpZ31ln21dus97h07gwSbhgcUgQebQncnDJVxG FTmWLZgq3KfB2/OjR+yXpBM8YXWRHKJp+WLQ2haAVfKXzKbFkVmcTQIrch4ZHhaQgeM+ pnouesMXOGAoFz4QI/DgUrFHdTMGktQ4jJzfrqzYGwFkYxCBwnkXraurAUu5y22zedFW bdyQ== 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:mime-version:content-transfer-encoding; bh=kYuVFCggRo9I8GvVGPv9n9TMuc3y+S8enCWZ0r9b2Rg=; b=jig31crtYLQl3RvA3ZMaxaTlZ2VbTSwgPaT0qjtkZ8py3ZoJ+S/ACRn8ij0/pN0ibP 1xt/2Gz5FTYs+ew8vu899oKFJRqbow+L/WiRGFMP7XGgiGZVVpnuCP2CzAlFN5galCzJ CTag9FETt/0kJhzrzOcF+ovo7DJ+Ztrm3O1pwtZl7DbYiCqvcuBpRZ6yya1gMOLRV3gk znvkuen4Pi8cpDJo1Ka2MEsJUJvVFEpQtgHik3pIuuXHScM/YDj1NSImzWuaxmVJ+pEF 5Hz04oZF96oihe+CMSObujCMlbTejUzKZQ1kWInyB/RybHt3p+dQGROJZdLeYo5q7cTm w2Cw== X-Gm-Message-State: AGi0PuYkTzAerevF4xrFRDziVbJlMIOuXJIlJOGLEIUtKxatfD1SJTXw 5Fg/+Z+T0+ea4eAItR8YTHE9YBtAtdo= X-Received: by 2002:a17:90b:4c84:: with SMTP id my4mr3182095pjb.3.1586270824425; Tue, 07 Apr 2020 07:47:04 -0700 (PDT) Received: from hsinchu02.internal.sifive.com (220-132-236-182.HINET-IP.hinet.net. [220.132.236.182]) by smtp.gmail.com with ESMTPSA id y15sm14190093pfc.206.2020.04.07.07.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2020 07:47:04 -0700 (PDT) From: Zong Li To: palmer@dabbelt.com, paul.walmsley@sifive.com, aou@eecs.berkeley.edu, mhiramat@kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Zong Li Subject: [PATCH v4 3/9] riscv: patch code by fixmap mapping Date: Tue, 7 Apr 2020 22:46:48 +0800 Message-Id: <0b6ad2759b47731a83008b46dbbed7c92e68cae2.1586265122.git.zong.li@sifive.com> X-Mailer: git-send-email 2.26.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On strict kernel memory permission, the ftrace have to change the permission of text for dynamic patching the intructions. Use riscv_patch_text_nosync() to patch code instead of probe_kernel_write. Signed-off-by: Zong Li Suggested-by: Masami Hiramatsu --- arch/riscv/kernel/ftrace.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/arch/riscv/kernel/ftrace.c b/arch/riscv/kernel/ftrace.c index c40fdcdeb950..08396614d6f4 100644 --- a/arch/riscv/kernel/ftrace.c +++ b/arch/riscv/kernel/ftrace.c @@ -7,9 +7,23 @@ #include #include +#include #include +#include #ifdef CONFIG_DYNAMIC_FTRACE +int ftrace_arch_code_modify_prepare(void) __acquires(&text_mutex) +{ + mutex_lock(&text_mutex); + return 0; +} + +int ftrace_arch_code_modify_post_process(void) __releases(&text_mutex) +{ + mutex_unlock(&text_mutex); + return 0; +} + static int ftrace_check_current_call(unsigned long hook_pos, unsigned int *expected) { @@ -46,20 +60,14 @@ static int __ftrace_modify_call(unsigned long hook_pos, unsigned long target, { unsigned int call[2]; unsigned int nops[2] = {NOP4, NOP4}; - int ret = 0; make_call(hook_pos, target, call); - /* replace the auipc-jalr pair at once */ - ret = probe_kernel_write((void *)hook_pos, enable ? call : nops, - MCOUNT_INSN_SIZE); - /* return must be -EPERM on write error */ - if (ret) + /* Replace the auipc-jalr pair at once. Return -EPERM on write error. */ + if (patch_text_nosync + ((void *)hook_pos, enable ? call : nops, MCOUNT_INSN_SIZE)) return -EPERM; - smp_mb(); - flush_icache_range((void *)hook_pos, (void *)hook_pos + MCOUNT_INSN_SIZE); - return 0; } -- 2.26.0