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=-16.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 DC208C433C1 for ; Wed, 24 Mar 2021 11:32:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A653061A0A for ; Wed, 24 Mar 2021 11:32:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232679AbhCXLcD (ORCPT ); Wed, 24 Mar 2021 07:32:03 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:37984 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230296AbhCXLbt (ORCPT ); Wed, 24 Mar 2021 07:31:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1616585509; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=osAVL8fG7IneP33aiY4VrrfHvzLWEWC/Z/+HBmNuouc=; b=XZTw6pHk/6SGCKKVZ0IjJh6YYSVJH82WDKffuupJ2lFwrgXWKANouqiKwmIZK1KsYtVqd8 Di24eH4X/yyyiEdZywf8qYpMXAjs1dE19qbnNYlOJh50cNhPvE7Bo16gG8ZcvWjlv4Vdxo GijF/8UPJ8g2jmDS0nX15z2YizwRqXE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-9-hRGzPux_OuqRr-A5_XbuQA-1; Wed, 24 Mar 2021 07:31:45 -0400 X-MC-Unique: hRGzPux_OuqRr-A5_XbuQA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CDC7F801817; Wed, 24 Mar 2021 11:31:42 +0000 (UTC) Received: from krava (unknown [10.40.196.25]) by smtp.corp.redhat.com (Postfix) with SMTP id 7138910023AF; Wed, 24 Mar 2021 11:31:39 +0000 (UTC) Date: Wed, 24 Mar 2021 12:31:38 +0100 From: Jiri Olsa To: Alexei Starovoitov Cc: Jiri Olsa , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: Re: [PATCH bpf] bpf: Take module reference for ip in module code Message-ID: References: <20210323211533.1931242-1-jolsa@kernel.org> <20210324012237.65pf4s52oqlicea3@ast-mbp> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210324012237.65pf4s52oqlicea3@ast-mbp> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Tue, Mar 23, 2021 at 06:22:37PM -0700, Alexei Starovoitov wrote: > On Tue, Mar 23, 2021 at 10:15:33PM +0100, Jiri Olsa wrote: > > Currently module can be unloaded even if there's a trampoline > > register in it. It's easily reproduced by running in parallel: > > > > # while :; do ./test_progs -t module_attach; done > > # while :; do ./test_progs -t fentry_test; done > > > > Taking the module reference in case the trampoline's ip is > > within the module code. Releasing it when the trampoline's > > ip is unregistered. > > > > Signed-off-by: Jiri Olsa > > --- > > kernel/bpf/trampoline.c | 32 ++++++++++++++++++++++++++++++++ > > 1 file changed, 32 insertions(+) > > > > diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c > > index 1f3a4be4b175..f6cb179842b2 100644 > > --- a/kernel/bpf/trampoline.c > > +++ b/kernel/bpf/trampoline.c > > @@ -87,6 +87,27 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key) > > return tr; > > } > > > > +static struct module *ip_module_get(unsigned long ip) > > +{ > > + struct module *mod; > > + int err = 0; > > + > > + preempt_disable(); > > + mod = __module_text_address(ip); > > + if (mod && !try_module_get(mod)) > > + err = -ENOENT; > > + preempt_enable(); > > + return err ? ERR_PTR(err) : mod; > > +} > > + > > +static void ip_module_put(unsigned long ip) > > +{ > > + struct module *mod = __module_text_address(ip); > > Conceptually looks correct, but how did you test it?! > Just doing your reproducer: > while :; do ./test_progs -t module_attach; done & while :; do ./test_progs -t fentry_test; done > > I immediately hit: > [ 19.461162] WARNING: CPU: 1 PID: 232 at kernel/module.c:264 module_assert_mutex_or_preempt+0x2e/0x40 > [ 19.477126] Call Trace: > [ 19.477464] __module_address+0x28/0xf0 > [ 19.477865] ? __bpf_trace_bpf_testmod_test_write_bare+0x10/0x10 [bpf_testmod] > [ 19.478711] __module_text_address+0xe/0x60 > [ 19.479156] bpf_trampoline_update+0x2ff/0x470 I don't have lockdep enabled.. ah the module_mutex is held during module init, that's why all the code I was using as a reference did not take it.. sorry, will fix > > Which points to an obvious bug above. > > How did you debug it to this module going away issue? > Why does test_progs -t fentry_test help to repro? > Or does it? > It doesn't touch anything in modules. test_prog also loads/unloads that module, but it could be just insmod/rmmod instead, will change jirka