Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3169420ybb; Mon, 6 Apr 2020 03:38:29 -0700 (PDT) X-Google-Smtp-Source: APiQypK8IS6GiTwCdWd0OW9e4GEuMv5KcrzkDdcFzJT9hJVLBjdpvjxpd/ZHc0TvSazn60W+wJIp X-Received: by 2002:aca:5b04:: with SMTP id p4mr12987665oib.105.1586169509226; Mon, 06 Apr 2020 03:38:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586169509; cv=none; d=google.com; s=arc-20160816; b=i7/FL/pWCOEjsrCQA2H+H5//P6qIsUqY9IcaXKxsVV5P1Xk0EEtuDw8/0ZoTIEbtbz 96jMtQSVjpoKraVD/2lBg/jx9tjo45KQQZqcJqWXqF/tQ89qEKPyQp5rur6ETm/RTDI6 Eb9Trj4+1q3KAc3nB81L0Xz3jdUQssMAVoSqqhfZPcB+kuujHTDQy/36ttNt909+ZT2M kfl/sl3iYIThe1ylhrCfalGApZcBZWC35FDlQknwxF0rsVZn4nOd1TQ9cguFO6e8Oc4i G8BY1HYPcWryaLWvX2d3/4yzHWAy/BA2rGaMpAnRmQROisRRurd0037k8Shk+8eckk0o kOpQ== 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=YnUc67zZ/CRLBeX19uteUhR1k0R3ljqlsHXrgc36ZpM=; b=sflR95DkfRv3ToHtU9ujDkZ8XrGzuW3VTjdrVaJTVKqsTyTbfUv8FPjNK8m3iP9kGg DA0EKrNolFICedYd1QUPnU9z1c9WQxrZQ0P4thkJVyYySgWflHu62CvFZ8sD7ZIotBUk BXuopr0JQKrcMg/qzZK+nJ+/ZFdm/+PX97evyPcYgsIEwayk/4OBuAGQT7CBDlKyPK9E UhU4rH3jA6Z1MPvX+OUyLVuuP6CPEstYpvYFnPVSPUtF2pfddWNVyldrneN6DaiRetBt All+NmNBX6f2UuPS6FXr+qrnm5UA7MLMy5+odHg9TynQV+yI4pusZXj67HiFoFDBZCNP TXPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=IhbJ0oUw; 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 l185si7242244oif.274.2020.04.06.03.38.16; Mon, 06 Apr 2020 03:38:29 -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=IhbJ0oUw; 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 S1726934AbgDFKgx (ORCPT + 99 others); Mon, 6 Apr 2020 06:36:53 -0400 Received: from mail-oi1-f193.google.com ([209.85.167.193]:43096 "EHLO mail-oi1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726841AbgDFKgx (ORCPT ); Mon, 6 Apr 2020 06:36:53 -0400 Received: by mail-oi1-f193.google.com with SMTP id k5so12605504oiw.10 for ; Mon, 06 Apr 2020 03:36:52 -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=YnUc67zZ/CRLBeX19uteUhR1k0R3ljqlsHXrgc36ZpM=; b=IhbJ0oUwADR2fgaEWeRMV48PrcMNnHViNOtP1JgRPDQBa9YPacnyR+DsYyzDGxhmPu Ap+6eKiQtZ7sciCLyzIlpXBqJU77ClQ4yVwBeiXS48W7BI2wDhiKRp6mOVeyivrhv0JX yDtMkr/uNfLm+rotKNnjhheKA9F84+eUuJmGZEDFsG7c4EO8lbULSRTIUUaQvQ/CqyoP s/lBySbL07cwsHZU0J6c1zhTMj3edWxmtrvHuzURQIHZQ4rk5Zs/x7zfTWtWN3aOIpZ5 AACYUMpZ+x6rMqY6GYJ9R2Of++CWq+oO3fC5s4Djxr3GBniWL2MezWmr3NOyfkxsMtTP kUxw== 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=YnUc67zZ/CRLBeX19uteUhR1k0R3ljqlsHXrgc36ZpM=; b=uUcwyhauv5yftwiNJ1KKf0O9dke4gJ4h0w3/XWWG1t1tnVH/BhTqsjj8T+59YHBif9 ulxlUK0nzcybwcQnmPYLZH7UG7JW65mtQs7wY7GXsY4OxJBWz4Ov/RnIC3XBmOHNKoma d6zmBRUmqSOgUxXoZgBShnHb4T/c5DDKd4NIh9DAg7cntn1oSDLRABxqR/qJ7eWl1qbu wquQHqTNS4xo+XnOBfcJk+iCkGPZr36OTj+zcGGPZyj8KY+3rQrbLehXt1QkBSiYNz7b 150gaMyIWIcHjF8b9F22kxBnk6pzCVKzxFg1MeAeQ8Q/ZDlmH7yry7vmunI7odbpkiVq W09g== X-Gm-Message-State: AGi0PubLmjC7+DGpiE0NEqKM/Y4L2XOXfP5g2pdZ1gngsyqGuywDJTP5 CGIonJMXeVZoRqep+lzde7FTnkhjg0goZGzNz4PyOg== X-Received: by 2002:a05:6808:a08:: with SMTP id n8mr12485794oij.91.1586169412277; Mon, 06 Apr 2020 03:36:52 -0700 (PDT) MIME-Version: 1.0 References: <20200401003233.17fe4b6f7075e5b8f0ed5114@kernel.org> <20200402101733.1ef240faeaeada6e4d38ae80@kernel.org> <20200404121428.596911ba5653f8b18a80eab2@kernel.org> In-Reply-To: From: Zong Li Date: Mon, 6 Apr 2020 18:36:42 +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 Sat, Apr 4, 2020 at 8:12 PM Zong Li wrote: > > On Sat, Apr 4, 2020 at 11:14 AM Masami Hiramatsu wrote: > > > > Hi Zong, > > > > On Fri, 3 Apr 2020 17:04:51 +0800 > > Zong Li wrote: > > > > > > > > > +{ > > > > > > > + 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. > > > > Hmm, even though, since it already running under kstop_machine(), no > > other thread will run. > > Could you consider to use text_mutex instead of that? The text_mutex > > is already widely used in x86 and kernel/kprobes.c etc. > > > > (Hmm, it seems except for x86, alternative code don't care about > > racing...) > > The mutex_lock doesn't seem to work in ftrace context, I think it might be the reason why other architectures didn't use text_mutex in somewhere. # echo function > current_tracer [ 28.198070] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:281 [ 28.198663] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 11, name: migration/0 [ 28.199491] CPU: 0 PID: 11 Comm: migration/0 Not tainted 5.6.0-00012-gd6f56a7a4be2-dirty #10 [ 28.200330] Call Trace: [ 28.200798] [] walk_stackframe+0x0/0xcc [ 28.201395] [] show_stack+0x3c/0x46 [ 28.200798] [] walk_stackframe+0x0/0xcc [ 28.201395] [] show_stack+0x3c/0x46 [ 28.201898] [] dump_stack+0x76/0x90 [ 28.202329] [] ___might_sleep+0x100/0x10e [ 28.202720] [] __might_sleep+0x4a/0x78 [ 28.203033] [] mutex_lock+0x2c/0x54 [ 28.203397] [] patch_insn_write+0x32/0xd8 [ 28.203780] [] patch_text_nosync+0x10/0x32 [ 28.204139] [] __ftrace_modify_call+0x5c/0x6c [ 28.204497] [] ftrace_update_ftrace_func+0x20/0x4a [ 28.204919] [] ftrace_modify_all_code+0xa0/0x148 [ 28.205378] [] __ftrace_modify_code+0x12/0x1c [ 28.205793] [] multi_cpu_stop+0xa2/0x158 [ 28.206147] [] cpu_stopper_thread+0xa4/0x13a [ 28.206510] [] smpboot_thread_fn+0xf8/0x1da [ 28.206868] [] kthread+0xfa/0x12a [ 28.207201] [] ret_from_exception+0x0/0xc > > Yes, text_mutex seems to be great. I'll change to use text_mutex in > the next version if it works fine after testing. Thanks. > > > Thank you, > > -- > > Masami Hiramatsu