Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7368505imu; Thu, 27 Dec 2018 19:00:31 -0800 (PST) X-Google-Smtp-Source: AFSGD/Wb3MnHZ1EEqnehYIA7YpFUZWfJAsVaj5B4jxgcONe1Q0KeGzCyrf2b/NiYgYsA+ShWLrc2 X-Received: by 2002:a62:4bcf:: with SMTP id d76mr27607941pfj.170.1545966031110; Thu, 27 Dec 2018 19:00:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545966031; cv=none; d=google.com; s=arc-20160816; b=MuW1OUGJ2tnmaHBII4XMdLLbQTyEUL47734N5TJtF5OwaHRcx0j9tint5buSrhOpml 5wKALT37gh6SDCEGhRCyVwae5rGbG1R1MRPVk5qP10nL2Se4M0WjVbhVp4AWBZ2PEdvb rV6EYSoJtiUdJ5U28TKyYyBGaBNlFwYBSbNhPApO4Kb4i/wBbNyRiprR/Ed2Oltt2JLJ DlZqSYaj02F9hecYWD114gM6IR7SbitZBy1VQ85xKUoyPtAVvV7CIJR+pGDSTWSBac7R z/U41wStzDQqLOSzfHSUeRxC7WDU08mY6P25FNKf9kVE10LBEaPTJC1+nSGp7bzkOad5 TzZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=dL10Nbf5DaOn0Qr5ZWd0sebGIsPWW1EyeGiD9qCqudo=; b=pVyb6FbrxUDZGlVWDKiB4TpvK72vm9Ja0gOwLh2P2tI4BXM9lDnxll125lZpCkKs4o bxGfo+fSKr6KUy9h80CsNQ4I2SsSNARwKt/5oZZZpp0A1YZv3VSlNc0aXQhetctD4hM6 CPqPZC05E5N7mZ2j/fivD3Lilz+A9VsD7KS/6mUdX7i6dh/qgwSNyUNv4/NFV83q7gHH ++7p8/5yBMuA1Sxh3MhTBNer953Ifz6DU+rl+4oQBmKZ0vihCaySvidcc+OrbEZ48Obc To/g0POiZdjmn/agwTbV3u6vU1BNaUrDLk91pMBVoAO2UA3ksTJWUpL6M9RZrsjKgefE yPhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=pFhlksWD; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a13si36672669pgb.412.2018.12.27.19.00.15; Thu, 27 Dec 2018 19:00:31 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=pFhlksWD; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729264AbeL0RJj (ORCPT + 99 others); Thu, 27 Dec 2018 12:09:39 -0500 Received: from mail-wm1-f68.google.com ([209.85.128.68]:52281 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728359AbeL0RJj (ORCPT ); Thu, 27 Dec 2018 12:09:39 -0500 Received: by mail-wm1-f68.google.com with SMTP id m1so17230995wml.2 for ; Thu, 27 Dec 2018 09:09:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=dL10Nbf5DaOn0Qr5ZWd0sebGIsPWW1EyeGiD9qCqudo=; b=pFhlksWDvKOj+9lOLuhkMxidFpJmWIiqW40Ndy1La9HOmtXE/hYoTgcL9R5ZmCuWUD NiAPRabOZdtyUjotXGyUUCsbKyXMlZO/gkIZkQHkunJq5WMJ0winWnxbdRS5lbTz54Q4 BlKPTYGOFEhqYBTbWQfwKBjxG3xTOFv+154b2qu//7qrVRxVwKyyFOwpYS+d+1XtoW5U vty7t+S6AdYu0ei8k1PtWBn9CRyrIPByBKs89wRVJcddyCn3ULm3zSmTOeZoHD+OroWc XFG7CnQsKKDVxrU6bVPvp8Dxy1buje9JRXwoHP9RZMR2S/SI8OkOD9O3Cbz2tXMm5V72 h9sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=dL10Nbf5DaOn0Qr5ZWd0sebGIsPWW1EyeGiD9qCqudo=; b=Mr65ggARmPrVe03PqY5XQ0qtOFOzM9OR2AymJFrecQ4tZ+QycQXWccZoPyktmW+o0p TtHIImm7RwOw18NycFSCEXx2TnPJ8K9vICSZm2d/LANGYLfykgyPJQCA6h+jLdPwnGqW /5SAUZAnYuassCn4QecrGJ9ejHjT3AoC8MpATzCLrlPSVo7rH0fmogNejG2qIglgn37J 7aWPseV5f+Y5k/ijTGKnHB76D8mdFzv/cw/99nubv/OtFfkaLcW9MjT0mGQ9aQgLi1b6 7TOE1gxNiF27Ds/7qXbWPTg/UdSuCkxocXRbt/1B6r8rLa+kY+6Mt11W+iMG9RYacjBK JuOQ== X-Gm-Message-State: AJcUukceBKIjQFMr7HjBRymlmQaqP2Uwz6Wf/gzeV7jP4BziPwuZZR1f L8oY25WABuBZqnkjShO0cQ== X-Received: by 2002:a1c:934e:: with SMTP id v75mr22139107wmd.126.1545930576223; Thu, 27 Dec 2018 09:09:36 -0800 (PST) Received: from localhost (host254-25-dynamic.53-79-r.retail.telecomitalia.it. [79.53.25.254]) by smtp.gmail.com with ESMTPSA id v8sm34826871wrq.53.2018.12.27.09.09.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 27 Dec 2018 09:09:35 -0800 (PST) Date: Thu, 27 Dec 2018 18:09:34 +0100 From: Andrea Righi To: Masami Hiramatsu Cc: Ingo Molnar , "Naveen N . Rao" , Anil S Keshavamurthy , "David S . Miller" , Yonghong Song , Andy Lutomirski , Thomas Gleixner , Borislav Petkov , "H . Peter Anvin" , x86@kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 0/3] x86: kprobes: Show correct blaclkist in debugfs Message-ID: <20181227170934.GA2057@xps-13> References: <154503482486.26176.6224515860220847638.stgit@devbox> <20181217154713.GA1308@Dell> <20181218135026.6f96a89247e9b70fa45afbe9@kernel.org> <20181218172134.GA2902@xps-13> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181218172134.GA2902@xps-13> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 18, 2018 at 06:24:35PM +0100, Andrea Righi wrote: > On Tue, Dec 18, 2018 at 01:50:26PM +0900, Masami Hiramatsu wrote: > ... > > > Side question: there are certain symbols in arch/x86/xen that should be > > > blacklisted explicitly, because they're non-attachable. > > > > > > More exactly, all functions defined in arch/x86/xen/spinlock.c, > > > arch/x86/xen/time.c and arch/x86/xen/irq.c. > > > > > > The reason is that these files are compiled without -pg to allow the > > > usage of ftrace within a Xen domain apparently (from > > > arch/x86/xen/Makefile): > > > > > > ifdef CONFIG_FUNCTION_TRACER > > > # Do not profile debug and lowlevel utilities > > > CFLAGS_REMOVE_spinlock.o = -pg > > > CFLAGS_REMOVE_time.o = -pg > > > CFLAGS_REMOVE_irq.o = -pg > > > endif > > > > > > Actually, the reason why you can not probe those functions via > > tracing/kprobe_events is just a side effect. You can probe it if you > > write a kprobe module. Since the kprobe_events depends on some ftrace > > tracing functions, it sometimes cause a recursive call problem. To avoid > > this issue, I have introduced a CONFIG_KPROBE_EVENTS_ON_NOTRACE, see > > commit 45408c4f9250 ("tracing: kprobes: Prohibit probing on notrace function"). > > > > If you set CONFIG_KPROBE_EVENTS_ON_NOTRACE=n, you can continue putting probes > > on Xen spinlock functions too. > > OK. > > > > > > Do you see a nice and clean way to blacklist all these functions > > > (something like arch_populate_kprobe_blacklist()), or should we just > > > flag all of them explicitly with NOKPROBE_SYMBOL()? > > > > As I pointed, you can probe it via your own kprobe module. Like systemtap, > > you still can probe it. The blacklist is for "kprobes", not for "kprobe_events". > > (Those are used to same, but since the above commit, those are different now) > > > > I think the most sane solution is, identifying which (combination of) functions > > in ftrace (kernel/trace/*) causes a problem, marking those NOKPROBE_SYMBOL() and > > removing CONFIG_KPROBE_EVENTS_ON_NOTRACE. I'm planning to spend a little bit more time on this and see if I can identify the problematic ftrace functions and eventually drop CONFIG_KPROBE_EVENTS_ON_NOTRACE, following the sane solution. However, in the meantime, with the following patch I've been able to get a more reliable kprobes blacklist and show also the notrace functions in debugfs when CONFIG_KPROBE_EVENTS_ON_NOTRACE is off. It's probably ugly and inefficient, because it's iterating over all symbols in x86's arch_populate_kprobe_blacklist(), but it seems to work for my specific use case, so I thought it shouldn't be bad to share it, just in case (maybe someone else is also interested). Thanks, From: Andrea Righi Subject: [PATCH] x86: kprobes: automatically blacklist all non-traceable functions Iterate over all symbols to detect those that are non-traceable and blacklist them. Signed-off-by: Andrea Righi --- arch/x86/kernel/kprobes/core.c | 11 +++++++++-- kernel/kprobes.c | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c index 4ba75afba527..8cc7191ba3f9 100644 --- a/arch/x86/kernel/kprobes/core.c +++ b/arch/x86/kernel/kprobes/core.c @@ -1026,10 +1026,17 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr) } NOKPROBE_SYMBOL(kprobe_fault_handler); +static int do_kprobes_arch_blacklist(void *data, const char *name, + struct module *mod, unsigned long addr) +{ + if (arch_within_kprobe_blacklist(addr)) + kprobe_add_ksym_blacklist(addr); + return 0; +} + int __init arch_populate_kprobe_blacklist(void) { - return kprobe_add_area_blacklist((unsigned long)__entry_text_start, - (unsigned long)__entry_text_end); + return kallsyms_on_each_symbol(do_kprobes_arch_blacklist, NULL); } int __init arch_init_kprobes(void) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index f4ddfdd2d07e..2e824cd536ba 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -1389,11 +1389,29 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p) return ret; } +#if defined(CONFIG_KPROBES_ON_FTRACE) && \ + !defined(CONFIG_KPROBE_EVENTS_ON_NOTRACE) +static bool within_notrace(unsigned long addr) +{ + unsigned long offset, size; + + if (!kallsyms_lookup_size_offset(addr, &size, &offset)) + return true; + return !ftrace_location_range(addr - offset, addr - offset + size); +} +#else +static bool within_notrace(unsigned long addr) +{ + return false; +} +#endif + bool __weak arch_within_kprobe_blacklist(unsigned long addr) { /* The __kprobes marked functions and entry code must not be probed */ - return addr >= (unsigned long)__kprobes_text_start && - addr < (unsigned long)__kprobes_text_end; + return (addr >= (unsigned long)__kprobes_text_start && + addr < (unsigned long)__kprobes_text_end) || + within_notrace(addr); } bool within_kprobe_blacklist(unsigned long addr) -- 2.17.1