Skip to contentSkip to conversation

gpio: implement the configfs testing module

PATCH Superseded Bartosz Golaszewski 5 participants 37 messages Mar 4, 2021 – Mar 9, 2021

Subsystems (8)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v2 cover gpio: implement the configfs testing module

From: Bartosz Golaszewski <[email protected]>

This series adds a new GPIO testing module based on configfs committable items and sysfs. The goal is to provide a testing driver that will be configurable at runtime (won't need module reload) and easily extensible. The control over the attributes is also much more fine-grained than in gpio-mockup.

This series also contains a respin of the patches I sent separately to the configfs maintainers - these patches implement the concept of committable items that was well defined for a long time but never actually completed.

Apart from the new driver itself, its selftests and the configfs patches, this series contains some changes to the bitmap API - most importantly: it adds devres managed variants of bitmap_alloc() and bitmap_zalloc().

v1 -> v2:
- add selftests for gpio-sim
- add helper programs for selftests
- update the configfs rename callback to work with the new API introduced in v5.11
- fix a missing quote in the documentation
- use !! whenever using bits operation that are required to return 0 or 1
- use provided bitmap API instead of reimplementing copy or fill operations
- fix a deadlock in gpio_sim_direction_output()
- add new read-only configfs attributes for mapping of configfs items to GPIO device names
- and address other minor issues pointed out in reviews of v1

Bartosz Golaszewski (12):
configfs: increase the item name length
configfs: use (1UL << bit) for internal flags
configfs: implement committable items
samples: configfs: add a committable group
lib: bitmap: remove the 'extern' keyword from function declarations
lib: bitmap: order includes alphabetically
lib: bitmap: provide devm_bitmap_alloc() and devm_bitmap_zalloc()
drivers: export device_is_bound()
gpio: sim: new testing module
selftests: gpio: provide a helper for reading chip info
selftests: gpio: add a helper for reading GPIO line names
selftests: gpio: add test cases for gpio-sim

 Documentation/admin-guide/gpio/gpio-sim.rst   |  72 ++
 Documentation/filesystems/configfs.rst        |   6 +-
 drivers/base/dd.c                             |   1 +
 drivers/gpio/Kconfig                          |   8 +
 drivers/gpio/Makefile                         |   1 +
 drivers/gpio/gpio-sim.c                       | 878 ++++++++++++++++++
 fs/configfs/configfs_internal.h               |  22 +-
 fs/configfs/dir.c                             | 245 ++++-
 include/linux/bitmap.h                        | 129 +--
 include/linux/configfs.h                      |   3 +-
 lib/bitmap.c                                  |  42 +-
 samples/configfs/configfs_sample.c            | 153 +++
 tools/testing/selftests/gpio/.gitignore       |   2 +
 tools/testing/selftests/gpio/Makefile         |   4 +-
 tools/testing/selftests/gpio/config           |   1 +
 tools/testing/selftests/gpio/gpio-chip-info.c |  57 ++
 tools/testing/selftests/gpio/gpio-line-name.c |  55 ++
 tools/testing/selftests/gpio/gpio-sim.sh      | 229 +++++
 18 files changed, 1822 insertions(+), 86 deletions(-)
 create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst
 create mode 100644 drivers/gpio/gpio-sim.c
 create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c
 create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c
 create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh
Signature · 1 line
2.29.1
#1 lore raw

PATCH v2 1/12 configfs: increase the item name length

From: Bartosz Golaszewski <[email protected]>

20 characters limit for item name is relatively small. Let's increase it to 32 to fit '04-committable-children' - a name we'll use in the sample code for committable items.

---
 include/linux/configfs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/configfs.h +1 -1
index 2e8c69b43c64..4f76dcc08134 100644
@@ -27,7 +27,7 @@
 #include <linux/kref.h>   /* struct kref */
 #include <linux/mutex.h>  /* struct mutex */
 
-#define CONFIGFS_ITEM_NAME_LEN	20
+#define CONFIGFS_ITEM_NAME_LEN	32
 
 struct module;
 
Signature · 1 line
2.29.1
#2 lore raw

PATCH v2 2/12 configfs: use (1UL << bit) for internal flags

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For better readability and maintenance: use the (1UL << bit) for flag definitions.

---
 fs/configfs/configfs_internal.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
fs/configfs/configfs_internal.h +10 -10
index 9a3aed249692..b495c9f043d4 100644
@@ -46,16 +46,16 @@ struct configfs_dirent {
 	struct configfs_fragment *s_frag;
 };
 
-#define CONFIGFS_ROOT		0x0001
-#define CONFIGFS_DIR		0x0002
-#define CONFIGFS_ITEM_ATTR	0x0004
-#define CONFIGFS_ITEM_BIN_ATTR	0x0008
-#define CONFIGFS_ITEM_LINK	0x0020
-#define CONFIGFS_USET_DIR	0x0040
-#define CONFIGFS_USET_DEFAULT	0x0080
-#define CONFIGFS_USET_DROPPING	0x0100
-#define CONFIGFS_USET_IN_MKDIR	0x0200
-#define CONFIGFS_USET_CREATING	0x0400
+#define CONFIGFS_ROOT			(1UL << 0)
+#define CONFIGFS_DIR			(1UL << 1)
+#define CONFIGFS_ITEM_ATTR		(1UL << 2)
+#define CONFIGFS_ITEM_BIN_ATTR		(1UL << 3)
+#define CONFIGFS_ITEM_LINK		(1UL << 5)
+#define CONFIGFS_USET_DIR		(1UL << 6)
+#define CONFIGFS_USET_DEFAULT		(1UL << 7)
+#define CONFIGFS_USET_DROPPING		(1UL << 8)
+#define CONFIGFS_USET_IN_MKDIR		(1UL << 9)
+#define CONFIGFS_USET_CREATING		(1UL << 10)
 #define CONFIGFS_NOT_PINNED	(CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)
 
 extern struct mutex configfs_symlink_mutex;
Signature · 1 line
2.29.1
#3 lore raw

PATCH v2 3/12 configfs: implement committable items

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

This implements configfs committable items. We mostly follow the documentation except that we extend config_group_ops with uncommit_item() callback for reverting the changes made by commit_item().

Each committable group has two sub-directories: pending and live. New items can only be created in pending/. Attributes can only be modified while the item is in pending/. Once it's ready to be committed, it must be moved over to live/ using the rename() system call. This is when the commit_item() function will be called.

Implementation-wise: we reuse the default group mechanism to elegantly plug the new pseude-groups into configfs. The pending group inherits the parent group's operations so that config_items can be seamlesly created in it using the callbacks supplied by the user as part of the committable group itself.

---
 Documentation/filesystems/configfs.rst |   6 +-
 fs/configfs/configfs_internal.h        |   2 +
 fs/configfs/dir.c                      | 245 ++++++++++++++++++++++++-
 include/linux/configfs.h               |   1 +
 4 files changed, 245 insertions(+), 9 deletions(-)
Documentation/filesystems/configfs.rst +2 -4
index 1d3d6f4a82a9..7e0e7c356450 100644
@@ -290,6 +290,7 @@ config_item_type::
 		struct config_group *(*make_group)(struct config_group *group,
 						   const char *name);
 		int (*commit_item)(struct config_item *item);
+		int (*uncommit_item)(struct config_item *item);
 		void (*disconnect_notify)(struct config_group *group,
 					  struct config_item *item);
 		void (*drop_item)(struct config_group *group,
@@ -490,9 +491,6 @@ pass up an error.
 Committable Items
 =================
 
-Note:
-     Committable items are currently unimplemented.
-
 Some config_items cannot have a valid initial state.  That is, no
 default values can be specified for the item's attributes such that the
 item can do its work.  Userspace must configure one or more attributes,
@@ -532,4 +530,4 @@ method returns zero and the item is moved to the "live" directory.
 As rmdir(2) does not work in the "live" directory, an item must be
 shutdown, or "uncommitted".  Again, this is done via rename(2), this
 time from the "live" directory back to the "pending" one.  The subsystem
-is notified by the ct_group_ops->uncommit_object() method.
+is notified by the ct_group_ops->uncommit_item() method.
fs/configfs/configfs_internal.h +2 -0
index b495c9f043d4..41ac21c82bf5 100644
@@ -56,6 +56,8 @@ struct configfs_dirent {
 #define CONFIGFS_USET_DROPPING		(1UL << 8)
 #define CONFIGFS_USET_IN_MKDIR		(1UL << 9)
 #define CONFIGFS_USET_CREATING		(1UL << 10)
+#define CONFIGFS_GROUP_PENDING		(1UL << 11)
+#define CONFIGFS_GROUP_LIVE		(1UL << 12)
 #define CONFIGFS_NOT_PINNED	(CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)
 
 extern struct mutex configfs_symlink_mutex;
fs/configfs/dir.c +240 -5

291 lines not on this page · raw message

include/linux/configfs.h +1 -0
index 4f76dcc08134..ff6b0e408136 100644
@@ -219,6 +219,7 @@ struct configfs_group_operations {
 	struct config_item *(*make_item)(struct config_group *group, const char *name);
 	struct config_group *(*make_group)(struct config_group *group, const char *name);
 	int (*commit_item)(struct config_item *item);
+	int (*uncommit_item)(struct config_item *item);
 	void (*disconnect_notify)(struct config_group *group, struct config_item *item);
 	void (*drop_item)(struct config_group *group, struct config_item *item);
 };
Signature · 1 line
2.29.1
#4 lore raw

PATCH v2 4/12 samples: configfs: add a committable group

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add an example of using committable items to configfs samples. Each config item has two attributes: read-write 'storeme' which works similarly to other examples in this file and a read-only 'committed' attribute which changes its value between false and true depending on whether it's committed or not at the moment.

---
 samples/configfs/configfs_sample.c | 153 +++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)
samples/configfs/configfs_sample.c +153 -0
index f9008be7a8a1..9bef74e4369d 100644
@@ -315,6 +315,158 @@ static struct configfs_subsystem group_children_subsys = {
 
 /* ----------------------------------------------------------------- */
 
+/*
+ * 04-committable-children
+ *
+ * This is an example of a committable group.  It's similar to the simple
+ * children example but each config_item has an additional 'committed'
+ * attribute which is read-only and is only modified when the config_item
+ * is moved from the 'pending' to the 'live' directory.
+ */
+
+struct committable_child {
+	struct config_item item;
+	int storeme;
+	bool committed;
+};
+
+static inline struct committable_child *
+to_committable_child(struct config_item *item)
+{
+	return container_of(item, struct committable_child, item);
+}
+
+static ssize_t
+committable_child_storeme_show(struct config_item *item, char *page)
+{
+	return sprintf(page, "%d\n", to_committable_child(item)->storeme);
+}
+
+static ssize_t committable_child_storeme_store(struct config_item *item,
+					       const char *page, size_t count)
+{
+	struct committable_child *child = to_committable_child(item);
+	int ret;
+
+	if (child->committed)
+		return -EPERM;
+
+	ret = kstrtoint(page, 10, &child->storeme);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+CONFIGFS_ATTR(committable_child_, storeme);
+
+static ssize_t
+committable_child_committed_show(struct config_item *item, char *page)
+{
+	return sprintf(page, "%s\n",
+		to_committable_child(item)->committed ? "true" : "false");
+}
+
+CONFIGFS_ATTR_RO(committable_child_, committed);
+
+static struct configfs_attribute *committable_child_attrs[] = {
+	&committable_child_attr_storeme,
+	&committable_child_attr_committed,
+	NULL,
+};
+
+static void committable_child_release(struct config_item *item)
+{
+	kfree(to_committable_child(item));
+}
+
+static struct configfs_item_operations committable_child_item_ops = {
+	.release	= committable_child_release,
+};
+
+static const struct config_item_type committable_child_type = {
+	.ct_item_ops	= &committable_child_item_ops,
+	.ct_attrs	= committable_child_attrs,
+	.ct_owner	= THIS_MODULE,
+};
+
+struct committable_children {
+	struct config_group group;
+};
+
+static struct config_item *
+committable_children_make_item(struct config_group *group, const char *name)
+{
+	struct committable_child *child;
+
+	child = kzalloc(sizeof(*child), GFP_KERNEL);
+	if (!child)
+		return ERR_PTR(-ENOMEM);
+
+	config_item_init_type_name(&child->item, name, &committable_child_type);
+
+	return &child->item;
+}
+
+static ssize_t
+committable_children_description_show(struct config_item *item, char *page)
+{
+	return sprintf(page,
+"[04-committable-children]\n"
+"\n"
+"This subsystem allows creation of committable config_items.  The subsystem\n"
+"has two subdirectories: pending and live.  New config_items can only be\n"
+"created in pending/ and they have one writable and readable attribute as\n"
+"well as a single read-only attribute.  The latter is only changed once the\n"
+"item is 'committed'.  This is done by moving the config_item (using\n"
+"rename()) to the live/ directory.  In this example, the storeme attribute\n"
+"becomes 'read-only' once committed.\n");
+}
+
+CONFIGFS_ATTR_RO(committable_children_, description);
+
+static struct configfs_attribute *committable_children_attrs[] = {
+	&committable_children_attr_description,
+	NULL,
+};
+
+static int committable_children_commit_item(struct config_item *item)
+{
+	to_committable_child(item)->committed = true;
+
+	return 0;
+}
+
+static int committable_children_uncommit_item(struct config_item *item)
+{
+	to_committable_child(item)->committed = false;
+
+	return 0;
+}
+
+static struct configfs_group_operations committable_children_group_ops = {
+	.make_item	= committable_children_make_item,
+	.commit_item	= committable_children_commit_item,
+	.uncommit_item	= committable_children_uncommit_item,
+};
+
+static const struct config_item_type committable_children_type = {
+	.ct_group_ops	= &committable_children_group_ops,
+	.ct_attrs	= committable_children_attrs,
+	.ct_owner	= THIS_MODULE,
+};
+
+static struct configfs_subsystem committable_children_subsys = {
+	.su_group = {
+		.cg_item = {
+			.ci_namebuf = "04-committable-children",
+			.ci_type = &committable_children_type,
+		},
+	},
+};
+
+/* ----------------------------------------------------------------- */
+
 /*
  * We're now done with our subsystem definitions.
  * For convenience in this module, here's a list of them all.  It
@@ -326,6 +478,7 @@ static struct configfs_subsystem *example_subsys[] = {
 	&childless_subsys.subsys,
 	&simple_children_subsys,
 	&group_children_subsys,
+	&committable_children_subsys,
 	NULL,
 };
 
Signature · 1 line
2.29.1
#5 lore raw

PATCH v2 5/12 lib: bitmap: remove the 'extern' keyword from function declarations

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The 'extern' keyword doesn't have any benefits in header files. Remove it.

---
 include/linux/bitmap.h | 115 ++++++++++++++++++++---------------------
 1 file changed, 57 insertions(+), 58 deletions(-)
include/linux/bitmap.h +57 -58
index 70a932470b2d..6939a8983026 100644
@@ -118,54 +118,53 @@
  * Allocation and deallocation of bitmap.
  * Provided in lib/bitmap.c to avoid circular dependency.
  */
-extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
-extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
-extern void bitmap_free(const unsigned long *bitmap);
+unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
+unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
+void bitmap_free(const unsigned long *bitmap);
 
 /*
  * lib/bitmap.c provides these functions:
  */
 
-extern int __bitmap_equal(const unsigned long *bitmap1,
-			  const unsigned long *bitmap2, unsigned int nbits);
-extern bool __pure __bitmap_or_equal(const unsigned long *src1,
-				     const unsigned long *src2,
-				     const unsigned long *src3,
-				     unsigned int nbits);
-extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
-			unsigned int nbits);
-extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
-				unsigned int shift, unsigned int nbits);
-extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
-				unsigned int shift, unsigned int nbits);
-extern void bitmap_cut(unsigned long *dst, const unsigned long *src,
-		       unsigned int first, unsigned int cut,
-		       unsigned int nbits);
-extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
+int __bitmap_equal(const unsigned long *bitmap1,
+		   const unsigned long *bitmap2, unsigned int nbits);
+bool __pure __bitmap_or_equal(const unsigned long *src1,
+			      const unsigned long *src2,
+			      const unsigned long *src3,
+			      unsigned int nbits);
+void __bitmap_complement(unsigned long *dst, const unsigned long *src,
+			 unsigned int nbits);
+void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
+			  unsigned int shift, unsigned int nbits);
+void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
+			 unsigned int shift, unsigned int nbits);
+void bitmap_cut(unsigned long *dst, const unsigned long *src,
+		unsigned int first, unsigned int cut, unsigned int nbits);
+int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
+		 const unsigned long *bitmap2, unsigned int nbits);
+void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
+		 const unsigned long *bitmap2, unsigned int nbits);
+void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
+		  const unsigned long *bitmap2, unsigned int nbits);
+int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
+		    const unsigned long *bitmap2, unsigned int nbits);
+void __bitmap_replace(unsigned long *dst,
+		      const unsigned long *old, const unsigned long *new,
+		      const unsigned long *mask, unsigned int nbits);
+int __bitmap_intersects(const unsigned long *bitmap1,
 			const unsigned long *bitmap2, unsigned int nbits);
-extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern void __bitmap_replace(unsigned long *dst,
-			const unsigned long *old, const unsigned long *new,
-			const unsigned long *mask, unsigned int nbits);
-extern int __bitmap_intersects(const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern int __bitmap_subset(const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
-extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
-extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
-
-extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
-						    unsigned long size,
-						    unsigned long start,
-						    unsigned int nr,
-						    unsigned long align_mask,
-						    unsigned long align_offset);
+int __bitmap_subset(const unsigned long *bitmap1,
+		    const unsigned long *bitmap2, unsigned int nbits);
+int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
+void __bitmap_set(unsigned long *map, unsigned int start, int len);
+void __bitmap_clear(unsigned long *map, unsigned int start, int len);
+
+unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
+					     unsigned long size,
+					     unsigned long start,
+					     unsigned int nr,
+					     unsigned long align_mask,
+					     unsigned long align_offset);
 
 /**
  * bitmap_find_next_zero_area - find a contiguous aligned zero area
@@ -190,33 +189,33 @@ bitmap_find_next_zero_area(unsigned long *map,
 					      align_mask, 0);
 }
 
-extern int bitmap_parse(const char *buf, unsigned int buflen,
+int bitmap_parse(const char *buf, unsigned int buflen,
 			unsigned long *dst, int nbits);
-extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
+int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
 			unsigned long *dst, int nbits);
-extern int bitmap_parselist(const char *buf, unsigned long *maskp,
+int bitmap_parselist(const char *buf, unsigned long *maskp,
 			int nmaskbits);
-extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
+int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
 			unsigned long *dst, int nbits);
-extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
+void bitmap_remap(unsigned long *dst, const unsigned long *src,
 		const unsigned long *old, const unsigned long *new, unsigned int nbits);
-extern int bitmap_bitremap(int oldbit,
+int bitmap_bitremap(int oldbit,
 		const unsigned long *old, const unsigned long *new, int bits);
-extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
+void bitmap_onto(unsigned long *dst, const unsigned long *orig,
 		const unsigned long *relmap, unsigned int bits);
-extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
+void bitmap_fold(unsigned long *dst, const unsigned long *orig,
 		unsigned int sz, unsigned int nbits);
-extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
-extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
-extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
+int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
+void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
+int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
 
 #ifdef __BIG_ENDIAN
-extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
+void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
 #else
 #define bitmap_copy_le bitmap_copy
 #endif
-extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
-extern int bitmap_print_to_pagebuf(bool list, char *buf,
+unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
+int bitmap_print_to_pagebuf(bool list, char *buf,
 				   const unsigned long *maskp, int nmaskbits);
 
 #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
@@ -265,9 +264,9 @@ static inline void bitmap_copy_clear_tail(unsigned long *dst,
  * therefore conversion is not needed when copying data from/to arrays of u32.
  */
 #if BITS_PER_LONG == 64
-extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
+void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
 							unsigned int nbits);
-extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
+void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
 							unsigned int nbits);
 #else
 #define bitmap_from_arr32(bitmap, buf, nbits)			\
Signature · 1 line
2.29.1
Reviewed-by on v2 5/12
#6 lore raw

Re: [PATCH v2 05/12] lib: bitmap: remove the 'extern' keyword from function declarations

Quoted 3 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The 'extern' keyword doesn't have any benefits in header files. Remove it.

A few nitpicks below.

Quoted 32 lines
---
 include/linux/bitmap.h | 115 ++++++++++++++++++++---------------------
 1 file changed, 57 insertions(+), 58 deletions(-)
include/linux/bitmap.h +3 -11
index 70a932470b2d..6939a8983026 100644
@@ -118,54 +118,53 @@
  * Allocation and deallocation of bitmap.
  * Provided in lib/bitmap.c to avoid circular dependency.
  */
-extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
-extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
-extern void bitmap_free(const unsigned long *bitmap);
+unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
+unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
+void bitmap_free(const unsigned long *bitmap);
 
 /*
  * lib/bitmap.c provides these functions:
  */
 
-extern int __bitmap_equal(const unsigned long *bitmap1,
-			  const unsigned long *bitmap2, unsigned int nbits);
-extern bool __pure __bitmap_or_equal(const unsigned long *src1,
-				     const unsigned long *src2,
-				     const unsigned long *src3,
-				     unsigned int nbits);
-extern void __bitmap_complement(unsigned long *dst, const unsigned long *src,
-			unsigned int nbits);

include/linux/bitmap.h line 130

Quoted 10 lines
include/linux/bitmap.h +2 -8
-extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
-				unsigned int shift, unsigned int nbits);
-extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
-				unsigned int shift, unsigned int nbits);
-extern void bitmap_cut(unsigned long *dst, const unsigned long *src,
-		       unsigned int first, unsigned int cut,
-		       unsigned int nbits);
-extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
+int __bitmap_equal(const unsigned long *bitmap1,
+		   const unsigned long *bitmap2, unsigned int nbits);

Logically better to have

int __bitmap_equal(const unsigned long *bitmap1, const unsigned long *bitmap2,

		   unsigned int nbits);
Quoted 64 lines
include/linux/bitmap.h +36 -23
+bool __pure __bitmap_or_equal(const unsigned long *src1,
+			      const unsigned long *src2,
+			      const unsigned long *src3,
+			      unsigned int nbits);
+void __bitmap_complement(unsigned long *dst, const unsigned long *src,
+			 unsigned int nbits);
+void __bitmap_shift_right(unsigned long *dst, const unsigned long *src,
+			  unsigned int shift, unsigned int nbits);
+void __bitmap_shift_left(unsigned long *dst, const unsigned long *src,
+			 unsigned int shift, unsigned int nbits);
+void bitmap_cut(unsigned long *dst, const unsigned long *src,
+		unsigned int first, unsigned int cut, unsigned int nbits);
+int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
+		 const unsigned long *bitmap2, unsigned int nbits);
+void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
+		 const unsigned long *bitmap2, unsigned int nbits);
+void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
+		  const unsigned long *bitmap2, unsigned int nbits);
+int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
+		    const unsigned long *bitmap2, unsigned int nbits);
+void __bitmap_replace(unsigned long *dst,
+		      const unsigned long *old, const unsigned long *new,
+		      const unsigned long *mask, unsigned int nbits);
+int __bitmap_intersects(const unsigned long *bitmap1,
 			const unsigned long *bitmap2, unsigned int nbits);
-extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern void __bitmap_replace(unsigned long *dst,
-			const unsigned long *old, const unsigned long *new,
-			const unsigned long *mask, unsigned int nbits);
-extern int __bitmap_intersects(const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern int __bitmap_subset(const unsigned long *bitmap1,
-			const unsigned long *bitmap2, unsigned int nbits);
-extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
-extern void __bitmap_set(unsigned long *map, unsigned int start, int len);
-extern void __bitmap_clear(unsigned long *map, unsigned int start, int len);
-
-extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
-						    unsigned long size,
-						    unsigned long start,
-						    unsigned int nr,
-						    unsigned long align_mask,
-						    unsigned long align_offset);
+int __bitmap_subset(const unsigned long *bitmap1,
+		    const unsigned long *bitmap2, unsigned int nbits);
+int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits);
+void __bitmap_set(unsigned long *map, unsigned int start, int len);
+void __bitmap_clear(unsigned long *map, unsigned int start, int len);
+
+unsigned long bitmap_find_next_zero_area_off(unsigned long *map,
+					     unsigned long size,
+					     unsigned long start,
+					     unsigned int nr,
+					     unsigned long align_mask,
+					     unsigned long align_offset);
 
 /**
  * bitmap_find_next_zero_area - find a contiguous aligned zero area

include/linux/bitmap.h bitmap_find_next_zero_area(unsigned long *map, · line 193

Quoted 6 lines
include/linux/bitmap.h +1 -1
@@ -190,33 +189,33 @@ bitmap_find_next_zero_area(unsigned long *map,
 					      align_mask, 0);
 }
 
-extern int bitmap_parse(const char *buf, unsigned int buflen,
+int bitmap_parse(const char *buf, unsigned int buflen,
 			unsigned long *dst, int nbits);

Can be

int bitmap_parse(const char *buf, unsigned int buflen, unsigned long *dst,

		 int nbits);

And I wonder why nbits here is signed.

include/linux/bitmap.h bitmap_find_next_zero_area(unsigned long *map, · line 195

Quoted 3 lines
include/linux/bitmap.h +1 -1
-extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
+int bitmap_parse_user(const char __user *ubuf, unsigned int ulen,
 			unsigned long *dst, int nbits);

Ditto.

include/linux/bitmap.h bitmap_find_next_zero_area(unsigned long *map, · line 197

Quoted 4 lines
include/linux/bitmap.h +1 -1
-extern int bitmap_parselist(const char *buf, unsigned long *maskp,
 
+int bitmap_parselist(const char *buf, unsigned long *maskp,
 			int nmaskbits);

Now can be one line.

include/linux/bitmap.h bitmap_find_next_zero_area(unsigned long *map,

Quoted 10 lines
include/linux/bitmap.h +3 -3
-extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
+int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen,
 			unsigned long *dst, int nbits);
-extern void bitmap_remap(unsigned long *dst, const unsigned long *src,
+void bitmap_remap(unsigned long *dst, const unsigned long *src,
 		const unsigned long *old, const unsigned long *new, unsigned int nbits);
-extern int bitmap_bitremap(int oldbit,
 
+int bitmap_bitremap(int oldbit,
 		const unsigned long *old, const unsigned long *new, int bits);

More logical

int bitmap_bitremap(int oldbit, const unsigned long *old,

		    const unsigned long *new, int bits);

Or even

int bitmap_bitremap(int oldbit, const unsigned long *old, const unsigned long *new,

		    int bits);
Quoted 27 lines
include/linux/bitmap.h +8 -8
-extern void bitmap_onto(unsigned long *dst, const unsigned long *orig,
+void bitmap_onto(unsigned long *dst, const unsigned long *orig,
 		const unsigned long *relmap, unsigned int bits);
-extern void bitmap_fold(unsigned long *dst, const unsigned long *orig,
+void bitmap_fold(unsigned long *dst, const unsigned long *orig,
 		unsigned int sz, unsigned int nbits);
-extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
-extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
-extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
+int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order);
+void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order);
+int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order);
 
 #ifdef __BIG_ENDIAN
-extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
+void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits);
 #else
 #define bitmap_copy_le bitmap_copy
 #endif
-extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
-extern int bitmap_print_to_pagebuf(bool list, char *buf,
+unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits);
+int bitmap_print_to_pagebuf(bool list, char *buf,
 				   const unsigned long *maskp, int nmaskbits);

#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1)))

include/linux/bitmap.h static inline void bitmap_copy_clear_tail(unsigned long *dst, · line 268

Quoted 6 lines
include/linux/bitmap.h +1 -1
@@ -265,9 +264,9 @@ static inline void bitmap_copy_clear_tail(unsigned long *dst,
  * therefore conversion is not needed when copying data from/to arrays of u32.
  */
 #if BITS_PER_LONG == 64
-extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
+void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf,
 							unsigned int nbits);

One line?

include/linux/bitmap.h static inline void bitmap_copy_clear_tail(unsigned long *dst, · line 270

Quoted 3 lines
include/linux/bitmap.h +1 -1
-extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
+void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap,
 							unsigned int nbits);

Ditto.

Quoted 5 lines
include/linux/bitmap.h +0 -0
 #else
 #define bitmap_from_arr32(bitmap, buf, nbits)			\
Signature · 1 line
2.29.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v2 05/12] lib: bitmap: remove the 'extern' keyword from function declarations

Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

The 'extern' keyword doesn't have any benefits in header files. Remove it.

A few nitpicks below.

Quoted 2 lines

---

Hi Andy,

regarding this patch and other places where you raise issues with line
breaking: I believe this is purely a question of taste. There are no guidelines on line breaking in the docs. I will leave it as it is here because it's not better or worse than your version, just different. Same for exceeding 80 characters - I personally believe it's justified when the line looks better but whenever it can be cleanly broken, it's better to stay within the limit.

Best Regards,
Bartosz

#8 lore raw

PATCH v2 6/12 lib: bitmap: order includes alphabetically

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For better readability and maintenance: order the includes in bitmap source files alphabetically.

---
 include/linux/bitmap.h | 4 ++--
 lib/bitmap.c           | 9 +++++----
 2 files changed, 7 insertions(+), 6 deletions(-)
include/linux/bitmap.h +2 -2
index 6939a8983026..3282db97e06c 100644
@@ -4,10 +4,10 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/types.h>
 #include <linux/bitops.h>
-#include <linux/string.h>
 #include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/types.h>
 
 /*
  * bitmaps provide bit arrays that consume one or more unsigned
lib/bitmap.c +5 -4
index 75006c4036e9..78f70d9007ad 100644
@@ -3,17 +3,18 @@
  * lib/bitmap.c
  * Helper functions for bitmap.h.
  */
-#include <linux/export.h>
-#include <linux/thread_info.h>
-#include <linux/ctype.h>
-#include <linux/errno.h>
+
 #include <linux/bitmap.h>
 #include <linux/bitops.h>
 #include <linux/bug.h>
+#include <linux/ctype.h>
+#include <linux/errno.h>
+#include <linux/export.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/thread_info.h>
 #include <linux/uaccess.h>
 
 #include <asm/page.h>
Signature · 1 line
2.29.1
Reviewed-by on v2 6/12
#9 lore raw

Re: [PATCH v2 06/12] lib: bitmap: order includes alphabetically

Quoted 4 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

For better readability and maintenance: order the includes in bitmap source files alphabetically.

Quoted 53 lines
---
 include/linux/bitmap.h | 4 ++--
 lib/bitmap.c           | 9 +++++----
 2 files changed, 7 insertions(+), 6 deletions(-)
include/linux/bitmap.h +2 -2
index 6939a8983026..3282db97e06c 100644
@@ -4,10 +4,10 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/types.h>
 #include <linux/bitops.h>
-#include <linux/string.h>
 #include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/types.h>
 
 /*
  * bitmaps provide bit arrays that consume one or more unsigned
lib/bitmap.c +5 -4
index 75006c4036e9..78f70d9007ad 100644
@@ -3,17 +3,18 @@
  * lib/bitmap.c
  * Helper functions for bitmap.h.
  */
-#include <linux/export.h>
-#include <linux/thread_info.h>
-#include <linux/ctype.h>
-#include <linux/errno.h>
+
 #include <linux/bitmap.h>
 #include <linux/bitops.h>
 #include <linux/bug.h>
+#include <linux/ctype.h>
+#include <linux/errno.h>
+#include <linux/export.h>
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/thread_info.h>
 #include <linux/uaccess.h>
 
 #include <asm/page.h>
Signature · 1 line
2.29.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw

PATCH v2 7/12 lib: bitmap: provide devm_bitmap_alloc() and devm_bitmap_zalloc()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide managed variants of bitmap_alloc() and bitmap_zalloc().

---
 include/linux/bitmap.h | 10 ++++++++++
 lib/bitmap.c           | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
include/linux/bitmap.h +10 -0
index 3282db97e06c..e41c622db1b8 100644
@@ -9,6 +9,8 @@
 #include <linux/string.h>
 #include <linux/types.h>
 
+struct device;
+
 /*
  * bitmaps provide bit arrays that consume one or more unsigned
  * longs.  The bitmap interface and available operations are listed
@@ -122,6 +124,14 @@ unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
 unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
 void bitmap_free(const unsigned long *bitmap);
 
+/*
+ * Managed variants of the above.
+ */
+unsigned long *devm_bitmap_alloc(struct device *dev,
+				 unsigned int nbits, gfp_t flags);
+unsigned long *devm_bitmap_zalloc(struct device *dev,
+				  unsigned int nbits, gfp_t flags);
+
 /*
  * lib/bitmap.c provides these functions:
  */
lib/bitmap.c +33 -0
index 78f70d9007ad..b4fd7fd084c6 100644
@@ -8,6 +8,7 @@
 #include <linux/bitops.h>
 #include <linux/bug.h>
 #include <linux/ctype.h>
+#include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/export.h>
 #include <linux/kernel.h>
@@ -1263,6 +1264,38 @@ void bitmap_free(const unsigned long *bitmap)
 }
 EXPORT_SYMBOL(bitmap_free);
 
+static void devm_bitmap_free(void *data)
+{
+	unsigned long *bitmap = data;
+
+	bitmap_free(bitmap);
+}
+
+unsigned long *devm_bitmap_alloc(struct device *dev,
+				 unsigned int nbits, gfp_t flags)
+{
+	unsigned long *bitmap;
+	int ret;
+
+	bitmap = bitmap_alloc(nbits, flags);
+	if (!bitmap)
+		return NULL;
+
+	ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);
+	if (ret)
+		return NULL;
+
+	return bitmap;
+}
+EXPORT_SYMBOL(devm_bitmap_alloc);
+
+unsigned long *devm_bitmap_zalloc(struct device *dev,
+				  unsigned int nbits, gfp_t flags)
+{
+	return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);
+}
+EXPORT_SYMBOL(devm_bitmap_zalloc);
+
 #if BITS_PER_LONG == 64
 /**
  * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
Signature · 1 line
2.29.1
Reviewed-by on v2 7/12
#11 lore raw

Re: [PATCH v2 07/12] lib: bitmap: provide devm_bitmap_alloc() and devm_bitmap_zalloc()

Quoted 3 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Provide managed variants of bitmap_alloc() and bitmap_zalloc().

A few nit-picks below.

Quoted 20 lines
---
 include/linux/bitmap.h | 10 ++++++++++
 lib/bitmap.c           | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
include/linux/bitmap.h +2 -0
index 3282db97e06c..e41c622db1b8 100644
@@ -9,6 +9,8 @@
 #include <linux/string.h>
 #include <linux/types.h>
 
+struct device;
+
 /*
  * bitmaps provide bit arrays that consume one or more unsigned
  * longs.  The bitmap interface and available operations are listed

include/linux/bitmap.h unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags); · line 129

Quoted 6 lines
include/linux/bitmap.h +3 -0
@@ -122,6 +124,14 @@ unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags);
 unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags);
 void bitmap_free(const unsigned long *bitmap);
 
+/*
+ * Managed variants of the above.
+ */

One line?

include/linux/bitmap.h unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags); · line 133

Quoted 4 lines
include/linux/bitmap.h +4 -0
+unsigned long *devm_bitmap_alloc(struct device *dev,
+				 unsigned int nbits, gfp_t flags);
+unsigned long *devm_bitmap_zalloc(struct device *dev,
+				  unsigned int nbits, gfp_t flags);

Both can be oneliners.

Quoted 18 lines
include/linux/bitmap.h +0 -0
 /*
  * lib/bitmap.c provides these functions:
  */
lib/bitmap.c +1 -0
index 78f70d9007ad..b4fd7fd084c6 100644
@@ -8,6 +8,7 @@
 #include <linux/bitops.h>
 #include <linux/bug.h>
 #include <linux/ctype.h>
+#include <linux/device.h>
 #include <linux/errno.h>
 #include <linux/export.h>
 #include <linux/kernel.h>
@@ -1263,6 +1264,38 @@ void bitmap_free(const unsigned long *bitmap)
 }
 EXPORT_SYMBOL(bitmap_free);

lib/bitmap.c void bitmap_free(const unsigned long *bitmap) · line 1275

Quoted 10 lines
lib/bitmap.c +8 -0
 
+static void devm_bitmap_free(void *data)
+{
+	unsigned long *bitmap = data;
+
+	bitmap_free(bitmap);
+}
 
+unsigned long *devm_bitmap_alloc(struct device *dev,
+				 unsigned int nbits, gfp_t flags)

One line?

Quoted 8 lines
lib/bitmap.c +8 -0
+{
+	unsigned long *bitmap;
+	int ret;
+
+	bitmap = bitmap_alloc(nbits, flags);
+	if (!bitmap)
+		return NULL;
+

lib/bitmap.c void bitmap_free(const unsigned long *bitmap) · line 1293

Quoted 10 lines
lib/bitmap.c +9 -0
+	ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap);
+	if (ret)
+		return NULL;
+
+	return bitmap;
+}
+EXPORT_SYMBOL(devm_bitmap_alloc);
 
+unsigned long *devm_bitmap_zalloc(struct device *dev,
+				  unsigned int nbits, gfp_t flags)

One line?

Quoted 11 lines
lib/bitmap.c +5 -0
+{
+	return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO);
+}
+EXPORT_SYMBOL(devm_bitmap_zalloc);
+
 #if BITS_PER_LONG == 64
 /**
  * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap
Signature · 1 line
2.29.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw

PATCH v2 8/12 drivers: export device_is_bound()

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
 	return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);
 
 static void driver_bound(struct device *dev)
 {
Signature · 1 line
2.29.1
#13 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

CC Greg

Quoted 27 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
drivers/base/dd.c +1 -1
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);
 
 static void driver_bound(struct device *dev)
 {
--

2.29.1

maintainer NAK
#14 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

NACK.

#15 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

The story goes like this: committing the configfs item registers a platform device. As far as I understand - there's no guarantee that the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

Please advise on how to handle it without device_is_bound().

Best Regards,
Bartosz

maintainer
#16 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 37 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

Quoted 8 lines

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Use the virtbus code instead perhaps?

Quoted 7 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

Quoted 5 lines

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

Quoted 1 line

Please advise on how to handle it without device_is_bound().

Please do not create sysfs files before they can be read from :)

thanks,

greg k-h

#17 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 45 lines · Greg KH
Quoted 43 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

Quoted 8 lines
Quoted 2 lines

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Use the virtbus code instead perhaps?

I have no idea what virtbus is and grepping for it only returns three hits in: ./drivers/pci/iov.c and it's a function argument.

If it stands for virtual bus then for sure it sounds like the right thing but I need to find more info on this.

Quoted 10 lines
Quoted 10 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

Quoted 7 lines
Quoted 2 lines

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

It's not sysfs - it's in configfs. Each chip has a read-only configfs attribute that returns the name of the device - I don't really have a better idea to map the configfs items to devices that committing creates.

Quoted 4 lines
Quoted 1 line

Please advise on how to handle it without device_is_bound().

Please do not create sysfs files before they can be read from :)

Don't worry, I don't.

Bartosz

maintainer
#18 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 52 lines · Bartosz Golaszewski
Quoted 51 lines · Greg KH
Quoted 37 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

Quoted 8 lines

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Quoted 8 lines
Quoted 2 lines

Use the virtbus code instead perhaps?

I have no idea what virtbus is and grepping for it only returns three hits in: ./drivers/pci/iov.c and it's a function argument.

If it stands for virtual bus then for sure it sounds like the right thing but I need to find more info on this.

Sorry, wrong name, see Documentation/driver-api/auxiliary_bus.rst for the details. "virtbus" was what I think about it as that was my original name for it, but it eventually got merged with a different name.

Quoted 18 lines
Quoted 10 lines
Quoted 10 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

Quoted 7 lines
Quoted 2 lines

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

Quoted 4 lines

It's not sysfs - it's in configfs. Each chip has a read-only configfs attribute that returns the name of the device - I don't really have a better idea to map the configfs items to devices that committing creates.

Same question, why are you exporting a configfs attribute that can not be read from? Only export it when your driver is bound to the device.

thanks,

greg k-h

#19 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 86 lines · Greg KH
Quoted 52 lines · Bartosz Golaszewski
Quoted 51 lines · Greg KH
Quoted 37 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

Quoted 8 lines

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Quoted 8 lines
Quoted 2 lines

Use the virtbus code instead perhaps?

I have no idea what virtbus is and grepping for it only returns three hits in: ./drivers/pci/iov.c and it's a function argument.

If it stands for virtual bus then for sure it sounds like the right thing but I need to find more info on this.

Sorry, wrong name, see Documentation/driver-api/auxiliary_bus.rst for the details. "virtbus" was what I think about it as that was my original name for it, but it eventually got merged with a different name.

Quoted 18 lines
Quoted 17 lines
Quoted 7 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

Quoted 5 lines

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

Quoted 8 lines
Quoted 4 lines

It's not sysfs - it's in configfs. Each chip has a read-only configfs attribute that returns the name of the device - I don't really have a better idea to map the configfs items to devices that committing creates.

Same question, why are you exporting a configfs attribute that can not be read from? Only export it when your driver is bound to the device.

The device doesn't know anything about configfs. Why would it? The configuration of a GPIO chip can't be changed after it's instantiated, this is why we have committable items.

We export a directory in configfs: gpio-sim -> user creates a new directory (item) in gpio-sim/pending/foo and it's not tied to any device yet but exports attributes which we use to configure the device (label, number of lines, line names etc.), then we mv gpio-sim/pending/foo gpio-sim/live and this is when the device gets created and registered with the subsystem. We take all the configured attributes and put them into device properties for both the driver and gpiolib core (for standard properties) to read - just like we would with a regular GPIO driver because this is the goal: test the core code.

Configfs doesn't even allow to dynamically export and unexport attributes.

Bart

maintainer
#20 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 100 lines · Bartosz Golaszewski
Quoted 86 lines · Greg KH
Quoted 52 lines · Bartosz Golaszewski
Quoted 51 lines · Greg KH
Quoted 37 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

Quoted 8 lines

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Quoted 8 lines
Quoted 2 lines

Use the virtbus code instead perhaps?

I have no idea what virtbus is and grepping for it only returns three hits in: ./drivers/pci/iov.c and it's a function argument.

If it stands for virtual bus then for sure it sounds like the right thing but I need to find more info on this.

Sorry, wrong name, see Documentation/driver-api/auxiliary_bus.rst for the details. "virtbus" was what I think about it as that was my original name for it, but it eventually got merged with a different name.

Quoted 18 lines
Quoted 17 lines
Quoted 7 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

Quoted 5 lines

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

Quoted 8 lines
Quoted 4 lines

It's not sysfs - it's in configfs. Each chip has a read-only configfs attribute that returns the name of the device - I don't really have a better idea to map the configfs items to devices that committing creates.

Same question, why are you exporting a configfs attribute that can not be read from? Only export it when your driver is bound to the device.

The device doesn't know anything about configfs. Why would it? The configuration of a GPIO chip can't be changed after it's instantiated, this is why we have committable items.

Quoted 10 lines

We export a directory in configfs: gpio-sim -> user creates a new directory (item) in gpio-sim/pending/foo and it's not tied to any device yet but exports attributes which we use to configure the device (label, number of lines, line names etc.), then we mv gpio-sim/pending/foo gpio-sim/live and this is when the device gets created and registered with the subsystem. We take all the configured attributes and put them into device properties for both the driver and gpiolib core (for standard properties) to read - just like we would with a regular GPIO driver because this is the goal: test the core code.

Ok, but they why are you trying to have dev_name be an exported thing? I don't understand an attribute here that is visable but can not be read from.

And why not just use the default device name function: dev_name(), which will always return a string that will work no matter if the device is bound to a driver or not.

thanks,

greg k-h

#21 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 63 lines · Greg KH
Quoted 61 lines · Bartosz Golaszewski
Quoted 60 lines · Greg KH
Quoted 58 lines · Bartosz Golaszewski
Quoted 45 lines · Greg KH
Quoted 43 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

Quoted 8 lines
Quoted 2 lines

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Use the virtbus code instead perhaps?

I have no idea what virtbus is and grepping for it only returns three hits in: ./drivers/pci/iov.c and it's a function argument.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

If it stands for virtual bus then for sure it sounds like the right thing but I need to find more info on this.

Sorry, wrong name, see Documentation/driver-api/auxiliary_bus.rst for the details. "virtbus" was what I think about it as that was my original name for it, but it eventually got merged with a different name.

Unless I'm not seeing something - it completely doesn't look like the right solution. This auxiliary bus sounds like MFD with extra steps. Its aim seems to be to provide virtual devices for sub-modules of real devices.

What I have here really is a dummy device for which no HW exists.
Also: while the preferred way is to use configfs to instantiate these simulated devices, then can still be registered from device-tree (this is a feature that was requested and eventually implemented in gpio-mockup which we want to phase out so we can't just drop it). AFAIK only platform devices can be populated from DT.

I guess we could create something like a "virtual bus" that would be there for devices that don't exist on any physical bus but this would end up in big part being the same thing as platform devices.

Quoted 42 lines
Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 7 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

Quoted 5 lines

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

Quoted 8 lines
Quoted 4 lines

It's not sysfs - it's in configfs. Each chip has a read-only configfs attribute that returns the name of the device - I don't really have a better idea to map the configfs items to devices that committing creates.

Same question, why are you exporting a configfs attribute that can not be read from? Only export it when your driver is bound to the device.

The device doesn't know anything about configfs. Why would it? The configuration of a GPIO chip can't be changed after it's instantiated, this is why we have committable items.

Quoted 10 lines

We export a directory in configfs: gpio-sim -> user creates a new directory (item) in gpio-sim/pending/foo and it's not tied to any device yet but exports attributes which we use to configure the device (label, number of lines, line names etc.), then we mv gpio-sim/pending/foo gpio-sim/live and this is when the device gets created and registered with the subsystem. We take all the configured attributes and put them into device properties for both the driver and gpiolib core (for standard properties) to read - just like we would with a regular GPIO driver because this is the goal: test the core code.

Quoted 4 lines

Ok, but they why are you trying to have dev_name be an exported thing? I don't understand an attribute here that is visable but can not be read from.

Because once the associated configfs item is committed and the device created, it will become readable. The list of attributes is fixed in configfs. I'm not sure what the better approach would be - return "none" if the device handle is NULL?

Quoted 4 lines

And why not just use the default device name function: dev_name(), which will always return a string that will work no matter if the device is bound to a driver or not.

I can do this but then it's possible that user-space gets the name of the device which doesn't exist in sysfs. I guess we can mention that in the documentation.

Bartosz

maintainer
#22 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 73 lines · Bartosz Golaszewski
Quoted 63 lines · Greg KH
Quoted 61 lines · Bartosz Golaszewski
Quoted 60 lines · Greg KH
Quoted 58 lines · Bartosz Golaszewski
Quoted 45 lines · Greg KH
Quoted 43 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

Quoted 8 lines
Quoted 2 lines

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Use the virtbus code instead perhaps?

I have no idea what virtbus is and grepping for it only returns three hits in: ./drivers/pci/iov.c and it's a function argument.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

If it stands for virtual bus then for sure it sounds like the right thing but I need to find more info on this.

Sorry, wrong name, see Documentation/driver-api/auxiliary_bus.rst for the details. "virtbus" was what I think about it as that was my original name for it, but it eventually got merged with a different name.

Quoted 6 lines

Unless I'm not seeing something - it completely doesn't look like the right solution. This auxiliary bus sounds like MFD with extra steps. Its aim seems to be to provide virtual devices for sub-modules of real devices.

What I have here really is a dummy device for which no HW exists.

Then just use a "normal" virtual device. We have loads of them. But if you want to bind a "driver" to it, then use the aux bus please. Do NOT abuse a platform device for this.

Quoted 5 lines

Also: while the preferred way is to use configfs to instantiate these simulated devices, then can still be registered from device-tree (this is a feature that was requested and eventually implemented in gpio-mockup which we want to phase out so we can't just drop it). AFAIK only platform devices can be populated from DT.

If you really are using DT, then ok, a platform device can be used, but you didn't say that :)

Quoted 3 lines

I guess we could create something like a "virtual bus" that would be there for devices that don't exist on any physical bus but this would end up in big part being the same thing as platform devices.

That's what the aux bus code is there for. So maybe you do need to use it.

Quoted 47 lines
Quoted 42 lines
Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 7 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

Quoted 5 lines

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

Quoted 8 lines
Quoted 4 lines

It's not sysfs - it's in configfs. Each chip has a read-only configfs attribute that returns the name of the device - I don't really have a better idea to map the configfs items to devices that committing creates.

Same question, why are you exporting a configfs attribute that can not be read from? Only export it when your driver is bound to the device.

The device doesn't know anything about configfs. Why would it? The configuration of a GPIO chip can't be changed after it's instantiated, this is why we have committable items.

Quoted 10 lines

We export a directory in configfs: gpio-sim -> user creates a new directory (item) in gpio-sim/pending/foo and it's not tied to any device yet but exports attributes which we use to configure the device (label, number of lines, line names etc.), then we mv gpio-sim/pending/foo gpio-sim/live and this is when the device gets created and registered with the subsystem. We take all the configured attributes and put them into device properties for both the driver and gpiolib core (for standard properties) to read - just like we would with a regular GPIO driver because this is the goal: test the core code.

Quoted 4 lines

Ok, but they why are you trying to have dev_name be an exported thing? I don't understand an attribute here that is visable but can not be read from.

Quoted 4 lines

Because once the associated configfs item is committed and the device created, it will become readable. The list of attributes is fixed in configfs. I'm not sure what the better approach would be - return "none" if the device handle is NULL?

Sounds reasonable, I don't know how configfs works, it's been a decade since I last touched it.

Quoted 8 lines
Quoted 4 lines

And why not just use the default device name function: dev_name(), which will always return a string that will work no matter if the device is bound to a driver or not.

I can do this but then it's possible that user-space gets the name of the device which doesn't exist in sysfs. I guess we can mention that in the documentation.

Device names can change over time, nothing new there.

thanks,

greg k-h

#23 lore raw

Re: [PATCH v2 08/12] drivers: export device_is_bound()

Quoted 86 lines · Greg KH
Quoted 73 lines · Bartosz Golaszewski
Quoted 63 lines · Greg KH
Quoted 61 lines · Bartosz Golaszewski
Quoted 60 lines · Greg KH
Quoted 58 lines · Bartosz Golaszewski
Quoted 45 lines · Greg KH
Quoted 43 lines · Bartosz Golaszewski
Quoted 31 lines · Greg KH
Quoted 16 lines · Geert Uytterhoeven

CC Greg

Quoted 13 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device.

---
 drivers/base/dd.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 9 lines

drivers/base/dd.c bool device_is_bound(struct device *dev) · line 356

Quoted 9 lines
drivers/base/dd.c +1 -0
index 9179825ff646..c62c02e3490a 100644
@@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev)
 {
        return dev->p && klist_node_attached(&dev->p->knode_driver);
 }
+EXPORT_SYMBOL_GPL(device_is_bound);

No. Please no. Why is this needed? Feels like someone is doing something really wrong...

Quoted 2 lines

NACK.

I should have Cc'ed you the entire series, my bad.

This is the patch that uses this change - it's a new, improved testing module for GPIO using configfs & sysfs as you (I think) suggested a while ago:

https://lkml.org/lkml/2021/3/4/355

Quoted 8 lines
Quoted 2 lines

The story goes like this: committing the configfs item registers a platform device.

Ick, no, stop there, that's not a "real" device, please do not abuse platform devices like that, you all know I hate this :(

Use the virtbus code instead perhaps?

I have no idea what virtbus is and grepping for it only returns three hits in: ./drivers/pci/iov.c and it's a function argument.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

If it stands for virtual bus then for sure it sounds like the right thing but I need to find more info on this.

Sorry, wrong name, see Documentation/driver-api/auxiliary_bus.rst for the details. "virtbus" was what I think about it as that was my original name for it, but it eventually got merged with a different name.

Quoted 6 lines

Unless I'm not seeing something - it completely doesn't look like the right solution. This auxiliary bus sounds like MFD with extra steps. Its aim seems to be to provide virtual devices for sub-modules of real devices.

What I have here really is a dummy device for which no HW exists.

Then just use a "normal" virtual device. We have loads of them. But if you want to bind a "driver" to it, then use the aux bus please. Do NOT abuse a platform device for this.

Quoted 9 lines
Quoted 5 lines

Also: while the preferred way is to use configfs to instantiate these simulated devices, then can still be registered from device-tree (this is a feature that was requested and eventually implemented in gpio-mockup which we want to phase out so we can't just drop it). AFAIK only platform devices can be populated from DT.

If you really are using DT, then ok, a platform device can be used, but you didn't say that :)

My bad. Yes we need to use DT. And platform device does sound like the best approach.

Quoted 7 lines
Quoted 3 lines

I guess we could create something like a "virtual bus" that would be there for devices that don't exist on any physical bus but this would end up in big part being the same thing as platform devices.

That's what the aux bus code is there for. So maybe you do need to use it.

I'm fine with that if it can be instantiated from DT but it doesn't seem so.

Quoted 60 lines
Quoted 47 lines
Quoted 42 lines
Quoted 31 lines
Quoted 18 lines
Quoted 18 lines
Quoted 17 lines
Quoted 7 lines

As far as I understand - there's no guarantee that
the device will be bound to a driver before the commit callback (or more specifically platform_device_register_full() in this case) returns so the user may try to retrieve the name of the device immediately (normally user-space should wait for the associated uevent but nobody can force that) by doing:

Quoted 5 lines

mv /sys/kernel/config/gpio-sim/pending/foo /sys/kernel/config/gpio-sim/live/ cat /sys/kernel/config/gpio-sim/live/foo/dev_name

If the device is not bound at this point, we'll have a crash in the kernel as opposed to just returning -ENODEV.

How will the kernel crash? What has created the dev_name sysfs file before it is possible to be read from? That feels like the root problem.

Quoted 8 lines
Quoted 4 lines

It's not sysfs - it's in configfs. Each chip has a read-only configfs attribute that returns the name of the device - I don't really have a better idea to map the configfs items to devices that committing creates.

Same question, why are you exporting a configfs attribute that can not be read from? Only export it when your driver is bound to the device.

The device doesn't know anything about configfs. Why would it? The configuration of a GPIO chip can't be changed after it's instantiated, this is why we have committable items.

Quoted 10 lines

We export a directory in configfs: gpio-sim -> user creates a new directory (item) in gpio-sim/pending/foo and it's not tied to any device yet but exports attributes which we use to configure the device (label, number of lines, line names etc.), then we mv gpio-sim/pending/foo gpio-sim/live and this is when the device gets created and registered with the subsystem. We take all the configured attributes and put them into device properties for both the driver and gpiolib core (for standard properties) to read - just like we would with a regular GPIO driver because this is the goal: test the core code.

Quoted 4 lines

Ok, but they why are you trying to have dev_name be an exported thing? I don't understand an attribute here that is visable but can not be read from.

Quoted 4 lines

Because once the associated configfs item is committed and the device created, it will become readable. The list of attributes is fixed in configfs. I'm not sure what the better approach would be - return "none" if the device handle is NULL?

Sounds reasonable, I don't know how configfs works, it's been a decade since I last touched it.

Quoted 5 lines
Quoted 4 lines

And why not just use the default device name function: dev_name(), which will always return a string that will work no matter if the device is bound to a driver or not.

Quoted 6 lines
Quoted 3 lines

I can do this but then it's possible that user-space gets the name of the device which doesn't exist in sysfs. I guess we can mention that in the documentation.

Device names can change over time, nothing new there.

Ok will change in v3. I'll Cc you next time.

Bart

#24 lore raw

PATCH v2 9/12 gpio: sim: new testing module

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.

---
 Documentation/admin-guide/gpio/gpio-sim.rst |  72 ++
 drivers/gpio/Kconfig                        |   8 +
 drivers/gpio/Makefile                       |   1 +
 drivers/gpio/gpio-sim.c                     | 878 ++++++++++++++++++++
 4 files changed, 959 insertions(+)
 create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst
 create mode 100644 drivers/gpio/gpio-sim.c
Documentation/admin-guide/gpio/gpio-sim.rst new file +72 -0
new file mode 100644
index 000000000000..08eac487e35e
@@ -0,0 +1,72 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+Configfs GPIO Simulator
+=======================
+
+The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO
+chips for testing purposes. The lines exposed by these chips can be accessed
+using the standard GPIO character device interface as well as manipulated
+using sysfs attributes.
+
+Creating simulated chips
+------------------------
+
+The gpio-sim module registers a configfs subsystem called 'gpio-sim'. It's a
+subsystem with committable items which means two subdirectories are created in
+the filesystem: pending and live. For more information on configfs and
+committable items, please refer to Documentation/filesystems/configfs.rst.
+
+In order to instantiate a new simulated chip, the user needs to mkdir() a new
+directory in pending/. Inside each new directory, there's a set of attributes
+that can be used to configure the new chip. Once the configuration is complete,
+the user needs to use rename() to move the chip to the live/ directory. This
+creates and registers the new device.
+
+In order to destroy a simulated chip, it has to be moved back to pending first
+and then removed using rmdir().
+
+Currently supported configuration attributes are:
+
+  num_lines - an unsigned integer value defining the number of GPIO lines to
+              export
+
+  label - a string defining the label for the GPIO chip
+
+  line_names - a list of GPIO line names in the form of quoted strings
+               separated by commas, e.g.: '"foo", "bar", "", "foobar"'. The
+               number of strings doesn't have to be equal to the value set in
+               the num_lines attribute. If it's lower than the number of lines,
+               the remaining lines are unnamed. If it's larger, the superfluous
+               lines are ignored. A name of the form: '""' means the line
+               should be unnamed.
+
+Additionally two read-only attributes named 'chip_name' and 'dev_name' are
+exposed in order to provide users with a mapping from configfs directories to
+the actual devices created in the kernel. The former returns the name of the
+GPIO device as assigned by gpiolib (i.e. "gpiochip0", "gpiochip1", etc.). The
+latter returns the parent device name as defined by the gpio-sim driver (i.e.
+"gpio-sim.0", "gpio-sim.1", etc.). This allows user-space to map the configfs
+items both to the correct character device file as well as the associated entry
+in sysfs.
+
+Simulated GPIO chips can also be defined in device-tree. The compatible string
+must be: "gpio-simulator". Supported properties are:
+
+  "gpio-sim,label" - chip label
+
+  "gpio-sim,nr-gpios" - number of lines
+
+Other standard GPIO properties (like "gpio-line-names" and gpio-hog) are also
+supported.
+
+Manipulating simulated lines
+----------------------------
+
+Each simulated GPIO chip creates a sysfs attribute group under its device
+directory called 'line-ctrl'. Inside each group, there's a separate attribute
+for each GPIO line. The name of the attribute is of the form 'gpioX' where X
+is the line's offset in the chip.
+
+Reading from a line attribute returns the current value. Writing to it (0 or 1)
+changes the configuration of the simulated pull-up/pull-down resistor
+(1 - pull-up, 0 - pull-down).
drivers/gpio/Kconfig +8 -0
index e3607ec4c2e8..b6b6150d0d04 100644
@@ -1633,6 +1633,14 @@ config GPIO_MOCKUP
 	  tools/testing/selftests/gpio/gpio-mockup.sh. Reference the usage in
 	  it.
 
+config GPIO_SIM
+	tristate "GPIO Simulator Module"
+	select IRQ_SIM
+	select CONFIGFS_FS
+	help
+	  This enables the GPIO simulator - a configfs-based GPIO testing
+	  driver.
+
 endmenu
 
 endif
drivers/gpio/Makefile +1 -0
index c58a90a3c3b1..d717aa85f8e1 100644
@@ -130,6 +130,7 @@ obj-$(CONFIG_GPIO_SAMA5D2_PIOBU)	+= gpio-sama5d2-piobu.o
 obj-$(CONFIG_GPIO_SCH311X)		+= gpio-sch311x.o
 obj-$(CONFIG_GPIO_SCH)			+= gpio-sch.o
 obj-$(CONFIG_GPIO_SIFIVE)		+= gpio-sifive.o
+obj-$(CONFIG_GPIO_SIM)			+= gpio-sim.o
 obj-$(CONFIG_GPIO_SIOX)			+= gpio-siox.o
 obj-$(CONFIG_GPIO_SL28CPLD)		+= gpio-sl28cpld.o
 obj-$(CONFIG_GPIO_SODAVILLE)		+= gpio-sodaville.o
drivers/gpio/gpio-sim.c new file +878 -0

878 lines not on this page · raw message

Signature · 1 line
2.29.1
#25 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.

Shall we put a reference to this in the gpio-mockup documentation and mark the latter deprecated?

Quoted 409 lines
---
 Documentation/admin-guide/gpio/gpio-sim.rst |  72 ++
 drivers/gpio/Kconfig                        |   8 +
 drivers/gpio/Makefile                       |   1 +
 drivers/gpio/gpio-sim.c                     | 878 ++++++++++++++++++++
 4 files changed, 959 insertions(+)
 create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst
 create mode 100644 drivers/gpio/gpio-sim.c
Documentation/admin-guide/gpio/gpio-sim.rst new file +72 -0
new file mode 100644
index 000000000000..08eac487e35e
@@ -0,0 +1,72 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+Configfs GPIO Simulator
+=======================
+
+The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO
+chips for testing purposes. The lines exposed by these chips can be accessed
+using the standard GPIO character device interface as well as manipulated
+using sysfs attributes.
+
+Creating simulated chips
+------------------------
+
+The gpio-sim module registers a configfs subsystem called 'gpio-sim'. It's a
+subsystem with committable items which means two subdirectories are created in
+the filesystem: pending and live. For more information on configfs and
+committable items, please refer to Documentation/filesystems/configfs.rst.
+
+In order to instantiate a new simulated chip, the user needs to mkdir() a new
+directory in pending/. Inside each new directory, there's a set of attributes
+that can be used to configure the new chip. Once the configuration is complete,
+the user needs to use rename() to move the chip to the live/ directory. This
+creates and registers the new device.
+
+In order to destroy a simulated chip, it has to be moved back to pending first
+and then removed using rmdir().
+
+Currently supported configuration attributes are:
+
+  num_lines - an unsigned integer value defining the number of GPIO lines to
+              export
+
+  label - a string defining the label for the GPIO chip
+
+  line_names - a list of GPIO line names in the form of quoted strings
+               separated by commas, e.g.: '"foo", "bar", "", "foobar"'. The
+               number of strings doesn't have to be equal to the value set in
+               the num_lines attribute. If it's lower than the number of lines,
+               the remaining lines are unnamed. If it's larger, the superfluous
+               lines are ignored. A name of the form: '""' means the line
+               should be unnamed.
+
+Additionally two read-only attributes named 'chip_name' and 'dev_name' are
+exposed in order to provide users with a mapping from configfs directories to
+the actual devices created in the kernel. The former returns the name of the
+GPIO device as assigned by gpiolib (i.e. "gpiochip0", "gpiochip1", etc.). The
+latter returns the parent device name as defined by the gpio-sim driver (i.e.
+"gpio-sim.0", "gpio-sim.1", etc.). This allows user-space to map the configfs
+items both to the correct character device file as well as the associated entry
+in sysfs.
+
+Simulated GPIO chips can also be defined in device-tree. The compatible string
+must be: "gpio-simulator". Supported properties are:
+
+  "gpio-sim,label" - chip label
+
+  "gpio-sim,nr-gpios" - number of lines
+
+Other standard GPIO properties (like "gpio-line-names" and gpio-hog) are also
+supported.
+
+Manipulating simulated lines
+----------------------------
+
+Each simulated GPIO chip creates a sysfs attribute group under its device
+directory called 'line-ctrl'. Inside each group, there's a separate attribute
+for each GPIO line. The name of the attribute is of the form 'gpioX' where X
+is the line's offset in the chip.
+
+Reading from a line attribute returns the current value. Writing to it (0 or 1)
+changes the configuration of the simulated pull-up/pull-down resistor
+(1 - pull-up, 0 - pull-down).
drivers/gpio/Kconfig +8 -0
index e3607ec4c2e8..b6b6150d0d04 100644
@@ -1633,6 +1633,14 @@ config GPIO_MOCKUP
 	  tools/testing/selftests/gpio/gpio-mockup.sh. Reference the usage in
 	  it.
 
+config GPIO_SIM
+	tristate "GPIO Simulator Module"
+	select IRQ_SIM
+	select CONFIGFS_FS
+	help
+	  This enables the GPIO simulator - a configfs-based GPIO testing
+	  driver.
+
 endmenu
 
 endif
drivers/gpio/Makefile +1 -0
index c58a90a3c3b1..d717aa85f8e1 100644
@@ -130,6 +130,7 @@ obj-$(CONFIG_GPIO_SAMA5D2_PIOBU)	+= gpio-sama5d2-piobu.o
 obj-$(CONFIG_GPIO_SCH311X)		+= gpio-sch311x.o
 obj-$(CONFIG_GPIO_SCH)			+= gpio-sch.o
 obj-$(CONFIG_GPIO_SIFIVE)		+= gpio-sifive.o
+obj-$(CONFIG_GPIO_SIM)			+= gpio-sim.o
 obj-$(CONFIG_GPIO_SIOX)			+= gpio-siox.o
 obj-$(CONFIG_GPIO_SL28CPLD)		+= gpio-sl28cpld.o
 obj-$(CONFIG_GPIO_SODAVILLE)		+= gpio-sodaville.o
drivers/gpio/gpio-sim.c new file +284 -0
new file mode 100644
index 000000000000..0ed297127bca
@@ -0,0 +1,878 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * GPIO testing driver based on configfs.
+ *
+ * Copyright (C) 2021 Bartosz Golaszewski <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/bitmap.h>
+#include <linux/configfs.h>
+#include <linux/device.h>
+#include <linux/gpio/driver.h>
+#include <linux/idr.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irq_sim.h>
+#include <linux/mod_devicetable.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/string_helpers.h>
+#include <linux/sysfs.h>
+
+#include "gpiolib.h"
+
+/*
+ * This normally should correspond with the number of attributes exposed over
+ * configfs + sentinel.
+ */
+#define GPIO_SIM_MAX_PROP 4
+
+static DEFINE_IDA(gpio_sim_ida);
+
+struct gpio_sim_chip {
+	struct gpio_chip gc;
+	unsigned long *directions;
+	unsigned long *values;
+	unsigned long *pulls;
+	struct irq_domain *irq_sim;
+	struct mutex lock;
+	struct attribute_group attr_group;
+};
+
+struct gpio_sim_attribute {
+	struct device_attribute dev_attr;
+	unsigned int offset;
+};
+
+static struct gpio_sim_attribute *
+to_gpio_sim_attr(struct device_attribute *dev_attr)
+{
+	return container_of(dev_attr, struct gpio_sim_attribute, dev_attr);
+}
+
+static int gpio_sim_apply_pull(struct gpio_sim_chip *chip,
+			       unsigned int offset, int value)
+{
+	int curr_val, irq, irq_type, ret;
+	struct gpio_desc *desc;
+	struct gpio_chip *gc;
+
+	gc = &chip->gc;
+	desc = &gc->gpiodev->descs[offset];
+
+	mutex_lock(&chip->lock);
+
+	if (test_bit(FLAG_REQUESTED, &desc->flags) &&
+	    !test_bit(FLAG_IS_OUT, &desc->flags)) {
+		curr_val = !!test_bit(offset, chip->values);
+		if (curr_val == value)
+			goto set_pull;
+
+		/*
+		 * This is fine - it just means, nobody is listening
+		 * for interrupts on this line, otherwise
+		 * irq_create_mapping() would have been called from
+		 * the to_irq() callback.
+		 */
+		irq = irq_find_mapping(chip->irq_sim, offset);
+		if (!irq)
+			goto set_value;
+
+		irq_type = irq_get_trigger_type(irq);
+
+		if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) ||
+		    (!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) {
+			ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING,
+						    true);
+			if (ret)
+				goto set_pull;
+		}
+	}
+
+set_value:
+	/* Change the value unless we're actively driving the line. */
+	if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
+	    !test_bit(FLAG_IS_OUT, &desc->flags))
+		__assign_bit(offset, chip->values, value);
+
+set_pull:
+	__assign_bit(offset, chip->pulls, value);
+	mutex_unlock(&chip->lock);
+	return 0;
+}
+
+static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+	int ret;
+
+	mutex_lock(&chip->lock);
+	ret = !!test_bit(offset, chip->values);
+	mutex_unlock(&chip->lock);
+
+	return ret;
+}
+
+static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	mutex_lock(&chip->lock);
+	__assign_bit(offset, chip->values, value);
+	mutex_unlock(&chip->lock);
+}
+
+static int gpio_sim_get_multiple(struct gpio_chip *gc,
+				 unsigned long *mask, unsigned long *bits)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	mutex_lock(&chip->lock);
+	bitmap_copy(bits, chip->values, gc->ngpio);
+	mutex_unlock(&chip->lock);
+
+	return 0;
+}
+
+static void gpio_sim_set_multiple(struct gpio_chip *gc,
+				  unsigned long *mask, unsigned long *bits)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	mutex_lock(&chip->lock);
+	bitmap_copy(chip->values, bits, gc->ngpio);
+	mutex_unlock(&chip->lock);
+}
+
+static int gpio_sim_direction_output(struct gpio_chip *gc,
+				     unsigned int offset, int value)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	mutex_lock(&chip->lock);
+	__clear_bit(offset, chip->directions);
+	__assign_bit(offset, chip->values, value);
+	mutex_unlock(&chip->lock);
+
+	return 0;
+}
+
+static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	mutex_lock(&chip->lock);
+	__set_bit(offset, chip->directions);
+	mutex_unlock(&chip->lock);
+
+	return 0;
+}
+
+static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+	int direction;
+
+	mutex_lock(&chip->lock);
+	direction = !!test_bit(offset, chip->directions);
+	mutex_unlock(&chip->lock);
+
+	return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
+}
+
+static int gpio_sim_set_config(struct gpio_chip *gc,
+				  unsigned int offset, unsigned long config)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	switch (pinconf_to_config_param(config)) {
+	case PIN_CONFIG_BIAS_PULL_UP:
+		return gpio_sim_apply_pull(chip, offset, 1);
+	case PIN_CONFIG_BIAS_PULL_DOWN:
+		return gpio_sim_apply_pull(chip, offset, 0);
+	default:
+		break;
+	}
+
+	return -ENOTSUPP;
+}
+
+static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	return irq_create_mapping(chip->irq_sim, offset);
+}
+
+static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset)
+{
+	struct gpio_sim_chip *chip = gpiochip_get_data(gc);
+
+	mutex_lock(&chip->lock);
+	__assign_bit(offset, chip->values, !!test_bit(offset, chip->pulls));
+	mutex_unlock(&chip->lock);
+}
+
+static ssize_t gpio_sim_sysfs_line_show(struct device *dev,
+					struct device_attribute *attr,
+					char *buf)
+{
+	struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
+	struct gpio_sim_chip *chip = dev_get_drvdata(dev);
+	int ret;
+
+	mutex_lock(&chip->lock);
+	ret = sprintf(buf, "%u\n", !!test_bit(line_attr->offset, chip->values));
+	mutex_unlock(&chip->lock);
+
+	return ret;
+}
+
+static ssize_t gpio_sim_sysfs_line_store(struct device *dev,
+					 struct device_attribute *attr,
+					 const char *buf, size_t len)
+{
+	struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr);
+	struct gpio_sim_chip *chip = dev_get_drvdata(dev);
+	int ret, val;
+
+	if (len > 2 || (buf[0] != '0' && buf[0] != '1'))
+		return -EINVAL;
+
+	val = buf[0] == '0' ? 0 : 1;
+
+	ret = gpio_sim_apply_pull(chip, line_attr->offset, val);
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static void gpio_sim_mutex_destroy(void *data)
+{
+	struct mutex *lock = data;
+
+	mutex_destroy(lock);
+}
+
+static void gpio_sim_sysfs_remove(void *data)
+{
+	struct gpio_sim_chip *chip = data;
+
+	sysfs_remove_group(&chip->gc.parent->kobj, &chip->attr_group);
+}
+
+static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip)
+{
+	unsigned int i, num_lines = chip->gc.ngpio;
+	struct device *dev = chip->gc.parent;
+	struct gpio_sim_attribute *line_attr;
+	struct device_attribute *dev_attr;
+	struct attribute **attrs;
+	int ret;
+
+	attrs = devm_kcalloc(dev, sizeof(*attrs), num_lines + 1, GFP_KERNEL);
+	if (!attrs)
+		return -ENOMEM;
+
+	for (i = 0; i < num_lines; i++) {

drivers/gpio/gpio-sim.c line 294

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+		line_attr = devm_kzalloc(dev, sizeof(*line_attr), GFP_KERNEL);
+		if (!line_attr)
+			return -ENOMEM;
+
+		line_attr->offset = i;
+
+		dev_attr = &line_attr->dev_attr;
 
+		dev_attr->attr.name = devm_kasprintf(dev, GFP_KERNEL,
+						     "gpio%u", i);

Reads better as one line.

Quoted 33 lines
drivers/gpio/gpio-sim.c new file +33 -0
+		if (!dev_attr->attr.name)
+			return -ENOMEM;
+
+		dev_attr->attr.mode = 0644;
+
+		dev_attr->show = gpio_sim_sysfs_line_show;
+		dev_attr->store = gpio_sim_sysfs_line_store;
+
+		attrs[i] = &dev_attr->attr;
+	}
+
+	chip->attr_group.name = "line-ctrl";
+	chip->attr_group.attrs = attrs;
+
+	ret = sysfs_create_group(&dev->kobj, &chip->attr_group);
+	if (ret)
+		return ret;
+
+	return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip);
+}
+
+static int gpio_sim_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct gpio_sim_chip *chip;
+	struct gpio_chip *gc;
+	const char *label;
+	u32 num_lines;
+	int ret;
+
+	ret = device_property_read_u32(dev, "gpio-sim,nr-gpios", &num_lines);
+	if (ret)
+		return ret;

drivers/gpio/gpio-sim.c line 337

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +10 -0
+
+	ret = device_property_read_string(dev, "gpio-sim,label", &label);
+	if (ret)
+		label = dev_name(dev);
+
+	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
+	if (!chip)
+		return -ENOMEM;
+
+	chip->directions = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);

What's the point to have "z" variant here...

drivers/gpio/gpio-sim.c line 342

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +5 -0
+	if (!chip->directions)
+		return -ENOMEM;
+
+	/* Default to input mode. */
+	bitmap_fill(chip->directions, num_lines);

...followed by filling all 1:s here?

Quoted 236 lines
drivers/gpio/gpio-sim.c new file +236 -0
+	chip->values = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
+	if (!chip->values)
+		return -ENOMEM;
+
+	chip->pulls = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL);
+	if (!chip->pulls)
+		return -ENOMEM;
+
+	chip->irq_sim = devm_irq_domain_create_sim(dev, NULL, num_lines);
+	if (IS_ERR(chip->irq_sim))
+		return PTR_ERR(chip->irq_sim);
+
+	mutex_init(&chip->lock);
+	ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy,
+				       &chip->lock);
+	if (ret)
+		return ret;
+
+	gc = &chip->gc;
+	gc->base = -1;
+	gc->ngpio = num_lines;
+	gc->label = label;
+	gc->owner = THIS_MODULE;
+	gc->parent = dev;
+	gc->get = gpio_sim_get;
+	gc->set = gpio_sim_set;
+	gc->get_multiple = gpio_sim_get_multiple;
+	gc->set_multiple = gpio_sim_set_multiple;
+	gc->direction_output = gpio_sim_direction_output;
+	gc->direction_input = gpio_sim_direction_input;
+	gc->get_direction = gpio_sim_get_direction;
+	gc->set_config = gpio_sim_set_config;
+	gc->to_irq = gpio_sim_to_irq;
+	gc->free = gpio_sim_free;
+
+	ret = devm_gpiochip_add_data(dev, gc, chip);
+	if (ret)
+		return ret;
+
+	/* Used by sysfs and configfs callbacks. */
+	dev_set_drvdata(dev, chip);
+
+	ret = gpio_sim_setup_sysfs(chip);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static const struct of_device_id gpio_sim_of_match[] = {
+	{ .compatible = "gpio-simulator" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, gpio_sim_of_match);
+
+static struct platform_driver gpio_sim_driver = {
+	.driver = {
+		.name = "gpio-sim",
+	},
+	.probe = gpio_sim_probe,
+};
+
+struct gpio_sim_chip_config {
+	struct config_item item;
+
+	/*
+	 * If pdev is NULL, the item is 'pending' (waiting for configuration).
+	 * Once the pointer is assigned, the device has been created and the
+	 * item is 'live'.
+	 */
+	struct platform_device *pdev;
+
+	/*
+	 * Each configfs filesystem operation is protected with the subsystem
+	 * mutex. Each separate attribute is protected with the buffer mutex.
+	 * This structure however can be modified by callbacks of different
+	 * attributes so we need another lock.
+	 */
+	struct mutex lock;
+
+	char label[32];
+	unsigned int num_lines;
+	char **line_names;
+	unsigned int num_line_names;
+};
+
+static struct gpio_sim_chip_config *
+to_gpio_sim_chip_config(struct config_item *item)
+{
+	return container_of(item, struct gpio_sim_chip_config, item);
+}
+
+static ssize_t gpio_sim_config_dev_name_show(struct config_item *item,
+					     char *page)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	struct platform_device *pdev;
+	int ret;
+
+	mutex_lock(&config->lock);
+	pdev = config->pdev;
+	if (pdev && device_is_bound(&pdev->dev))
+		ret = sprintf(page, "%s\n", dev_name(&pdev->dev));
+	else
+		ret = -ENODEV;
+	mutex_unlock(&config->lock);
+
+	return ret;
+}
+
+CONFIGFS_ATTR_RO(gpio_sim_config_, dev_name);
+
+static ssize_t gpio_sim_config_chip_name_show(struct config_item *item,
+					      char *page)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	struct platform_device *pdev;
+	struct gpio_sim_chip *chip;
+	int ret;
+
+	mutex_lock(&config->lock);
+	pdev = config->pdev;
+	if (pdev && device_is_bound(&pdev->dev)) {
+		chip = dev_get_drvdata(&pdev->dev);
+		ret = sprintf(page, "%s\n", dev_name(&chip->gc.gpiodev->dev));
+	} else {
+		ret = -ENODEV;
+	}
+	mutex_unlock(&config->lock);
+
+	return ret;
+}
+
+CONFIGFS_ATTR_RO(gpio_sim_config_, chip_name);
+
+static ssize_t gpio_sim_config_label_show(struct config_item *item, char *page)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	int ret;
+
+	mutex_lock(&config->lock);
+	ret = sprintf(page, "%s\n", config->label);
+	mutex_unlock(&config->lock);
+
+	return ret;
+}
+
+static ssize_t gpio_sim_config_label_store(struct config_item *item,
+					   const char *page, size_t count)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	char *dup, *trimmed;
+	int ret;
+
+	mutex_lock(&config->lock);
+
+	if (config->pdev) {
+		mutex_unlock(&config->lock);
+		return -EBUSY;
+	}
+
+	dup = kstrndup(page, count, GFP_KERNEL);
+	if (!dup) {
+		mutex_unlock(&config->lock);
+		return -ENOMEM;
+	}
+
+	trimmed = strstrip(dup);
+	ret = snprintf(config->label, sizeof(config->label), "%s", trimmed);
+	kfree(dup);
+	if (ret < 0) {
+		mutex_unlock(&config->lock);
+		return ret;
+	}
+
+	mutex_unlock(&config->lock);
+	return count;
+}
+
+CONFIGFS_ATTR(gpio_sim_config_, label);
+
+static ssize_t gpio_sim_config_num_lines_show(struct config_item *item,
+					      char *page)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	int ret;
+
+	mutex_lock(&config->lock);
+	ret = sprintf(page, "%u\n", config->num_lines);
+	mutex_unlock(&config->lock);
+
+	return ret;
+}
+
+static ssize_t gpio_sim_config_num_lines_store(struct config_item *item,
+					       const char *page, size_t count)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	unsigned int num_lines;
+	int ret;
+
+	mutex_lock(&config->lock);
+
+	if (config->pdev) {
+		mutex_unlock(&config->lock);
+		return -EBUSY;
+	}
+
+	ret = kstrtouint(page, 10, &num_lines);
+	if (ret) {
+		mutex_unlock(&config->lock);
+		return ret;
+	}
+
+	if (num_lines == 0) {
+		mutex_unlock(&config->lock);
+		return -EINVAL;
+	}
+
+	config->num_lines = num_lines;
+
+	mutex_unlock(&config->lock);
+	return count;
+}
+
+CONFIGFS_ATTR(gpio_sim_config_, num_lines);
+
+static ssize_t gpio_sim_config_line_names_show(struct config_item *item,
+					       char *page)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	int ret, i, written = 0;
+
+	mutex_lock(&config->lock);
+
+	if (!config->line_names) {

drivers/gpio/gpio-sim.c line 588

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+		mutex_unlock(&config->lock);
+		return sprintf(page, "\n");
+	}
+
+	for (i = 0; i < config->num_line_names; i++) {
 
+		ret = sprintf(page + written,
+			i < config->num_line_names - 1 ?
+				"\"%s\", " : "\"%s\"\n",
+			config->line_names[i] ?: "");

Indentation here looks not the best...

Quoted 49 lines
drivers/gpio/gpio-sim.c new file +48 -0
+		if (ret < 0) {
+			mutex_unlock(&config->lock);
+			return ret;
+		}
+
+		written += ret;
+	}
 
