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=-8.6 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham 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 B63F5C33CAA for ; Tue, 21 Jan 2020 04:54:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8922424654 for ; Tue, 21 Jan 2020 04:54:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579582484; bh=YQ38mVpjQZHpusPeDpF8uz55Se31rHxLJgTzd+LPI4k=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:List-ID: From; b=mfStl/yaZTPTS0IFF7YBtvhxd+A4PUBchYX66P73Hz7gXpy39XIkY+xiAaicHblW0 LkK6YPYeSUHWmxvgMKyZltE+OI8pSO3g0aG7n7Y1nekfQd+Erz2FSqFOP3u3KyRvCX pEUpMS26GCwe+YjKU2/8PoW0R33sn5Ed2fbpucxs= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728689AbgAUEyo (ORCPT ); Mon, 20 Jan 2020 23:54:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:59838 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728665AbgAUEyo (ORCPT ); Mon, 20 Jan 2020 23:54:44 -0500 Received: from paulmck-ThinkPad-P72.home (50-39-105-78.bvtn.or.frontiernet.net [50.39.105.78]) (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 247BD24653; Tue, 21 Jan 2020 04:54:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579582483; bh=YQ38mVpjQZHpusPeDpF8uz55Se31rHxLJgTzd+LPI4k=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=UFYnZfeXIVCE7WvV0ESV5me39RbySqfd+AG4s0SLSYnocI8qhKCe9TaEttgLla/FP C5/ykxYVixrA9nWN6/0QG/vbADyj7+o8jxhRloY4beVoTF7NExZ0DeBhwrF0sSLsdn 4kLvfK7qvUIoeNLTYPhY1HctNw4H7BUQuOGNf0M4= Received: by paulmck-ThinkPad-P72.home (Postfix, from userid 1000) id F3C443520AE0; Mon, 20 Jan 2020 20:54:42 -0800 (PST) Date: Mon, 20 Jan 2020 20:54:42 -0800 From: "Paul E. McKenney" To: Alexei Starovoitov Cc: davem@davemloft.net, daniel@iogearbox.net, jannh@google.com, netdev@vger.kernel.org, bpf@vger.kernel.org, kernel-team@fb.com Subject: Re: [PATCH bpf-next] bpf: Fix trampoline usage in preempt Message-ID: <20200121045442.GN2935@paulmck-ThinkPad-P72> Reply-To: paulmck@kernel.org References: <20200121032231.3292185-1-ast@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200121032231.3292185-1-ast@kernel.org> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org On Mon, Jan 20, 2020 at 07:22:31PM -0800, Alexei Starovoitov wrote: > Though the second half of trampoline page is unused a task could be > preempted in the middle of the first half of trampoline and two > updates to trampoline would change the code from underneath the > preempted task. Hence wait for tasks to voluntarily schedule or go > to userspace. > Add similar wait before freeing the trampoline. > > Fixes: fec56f5890d9 ("bpf: Introduce BPF trampoline") > Reported-by: Jann Horn > Signed-off-by: Alexei Starovoitov > --- > kernel/bpf/trampoline.c | 10 ++++++++++ > 1 file changed, 10 insertions(+) > > diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c > index 79a04417050d..7657ede7aee2 100644 > --- a/kernel/bpf/trampoline.c > +++ b/kernel/bpf/trampoline.c > @@ -160,6 +160,14 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr) > if (fexit_cnt) > flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; > > + /* Though the second half of trampoline page is unused a task could be > + * preempted in the middle of the first half of trampoline and two > + * updates to trampoline would change the code from underneath the > + * preempted task. Hence wait for tasks to voluntarily schedule or go > + * to userspace. > + */ > + synchronize_rcu_tasks(); So in this case, although the trampoline is not freed, it is reused. And we need to clear everyone off of the trampoline before reusing it. If this states the situation correctly: Acked-by: Paul E. McKenney > err = arch_prepare_bpf_trampoline(new_image, new_image + PAGE_SIZE / 2, > &tr->func.model, flags, > fentry, fentry_cnt, > @@ -251,6 +259,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr) > goto out; > if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT]))) > goto out; > + /* wait for tasks to get out of trampoline before freeing it */ > + synchronize_rcu_tasks(); > bpf_jit_free_exec(tr->image); > hlist_del(&tr->hlist); > kfree(tr); > -- > 2.23.0 >