git: b71e429d5478 - stable/14 - LinuxKPI: Add devm_device_add_group to linux/device.h

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Thu, 01 Aug 2024 22:28:01 UTC
The branch stable/14 has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=b71e429d54780ddc447b74d551eac38761847360

commit b71e429d54780ddc447b74d551eac38761847360
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2024-07-21 13:09:12 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2024-08-01 21:15:28 +0000

    LinuxKPI: Add devm_device_add_group to linux/device.h
    
    devm_device_add_group creates a managed attribute group for a device.
    
    Sponsored by:   Serenity Cyber Security, LLC
    MFC after:      1 week
    Reviewed by:    manu
    Differential Revision:  https://reviews.freebsd.org/D45845
    
    (cherry picked from commit a1181662d3707d8165d7153a0aa5d76ea1ddc11a)
---
 sys/compat/linuxkpi/common/include/linux/device.h |  4 +++
 sys/compat/linuxkpi/common/src/linux_compat.c     | 30 +++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/device.h b/sys/compat/linuxkpi/common/include/linux/device.h
index 98c16212512d..de7e66d9a4ad 100644
--- a/sys/compat/linuxkpi/common/include/linux/device.h
+++ b/sys/compat/linuxkpi/common/include/linux/device.h
@@ -700,4 +700,8 @@ int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), vo
 #define	devm_add_action_or_reset(dev, action, data)	\
 	lkpi_devm_add_action_or_reset(dev, action, data)
 
+int lkpi_devm_device_add_group(struct device *dev, const struct attribute_group *group);
+#define	devm_device_add_group(dev, group)	\
+	lkpi_devm_device_add_group(dev, group)
+
 #endif	/* _LINUXKPI_LINUX_DEVICE_H_ */
diff --git a/sys/compat/linuxkpi/common/src/linux_compat.c b/sys/compat/linuxkpi/common/src/linux_compat.c
index 90d03d480089..ec4e59f66e7f 100644
--- a/sys/compat/linuxkpi/common/src/linux_compat.c
+++ b/sys/compat/linuxkpi/common/src/linux_compat.c
@@ -2615,6 +2615,36 @@ device_can_wakeup(struct device *dev)
 	return (false);
 }
 
+static void
+devm_device_group_remove(struct device *dev, void *p)
+{
+	const struct attribute_group **dr = p;
+	const struct attribute_group *group = *dr;
+
+	sysfs_remove_group(&dev->kobj, group);
+}
+
+int
+lkpi_devm_device_add_group(struct device *dev,
+    const struct attribute_group *group)
+{
+	const struct attribute_group **dr;
+	int ret;
+
+	dr = devres_alloc(devm_device_group_remove, sizeof(*dr), GFP_KERNEL);
+	if (dr == NULL)
+		return (-ENOMEM);
+
+	ret = sysfs_create_group(&dev->kobj, group);
+	if (ret == 0) {
+		*dr = group;
+		devres_add(dev, dr);
+	} else
+		devres_free(dr);
+
+	return (ret);
+}
+
 #if defined(__i386__) || defined(__amd64__)
 bool linux_cpu_has_clflush;
 struct cpuinfo_x86 boot_cpu_data;