+	mutex_unlock(&config->lock);
+	return written;
+}
+
+static ssize_t gpio_sim_config_line_names_store(struct config_item *item,
+						const char *page, size_t count)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	unsigned int num_new_names = 1, num_old_names, name_idx = 0;
+	bool in_quote = false, got_comma = true;
+	char **new_names, **old_names, *name, c;
+	const char *start = page;
+	size_t pos, name_len;
+	int err = -EINVAL;
+
+	mutex_lock(&config->lock);
+
+	if (config->pdev) {
+		mutex_unlock(&config->lock);
+		return -EBUSY;
+	}
+
+	/*
+	 * Line names are stored in a pointer array so that we can easily
+	 * pass them down to the GPIO subsystem in a "gpio-line-names"
+	 * property.
+	 *
+	 * Line names must be passed as a list of quoted names separated by
+	 * commas, for example: '"foo", "bar", "foobar"'.
+	 */
+
+	for (pos = 0; pos < count; pos++) {
+		/*
+		 * Just count the commas and assume the number if strings
+		 * equals the number of commas + 1. If the format is wrong
+		 * we'll bail out anyway.
+		 */
+		if (page[pos] == ',')
+			num_new_names++;
+	}
+

drivers/gpio/gpio-sim.c line 647

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +10 -0
+	new_names = kcalloc(num_new_names, sizeof(char *), GFP_KERNEL);
+	if (!new_names) {
+		mutex_unlock(&config->lock);
+		return -ENOMEM;
+	}
+
+	/*
+	 * FIXME If anyone knows a better way to parse that - please let me
+	 * know.
+	 */

If comma can be replaced with ' ' (space) then why not to use next_arg() from cmdline.c? I.o.w. do you have strong opinion why should we use comma here?

Quoted 11 lines
drivers/gpio/gpio-sim.c new file +11 -0
+	for (pos = 0; pos < count; pos++) {
+		c = page[pos];
+
+		if (in_quote) {
+			if (c == '"') {
+				/* This is the end of the name. */
+				in_quote = got_comma = false;
+				name_len = (page + pos) - start;
+				if (name_len == 0) {
+					/* Name is empty (passed as ""). */
+					name_idx++;

drivers/gpio/gpio-sim.c line 668

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+					continue;
+				}
 
+				name = kzalloc(name_len + 1, GFP_KERNEL);
+				if (!name) {
+					err = -ENOMEM;
+					goto err_out;
+				}
+
+				memcpy(name, start, name_len);

kmemdup()/kstrndup() or so?

Quoted 140 lines
drivers/gpio/gpio-sim.c new file +140 -0
+				new_names[name_idx++] = name;
+			}
+		} else {
+			if (c == '"') {
+				/* Enforce separating names with commas. */
+				if (!got_comma)
+					goto err_out;
+
+				start = page + pos + 1;
+				in_quote = true;
+			} else if (c == ',') {
+				if (!got_comma)
+					got_comma = true;
+				else
+					/* Double commas are not allowed. */
+					goto err_out;
+			} else if (!isspace(c)) {
+				goto err_out;
+			}
+		}
+	}
+
+	/*
+	 * End of input sanity checks, must not have a comma at the end and
+	 * must have finished scanning the last name.
+	 */
+	if (in_quote || got_comma)
+		goto err_out;
+
+	old_names = config->line_names;
+	num_old_names = config->num_line_names;
+	config->line_names = new_names;
+	config->num_line_names = num_new_names;
+
+	mutex_unlock(&config->lock);
+	kfree_strarray(old_names, num_old_names);
+	return count;
+
+err_out:
+	mutex_unlock(&config->lock);
+	kfree_strarray(new_names, name_idx);
+	return err;
+}
+
+CONFIGFS_ATTR(gpio_sim_config_, line_names);
+
+static struct configfs_attribute *gpio_sim_config_attrs[] = {
+	&gpio_sim_config_attr_dev_name,
+	&gpio_sim_config_attr_chip_name,
+	&gpio_sim_config_attr_label,
+	&gpio_sim_config_attr_num_lines,
+	&gpio_sim_config_attr_line_names,
+	NULL
+};
+
+static void gpio_sim_chip_config_release(struct config_item *item)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+
+	mutex_destroy(&config->lock);
+	kfree_strarray(config->line_names, config->num_line_names);
+	kfree(config);
+}
+
+static struct configfs_item_operations gpio_sim_config_item_ops = {
+	.release	= gpio_sim_chip_config_release,
+};
+
+static const struct config_item_type gpio_sim_chip_config_type = {
+	.ct_item_ops	= &gpio_sim_config_item_ops,
+	.ct_attrs	= gpio_sim_config_attrs,
+	.ct_owner	= THIS_MODULE,
+};
+
+static struct config_item *
+gpio_sim_config_make_item(struct config_group *group, const char *name)
+{
+	struct gpio_sim_chip_config *config;
+
+	config = kzalloc(sizeof(*config), GFP_KERNEL);
+	if (!config)
+		return ERR_PTR(-ENOMEM);
+
+	config_item_init_type_name(&config->item, name,
+				   &gpio_sim_chip_config_type);
+	config->num_lines = 1;
+	mutex_init(&config->lock);
+
+	return &config->item;
+}
+
+static int gpio_sim_config_commit_item(struct config_item *item)
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	struct property_entry properties[GPIO_SIM_MAX_PROP];
+	struct platform_device_info pdevinfo;
+	struct platform_device *pdev;
+	unsigned int prop_idx = 0;
+
+	memset(&pdevinfo, 0, sizeof(pdevinfo));
+	memset(properties, 0, sizeof(properties));
+
+	mutex_lock(&config->lock);
+
+	properties[prop_idx++] = PROPERTY_ENTRY_U32("gpio-sim,nr-gpios",
+						    config->num_lines);
+
+	if (config->label[0] != '\0')
+		properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label",
+							       config->label);
+
+	if (config->line_names)
+		properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN(
+						"gpio-line-names",
+						config->line_names,
+						config->num_line_names);
+
+	pdevinfo.id = ida_alloc(&gpio_sim_ida, GFP_KERNEL);
+	if (pdevinfo.id < 0) {
+		mutex_unlock(&config->lock);
+		return pdevinfo.id;
+	}
+
+	pdevinfo.name = "gpio-sim";
+	pdevinfo.properties = properties;
+
+	pdev = platform_device_register_full(&pdevinfo);
+	if (IS_ERR(pdev)) {
+		ida_free(&gpio_sim_ida, pdevinfo.id);
+		mutex_unlock(&config->lock);
+		return PTR_ERR(pdev);
+	}
+
+	config->pdev = pdev;
+	mutex_unlock(&config->lock);
+
+	return 0;
+}
+
+static int gpio_sim_config_uncommit_item(struct config_item *item)

drivers/gpio/gpio-sim.c line 817

Quoted 10 lines
drivers/gpio/gpio-sim.c new file +9 -0
+{
+	struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+	int id;
+
+	mutex_lock(&config->lock);
+	id = config->pdev->id;
+	platform_device_unregister(config->pdev);
+	config->pdev = NULL;
 
+	ida_free(&gpio_sim_ida, id);

Isn't it atomic per se? I mean that IDA won't give the same ID until you free it. I.o.w. why is it under the mutex?

Quoted 64 lines
drivers/gpio/gpio-sim.c new file +61 -0
+	mutex_unlock(&config->lock);
+
+	return 0;
+}
+
+static struct configfs_group_operations gpio_sim_config_group_ops = {
+	.make_item	= gpio_sim_config_make_item,
+	.commit_item	= gpio_sim_config_commit_item,
+	.uncommit_item	= gpio_sim_config_uncommit_item,
+};
+
+static const struct config_item_type gpio_sim_config_type = {
+	.ct_group_ops	= &gpio_sim_config_group_ops,
+	.ct_owner	= THIS_MODULE,
+};
+
+static struct configfs_subsystem gpio_sim_config_subsys = {
+	.su_group = {
+		.cg_item = {
+			.ci_namebuf	= "gpio-sim",
+			.ci_type	= &gpio_sim_config_type,
+		},
+	},
+};
+
+static int __init gpio_sim_init(void)
+{
+	int ret;
+
+	ret = platform_driver_register(&gpio_sim_driver);
+	if (ret) {
+		pr_err("Error %d while registering the platform driver\n", ret);
+		return ret;
+	}
+
+	config_group_init(&gpio_sim_config_subsys.su_group);
+	mutex_init(&gpio_sim_config_subsys.su_mutex);
+	ret = configfs_register_subsystem(&gpio_sim_config_subsys);
+	if (ret) {
+		pr_err("Error %d while registering the configfs subsystem %s\n",
+		       ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf);
+		mutex_destroy(&gpio_sim_config_subsys.su_mutex);
+		platform_driver_unregister(&gpio_sim_driver);
+		return ret;
+	}
+
+	return 0;
+}
+module_init(gpio_sim_init);
+
+static void __exit gpio_sim_exit(void)
+{
+	configfs_unregister_subsystem(&gpio_sim_config_subsys);
+	mutex_destroy(&gpio_sim_config_subsys.su_mutex);
+	platform_driver_unregister(&gpio_sim_driver);
+}
+module_exit(gpio_sim_exit);
+
+MODULE_AUTHOR("Bartosz Golaszewski <[email protected]>");
+MODULE_DESCRIPTION("GPIO Simulator Module");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.29.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#26 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 9 lines
Quoted 5 lines

Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup.

Shall we put a reference to this in the gpio-mockup documentation and mark the latter deprecated?

I don't think it's necessary right away. Let's phase out gpio-mockup once this one gets some attention (for example: after libgpiod switches to using it).

[snip]

Quoted 6 lines

drivers/gpio/gpio-sim.c line 294

Quoted 3 lines
drivers/gpio/gpio-sim.c new file +2 -0
+             dev_attr->attr.name = devm_kasprintf(dev, GFP_KERNEL,
+                                                  "gpio%u", i);

Reads better as one line.

Yeah, so the removal of the 80 characters limit should not be abused when there's no need for it - this doesn't look that bad really with a broken line. Same elsewhere where the limit is exceeded.

[snip]

Quoted 8 lines

drivers/gpio/gpio-sim.c line 588

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +4 -0
+             ret = sprintf(page + written,
+                     i < config->num_line_names - 1 ?
+                             "\"%s\", " : "\"%s\"\n",
+                     config->line_names[i] ?: "");

Indentation here looks not the best...

So this is the place where it may make sense to go over 80 chars.

[snip]

Quoted 9 lines

drivers/gpio/gpio-sim.c line 647

Quoted 5 lines
drivers/gpio/gpio-sim.c new file +5 -0
+
+     /*
+      * FIXME If anyone knows a better way to parse that - please let me
+      * know.
+      */

