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=-2.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 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 B555FC33C99 for ; Tue, 7 Jan 2020 08:28:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 95CF42080A for ; Tue, 7 Jan 2020 08:28:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726514AbgAGI2p (ORCPT ); Tue, 7 Jan 2020 03:28:45 -0500 Received: from mx2.suse.de ([195.135.220.15]:45134 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726485AbgAGI2p (ORCPT ); Tue, 7 Jan 2020 03:28:45 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 0A8EBAAA6; Tue, 7 Jan 2020 08:28:43 +0000 (UTC) Date: Tue, 7 Jan 2020 09:28:42 +0100 From: Petr Mladek To: Peter Zijlstra Cc: Jann Horn , bpf@vger.kernel.org, live-patching@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Steven Rostedt , KP Singh , Andy Lutomirski , kernel list , the arch/x86 maintainers , Josh Poimboeuf , Ingo Molnar Subject: Re: BPF tracing trampoline synchronization between update/freeing and execution? Message-ID: <20200107082842.5w6zjgxy56wiftmm@pathway.suse.cz> References: <20200106165654.GP2844@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200106165654.GP2844@hirez.programming.kicks-ass.net> User-Agent: NeoMutt/20170912 (1.9.0) Sender: live-patching-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: live-patching@vger.kernel.org On Mon 2020-01-06 17:56:54, Peter Zijlstra wrote: > On Mon, Jan 06, 2020 at 05:39:30PM +0100, Jann Horn wrote: > > Hi! > > > > I was chatting with kpsingh about BPF trampolines, and I noticed that > > it looks like BPF trampolines (as of current bpf-next/master) seem to > > be missing synchronization between trampoline code updates and > > trampoline execution. Or maybe I'm missing something? > > > > If I understand correctly, trampolines are executed directly from the > > fentry placeholders at the start of arbitrary kernel functions, so > > they can run without any locks held. So for example, if task A starts > > executing a trampoline on entry to sys_open(), then gets preempted in > > the middle of the trampoline, and then task B quickly calls > > BPF_RAW_TRACEPOINT_OPEN twice, and then task A continues execution, > > task A will end up executing the middle of newly-written machine code, > > which can probably end up crashing the kernel somehow? > > > > I think that at least to synchronize trampoline text freeing with > > concurrent trampoline execution, it is necessary to do something > > similar to what the livepatching code does with klp_check_stack(), and > > then either use a callback from the scheduler to periodically re-check > > tasks that were in the trampoline or let the trampoline tail-call into > > a cleanup helper that is part of normal kernel text. And you'd > > probably have to gate BPF trampolines on > > CONFIG_HAVE_RELIABLE_STACKTRACE. > > ftrace uses synchronize_rcu_tasks() to flip between trampolines iirc. ftrace calls also schedule_on_each_cpu(ftrace_sync) to handle situations where RCU is not watching, see rcu_is_watching(). The following is called in ftrace_shutdown(): schedule_on_each_cpu(ftrace_sync); if (IS_ENABLED(CONFIG_PREEMPTION)) synchronize_rcu_tasks(); arch_ftrace_trampoline_free(ops); Best Regards, Petr