From patchwork Fri May 10 00:29:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13660754 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18F5717995 for ; Fri, 10 May 2024 00:29:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300988; cv=none; b=Y65pGDM9XGx822RerUhZSUAnvMuuHPujzcV9z7f18+E+FRnoc1mvSUxtIIIpByqOLkue8LNX/icCIG3V1SuxHNKPgNvCnTtlLqYUY5vAN4nespcJ9ip3qTcS96BkiQc1lXMne5/RvP+xU4RqTRqU+tXdtNBGtWpZyhJX/QrOyus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300988; c=relaxed/simple; bh=VUGRX/70hVzxNPNnDhlV/a/lOJJQl40JorO2jRPSOag=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Oa8NFxSpk0O8uFeDAiwnFq0bjPKe+3kzefJ0tVKZVXsL5nBMqjk+9Qktt11CKImz/eI4pe4aGqjwCFjwkrOtDcwDlFck73HoZl0XZWY2bP5DuCQRRzVsGlmeEzJTOcD3ewpjsKvehid492XqkE1zFxgv2TS6lkyPaqRsAGbqsqI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Sx/DMwlk; arc=none smtp.client-ip=209.85.167.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Sx/DMwlk" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3c9539d1bbcso1210127b6e.1 for ; Thu, 09 May 2024 17:29:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715300986; x=1715905786; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=m0zZzdex50aU7PIEB2qKSmX/JBrGBruW4jEWlu/qrkg=; b=Sx/DMwlk+vwqeUf1L3MhxwHnN/3jqFlxp88aM/xuh1F9eyV4BdTCkCnTpA013YWZFl C8gxYtvofiAaRF/XJu2BCA5pKR5nf4/6YDx7rz0UrTy985WY1c9fGfNAahvXSatS7m4V 9f4EdxIOSk+kGwZMVNVRNuRD5PGKfRjkYTqLiNZWOfX82HUQnISKxPbXz+UJ2K6+y7lm 2pDABibJJCKjDIZUK1KYdq3yh4swNI4Z0b56iUJRD9Tvn7nv0WpneZROtSsylEqzCNFE e2lH2XYj+g8EfKLWAVGgf82Esu0JIQZ7m1yFigdJu9AS8qm4v/bH3TkwkXv+CcqXJi0b 5kpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715300986; x=1715905786; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m0zZzdex50aU7PIEB2qKSmX/JBrGBruW4jEWlu/qrkg=; b=WuGKt5FtNjP+ax0Euci4lXyQUhacUoFKK9wu5CtQTAixeM0Y5gUQc29Qqn/acxhKeF qu+WbyTdalPYM3quP/sDXZUbRhqHnSykqpXo6Y0u1lTSSoViPpFvd6P0VMeu1b++wUAp TZtKCT3kggylS1rK0CtWkj+bEkw3ovnvcPZIYabSMWYe9gEM5+rxhOpVGHmjYvXxiORV i/+0sm55S7smXgyii74ZMjBrWqeBANwUv2Vptmu1gezhKCCvSjB/645YdGxUfrI11Pp/ kpx6DdQMYvC1X/q+quho6h8g33vcpnGwlVf73WRGTRiF45rKmi6FtWtc0oquG5DSz51T qfXg== X-Gm-Message-State: AOJu0YzcNE6p89H0soFJXeA+0lXaGtYggqBLoPyvtpkLh3HvNgZ4XOd5 KkiNs1qcDIytVAS3KaHxU8bC8ZvWDz7ek0flk6eJxN4TQYzptZA0APa7kA== X-Google-Smtp-Source: AGHT+IHbzo9jJcCFh3M12oIeKGvWMmfqZND1uhU/MPOJhpB543gQa3UnhrwATONBmduKBvyy4dt95g== X-Received: by 2002:a05:6808:1820:b0:3c9:74e6:1f51 with SMTP id 5614622812f47-3c996d22738mr751325b6e.18.1715300985857; Thu, 09 May 2024 17:29:45 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3c98fc7e00bsm433251b6e.4.2024.05.09.17.29.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 17:29:45 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 1/7] bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops. Date: Thu, 9 May 2024 17:29:36 -0700 Message-Id: <20240510002942.1253354-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510002942.1253354-1-thinker.li@gmail.com> References: <20240510002942.1253354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Pass an additional pointer of bpf_struct_ops_link to callback function reg, unreg, and update provided by subsystems defined in bpf_struct_ops. A bpf_struct_ops_map can be registered for multiple links. Passing a pointer of bpf_struct_ops_link helps subsystems to distinguish them. This pointer will be used in the later patches to let the subsystem initiate a detachment on a link that was registered to it previously. Signed-off-by: Kui-Feng Lee --- include/linux/bpf.h | 6 +++--- kernel/bpf/bpf_struct_ops.c | 10 +++++----- net/bpf/bpf_dummy_struct_ops.c | 4 ++-- net/ipv4/bpf_tcp_ca.c | 6 +++--- .../selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c | 4 ++-- tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 6 +++--- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 90094400cc63..b600767ebe02 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1730,9 +1730,9 @@ struct bpf_struct_ops { int (*init_member)(const struct btf_type *t, const struct btf_member *member, void *kdata, const void *udata); - int (*reg)(void *kdata); - void (*unreg)(void *kdata); - int (*update)(void *kdata, void *old_kdata); + int (*reg)(void *kdata, struct bpf_link *link); + void (*unreg)(void *kdata, struct bpf_link *link); + int (*update)(void *kdata, void *old_kdata, struct bpf_link *link); int (*validate)(void *kdata); void *cfi_stubs; struct module *owner; diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 86c7884abaf8..1542dded7489 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -757,7 +757,7 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, goto unlock; } - err = st_ops->reg(kdata); + err = st_ops->reg(kdata, NULL); if (likely(!err)) { /* This refcnt increment on the map here after * 'st_ops->reg()' is secure since the state of the @@ -805,7 +805,7 @@ static long bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key) BPF_STRUCT_OPS_STATE_TOBEFREE); switch (prev_state) { case BPF_STRUCT_OPS_STATE_INUSE: - st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data); + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, NULL); bpf_map_put(map); return 0; case BPF_STRUCT_OPS_STATE_TOBEFREE: @@ -1060,7 +1060,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) /* st_link->map can be NULL if * bpf_struct_ops_link_create() fails to register. */ - st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data); + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link); bpf_map_put(&st_map->map); } kfree(st_link); @@ -1125,7 +1125,7 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map goto err_out; } - err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data); + err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data, link); if (err) goto err_out; @@ -1176,7 +1176,7 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (err) goto err_out; - err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data); + err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { bpf_link_cleanup(&link_primer); link = NULL; diff --git a/net/bpf/bpf_dummy_struct_ops.c b/net/bpf/bpf_dummy_struct_ops.c index 891cdf61c65a..3ea52b05adfb 100644 --- a/net/bpf/bpf_dummy_struct_ops.c +++ b/net/bpf/bpf_dummy_struct_ops.c @@ -272,12 +272,12 @@ static int bpf_dummy_init_member(const struct btf_type *t, return -EOPNOTSUPP; } -static int bpf_dummy_reg(void *kdata) +static int bpf_dummy_reg(void *kdata, struct bpf_link *link) { return -EOPNOTSUPP; } -static void bpf_dummy_unreg(void *kdata) +static void bpf_dummy_unreg(void *kdata, struct bpf_link *link) { } diff --git a/net/ipv4/bpf_tcp_ca.c b/net/ipv4/bpf_tcp_ca.c index 18227757ec0c..3f88d0961e5b 100644 --- a/net/ipv4/bpf_tcp_ca.c +++ b/net/ipv4/bpf_tcp_ca.c @@ -260,17 +260,17 @@ static int bpf_tcp_ca_check_member(const struct btf_type *t, return 0; } -static int bpf_tcp_ca_reg(void *kdata) +static int bpf_tcp_ca_reg(void *kdata, struct bpf_link *link) { return tcp_register_congestion_control(kdata); } -static void bpf_tcp_ca_unreg(void *kdata) +static void bpf_tcp_ca_unreg(void *kdata, struct bpf_link *link) { tcp_unregister_congestion_control(kdata); } -static int bpf_tcp_ca_update(void *kdata, void *old_kdata) +static int bpf_tcp_ca_update(void *kdata, void *old_kdata, struct bpf_link *link) { return tcp_update_congestion_control(kdata, old_kdata); } diff --git a/tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c b/tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c index b1dd889d5d7d..948eb3962732 100644 --- a/tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c +++ b/tools/testing/selftests/bpf/bpf_test_no_cfi/bpf_test_no_cfi.c @@ -22,12 +22,12 @@ static int dummy_init_member(const struct btf_type *t, return 0; } -static int dummy_reg(void *kdata) +static int dummy_reg(void *kdata, struct bpf_link *link) { return 0; } -static void dummy_unreg(void *kdata) +static void dummy_unreg(void *kdata, struct bpf_link *link) { } diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index e93013fc7bf4..1150e758e630 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -817,7 +817,7 @@ static const struct bpf_verifier_ops bpf_testmod_verifier_ops = { .is_valid_access = bpf_testmod_ops_is_valid_access, }; -static int bpf_dummy_reg(void *kdata) +static int bpf_dummy_reg(void *kdata, struct bpf_link *link) { struct bpf_testmod_ops *ops = kdata; @@ -832,7 +832,7 @@ static int bpf_dummy_reg(void *kdata) return 0; } -static void bpf_dummy_unreg(void *kdata) +static void bpf_dummy_unreg(void *kdata, struct bpf_link *link) { } @@ -868,7 +868,7 @@ struct bpf_struct_ops bpf_bpf_testmod_ops = { .owner = THIS_MODULE, }; -static int bpf_dummy_reg2(void *kdata) +static int bpf_dummy_reg2(void *kdata, struct bpf_link *link) { struct bpf_testmod_ops2 *ops = kdata; From patchwork Fri May 10 00:29:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13660756 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F3E24C7D for ; Fri, 10 May 2024 00:29:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300989; cv=none; b=YSLCWzDeB72fGHscex0YC1VzYEaHwdX4cfVBqblxV++ROyWper9mbO3CrHUbDPF12Oimc636VeRiEYL1zFL1QzCgViDs4xhKdfET0SNJ/r2l6d+PZHEaH3CLZfw5/N3f3C4vv95ySFSMdrtFK6kejg2OxQ3pyoA/V8bidJNsq/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300989; c=relaxed/simple; bh=HWs/ISbPlESsrkP0SvzIrIGbtO3Zf2ve0IcdgaB1iFI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oy/6vcbqjQr2uWgVkynu9LY2GhO70/x+U4UoMm+gCv/g8BF/vDv8mNi3lmdtvIpU30FYTG802sogmUiPNzvtPrXY4F7XvWVrX1WimCexG/g78bOFWdomYyWuCb1UhzyQd3mL60fFoReB/yB4ASh6yU737Ugk3nakdYyFGfI45xw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dX8I5dyg; arc=none smtp.client-ip=209.85.167.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dX8I5dyg" Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3c9996178faso24979b6e.2 for ; Thu, 09 May 2024 17:29:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715300987; x=1715905787; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XwzSOLipthCVO++sk5fkDnUQ7UJc91lhr4BYqbA2DO8=; b=dX8I5dygrlGPvGVDbobco2g+iMolaE93el/T8FA1DUvZco1bl3XVf3/dqbmCGtR4hC A7ev69J+lLbf+Z8VGMN2s/+g2Ad2cAvODtQQjhMOmlhrorBwv7PIwEdncXnTG6+Kz5WY mZoUCmcZGveg1UAJRBJBNYUt2JAxgGri9fQvYnrv8icGZEggdvfTI9hqlAbY4SNz5+mO jA/FVdNc95P0cKep6JPZVxLbk2vnFt7tpM+QnI0VKtM/QEFJP3bgCCJ4TpGVeng1GC5T EtaggCmivYG6yT+5w0g1ad2FxNNcvH6xMYZXIGBH1AbbuciKeFbg5cgkhIED6YsGgEt/ 4EhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715300987; x=1715905787; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XwzSOLipthCVO++sk5fkDnUQ7UJc91lhr4BYqbA2DO8=; b=gm9LaAMsahJBur3JMkl2QNPONyVxtIqLD8CymF8NsxtIYTCcTXM4Cz3/Dp7PLN43sP 5TfiiZRVGIwUkhhJ/PFaVplOktaZ5gopfDemDdIAT/NQpRjuEMxbYl/zyVM1BnzW+8J4 Cj45Dij7xMdcSliABy3HX/aMp3xDpmgAKZoJpkk18jtQgMVnojLP9nWWot6rhLBIS2ti 5BJ7DjoltpKk+18rFz08rSUBmFeSeYSxLYhzjN1y12RtlFPbYsVrhkdoH5Ympiqb3rjM edLlVEjNBMmhc/9StVcYATqiIHYgZjatFmbF/kytaxVBSoRNpNZ7y0w4mQ9RtGuvL5dM Ctag== X-Gm-Message-State: AOJu0YzLBNubliHtKzrCg2DFo+zJn5GavDGhtkvVMts/CWYPaW3pnXLY 8D1LSNMstUUfRmUV7dpyn2wX+79CSOFbzbNOglHA4rTdAol6/5l/PmeGdA== X-Google-Smtp-Source: AGHT+IFPxhnPXD6rO8f1yJseYyMwB+SugNC3zyO2yN9Fwbx0O6lUqTZOGmI+fPSNK0EREWO2hU6gHg== X-Received: by 2002:a54:4415:0:b0:3c8:430c:ab4f with SMTP id 5614622812f47-3c9971dd2famr1358826b6e.55.1715300987064; Thu, 09 May 2024 17:29:47 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3c98fc7e00bsm433251b6e.4.2024.05.09.17.29.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 17:29:46 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 2/7] bpf: enable detaching links of struct_ops objects. Date: Thu, 9 May 2024 17:29:37 -0700 Message-Id: <20240510002942.1253354-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510002942.1253354-1-thinker.li@gmail.com> References: <20240510002942.1253354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Implement the detach callback in bpf_link_ops for struct_ops so that user programs can detach a struct_ops link. The subsystems that struct_ops objects are registered to can also use this callback to detach the links being passed to them. Signed-off-by: Kui-Feng Lee --- kernel/bpf/bpf_struct_ops.c | 50 ++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 1542dded7489..7e270ee15f6e 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -1057,9 +1057,6 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link) st_map = (struct bpf_struct_ops_map *) rcu_dereference_protected(st_link->map, true); if (st_map) { - /* st_link->map can be NULL if - * bpf_struct_ops_link_create() fails to register. - */ st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link); bpf_map_put(&st_map->map); } @@ -1075,7 +1072,8 @@ static void bpf_struct_ops_map_link_show_fdinfo(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - seq_printf(seq, "map_id:\t%d\n", map->id); + if (map) + seq_printf(seq, "map_id:\t%d\n", map->id); rcu_read_unlock(); } @@ -1088,7 +1086,8 @@ static int bpf_struct_ops_map_link_fill_link_info(const struct bpf_link *link, st_link = container_of(link, struct bpf_struct_ops_link, link); rcu_read_lock(); map = rcu_dereference(st_link->map); - info->struct_ops.map_id = map->id; + if (map) + info->struct_ops.map_id = map->id; rcu_read_unlock(); return 0; } @@ -1113,6 +1112,10 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map mutex_lock(&update_mutex); old_map = rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex)); + if (!old_map) { + err = -EINVAL; + goto err_out; + } if (expected_old_map && old_map != expected_old_map) { err = -EPERM; goto err_out; @@ -1139,8 +1142,37 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map return err; } +static int bpf_struct_ops_map_link_detach(struct bpf_link *link) +{ + struct bpf_struct_ops_link *st_link = container_of(link, struct bpf_struct_ops_link, link); + struct bpf_struct_ops_map *st_map; + struct bpf_map *map; + + mutex_lock(&update_mutex); + + map = rcu_dereference_protected(st_link->map, lockdep_is_held(&update_mutex)); + if (!map) { + mutex_unlock(&update_mutex); + return -EINVAL; + } + st_map = container_of(map, struct bpf_struct_ops_map, map); + + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link); + + rcu_assign_pointer(st_link->map, NULL); + /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or + * bpf_map_inc() in bpf_struct_ops_map_link_update(). + */ + bpf_map_put(&st_map->map); + + mutex_unlock(&update_mutex); + + return 0; +} + static const struct bpf_link_ops bpf_struct_ops_map_lops = { .dealloc = bpf_struct_ops_map_link_dealloc, + .detach = bpf_struct_ops_map_link_detach, .show_fdinfo = bpf_struct_ops_map_link_show_fdinfo, .fill_link_info = bpf_struct_ops_map_link_fill_link_info, .update_map = bpf_struct_ops_map_link_update, @@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) if (err) goto err_out; + /* Init link->map before calling reg() in case being detached + * immediately. + */ + RCU_INIT_POINTER(link->map, map); + err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { + RCU_INIT_POINTER(link->map, NULL); bpf_link_cleanup(&link_primer); + /* The link has been free by bpf_link_cleanup() */ link = NULL; goto err_out; } - RCU_INIT_POINTER(link->map, map); return bpf_link_settle(&link_primer); From patchwork Fri May 10 00:29:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13660757 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C33C45337D for ; Fri, 10 May 2024 00:29:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300991; cv=none; b=d3uUksym2R+1Js5hOSa/rTBcNJ9rIu7f1nwBHtCGRTvJQG5XvjlxTFiw5FqtwJAC4mQX60sH9gCEWymdG+PFwBZmcESpd7cTkobAayGZLnD3DRAgbRLUmecewaYvGbblxpoWgu9T1XRQjmOvAF+S4Bpfbs0TKk7EBvjVmwtbYxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300991; c=relaxed/simple; bh=eUPExguRfTNimcD1n3nCj8mT4uUePCZMQR5qu84kVLI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Yu/SUzZmit9c30EbnOzdvGkr7Cf7Ens8M3uhzDe6aztJTv5whN1D2GIhL0xRTbTxnCGAVWIYUe5P1tY+L6MEeXF7apHPM/D2aHWNv8MCjUGsWGJcg7r7fgdQMt9DneW2GpFGEYo4LJUnAvlHAExnfQQ8ygGz4KNkNCryjlGeHYg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PevVrVI/; arc=none smtp.client-ip=209.85.167.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PevVrVI/" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3c969fa8fd2so887084b6e.1 for ; Thu, 09 May 2024 17:29:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715300988; x=1715905788; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=t6yTtVfiSjzl44U+LGTO04JMGg1y5t8TP/wNxJEy6iE=; b=PevVrVI/6BLoqXySCs5gn+muFS3r1VtqohCgZ3PK5vzGrLGR2JJDHkJayeTelOy/Tf EdrX4ub0RUehLnPbO/v6Mik1PPOG6wQ5KVa/uFoO6KYQvT/7yQTBRJyg8FLZHNfG8e5E Us7FmK+lIXKud5KHYCHXU1slGTKp00DX/atj78vIeZWevwbGxjfI2AtgbitaYxSw1HAq NYpG3zVzrvUsyKUosRNcOBOCMZOjBJLMcoYnhE09TSdrlSD81xchvQplNg/m4WdmGOxa dQTiwr2fKo32VJpDF8wVE78/RaZ86E8kM/Tx/nvaJkqSC1Qsd3weLF5TQjudML1UUNsh 162A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715300988; x=1715905788; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t6yTtVfiSjzl44U+LGTO04JMGg1y5t8TP/wNxJEy6iE=; b=a7iDsTNYAvq0fXkjTz7ws+FND7Kk04LepKz+n+KFElgE3LiKhUyajsecDn3arpJKLG +ac/89Ih90XUqirhVPZyQZeu+ouSn3z4ZFhF0uTgrrcxQz5l9/2NJO9fWZnycyD8Ba6N GOpqPm6exzJgosKDr0XKMBmqM/4oItO6MjQFR293+7hK/8thMa/CkkGxTpCLSB3LWkML v4a3umtB1L9VlZE0NVir1sG41mTPvzRiA1UsT+xS1n98rqhM36O6JYUmDdYUk4qKLisQ glhRicy9HmZ0AZY/YsbLVeoo4KMgNr7wY335euGv7yEp2cJHAadmsKOkCiR/KJDrICtR Depg== X-Gm-Message-State: AOJu0YwE5qhKLbVSVhVJe0MLlmuhRkGeBvayFMCq7RPVe8hQPryXWMf3 GIz4G839EUdVaaRnKoztbkiCfgf/UCbPUtYJ18+mLiQXlYUrlyMt2q9MMg== X-Google-Smtp-Source: AGHT+IEg8+LNggcXyifTFz23buTnhhFp/jAFS4//GENzSDQBlbXZV4Ev7ppI5XfKS0vMavPlORIfVQ== X-Received: by 2002:a05:6808:487:b0:3c9:6a55:5f1a with SMTP id 5614622812f47-3c9971dcf6cmr1319794b6e.49.1715300988278; Thu, 09 May 2024 17:29:48 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3c98fc7e00bsm433251b6e.4.2024.05.09.17.29.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 17:29:47 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 3/7] bpf: support epoll from bpf struct_ops links. Date: Thu, 9 May 2024 17:29:38 -0700 Message-Id: <20240510002942.1253354-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510002942.1253354-1-thinker.li@gmail.com> References: <20240510002942.1253354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add epoll support to bpf struct_ops links to trigger EPOLLHUP event upon detachment. This patch implements the "poll" of the "struct file_operations" for BPF links and introduces a new "poll" operator in the "struct bpf_link_ops". By implementing "poll" of "struct bpf_link_ops" for the links of struct_ops, the file descriptor of a struct_ops link can be added to an epoll file descriptor to receive EPOLLHUP events. Signed-off-by: Kui-Feng Lee --- include/linux/bpf.h | 1 + kernel/bpf/bpf_struct_ops.c | 17 +++++++++++++++++ kernel/bpf/syscall.c | 11 +++++++++++ 3 files changed, 29 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index b600767ebe02..5f7496ef8b7c 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1612,6 +1612,7 @@ struct bpf_link_ops { struct bpf_link_info *info); int (*update_map)(struct bpf_link *link, struct bpf_map *new_map, struct bpf_map *old_map); + __poll_t (*poll)(struct file *file, struct poll_table_struct *pts); }; struct bpf_tramp_link { diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 7e270ee15f6e..497526bd5fec 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -12,6 +12,7 @@ #include #include #include +#include struct bpf_struct_ops_value { struct bpf_struct_ops_common_value common; @@ -56,6 +57,7 @@ struct bpf_struct_ops_map { struct bpf_struct_ops_link { struct bpf_link link; struct bpf_map __rcu *map; + wait_queue_head_t wait_hup; }; static DEFINE_MUTEX(update_mutex); @@ -1167,15 +1169,28 @@ static int bpf_struct_ops_map_link_detach(struct bpf_link *link) mutex_unlock(&update_mutex); + wake_up_interruptible_poll(&st_link->wait_hup, EPOLLHUP); + return 0; } +static __poll_t bpf_struct_ops_map_link_poll(struct file *file, + struct poll_table_struct *pts) +{ + struct bpf_struct_ops_link *st_link = file->private_data; + + poll_wait(file, &st_link->wait_hup, pts); + + return (st_link->map) ? 0 : EPOLLHUP; +} + static const struct bpf_link_ops bpf_struct_ops_map_lops = { .dealloc = bpf_struct_ops_map_link_dealloc, .detach = bpf_struct_ops_map_link_detach, .show_fdinfo = bpf_struct_ops_map_link_show_fdinfo, .fill_link_info = bpf_struct_ops_map_link_fill_link_info, .update_map = bpf_struct_ops_map_link_update, + .poll = bpf_struct_ops_map_link_poll, }; int bpf_struct_ops_link_create(union bpf_attr *attr) @@ -1213,6 +1228,8 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) */ RCU_INIT_POINTER(link->map, map); + init_waitqueue_head(&link->wait_hup); + err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { RCU_INIT_POINTER(link->map, NULL); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 13ad74ecf2cd..ad4f81ed27f0 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3150,6 +3150,16 @@ static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp) } #endif +static __poll_t bpf_link_poll(struct file *file, struct poll_table_struct *pts) +{ + struct bpf_link *link = file->private_data; + + if (link->ops->poll) + return link->ops->poll(file, pts); + + return 0; +} + static const struct file_operations bpf_link_fops = { #ifdef CONFIG_PROC_FS .show_fdinfo = bpf_link_show_fdinfo, @@ -3157,6 +3167,7 @@ static const struct file_operations bpf_link_fops = { .release = bpf_link_release, .read = bpf_dummy_read, .write = bpf_dummy_write, + .poll = bpf_link_poll, }; static int bpf_link_alloc_id(struct bpf_link *link) From patchwork Fri May 10 00:29:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13660758 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F1C1535BA for ; Fri, 10 May 2024 00:29:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300992; cv=none; b=jTLzJfReyVKfw0p0qCsJzMeZQ8UCjGQ0vNAcK5E0WI5eDlm/hUBTYz7CDQlXP8JOAr8J2zIuJD00nJ2f0Vy6ygbTXEnC8wIQWK8ny5uraYtWXv2l3wdlnXjbTtl9c1FwLcA7W5kbVAD2iLKNkeu5kZqz8LsVsAuHVvht1IlZuyg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300992; c=relaxed/simple; bh=34VL9O4NDfy7Ojt5kCpX/zgfJ2l99XUzUsH5/dLKqgI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NvyBX4rpT6TTEmf69KBdaddVwK1azsCA1iuhKkf8We2iTEY3/rI/E8GRjO0lDss9uOM6cDS6EmDOIJYIgGlKvi+SREaIz2fbUpg2yE6ZZ2pzbZ11MWFGBCAjxe9tFglaSKWW56CDVyl6un+nyv8ljY+9ssx5hjMikP/36j9ZiVQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WOd2H/ZB; arc=none smtp.client-ip=209.85.167.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WOd2H/ZB" Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3c9881d07fdso890579b6e.3 for ; Thu, 09 May 2024 17:29:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715300989; x=1715905789; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QXM2QFy+xceW32g6/wIpbUNL9uI/2VITc2w+pZ/sTY8=; b=WOd2H/ZBF2oFyReHyEfbla8AWgAbLdbdOVfcb7HLCm1Yf6f0wRX6NNNs7+W12yOA0V fvAtfju7C2hSXBfYkf4fejwHZ4aFHPpUqcZTtTLXnpaQ7gh9dJKAE90Uxk/v5xY6GEXp XJpfkCwxfRckVufJ/2eVA/VkKx4maSANrcutbdOXVUQa7cMDWnHgTfqyjcduuMwbBLKO INYi+jlLMOyQxL3L38AXWmymyfQ3LdZ4ANLF90SskmO5FbplqajuYXP7bUQE9efCg9Ws /YYEIQIG3qtrKuXOrMsehJ3kNihNsAWJHKZClRL0C0zU6zOw/FWkrTHJPm20yMddm817 p6Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715300989; x=1715905789; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QXM2QFy+xceW32g6/wIpbUNL9uI/2VITc2w+pZ/sTY8=; b=J64t8Y3OyY6j+5CXCmdiDs828CxXFCG1LtiWcptVn/1xRvrKc1HgdJKXc/G3Kn6nMX A5j9Vr2ddYncwY8bSFZbgCEym1xucLkQgev1PUHUn4/zymLDt/+VVDFXYked/uIvDMll pnqK0wn/2DEiQqXXgD6EFGVQl8AfJ3RsRc+BMbruNx7/9gAviEtStt1imNGXcy0pQJra GQBIfE46wNbsCmVAiPtapVtE5V/Fru4QjqZsYgSACFh9nY2urwkTHaMaafmVsEANiM87 BxtBk6/Mv5D3EaG+r4Xul0rMK1s3wLBH+7FaIn5k6Uh8huzztbjBlY5ZrMLOnrX3Y3Js h67A== X-Gm-Message-State: AOJu0YwvB2HGv5O3iGsiSMnh50MuyvszbJtWS16l6lAlkYhA+J25fAzT 9bcqaGmLhUhpnMJzdwS2PPpfSSIc57p+cnSSDT61WeqUkTGeQNA7j2wixQ== X-Google-Smtp-Source: AGHT+IEVEameRzuB1MzpXOdi2QGSKTBXT0dcM3QHHfNRee2xereXzE7WoVuuN7Q+yuegUZ27yI5qMg== X-Received: by 2002:a05:6808:4185:b0:3c9:6e10:ab35 with SMTP id 5614622812f47-3c9971f9993mr1122949b6e.58.1715300989540; Thu, 09 May 2024 17:29:49 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3c98fc7e00bsm433251b6e.4.2024.05.09.17.29.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 17:29:49 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 4/7] bpf: export bpf_link_inc_not_zero. Date: Thu, 9 May 2024 17:29:39 -0700 Message-Id: <20240510002942.1253354-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510002942.1253354-1-thinker.li@gmail.com> References: <20240510002942.1253354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net bpf_link_inc_not_zero() will be used by kernel modules. We will use it in bpf_testmod.c later. Signed-off-by: Kui-Feng Lee --- include/linux/bpf.h | 6 ++++++ kernel/bpf/syscall.c | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 5f7496ef8b7c..6b592094f9b4 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2351,6 +2351,7 @@ int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer); int bpf_link_settle(struct bpf_link_primer *primer); void bpf_link_cleanup(struct bpf_link_primer *primer); void bpf_link_inc(struct bpf_link *link); +struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link); void bpf_link_put(struct bpf_link *link); int bpf_link_new_fd(struct bpf_link *link); struct bpf_link *bpf_link_get_from_fd(u32 ufd); @@ -2722,6 +2723,11 @@ static inline void bpf_link_inc(struct bpf_link *link) { } +static inline struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) +{ + return NULL; +} + static inline void bpf_link_put(struct bpf_link *link) { } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index ad4f81ed27f0..31fabe26371d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5422,10 +5422,11 @@ static int link_detach(union bpf_attr *attr) return ret; } -static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) +struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link) { return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT); } +EXPORT_SYMBOL(bpf_link_inc_not_zero); struct bpf_link *bpf_link_by_id(u32 id) { From patchwork Fri May 10 00:29:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13660759 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1EF2537F8 for ; Fri, 10 May 2024 00:29:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300993; cv=none; b=KQ8E/ZJVlByI4puuVqLj6Hl+8y/qox00H38kvRO76ht45vPHiBnBCDPo0McER09wIQgjPZtwyX/bnYNdPCxmiJyO0g66/QVYFiLxshIzZzY0w0rVdknl1u9yhpYsXTQJbEiHFTSo7488xIo70uzYtQYXDid+dvMElnXELCXB4i8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300993; c=relaxed/simple; bh=0moVWm5H0/PxkymYi1Wkc4v/Yc9p3AxXBq0vFZgiFh0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hGPfX55N01K7s4xqmHJBwtkZIsq/3CmqTXxoQdajE5HQc+Ha8GUJRx2FwrKHodmAn8vli/MZUrA+mnz+q++enOqHBBcYJh4xK8aS5a3oI2H20o0MzxjjsV/vGhVo35Eo0nrR/xV86hI27O4xylORl/1JYJ3WwDqgrwtFh5YIjnw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DZtpqP+D; arc=none smtp.client-ip=209.85.167.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DZtpqP+D" Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3c9996178faso24988b6e.2 for ; Thu, 09 May 2024 17:29:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715300990; x=1715905790; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WzaF0DPOk0k2y1diONsO/hU9U1jewZi8cQG8L4Jq9yg=; b=DZtpqP+DuBUXZDElkp7gpbTpTgK0FrkGookETQ3BKGSHFCWFULXF5SKi+ToLS/ZlMa FMcbP2Ngwt09XhgMFbBO0ngATe6kDigDmy4puoM9qkx+AVvClANfV7oEVhIsL/nLDOHs dz4ZgtYDljT2AjE1br42VALUIbPIaWesJSdboUinb6UoNECUgL+r1pxAmCu72go+Ej9x yY9GYwuCeni47YkiJDa6yTB0lZOteVhh3vRl9gnWqqfIphR5NWW5Dhq7Udo9tCYsgjH6 oIq6qAdx4yUzEfzlpMhEy0d2kuovpo4BLFQpTdiMl6/L6JbRLKhAl5rii9BiUynclm6b tqKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715300990; x=1715905790; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WzaF0DPOk0k2y1diONsO/hU9U1jewZi8cQG8L4Jq9yg=; b=YMTUQflaJGIWfZVQ6GIIuXwLjFt86mp6CpcqS4GzD0MrNT6IEjpx8voQwMMX5xlHtK b7Vvjc/OU1Way4kYvaeCJJ65lceOkHqiBmo/Uhp/pNUIK18JxWAmywIBur/H6P+uzsWd kTlIlOslXUTQY1ibw0mdLyiIU3yK+DucWvl5Rr6mPEW7zmtIcEhTUwBgjoVnuP8GgTSP o+ilrzL0TwjrapLuel4mk7A9PIssSoWhw3j8fhbWmG+U7kY+kPq7Kscgf7Yy6h7l/xaz KwbNPlKWhltL1HTvaVRKe+8HmIr3tajGXO2Lv/liMeFFqQmJotwiNh4GZWY2EJMiXXAw KufA== X-Gm-Message-State: AOJu0Yw+PUhLySB07icfw7CdFVQgHEvWd00xshLnorGNHFltylWK+sWC MV74i/YjhRU6yl7L7PtPsKLWUEPepK4cIZaQ0LxqmIWCdeUECvwotlFeOA== X-Google-Smtp-Source: AGHT+IFOiDFx5bZ2ExrirjLtVVnEzaaI4xc/wqXfEtv3Ji+5mEXbqZN1gBRbOYYEOmIGhplBTo2QYQ== X-Received: by 2002:a05:6808:5d4:b0:3c7:4fd4:ae76 with SMTP id 5614622812f47-3c9970367d4mr1378033b6e.10.1715300990595; Thu, 09 May 2024 17:29:50 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3c98fc7e00bsm433251b6e.4.2024.05.09.17.29.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 17:29:50 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 5/7] selftests/bpf: test struct_ops with epoll Date: Thu, 9 May 2024 17:29:40 -0700 Message-Id: <20240510002942.1253354-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510002942.1253354-1-thinker.li@gmail.com> References: <20240510002942.1253354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Verify whether a user space program is informed through epoll with EPOLLHUP when a struct_ops object is detached. The BPF code in selftests/bpf/progs/struct_ops_module.c has become complex. Therefore, struct_ops_detach.c has been added to segregate the BPF code for detachment tests from the BPF code for other tests based on the recommendation of Andrii Nakryiko. Suggested-by: Andrii Nakryiko Signed-off-by: Kui-Feng Lee --- .../bpf/prog_tests/test_struct_ops_module.c | 57 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 9 +++ 2 files changed, 66 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_detach.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index 29e183a80f49..bbcf12696a6b 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -3,9 +3,12 @@ #include #include +#include + #include "struct_ops_module.skel.h" #include "struct_ops_nulled_out_cb.skel.h" #include "struct_ops_forgotten_cb.skel.h" +#include "struct_ops_detach.skel.h" static void check_map_info(struct bpf_map_info *info) { @@ -242,6 +245,58 @@ static void test_struct_ops_forgotten_cb(void) struct_ops_forgotten_cb__destroy(skel); } +/* Detach a link from a user space program */ +static void test_detach_link(void) +{ + struct epoll_event ev, events[2]; + struct struct_ops_detach *skel; + struct bpf_link *link = NULL; + int fd, epollfd = -1, nfds; + int err; + + skel = struct_ops_detach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_detach__open_and_load")) + return; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_do_detach); + if (!ASSERT_OK_PTR(link, "attach_struct_ops")) + goto cleanup; + + fd = bpf_link__fd(link); + if (!ASSERT_GE(fd, 0, "link_fd")) + goto cleanup; + + epollfd = epoll_create1(0); + if (!ASSERT_GE(epollfd, 0, "epoll_create1")) + goto cleanup; + + ev.events = EPOLLHUP; + ev.data.fd = fd; + err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); + if (!ASSERT_OK(err, "epoll_ctl")) + goto cleanup; + + err = bpf_link__detach(link); + if (!ASSERT_OK(err, "detach_link")) + goto cleanup; + + /* Wait for EPOLLHUP */ + nfds = epoll_wait(epollfd, events, 2, 500); + if (!ASSERT_EQ(nfds, 1, "epoll_wait")) + goto cleanup; + + if (!ASSERT_EQ(events[0].data.fd, fd, "epoll_wait_fd")) + goto cleanup; + if (!ASSERT_TRUE(events[0].events & EPOLLHUP, "events[0].events")) + goto cleanup; + +cleanup: + if (epollfd >= 0) + close(epollfd); + bpf_link__destroy(link); + struct_ops_detach__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("struct_ops_load")) @@ -254,5 +309,7 @@ void serial_test_struct_ops_module(void) test_struct_ops_nulled_out_cb(); if (test__start_subtest("struct_ops_forgotten_cb")) test_struct_ops_forgotten_cb(); + if (test__start_subtest("test_detach_link")) + test_detach_link(); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_detach.c b/tools/testing/selftests/bpf/progs/struct_ops_detach.c new file mode 100644 index 000000000000..45eacc2ca657 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_detach.c @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include +#include "../bpf_testmod/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_do_detach; From patchwork Fri May 10 00:29:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13660760 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9EFB53805 for ; Fri, 10 May 2024 00:29:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300994; cv=none; b=NbgCXsYOPmrjW52R1VT+jIV01BeeW7yml50WvTkolGzYZu59Nf/918Au/4FTtGVORw8VICFOGUChmXJPzaySscragRXbQX+G/nmCMYma2g+9jSFed7dALomDNSCfoUPDpiWMDvVFI7WBKo31SNgj/7u+GlvfwC3RH1gULX3hGCc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300994; c=relaxed/simple; bh=Um/5Z3S2V4ItfawPhH8I2yTl6PdtJKjDEGuFOyw6k4c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iNYyX5qEJYRq/Oy2+1KcFqGFc3GNPOvcrhreIJyIryLgT1N/K370d2pY1DQ3TCo0KbEdDm8Gtp7GnqbqIa00uz4nL68uHFlhId9+KJ8QwUEG8OzosnCUVCcneN/AN35FqHTO8l+MfpV1TAXmfPYKo64OVMd4I/I/ojAzrURWxhg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=IloH7RUr; arc=none smtp.client-ip=209.85.210.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IloH7RUr" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-6f0e7af802eso389411a34.0 for ; Thu, 09 May 2024 17:29:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715300991; x=1715905791; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NE/wI6J9vuIzbDTRrQtZ7FmhEo6YOrwUK11Y5T7bAG0=; b=IloH7RUr/nZZx06TNRDXzJUYTMz6iwGRnhBuNrm3tXsdQfg1dq9WB7y9Zn2LCZxVdl 4JZ7Q5Ticw46CAoiT11nHCxa6vQm/5DZxiZ5RXy9T5nvBEsjg2S/Uy+m/6gtumSxrWg/ sZBW4eMkS1xGsfBbylvcW3c1cB3an6GoTWyA2u07buVq1Poaz7IvUbYc8TACgxI+SISy p61DVFzUeb/JHSIKKndU5KVJtIPinUZD5AMvjuW4+bRZCZHCAVRDtdtVcauniRP708zm pCXEd6deUx/lJ6V5/WOtMOU7P+8JtaBWJOiNpdQJ+WhWSDJwPKnTkvBp8CWEXylrCWnG DNPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715300991; x=1715905791; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NE/wI6J9vuIzbDTRrQtZ7FmhEo6YOrwUK11Y5T7bAG0=; b=Pub/CXYOh535pM5S0XCkX3GaRZ0DiQIX+ZX5DRllvGcCMirKh2mTSUZlDnbuAUtTKI /k4WgOLR89bP+O0tcZ6WOGWs7sfggQNcN1equsg+h7QWllQuL60ZTXR/OG7EvLO0WoEX ji885iosFYEStNJs1xi4cUne7vaPWCNLC5hGZJPBEQxlL18TgzBY6pwDASsYzxvCx1ax W++qvxhcz5tD2fzLoi597yXrBfZtJvlV7+QFx7LKcorIgsdGNIfwka7HaF+3rWxObY+3 aBtvTFGkooagtghOVPn2LIpBauzHZpi4lNyqllDHT7xSRWMRO0LUQjd7KOezq5hKm1Gv 5cog== X-Gm-Message-State: AOJu0YxYKbwbNYEPwM+sdEFvf2PPsWGfMpajGT62q8EXw3x9Fd7Dell/ pPQP4fF4cp/Ya4WRhFqFmY0JdG58SKKOfXN2bTgB7gOEdl0Z0Jsnhq3igA== X-Google-Smtp-Source: AGHT+IHVo2ypCTi0l+iAGKqQ5UOKmwyO8BJTe2jDXiipjn5lrExG0VgjXbAjmPyXrp8XXuwffRdEPg== X-Received: by 2002:a05:6808:17aa:b0:3c9:6bf6:f2b6 with SMTP id 5614622812f47-3c9970b961emr1488547b6e.37.1715300991582; Thu, 09 May 2024 17:29:51 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3c98fc7e00bsm433251b6e.4.2024.05.09.17.29.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 17:29:51 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 6/7] selftests/bpf: detach a struct_ops link from the subsystem managing it. Date: Thu, 9 May 2024 17:29:41 -0700 Message-Id: <20240510002942.1253354-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510002942.1253354-1-thinker.li@gmail.com> References: <20240510002942.1253354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Not only a user space program can detach a struct_ops link, the subsystem managing a link can also detach the link. This patch adds a kfunc to simulate detaching a link by the subsystem managing it and makes sure user space programs get notified through epoll. Signed-off-by: Kui-Feng Lee --- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 42 ++++++++++++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + .../bpf/prog_tests/test_struct_ops_module.c | 67 +++++++++++++++++++ .../selftests/bpf/progs/struct_ops_detach.c | 7 ++ 4 files changed, 117 insertions(+) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c index 1150e758e630..1f347eed6c18 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -741,6 +741,38 @@ __bpf_kfunc int bpf_kfunc_call_kernel_getpeername(struct addr_args *args) return err; } +static DEFINE_SPINLOCK(detach_lock); +static struct bpf_link *link_to_detach; + +__bpf_kfunc int bpf_dummy_do_link_detach(void) +{ + struct bpf_link *link; + int ret = -ENOENT; + + /* A subsystem must ensure that a link is valid when detaching the + * link. In order to achieve that, the subsystem may need to obtain + * a lock to safeguard a table that holds the pointer to the link + * being detached. However, the subsystem cannot invoke + * link->ops->detach() while holding the lock because other tasks + * may be in the process of unregistering, which could lead to + * acquiring the same lock and causing a deadlock. This is why + * bpf_link_inc_not_zero() is used to maintain the link's validity. + */ + spin_lock(&detach_lock); + link = link_to_detach; + /* Make sure the link is still valid by increasing its refcnt */ + if (link && IS_ERR(bpf_link_inc_not_zero(link))) + link = NULL; + spin_unlock(&detach_lock); + + if (link) { + ret = link->ops->detach(link); + bpf_link_put(link); + } + + return ret; +} + BTF_KFUNCS_START(bpf_testmod_check_kfunc_ids) BTF_ID_FLAGS(func, bpf_testmod_test_mod_kfunc) BTF_ID_FLAGS(func, bpf_kfunc_call_test1) @@ -777,6 +809,7 @@ BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_sendmsg, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_kfunc_call_sock_sendmsg, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getsockname, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_kfunc_call_kernel_getpeername, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_dummy_do_link_detach) BTF_KFUNCS_END(bpf_testmod_check_kfunc_ids) static int bpf_testmod_ops_init(struct btf *btf) @@ -829,11 +862,20 @@ static int bpf_dummy_reg(void *kdata, struct bpf_link *link) if (ops->test_2) ops->test_2(4, ops->data); + spin_lock(&detach_lock); + if (!link_to_detach) + link_to_detach = link; + spin_unlock(&detach_lock); + return 0; } static void bpf_dummy_unreg(void *kdata, struct bpf_link *link) { + spin_lock(&detach_lock); + if (link == link_to_detach) + link_to_detach = NULL; + spin_unlock(&detach_lock); } static int bpf_testmod_test_1(void) diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h index b0d586a6751f..19131baf4a9e 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h @@ -121,6 +121,7 @@ void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p); void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p); void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p); void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len); +int bpf_dummy_do_link_detach(void) __ksym; void bpf_kfunc_common_test(void) __ksym; diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index bbcf12696a6b..f4000bf04752 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -2,6 +2,7 @@ /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ #include #include +#include #include @@ -297,6 +298,70 @@ static void test_detach_link(void) struct_ops_detach__destroy(skel); } +/* Detach a link from the subsystem that the link was registered to */ +static void test_subsystem_detach(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4)); + struct epoll_event ev, events[2]; + struct struct_ops_detach *skel; + struct bpf_link *link = NULL; + int fd, epollfd = -1, nfds; + int prog_fd; + int err; + + skel = struct_ops_detach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_detach_open_and_load")) + return; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_do_detach); + if (!ASSERT_OK_PTR(link, "attach_struct_ops")) + goto cleanup; + + fd = bpf_link__fd(link); + if (!ASSERT_GE(fd, 0, "link_fd")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.start_detach); + if (!ASSERT_GE(prog_fd, 0, "start_detach_fd")) + goto cleanup; + + /* Do detachment from the registered subsystem */ + err = bpf_prog_test_run_opts(prog_fd, &topts); + if (!ASSERT_OK(err, "start_detach_run")) + goto cleanup; + + if (!ASSERT_EQ(topts.retval, 0, "start_detach_run_retval")) + goto cleanup; + + epollfd = epoll_create1(0); + if (!ASSERT_GE(epollfd, 0, "epoll_create1")) + goto cleanup; + + ev.events = EPOLLHUP; + ev.data.fd = fd; + err = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); + if (!ASSERT_OK(err, "epoll_ctl")) + goto cleanup; + + /* Wait for EPOLLHUP */ + nfds = epoll_wait(epollfd, events, 2, 5000); + if (!ASSERT_EQ(nfds, 1, "epoll_wait")) + goto cleanup; + + if (!ASSERT_EQ(events[0].data.fd, fd, "epoll_wait_fd")) + goto cleanup; + if (!ASSERT_TRUE(events[0].events & EPOLLHUP, "events[0].events")) + goto cleanup; + +cleanup: + if (epollfd >= 0) + close(epollfd); + bpf_link__destroy(link); + struct_ops_detach__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("struct_ops_load")) @@ -311,5 +376,7 @@ void serial_test_struct_ops_module(void) test_struct_ops_forgotten_cb(); if (test__start_subtest("test_detach_link")) test_detach_link(); + if (test__start_subtest("test_subsystem_detach")) + test_subsystem_detach(); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_detach.c b/tools/testing/selftests/bpf/progs/struct_ops_detach.c index 45eacc2ca657..5c742b0df04d 100644 --- a/tools/testing/selftests/bpf/progs/struct_ops_detach.c +++ b/tools/testing/selftests/bpf/progs/struct_ops_detach.c @@ -2,8 +2,15 @@ /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ #include #include "../bpf_testmod/bpf_testmod.h" +#include "../bpf_testmod/bpf_testmod_kfunc.h" char _license[] SEC("license") = "GPL"; SEC(".struct_ops.link") struct bpf_testmod_ops testmod_do_detach; + +SEC("tc") +int start_detach(void *skb) +{ + return bpf_dummy_do_link_detach(); +} From patchwork Fri May 10 00:29:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kui-Feng Lee X-Patchwork-Id: 13660761 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F54E53E07 for ; Fri, 10 May 2024 00:29:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300995; cv=none; b=eBYnFX8SVShj8fCHB8FmLXCaFSJgkil+gZDNOgYRwviv7xnq08PJvKimrhuct9kmNzbHo1qHSTh5DEDbtgVCLrCBZSnmvlSapx7M+sVr20JQBpdJzmxV6Wz9Oejzy3rw7Mu4ExAEGYi5J0WmgYeKw/m7l60avHe7AMDWN6sfmuo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715300995; c=relaxed/simple; bh=AOuolpRVxv8BKwP/7SBIG5zYThDowJbKd4TrLSOyZRE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KsTK8mf09bESxOlGwS8IHOkh+m9/LDvhU76hdzGhXqgBpjYB+nTsvA37Pz60ijyIsSSanAmTLTL4x2psDvL/gW/AW5q6VKeO7Zubx6WfWPH32LT1tpjamNS5JbaTOcgFjQFMXbib6/AyK02DtnoBSrApLS7Idf0o/6XvNcUzawg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dxZQ5cQW; arc=none smtp.client-ip=209.85.167.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dxZQ5cQW" Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3c9936cb7a8so546550b6e.2 for ; Thu, 09 May 2024 17:29:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715300993; x=1715905793; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JYcqFYutho0m48sKDHLd4sCd0cqM9g/XMnashe1IAHo=; b=dxZQ5cQWo4rjT0+wkguOCBjB49uAcu+BTU9DSum/juXCGE/j9xN2VlJt+8+KzBbt/+ bSJCXoSLk4I0UJmE+e1wUJpXRWc5rGE+BCxxirsKtc3n6Etu5cA3O3evav8Bkl0ISjB5 1Pq40a4Qaqvf0ztcgQ5cqCzi3imQqEj5lUQ5jHCvBtmJYOycOUBTJTkvYGG1aeC0BRLh Y/DUrjq3SvldcRqgWMs37gDlyVC4FVr6PmSOkfewAE+/FQ87TgE7ngt8zw/U/F7Gc3UK dQa4HWz0R745JVoztmB62OpHVu5Z3szWvR5MRC+Y7ODh1i1u2KICuxW//FDp9LGrhjli sF6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715300993; x=1715905793; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JYcqFYutho0m48sKDHLd4sCd0cqM9g/XMnashe1IAHo=; b=dqCO+WIDaDNwu4RKuHEZAIVQo3PTt2nsYJ62TLJ7zrN7un1q+khRMNRh1qaa0R6pyO 9kwYo1u6bTZ3BGWnKzfdiuYJDTKS42ltyzp+1YeUMoPAKqpUxf0LxecsCkrIoIgBedAk LmLbBVKXMwlTfh93l0QSxX/yKuriUvNOOKlXD8kqUdzSMTTFz7cQLGUmMlYNpaHmcKkS K2jXhXiTzdlxZSzrOIPgRB1iphaJtRnUHpx7sn3HinD8nFmk+Lf4J1ZrbqN14ABlqsa2 4E8klxm+qXMPDXaKsD0lILpLq7Sl8jX1RRpTgqO8/BL5oQv5f/eQl3wmrwCJiMxQIuUi g1gQ== X-Gm-Message-State: AOJu0YyVCYDrQ6dCN5SxDFsirsWsBJhrxc0P1d64hq3tWC6K8DE9DxYn vHfHkqGCbaDDypXabhDIeVs5SiHaca1Y1sRZQTWrrqvTJyZys2N4SVMQDA== X-Google-Smtp-Source: AGHT+IH35Jn1nWuKEDPnDE4RhB27h9nO+1cQ2wKCd1cI180qnD1G1e8cV7h46f4LZHto5nnB/uGVjw== X-Received: by 2002:a05:6808:299:b0:3c9:7ce6:3b14 with SMTP id 5614622812f47-3c9970ce763mr1132955b6e.52.1715300992688; Thu, 09 May 2024 17:29:52 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:66fe:82c7:2d03:7176]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3c98fc7e00bsm433251b6e.4.2024.05.09.17.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 17:29:52 -0700 (PDT) From: Kui-Feng Lee To: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev, song@kernel.org, kernel-team@meta.com, andrii@kernel.org Cc: sinquersw@gmail.com, kuifeng@meta.com, Kui-Feng Lee Subject: [PATCH bpf-next v3 7/7] selftests/bpf: make sure bpf_testmod handling racing link destroying well. Date: Thu, 9 May 2024 17:29:42 -0700 Message-Id: <20240510002942.1253354-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240510002942.1253354-1-thinker.li@gmail.com> References: <20240510002942.1253354-1-thinker.li@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Do detachment from the subsystem after a link being closed/freed. This test make sure the pattern implemented by bpf_dummy_do_link_detach() works correctly. Refer to bpf_dummy_do_link_detach() in bpf_testmod.c for more details. Signed-off-by: Kui-Feng Lee --- .../bpf/prog_tests/test_struct_ops_module.c | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index f4000bf04752..3a8cdf440edd 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -362,6 +362,48 @@ static void test_subsystem_detach(void) struct_ops_detach__destroy(skel); } +/* A subsystem detaches a link while the link is going to be free. */ +static void test_subsystem_detach_free(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4)); + struct struct_ops_detach *skel; + struct bpf_link *link = NULL; + int prog_fd; + int err; + + skel = struct_ops_detach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "struct_ops_detach_open_and_load")) + return; + + link = bpf_map__attach_struct_ops(skel->maps.testmod_do_detach); + if (!ASSERT_OK_PTR(link, "attach_struct_ops")) + goto cleanup; + + bpf_link__destroy(link); + + prog_fd = bpf_program__fd(skel->progs.start_detach); + if (!ASSERT_GE(prog_fd, 0, "start_detach_fd")) + goto cleanup; + + /* Do detachment from the registered subsystem */ + err = bpf_prog_test_run_opts(prog_fd, &topts); + if (!ASSERT_OK(err, "start_detach_run")) + goto cleanup; + + /* The link has zeroed refcount value or even has been + * unregistered, so the detachment from the subsystem should fail. + */ + ASSERT_EQ(topts.retval, (u32)-ENOENT, "start_detach_run_retval"); + + /* Sync RCU to make sure the link is freed without any crash */ + ASSERT_OK(kern_sync_rcu(), "kern_sync_rcu"); + +cleanup: + struct_ops_detach__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("struct_ops_load")) @@ -378,5 +420,7 @@ void serial_test_struct_ops_module(void) test_detach_link(); if (test__start_subtest("test_subsystem_detach")) test_subsystem_detach(); + if (test__start_subtest("test_subsystem_detach_free")) + test_subsystem_detach_free(); }