Skip to contentSkip to conversation

resource: add WARN_ON_ONCE for resource_size() and document misusage

PATCH Superseded Christian Marangi 4 participants 6 messages Dec 8, 2025 – Dec 9, 2025 Memory management

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 include/linux/ioport.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/ioport.h +12 -0
index e8b2d6aa4013..8b60f820993c 100644
@@ -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)
Signature · 1 line
2.51.0
#1 lore raw
kernel test robot noticed the following build errors:

Hi Christian,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on linus/master v6.18 next-20251209] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Christian-Marangi/resource-add-WARN_ON_ONCE-for-resource_size-and-document-misusage/20251209-043037
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20251208202921.16819-1-ansuelsmth%40gmail.com
patch subject: [PATCH] resource: add WARN_ON_ONCE for resource_size() and document misusage
config: openrisc-allnoconfig (https://download.01.org/0day-ci/archive/20251209/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251209/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from kernel/resource.c:15:
include/linux/ioport.h: In function 'resource_size':

>> include/linux/ioport.h:302:9: error: implicit declaration of function 'WARN_ON_ONCE' [-Wimplicit-function-declaration]
     302 |         WARN_ON_ONCE(!res->flags);
         |         ^~~~~~~~~~~~

vim +/WARN_ON_ONCE +302 include/linux/ioport.h

288
289 /**
290 * resource_size - Get the size of the resource
291 * @res: Resource descriptor
292 *
293 * This MUST be used ONLY with correctly inizialized resource descriptor.
294 * Passing a resource descriptor with zeroed flags will produce a WARN
295 * signaling a misusage of this helper and probably a BUG in the user
296 * of this helper.
297 *
298 * Return: Size of the resource calculated from resource end - start + 1.
299 */
300 static inline resource_size_t resource_size(const struct resource *res)
301 {

 > 302		WARN_ON_ONCE(!res->flags);

303 return res->end - res->start + 1;
304 }
305 static inline unsigned long resource_type(const struct resource *res)
306 {
307 return res->flags & IORESOURCE_TYPE_BITS;
308 }
309 static inline unsigned long resource_ext_type(const struct resource *res)
310 {
311 return res->flags & IORESOURCE_EXT_TYPE_BITS;
312 }
313 /* True iff r1 completely contains r2 */
314 static inline bool resource_contains(const struct resource *r1, const struct resource *r2)
315 {
316 if (resource_type(r1) != resource_type(r2))
317 return false;
318 if (r1->flags & IORESOURCE_UNSET || r2->flags & IORESOURCE_UNSET)
319 return false;
320 return r1->start <= r2->start && r1->end >= r2->end;
321 }
322

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#2 lore raw
In reply to Christian Marangi
Quoted 5 lines · Christian Marangi

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)

Move . after the () part. Or make the parenthesis part a real sentence.

In general, you should first explain the problem in the changelog and then the solution, not to go directly to the solution. Especially in the changes such as this that are non-trivial to understand and are meant to enforce good code hygiene/standard. So I think you should reorder some of the sentences to fix the ordering.

Quoted 1 line

Historically, it was assumed that resource_size was ALWAYS used AFTER

For function names, always use () in the changelog.

Quoted 1 line

correct API fill the data of the resource descriptor (or errors out)

Add .

Somehow I fail to connect that "(or errors out)" to rest of what is being said in the sentence, maybe rephrase it?

Quoted 2 lines

But lack of comments might have introduced some logic error and

some logic error -> logic errors

Quoted 2 lines

confusion in any user of resource_size() with it used on resource description initialized to all zero.

description -> descriptor

It's hard to understand what this tries to say, I suggest you try to rephrase it. Maybe something along these lines:

... and using resource_size() for a resource descriptor that is initialized to all zeros. Using resource_size() for a resource descriptor that has zero start and end yields to resource size of 1 (this is correct and necessary behavior!) which may beconfusing to some callers.

Quoted 1 line

The normal way to inizialize an "uninizialized" resource descriptor

2x iniz typos

Quoted 2 lines

would be to use DEFINE_RES macro or resource_set_range() ideally with a proper flag set to it.

proper flag -> proper type ?

I'd remove the resource_set_range() part actually since it's meant to be used for a resource that is already initialized and the code only wants to change the address range.

Quoted 2 lines

Hence initializing a resource descriptor to all zero and passing it to resource_size() would actually produce a size of 1.

