diff mbox series

[RFC,2/2] kunit: Add APIs for managing devices

Message ID 20230325043104.3761770-3-davidgow@google.com
State New
Headers show
Series KUnit device API proposal | expand

Commit Message

David Gow March 25, 2023, 4:31 a.m. UTC
Tests for drivers often require a struct device to pass to other
functions. While it's possible to create these with
root_device_register(), or to use something like a platform device, this
is both a misuse of those APIs, and can be difficult to clean up after,
for example, a failed assertion.

Add two KUnit-specific functions for registering and unregistering a
struct device:
- kunit_device_register()
- kunit_device_unregister()

These behave similarly to root_device_register() and
root_device_unregister() except:
- They take a struct kunit pointer with a test context.
- They do not create a root device directory in sysfs.
- The device will automatically be unregistered when the test exits
  (unless it has already been unregistered using
  kunit_device_unregister())
- The device name is set to <test-name>.<device-name>.

This API can be extended in the future to, for example, add these
devices to a KUnit bus as tests begin to require that functionality.

Signed-off-by: David Gow <davidgow@google.com>
---
 include/kunit/device.h | 25 ++++++++++++++++
 lib/kunit/Makefile     |  1 +
 lib/kunit/device.c     | 68 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 include/kunit/device.h
 create mode 100644 lib/kunit/device.c

Comments

Greg KH March 25, 2023, 6:28 a.m. UTC | #1
On Sat, Mar 25, 2023 at 12:31:04PM +0800, David Gow wrote:
> Tests for drivers often require a struct device to pass to other
> functions. While it's possible to create these with
> root_device_register(), or to use something like a platform device, this
> is both a misuse of those APIs, and can be difficult to clean up after,
> for example, a failed assertion.
> 
> Add two KUnit-specific functions for registering and unregistering a
> struct device:
> - kunit_device_register()
> - kunit_device_unregister()
> 
> These behave similarly to root_device_register() and
> root_device_unregister() except:
> - They take a struct kunit pointer with a test context.
> - They do not create a root device directory in sysfs.

But they show up in the root directory in sysfs, in /sys/devices/ which
is not a good place to be.  Why not make them part of a class, perhaps
called 'kunit', so that they will be in a contained place?

thanks,

greg k-h
Maxime Ripard March 27, 2023, 1:57 p.m. UTC | #2
Hi,

On Sat, Mar 25, 2023 at 12:31:04PM +0800, David Gow wrote:
> Tests for drivers often require a struct device to pass to other
> functions. While it's possible to create these with
> root_device_register(), or to use something like a platform device, this
> is both a misuse of those APIs, and can be difficult to clean up after,
> for example, a failed assertion.
> 
> Add two KUnit-specific functions for registering and unregistering a
> struct device:
> - kunit_device_register()
> - kunit_device_unregister()

If kunit_device_register() registers an action to clean up after the
test has ran, I'm not sure why do we need kunit_device_unregister()

I guess the typical test would just call kunit_device_register() and
be done with it, right?

Maxime
diff mbox series

Patch

diff --git a/include/kunit/device.h b/include/kunit/device.h
new file mode 100644
index 000000000000..19a35b5e4e59
--- /dev/null
+++ b/include/kunit/device.h
@@ -0,0 +1,25 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * KUnit basic device implementation
+ *
+ * Implementation of struct kunit_device helpers.
+ *
+ * Copyright (C) 2023, Google LLC.
+ * Author: David Gow <davidgow@google.com>
+ */
+
+#ifndef _KUNIT_DEVICE_H
+#define _KUNIT_DEVICE_H
+
+#if IS_ENABLED(CONFIG_KUNIT)
+
+#include <kunit/test.h>
+
+struct kunit_device;
+
+struct device *kunit_device_register(struct kunit *test, const char *name);
+void kunit_device_unregister(struct kunit *test, struct device *dev);
+
+#endif
+
+#endif
diff --git a/lib/kunit/Makefile b/lib/kunit/Makefile
index cb417f504996..b9bd059269ed 100644
--- a/lib/kunit/Makefile
+++ b/lib/kunit/Makefile
@@ -6,6 +6,7 @@  kunit-objs +=				test.o \
 					string-stream.o \
 					assert.o \
 					try-catch.o \
+					device.o \
 					executor.o
 
 ifeq ($(CONFIG_KUNIT_DEBUGFS),y)
diff --git a/lib/kunit/device.c b/lib/kunit/device.c
new file mode 100644
index 000000000000..ce87b7c40d9b
--- /dev/null
+++ b/lib/kunit/device.c
@@ -0,0 +1,68 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit basic device implementation
+ *
+ * Implementation of struct kunit_device helpers.
+ *
+ * Copyright (C) 2023, Google LLC.
+ * Author: David Gow <davidgow@google.com>
+ */
+
+#include <linux/device.h>
+
+#include <kunit/test.h>
+#include <kunit/resource.h>
+
+/* A device owned by a KUnit test. */
+struct kunit_device {
+	struct device dev;
+	struct kunit *owner;
+};
+
+static inline struct kunit_device *to_kunit_device(struct device *d)
+{
+	return container_of(d, struct kunit_device, dev);
+}
+
+static void kunit_device_release(struct device *d)
+{
+	kfree(to_kunit_device(d));
+}
+
+struct device *kunit_device_register(struct kunit *test, const char *name)
+{
+	struct kunit_device *kunit_dev;
+	int err = -ENOMEM;
+
+	kunit_dev = kzalloc(sizeof(struct kunit_device), GFP_KERNEL);
+	if (!kunit_dev)
+		return ERR_PTR(err);
+
+	kunit_dev->owner = test;
+
+	err = dev_set_name(&kunit_dev->dev, "%s.%s", test->name, name);
+	if (err) {
+		kfree(kunit_dev);
+		return ERR_PTR(err);
+	}
+
+	kunit_dev->dev.release = kunit_device_release;
+
+	err = device_register(&kunit_dev->dev);
+	if (err) {
+		put_device(&kunit_dev->dev);
+		return ERR_PTR(err);
+	}
+
+	kunit_defer(test, (kunit_defer_function_t)device_unregister, &kunit_dev->dev, GFP_KERNEL);
+
+	return &kunit_dev->dev;
+}
+EXPORT_SYMBOL_GPL(kunit_device_register);
+
+void kunit_device_unregister(struct kunit *test, struct device *dev)
+{
+	kunit_defer_trigger(test, (kunit_defer_function_t)device_unregister, dev);
+}
+EXPORT_SYMBOL_GPL(kunit_device_unregister);
+