Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp1623825pxx; Fri, 30 Oct 2020 14:49:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxU0IbpXEEFV79GU6C6Arsr/9Cqgr0wqHY/S/Eqhonm/ZmsW8DAhh3eWpX+oSPnqMJBrEN3 X-Received: by 2002:a17:906:8610:: with SMTP id o16mr4444872ejx.209.1604094596620; Fri, 30 Oct 2020 14:49:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1604094596; cv=none; d=google.com; s=arc-20160816; b=vIm4VdcaUJY18vhhwVfadNxzLIl54+8AewiZeDpCszhRptaFUAxoPGEo1BWJtCt1Z7 EQ9n5TXCc5kI7WFKmaLoWpGpLC0WzTf47ZQjKlXDR3Ts9aFxblVGyeouMA/Z/M+DCluD CWVsl5Lk0ZVz274xRg1N9pGd3BibtrAQcgXMydFnOkjOb7xP7jfSbWIkcjP71chl4Rml xFayzhkUzAUQjr/eEjWQg4bKyhbrdpxNDw+H674xLfG/5QDfpo9KLX5iku/D8/m12UiZ 7PYmr8qChn8BIuisusoj0r1jxqOrUUQe3nwx//c5LagyRxJeXjtuc7OGbHRw57J3wUFq 3ZIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=2H6cPN0gnqQZAudY3hjr6N8uSef0pLDKUZk1I184RGs=; b=HckHdKtpwHz5Jsx/M1fI+fC5Wudihsm90b5vcYBfu3LtUYmogzFT2sIFeTMM+G7qQj cSWA0sKaUjL8UHSZDPM2WBWo6cj0zE3/lHb2TbVCAKfBDwetZOHGsJzp8oLdzEmTSRzI JGdHVPtJDr0PzSH0QHlhqKHBix8ZpuTJyEFWH4zbkeq+F6FFbnQw2py8FU7l+AMgjLTh r//QRUthlk9wa9dxnJKZDwKMWkM+cmPDW4TbkdZ1VAuJ6rOqXQDlR3i8H5VNnxVby2r5 ipwbDBwDz4I8wjvaG1n1kaoPdJdp9KcxN0Qls3cujQduxCzkoGK7F2YM0KRuaat35pGe kjMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=Iuoe3TrD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s15si6124265edi.111.2020.10.30.14.49.33; Fri, 30 Oct 2020 14:49:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=Iuoe3TrD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725797AbgJ3VsI (ORCPT + 99 others); Fri, 30 Oct 2020 17:48:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725780AbgJ3VsI (ORCPT ); Fri, 30 Oct 2020 17:48:08 -0400 Received: from mail-il1-x143.google.com (mail-il1-x143.google.com [IPv6:2607:f8b0:4864:20::143]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48C12C0613D2 for ; Fri, 30 Oct 2020 14:48:08 -0700 (PDT) Received: by mail-il1-x143.google.com with SMTP id k6so7772289ilq.2 for ; Fri, 30 Oct 2020 14:48:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2H6cPN0gnqQZAudY3hjr6N8uSef0pLDKUZk1I184RGs=; b=Iuoe3TrDQMXYgYBYoFwqatJLMHxMuM2piSNX8Ny+q2pX80tWo4LElHitBLDDbAYVXS sscYvyHpT0v9rwQQchtbt1McePjU14Ij5+lJ5QmFFswHp/Ip8zo1TXFcD0ibt/UqW44P +BRIaRIDeDNZAomnbqHwTB2ggHd4z/LUAqsWI= 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=2H6cPN0gnqQZAudY3hjr6N8uSef0pLDKUZk1I184RGs=; b=eAhO9Tvb0DvExBwSWG0hpreoCt1zH6Vrd0wrXoZYTZdCN6/4fI4PpVQaRNtfa103kW Dz7Pu5EAlBzJH1MyneyAUrBhyAxQQ83LXl5KJLCq5hVPfK1g52x8klXrv5kerbQQ4dHe 6801KSKQ/8GCJa1CdVkX65+mB6j2MZjY++aSYh7rsd+V824JHjD0rbppqIfkWCsiIsVG zEpY93P5s4mgJjfzbzIifwg8BWHGYRsE2r5Ggs5uUMVHtH0x05NPtL2ncpLpkRJ4H3j2 Oly9XNkMcgK4RkaZceR0G25L2MWNYmVRPylS9ye5tIqepkpOll3VSw0xcRKuyN/DAI9L I61Q== X-Gm-Message-State: AOAM530AU/W1rmpHADzcYuhcAledndTHibmtQaHPvjCMr7932YATHs5a RO32FjCYl0dczxul+z14M9xD4CQWZhqR9kTb5u2E X-Received: by 2002:a92:cf41:: with SMTP id c1mr2471028ilr.219.1604094487353; Fri, 30 Oct 2020 14:48:07 -0700 (PDT) MIME-Version: 1.0 References: <20201021073839.43935-1-zong.li@sifive.com> In-Reply-To: From: Atish Patra Date: Fri, 30 Oct 2020 14:47:56 -0700 Message-ID: Subject: Re: [PATCH] stop_machine: Mark functions as notrace To: Guo Ren Cc: Zong Li , Paul McKenney , josh@joshtriplett.org, Steven Rostedt , mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com, joel@joelfernandes.org, vincent.whitchurch@axis.com, Thomas Gleixner , Paul Walmsley , Palmer Dabbelt , Masami Hiramatsu , Greentime Hu , Colin Ian King , rcu@vger.kernel.org, Linux Kernel Mailing List , linux-riscv Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 29, 2020 at 8:28 PM Guo Ren wrote: > > On Fri, Oct 30, 2020 at 2:46 AM Atish Patra wrote: > > > > On Thu, Oct 29, 2020 at 9:06 AM Guo Ren wrote: > > > > > > On Thu, Oct 29, 2020 at 10:34 AM Zong Li wrote: > > > > > > > > On Thu, Oct 29, 2020 at 8:23 AM Atish Patra wrote: > > > > > > > > > > On Wed, Oct 28, 2020 at 8:44 AM Guo Ren wrote: > > > > > > > > > > > > Hi Zong & Atish, > > > > > > > > > > > > In our 2 harts c910 chip, we found: > > > > > > > > > > > > echo function > /sys/kernel/debug/tracing/current_tracer > > > > > > echo function_graph > /sys/kernel/debug/tracing/current_tracer > > > > > > echo function > /sys/kernel/debug/tracing/current_tracer > > > > > > echo function_graph > /sys/kernel/debug/tracing/current_tracer > > > > > > > > > > > > Then one core halted at stop_machine_yield: > > > > > > arch_cpu_idle () at arch/riscv/kernel/process.c:39 > > > > > > 39 local_irq_enable(); > > > > > > (gdb) i th > > > > > > Id Target Id Frame > > > > > > * 1 Thread 1 (CPU#0) arch_cpu_idle () at arch/riscv/kernel/process.c:39 > > > > > > 2 Thread 2 (CPU#1) stop_machine_yield > > > > > > (cpumask=0xffffffe001371fa8 <__cpu_online_mask>) at > > > > > > ./arch/riscv/include/asm/vdso/processor.h:12 > > > > > > (gdb) thread 2 > > > > > > [Switching to thread 2 (Thread 2)] > > > > > > #0 stop_machine_yield (cpumask=0xffffffe001371fa8 > > > > > > <__cpu_online_mask>) at ./arch/riscv/include/asm/vdso/processor.h:12 > > > > > > 12 __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy)); > > > > > > > > > > > > With your patch, it's solved. For this patch, I'll give: > > > > > > Tested by: Guo Ren > > > > > > > > > > > > But that's not enough, we still need: > > > > > > > > > > > > diff --git a/arch/riscv/kernel/sbi.c b/arch/riscv/kernel/sbi.c > > > > > > index 226ccce..12b8808 100644 > > > > > > --- a/arch/riscv/kernel/sbi.c > > > > > > +++ b/arch/riscv/kernel/sbi.c > > > > > > @@ -376,7 +376,7 @@ EXPORT_SYMBOL(sbi_send_ipi); > > > > > > * > > > > > > * Return: None > > > > > > */ > > > > > > -void sbi_remote_fence_i(const unsigned long *hart_mask) > > > > > > +void notrace sbi_remote_fence_i(const unsigned long *hart_mask) > > > > > > { > > > > > > __sbi_rfence(SBI_EXT_RFENCE_REMOTE_FENCE_I, > > > > > > hart_mask, 0, 0, 0, 0); > > > > > > diff --git a/arch/riscv/mm/cacheflush.c b/arch/riscv/mm/cacheflush.c > > > > > > index 400b945d..9467d987 100644 > > > > > > --- a/arch/riscv/mm/cacheflush.c > > > > > > +++ b/arch/riscv/mm/cacheflush.c > > > > > > @@ -9,12 +9,12 @@ > > > > > > > > > > > > #include > > > > > > > > > > > > -static void ipi_remote_fence_i(void *info) > > > > > > +static void notrace ipi_remote_fence_i(void *info) > > > > > > { > > > > > > return local_flush_icache_all(); > > > > > > } > > > > > > > > > > > > -void flush_icache_all(void) > > > > > > +void notrace flush_icache_all(void) > > > > > > { > > > > > > if (IS_ENABLED(CONFIG_RISCV_SBI)) > > > > > > sbi_remote_fence_i(NULL); > > > > > > > > > > > > > > > > Did you see any issue if these functions are not marked as notrace ? > > > > > > > > > > As per Zong's explanation, the issue was that the other harts already > > > > > fetched the next 2 nops and > > > > > executed 1 while kernel patching replaced other with one of the auipc > > > > > + jalr pair. > > > > > > > > > > @Zong can correct me if I am wrong. > > > > > > > > > > These functions are too far ahead. Can it cause such issues ? If yes, > > > > > then we need to mark each and every function > > > > > that can be invoked from patch_text_nosync and are not inlined. > > > > > > > > > > That includes copy_to_kernel_nofault, __sbi_rfence_v02, > > > > > __sbi_rfence_v02_call, sbi_ecall. > > > > > > > > > > Few of these functions may be inlined by compiler. Can we depend on that ? > > > > > > > > > > > Because: > > > > > > (gdb) bt > > > > > > #0 flush_icache_all () at arch/riscv/mm/cacheflush.c:20 > > > > > > #1 0xffffffe00020473a in patch_text_nosync (addr=, insns= > > > > > > , len=) at arch/riscv/kernel/patch.c:96 > > > > > > #2 0xffffffe000206792 in ftrace_make_call (rec=, > > > > > > addr=) at arch/riscv/kernel/ftrace.c:109 > > > > > > #3 0xffffffe0002c9be4 in __ftrace_replace_code (rec=0xffffffe01ae40020, e > > > > > > nable=true) at kernel/trace/ftrace.c:2503 > > > > > > #4 0xffffffe0002ca092 in ftrace_replace_code (mod_flags= > > > > > out>) at kernel/trace/ftrace.c:2530 > > > > > > #5 0xffffffe0002ca24a in ftrace_modify_all_code (command=9) at kernel > > > > > > /trace/ftrace.c:2677 > > > > > > #6 0xffffffe0002ca2ee in __ftrace_modify_code (data=) at > > > > > > kernel/trace/ftrace.c:2703 > > > > > > #7 0xffffffe0002c1390 in multi_cpu_stop (data=0x0) at kernel/stop_machin > > > > > > e.c:224 > > > > > > #8 0xffffffe0002c0fbe in cpu_stopper_thread (cpu=) at kern > > > > > > el/stop_machine.c:491 > > > > > > #9 0xffffffe0002343be in smpboot_thread_fn (data=0x0) at kernel/smpboot. > > > > > > c:165 > > > > > > #10 0xffffffe00022f894 in kthread (_create=0xffffffe01af13040) at kern > > > > > > el/kthread.c:292 > > > > > > #11 0xffffffe000201fac in handle_exception () at arch/riscv/kernel/entry.S:236 > > > > > > > > > > > > > > It seems to me that the problem happens on the waiting threads, it > > > No, that is the call trace to show ftrace_make_call -> > > > flush_icache_all and we should give notrace on the whole path. > > > > > > > Hmm. I am curious to understand how other architectures avoid this problem. > > for arm64 > static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, > bool validate) > { > u32 replaced; > ... > if (aarch64_insn_patch_text_nosync((void *)pc, new)) > return -EPERM; > > int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn) > { > u32 *tp = addr; > int ret; > > /* A64 instructions must be word aligned */ > if ((uintptr_t)tp & 0x3) > return -EINVAL; > > ret = aarch64_insn_write(tp, insn); > if (ret == 0) > __flush_icache_range((uintptr_t)tp, > (uintptr_t)tp + AARCH64_INSN_SIZE); > > Look at arm64, they __kprobes flag and I guess it would also prevent > ftrace call site. > Are you sure about that ? __kprobes puts the code in .kprobes.text section which is under whitelist sections in recordmcount.pl & recordmcount.c. > __flush_icache_range is written in asm and no possible ftrace call site. > > > Is it a bigger issue in RISC-V because we have to switch privilege > > mode to sync I/D cache ? > We should sync I/D cache at s-mode because we need virtual address. > For c910 we've added icache broadcast invalid instructions by physical > address and virtual address. > > Current linux/arch/riscv I/D cache sync is so expensive. > Yes. It is a known fact. Unfortunately, RISC-V specifications doesn't allow any other method yet. I hope the specification is modified to allow some method to sync I/D cache from S-mode soon. > -- > Best Regards > Guo Ren > > ML: https://lore.kernel.org/linux-csky/ -- Regards, Atish