From patchwork Wed Mar 27 16:04:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 13607033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A98DBCD1280 for ; Wed, 27 Mar 2024 16:06:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=CspQzca/+MaEIWC7SwJ1tCunEK7+fDNqICimS/xzRsI=; b=Hp4VsTAbLEksYK l9vtA3g8JvlzVWgU0/8t6c/9kHZZf2K1KUVpHPY2rL3lClmzCer/N1b7YcTonMikXwMrzU47lGWZR 9TiSGaj2Hw3PdoabaTpEc3/4Aw8zW0Yqh5WC4/yUHfewwNlgg0CPLK61K5tCH1wevWPYD8fURzlkQ lM8vAaNUHpLc06I2PRnSeXH5e+/7jsgPdZn0BUyQTFTsaBz4BGDJ+3Idw/HXQKPMbdYKykfVyWdMY UVD6903Rp6/ux4vTYEYSdyxj5K2COdcJf/fj2jlWThorZKhqJdahbYLxuQ/J/hvb8ZcCC0Jc0ADih KzvsAq1xpQKC5DcLH5MQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpVnF-00000009zbX-2isy; Wed, 27 Mar 2024 16:06:25 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpVmQ-00000009zA4-2XbB for linux-riscv@lists.infradead.org; Wed, 27 Mar 2024 16:05:45 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1e0d6356ce9so25928805ad.3 for ; Wed, 27 Mar 2024 09:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1711555529; x=1712160329; darn=lists.infradead.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=RSdeTCpLrbMRKKMvHtorfIir+maEw9krU8ML3vSQsXQ=; b=KEyMtn/uDLo+QcauVV8z6qAeFbkXF4mtS2LzxSw92wX2R9Ti9pjudywYapefaxTFhq pOsnz7QsGVAm92/pzwf/I3LwxsCSS/k5fcrlvhD8h3wTJBM3ht3FyYYm6uJGXMWX1LA9 gdE91JEZYo9pqpSWMNnxfqdsaevf2kAs6D2yxxCo1kUwGvHG6eT1LLd44ma2vmBrctry KMHvWCIqhyGrMTDDD6IXNxSIdQ3607lh1aFUSavIoWVqLwmu0gieZIg+LE/Fv3v+9CwY F4rVaKsCZGM4TDwvdRTFfYpiotZ4/1jeoqQwODc63VL9J8vtj9razKR35pxePQoj5kat OhWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711555529; x=1712160329; 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=RSdeTCpLrbMRKKMvHtorfIir+maEw9krU8ML3vSQsXQ=; b=V5pEY31SPdrg8HQn6mNFcowP3C+jSRxF9X2bi98N36INRnnfdBgZlLO+8XD0jFoXpO JV92PkJgc4liYynvEtU8pDoaT8dxOW4/zNFxW61sR0nDxpTd3cQ4YNzLJHloZjBJNXA9 L1M/QV6pMQwfV+fpW5m21UTPL4P+1ATC/27Y0W0TjK3odiJEsH7QAiknmK2M2kkyRhvy rTsoGXYf0Zn4G1ZTQI9Ha1kPNw5aMzjzDkp3+LdQG+F1aRvzmP0uHSaOTAwk/XqoxQBt GJn3RiRldYh0YwXb2/dhLnzX9T45n2wHWxRT2jf/UHRUiu6UqwC2oddQTqkS1t6YEHmu 0vVg== X-Forwarded-Encrypted: i=1; AJvYcCUWk/X1ZuiDT6fZnGfDypazgaZ6QZ2tl5KVh1y2Maz6FtSeSsa/Sa4N5Al5elqWslYMGk3RSNcyabiOoGzrPt573G1rqzM8h7FT3dRqaFum X-Gm-Message-State: AOJu0Yw8N4sjZ6qy9zj67M+eV1bJ1NPCUjI7LHNn/kanuUWmIUv+FlGd GmMDGQYIQM8ewtqnGbRPfqhLaWdCDh/3UTAnFue75rSA8J5PsK+ccGjkPgP7K50= X-Google-Smtp-Source: AGHT+IEqRVMXpfbvhLVGnT+rNK/EL/AmWPjQNfVTk43pBb2QF8NUiJFixCdoiQv5OswSn3xtBhEfyw== X-Received: by 2002:a17:903:8cd:b0:1df:fa1a:529f with SMTP id lk13-20020a17090308cd00b001dffa1a529fmr148822plb.24.1711555529446; Wed, 27 Mar 2024 09:05:29 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b001e14807f9cesm1718226plb.125.2024.03.27.09.05.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 09:05:28 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Samuel Holland , Daniel Borkmann , bpf@vger.kernel.org Subject: [PATCH v2 5/7] riscv: Pass patch_text() the length in bytes Date: Wed, 27 Mar 2024 09:04:44 -0700 Message-ID: <20240327160520.791322-6-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240327160520.791322-1-samuel.holland@sifive.com> References: <20240327160520.791322-1-samuel.holland@sifive.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240327_090539_176514_71327441 X-CRM114-Status: GOOD ( 16.05 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org patch_text_nosync() already handles an arbitrary length of code, so this removes a superfluous loop and reduces the number of icache flushes. Reviewed-by: Björn Töpel Signed-off-by: Samuel Holland Reviewed-by: Conor Dooley --- Changes in v2: - Remove unnecessary line wrapping arch/riscv/include/asm/patch.h | 2 +- arch/riscv/kernel/patch.c | 14 +++++--------- arch/riscv/kernel/probes/kprobes.c | 18 ++++++++++-------- arch/riscv/net/bpf_jit_comp64.c | 7 ++++--- 4 files changed, 20 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/patch.h b/arch/riscv/include/asm/patch.h index 9f5d6e14c405..7228e266b9a1 100644 --- a/arch/riscv/include/asm/patch.h +++ b/arch/riscv/include/asm/patch.h @@ -9,7 +9,7 @@ int patch_insn_write(void *addr, const void *insn, size_t len); int patch_text_nosync(void *addr, const void *insns, size_t len); int patch_text_set_nosync(void *addr, u8 c, size_t len); -int patch_text(void *addr, u32 *insns, int ninsns); +int patch_text(void *addr, u32 *insns, size_t len); extern int riscv_patch_in_stop_machine; diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 0d1700d1934c..243e1573410b 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -19,7 +19,7 @@ struct patch_insn { void *addr; u32 *insns; - int ninsns; + size_t len; atomic_t cpu_count; }; @@ -231,14 +231,10 @@ NOKPROBE_SYMBOL(patch_text_nosync); static int patch_text_cb(void *data) { struct patch_insn *patch = data; - unsigned long len; - int i, ret = 0; + int ret = 0; if (atomic_inc_return(&patch->cpu_count) == num_online_cpus()) { - for (i = 0; ret == 0 && i < patch->ninsns; i++) { - len = GET_INSN_LENGTH(patch->insns[i]); - ret = patch_insn_write(patch->addr + i * len, &patch->insns[i], len); - } + ret = patch_insn_write(patch->addr, patch->insns, patch->len); /* * Make sure the patching store is effective *before* we * increment the counter which releases all waiting CPUs @@ -258,13 +254,13 @@ static int patch_text_cb(void *data) } NOKPROBE_SYMBOL(patch_text_cb); -int patch_text(void *addr, u32 *insns, int ninsns) +int patch_text(void *addr, u32 *insns, size_t len) { int ret; struct patch_insn patch = { .addr = addr, .insns = insns, - .ninsns = ninsns, + .len = len, .cpu_count = ATOMIC_INIT(0), }; diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c index fecbbcf40ac3..f3cbc3ec9e6a 100644 --- a/arch/riscv/kernel/probes/kprobes.c +++ b/arch/riscv/kernel/probes/kprobes.c @@ -23,13 +23,13 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *); static void __kprobes arch_prepare_ss_slot(struct kprobe *p) { + size_t len = GET_INSN_LENGTH(p->opcode); u32 insn = __BUG_INSN_32; - unsigned long offset = GET_INSN_LENGTH(p->opcode); - p->ainsn.api.restore = (unsigned long)p->addr + offset; + p->ainsn.api.restore = (unsigned long)p->addr + len; - patch_text_nosync(p->ainsn.api.insn, &p->opcode, 1); - patch_text_nosync(p->ainsn.api.insn + offset, &insn, 1); + patch_text_nosync(p->ainsn.api.insn, &p->opcode, len); + patch_text_nosync(p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(insn)); } static void __kprobes arch_prepare_simulate(struct kprobe *p) @@ -116,16 +116,18 @@ void *alloc_insn_page(void) /* install breakpoint in text */ void __kprobes arch_arm_kprobe(struct kprobe *p) { - u32 insn = (p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32 ? - __BUG_INSN_32 : __BUG_INSN_16; + size_t len = GET_INSN_LENGTH(p->opcode); + u32 insn = len == 4 ? __BUG_INSN_32 : __BUG_INSN_16; - patch_text(p->addr, &insn, 1); + patch_text(p->addr, &insn, len); } /* remove breakpoint from text */ void __kprobes arch_disarm_kprobe(struct kprobe *p) { - patch_text(p->addr, &p->opcode, 1); + size_t len = GET_INSN_LENGTH(p->opcode); + + patch_text(p->addr, &p->opcode, len); } void __kprobes arch_remove_kprobe(struct kprobe *p) diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c index aac190085472..6ed4035a8136 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -15,6 +15,7 @@ #include "bpf_jit.h" #define RV_FENTRY_NINSNS 2 +#define RV_FENTRY_NBYTES (RV_FENTRY_NINSNS * 4) #define RV_REG_TCC RV_REG_A6 #define RV_REG_TCC_SAVED RV_REG_S6 /* Store A6 in S6 if program do calls */ @@ -663,7 +664,7 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type, if (ret) return ret; - if (memcmp(ip, old_insns, RV_FENTRY_NINSNS * 4)) + if (memcmp(ip, old_insns, RV_FENTRY_NBYTES)) return -EFAULT; ret = gen_jump_or_nops(new_addr, ip, new_insns, is_call); @@ -672,8 +673,8 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type, cpus_read_lock(); mutex_lock(&text_mutex); - if (memcmp(ip, new_insns, RV_FENTRY_NINSNS * 4)) - ret = patch_text(ip, new_insns, RV_FENTRY_NINSNS); + if (memcmp(ip, new_insns, RV_FENTRY_NBYTES)) + ret = patch_text(ip, new_insns, RV_FENTRY_NBYTES); mutex_unlock(&text_mutex); cpus_read_unlock();