Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp258426ybb; Fri, 3 Apr 2020 02:06:43 -0700 (PDT) X-Google-Smtp-Source: APiQypL8HowiyjUohxRbou9MJYRt2SZrio8KvXNHfHCvt7fnopSjPgtePxXpLj87tWMciUBCxDZY X-Received: by 2002:a9d:1a3:: with SMTP id e32mr5781213ote.206.1585904802890; Fri, 03 Apr 2020 02:06:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585904802; cv=none; d=google.com; s=arc-20160816; b=Fl9NYW+gAbYRcXXhFQIUuLDWUFkRqj50ba1XHBOYe+W73VdSaAkjcFavdkwYyghxMD A1VxeihJeuzFqhoOJXa3ANgVisIFJnpphy+Cn49gM653eAc2AUgyIkCipxYoE9fOlsTA rH1scavI/ne5rq7mZGg8mt4NfNWwdvZkWSPtyXfKHX8KVPBrxeGijrKORtRc0ZDqGl03 /xSJ4ORgOc3gewJnLYKZjD+KzzjgDc2lHen1ymYMGDjOKWexxw8dtdgNdu2nnmQ5WIkf tq9+fQ6NmP3UclalGk9Lu59qjP2IX6xcdUtj+WDmFjohoZomg3mXigre6K5ZJrqXIw9t 0ftQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=k3k/FHfRrWrYtfi+UJknDJkxs4/RbdowOhFZ8Z2iWdM=; b=JvnaboG0OeZFqwAwEWgZdxeOsZ0fzRgj/mFywvhXegzKVEqXvGwUg7ubuC7zDiQJN1 P9nJ3aBFHbRWBZirJCP2qIbdtqnS3AnOe6aBMbgrJCgeTy67La/N0nbWKePUVkVeHUYW WPLOcW7iHLqZeXuhy7vuESayAOW5uHbZXAEeiJmG+ni5pB0Cbe4uCfTciT8if4C+Qp8a Eijtbs2E5ExaCNw23reprPM/ryONAXFhzo+74xTkOn2N3aGS9y483HUq9WvQ97bIckds //xQ51NMDXSvzEvCSmVsRrBnBy4lSX7OWz81E48rRwgwFVmCyszTmTR2EUkytXN40hY5 0q+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=cGHwfv2D; 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 g124si3559778oia.0.2020.04.03.02.06.30; Fri, 03 Apr 2020 02:06:42 -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=cGHwfv2D; 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 S1727879AbgDCJFC (ORCPT + 99 others); Fri, 3 Apr 2020 05:05:02 -0400 Received: from mail-oi1-f193.google.com ([209.85.167.193]:44871 "EHLO mail-oi1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727635AbgDCJFC (ORCPT ); Fri, 3 Apr 2020 05:05:02 -0400 Received: by mail-oi1-f193.google.com with SMTP id v134so5458246oie.11 for ; Fri, 03 Apr 2020 02:05:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=k3k/FHfRrWrYtfi+UJknDJkxs4/RbdowOhFZ8Z2iWdM=; b=cGHwfv2DRr36j1c2rTxyvR/MF3X2vakyJoCIVMx8L1kaZ4RlB6abpoa7W57bNztggz lgHpay/LPc54nWXoD03Wa2l3QIxm0xDS7zrWfRLnKCB8r5aj3/4HRam8GxCxnP6cIdzD HVkXi84hVEERv7nK7ttIFp/xLCTRWbfg0c7z3gdqMyb8r1Wa9yCMp8JyXLDZsMsflUJr urBA40BCxuFoQn9IAEwYrt7AWUpb78QdyoYTGri24sF0lRBmNajO2+4DFiGhfs1GjeNz tl6CqDDFNs9mdIF3RIvg7+qax2xWXiZV2tIj5gneVVEdI9xLMw4LRA1Mw/YlDy0A4SPT hAhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=k3k/FHfRrWrYtfi+UJknDJkxs4/RbdowOhFZ8Z2iWdM=; b=S7wvT01W6XTYnVDxZehHT6T9xITgPqkhQtcxSW7o0ySl4bOBWvqGVlb5t0Oq3ZUxu3 STFyuB15x58eSJcgexGPc9Zz7kyLWKSN9gj7ATikRMPbP+sXibh4IdrthXvj6gEP1+uA /sCRe3lQes2k1zWxm8CaczkvpKQ26aGWfTeIQmGPJBboF+Q2vH8vJ+0pR/2KkIGcDm09 tNw/mBTW31DdOOF/7UwavKDuOtS+JysxeiSyrTSzILfj8yvJ1rCtOXlcGIQHqiP4t6pS pN2LyoyDOusREsRRmiIK+absEwsbm3u+Jt0tpZL2O/cQ833rNLSebEMm9KGIvcHSHDIm HWtg== X-Gm-Message-State: AGi0PuZiLIcZe93EsTB6zej03v8rMBrJfzg1p9DLiyj2JYhBq/Q8tAjq F+Ye8w0hSFcByVYRXmKTfNZ9ewpsg+PsqjvWO+bz5Q== X-Received: by 2002:a05:6808:56:: with SMTP id v22mr2110639oic.116.1585904701421; Fri, 03 Apr 2020 02:05:01 -0700 (PDT) MIME-Version: 1.0 References: <20200401003233.17fe4b6f7075e5b8f0ed5114@kernel.org> <20200402101733.1ef240faeaeada6e4d38ae80@kernel.org> In-Reply-To: <20200402101733.1ef240faeaeada6e4d38ae80@kernel.org> From: Zong Li Date: Fri, 3 Apr 2020 17:04:51 +0800 Message-ID: Subject: Re: [PATCH v3 8/9] riscv: introduce interfaces to patch kernel code To: Masami Hiramatsu Cc: Palmer Dabbelt , Paul Walmsley , Albert Ou , linux-riscv , "linux-kernel@vger.kernel.org List" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 2, 2020 at 9:17 AM Masami Hiramatsu wrote: > > Hi, > > On Wed, 1 Apr 2020 15:42:30 +0800 > Zong Li wrote: > > > > > + > > > > +static int __kprobes riscv_insn_write(void *addr, const void *insn, size_t len) > > > > > > Why would you add "riscv_" prefix for those functions? It seems a bit odd. > > > > There is no particular reason, I just was used to adding a prefix for > > arch-related stuff. I have no preference here, it's OK to me to remove > > the prefix of these functions, do you think we need to remove them? > > Yeah, it will be better, unless it can mixed up with arch-independent > functions. OK. I'll remove it and use NOKPROBE_SYMBOL() instead of __kprobes annotation. > > > > > +{ > > > > + void *waddr = addr; > > > > + bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE; > > > > + unsigned long flags = 0; > > > > + int ret; > > > > + > > > > + raw_spin_lock_irqsave(&patch_lock, flags); > > > > > > This looks a bit odd since stop_machine() is protected by its own mutex, > > > and also the irq is already disabled here. > > > > We need it because we don't always enter the riscv_patch_text_nosync() > > through stop_machine mechanism. If we call the > > riscv_patch_text_nosync() directly, we need a lock to protect the > > page. > > Oh, OK, but it leads another question. Is that safe to patch the > text without sync? Would you use it for UP system? > I think it is better to clarify "in what case user can call _nosync()" > and add a comment on it. The ftrace is one of the cases, as documentation of ftrace said, when dynamic ftrace is initialized, it calls kstop_machine to make the machine act like a uniprocessor so that it can freely modify code without worrying about other processors executing that same code. So the ftrace called the _nosync interface here directly. > > Thank you, > > > > > > > > > Thank you, > > > > > > > + > > > > + if (across_pages) > > > > + patch_map(addr + len, FIX_TEXT_POKE1); > > > > + > > > > + waddr = patch_map(addr, FIX_TEXT_POKE0); > > > > + > > > > + ret = probe_kernel_write(waddr, insn, len); > > > > + > > > > + patch_unmap(FIX_TEXT_POKE0); > > > > + > > > > + if (across_pages) > > > > + patch_unmap(FIX_TEXT_POKE1); > > > > + > > > > + raw_spin_unlock_irqrestore(&patch_lock, flags); > > > > + > > > > + return ret; > > > > +} > > > > +#else > > > > +static int __kprobes riscv_insn_write(void *addr, const void *insn, size_t len) > > > > +{ > > > > + return probe_kernel_write(addr, insn, len); > > > > +} > > > > +#endif /* CONFIG_MMU */ > > > > + > > > > +int __kprobes riscv_patch_text_nosync(void *addr, const void *insns, size_t len) > > > > +{ > > > > + u32 *tp = addr; > > > > + int ret; > > > > + > > > > + ret = riscv_insn_write(tp, insns, len); > > > > + > > > > + if (!ret) > > > > + flush_icache_range((uintptr_t) tp, (uintptr_t) tp + len); > > > > + > > > > + return ret; > > > > +} > > > > + > > > > +static int __kprobes riscv_patch_text_cb(void *data) > > > > +{ > > > > + struct riscv_insn_patch *patch = data; > > > > + int ret = 0; > > > > + > > > > + if (atomic_inc_return(&patch->cpu_count) == 1) { > > > > + ret = > > > > + riscv_patch_text_nosync(patch->addr, &patch->insn, > > > > + GET_INSN_LENGTH(patch->insn)); > > > > + atomic_inc(&patch->cpu_count); > > > > + } else { > > > > + while (atomic_read(&patch->cpu_count) <= num_online_cpus()) > > > > + cpu_relax(); > > > > + smp_mb(); > > > > + } > > > > + > > > > + return ret; > > > > +} > > > > + > > > > +int __kprobes riscv_patch_text(void *addr, u32 insn) > > > > +{ > > > > + struct riscv_insn_patch patch = { > > > > + .addr = addr, > > > > + .insn = insn, > > > > + .cpu_count = ATOMIC_INIT(0), > > > > + }; > > > > + > > > > + return stop_machine_cpuslocked(riscv_patch_text_cb, > > > > + &patch, cpu_online_mask); > > > > +} > > > > -- > > > > 2.25.1 > > > > > > > > > > > > > -- > > > Masami Hiramatsu > > > -- > Masami Hiramatsu