This would naturally belong earlier when you talk about using resource_size() for a descriptor that is initialized to all zero. (I ended up writing this into the earlier suggestion how to rephrase things so if you use it, you can drop this as it then becomes duplicate).

Quoted 1 line

Correct comments on the usage of this helper in conjunction of WARN

Add kernel doc to resource_size() ...

Quoted 15 lines

should prevent from now on any possible misusage of this and permit to catch and fix any possible BUG caused by this logic confusion.

---
 include/linux/ioport.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
include/linux/ioport.h +0 -0
index e8b2d6aa4013..8b60f820993c 100644

include/linux/ioport.h static inline void resource_set_range(struct resource *res, · line 293

Quoted 8 lines
include/linux/ioport.h +5 -0
@@ -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.

initialized

I'd add:

Do not use resource_size() as a proxy for checking validity of @res or for checking if @res is in a resource tree (use flags checks or call resource_assigned() instead).

include/linux/ioport.h static inline void resource_set_range(struct resource *res, · line 294

Quoted 1 line
include/linux/ioport.h +1 -0
+ * Passing a resource descriptor with zeroed flags will produce a WARN

The caller must ensure @res is properly initialized.

include/linux/ioport.h static inline void resource_set_range(struct resource *res, · line 298

Quoted 4 lines
include/linux/ioport.h +4 -0
+ * 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.

I'd put this into the main description before the notes/warning on what to not do. And only say here:

* Return: size of the resource.

include/linux/ioport.h static inline void resource_set_range(struct resource *res, · line 302

Quoted 4 lines
include/linux/ioport.h +2 -0
+ */
 static inline resource_size_t resource_size(const struct resource *res)
 {
+	WARN_ON_ONCE(!res->flags);

As lkp also found out, you need to add #include <linux/bug.h>

Quoted 4 lines
include/linux/ioport.h +0 -0
 	return res->end - res->start + 1;
 }
 static inline unsigned long resource_type(const struct resource *res)
Signature · 1 line
 i.
#3 lore raw
Quoted 1 line · Ilpo Järvinen

On Mon, 8 Dec 2025, Christian Marangi wrote:

...

Quoted 5 lines

include/linux/ioport.h static inline void resource_set_range(struct resource *res, · line 302

Quoted 3 lines

static inline resource_size_t resource_size(const struct resource *res) {

include/linux/ioport.h +1 -0
+	WARN_ON_ONCE(!res->flags);

As lkp also found out, you need to add #include <linux/bug.h>

Since it's header, we may use better one from asm, i.e. asm/bug.h. This will give finer granularity and less possible tangled dependencies in the future.

Quoted 2 lines
Quoted 2 lines

return res->end - res->start + 1;
}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 1 line · Ilpo Järvinen

On Mon, 8 Dec 2025, Christian Marangi wrote:

...

Quoted 5 lines

include/linux/ioport.h static inline void resource_set_range(struct resource *res, · line 302

Quoted 3 lines

static inline resource_size_t resource_size(const struct resource *res) {

include/linux/ioport.h +1 -0
+	WARN_ON_ONCE(!res->flags);

As lkp also found out, you need to add #include <linux/bug.h>

Quoted 4 lines

Since it's header, we may use better one from asm, i.e. asm/bug.h. This will give finer granularity and less possible tangled dependencies in the future.

Will change in v3 I assume minor touch up to wording might be needed so it's not a problem.

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

return res->end - res->start + 1;
}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Signature · 1 line
	Ansuel
#5 lore raw
Quoted 3 lines · Christian Marangi
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Ilpo Järvinen

On Mon, 8 Dec 2025, Christian Marangi wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

include/linux/ioport.h static inline void resource_set_range(struct resource *res, · line 302

Quoted 3 lines

static inline resource_size_t resource_size(const struct resource *res) {

include/linux/ioport.h +1 -0
+	WARN_ON_ONCE(!res->flags);

As lkp also found out, you need to add #include <linux/bug.h>

Quoted 7 lines
Quoted 4 lines

Since it's header, we may use better one from asm, i.e. asm/bug.h. This will give finer granularity and less possible tangled dependencies in the future.

Will change in v3 I assume minor touch up to wording might be needed so it's not a problem.

Ah, I just commented on v2, yeah, please give at least 24h between versions of the patch.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

return res->end - res->start + 1;
}

Signature · 2 lines
With Best Regards,
Andy Shevchenko