Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7866215imu; Mon, 3 Dec 2018 21:51:42 -0800 (PST) X-Google-Smtp-Source: AFSGD/UCnYOLGE/16fRcBjjgpp9BCrG7drvwUKeReQ0GU66Tibws1qFEqWyflc4KXIgn33Q7oi8x X-Received: by 2002:a17:902:2ac3:: with SMTP id j61mr18795104plb.185.1543902702399; Mon, 03 Dec 2018 21:51:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543902702; cv=none; d=google.com; s=arc-20160816; b=jX3tJRk/QJ/shIZVbZdgblyiOdvnCWxEpp8upf5yrl9PBT1MIPXLCQMNQzLun/jfSk mD2V6WTbF1uLKu0xOcOYqexNaqiYH8YSrx+aEI/Y3/bum1FO6KPkERI8IBWrzLFOLVFm WXB7v+yEOSZOGub/hlpP3EZ0f7alRf6LjDZTTYKp+i3EdtF9jIjE1Y1A36iaSZG0px08 xtvx9sZ9RxBXG6KrNPDp7nI6Ag9dtnxpUHelLh10TEHafnoetUpd5bp/dMPcVUL55CR9 ctFRhH4F+408YjDLGDUerUh6K7aB3lQId8wcpAB+s+a0YFKPm2iWUxTQ/PStagpNf5Jx bjBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=wc8JizAJlzWY5do3CFyw1Wxjw37Hh8ihxlPn4jg/VpY=; b=G1MmJFqWTbx8aFlXVv5dK179pQky1e2Q0g6Q626bLlD8GXQWGoWgmbitEI4rwDVNY2 7KtMEUeD+BHPa7ga0PpgxoWsPXgIa1Wt2iCOs2AVvrM6aGGCdWtrgweIeNt29FBdj4yq TBKfQkpBZpKQdd67h0IjsJmQX892Mroa5BhI3FLMB+sPCITpLKkLDPUpm0IiMqyfarx/ UHxhbRlPp7hJRYk8ITSITr7Y9W80M7+8A8kvQx2KcoR44hqIi5v3+FSkyg6hjorLNC/X Wap/nXyrOvdORtmCF/RrjJL8R3nwNhtqe2dQrlul3CtgjHhO2g4tRvk0SbsnwWj9KyvE +QSQ== ARC-Authentication-Results: i=1; mx.google.com; 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 y10si14814147plt.406.2018.12.03.21.51.27; Mon, 03 Dec 2018 21:51:42 -0800 (PST) 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; 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 S1726085AbeLDFuP (ORCPT + 99 others); Tue, 4 Dec 2018 00:50:15 -0500 Received: from mail.kernel.org ([198.145.29.99]:52800 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726030AbeLDFuP (ORCPT ); Tue, 4 Dec 2018 00:50:15 -0500 Received: from vmware.local.home (unknown [184.48.106.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2BA0F20834; Tue, 4 Dec 2018 05:50:14 +0000 (UTC) Date: Tue, 4 Dec 2018 00:50:12 -0500 From: Steven Rostedt To: Arnd Bergmann Cc: Will Deacon , Anders Roxell , Ingo Molnar , Catalin Marinas , Kees Cook , Linux Kernel Mailing List , Linux ARM Subject: Re: [PATCH 3/3] arm64: ftrace: add cond_resched() to func ftrace_make_(call|nop) Message-ID: <20181204005012.11f73df9@vmware.local.home> In-Reply-To: References: <20181130150956.27620-1-anders.roxell@linaro.org> <20181203192228.GC29028@arm.com> X-Mailer: Claws Mail 3.15.1 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 3 Dec 2018 22:51:52 +0100 Arnd Bergmann wrote: > On Mon, Dec 3, 2018 at 8:22 PM Will Deacon wrote: > > > > Hi Anders, > > > > On Fri, Nov 30, 2018 at 04:09:56PM +0100, Anders Roxell wrote: > > > Both of those functions end up calling ftrace_modify_code(), which is > > > expensive because it changes the page tables and flush caches. > > > Microseconds add up because this is called in a loop for each dyn_ftrace > > > record, and this triggers the softlockup watchdog unless we let it sleep > > > occasionally. > > > Rework so that we call cond_resched() before going into the > > > ftrace_modify_code() function. > > > > > > Co-developed-by: Arnd Bergmann > > > Signed-off-by: Arnd Bergmann > > > Signed-off-by: Anders Roxell > > > --- > > > arch/arm64/kernel/ftrace.c | 10 ++++++++++ > > > 1 file changed, 10 insertions(+) > > > > It sounds like you're running into issues with the existing code, but I'd > > like to understand a bit more about exactly what you're seeing. Which part > > of the ftrace patching is proving to be expensive? > > > > The page table manipulation only happens once per module when using PLTs, > > and the cache maintenance is just a single line per patch site without an > > IPI. > > > > Is it the loop in ftrace_replace_code() that is causing the hassle? > > Yes: with an allmodconfig kernel, the ftrace selftest calls ftrace_replace_code > to look >40000 through ftrace_make_call/ftrace_make_nop, and these > end up calling > > static int __kprobes __aarch64_insn_write(void *addr, __le32 insn) > { > void *waddr = addr; > unsigned long flags = 0; > int ret; > > raw_spin_lock_irqsave(&patch_lock, flags); > waddr = patch_map(addr, FIX_TEXT_POKE0); > > ret = probe_kernel_write(waddr, &insn, AARCH64_INSN_SIZE); > > patch_unmap(FIX_TEXT_POKE0); > raw_spin_unlock_irqrestore(&patch_lock, flags); > > return ret; > } > 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); > > return ret; > } > > which seems to be where the main cost is. This is with inside of > qemu, and with lots of debugging options (in particular > kcov and ubsan) enabled, that make each function call > more expensive. I was thinking more about this. Would something like this work? -- Steve diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 8ef9fc226037..42e89397778b 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -2393,11 +2393,14 @@ void __weak ftrace_replace_code(int enable) { struct dyn_ftrace *rec; struct ftrace_page *pg; + bool schedulable; int failed; if (unlikely(ftrace_disabled)) return; + schedulable = !irqs_disabled() & !preempt_count(); + do_for_each_ftrace_rec(pg, rec) { if (rec->flags & FTRACE_FL_DISABLED) @@ -2409,6 +2412,8 @@ void __weak ftrace_replace_code(int enable) /* Stop processing */ return; } + if (schedulable) + cond_resched(); } while_for_each_ftrace_rec(); }