From patchwork Tue May 21 22:51:15 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: 13669803 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (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 D77A73DB89 for ; Tue, 21 May 2024 22:53:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331984; cv=none; b=KVQK93gSQsW7YAyP3LBirIW6j5dDz0bY3S2glbS4y8I3vUav33FOEli7ip5M0GQZVCtSC4HUzHOjwQUkuXOqjyIPvyeRn/kNr6ky+SzS2vG6vcNVyQiLULgPh+hef3N8V7rMy9aDlwjj+L3365mC9KqNJI7x6fBnqD+TJLXIbL8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331984; c=relaxed/simple; bh=CJw1aL03b9FTfcvig5zTnobUTi1c6JlqNPNhQ64K2wA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=H4IWvKBOPvKWkXLylZN4CsyleILnInZvdqk+23DUZNwb3bEJ0pt/ZwlkRjzV9AZLHL1DN8IICh80+e0H9lEqYWJArDgiwNdYfcqYK8SvGYW8Jb2Q4SyVctgFaGk3p02kzZBL47F9oAaiwvVAdgvOgfP0uceedx2unz5Ezks0meY= 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=gnoNTLse; arc=none smtp.client-ip=209.85.219.171 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="gnoNTLse" Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-df4dce67becso391036276.2 for ; Tue, 21 May 2024 15:53:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716331981; x=1716936781; 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=tmt0rlNlJbrjlCasd7uin+JcPRwcvSc+pKBeG7SHYHw=; b=gnoNTLsevPIuX/itLpZNqe481NXjhUSCiXCk3vtZh09Rr2AXvwseapA7HQK3ka002q zCfaulKTIfcEddmzDLmQF4wc6OrNKWbPIBS0J/fpyBMD8shUGQ5d+4Z2GRPqVK+PO2zV gdfcDdrO1WLRLR+7mK15T+7dGoXN8MqblylussDQim+sthjMBxUIk5qtJ9bnIp7qPJju +SAeX7uaLczGwdvKNEPtmSBKvEGuhHTQAvjwVgd6BYJKibXBWl3JmPGu8uGztaSQt4X0 gJfPd/V0LSOZKyWbViz1LUE8DlRbPfYAR6k/kg5nZd0KF66zxRkBXiopC/IEuVgzv73M YHnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716331981; x=1716936781; 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=tmt0rlNlJbrjlCasd7uin+JcPRwcvSc+pKBeG7SHYHw=; b=r1LJGlvanGXdOn+WjxHpAp53siY+qeB51ZUoDco/2DYbp7V7i4ZN2B2qyjOM/fJPz0 i2GWx7cmeOOdo5Lcx1XC7GdKuQWP63GJBoncYNEFSm2gHwoT8J0W/IgftIPfEaD7s+z4 GFOKLv3w4+kI/sn2x9jZIkTUMpsbHA1MO/f1+ockIJAUWmCtywOe4/Mv8c8AYO4GGesY tNSYKj9+LvkcA8l26lilaZXWADn8IpbUKymaG9rB2HozYuoZx6HHHy3z2jVVmvRdhidi 5IQMMnk43PRsa8AL9DKuH1ShzK4SGsGQvajO0eu18dx8HJvr2DNWz3c7Ngx/+kzTarLi 81vw== X-Gm-Message-State: AOJu0YxdVCejAztybXEsEQKXf5izZ8ndzRcYmNVBI3OxqcOoMgvGRbyP iSGjirqkOBRrIxRVo9DdQItmTBVDiw3kTq2cL0uNbv89DLbMGFbRtHSsGA== X-Google-Smtp-Source: AGHT+IGCfAdSgMeaKbbmnRi2OrrUWI4Jatj928/eJ2jG77iC1fuEz+HImhVFqizczvQZocsCFAJ3Nw== X-Received: by 2002:a5b:387:0:b0:df4:b3f0:5ee4 with SMTP id 3f1490d57ef6-df4e0aca9fcmr510502276.16.1716331981444; Tue, 21 May 2024 15:53:01 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:1437:59a6:29be:9221]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-debd385be51sm5584956276.54.2024.05.21.15.52.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 15:53:00 -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 v4 1/7] bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops. Date: Tue, 21 May 2024 15:51:15 -0700 Message-Id: <20240521225121.770930-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240521225121.770930-1-thinker.li@gmail.com> References: <20240521225121.770930-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 2a18bd320e92..0a09732cde4b 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -820,7 +820,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; @@ -835,7 +835,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) { } @@ -871,7 +871,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 Tue May 21 22:51:16 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: 13669804 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) (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 65228168BD for ; Tue, 21 May 2024 22:53:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331986; cv=none; b=LxHfUgTS+0t/bKApLL1poQ7E8AKWAbhCSRlzWfU7/DRgwEA2ouBdv+MJpW1zkM9KexWwXW05x6yhb8H6+22LKvWxdwhOsW9zgaqxw8inRVAF6Q6bXFf7S4lUGs5Z5fpF+rink6EUxtFLrqSk3wGjtBfsJUzyRUdUXas6k1hrxkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331986; c=relaxed/simple; bh=SN+VBDApFs6v6iui55nDMM2xqXaNc7HG3UYrm+zjXfY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eKCOQCIZSMcBPAqe8EezTHy9BVbUYBZeZ1RWapahG/qTJNt/mG9GgqjgFJrQ/TkwWq5zBAxLvVetXW2BC4CKoiGgyXpeFn9/ko7VzsNK4PuL+hwL4LbyWMpAGoYU7PviaJub5LJ0EpoxcIb/5/8LrdPLOfK1DNopQW+g+VHXGC0= 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=RQVnqGWh; arc=none smtp.client-ip=209.85.219.181 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="RQVnqGWh" Received: by mail-yb1-f181.google.com with SMTP id 3f1490d57ef6-deb99fa47c3so4420575276.2 for ; Tue, 21 May 2024 15:53:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716331983; x=1716936783; 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=chpm7+dADH4AMcj76DQLCHu1k8L/vbJZBmWRkL9eTOU=; b=RQVnqGWhFER48aHCxZIns9MnF9TgIlRbP75VsFuIGOBpNSxXWlfU+JBbdCiSc0GMRn aiJ7WlvG4Mvc8W2LeVL5qej08JpdE6plMEnap4vZmucC8iEQdbUnPbsYBnMCpNrscIZO AckHYHjV3QU6Ma3qIRrN6YzhSdsy8lAGOUjP7CudXcosOVaQHSmWhqg4HdxTdpG820gT zLYysNnkmxCLn3uliiu6vZpykNfJSHu/04yi2wSvALZL+n7t1fV/nZSgltP8PFx6lJHz jP1pmFLq47k5v++k770YK4kj4G9noCRxxHCnpbOxQvndelcAftu6mxqoN6E5mmttZz5A BT8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716331983; x=1716936783; 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=chpm7+dADH4AMcj76DQLCHu1k8L/vbJZBmWRkL9eTOU=; b=qAvC/cCvoXEbB6Oc0JoJzvF20Zm5/KFUsvMc8GEvqgY2HxxOsw6+mEA4k6GDtEXo0k Q5WNGfddR6J+BvZ4AVgulK4+WKHuGaGC41ILjFGX6VQ+l61jTCMKPhFkVA4R7xF9hVje FXZ1JwJO5e7wibWtfLkx5Jsbi3ZAI9Ok+RaGHFpaf8sn3j2TNPVyHzFXKLjzll8Al7fz uVpThU+uNAAQlMU2untv9bClr9bnpkZqU0C5ZlMIzLmXDjgUThofMXoMQlHH36Lv2xsA EWBvr8khmWaKU3cKYLarXoJm9GJ5giIaPNWcEnkwJmEg+2bdLn3ai4Nc+3GP8Z5BBewA hVtA== X-Gm-Message-State: AOJu0Yyd4CDt5dGxSjRY0mO+zmxSUX+VSrZYx5dJrDTJcGbijRxQDpmO dZ4LO2Vi12JjMF9ADAYkDj9X61HVhYLh0LB8K2AexK8K/SKX4MjcDNhnMw== X-Google-Smtp-Source: AGHT+IFT9eU65Z0U6F7MypaIQKICz+/pondI0stiL9wQlMy8/++ZLl/tnxfM3CBJGXTSNGU5P3L91Q== X-Received: by 2002:a25:3343:0:b0:de5:9f08:d30f with SMTP id 3f1490d57ef6-df4e0e1eb18mr404391276.42.1716331982866; Tue, 21 May 2024 15:53:02 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:1437:59a6:29be:9221]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-debd385be51sm5584956276.54.2024.05.21.15.53.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 15:53:02 -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 v4 2/7] bpf: enable detaching links of struct_ops objects. Date: Tue, 21 May 2024 15:51:16 -0700 Message-Id: <20240521225121.770930-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240521225121.770930-1-thinker.li@gmail.com> References: <20240521225121.770930-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 | 63 +++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 1542dded7489..fb6e8a3190ef 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,32 @@ 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); + + /* Once reg() is called, the object and link is already available + * to the subsystem, and it can call + * bpf_struct_ops_map_link_detach() to unreg() it. However, it is + * sfae not holding update_mutex here. + * + * In the case of failure in reg(), the subsystem has no reason to + * call bpf_struct_ops_map_link_detach() since the object is not + * accepted by it. In the case of success, the subsystem may call + * bpf_struct_ops_map_link_detach() to unreg() it, but we don't + * change the content of the link anymore except changing link->id + * in bpf_link_settle(). So, it is safe to not hold update_mutex + * here. + */ 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 Tue May 21 22:51:17 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: 13669805 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f175.google.com (mail-yb1-f175.google.com [209.85.219.175]) (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 921DD3DB89 for ; Tue, 21 May 2024 22:53:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331987; cv=none; b=nC0xVqaX6HPDagohY4Z+FVYsz1yxLYijtqmlJXohBj8rTR7oiMw7zi0m8KLsB/QXRhMyoDOTFsUTz36kXra71ug6yi7gSEBOXJK1WguHz931okivd5ZSgKtwmZst6WhuRBiFpng9r9ely8DkPlVRf4X2zazFzo++yeBBzWizGp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331987; c=relaxed/simple; bh=04LoSuRRxOtHnGba7+hhoWLwL7VVgeZlsTwx0PPIAgI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=P+CUI+RWf1twEQAn26r77zC81kB/hJmSS/SWI40KC5xbiHzArwfJA0uZZTl9UXHm2KYmut3wu90zUiEzgLWuSXKRlKkxaDTwULXp5f0YL+sA6Y24+/AJoDyuE3vBv+fEzGWwpgKejLC8Ce/RFBBb8biANh9G3E+26xwmWDUjwIQ= 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=RMzns63I; arc=none smtp.client-ip=209.85.219.175 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="RMzns63I" Received: by mail-yb1-f175.google.com with SMTP id 3f1490d57ef6-deb65b541faso220784276.0 for ; Tue, 21 May 2024 15:53:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716331984; x=1716936784; 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=1IfzfcI2K9QtYKxHBIj5Q4K0FDQCL16Wws/rejUd8fg=; b=RMzns63IjbG6WkXLkZ+FD7aQjAsGwjzZ1b0yo1+P214dPMVw8jLj/YIiFxvKicW7Ns FkobNNoPTjt/T6/WzuADy5MLtLoV9eMukf2e1u0MryA16DhM9P0GpfkpKA3IxWuEOaTE WU9ZFNz1G64y61yLE9qnhT00WQY3cOf3lQW6n8/u0Sr4Yppw9/kjEUMBsOIPaiOMqBws jEpqg+ymlD/p/xznh0fqKT15mexE1cmAn/qjR6KftLLt7sIMvPOI2bBAyP9VKP5PwU8Z O8lzLgfTcmrW2ofBEzLTK4EgG9IRcoffegvbPsGGj7IylReczE40hFOb89VFDOEEdbHe b2Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716331984; x=1716936784; 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=1IfzfcI2K9QtYKxHBIj5Q4K0FDQCL16Wws/rejUd8fg=; b=q7+5vjBHGRtpcltU47kQOCONwGsHYGaFi0J/D0vqaMeurXso2XoOj4idnzHfsI7+op uYdcWO1MK2J450vEVUCi/VuqdFJJDmkjqcXXBlddl7G9RG4F+3UppAyUX/QIGhxXLoUj STSeFiAd28QYNYKKyLlas1/jzwLtlTI7vVfMj4xGHpd1mr5WU5P5yfQmGJLrI9CNvmwt WSc+SoyXH2gaofTn1LmbFu16v/t92HaSweksBL+dWQmFAlzgBv+anEtLBE/hXhCcKZDT Ud5qmk+YA7GiGcFJFwhxXJBAcSk+BT1F8Xt29kp//6D86XMfudxeNs6L8MnJVe6mxKvJ iWfA== X-Gm-Message-State: AOJu0YwV+tM7hG6nLy+PHFazeU3Bm1hCoshzMSULVa//r5knsl6+no9h M7/2fFngdrqvj0tihhtHP1AJJ6Tb3Oyr6fNaGsmOuUvQPnJKONAdFsBptw== X-Google-Smtp-Source: AGHT+IF1WiupjfZsV1FMjQCCu8X1+uV5BzuID1DY+Y1siACxZ5ykwECqdb8By4o0Mrt8zSByOsuWwA== X-Received: by 2002:a05:6902:18d3:b0:df4:dd4c:2b0e with SMTP id 3f1490d57ef6-df4e0dc8f1bmr574438276.48.1716331984236; Tue, 21 May 2024 15:53:04 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:1437:59a6:29be:9221]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-debd385be51sm5584956276.54.2024.05.21.15.53.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 15:53:03 -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 v4 3/7] bpf: support epoll from bpf struct_ops links. Date: Tue, 21 May 2024 15:51:17 -0700 Message-Id: <20240521225121.770930-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240521225121.770930-1-thinker.li@gmail.com> References: <20240521225121.770930-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 fb6e8a3190ef..794549dc9f4b 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 rcu_access_pointer(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); + /* Once reg() is called, the object and link is already available * to the subsystem, and it can call * bpf_struct_ops_map_link_detach() to unreg() it. However, it is 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 Tue May 21 22:51:18 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: 13669806 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com [209.85.219.172]) (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 5970614A08E for ; Tue, 21 May 2024 22:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331988; cv=none; b=NSTOLQhIXeilPTs6LR73UhoOP5XnLcECJCiI00ywJu7GjOpw2+BWCTAr1SrcRY9oVuIW1jiLHzkzOodz6GqGkuHMuviJ7APPw0PeMjkeLSe7/Qk9Wc32dN72w4nHcM8TsYp3C0wGvI2Vp23jWAEyuA+It5KcseUM0iKgz25xb8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331988; c=relaxed/simple; bh=34VL9O4NDfy7Ojt5kCpX/zgfJ2l99XUzUsH5/dLKqgI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hKycT0ZV95dzaHCBmFZhHHLrG4t57GZSiAp4UnKCF5OYtjY2rYyqkH0qNFeH+Hc3d3uSlYB+4khWgKCbn37AXxq1vICaAkw+AEYWucokcqXD9ghlO3VVtlVSVr0yNNYWMle53FHMl7ec8i/9K8ag0hqXRJfI8YHvCHTuhmtR83Y= 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=Z8pQ2VNQ; arc=none smtp.client-ip=209.85.219.172 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="Z8pQ2VNQ" Received: by mail-yb1-f172.google.com with SMTP id 3f1490d57ef6-df3dfcf7242so4370449276.2 for ; Tue, 21 May 2024 15:53:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716331986; x=1716936786; 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=Z8pQ2VNQKWHLwWRM5LjFIH0K/AFDzsmbCqBqy71sVLybeLX/EfFA4CYAX/YpNMCkRh XCD+MVA6VqLfsHIi6jB+051weJ1f4RyZof56qcusKfy3tyUkkBmQKktq6CvCcIwqOQFI AWFSgAfkb34atHFsmkvQMF/6FcLz5JvLzOBGLU30cM3QGYB6vzTtpEExzju0hKhyWO5T xRgF+w8w34gEZYKLbqIrVMA+5YMCert6e5s7Mm+ybTsEp1K+WvTdyeH87wocaxVpqqkZ q+VOXqe8L+etPVz+Q/jQaVdTJOhO3jmeMQ0s+ZCUmj/TjpBaxW3bPvvd2l5VgUUbDpWN UkNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716331986; x=1716936786; 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=UAD4JvnNz2RvR/guI3fjtMVZBmfC/1M7G36bKLwkq+sbf+SfQD4meIgEdZAOwqBjOt 3ZYNz+NIo00LgpbTOejB6KSGVlpafFL78aoxPoWnMFuVL0fC84dCxtTxIGjwYIdH2kRR mXMIa+4kQgana4VxPLMkgKyBr+z/KT6sp8AsPZ048VkdveyfIBtFfOf7yvn4Kji5m8N9 eqCXBB4dVJwIY6+0I4roUK7EGTYD3sL5BJJfS1pkYGQQFPgYjNzDsSCaJ1bK4zlEilwu FqCU1w8iMztDFNyO1XguYaWm9ZKHnyyQRxKLMq9swJr4HOsg/pQtwnzWtOvOKNOd4Ffo Dr4w== X-Gm-Message-State: AOJu0YwWEYdIFcDBFxwC7HyNVTGRfswn7NDHbfGdHQtorfIdadGR+IIM bBYqReREhVP/un93gKSS48TkS4GRWzVTC1KsVWNxJwqR/JazXVIJ/dW9Ug== X-Google-Smtp-Source: AGHT+IFhLsnl4yrwk6Ob1Z+sEeRfKZpNO+JcMKPyOHpPOxBTl6viQhSMzrJe81zdaIdssvEubDYptA== X-Received: by 2002:a25:e047:0:b0:df4:ab4d:2c6c with SMTP id 3f1490d57ef6-df4e0a892e5mr463929276.6.1716331985884; Tue, 21 May 2024 15:53:05 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:1437:59a6:29be:9221]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-debd385be51sm5584956276.54.2024.05.21.15.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 15:53:05 -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 v4 4/7] bpf: export bpf_link_inc_not_zero. Date: Tue, 21 May 2024 15:51:18 -0700 Message-Id: <20240521225121.770930-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240521225121.770930-1-thinker.li@gmail.com> References: <20240521225121.770930-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 Tue May 21 22:51:19 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: 13669807 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f177.google.com (mail-yb1-f177.google.com [209.85.219.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 AD4E13DB89 for ; Tue, 21 May 2024 22:53:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331990; cv=none; b=WeLkWWVYhrXdyEYm6Z85wb54tfwXkU1wf3vOMXDcyYksRHGZax1nrL8EJOGowV+9h/S51JFxz1DuG4207zmu4GQIMBWRDp1fnWgnhcJjif7pOIOCCGiLNg3qfWhrcvOHUm+KxTSercM8p6H+t03G5jzJQA71adi+VXxPEw4XuwY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331990; c=relaxed/simple; bh=0moVWm5H0/PxkymYi1Wkc4v/Yc9p3AxXBq0vFZgiFh0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RRdYhBPOAga6n1XNMQtGo2DWZOfobesb5fWqWnM8PlGRgsive4g5nUvYWZVAoET7c+DbTbZyVKOsORlhGussQoql+YsWywim0nGAYI55beJD97uwG91MgyzOI2QMrayM306iKGW9rBRlPa339QVE5p1sNk82zk4wO9tA8Jaftv0= 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=RNY5WKH5; arc=none smtp.client-ip=209.85.219.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="RNY5WKH5" Received: by mail-yb1-f177.google.com with SMTP id 3f1490d57ef6-df475159042so3688215276.1 for ; Tue, 21 May 2024 15:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716331987; x=1716936787; 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=RNY5WKH5g+Fkj0nBFC2eISp9Pt/4P/2e9FgRtVabB/41hMZ5B5BhqpfpOry6x/EJGh FgJLs6XFcwWb95jEOdU+xufwdvE+kNNdQmgnHma3AZLoVoKD85alVZ2hSlB7M8+a7yFe gOyfFZi7BTc2BO1m9qIzGlj7OH9YMZe+m1rkzsSTI5FzY1I6cWPSGDeLkLOGxfwqMNJK qcvhxp1TpGI2jc2zCklQ9NWYlb3u4aml3tnYhFWwV02W/RvOjK0BDm7AAP08ZNhKMilf PrkmFopqubtqYdhSa7Z2MedjVhVJEHn6HB8tt6d2cH1ZoYh9FL94jkYeoE24Lj2HB4rd emPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716331987; x=1716936787; 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=GzG6J09E9fyc9x1LwLIZ+8H5lavYrZ+3TZFyruR+Pjmfec4fiJJFG5Ccv71Hsw/G6I YOnTQaqyKPGIyqs65ddehcIiAgpssHGjHesJW9J9txlBPPe0KFdpNjtXbRnDX2YS8MbG pHl9NlfNViF6a5PllUBLCedaE9dcWVA1G3V5qe+TFXiIaF2aZrZxNOIVQf0iiyC9S1nV Jo4CbnJdP98pW2LGAonqXhe4rL13h/60jQsMoq1+Rx2f6mGnEVjuRc/8hqaxs08nXX3q 0WB0D4do3mLEcoGe2SimLGNt2SAk5TejntlOvZnXIe3++WZXxkUUJWdPylxcrTfmFc7+ Ylkg== X-Gm-Message-State: AOJu0YwAKjYdDMjL57jIeu+aitfdhnm+eWsleo37UWZ5SwDRFBF5wnRc 6M6tywP4tP2DhfXDVM+ZpkrlI36oK8veOTe/5gHNkST6kB/B9AsDisE9lQ== X-Google-Smtp-Source: AGHT+IH52BZVgM3CFeX62JGOjES9NfTSwcUJ+Wnb6793zKObFVbHDLPbMm4g8gkYGDZn/DaVA9mnAg== X-Received: by 2002:a25:d3c4:0:b0:dcd:df0:e672 with SMTP id 3f1490d57ef6-df4e0d9bb11mr463869276.47.1716331987343; Tue, 21 May 2024 15:53:07 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:1437:59a6:29be:9221]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-debd385be51sm5584956276.54.2024.05.21.15.53.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 15:53:06 -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 v4 5/7] selftests/bpf: test struct_ops with epoll Date: Tue, 21 May 2024 15:51:19 -0700 Message-Id: <20240521225121.770930-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240521225121.770930-1-thinker.li@gmail.com> References: <20240521225121.770930-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 Tue May 21 22:51:20 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: 13669808 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.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 26ACD148FF1 for ; Tue, 21 May 2024 22:53:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331991; cv=none; b=tX5qci4puxyQL2RjSTWxwZ5MFLZEc+4VhruSfRJGT9stX79xGYW0RybohZQ5oB+peNRN0CDWobH9pIKsrsDj6cYsDlO2N/1TxvkFtBw7cvMAmSk0bgDtX8Es4pDWsdzzxuKIqRcHxLaHmsD2Q9WPOmDRFX3UGbhaytmayXgYf8o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331991; c=relaxed/simple; bh=M/Uv8vWTI1pT9gejmLQvJRT763bIa7cjhVoGiJeMPyo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Wtzy33cxe+t+DVhPMx4p7waFUYUGCaWGDG8zZ9f6MsgnsDS7sm2sf28JVmPXlHSR0wFxgzJp+TN+W18CGvb/Me/cDBLHwoKU7ULS4a2dZURd7A26JTPtLKzrRnv9wV6NCkxIRhVqfvv6v3f/TKRqigfcY/pg3RitVpSzpvesK98= 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=IsXLzZXQ; arc=none smtp.client-ip=209.85.128.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="IsXLzZXQ" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-61be4b9869dso44872897b3.1 for ; Tue, 21 May 2024 15:53:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716331989; x=1716936789; 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=AUnPrrURnrhMYgU2LE3oHrFC+PkOKbHjxUopkZxUqDQ=; b=IsXLzZXQBo4qz7j6H4g8SMPqARJYTp9cBvb5t1v48ycaPMlJZzwCyUsCgsUXDaeMAi lZn+8GZrr/UOuv9SlQyuOKO88Mx4kPF1X2SPYWfYoyAb7vPIvQxWS6hmhH1aaaonuvsK QE5DUgEgzlU0H76x9sEzvyXwT9vKeS66PuJna/4RCkLWx6msjtu+yRLdusw6lJ0OslDQ /dXuEsIHAwvMOaxo+0V+ZJZH/PUI8lqB5lfSkYGhFqKbBIj41KMActtMEnqUkgEL7Poz 2wcu5gAKUuQ/pDTubzB0TXmqwfh2y5VbXBp9FIbYTxMPLKxx1xdjOUr7pkTZ9qFhSCcM c+sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716331989; x=1716936789; 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=AUnPrrURnrhMYgU2LE3oHrFC+PkOKbHjxUopkZxUqDQ=; b=IjvQTRKWH/l3LSi5Sgq0uPVYBNvkKYz8FGTtgu3jxZKiRfCeauFiMpsiGdNfVTn4XY p+SXbkm0Pu5vbXqA7k7nVLUm41eFHeaPWWZWboxd/h6S05pfutuUaHG/1Pjd8O0wW3Ym 4VW9iaQhq/fusb4/q69k9u5LB6QVR2RG2S0KpYP5kdgzO6QLv1qpL92BiU9xb7+zv2m3 kpX7t8oveB1QMEqWJC6SEVqywhxy44x6bjuL9bBw/7Mx5o284laX1Sga9XkpMwck1N6z xFUoPQTpjDWd3jJ6i0A9bZdDX8pnVaTDo8S8S7YUDkHZC+ZXDKGwQKxw2QAM4/GC/eml uc/w== X-Gm-Message-State: AOJu0YyR1x9zo51EbLTeqoWg6MvNLxRL7L7LQ1EH4XIALwezy3WN115m 7YKR4nykzbZPEOWvHdh13K6p93Du1zqAuui92S3SS8wAGEYE0rLy0O0Yag== X-Google-Smtp-Source: AGHT+IEVZsg4WUyUaXN5LkTdDpTbicLEkrMvX7w6L+XxxnD8gi6Q1hG6oIgZ0F22vS8FtG1S4EadRA== X-Received: by 2002:a25:b01:0:b0:deb:c936:abf6 with SMTP id 3f1490d57ef6-df4e0acf030mr498660276.24.1716331988679; Tue, 21 May 2024 15:53:08 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:1437:59a6:29be:9221]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-debd385be51sm5584956276.54.2024.05.21.15.53.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 15:53:08 -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 v4 6/7] selftests/bpf: detach a struct_ops link from the subsystem managing it. Date: Tue, 21 May 2024 15:51:20 -0700 Message-Id: <20240521225121.770930-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240521225121.770930-1-thinker.li@gmail.com> References: <20240521225121.770930-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 0a09732cde4b..2b3a89609b7e 100644 --- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c @@ -744,6 +744,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) @@ -780,6 +812,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) @@ -832,11 +865,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 Tue May 21 22:51:21 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: 13669809 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (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 D09252262B for ; Tue, 21 May 2024 22:53:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331993; cv=none; b=QRg3F2yNokS2dAYx37DjNy8vbWR1f5oW7mhnubL9mupmPHw30lEwdpC0+S3d+fKx/d92E6ZMiu1umrQF/k6CZ3AbEVcWKKxeRorzZ9/rHtTTaeHTjepMZWlyfjGewTssUG/NArDM4uSyjMKDrJqPnh8F4HeAtEqJtggLH2uN+Pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716331993; c=relaxed/simple; bh=AOuolpRVxv8BKwP/7SBIG5zYThDowJbKd4TrLSOyZRE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ke7+6yASahBnU3klIfpYd4fsL9mVCuACfa3BGCz47RZjbhi1zj+v8ZWtjoTttiz+HpXYipNhHFDzk5IJKYfNIM5JPpE4GH6qKei6tBzpD0oya17eIwodLWPIYtTjKiKpGzpNiDsO8q+tePrrKfzfeOfbXuxk1Kju+QpCuicwk/c= 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=UbY8uZX7; arc=none smtp.client-ip=209.85.128.171 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="UbY8uZX7" Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-620314aa1d7so44627887b3.2 for ; Tue, 21 May 2024 15:53:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716331990; x=1716936790; 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=UbY8uZX7BTQbnF5Zu7SPcPBXRFNdNzt5LswSN1AV7xLK3h8mFhg8giFHOd4xLMJI1B EFCEEg5VW8dNJoCdBf9U6fxmU7RS4P8BHdOw9NhPFtqioRzYO44pfc+bIee8yYjO83Qs G9xlTmAud+TKN/VfizCXlrl2LAkeKZCR7FyxfVnVQgz6rI9PHhWg06bV7aV9wPQiAdhJ Te6QQRyWRmbYNTPA1OzZFAOXl/a6ros2CPtHXOU/4jO+dJWe+o4DtCLsEZWUuW70Xmpf SGnq9+f5PL2wbpUk3HllXLAbDtk9sVn1MVOBt2/U+eT1h5B4GBKxIs1051nMUJ3MFRvO 4NDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716331990; x=1716936790; 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=WZco96/G4zZm3pUvLmCKIMagIuF3kNqJr8ZFwTguHDw/1fa5G6oo/pZAKP/WaM2Qlw iet3oVlj8JxbqM+9re3iAjOzzdWVAmKmhDEJ3wKSOw9EQ4A0oLpn2oBEdj6ERYcOcBhD HilloxFXEP3G/PNyBk7veEole6WovY1WnJfg4LW1FMAoAjB7nB5zKBwoXmllkoA3dpG4 2zOsuU/KWjC+NKKvxEZKB+Ehgen9pGcTgnFiemz7/hkwp0gosU9LEOGTdSGEHlhB7nRP RuH4Zjh4yfcHnDAlCe/yGHaW258XBL1eX9URwpHf28k4zqAnkXUGvqJBs5Q3gJeQ1c36 AUrQ== X-Gm-Message-State: AOJu0YwZtRIRPfXdXOCOv8z+/fYp3AIQKMMp3WwjoteLxO384QNCMOgs uPfEoIjzI85I3fDjw3yrxVLIqnTN/xCvLdKQ+lhH0lqUNT8JEe+w3kMcQA== X-Google-Smtp-Source: AGHT+IFrDOZu3EHYguaNMVxdf6kntbGrJnCULTSAHl+wHA3r34j1mQYZ3RbmtfBmY0lDGZtHpf3grQ== X-Received: by 2002:a25:aea1:0:b0:df4:ab39:8c1f with SMTP id 3f1490d57ef6-df4e0dd1f85mr487427276.53.1716331990127; Tue, 21 May 2024 15:53:10 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:1437:59a6:29be:9221]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-debd385be51sm5584956276.54.2024.05.21.15.53.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 15:53:09 -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 v4 7/7] selftests/bpf: make sure bpf_testmod handling racing link destroying well. Date: Tue, 21 May 2024 15:51:21 -0700 Message-Id: <20240521225121.770930-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240521225121.770930-1-thinker.li@gmail.com> References: <20240521225121.770930-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(); }