thermal: netlink: Add thermal_group_has_listeners() helper
authorStanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
Thu, 28 Dec 2023 10:02:48 +0000 (11:02 +0100)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 28 Dec 2023 18:56:40 +0000 (19:56 +0100)
Add a helper function to check if there are listeners for
thermal_gnl_family multicast groups.

For now use it to avoid unnecessary allocations and sending
thermal genl messages when there are no recipients.

In the future, in conjunction with (not yet implemented) notification
of change in the netlink socket group membership, this helper can be
used to open/close hardware interfaces based on the presence of
user space subscribers.

Signed-off-by: Stanislaw Gruszka <stanislaw.gruszka@linux.intel.com>
Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/thermal/thermal_netlink.c

index aca36c4ddbf3adc7ce4dc40f3e72a0cd7e1c97d0..332052e24a86c07a7b3af1f9a5c280d08b6374a8 100644 (file)
@@ -76,6 +76,11 @@ typedef int (*cb_t)(struct param *);
 
 static struct genl_family thermal_gnl_family;
 
+static int thermal_group_has_listeners(enum thermal_genl_multicast_groups group)
+{
+       return genl_has_listeners(&thermal_gnl_family, &init_net, group);
+}
+
 /************************** Sampling encoding *******************************/
 
 int thermal_genl_sampling_temp(int id, int temp)
@@ -83,6 +88,9 @@ int thermal_genl_sampling_temp(int id, int temp)
        struct sk_buff *skb;
        void *hdr;
 
+       if (!thermal_group_has_listeners(THERMAL_GENL_SAMPLING_GROUP))
+               return 0;
+
        skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
        if (!skb)
                return -ENOMEM;
@@ -280,6 +288,9 @@ static int thermal_genl_send_event(enum thermal_genl_event event,
        int ret = -EMSGSIZE;
        void *hdr;
 
+       if (!thermal_group_has_listeners(THERMAL_GENL_EVENT_GROUP))
+               return 0;
+
        msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
        if (!msg)
                return -ENOMEM;