Skip to contentSkip to conversation

mm: vmstat.h: Annotate operations between enums

PATCH In review Ricardo Ribalda 3 participants 4 messages Dec 2, 2024 – Dec 15, 2024 MM core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: vmstat.h: Annotate operations between enums

Compiler is confused when we do arithmetic operations between two different enum types.
In this case NR_VM_STAT_ITEMS and NR_LRU_BASE are not actual enumerators, they are used to find the first and the count of the enumerator.

Add a casting to int, to avoid the following llvm 9 warning:
./include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

  504 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  505 |                            item];
      |                            ~~~~
./include/linux/vmstat.h:511:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
  511 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  512 |                            NR_VM_NUMA_EVENT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~~
./include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
  518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
      |                               ~~~~~~~~~~~ ^ ~~~
./include/linux/vmstat.h:524:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
  524 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  525 |                            NR_VM_NUMA_EVENT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~~
---
 include/linux/vmstat.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
include/linux/vmstat.h +4 -5
index d2761bf8ff32..32c641d25bea 100644
@@ -501,27 +501,26 @@ static inline const char *zone_stat_name(enum zone_stat_item item)
 #ifdef CONFIG_NUMA
 static inline const char *numa_stat_name(enum numa_stat_item item)
 {
-	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
-			   item];
+	return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS + item];
 }
 #endif /* CONFIG_NUMA */
 
 static inline const char *node_stat_name(enum node_stat_item item)
 {
-	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
+	return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS +
 			   NR_VM_NUMA_EVENT_ITEMS +
 			   item];
 }
 
 static inline const char *lru_list_name(enum lru_list lru)
 {
-	return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
+	return node_stat_name((int)NR_LRU_BASE + lru) + 3; // skip "nr_"
 }
 
 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
 static inline const char *vm_event_name(enum vm_event_item item)
 {
-	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
+	return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS +
 			   NR_VM_NUMA_EVENT_ITEMS +
 			   NR_VM_NODE_STAT_ITEMS +
 			   NR_VM_STAT_ITEMS +

---
base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37
change-id: 20241202-fix-vmstat-88968bcaa955

Best regards,

Signature · 1 line
Ricardo Ribalda <[email protected]>
#1 lore raw

Hi Ricardo,

Quoted 26 lines · Ricardo Ribalda

Compiler is confused when we do arithmetic operations between two different enum types.
In this case NR_VM_STAT_ITEMS and NR_LRU_BASE are not actual enumerators, they are used to find the first and the count of the enumerator.

Add a casting to int, to avoid the following llvm 9 warning:
./include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

  504 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  505 |                            item];
      |                            ~~~~
./include/linux/vmstat.h:511:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
  511 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  512 |                            NR_VM_NUMA_EVENT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~~
./include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
  518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
      |                               ~~~~~~~~~~~ ^ ~~~
./include/linux/vmstat.h:524:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
  524 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  525 |                            NR_VM_NUMA_EVENT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~~
Quoted 1 line

I think it is just better to disable this specific warning for normal and W=1 builds so that it is not visible for people, as this objectively makes the code uglier:

https://lore.kernel.org/20241008005136.GA241099@thelio-3990X/

https://lore.kernel.org/20241017-disable-two-clang-enum-warnings-v2-1-163ac04346ae@kernel.org/

I have just pinged that patch for acceptance.

Cheers,
Nathan

Quoted 49 lines
---
 include/linux/vmstat.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
include/linux/vmstat.h +4 -6
index d2761bf8ff32..32c641d25bea 100644
@@ -501,27 +501,26 @@ static inline const char *zone_stat_name(enum zone_stat_item item)
 #ifdef CONFIG_NUMA
 static inline const char *numa_stat_name(enum numa_stat_item item)
 {
-	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
-			   item];
+	return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS + item];
 }
 #endif /* CONFIG_NUMA */
 
 static inline const char *node_stat_name(enum node_stat_item item)
 {
-	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
+	return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS +
 			   NR_VM_NUMA_EVENT_ITEMS +
 			   item];
 }
 
 static inline const char *lru_list_name(enum lru_list lru)
 {
-	return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
+	return node_stat_name((int)NR_LRU_BASE + lru) + 3; // skip "nr_"
 }
 
 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
 static inline const char *vm_event_name(enum vm_event_item item)
 {
-	return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
+	return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS +
 			   NR_VM_NUMA_EVENT_ITEMS +
 			   NR_VM_NODE_STAT_ITEMS +
 			   NR_VM_STAT_ITEMS +
 
---

base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37
change-id: 20241202-fix-vmstat-88968bcaa955

Best regards,

Signature · 1 line
Ricardo Ribalda <[email protected]>
#2 lore raw
Quoted 32 lines · Nathan Chancellor

Hi Ricardo,

Quoted 26 lines · Ricardo Ribalda

Compiler is confused when we do arithmetic operations between two different enum types.
In this case NR_VM_STAT_ITEMS and NR_LRU_BASE are not actual enumerators, they are used to find the first and the count of the enumerator.

Add a casting to int, to avoid the following llvm 9 warning:
./include/linux/vmstat.h:504:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]

  504 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  505 |                            item];
      |                            ~~~~
./include/linux/vmstat.h:511:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
  511 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  512 |                            NR_VM_NUMA_EVENT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~~
