Message ID | 20240109164317.16371-1-asavkov@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-21121-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:2411:b0:101:2151:f287 with SMTP id m17csp235503dyi; Tue, 9 Jan 2024 08:44:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHuB3ruemIpW15y5nBSmjqRfcMd47dyhSfloe2X9F8mlk95y0BDlYM6Z+su92gq3gwCbDa7 X-Received: by 2002:a17:907:2ccf:b0:a23:36f7:4918 with SMTP id hg15-20020a1709072ccf00b00a2336f74918mr694055ejc.72.1704818646702; Tue, 09 Jan 2024 08:44:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704818646; cv=none; d=google.com; s=arc-20160816; b=wDBEII1aqySgwj0mB38LC/cxWFK/IVy8O8iUEguzBWqKHuKsIlJkgFfjqg/rp1q1zv qvuf6TvzYU1Zy9fcKCk1SfauD3wadXPD/fCaD/317813YbWDcY7PFqzCLix+2A/Iy4IO 7UZzx4VAF5VkH0NrnHSG0YzJKik0PEszG2FlXJlB7osn149w9hgMsAD+DeW3s9QXZPOm RmJp9j/m8y9nf9jEZgTPKFE4ponZ1dS8plqagnkFqOPmGz0zOdocI9NljsRpwbzzTgoY Y5VjefbzlU2tSdIEQMnXE9tH0OCOyFB4MVNrBUGX+iUuxspYtvJxoUxyqJZIyWAnt3Rk kDAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=nwZE6djCSMlJ+VRm2x3lAYqf79cV0k+KDTXbOaHEb1o=; fh=bEJ5O7v4tXTst3cJEb5TGX8sFhAOKh+R2nG8r5Z27K8=; b=JDKu65nVG7KUHLasnfPYnmLhEXwVSGnqcvBOUGSsJKYPP1uAdg8sGUtcPssV2h6dn8 WymTsM92okHvbtKiXnE9vrnyLch2uO0LFRxYDYb3bQs6UsimASqo7DovjhNZRJSQFIo2 44km1WVxXXfN6p9Vx/Qae7pt/QFOTAZr1f/h6c60rrW06bmQSG1NdXV/BsjAZRGtQ7FZ yGGPg4Vj4dDh89j7oT8oqcS7RM7LRxshUlNIafU8BzhsJ9Qc5lIIJ/lnTnQrrgWFo1jy +wBQvqy/Ey4BR5vEGNDvTreoOQ17h08rMpluqTqJe6+6NYtgyWvthHydlTLmh2w/cRhB ICiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=feTCm2m0; spf=pass (google.com: domain of linux-kernel+bounces-21121-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21121-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id d10-20020a1709061f4a00b00a27e0d1f0a3si939406ejk.546.2024.01.09.08.44.06 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 08:44:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21121-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=feTCm2m0; spf=pass (google.com: domain of linux-kernel+bounces-21121-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21121-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 4D9831F26004 for <ouuuleilei@gmail.com>; Tue, 9 Jan 2024 16:44:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F08139FFC; Tue, 9 Jan 2024 16:43:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="feTCm2m0" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 867273A294 for <linux-kernel@vger.kernel.org>; Tue, 9 Jan 2024 16:43:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704818605; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=nwZE6djCSMlJ+VRm2x3lAYqf79cV0k+KDTXbOaHEb1o=; b=feTCm2m0+b/XMGBuOqq6rmm3iWrRpLQEhrEHXujPnQeZb2gwPO1d4TWgEl+Mk9HgRVpTyA Q4WfRm1JJeRS52AaApgr9uoBzzZyjO1gFe+YdTEa+b8/1mGj2EwxCTbsfrCt4r5BsKbR9y cjcrUsDzPttr16INLX13dTWv2E9Fh2c= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-140-q87lt0BTMCS_eqIMFxCJwg-1; Tue, 09 Jan 2024 11:43:22 -0500 X-MC-Unique: q87lt0BTMCS_eqIMFxCJwg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DEADA1C051A0; Tue, 9 Jan 2024 16:43:21 +0000 (UTC) Received: from alecto.usersys.redhat.com (unknown [10.45.224.76]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0E22B2026D66; Tue, 9 Jan 2024 16:43:19 +0000 (UTC) From: Artem Savkov <asavkov@redhat.com> To: Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Andrii Nakryiko <andrii@kernel.org>, bpf@vger.kernel.org, netdev@vger.kernel.org, jolsa@kernel.org Cc: linux-kernel@vger.kernel.org, Artem Savkov <asavkov@redhat.com> Subject: [PATCH bpf-next] selftests/bpf: fix potential premature unload in bpf_testmod Date: Tue, 9 Jan 2024 17:43:17 +0100 Message-ID: <20240109164317.16371-1-asavkov@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787631917513482673 X-GMAIL-MSGID: 1787631917513482673 |
Series |
[bpf-next] selftests/bpf: fix potential premature unload in bpf_testmod
|
|
Commit Message
Artem Savkov
Jan. 9, 2024, 4:43 p.m. UTC
It is possible for bpf_kfunc_call_test_release() to be called from bpf_map_free_deferred() when bpf_testmod is already unloaded and perf_test_stuct.cnt which it tries to decrease is no longer in memory. This patch tries to fix the issue by waiting for all references to be dropped in bpf_testmod_exit(). The issue can be triggered by running 'test_progs -t map_kptr' in 6.5, but is obscured in 6.6 by d119357d07435 ("rcu-tasks: Treat only synchronous grace periods urgently"). Fixes: 65eb006d85a2a ("bpf: Move kernel test kfuncs to bpf_testmod") --- tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 4 ++++ 1 file changed, 4 insertions(+)
Comments
On 1/9/24 8:43 AM, Artem Savkov wrote: > It is possible for bpf_kfunc_call_test_release() to be called from > bpf_map_free_deferred() when bpf_testmod is already unloaded and > perf_test_stuct.cnt which it tries to decrease is no longer in memory. > This patch tries to fix the issue by waiting for all references to be > dropped in bpf_testmod_exit(). > > The issue can be triggered by running 'test_progs -t map_kptr' in 6.5, > but is obscured in 6.6 by d119357d07435 ("rcu-tasks: Treat only > synchronous grace periods urgently"). > > Fixes: 65eb006d85a2a ("bpf: Move kernel test kfuncs to bpf_testmod") Please add your Signed-off-by tag. I think the root cause is that bpf_kfunc_call_test_acquire() kfunc is defined in bpf_testmod and the kfunc returns some data in bpf_testmod. But the release function bpf_kfunc_call_test_release() is in the kernel. The release func tries to access some data in bpf_testmod which might have been unloaded. The prog_test_ref_kfunc is defined in the kernel, so no bpf_testmod btf reference is hold so bpf_testmod can be unloaded before bpf_kfunc_call_test_release(). As you mentioned, we won't have this issue if bpf_kfunc_call_test_acquire() is also in the kernel. I think putting bpf_kfunc_call_test_acquire() in bpf_testmod and bpf_kfunc_call_test_release() in kernel is not a good idea and confusing. But since this is only for tests, I guess we can live with that. With that, Acked-by: Yonghong Song <yonghong.song@linux.dev> > --- > tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > index 91907b321f913..63f0dbd016703 100644 > --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > @@ -2,6 +2,7 @@ > /* Copyright (c) 2020 Facebook */ > #include <linux/btf.h> > #include <linux/btf_ids.h> > +#include <linux/delay.h> > #include <linux/error-injection.h> > #include <linux/init.h> > #include <linux/module.h> > @@ -544,6 +545,9 @@ static int bpf_testmod_init(void) > > static void bpf_testmod_exit(void) > { > + while (refcount_read(&prog_test_struct.cnt) > 1) > + msleep(20); > + > return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); > } >
On Tue, Jan 09, 2024 at 11:40:38AM -0800, Yonghong Song wrote: > > On 1/9/24 8:43 AM, Artem Savkov wrote: > > It is possible for bpf_kfunc_call_test_release() to be called from > > bpf_map_free_deferred() when bpf_testmod is already unloaded and > > perf_test_stuct.cnt which it tries to decrease is no longer in memory. > > This patch tries to fix the issue by waiting for all references to be > > dropped in bpf_testmod_exit(). > > > > The issue can be triggered by running 'test_progs -t map_kptr' in 6.5, > > but is obscured in 6.6 by d119357d07435 ("rcu-tasks: Treat only > > synchronous grace periods urgently"). > > > > Fixes: 65eb006d85a2a ("bpf: Move kernel test kfuncs to bpf_testmod") > > Please add your Signed-off-by tag. Thanks for noticing. Will resend with signed-off-by and your ack. > I think the root cause is that bpf_kfunc_call_test_acquire() kfunc > is defined in bpf_testmod and the kfunc returns some data in bpf_testmod. > But the release function bpf_kfunc_call_test_release() is in the kernel. > The release func tries to access some data in bpf_testmod which might > have been unloaded. The prog_test_ref_kfunc is defined in the kernel, so > no bpf_testmod btf reference is hold so bpf_testmod can be unloaded before > bpf_kfunc_call_test_release(). > As you mentioned, we won't have this issue if bpf_kfunc_call_test_acquire() > is also in the kernel. > > I think putting bpf_kfunc_call_test_acquire() in bpf_testmod and > bpf_kfunc_call_test_release() in kernel is not a good idea and confusing. > But since this is only for tests, I guess we can live with that. With that, Correct. 65eb006d85a2a ("bpf: Move kernel test kfuncs to bpf_testmod") also mentions why bpf_kfunc_call_test_release() is not in the module and states that this is temporary. I'll add a comment in v2 so the wait can be removed once the functions are re-united. > Acked-by: Yonghong Song <yonghong.song@linux.dev> > > > --- > > tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 4 ++++ > > 1 file changed, 4 insertions(+) > > > > diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > > index 91907b321f913..63f0dbd016703 100644 > > --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > > +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > > @@ -2,6 +2,7 @@ > > /* Copyright (c) 2020 Facebook */ > > #include <linux/btf.h> > > #include <linux/btf_ids.h> > > +#include <linux/delay.h> > > #include <linux/error-injection.h> > > #include <linux/init.h> > > #include <linux/module.h> > > @@ -544,6 +545,9 @@ static int bpf_testmod_init(void) > > static void bpf_testmod_exit(void) > > { > > + while (refcount_read(&prog_test_struct.cnt) > 1) > > + msleep(20); > > + > > return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); > > } >
On Wed, Jan 10, 2024 at 09:14:51AM +0100, Artem Savkov wrote: > On Tue, Jan 09, 2024 at 11:40:38AM -0800, Yonghong Song wrote: > > > > On 1/9/24 8:43 AM, Artem Savkov wrote: > > > It is possible for bpf_kfunc_call_test_release() to be called from > > > bpf_map_free_deferred() when bpf_testmod is already unloaded and > > > perf_test_stuct.cnt which it tries to decrease is no longer in memory. > > > This patch tries to fix the issue by waiting for all references to be > > > dropped in bpf_testmod_exit(). > > > > > > The issue can be triggered by running 'test_progs -t map_kptr' in 6.5, > > > but is obscured in 6.6 by d119357d07435 ("rcu-tasks: Treat only > > > synchronous grace periods urgently"). > > > > > > Fixes: 65eb006d85a2a ("bpf: Move kernel test kfuncs to bpf_testmod") > > > > Please add your Signed-off-by tag. > > Thanks for noticing. Will resend with signed-off-by and your ack. > > > I think the root cause is that bpf_kfunc_call_test_acquire() kfunc > > is defined in bpf_testmod and the kfunc returns some data in bpf_testmod. > > But the release function bpf_kfunc_call_test_release() is in the kernel. > > The release func tries to access some data in bpf_testmod which might > > have been unloaded. The prog_test_ref_kfunc is defined in the kernel, so > > no bpf_testmod btf reference is hold so bpf_testmod can be unloaded before > > bpf_kfunc_call_test_release(). > > As you mentioned, we won't have this issue if bpf_kfunc_call_test_acquire() > > is also in the kernel. > > > > I think putting bpf_kfunc_call_test_acquire() in bpf_testmod and > > bpf_kfunc_call_test_release() in kernel is not a good idea and confusing. > > But since this is only for tests, I guess we can live with that. With that, > > Correct. 65eb006d85a2a ("bpf: Move kernel test kfuncs to bpf_testmod") > also mentions why bpf_kfunc_call_test_release() is not in the module and > states that this is temporary. I'll add a comment in v2 so the wait can > be removed once the functions are re-united. I somehow recall it has to do with the fact you can't have trusted pointer on module's object, so that's why those structs had to stay in kernel.. but I might be wrong jirka > > > Acked-by: Yonghong Song <yonghong.song@linux.dev> > > > > > --- > > > tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 4 ++++ > > > 1 file changed, 4 insertions(+) > > > > > > diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > > > index 91907b321f913..63f0dbd016703 100644 > > > --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > > > +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c > > > @@ -2,6 +2,7 @@ > > > /* Copyright (c) 2020 Facebook */ > > > #include <linux/btf.h> > > > #include <linux/btf_ids.h> > > > +#include <linux/delay.h> > > > #include <linux/error-injection.h> > > > #include <linux/init.h> > > > #include <linux/module.h> > > > @@ -544,6 +545,9 @@ static int bpf_testmod_init(void) > > > static void bpf_testmod_exit(void) > > > { > > > + while (refcount_read(&prog_test_struct.cnt) > 1) > > > + msleep(20); > > > + > > > return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); > > > } > > > > -- > Regards, > Artem >
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 91907b321f913..63f0dbd016703 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -2,6 +2,7 @@ /* Copyright (c) 2020 Facebook */ #include <linux/btf.h> #include <linux/btf_ids.h> +#include <linux/delay.h> #include <linux/error-injection.h> #include <linux/init.h> #include <linux/module.h> @@ -544,6 +545,9 @@ static int bpf_testmod_init(void) static void bpf_testmod_exit(void) { + while (refcount_read(&prog_test_struct.cnt) > 1) + msleep(20); + return sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file); }