diff mbox series

[v4,1/3] genetlink: Add per family bind/unbind callbacks

Message ID 20240212161615.161935-2-stanislaw.gruszka@linux.intel.com (mailing list archive)
State Changes Requested, archived
Headers show
Series thermal/netlink/intel_hfi: Enable HFI feature only when required | expand

Commit Message

Stanislaw Gruszka Feb. 12, 2024, 4:16 p.m. UTC
Add genetlink family bind()/unbind() callbacks when adding/removing
multicast group to/from netlink client socket via setsockopt() or
bind() syscall.

They can be used to track if consumers of netlink multicast messages
emerge or disappear. Thus, a client implementing callbacks, can now
send events only when there are active consumers, preventing unnecessary
work when none exist.

Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
---
 include/net/genetlink.h |  4 ++++
 net/netlink/genetlink.c | 30 ++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

Comments

Jakub Kicinski Feb. 13, 2024, 1:07 a.m. UTC | #1
On Mon, 12 Feb 2024 17:16:13 +0100 Stanislaw Gruszka wrote:
> Add genetlink family bind()/unbind() callbacks when adding/removing
> multicast group to/from netlink client socket via setsockopt() or
> bind() syscall.
> 
> They can be used to track if consumers of netlink multicast messages
> emerge or disappear. Thus, a client implementing callbacks, can now
> send events only when there are active consumers, preventing unnecessary
> work when none exist.
> 
> Suggested-by: Jakub Kicinski <kuba@kernel.org>
> Signed-off-by: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>

LGTM! genetlink code is a bit hot lately, to avoid any conflicts can
I put the first patch (or all of them) on a shared branch for both
netdev and PM to pull in? Once the other two patches are reviewed,
obviously.
Srinivas Pandruvada Feb. 13, 2024, 1:52 a.m. UTC | #2
On Mon, 2024-02-12 at 17:07 -0800, Jakub Kicinski wrote:
> On Mon, 12 Feb 2024 17:16:13 +0100 Stanislaw Gruszka wrote:
> > Add genetlink family bind()/unbind() callbacks when adding/removing
> > multicast group to/from netlink client socket via setsockopt() or
> > bind() syscall.
> > 
> > They can be used to track if consumers of netlink multicast
> > messages
> > emerge or disappear. Thus, a client implementing callbacks, can now
> > send events only when there are active consumers, preventing
> > unnecessary
> > work when none exist.
> > 
> > Suggested-by: Jakub Kicinski <kuba@kernel.org>
> > Signed-off-by: Stanislaw Gruszka
> > <stanislaw.gruszka@linux.intel.com>
> 
> LGTM! genetlink code is a bit hot lately, to avoid any conflicts can
> I put the first patch (or all of them) on a shared branch for both
> netdev and PM to pull in? Once the other two patches are reviewed,
> obviously.
> 
If netlink maintainers are happy with the 1/3, you can take 1/3. Once
merged, the PM patches can go separately as they need 1/3.

Hi Daniel,
Please look at 2/3 and also 3/3 if you can.

Thanks,
Srinivas
Jiri Pirko Feb. 13, 2024, 9:11 a.m. UTC | #3
Mon, Feb 12, 2024 at 05:16:13PM CET, stanislaw.gruszka@linux.intel.com wrote:
>Add genetlink family bind()/unbind() callbacks when adding/removing
>multicast group to/from netlink client socket via setsockopt() or
>bind() syscall.
>
>They can be used to track if consumers of netlink multicast messages
>emerge or disappear. Thus, a client implementing callbacks, can now
>send events only when there are active consumers, preventing unnecessary
>work when none exist.
>
>Suggested-by: Jakub Kicinski <kuba@kernel.org>
>Signed-off-by: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>

Reviewed-by: Jiri Pirko <jiri@nvidia.com>
diff mbox series

Patch

diff --git a/include/net/genetlink.h b/include/net/genetlink.h
index e61469129402..ecadba836ae5 100644
--- a/include/net/genetlink.h
+++ b/include/net/genetlink.h
@@ -41,6 +41,8 @@  struct genl_info;
  *	do additional, common, filtering and return an error
  * @post_doit: called after an operation's doit callback, it may
  *	undo operations done by pre_doit, for example release locks
+ * @bind: called when family multicast group is added to a netlink socket
+ * @unbind: called when family multicast group is removed from a netlink socket
  * @module: pointer to the owning module (set to THIS_MODULE)
  * @mcgrps: multicast groups used by this family
  * @n_mcgrps: number of multicast groups
@@ -84,6 +86,8 @@  struct genl_family {
 	void			(*post_doit)(const struct genl_split_ops *ops,
 					     struct sk_buff *skb,
 					     struct genl_info *info);
+	int			(*bind)(int mcgrp);
+	void			(*unbind)(int mcgrp);
 	const struct genl_ops *	ops;
 	const struct genl_small_ops *small_ops;
 	const struct genl_split_ops *split_ops;
diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
index 8c7af02f8454..50ec599a5cff 100644
--- a/net/netlink/genetlink.c
+++ b/net/netlink/genetlink.c
@@ -1836,6 +1836,9 @@  static int genl_bind(struct net *net, int group)
 		    !ns_capable(net->user_ns, CAP_SYS_ADMIN))
 			ret = -EPERM;
 
+		if (family->bind)
+			family->bind(i);
+
 		break;
 	}
 
@@ -1843,12 +1846,39 @@  static int genl_bind(struct net *net, int group)
 	return ret;
 }
 
+static void genl_unbind(struct net *net, int group)
+{
+	const struct genl_family *family;
+	unsigned int id;
+
+	down_read(&cb_lock);
+
+	idr_for_each_entry(&genl_fam_idr, family, id) {
+		int i;
+
+		if (family->n_mcgrps == 0)
+			continue;
+
+		i = group - family->mcgrp_offset;
+		if (i < 0 || i >= family->n_mcgrps)
+			continue;
+
+		if (family->unbind)
+			family->unbind(i);
+
+		break;
+	}
+
+	up_read(&cb_lock);
+}
+
 static int __net_init genl_pernet_init(struct net *net)
 {
 	struct netlink_kernel_cfg cfg = {
 		.input		= genl_rcv,
 		.flags		= NL_CFG_F_NONROOT_RECV,
 		.bind		= genl_bind,
+		.unbind		= genl_unbind,
 		.release	= genl_release,
 	};