mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christian Marangi <ansuelsmth@gmail.com>
To: Christian Marangi <ansuelsmth@gmail.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org
Cc: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
Subject: [PATCH] resource: add WARN_ON_ONCE for resource_size() and document misusage
Date: Mon,  8 Dec 2025 21:29:19 +0100	[thread overview]
Message-ID: <20251208202921.16819-1-ansuelsmth@gmail.com> (raw)

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@linux.intel.com>
Signed-off-by: Christian Marangi <ansuelsmth@gmail.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


             reply	other threads:[~2025-12-08 20:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-08 20:29 Christian Marangi [this message]
2025-12-09  9:54 ` kernel test robot
2025-12-09 10:25 ` Ilpo Järvinen
2025-12-09 15:07   ` Andy Shevchenko
2025-12-09 15:09     ` Christian Marangi
2025-12-09 15:40       ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251208202921.16819-1-ansuelsmth@gmail.com \
    --to=ansuelsmth@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®