./include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
  518 |         return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
      |                               ~~~~~~~~~~~ ^ ~~~
./include/linux/vmstat.h:524:43: warning: arithmetic between different enumeration types ('enum zone_stat_item' and 'enum numa_stat_item') [-Wenum-enum-conversion]
  524 |         return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~ ^
  525 |                            NR_VM_NUMA_EVENT_ITEMS +
      |                            ~~~~~~~~~~~~~~~~~~~~~~
Quoted 1 line
Quoted 7 lines

I think it is just better to disable this specific warning for normal and W=1 builds so that it is not visible for people, as this objectively makes the code uglier:

https://lore.kernel.org/20241008005136.GA241099@thelio-3990X/

https://lore.kernel.org/20241017-disable-two-clang-enum-warnings-v2-1-163ac04346ae@kernel.org/

Thanks for the heads-up.

Either disabling the config or this patch will work for my usecase (media-ci).

FWIW, we only need 3 patches (plus this) to build media with W=1 and llvm19....

I do not think that this patch is very ugly :P

Quoted 55 lines

I have just pinged that patch for acceptance.

Cheers,
Nathan

Quoted 49 lines
---
 include/linux/vmstat.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
include/linux/vmstat.h +4 -6
index d2761bf8ff32..32c641d25bea 100644
@@ -501,27 +501,26 @@ static inline const char *zone_stat_name(enum zone_stat_item item)
 #ifdef CONFIG_NUMA
 static inline const char *numa_stat_name(enum numa_stat_item item)
 {
-     return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
-                        item];
+     return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS + item];
 }
 #endif /* CONFIG_NUMA */
 
 static inline const char *node_stat_name(enum node_stat_item item)
 {
-     return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
+     return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS +
                         NR_VM_NUMA_EVENT_ITEMS +
                         item];
 }
 
 static inline const char *lru_list_name(enum lru_list lru)
 {
-     return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
+     return node_stat_name((int)NR_LRU_BASE + lru) + 3; // skip "nr_"
 }
 
 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
 static inline const char *vm_event_name(enum vm_event_item item)
 {
-     return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
+     return vmstat_text[(int)NR_VM_ZONE_STAT_ITEMS +
                         NR_VM_NUMA_EVENT_ITEMS +
                         NR_VM_NODE_STAT_ITEMS +
                         NR_VM_STAT_ITEMS +
 
---

base-commit: 40384c840ea1944d7c5a392e8975ed088ecf0b37
change-id: 20241202-fix-vmstat-88968bcaa955

Best regards,
--
Ricardo Ribalda <[email protected]>

Signature · 1 line
Ricardo Ribalda
#3 lore raw
In reply to Ricardo Ribalda
kernel test robot noticed the following build errors:

Hi Ricardo,

kernel test robot noticed the following build errors:

[auto build test ERROR on 40384c840ea1944d7c5a392e8975ed088ecf0b37]

url:    https://github.com/intel-lab-lkp/linux/commits/Ricardo-Ribalda/mm-vmstat-h-Annotate-operations-between-enums/20241203-020840
base:   40384c840ea1944d7c5a392e8975ed088ecf0b37
patch link:    https://lore.kernel.org/r/20241202-fix-vmstat-v1-1-c995585b7df5%40chromium.org
patch subject: [PATCH] mm: vmstat.h: Annotate operations between enums
config: x86_64-randconfig-004-20241216 (https://download.01.org/0day-ci/archive/20241216/[email protected]/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241216/[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 >>):

>> drivers/gpu/drm/amd/amdgpu/../display/dc/dml2/dml21/src/dml2_core/dml2_core_dcn4_calcs.c:7021:13: error: stack frame size (3096) exceeds limit (3072) in 'dml_core_mode_support' [-Werror,-Wframe-larger-than]

7021 | static bool dml_core_mode_support(struct dml2_core_calcs_mode_support_ex *in_out_params)
| ^
1 error generated.
--

>> drivers/gpu/drm/amd/amdgpu/../display/dc/dml2/display_mode_core.c:6712:12: error: stack frame size (4088) exceeds limit (3072) in 'dml_core_mode_support' [-Werror,-Wframe-larger-than]

6712 | dml_bool_t dml_core_mode_support(struct display_mode_lib_st *mode_lib)
| ^
1 error generated.

vim +/dml_core_mode_support +7021 drivers/gpu/drm/amd/amdgpu/../display/dc/dml2/dml21/src/dml2_core/dml2_core_dcn4_calcs.c

2563391e57b5a9 Chaitanya Dhere 2024-07-02 7020

70839da6360500 Aurabindo Pillai 2024-04-19 @7021  static bool dml_core_mode_support(struct dml2_core_calcs_mode_support_ex *in_out_params)
70839da6360500 Aurabindo Pillai 2024-04-19  7022  {
70839da6360500 Aurabindo Pillai 2024-04-19  7023  	struct dml2_core_internal_display_mode_lib *mode_lib = in_out_params->mode_lib;
70839da6360500 Aurabindo Pillai 2024-04-19  7024  	const struct dml2_display_cfg *display_cfg = in_out_params->in_display_cfg;
70839da6360500 Aurabindo Pillai 2024-04-19  7025  	const struct dml2_mcg_min_clock_table *min_clk_table = in_out_params->min_clk_table;

70839da6360500 Aurabindo Pillai 2024-04-19 7026

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki