Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp4361365ybb; Tue, 7 Apr 2020 06:07:40 -0700 (PDT) X-Google-Smtp-Source: APiQypK4yQjjdmh94AoQ21l6Fks+isLNqWkJNoKr6OYOOJbolMUWckXs2WGSrRK88NYJ6JHMkUXs X-Received: by 2002:a4a:aece:: with SMTP id v14mr1749135oon.48.1586264860183; Tue, 07 Apr 2020 06:07:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586264860; cv=none; d=google.com; s=arc-20160816; b=J+NrKPazNUVbdn3fctY49q1muvUU1jB9DXJOlCpwp/ypFRxXA3c/l41JWgHF8239YD DE0tRNzDooVmS91Wl3yfm5u6eZMAqats3ZDJYvc7ScZj9aPtn+Z5qXcDqGwZ6E7EUq78 6h6dBjhXMKA6auI5iA9Fska3ywopXU1L5+l9W49OZapPKP0T3GgR8Gc1EymjLCOmcPNW 9qV/gFnJ5UKhWP8HdX8nlD6nfEs9dt3NDa0Mm5wMUlkxvcSZLJg+TyfWGxML5wPJeiKa Czr6cLk6q/hvlNSYMSeQXjKxvJRoUFJL3Rpqhb1xpr0fFQtXDgH2fXnh+UlceYpkpgUH x4ow== 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=zy4GWkHjoSAA1VBEBggmBAO3XC31aJRabe80zBna8OQ=; b=RwSf+eNVe2aywrymeH1CUcj3z/QwJlqk9To6yma0BJWoYJw2NtdlH/mcC8T0tinimG ogstXCqrgPS0couiFt+6Yh9wyzXuzAMqR9lF2NqT/S7CDyhm++CLq/UrDPTqJP++X8h2 FM9OcqLeapOGEJiPWKdFr5tvFMBMPA6Zt6WARVt8oTS8m3XrAU0Uu3m5oRjLFvx4vi+K B3hKOc6dWkrvZflzGT6yIhg+j7PpL34D1+3dsqQEGolf31UOibkgrCKPuXD/PYo7nGQM QxGGSZ85TYFSz7kA7lwUr99X2pBSbzIDX8XSTJFl70TBOrC5qNY57U3mG4T5B0WN73qG NiAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=Wi+tsH87; 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 u7si1258682oou.82.2020.04.07.06.07.20; Tue, 07 Apr 2020 06:07:40 -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=Wi+tsH87; 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 S1728799AbgDGNGP (ORCPT + 99 others); Tue, 7 Apr 2020 09:06:15 -0400 Received: from mail-ot1-f68.google.com ([209.85.210.68]:37691 "EHLO mail-ot1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726562AbgDGNGP (ORCPT ); Tue, 7 Apr 2020 09:06:15 -0400 Received: by mail-ot1-f68.google.com with SMTP id g23so3008919otq.4 for ; Tue, 07 Apr 2020 06:06:14 -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=zy4GWkHjoSAA1VBEBggmBAO3XC31aJRabe80zBna8OQ=; b=Wi+tsH87h9dbxKaSway7BosMBbnj9lbUAyXu8vVb6Tay1JEaiyBABT+84KTuVY015i ebtQKpQTyS7TdbLbDzvLo3/1vL8D3PFkX1vCh8MfhZc0bfuDOD0JZZu1RKZ1ItDFLp+x s6sP6tLPzgwJpLbLiiv3jsx/+Ky3/yHCZv4TEVH6gvgkEd+LoDxXdYOrm00fTJ7Sy1Oj bMRaFofflgwerKQccxWr1JOVnI5YqKoCJKR0Np81yByRUgyfY3j6YbVo1/QOMa1ZGGvp XboKSw6xNDcY4YaUzYLb1KeAteU5G4zDsW8IeVG8Dl/gnc/ZLvAFm8CS3a8BkeEP4r5p Bafw== 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=zy4GWkHjoSAA1VBEBggmBAO3XC31aJRabe80zBna8OQ=; b=gaVHX4ZUWbPxYkcsmnPVMWkp8LF6M8r7fJR//rv5bkI180J3RhfGJkRMhnRTzV9rH2 2xwEtdu6n8AyjEoaJ+3APxJKUE0bDuVpRTuOG334/gdhkAWjrTM8a5aAswlJrUBb1qSf hIQ8xnS4oFeck2Q++scz50KD61Xi7RXLN4dHhY29k4IxEeIV+Pd/D2gKtYJj49aZaNxi r7/fWpfSi/0S6HMx2viIvf5N73qCh8i3ckbQd2BP+yJalS6YnpClIzGHrOQs7j3Wvi88 MIeUa9rR27lrR2X32nD6UYvG0/wHBjDg7U/S3rdZxwfcMv8PkF/wnt+eoIPZZD4bkson ACXg== X-Gm-Message-State: AGi0PuaYO9cB3g9hdn1qvqrW2TBeYzKes9dFQxzfxQB+MTjBVy5tl3vP kD0p5J8VGE8q6BMc7pzsLUwJGeSHY9J39fCUZMkjsg== X-Received: by 2002:a05:6830:1e10:: with SMTP id s16mr1475505otr.33.1586264773945; Tue, 07 Apr 2020 06:06:13 -0700 (PDT) MIME-Version: 1.0 References: <20200401003233.17fe4b6f7075e5b8f0ed5114@kernel.org> <20200402101733.1ef240faeaeada6e4d38ae80@kernel.org> <20200404121428.596911ba5653f8b18a80eab2@kernel.org> <20200407212918.235324cbc82e9e4deb839b14@kernel.org> In-Reply-To: <20200407212918.235324cbc82e9e4deb839b14@kernel.org> From: Zong Li Date: Tue, 7 Apr 2020 21:06:03 +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 Tue, Apr 7, 2020 at 8:29 PM Masami Hiramatsu wrote: > > On Mon, 6 Apr 2020 18:36:42 +0800 > Zong Li wrote: > > > 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. > > Yes, you need to implement ftrace_arch_code_modify_prepare() and > ftrace_arch_code_modify_post_process() in arch/riscv/kernel/ftrace.c. > Please see arch/x86/kernel/ftrace.c. > Oh ok, I misunderstood it before, I just use text_mutex instead of patch_lock in patch.c. Thanks. > Thank you, > > > > > # 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 > > > -- > Masami Hiramatsu