[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251208202921.16819-1-ansuelsmth@gmail.com>
Date: Mon, 8 Dec 2025 21:29:19 +0100
From: Christian Marangi <ansuelsmth@...il.com>
To: Christian Marangi <ansuelsmth@...il.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Andrew Morton <akpm@...ux-foundation.org>,
linux-kernel@...r.kernel.org
Cc: Ilpo Järvinen <ilpo.jarvinen@...ux.intel.com>
Subject: [PATCH] resource: add WARN_ON_ONCE for resource_size() and document misusage
To catch any possible misusage of resource_size() helper, emit a WARN if
we detect the passed resource descriptor have zeroed flags. This would
signal the resource descriptor is not correctly inizialized and will
probably result in resource_size() returning unexpected values. (for
example returning 1 if the resource descriptor is all set to zero)
Historically, it was assumed that resource_size was ALWAYS used AFTER
correct API fill the data of the resource descriptor (or errors out)
But lack of comments might have introduced some logic error and
confusion in any user of resource_size() with it used on resource
description initialized to all zero.
The normal way to inizialize an "uninizialized" resource descriptor
would be to use DEFINE_RES macro or resource_set_range() ideally with a
proper flag set to it.
Hence initializing a resource descriptor to all zero and passing it to
resource_size() would actually produce a size of 1.
Correct comments on the usage of this helper in conjunction of WARN
should prevent from now on any possible misusage of this and permit
to catch and fix any possible BUG caused by this logic confusion.
Link: https://lore.kernel.org/all/20251207215359.28895-1-ansuelsmth@gmail.com/T/#m990492684913c5a158ff0e5fc90697d8ad95351b
Suggested-by: Ilpo Järvinen <ilpo.jarvinen@...ux.intel.com>
Signed-off-by: Christian Marangi <ansuelsmth@...il.com>
---
include/linux/ioport.h | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index e8b2d6aa4013..8b60f820993c 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -286,8 +286,20 @@ static inline void resource_set_range(struct resource *res,
resource_set_size(res, size);
}
+/**
+ * resource_size - Get the size of the resource
+ * @res: Resource descriptor
+ *
+ * This MUST be used ONLY with correctly inizialized resource descriptor.
+ * Passing a resource descriptor with zeroed flags will produce a WARN
+ * signaling a misusage of this helper and probably a BUG in the user
+ * of this helper.
+ *
+ * Return: Size of the resource calculated from resource end - start + 1.
+ */
static inline resource_size_t resource_size(const struct resource *res)
{
+ WARN_ON_ONCE(!res->flags);
return res->end - res->start + 1;
}
static inline unsigned long resource_type(const struct resource *res)
--
2.51.0
Powered by blists - more mailing lists