* [PATCH 1/2] cdev: simplify loop in cdev_by_device_node
@ 2024-01-03 10:20 Ahmad Fatoum
2024-01-03 10:20 ` [PATCH 2/2] cdev: add cdev_alloc helper for creating cdevs Ahmad Fatoum
2024-01-04 11:17 ` [PATCH 1/2] cdev: simplify loop in cdev_by_device_node Sascha Hauer
0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-01-03 10:20 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We don't need to compare the cdev's device node pointer both against NULL
and against the searched for device node on each iteration.
Instead, it's sufficient to just compare the device nodes directly.
To maintain previous behavior, when searching for a NULL device node, we
shouldn't return the first cdev, but return NULL. Do that via an early
exit instead of doing it after iterating over all cdevs.
No functional change.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
fs/devfs-core.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 244f76f62c52..c79b092a112e 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -87,9 +87,10 @@ struct cdev *cdev_by_device_node(struct device_node *node)
{
struct cdev *cdev;
+ if (!node)
+ return NULL;
+
for_each_cdev(cdev) {
- if (!cdev->device_node)
- continue;
if (cdev->device_node == node)
return cdev_readlink(cdev);
}
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] cdev: add cdev_alloc helper for creating cdevs
2024-01-03 10:20 [PATCH 1/2] cdev: simplify loop in cdev_by_device_node Ahmad Fatoum
@ 2024-01-03 10:20 ` Ahmad Fatoum
2024-01-04 11:17 ` [PATCH 1/2] cdev: simplify loop in cdev_by_device_node Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-01-03 10:20 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We have different helpers for creating cdevs, depending on whether they
are loop devices, partitions or links.
Create a common cdev_alloc function, so it's easier to instrument cdev
creation during debugging.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
fs/devfs-core.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index c79b092a112e..9f5b41761f52 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -324,6 +324,16 @@ int cdev_truncate(struct cdev *cdev, size_t size)
return -EPERM;
}
+static struct cdev *cdev_alloc(const char *name)
+{
+ struct cdev *new;
+
+ new = xzalloc(sizeof(*new));
+ new->name = xstrdup(name);
+
+ return new;
+}
+
int devfs_create(struct cdev *new)
{
struct cdev *cdev;
@@ -358,8 +368,7 @@ int devfs_create_link(struct cdev *cdev, const char *name)
*/
cdev = cdev_readlink(cdev);
- new = xzalloc(sizeof(*new));
- new->name = xstrdup(name);
+ new = cdev_alloc(name);
new->link = cdev;
if (cdev->partname) {
@@ -540,8 +549,7 @@ struct cdev *cdevfs_add_partition(struct cdev *cdev,
return &mtd->cdev;
}
- new = xzalloc(sizeof(*new));
- new->name = strdup(partinfo->name);
+ new = cdev_alloc(partinfo->name);
if (!strncmp(cdev->name, partinfo->name, strlen(cdev->name)))
new->partname = xstrdup(partinfo->name + strlen(cdev->name) + 1);
@@ -679,6 +687,7 @@ static const struct cdev_operations loop_ops = {
struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset)
{
+ char str[16];
struct cdev *new;
struct loop_priv *priv;
static int loopno;
@@ -692,10 +701,10 @@ struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset)
return NULL;
}
- new = xzalloc(sizeof(*new));
+ snprintf(str, sizeof(str), "loop%u", loopno++);
+ new = cdev_alloc(str);
new->ops = &loop_ops;
- new->name = basprintf("loop%u", loopno++);
new->priv = priv;
ofs = lseek(priv->fd, 0, SEEK_END);
--
2.39.2
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] cdev: simplify loop in cdev_by_device_node
2024-01-03 10:20 [PATCH 1/2] cdev: simplify loop in cdev_by_device_node Ahmad Fatoum
2024-01-03 10:20 ` [PATCH 2/2] cdev: add cdev_alloc helper for creating cdevs Ahmad Fatoum
@ 2024-01-04 11:17 ` Sascha Hauer
1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-01-04 11:17 UTC (permalink / raw)
To: Ahmad Fatoum; +Cc: barebox
On Wed, Jan 03, 2024 at 11:20:34AM +0100, Ahmad Fatoum wrote:
> We don't need to compare the cdev's device node pointer both against NULL
> and against the searched for device node on each iteration.
>
> Instead, it's sufficient to just compare the device nodes directly.
> To maintain previous behavior, when searching for a NULL device node, we
> shouldn't return the first cdev, but return NULL. Do that via an early
> exit instead of doing it after iterating over all cdevs.
>
> No functional change.
>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> fs/devfs-core.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
Applied, thanks
Sascha
>
> diff --git a/fs/devfs-core.c b/fs/devfs-core.c
> index 244f76f62c52..c79b092a112e 100644
> --- a/fs/devfs-core.c
> +++ b/fs/devfs-core.c
> @@ -87,9 +87,10 @@ struct cdev *cdev_by_device_node(struct device_node *node)
> {
> struct cdev *cdev;
>
> + if (!node)
> + return NULL;
> +
> for_each_cdev(cdev) {
> - if (!cdev->device_node)
> - continue;
> if (cdev->device_node == node)
> return cdev_readlink(cdev);
> }
> --
> 2.39.2
>
>
>
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-01-04 11:18 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-03 10:20 [PATCH 1/2] cdev: simplify loop in cdev_by_device_node Ahmad Fatoum
2024-01-03 10:20 ` [PATCH 2/2] cdev: add cdev_alloc helper for creating cdevs Ahmad Fatoum
2024-01-04 11:17 ` [PATCH 1/2] cdev: simplify loop in cdev_by_device_node Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox