From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 218C4C2BA1E for ; Mon, 6 Apr 2020 10:37:03 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id E6FEC206C3 for ; Mon, 6 Apr 2020 10:37:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="LU5kw2Zu"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="IhbJ0oUw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E6FEC206C3 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+infradead-linux-riscv=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:To: Subject:Message-ID:Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=YnUc67zZ/CRLBeX19uteUhR1k0R3ljqlsHXrgc36ZpM=; b=LU5kw2ZuY77KC675HVcKZBERI Vm+v2VvluNQo6DrbOt8Rfvrlxgos/4V3+vIU7rnHS0HizAQJKIjOfQOTOI8YS2RNmWi1adpt+jxEb 5lnnssEUI8oENPM0mCraIucxYftsuB9ikfWa0QstiLsHSHp5F4lnxO2lWzGJvtSCsO2unrxSLPPXI aXTBwcxnHXeB05osOV1hrI+4nEl2jOKjd2SFhOYduReuMaKeSM3jzAK+YmZ3CmVr+AvBA2O6rndn6 xURB5ppNOkE8Zq+tR2MeEgc3asn00P1koRcUX7QVUvgC2oHA50Vtd7eSEef4PMlX8Z9sIV9x/lDh2 bCxs5f81Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jLP7h-0006Ok-Ve; Mon, 06 Apr 2020 10:36:57 +0000 Received: from mail-oi1-x241.google.com ([2607:f8b0:4864:20::241]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jLP7d-0006O1-SL for linux-riscv@lists.infradead.org; Mon, 06 Apr 2020 10:36:55 +0000 Received: by mail-oi1-x241.google.com with SMTP id q204so12574726oia.13 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=mo1YY65F7ZxpwX0J4nxDpj3wr7QqJVGntp9LCasGwmBE84yAc+mEE799FUNVR0O9Yl vZmjse9vEUQIz8sy1mxZjaaRUgJvJlJyUwQPI7tKyjf5RuAdXxOPeCKrLh3imYWYSCkE FbUqjNNnA1sgLTUozJnAR/HZPK3psI8eJF38tyDdDZ0kP6JeP46MLjzeWbSGPJdblzDI MZ4MwYA5Jjv1nJr8vkqziHld1elNFv1Qy9W+qwlg1z9tPJ6OFvp+VQu1B8s7m8YlNQH5 MouThSHw7bvGSw4kJ3OsrIjDqwbBkgIs3qeA7wzTu+eoEVZ8cFDKXp4E1oWVVVnEHS8m QgXA== X-Gm-Message-State: AGi0PuYzY0Y3pIZHerN4sX7NJbAn+3SpGuBJWWtxJroNRPlh2ED3uf/F xeo+eNDtwZHfWS/W+ZakwUPOz0eGzYgE3Mb0O/q4hg== X-Google-Smtp-Source: APiQypIkvAa/Z6iNoR5Ipeze6p/8TgwGtMmW1cKIGCS+3DI3TnGLwPkip6UEEdHQPIB3pNKQE3lCwZ2IBAdcIXfzNDI= 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 Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200406_033654_432838_E967CFE8 X-CRM114-Status: GOOD ( 22.38 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-riscv , Albert Ou , Palmer Dabbelt , "linux-kernel@vger.kernel.org List" , Paul Walmsley Sender: "linux-riscv" Errors-To: linux-riscv-bounces+infradead-linux-riscv=archiver.kernel.org@lists.infradead.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