If comma can be replaced with ' ' (space) then why not to use next_arg() from cmdline.c? I.o.w. do you have strong opinion why should we use comma here?

My opinion is not very strong but I wanted to make the list of names resemble what we pass to the gpio-line-names property in device tree. Doesn't next_arg() react differently to string of the form: "foo=bar"?

[snip]

Quoted 11 lines
Quoted 10 lines
drivers/gpio/gpio-sim.c new file +10 -0
+
+static int gpio_sim_config_uncommit_item(struct config_item *item)
+{
+     struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item);
+     int id;
+
+     mutex_lock(&config->lock);
+     id = config->pdev->id;
+     platform_device_unregister(config->pdev);
+     config->pdev = NULL;
Quoted 5 lines
Quoted 1 line

+ ida_free(&gpio_sim_ida, id);

Isn't it atomic per se? I mean that IDA won't give the same ID until you free it. I.o.w. why is it under the mutex?

You're right but if we rapidly create and destroy chips we'll be left with holes in the numbering (because new devices would be created before the IDA numbers are freed, so the driver would take a larger number that's currently free). It doesn't hurt but it would look worse IMO. Do you have a strong opinion on this?

[snip]

I'll address issues I didn't comment on.

Thanks for the review!
Bart

#27 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

drivers/gpio/gpio-sim.c new file +5 -0
+
+     /*
+      * FIXME If anyone knows a better way to parse that - please let me
+      * know.
+      */
Quoted 7 lines
Quoted 3 lines

If comma can be replaced with ' ' (space) then why not to use next_arg() from cmdline.c? I.o.w. do you have strong opinion why should we use comma here?

My opinion is not very strong but I wanted to make the list of names resemble what we pass to the gpio-line-names property in device tree. Doesn't next_arg() react differently to string of the form: "foo=bar"?

It's ambiguous here.

So, the strings '"foo=bar"' and 'foo=bar' (w/o single quotes!) are indeed parsed differently, i.e.

	'"foo=bar"' -> 'foo=bar',
while
	"foo=bar" -> 'foo' + 'bar'.

...

Quoted 11 lines
Quoted 5 lines
Quoted 1 line

+ ida_free(&gpio_sim_ida, id);

Isn't it atomic per se? I mean that IDA won't give the same ID until you free it. I.o.w. why is it under the mutex?

You're right but if we rapidly create and destroy chips we'll be left with holes in the numbering (because new devices would be created before the IDA numbers are freed, so the driver would take a larger number that's currently free). It doesn't hurt but it would look worse IMO. Do you have a strong opinion on this?

It's not strong per se, but I would rather follow the 2nd rule of locking:
don't protect something which doesn't need it.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#28 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 21 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

drivers/gpio/gpio-sim.c new file +5 -0
+
+     /*
+      * FIXME If anyone knows a better way to parse that - please let me
+      * know.
+      */
Quoted 7 lines
Quoted 3 lines

If comma can be replaced with ' ' (space) then why not to use next_arg() from cmdline.c? I.o.w. do you have strong opinion why should we use comma here?

My opinion is not very strong but I wanted to make the list of names resemble what we pass to the gpio-line-names property in device tree. Doesn't next_arg() react differently to string of the form: "foo=bar"?

Quoted 8 lines

It's ambiguous here.

So, the strings '"foo=bar"' and 'foo=bar' (w/o single quotes!) are indeed parsed differently, i.e.

        '"foo=bar"' -> 'foo=bar',
while
        "foo=bar" -> 'foo' + 'bar'.

IMO '"foo", "bar", "", "foobar"' looks better than '"foo" "bar" "" "foobar"' and I'm also not sure next_arg will understand an empty quote?

If you're not objecting strongly, then I would prefer my version.

Quoted 8 lines

...

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

+ ida_free(&gpio_sim_ida, id);

Isn't it atomic per se? I mean that IDA won't give the same ID until you free it. I.o.w. why is it under the mutex?

Quoted 9 lines
Quoted 5 lines

You're right but if we rapidly create and destroy chips we'll be left with holes in the numbering (because new devices would be created before the IDA numbers are freed, so the driver would take a larger number that's currently free). It doesn't hurt but it would look worse IMO. Do you have a strong opinion on this?

It's not strong per se, but I would rather follow the 2nd rule of locking:
don't protect something which doesn't need it.

OK, makes sense.

Quoted 5 lines

--
With Best Regards,
Andy Shevchenko

#29 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 31 lines · Bartosz Golaszewski
Quoted 20 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

drivers/gpio/gpio-sim.c new file +5 -0
+
+     /*
+      * FIXME If anyone knows a better way to parse that - please let me
+      * know.
+      */
Quoted 7 lines
Quoted 3 lines

If comma can be replaced with ' ' (space) then why not to use next_arg() from cmdline.c? I.o.w. do you have strong opinion why should we use comma here?

My opinion is not very strong but I wanted to make the list of names resemble what we pass to the gpio-line-names property in device tree. Doesn't next_arg() react differently to string of the form: "foo=bar"?

Quoted 8 lines

It's ambiguous here.

So, the strings '"foo=bar"' and 'foo=bar' (w/o single quotes!) are indeed parsed differently, i.e.

        '"foo=bar"' -> 'foo=bar',
while
        "foo=bar" -> 'foo' + 'bar'.
Quoted 3 lines

IMO '"foo", "bar", "", "foobar"' looks better than '"foo" "bar" "" "foobar"' and I'm also not sure next_arg will understand an empty quote?

I guess it understands it. But I agree that comma-separated it would look better.

Quoted 1 line

If you're not objecting strongly, then I would prefer my version.

I have strong opinion not to open code "yet another parser".

So, grepping on 'strsep(.*, ",")' shows a lot of code that wants something like this. Interesting are the net/9p cases. This in particular pointed out to lib/parser.c which in turn shows promising match_strlcpy() / match_strdup(). I haven't looked deeply though.

That said, I agree that next_arg() is not the best here.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#30 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 49 lines · Andy Shevchenko
Quoted 31 lines · Bartosz Golaszewski
Quoted 20 lines · Andy Shevchenko
Quoted 11 lines · Bartosz Golaszewski
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

drivers/gpio/gpio-sim.c new file +5 -0
+
+     /*
+      * FIXME If anyone knows a better way to parse that - please let me
+      * know.
+      */
Quoted 7 lines
Quoted 3 lines

If comma can be replaced with ' ' (space) then why not to use next_arg() from cmdline.c? I.o.w. do you have strong opinion why should we use comma here?

My opinion is not very strong but I wanted to make the list of names resemble what we pass to the gpio-line-names property in device tree. Doesn't next_arg() react differently to string of the form: "foo=bar"?

Quoted 8 lines

It's ambiguous here.

So, the strings '"foo=bar"' and 'foo=bar' (w/o single quotes!) are indeed parsed differently, i.e.

        '"foo=bar"' -> 'foo=bar',
while
        "foo=bar" -> 'foo' + 'bar'.
Quoted 3 lines

IMO '"foo", "bar", "", "foobar"' looks better than '"foo" "bar" "" "foobar"' and I'm also not sure next_arg will understand an empty quote?

I guess it understands it. But I agree that comma-separated it would look better.

Quoted 1 line

If you're not objecting strongly, then I would prefer my version.

I have strong opinion not to open code "yet another parser".

So, grepping on 'strsep(.*, ",")' shows a lot of code that wants something like this. Interesting are the net/9p cases. This in particular pointed out to lib/parser.c which in turn shows promising match_strlcpy() / match_strdup(). I haven't looked deeply though.

Quoted 7 lines

That said, I agree that next_arg() is not the best here.

--
With Best Regards,
Andy Shevchenko

Shall we revisit this once it's upstream with a generalization for separating comma separated strings?

Bart

#31 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Mon, Mar 08, 2021 at 03:23:31PM +0100, Bartosz Golaszewski wrote:

...

Quoted 11 lines
Quoted 8 lines

I have strong opinion not to open code "yet another parser".

So, grepping on 'strsep(.*, ",")' shows a lot of code that wants something like this. Interesting are the net/9p cases. This in particular pointed out to lib/parser.c which in turn shows promising match_strlcpy() / match_strdup(). I haven't looked deeply though.

That said, I agree that next_arg() is not the best here.

Shall we revisit this once it's upstream with a generalization for separating comma separated strings?

How can we guarantee it won't be forgotten?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#32 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 15 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Mon, Mar 08, 2021 at 03:23:31PM +0100, Bartosz Golaszewski wrote:

...

Quoted 7 lines
Quoted 7 lines

I have strong opinion not to open code "yet another parser".

So, grepping on 'strsep(.*, ",")' shows a lot of code that wants something like this. Interesting are the net/9p cases. This in particular pointed out to lib/parser.c which in turn shows promising match_strlcpy() / match_strdup(). I haven't looked deeply though.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

That said, I agree that next_arg() is not the best here.

Shall we revisit this once it's upstream with a generalization for separating comma separated strings?

How can we guarantee it won't be forgotten?

I will add a REVISIT comment, so *obviously* it ***will*** be revisited. :)

Bartosz

#33 lore raw

Re: [PATCH v2 09/12] gpio: sim: new testing module

Quoted 19 lines · Bartosz Golaszewski
Quoted 17 lines · Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
Quoted 1 line · Andy Shevchenko

On Mon, Mar 08, 2021 at 03:23:31PM +0100, Bartosz Golaszewski wrote:

...

Quoted 9 lines
Quoted 8 lines

I have strong opinion not to open code "yet another parser".

So, grepping on 'strsep(.*, ",")' shows a lot of code that wants something like this. Interesting are the net/9p cases. This in particular pointed out to lib/parser.c which in turn shows promising match_strlcpy() / match_strdup(). I haven't looked deeply though.

That said, I agree that next_arg() is not the best here.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Shall we revisit this once it's upstream with a generalization for separating comma separated strings?

How can we guarantee it won't be forgotten?

I will add a REVISIT comment, so *obviously* it ***will*** be revisited. :)

Fine by me!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#34 lore raw

PATCH v2 10/12 selftests: gpio: provide a helper for reading chip info

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add a simple program that allows to retrieve chip properties from the GPIO character device. This will be used in gpio-sim selftests.

---
 tools/testing/selftests/gpio/.gitignore       |  1 +
 tools/testing/selftests/gpio/Makefile         |  2 +-
 tools/testing/selftests/gpio/gpio-chip-info.c | 57 +++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c
tools/testing/selftests/gpio/.gitignore +1 -0
index a4969f7ee020..4ea4f58dab1a 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 gpio-mockup-cdev
+gpio-chip-info
tools/testing/selftests/gpio/Makefile +1 -1
index 39f2bbe8dd3d..84b48547f94c 100644
@@ -2,6 +2,6 @@
 
 TEST_PROGS := gpio-mockup.sh
 TEST_FILES := gpio-mockup-sysfs.sh
-TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev
+TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info
 
 include ../lib.mk
tools/testing/selftests/gpio/gpio-chip-info.c new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.29.1
#35 lore raw

PATCH v2 11/12 selftests: gpio: add a helper for reading GPIO line names

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add a simple program that allows to read GPIO line names from the character device. This will be used in gpio-sim selftests.

---
 tools/testing/selftests/gpio/.gitignore       |  1 +
 tools/testing/selftests/gpio/Makefile         |  2 +-
 tools/testing/selftests/gpio/gpio-line-name.c | 55 +++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c
tools/testing/selftests/gpio/.gitignore +1 -0
index 4ea4f58dab1a..ededb077a3a6 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 gpio-mockup-cdev
 gpio-chip-info
+gpio-line-name
tools/testing/selftests/gpio/Makefile +1 -1
index 84b48547f94c..d7d8f1985d99 100644
@@ -2,6 +2,6 @@
 
 TEST_PROGS := gpio-mockup.sh
 TEST_FILES := gpio-mockup-sysfs.sh
-TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info
+TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name
 
 include ../lib.mk
tools/testing/selftests/gpio/gpio-line-name.c new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.29.1
#36 lore raw

PATCH v2 12/12 selftests: gpio: add test cases for gpio-sim

In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Add a set of tests for the new gpio-sim module. This is a pure shell test-suite and uses the helper programs available in the gpio selftests directory. These test-cases only test the functionalities exposed by the gpio-sim driver, not those handled by core gpiolib code.

---
 tools/testing/selftests/gpio/Makefile    |   2 +-
 tools/testing/selftests/gpio/config      |   1 +
 tools/testing/selftests/gpio/gpio-sim.sh | 229 +++++++++++++++++++++++
 3 files changed, 231 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh
tools/testing/selftests/gpio/Makefile +1 -1
index d7d8f1985d99..4c6df61c76a8 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-TEST_PROGS := gpio-mockup.sh
+TEST_PROGS := gpio-mockup.sh gpio-sim.sh
 TEST_FILES := gpio-mockup-sysfs.sh
 TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name
 
tools/testing/selftests/gpio/config +1 -0
index ce100342c20b..409a8532facc 100644
@@ -1,3 +1,4 @@
 CONFIG_GPIOLIB=y
 CONFIG_GPIO_CDEV=y
 CONFIG_GPIO_MOCKUP=m
+CONFIG_GPIO_SIM=m
tools/testing/selftests/gpio/gpio-sim.sh new file +229 -0

229 lines not on this page · raw message

Signature · 1 line
2.29.1