From patchwork Fri May 24 22:30:29 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: 13673769 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 5895C383B0 for ; Fri, 24 May 2024 22:30:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589843; cv=none; b=X7zzp3FL+3uEPz8j+WTM/jsqJYepGKFOn1Kswgj8JBCMpxM1Xqmit2gc+om8uuqVNaAJdkHBLxZ89hDu8lTPhA2QEAAHyEMzb8yl41mvczUeMa6I/KHDFITstIfX1WFXyIbeOyOJ2CptdPVV1pZj1EEQvC0urTg2qfGDubggsCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589843; c=relaxed/simple; bh=CJw1aL03b9FTfcvig5zTnobUTi1c6JlqNPNhQ64K2wA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QZAGM5P1Q9VAqp4NfaRAPSsCEcAMjwUa6Vjr8Qd1CcgeFOjmBYwEXcq66AV8uH/ldnuQ9+Kx+loK/E3tpdJsfstZyrCA/z+GMe1EoYl6bq7BzidHJLR+ngrV2sBsXkQgWCfoW+k7Lk5riR6Ogjr+a36ecyukILAK+Smw0EXI+ek= 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=VKqF7LYB; arc=none smtp.client-ip=209.85.128.180 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="VKqF7LYB" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-62a145e0bb2so8706987b3.0 for ; Fri, 24 May 2024 15:30:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589841; x=1717194641; 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=VKqF7LYBEoCKcuAUv3YrgofolzfPWpJy+4ZyZOS2PndgkBR9t/svCWfG19B9hx1PLe tKhBz9Lap281M3ywwp94M0FkgVnKvWKkesWJuhImHTPRbOHC+nuvSdI1r0n4wL4kscBv rM38OspN2FoQ+qwssqryEcJPEa8X/Ij89arFllvaN+yjK/5Xz6YgZylTO9nrHuYSu4Lf SuZsI/4pe1F67du0EA7IuWVKDgy2sZGSyXfTKectHTgwKMVEVvPKaeVISVbt6482AUDi J+0Hlxsyy8WFGI4fGC7QoqJRSDGM4/xP+MeOQAqg34Xtl5XZtnJt1VPNl2fkSuvhqjbb e5Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589841; x=1717194641; 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=gqgN3lP2+SKDm+fmSBu4MDivTDumzdL0k6sB9uDK+WG99QkfkNGOWxCGpvf0h2RwxJ PVpQBLhSebRCzMTlvByEozuG8Db6qdvfZ8N2ND0641KPooatJJv/rOC4LcdoaXZhATca q0yplQ/HFtc5AggjT0siFuI/a4mGQr5BXFpzvfDFfMeqtntMaIlyaa27waIHDLOq1Owl op8JGjGRkF+8ajbCmYGQVohB19CXWb9Wa1+tvgAaEqm5iZJR2ay1W4Gaa2FjHx6+Zvu0 R8l7FP+VXwXORAA+gJuqLqJQ5SGeqbf37ImT0QusCVBKOw3mXMJIvFlHubEG7BPcPKIy rwbg== X-Gm-Message-State: AOJu0Yz+HcXCu9FBlvyjPauiIMTnTlcaTSvvSR8AglYqQEp7R8pqvcJo m2AV8N55gINEk55mQZv6qPu6VTGdYmjWtXBbtYnO5HoKs9yX+DgNa6NZSw== X-Google-Smtp-Source: AGHT+IEEKpYPHfl1N/J1pRaL7dtl6WG3t+kNdVnI8tG1rHyL95DfE8DkEFhpVpqDYCc7JRauXnkw+Q== X-Received: by 2002:a81:b625:0:b0:614:7146:ea93 with SMTP id 00721157ae682-62a08dc235amr36080907b3.25.1716589840831; Fri, 24 May 2024 15:30:40 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30:40 -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 v6 1/8] bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops. Date: Fri, 24 May 2024 15:30:29 -0700 Message-Id: <20240524223036.318800-2-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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 Fri May 24 22:30:30 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: 13673770 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) (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 82B0B86252 for ; Fri, 24 May 2024 22:30:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589845; cv=none; b=S0Sx6WU16mdrh+et30S9sCYs+mq5qfTvGztLQV/BmCz7R9wKsfmWfSj4BoANegZ6M2cT+iJPL93IUnw2LEZcT7Yh8PBJSiY5avfdCmNi4G1YwYcpRczBZYFt8Ey+0NroqvgqHHy8dvRuLu0a6O5hxoY/6dCilfglYd+1uw/YPV0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589845; c=relaxed/simple; bh=xppD84te++LZ4ApfbZ1oIZIJ9J0Hv6djDyNJeWeu1pk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ILC3i60g62IzppKcEJHFG7EgNXak4CvG+Q7xsPGbG4zMDgZ8P9Q9aJoobUvkAI0WC8/SjgssnskO9uk8IxmO28vOfsFSlOahr7MZ7VHabhmKB3mtk2AocwEVHIhBmuxufaZHZf5TU22esZeUHyG9NbM8dbfuDDKUD/PTHFIDfXg= 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=azdYprYg; arc=none smtp.client-ip=209.85.128.173 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="azdYprYg" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-62a08b1a81bso12972397b3.3 for ; Fri, 24 May 2024 15:30:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589842; x=1717194642; 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=UuNnsh+t+vm30XUZlxFAiVM4aWEhFtEMhV1CFeDWxlE=; b=azdYprYgL5d3cXejgTnaKrIR8qbAPjGfvUz0znLoqyKU8DElNzPMVaPf1+wRDND9HG GAfhvgvJiAb8oxrbRiricKlGX2vBjm2qKdGnaoucHHTSR5x0sQmm3klAPaGb4LiTU7pu R0amrX8a7jPNe7iaHhkltegFWRnDBCSQtk0LURH2PUcMU07JiyHe5nyaWRXvHDMB6mkd 9BeWF0llWr1oVvXwqN9CFmJZ7CuTNqKlwqncJsdIRmPHcn157Gtteh5gwBOckv3aK5xh aLGLFTQT9MFGi1NI9UTYN0YlU+XKgXQfBCS7TcRIKFNXgPtOVJS15OnIbOULK765ahIX KTpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589842; x=1717194642; 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=UuNnsh+t+vm30XUZlxFAiVM4aWEhFtEMhV1CFeDWxlE=; b=LuLmoNuOuVPob3ES0hgBy68xgDDIeR1dUuDc/XKg5ordG++vlrVaEWYzKa/D/mnKAN q1ocX0qsIkzT73iZSvNqJSKj3rz9goRRRFtZPdOlqsjmHsDJrNJ/URhDP4qFID3rsDV2 O2v0AK/eaxGRoz5xSKJUJRokykWmvy41XlgFxRABUA6YRNUtF8bPmSgAVloDreA6Lv22 nelyK4KZIFaJ0pPNb0f9QmfDUBRPQ32qnFLApnfuJrcOhQyLPHQPJe5lSkbb94ZgYBzV IQlSiA74rZoBDroxO/qt49xDtTQwn8BarFqvL82G+tJIM3TEgfPtDCa59y7AFCxGz4uV DMfw== X-Gm-Message-State: AOJu0YxK/bakrLPJ4r6994wcijNM2PlnWRDxyVR+SpNBTB1RzoCAaiQz Yl2XuM9WkSk+SC7s44m/SizwvccMrL2NPWmJipYsgx0suHuRMXHaAJ50hA== X-Google-Smtp-Source: AGHT+IGoZoWrujF1sxhjGZa0nox3LxgMmmzlkshUYUH3kXV3GiAXvdAvCZD//FH9ku4wag6EBx28Rw== X-Received: by 2002:a0d:d509:0:b0:615:79df:e7ff with SMTP id 00721157ae682-62a08dbfb49mr35927407b3.29.1716589842052; Fri, 24 May 2024 15:30:42 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30:41 -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 v6 2/8] bpf: enable detaching links of struct_ops objects. Date: Fri, 24 May 2024 15:30:30 -0700 Message-Id: <20240524223036.318800-3-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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 | 53 ++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index 1542dded7489..f2439acd9757 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 = -ENOLINK; + 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 0; + } + 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,22 @@ 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); + + mutex_lock(&update_mutex); err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { + RCU_INIT_POINTER(link->map, NULL); + mutex_unlock(&update_mutex); 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); + mutex_unlock(&update_mutex); return bpf_link_settle(&link_primer); From patchwork Fri May 24 22:30:31 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: 13673771 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.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 833B91272C0 for ; Fri, 24 May 2024 22:30:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589846; cv=none; b=qZuHWR7pSmJ3XwfWpZ7y7/p8T5TzowtosgV1rcT+xU3Rx06JqOBbYS84LOPWNqtdey3K2RD8A6eSfsqD8NLj1HVLv1BWuFsY0GRcZNOlAMYp5C8SCEJ3sWQTbA6QUW/uv0YGYjLfweqvOt0nQsO5LrzdcIaMSwzJpqNJs/rPC7c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589846; c=relaxed/simple; bh=MWDCpjl9BLy3RGzWzjmcFwlOX12n4Lxxz8u31F8OV80=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X+pazO17JMAs70/hPJOjPa+LuPx8qmPI4ccMxUoyJdzsS4Ef12xceamIcOPwbzhuxm68cANPTog9iTbVyaW40Ju6f7wqm41bADKicmj6QRo2YF9bADSs8INh+OtLVPC0H1fym0jeQ9MoZfq1Jv97wlHnxQ/rUEm9dcaX6ihK59A= 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=R9rQli1d; arc=none smtp.client-ip=209.85.128.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="R9rQli1d" Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-627eb38eb1bso33190527b3.1 for ; Fri, 24 May 2024 15:30:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589843; x=1717194643; 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=JsUJ/ZUPCANLU2SOSyqnWVxDYlfF/YXSX4GUUYnQdmY=; b=R9rQli1d2nDZzWwt8WGIgh0O3bPQSZxSWXcmxRoJ9gTzpweHYyNYk4//p2cQyqXENQ mXMU8pTn3uKZdWmYRQncikZd7gSaMuj3zPVoV1sCLCGnlIoH9PA2urQS+bEB14zlOxKP azYA/nk9JZA73USbU8BRC2lEzjG/BrnJjtuNZCJw3cmi7dO+U/IG1mhfxjKOJG/28p3Y KkDD6iIfFoC/99ymq7dLorFhe7aAIl1upXsik2+AvHNfGMVzTtRf40BLpPofH0+yol8r pAIAmkFY+XB/nxDvhe176LoPIKfa1ZItGvbrIjhAU/NbtMWroQ6xKWrifK+/s4OVt8gB bPlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589843; x=1717194643; 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=JsUJ/ZUPCANLU2SOSyqnWVxDYlfF/YXSX4GUUYnQdmY=; b=orLW3wGZl5JpregmWgLtM69b/nGtHlZIvrlDwMmWfkDFXW+6BYYKhJ2oi33w8OkZjf S4g9lsmUweD0xSK40NuRrKCStF26VXOEyTso4IF2ZhpwXaUcdsVjcVph+Gt0dmMstID2 KM6Z/WfElLiZTJZoheZIccRMgerTuGZkzuofQme9s+ZPeCDVcALCbpPy086bTKdPo0sU xOLPaN5cmD5l7HVvRb5zUiwABtCd5fJ6hLa9/mOocouKGztMnFsdnj1dasCgGHPGxgvF YvAXfNurQ/tItN9byYmqyIX20Bn7TjuU16gA0pR9UilLweDTAyLihbm/zi2eNK9KycKq ReSw== X-Gm-Message-State: AOJu0Yyij6WjZd57e3GSRhNMd9nrp+TlMN2+1uHp1srxeWYDm3gWzuQk 0VPizeNhhzFACYMpKZcAGiLwPTM51ChIN+ILCqutZJMuvDgVcJ+pzuNNjg== X-Google-Smtp-Source: AGHT+IEZaSgzWhnvPN9RaaI4guyih9z87LWJxii2++Paqb+ByCdOSyOfosc7f8YnDsDecBwzlRJT7g== X-Received: by 2002:a81:a184:0:b0:627:e281:459a with SMTP id 00721157ae682-62a08d8a4e7mr36446567b3.15.1716589843202; Fri, 24 May 2024 15:30:43 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30:42 -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 v6 3/8] bpf: support epoll from bpf struct_ops links. Date: Fri, 24 May 2024 15:30:31 -0700 Message-Id: <20240524223036.318800-4-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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 | 31 ++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 5 deletions(-) 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 f2439acd9757..855a1b2b6e79 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); + mutex_lock(&update_mutex); err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link); if (err) { diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 13ad74ecf2cd..741f91153fe6 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3150,6 +3150,13 @@ 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; + + return link->ops->poll(file, pts); +} + static const struct file_operations bpf_link_fops = { #ifdef CONFIG_PROC_FS .show_fdinfo = bpf_link_show_fdinfo, @@ -3159,6 +3166,16 @@ static const struct file_operations bpf_link_fops = { .write = bpf_dummy_write, }; +static const struct file_operations bpf_link_fops_poll = { +#ifdef CONFIG_PROC_FS + .show_fdinfo = bpf_link_show_fdinfo, +#endif + .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) { int id; @@ -3201,7 +3218,9 @@ int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer) return id; } - file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC); + file = anon_inode_getfile("bpf_link", + link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, + link, O_CLOEXEC); if (IS_ERR(file)) { bpf_link_free_id(id); put_unused_fd(fd); @@ -3229,7 +3248,9 @@ int bpf_link_settle(struct bpf_link_primer *primer) int bpf_link_new_fd(struct bpf_link *link) { - return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC); + return anon_inode_getfd("bpf-link", + link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops, + link, O_CLOEXEC); } struct bpf_link *bpf_link_get_from_fd(u32 ufd) @@ -3239,7 +3260,7 @@ struct bpf_link *bpf_link_get_from_fd(u32 ufd) if (!f.file) return ERR_PTR(-EBADF); - if (f.file->f_op != &bpf_link_fops) { + if (f.file->f_op != &bpf_link_fops && f.file->f_op != &bpf_link_fops_poll) { fdput(f); return ERR_PTR(-EINVAL); } @@ -4966,7 +4987,7 @@ static int bpf_obj_get_info_by_fd(const union bpf_attr *attr, uattr); else if (f.file->f_op == &btf_fops) err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr); - else if (f.file->f_op == &bpf_link_fops) + else if (f.file->f_op == &bpf_link_fops || f.file->f_op == &bpf_link_fops_poll) err = bpf_link_get_info_by_fd(f.file, f.file->private_data, attr, uattr); else @@ -5101,7 +5122,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr, if (!file) return -EBADF; - if (file->f_op == &bpf_link_fops) { + if (file->f_op == &bpf_link_fops || file->f_op == &bpf_link_fops_poll) { struct bpf_link *link = file->private_data; if (link->ops == &bpf_raw_tp_link_lops) { From patchwork Fri May 24 22:30:32 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: 13673772 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 A419D128808 for ; Fri, 24 May 2024 22:30:45 +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=1716589847; cv=none; b=jp/FDsaq7VcNOtBBf6LLZiLjhtVZI+2quPv/Uc8iBrmnq8A8P6BKTc41SinmrCbN46nShGtILkyRgiOlYan/EKuEgHiDJEfktIqttxBSrYR1rDL8DjQJ3tkkAr/t7QSO5IhDClQ2E9t8V3IYR65qLogJLyfa2m+FMoEpLmVEQms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589847; c=relaxed/simple; bh=pqzVqwQ++8qd6amXfFidlSgABpQlHOg/9TLDkfHrsJk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JfZfkvXPVDwMTqKFqulVA+evrNhXkTodtQXFaC9XJuqdn5MmsmLjqc5SmwEVDq/2bDj+ApNqUDvmRLsnNQSkb5Mg9BMxDFLjz4qigpP4e5T+XSktSoUEczNDfdsOv/nuk/E1V7ft5hh8pNT6Zhhg2CtiJnrs8Yp8kAqczisVbnM= 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=iwDKdfuD; 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="iwDKdfuD" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-62a0849f8e5so13785827b3.2 for ; Fri, 24 May 2024 15:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589844; x=1717194644; 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=404ZgiGbr0O6+NfupkiGmQYModIgXFWM0DgrDzRNv/s=; b=iwDKdfuDJ3xAOEtYLmwMdFYHkrK+cCiOdHdMSPh418JbT0/wEFWyB9bgWyaQbb03HG uaaiecYzsr5dw6lwq+LbErnAAkiw+fqLK2CI0TbhfSHDDObw402S4m+huXXLPFE69jBX imGGqIFvZMA3MoZUbjZTXps6q94Ew9DLQ0ALdre4DoYYhhWYn2Lba7Rwld4gS1g/MITr ctkBJLSpq10JDA4rAQh7gpTMqb+MMCbOazwg7hU6UgXW4KZs2xxBIVm1YhPBEGRZj9OT kShtkyc1fbiW3rC5YO/0pSf1GdekVcSPoGN+O69PqQ4ZcHOOnm0FL0gcOC9vorC+RmjO hhqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589844; x=1717194644; 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=404ZgiGbr0O6+NfupkiGmQYModIgXFWM0DgrDzRNv/s=; b=Gp3w1yUsYjLHhJ9h0c5iNPFq6X6EIA/SVycQjRIGD60ABui4KVujxw0saNXq7Wb23B QZluFajsv3KRyW4IycPVoIVLqdBhzTFK702tjrgoZmR8W7eLggiSin3Z2iarqChPseHH O+KzJmzdOAmKNyJT+oWB8DJbgmVrkzfuVAasU0x7yQ8RQyPHBsJEKqYVbzKqpRZYCXio nflmMHFJO7dSwZm0HXqG//OxTnSuRuIe2JhUVEFSKKB2eMwXYDpgcRfr8t8BkUlcmlXq gBhfxkvcyogJVWmW9DM64OlCudlzZjGRkhP9Zf9FeD3vDXJhyILe12t+2bLoYShzL3bn ZWJA== X-Gm-Message-State: AOJu0YxExqJJNMZPTbWqe8yakmG44EJpem6+mqISHjsjqpvbIPZifpcW J8WRIgVrO5QDvLi3T+deTzbYwWiy7tTTQuHlMJplCl7yQv7uZ4Jzjx35LA== X-Google-Smtp-Source: AGHT+IGzUxGG+1n1fAaspOGsrVwE9by+wFZgf2otRFq0eaBHB48WcLUM5HEti342e8/x7wtzAzT1Hw== X-Received: by 2002:a0d:dfc8:0:b0:615:225e:ac13 with SMTP id 00721157ae682-62a08d8735amr38121407b3.18.1716589844381; Fri, 24 May 2024 15:30:44 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30:44 -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 v6 4/8] bpf: export bpf_link_inc_not_zero. Date: Fri, 24 May 2024 15:30:32 -0700 Message-Id: <20240524223036.318800-5-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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 741f91153fe6..0d59de63540d 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -5432,10 +5432,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 24 22:30:33 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: 13673773 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (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 CF8AC1272C0 for ; Fri, 24 May 2024 22:30:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589848; cv=none; b=orwAOYNmEbUYVZSlr2+uS+UDzTCrH2eph5pH/QnNTYDHobhMA2NmeejJu/eIc8ZsDcEcDvd1GMhTSHszxNcYrZPiTpRYkMvSCvQk9kfBG2EfGkSy1ZSb9J4wfVaum2LiQxPH3T8oUykzr4eX6LZ2qrjS20oJcnv7mAEH0g6tnhE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589848; c=relaxed/simple; bh=0moVWm5H0/PxkymYi1Wkc4v/Yc9p3AxXBq0vFZgiFh0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UizFSX8RjJDBk75+2rnYhm2XPUzANO386/7Z2Upw8jdIftUKFtHt7/hxGCKZUSBCd/x4As3Ycsb1UxQD5GUAvrnHhlG9D/MrtrX7PerU10dHL5+J3jQfDnd1rApVkiMtjhXenglPZmym1lIm648Tj1oW+F1a02EewnBOpfl7Yko= 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=QJ2eNs45; arc=none smtp.client-ip=209.85.128.176 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="QJ2eNs45" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-62a08438b9aso13897657b3.3 for ; Fri, 24 May 2024 15:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589845; x=1717194645; 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=QJ2eNs45qe1QnVeu83gi8UkEwETnOXHsvI4pvUQxs37GyBP0DquLNGGaToFqqrUTB6 cirlQhX6oKPzZu7VLcjALdZtrX7rb+X8plCFO6DjaECWGINIhHyUXFmyrcvQbrKIxRj+ 7An5P+HBFKjhwq9M3Nffpvc0WWpxCSIFiGSJiTpT2c+iGytzvOenTWZj3Y0qACncGgpS GhhLiXaQGZI0k6jInhYYsyp0UPj7nNB32kBRaCnHmAH56cyNSPYHHuWzcLt/+Bb8Emp+ lCpOU0NZZACZXQT6do+cjV0WzcJI0A+4ojDpkzAbsszmnMSSnbvkUDllMOGHpxGelCK3 YkIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589845; x=1717194645; 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=R3+zLQJBQyv4LECTBCpnTiHphVo/GLWc0VXJOkoMvkQ11+m4QmFmmdQaNo8JZFmMBH XxvbHgJ1pAEizBofbb1WpjwWT1hSE1y1SsIDEpqwKfi3ezdjn7LeDlpPXOeh8i8KoFYy WgJb8J1Owcuxqcuu0YWYib743QjTFkirbb/rluzdbHUF+iyVM3LAJVQ/Gpt+DCgB4+uT +YwL/2kNAoS8NW4sFrEdh7MmJ4S2rW5w/z9cWkhecmITgCp5OSqiOlryBovhBwnwFRJ6 stx2vy8SCFlAF8U77UhZFUqMx2xPcu65dkaFMHizjRaimqYqPK6aJcGEi4uAqWHSbefz szyA== X-Gm-Message-State: AOJu0YyN1cx5JU1JbApXIIqMHuGe9GeWmz+BJ3X5xA4hSCaNOMcAyMTm PCT5BRuWpVlnY7nLdjSs2Dbf2SI3Xq8eu2WBy/z1CjmIDvozLnIb+HF7DQ== X-Google-Smtp-Source: AGHT+IHd+DZ5SmbSjmwv4vYpMyTzkzLTJ6PRnopG6gcfDWU3Ir/juikRj2x5DCbUnctsbSfpGSFt1g== X-Received: by 2002:a81:af02:0:b0:61b:3356:d28c with SMTP id 00721157ae682-62a08d754d1mr34090457b3.2.1716589845511; Fri, 24 May 2024 15:30:45 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30: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 v6 5/8] selftests/bpf: test struct_ops with epoll Date: Fri, 24 May 2024 15:30:33 -0700 Message-Id: <20240524223036.318800-6-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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 24 22:30:34 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: 13673774 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 EEA2F128808 for ; Fri, 24 May 2024 22:30:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589849; cv=none; b=MxQ7u9xycQEkzs9fFRR0/yAHaH3+/RSounGxI7IxyvI5D3N4mvvW01MmaH+rcQFMT2ljDc0iKjuOgGPWPzJk5Z7QYdTWM/zk761Qu8+I4FRjm2hOhBcGbBjoZnnciqdIfz4D4BjGkA/Gub5aSE1LZawpuWfunpx8JhGEmKWgYGI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589849; c=relaxed/simple; bh=M/Uv8vWTI1pT9gejmLQvJRT763bIa7cjhVoGiJeMPyo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X3iEbE4rjJ3puJ9Uj1//gh6ejrAhOR4iMk4GgdF7wUbz4Va1P0g0L2FV2+jEHl2wrHxlhFi9PvkSThlnkmSca40HGWC3Gad264paMBMfPIqyXVBcbNBm60e2FPk3q9elfn33MXFVBW1ooWp1UsM5cbunt+i9+dfWHOaQlwCGYGQ= 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=lmmnnmF3; arc=none smtp.client-ip=209.85.128.180 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="lmmnnmF3" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-62a088f80f6so10217097b3.0 for ; Fri, 24 May 2024 15:30:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589847; x=1717194647; 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=lmmnnmF3i/stPRMK6AJarU/lvy76IPWmkUKVirvA6PmY0aUnOmtmt+mDDkkx0x9A+j 86IyFUBClPvPH98myjLo8ARNWxSTawKr6zA7gDpK7IfkWoV/B/X2GMs1X4I/Q1ckgXFK 7bSky5XZ4xf8vLPmKk6JwFHVS1pqfyXgmzsRY86wYGx7xgxbiBRyhLvKOrvdhat+rod5 O2Yu/Wk4HWJhzeyuVwb03xZEgCci698UtyCFdI8ldeIFl2IVc1oSLpqtLtBrssRjk5iV q9VctBGs3pq42jYhcIyvZ2uJze1zCokVraaNZoQYtk5cUP0sMcBgFT2LL7DK3OwcVoIX mF7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589847; x=1717194647; 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=V3np7AU7PrihmnEFWcdkqHCLtFfkz9P5KhMGFUpgQvf1Raw0TCjHyfWKt6cA5Bhj1u M/HMIWRp7CQRH8wDLnhoOCmdsBUfY0TEDzIO4k4lRcU1idNwUCmMHNa7uNJWVIfoJJCu irqpYDrZ0lJKQ9Rn7JILL/NYf4MJYW/wh++yfTAldu8MJp2bARhE4B3jzeaNjf1kVDZ8 3ikaBcsWWgwu7JwMaFFBjGp4Mdl3iwuwnnW/OaNy+yh2Niu1On2LRXRasTg9i+lkgXAN HDeiPID1c+uDksApsVXQOAy58r2JxKTN7dGsdsPnDa64dJBs2VIu1oMxKqM98jeJ8uOJ 5JqA== X-Gm-Message-State: AOJu0YzzN+Oc3tVG8VptVUXnu4vASFJX84Zanb1m0jBIPls5PmsBZr1I tqEFt+oCFGmXc1tiLwyLxlJUyBLjSkeYOZE1gC6UOo9Mk/mUZ+14n2txfg== X-Google-Smtp-Source: AGHT+IGWWqHT9VLqKqeVu2+8K6WbYyEmsj62O5Vue2zVBuNuZwchYYBYv1cPXm0vBCZLLZbmRE3A3w== X-Received: by 2002:a0d:ea0a:0:b0:627:e3ba:2ad7 with SMTP id 00721157ae682-62a0732cef8mr28619727b3.9.1716589846647; Fri, 24 May 2024 15:30:46 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30: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 v6 6/8] selftests/bpf: detach a struct_ops link from the subsystem managing it. Date: Fri, 24 May 2024 15:30:34 -0700 Message-Id: <20240524223036.318800-7-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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 Fri May 24 22:30:35 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: 13673775 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 8E51E8614D for ; Fri, 24 May 2024 22:30:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589851; cv=none; b=RKrfMLGoj37KrVzulFpgcTgTcPjwDXEajUYAQnJtWFJ28+t4MZny+0vB3mfxCM37U5WC7eXKgLP3a7JgC613xvOeNLF9tuR7pxaJFHtrW/rnS5WzTdfVI7+UEuLX66xkBeQ0+hiu/1hCob5wFpiHD2GOJgF+jOE8qyqV2ARLQaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589851; c=relaxed/simple; bh=AOuolpRVxv8BKwP/7SBIG5zYThDowJbKd4TrLSOyZRE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OH1PUtbwSmXJhjlzmiKT+nslK1eCt8IBSjGTcvyQGaB34nFHL0kDG164FvKwTLyfNd7NLisfLMLDTDGnIo2o7ZeQi8Ol6SQq1p+zq+pe1gIg1yjz4A8sQ2TbNGCXKIpTwsXzhnMXmlNuNICKiW/cfhFee+hT+B6c1LZNHE2L0IY= 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=jTwd+6cS; arc=none smtp.client-ip=209.85.128.180 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="jTwd+6cS" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-62a088f80f6so10217347b3.0 for ; Fri, 24 May 2024 15:30:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589848; x=1717194648; 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=jTwd+6cSXAIMZJRw7jJ5tdN/386+0tBmfFdqPptan8d8Y2+Eqcp6wJL1dtb5djqPlf PTDuQK2UTRm9zrRLGPQ2rbdbwQ0wDF0Moy9B0LHerFgQIh4WI1X3P0hP2ylep/zJjQoD 7OZAD6Zay5zEhUYC2WrpqvG1GSPkKcG8KmiyTshWc+/PraP7LZFscnsHK3BIX9cmgaG9 HSZYL6To5OA9UOKCMlPtg2dkyCjqthdWsIIqDZVpKlNWb+hsUGi2qlAm1xk2HejcFGlo LsJsThwCg4txr7GnxtMe/1V99KHMufopeBQzUFEUZ0SoOv3URjePEAEMrMJK3VDj9CQ9 z9aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589848; x=1717194648; 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=ismsoOXXk2B2OMef5L4sO/3j7prPU+fw556sliBXR2GxQpZETjmsyO4Novg71XGokN 3Zp4x6ux/ISzN7bVH+cG/DOva5brYBd+wBCU+fEicLG1WSVJtb9kFhloSLJsS30M05NQ jopZfZ/z+e6QTBmo9CIgXR0kI6tRo+9bDQmjM/HwGjzA7WtY5roYopZZi/uskrE6cQHk i9CWsT9W6wkcgHd1TYA1m+wNZv9PCeraBk3+AhMBI55eOCootifsckEVO/eN/3Kl88aV z9D+FbxoSUsmD5k4Snx/sACW6/i6Id9h/TZYTdaC0DHdHd9FGpu+zJM38EjNWKhrNWg9 ta2A== X-Gm-Message-State: AOJu0Ywo8SwWi9qRuE281epbRHJ06MWElvLEvKoLNYNdxrhIKbdifAhY 9QDc4hLdtGijyQf4buZUf7y1yl0mYXbxGYkdz4ahWdb9BJWbxCob6CVZQg== X-Google-Smtp-Source: AGHT+IEHjyhFsORbOlXJgzYqXQzU6lvtDGkA2el/SjMUQw58x1WzOFbJ32gzitMLSkWfw2aMQ22Vlg== X-Received: by 2002:a0d:ea0a:0:b0:627:e3ba:2ad7 with SMTP id 00721157ae682-62a0732cef8mr28620187b3.9.1716589847967; Fri, 24 May 2024 15:30:47 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30: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 v6 7/8] selftests/bpf: make sure bpf_testmod handling racing link destroying well. Date: Fri, 24 May 2024 15:30:35 -0700 Message-Id: <20240524223036.318800-8-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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(); } From patchwork Fri May 24 22:30: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: 13673776 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (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 7EDAE128808 for ; Fri, 24 May 2024 22:30:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589851; cv=none; b=rlJPQFqcbX2sv5tM9uJVjg+37x6u871fjxH3DMvJFG2aAOf8R4DjmGbqXMl/KsEYl/ga4FLmnzw5vzogg+M3SMBn4BqhuTldN9nJEHzxUOKfAHCny9z4GPPNfDna53T7KnXPNHQpnLJzgPgEeLaZ+n28vwFqPc8sNcoEFA1M4jM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716589851; c=relaxed/simple; bh=EG5Ebj9lpmdl+7pnmhFq0IBANIQ2FH4ulS7Se5ZXRTE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=b5XGbWVmA/q+IJFENwo8WZ2TsDMaGFCPWy4BtJiJStVIPaTDBhtU/pnJym5H4BKsmvn54ovCn2tE3bbtEvVAA89HUR1KPLIYfUd9aV662gSP9x3cMGMd+EwKfg/QmbTtaH/Zz1V8aMKR0BYAYBr82N+cmDzR6d4HemS7rqCoTa8= 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=BWZ9aQuK; arc=none smtp.client-ip=209.85.128.178 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="BWZ9aQuK" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-627f46fbe14so32666387b3.2 for ; Fri, 24 May 2024 15:30:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716589849; x=1717194649; 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=j/z09TdUY2NxiMoECTkZ6AWPRP0HpyoEfvAwYbCuC2o=; b=BWZ9aQuKvfjgPxpAE39GqLQpMRTdv5QE4zwuVm8pJEtDpFSnX3krgiYFax7eap3Om6 bZazZCJGnmBx1ZbjEW/PJTM3uw2q8Kva8qmgtlCl8AGhir7drnsTejNkRk49ZayXYuqo d4qvr4/pQ0Uyy72Wx8iP0+m8tvdTVttBK8CJaIntdhY667ZRURycevw3kKs/O6exGyFR jfjmRZ2OsK8F1Du0bwqFB4PNwyB6jqa77lzv69Lk5jkODiPaV/QcF65zEwHvusgVR8L4 s+2GqYoiLCWwC8IhKKZYkQzoWSeOQUurbkVbLQuEILBfec0ZLpA11RLAqrFgztNJpox7 Uurw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716589849; x=1717194649; 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=j/z09TdUY2NxiMoECTkZ6AWPRP0HpyoEfvAwYbCuC2o=; b=wfs1D/zS78f9T+DIiBwYItg/v5N88RnrYnaY7A8sDeUizXvjQZge5qjAZn3MeyOL9e SPn1dtMf00cKxzUEJ5aRuwERDz0LYc8DB4m1Y7j4/O92CmyW0mpLaIhbqZS5epV1wiHS MruonkU6K1YzwfEx4MFGYofDbbhbTI+y5u4YCMjb+j97lU+vYxXLfchIGHCtgMfT1Iga 5RJAVN9fSwB3XwXZCnG+maw1XV072sepuAq0isXVj9cIuOsoOEA7nCACRgsxLvhZ//x0 TEVV7hQADbsdViW7v8Bn3nXVmWRKHUJ5nSwcXBox9qTqKHVKG/v+lEpwDzHdKQ9NIgdj x1JA== X-Gm-Message-State: AOJu0Yys8YS2zwNiA99aQyLdZngFVZH0rPMNa/9QBBoxlvQw7so8aoWv 3s/hfnVIpfn7xxFqLIMbJ8Gh4JD21OSnsPWuHNFlY3bhpHC836lQZ+ij1Q== X-Google-Smtp-Source: AGHT+IF4gN1hAcJOcRU2C8cxPaIARkAWws1CTF8cgfHSKRBv27814IIHe9u/lXAQuDDGPBiuzMMdbA== X-Received: by 2002:a0d:e20e:0:b0:61b:3304:b702 with SMTP id 00721157ae682-62a08d744c7mr34319217b3.5.1716589849217; Fri, 24 May 2024 15:30:49 -0700 (PDT) Received: from kickker.attlocal.net ([2600:1700:6cf8:1240:6aeb:e91b:f49d:e77d]) by smtp.gmail.com with ESMTPSA id 00721157ae682-62a0a3bfa19sm4169987b3.44.2024.05.24.15.30.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 15:30:48 -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 , Quentin Monnet Subject: [PATCH bpf-next v6 8/8] bpftool: Change pid_iter.bpf.c to comply with the change of bpf_link_fops. Date: Fri, 24 May 2024 15:30:36 -0700 Message-Id: <20240524223036.318800-9-thinker.li@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240524223036.318800-1-thinker.li@gmail.com> References: <20240524223036.318800-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 To support epoll, a new instance of file_operations, bpf_link_fops_poll, has been added for links that support epoll. The pid_iter.bpf.c checks f_ops for links and other BPF objects. The check should fail for struct_ops links without this patch. Acked-by: Quentin Monnet Signed-off-by: Kui-Feng Lee --- tools/bpf/bpftool/skeleton/pid_iter.bpf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c index 7bdbcac3cf62..948dde25034e 100644 --- a/tools/bpf/bpftool/skeleton/pid_iter.bpf.c +++ b/tools/bpf/bpftool/skeleton/pid_iter.bpf.c @@ -29,6 +29,7 @@ enum bpf_link_type___local { }; extern const void bpf_link_fops __ksym; +extern const void bpf_link_fops_poll __ksym __weak; extern const void bpf_map_fops __ksym; extern const void bpf_prog_fops __ksym; extern const void btf_fops __ksym; @@ -84,7 +85,11 @@ int iter(struct bpf_iter__task_file *ctx) fops = &btf_fops; break; case BPF_OBJ_LINK: - fops = &bpf_link_fops; + if (&bpf_link_fops_poll && + file->f_op == &bpf_link_fops_poll) + fops = &bpf_link_fops_poll; + else + fops = &bpf_link_fops; break; default: return 0;