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.0 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, 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 94379C33C8C for ; Mon, 6 Jan 2020 16:57:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 65FB62146E for ; Mon, 6 Jan 2020 16:57:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="a5X7Xgvn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726546AbgAFQ5Q (ORCPT ); Mon, 6 Jan 2020 11:57:16 -0500 Received: from merlin.infradead.org ([205.233.59.134]:43152 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726448AbgAFQ5P (ORCPT ); Mon, 6 Jan 2020 11:57:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=KtA6VhFyLFqi7kWb+PYao12HEhPZ8hVXP/AvLkDg57w=; b=a5X7Xgvn+fusWiITMXIBJxch6 EibH0GZfsBa2Tv9KJ+ZrJeS+EMTFUA37iCHDQ/323udgQbtuCpXVVvBd2Cb47R25VSxG2xSyKYPoH Mg4HVzLO1jpMtvDH/YH0NMqO8phqEKdv8gwhGNdVwRb5rVIcSf0XB5xLWl1Wgd+Ifl16JA37EXwaS aAJOJnW0/B1z9hvPB6HziekS9RhBQ/qb1DMlJIqpu1mONgo4IEaaiUsFfub1LuVbNUtWf/DolglDm KaEVL1bzv8juBkJM4P1B8MhCoMUPZwSewjJRJLwdhjw8AtT9mr6JESczMhgynMp9FsMRptO6rtteP gu+7D+CfA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1ioVgY-00046W-7E; Mon, 06 Jan 2020 16:56:58 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id CC2CF3012DC; Mon, 6 Jan 2020 17:55:22 +0100 (CET) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id E06D12B2844FC; Mon, 6 Jan 2020 17:56:54 +0100 (CET) Date: Mon, 6 Jan 2020 17:56:54 +0100 From: Peter Zijlstra To: Jann Horn Cc: 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: <20200106165654.GP2844@hirez.programming.kicks-ass.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Sender: live-patching-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: live-patching@vger.kernel.org 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.