Re: mm/page_alloc.c:984:9: warning: iteration 119304648 invokes undefined behavior

From: Matthew Wilcox
Date: Thu May 19 2022 - 17:28:36 EST


On Fri, May 20, 2022 at 05:14:52AM +0800, kernel test robot wrote:
> All warnings (new ones prefixed by >>):
>
> mm/page_alloc.c: In function 'rmqueue.isra':
> >> mm/page_alloc.c:984:9: warning: iteration 119304648 invokes undefined behavior [-Waggressive-loop-optimizations]
> 984 | list_add(&page->lru, &area->free_list[migratetype]);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> mm/page_alloc.c:2305:21: note: within this loop
> 2305 | while (high > low) {
> | ~~~~~^~~~~

That's OK. 'high' and 'low' are page orders. They both lie in the
range [0,63], so this will not iterate 119304648 times (what a funny
number; I wonder why GCC chose it).

Can we make GCC happier by telling it that low and high are unsigned?
It should have figured out that 'high' is <= 63 already, as that's a
consequence of:

unsigned long size = 1 << high;