Skip to contentSkip to conversation

mm: Replace (20 - PAGE_SHIFT) with common macros for pages<->MB conversion

PATCH In review Ye Liu 9 participants 15 messages Jul 18, 2025 – Jul 20, 2025

Subsystems (7)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH 1/1 mm: Replace (20 - PAGE_SHIFT) with common macros for pages<->MB conversion

From: Ye Liu <[email protected]>

Replace repeated (20 - PAGE_SHIFT) calculations with standard macros:
- MB_TO_PAGES(mb) converts MB to page count
- PAGES_TO_MB(pages) converts pages to MB

No functional change.

---
 include/linux/mm.h    | 9 +++++++++
 kernel/rcu/rcuscale.c | 2 +-
 kernel/sched/fair.c   | 5 ++---
 mm/backing-dev.c      | 2 +-
 mm/huge_memory.c      | 2 +-
 mm/swap.c             | 2 +-
 6 files changed, 15 insertions(+), 7 deletions(-)
include/linux/mm.h +9 -0
index 957acde6ae62..0c1b2c074142 100644
@@ -69,6 +69,15 @@ static inline void totalram_pages_add(long count)
 
 extern void * high_memory;
 
+/*
+ * Convert between pages and MB
+ * 20 is the shift for 1MB (2^20 = 1MB)
+ * PAGE_SHIFT is the shift for page size (e.g., 12 for 4KB pages)
+ * So (20 - PAGE_SHIFT) converts between pages and MB
+ */
+#define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
+#define MB_TO_PAGES(mb)    ((mb) << (20 - PAGE_SHIFT))
+
 #ifdef CONFIG_SYSCTL
 extern int sysctl_legacy_va_layout;
 #else
kernel/rcu/rcuscale.c +1 -1
index b521d0455992..7484d8ad5767 100644
@@ -796,7 +796,7 @@ kfree_scale_thread(void *arg)
 		pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
 		       (unsigned long long)(end_time - start_time), kfree_loops,
 		       rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
-		       (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
+		       PAGES_TO_MB(mem_begin - mem_during));
 
 		if (shutdown) {
 			smp_mb(); /* Assign before wake. */
kernel/sched/fair.c +2 -3
index b9b4bbbf0af6..ae1d9a7ef202 100644
@@ -1489,7 +1489,7 @@ static unsigned int task_nr_scan_windows(struct task_struct *p)
 	 * by the PTE scanner and NUMA hinting faults should be trapped based
 	 * on resident pages
 	 */
-	nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
+	nr_scan_pages = MB_TO_PAGES(sysctl_numa_balancing_scan_size);
 	rss = get_mm_rss(p->mm);
 	if (!rss)
 		rss = nr_scan_pages;
@@ -1926,8 +1926,7 @@ bool should_numa_migrate_memory(struct task_struct *p, struct folio *folio,
 		}
 
 		def_th = sysctl_numa_balancing_hot_threshold;
-		rate_limit = sysctl_numa_balancing_promote_rate_limit << \
-			(20 - PAGE_SHIFT);
+		rate_limit = MB_TO_PAGES(sysctl_numa_balancing_promote_rate_limit);
 		numa_promotion_adjust_threshold(pgdat, rate_limit, def_th);
 
 		th = pgdat->nbp_threshold ? : def_th;
mm/backing-dev.c +1 -1
index 783904d8c5ef..e4d578e6121c 100644
@@ -510,7 +510,7 @@ static void wb_update_bandwidth_workfn(struct work_struct *work)
 /*
  * Initial write bandwidth: 100 MB/s
  */
-#define INIT_BW		(100 << (20 - PAGE_SHIFT))
+#define INIT_BW		MB_TO_PAGES(100)
 
 static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 		   gfp_t gfp)
mm/huge_memory.c +1 -1
index 389620c65a5f..dcc33d9c300f 100644
@@ -911,7 +911,7 @@ static int __init hugepage_init(void)
 	 * where the extra memory used could hurt more than TLB overhead
 	 * is likely to save.  The admin can still enable it through /sys.
 	 */
-	if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
+	if (totalram_pages() < MB_TO_PAGES(512)) {
 		transparent_hugepage_flags = 0;
 		return 0;
 	}
mm/swap.c +1 -1
index 3632dd061beb..cb164f9ef9e3 100644
@@ -1096,7 +1096,7 @@ static const struct ctl_table swap_sysctl_table[] = {
  */
 void __init swap_setup(void)
 {
-	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
+	unsigned long megs = PAGES_TO_MB(totalram_pages());
 
 	/* Use a smaller cluster for small-memory machines */
 	if (megs < 16)
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 26 lines · On Fri, 18 Jul 2025 10:41:32 +0800 Ye Liu <[email protected]> wrote:

From: Ye Liu <[email protected]>

Replace repeated (20 - PAGE_SHIFT) calculations with standard macros:
- MB_TO_PAGES(mb) converts MB to page count
- PAGES_TO_MB(pages) converts pages to MB

No functional change.

...

include/linux/mm.h +9 -0
+/*
+ * Convert between pages and MB
+ * 20 is the shift for 1MB (2^20 = 1MB)
+ * PAGE_SHIFT is the shift for page size (e.g., 12 for 4KB pages)
+ * So (20 - PAGE_SHIFT) converts between pages and MB
+ */
+#define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
+#define MB_TO_PAGES(mb)    ((mb) << (20 - PAGE_SHIFT))
+
 #ifdef CONFIG_SYSCTL
 extern int sysctl_legacy_va_layout;
 #else
 
...

include/linux/mm.h kfree_scale_thread(void *arg) · line 802

Quoted 8 lines
include/linux/mm.h +1 -1
@@ -796,7 +796,7 @@ kfree_scale_thread(void *arg)
 		pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
 		       (unsigned long long)(end_time - start_time), kfree_loops,
 		       rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
-		       (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
+		       PAGES_TO_MB(mem_begin - mem_during));
 
 		if (shutdown) {
 			smp_mb(); /* Assign before wake. */

But, but, but, obscure hard-coded magic numbers are there for our job security!

Oh well, we got caught. Applied, thanks.

reviewer
#4 lore raw
In reply to Ye Liu
Quoted 11 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace repeated (20 - PAGE_SHIFT) calculations with standard macros:
- MB_TO_PAGES(mb) converts MB to page count
- PAGES_TO_MB(pages) converts pages to MB

No functional change.

---

sh and x86 have their own pages_to_mb, drivers/target/target_core_user.c too. I guess no one likes to clean the kernel :)

#5 lore raw
Quoted 8 lines · Dev Jain
Quoted 6 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace repeated (20 - PAGE_SHIFT) calculations with standard macros:
- MB_TO_PAGES(mb) converts MB to page count
- PAGES_TO_MB(pages) converts pages to MB

Quoted 8 lines
Quoted 4 lines

No functional change.

---

sh and x86 have their own pages_to_mb, drivers/target/target_core_user.c too. I guess no one likes to clean the kernel :)

*A wild Lorenzo appaers*

Reviewed-by on v1 1/1
#6 lore raw
In reply to Ye Liu
Quoted 9 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace repeated (20 - PAGE_SHIFT) calculations with standard macros:
- MB_TO_PAGES(mb) converts MB to page count
- PAGES_TO_MB(pages) converts pages to MB

No functional change.

Nice idea :)

NOte I see arch/x86/include/asm/pgtable.h has a pages_to_mb() static inline declaration, but probably being an asm include can't ref mm.h so meh not a big deal.

LGTM, so:

Quoted 107 lines
---
 include/linux/mm.h    | 9 +++++++++
 kernel/rcu/rcuscale.c | 2 +-
 kernel/sched/fair.c   | 5 ++---
 mm/backing-dev.c      | 2 +-
 mm/huge_memory.c      | 2 +-
 mm/swap.c             | 2 +-
 6 files changed, 15 insertions(+), 7 deletions(-)
include/linux/mm.h +9 -0
index 957acde6ae62..0c1b2c074142 100644
@@ -69,6 +69,15 @@ static inline void totalram_pages_add(long count)
 
 extern void * high_memory;
 
+/*
+ * Convert between pages and MB
+ * 20 is the shift for 1MB (2^20 = 1MB)
+ * PAGE_SHIFT is the shift for page size (e.g., 12 for 4KB pages)
+ * So (20 - PAGE_SHIFT) converts between pages and MB
+ */
+#define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
+#define MB_TO_PAGES(mb)    ((mb) << (20 - PAGE_SHIFT))
+
 #ifdef CONFIG_SYSCTL
 extern int sysctl_legacy_va_layout;
 #else
kernel/rcu/rcuscale.c +1 -1
index b521d0455992..7484d8ad5767 100644
@@ -796,7 +796,7 @@ kfree_scale_thread(void *arg)
 		pr_alert("Total time taken by all kfree'ers: %llu ns, loops: %d, batches: %ld, memory footprint: %lldMB\n",
 		       (unsigned long long)(end_time - start_time), kfree_loops,
 		       rcuscale_seq_diff(b_rcu_gp_test_finished, b_rcu_gp_test_started),
-		       (mem_begin - mem_during) >> (20 - PAGE_SHIFT));
+		       PAGES_TO_MB(mem_begin - mem_during));
 
 		if (shutdown) {
 			smp_mb(); /* Assign before wake. */
kernel/sched/fair.c +2 -3
index b9b4bbbf0af6..ae1d9a7ef202 100644
@@ -1489,7 +1489,7 @@ static unsigned int task_nr_scan_windows(struct task_struct *p)
 	 * by the PTE scanner and NUMA hinting faults should be trapped based
 	 * on resident pages
 	 */
-	nr_scan_pages = sysctl_numa_balancing_scan_size << (20 - PAGE_SHIFT);
+	nr_scan_pages = MB_TO_PAGES(sysctl_numa_balancing_scan_size);
 	rss = get_mm_rss(p->mm);
 	if (!rss)
 		rss = nr_scan_pages;
@@ -1926,8 +1926,7 @@ bool should_numa_migrate_memory(struct task_struct *p, struct folio *folio,
 		}
 
 		def_th = sysctl_numa_balancing_hot_threshold;
-		rate_limit = sysctl_numa_balancing_promote_rate_limit << \
-			(20 - PAGE_SHIFT);
+		rate_limit = MB_TO_PAGES(sysctl_numa_balancing_promote_rate_limit);
 		numa_promotion_adjust_threshold(pgdat, rate_limit, def_th);
 
 		th = pgdat->nbp_threshold ? : def_th;
mm/backing-dev.c +1 -1
index 783904d8c5ef..e4d578e6121c 100644
@@ -510,7 +510,7 @@ static void wb_update_bandwidth_workfn(struct work_struct *work)
 /*
  * Initial write bandwidth: 100 MB/s
  */
-#define INIT_BW		(100 << (20 - PAGE_SHIFT))
+#define INIT_BW		MB_TO_PAGES(100)
 
 static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 		   gfp_t gfp)
mm/huge_memory.c +1 -1
index 389620c65a5f..dcc33d9c300f 100644
@@ -911,7 +911,7 @@ static int __init hugepage_init(void)
 	 * where the extra memory used could hurt more than TLB overhead
 	 * is likely to save.  The admin can still enable it through /sys.
 	 */
-	if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
+	if (totalram_pages() < MB_TO_PAGES(512)) {
 		transparent_hugepage_flags = 0;
 		return 0;
 	}
mm/swap.c +1 -2
index 3632dd061beb..cb164f9ef9e3 100644
@@ -1096,7 +1096,7 @@ static const struct ctl_table swap_sysctl_table[] = {
  */
 void __init swap_setup(void)
 {
-	unsigned long megs = totalram_pages() >> (20 - PAGE_SHIFT);
+	unsigned long megs = PAGES_TO_MB(totalram_pages());
 
 	/* Use a smaller cluster for small-memory machines */
 	if (megs < 16)
--

2.43.0

#7 lore raw
Quoted 3 lines · Lorenzo Stoakes

NOte I see arch/x86/include/asm/pgtable.h has a pages_to_mb() static inline declaration, but probably being an asm include can't ref mm.h so meh not a big deal.

Should probably go to linux/sizes.h, except that it uses PAGE_SIZE which isn't available there. But asm-generic/getorder.h might be a good place for it.

(hm, is including getorder.h safe by itself? looks like it relies on something else to bring in the definition of PAGE_SHIFT)

reviewer Acked-by on v1 1/1
#8 lore raw
In reply to Ye Liu
Quoted 9 lines · Ye Liu

From: Ye Liu <[email protected]>

Replace repeated (20 - PAGE_SHIFT) calculations with standard macros:
- MB_TO_PAGES(mb) converts MB to page count
- PAGES_TO_MB(pages) converts pages to MB

No functional change.

Quoted 9 lines
---
 include/linux/mm.h    | 9 +++++++++
 kernel/rcu/rcuscale.c | 2 +-
 kernel/sched/fair.c   | 5 ++---
 mm/backing-dev.c      | 2 +-
 mm/huge_memory.c      | 2 +-
 mm/swap.c             | 2 +-
 6 files changed, 15 insertions(+), 7 deletions(-)

Thanks.

--
Best Regards,
Yan, Zi

#10 lore raw
In reply to Ye Liu
kernel test robot noticed the following build warnings:

Hi Ye,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Ye-Liu/mm-Replace-20-PAGE_SHIFT-with-common-macros-for-pages-MB-conversion/20250718-104347
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250718024134.1304745-1-ye.liu%40linux.dev
patch subject: [PATCH] mm: Replace (20 - PAGE_SHIFT) with common macros for pages<->MB conversion
config: arc-randconfig-001-20250719 (https://download.01.org/0day-ci/archive/20250719/[email protected]/config)
compiler: arc-linux-gcc (GCC) 14.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250719/[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 warnings (new ones prefixed by >>):

In file included from arch/arc/kernel/asm-offsets.c:7:

>> include/linux/mm.h:78:9: warning: "PAGES_TO_MB" redefined
      78 | #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
         |         ^~~~~~~~~~~
   In file included from arch/arc/include/asm/irqflags-arcv2.h:9,
                    from arch/arc/include/asm/irqflags.h:13,
                    from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/sched.h:2205,
                    from arch/arc/kernel/asm-offsets.c:6:
   arch/arc/include/asm/arcregs.h:155:9: note: this is the location of the previous definition
     155 | #define PAGES_TO_MB(n_pages)    (PAGES_TO_KB(n_pages) >> 10)
         |         ^~~~~~~~~~~
--
   In file included from arch/arc/include/asm/cacheflush.h:18,
                    from include/linux/cacheflush.h:5,
                    from include/linux/highmem.h:8,
                    from include/linux/bvec.h:10,
                    from include/linux/blk_types.h:10,
                    from include/linux/blkdev.h:9,
                    from fs/ufs/super.c:83:
>> include/linux/mm.h:78:9: warning: "PAGES_TO_MB" redefined
      78 | #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
         |         ^~~~~~~~~~~
   In file included from arch/arc/include/asm/irqflags-arcv2.h:9,
                    from arch/arc/include/asm/irqflags.h:13,
                    from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/mmzone.h:8,
                    from include/linux/gfp.h:7,
                    from include/linux/umh.h:4,
                    from include/linux/kmod.h:9,
                    from include/linux/module.h:17,
                    from fs/ufs/super.c:70:
   arch/arc/include/asm/arcregs.h:155:9: note: this is the location of the previous definition
     155 | #define PAGES_TO_MB(n_pages)    (PAGES_TO_KB(n_pages) >> 10)
         |         ^~~~~~~~~~~
   fs/ufs/super.c: In function 'ufs_reconfigure':
   fs/ufs/super.c:1246:22: warning: variable 'ufstype' set but not used [-Wunused-but-set-variable]
    1246 |         unsigned int ufstype;
         |                      ^~~~~~~
--
   In file included from arch/arc/kernel/asm-offsets.c:7:
>> include/linux/mm.h:78:9: warning: "PAGES_TO_MB" redefined
      78 | #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
         |         ^~~~~~~~~~~
   In file included from arch/arc/include/asm/irqflags-arcv2.h:9,
                    from arch/arc/include/asm/irqflags.h:13,
                    from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/sched.h:2205,
                    from arch/arc/kernel/asm-offsets.c:6:
   arch/arc/include/asm/arcregs.h:155:9: note: this is the location of the previous definition
     155 | #define PAGES_TO_MB(n_pages)    (PAGES_TO_KB(n_pages) >> 10)
         |         ^~~~~~~~~~~

vim +/PAGES_TO_MB +78 include/linux/mm.h

    71	
    72	/*
    73	 * Convert between pages and MB
    74	 * 20 is the shift for 1MB (2^20 = 1MB)
    75	 * PAGE_SHIFT is the shift for page size (e.g., 12 for 4KB pages)
    76	 * So (20 - PAGE_SHIFT) converts between pages and MB
    77	 */
  > 78	#define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
    79	#define MB_TO_PAGES(mb)    ((mb) << (20 - PAGE_SHIFT))
    80	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#11 lore raw

Ah yeah looks like this is already defined for arc only (!) in arch/arc/include/asm/arcregs.h.

Maybe pop in a #ifndef PAGES_TO_MB ? It seems to be equivalent.

Quoted 94 lines · kernel test robot

Hi Ye,

kernel test robot noticed the following build warnings:

[auto build test WARNING on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Ye-Liu/mm-Replace-20-PAGE_SHIFT-with-common-macros-for-pages-MB-conversion/20250718-104347
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20250718024134.1304745-1-ye.liu%40linux.dev
patch subject: [PATCH] mm: Replace (20 - PAGE_SHIFT) with common macros for pages<->MB conversion
config: arc-randconfig-001-20250719 (https://download.01.org/0day-ci/archive/20250719/[email protected]/config)
compiler: arc-linux-gcc (GCC) 14.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250719/[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 warnings (new ones prefixed by >>):

In file included from arch/arc/kernel/asm-offsets.c:7:

Quoted 1 line
Quoted 1 line

include/linux/mm.h:78:9: warning: "PAGES_TO_MB" redefined

      78 | #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
         |         ^~~~~~~~~~~
   In file included from arch/arc/include/asm/irqflags-arcv2.h:9,
                    from arch/arc/include/asm/irqflags.h:13,
                    from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/sched.h:2205,
                    from arch/arc/kernel/asm-offsets.c:6:
   arch/arc/include/asm/arcregs.h:155:9: note: this is the location of the previous definition
     155 | #define PAGES_TO_MB(n_pages)    (PAGES_TO_KB(n_pages) >> 10)
         |         ^~~~~~~~~~~
--
   In file included from arch/arc/include/asm/cacheflush.h:18,
                    from include/linux/cacheflush.h:5,
                    from include/linux/highmem.h:8,
                    from include/linux/bvec.h:10,
                    from include/linux/blk_types.h:10,
                    from include/linux/blkdev.h:9,
                    from fs/ufs/super.c:83:
Quoted 1 line
Quoted 1 line

include/linux/mm.h:78:9: warning: "PAGES_TO_MB" redefined

      78 | #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
         |         ^~~~~~~~~~~
   In file included from arch/arc/include/asm/irqflags-arcv2.h:9,
                    from arch/arc/include/asm/irqflags.h:13,
                    from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/mmzone.h:8,
                    from include/linux/gfp.h:7,
                    from include/linux/umh.h:4,
                    from include/linux/kmod.h:9,
                    from include/linux/module.h:17,
                    from fs/ufs/super.c:70:
   arch/arc/include/asm/arcregs.h:155:9: note: this is the location of the previous definition
     155 | #define PAGES_TO_MB(n_pages)    (PAGES_TO_KB(n_pages) >> 10)
         |         ^~~~~~~~~~~
   fs/ufs/super.c: In function 'ufs_reconfigure':
   fs/ufs/super.c:1246:22: warning: variable 'ufstype' set but not used [-Wunused-but-set-variable]
    1246 |         unsigned int ufstype;
         |                      ^~~~~~~

--
In file included from arch/arc/kernel/asm-offsets.c:7:

Quoted 1 line
Quoted 1 line

include/linux/mm.h:78:9: warning: "PAGES_TO_MB" redefined

      78 | #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))
         |         ^~~~~~~~~~~
   In file included from arch/arc/include/asm/irqflags-arcv2.h:9,
                    from arch/arc/include/asm/irqflags.h:13,
                    from include/linux/irqflags.h:18,
                    from include/linux/spinlock.h:59,
                    from include/linux/sched.h:2205,
                    from arch/arc/kernel/asm-offsets.c:6:
   arch/arc/include/asm/arcregs.h:155:9: note: this is the location of the previous definition
     155 | #define PAGES_TO_MB(n_pages)    (PAGES_TO_KB(n_pages) >> 10)
         |         ^~~~~~~~~~~

vim +/PAGES_TO_MB +78 include/linux/mm.h

    71
    72	/*
    73	 * Convert between pages and MB
    74	 * 20 is the shift for 1MB (2^20 = 1MB)
    75	 * PAGE_SHIFT is the shift for page size (e.g., 12 for 4KB pages)
    76	 * So (20 - PAGE_SHIFT) converts between pages and MB
    77	 */
Quoted 1 line

78 #define PAGES_TO_MB(pages) ((pages) >> (20 - PAGE_SHIFT))

    79	#define MB_TO_PAGES(mb)    ((mb) << (20 - PAGE_SHIFT))
    80

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

maintainer
#12 lore raw
Quoted 4 lines · On Sat, 19 Jul 2025 07:56:16 +0100 Lorenzo Stoakes…

Ah yeah looks like this is already defined for arc only (!) in arch/arc/include/asm/arcregs.h.

Maybe pop in a #ifndef PAGES_TO_MB ? It seems to be equivalent.

Well, those arc helpers

/* Helpers */
#define TO_KB(bytes) ((bytes) >> 10)
#define TO_MB(bytes) (TO_KB(bytes) >> 10)
#define PAGES_TO_KB(n_pages) ((n_pages) << (PAGE_SHIFT - 10))
#define PAGES_TO_MB(n_pages) (PAGES_TO_KB(n_pages) >> 10)

are simply in the wrong place.

I was thinking move all four into mm.h (for the lack of a better place). Then someone can make a career out of hunting down all the code sites which can use them.

eg,

hp2:/usr/src/linux-6.16-rc5> fgrep -r ">> 10" . | wc -l 886

#13 lore raw
Quoted 17 lines · Andrew Morton
Quoted 4 lines · On Sat, 19 Jul 2025 07:56:16 +0100 Lorenzo Stoakes…

Ah yeah looks like this is already defined for arc only (!) in arch/arc/include/asm/arcregs.h.

Maybe pop in a #ifndef PAGES_TO_MB ? It seems to be equivalent.

Well, those arc helpers

/* Helpers */
#define TO_KB(bytes) ((bytes) >> 10)
#define TO_MB(bytes) (TO_KB(bytes) >> 10)
#define PAGES_TO_KB(n_pages) ((n_pages) << (PAGE_SHIFT - 10))
#define PAGES_TO_MB(n_pages) (PAGES_TO_KB(n_pages) >> 10)

are simply in the wrong place.

Quoted 8 lines

I was thinking move all four into mm.h (for the lack of a better place). Then someone can make a career out of hunting down all the code sites which can use them.

eg,

hp2:/usr/src/linux-6.16-rc5> fgrep -r ">> 10" . | wc -l 886

Yeah I agree for sure, this is misplaced, but maybe one for a follow up patch?

There's more work to be done here, I already have an idea for a patch to fix another similar case...

maintainer
#14 lore raw
Quoted 14 lines · On Sun, 20 Jul 2025 09:59:55 +0100 Lorenzo Stoakes…
Quoted 14 lines

/* Helpers */
#define TO_KB(bytes) ((bytes) >> 10)
#define TO_MB(bytes) (TO_KB(bytes) >> 10)
#define PAGES_TO_KB(n_pages) ((n_pages) << (PAGE_SHIFT - 10))
#define PAGES_TO_MB(n_pages) (PAGES_TO_KB(n_pages) >> 10)

are simply in the wrong place.

I was thinking move all four into mm.h (for the lack of a better place). Then someone can make a career out of hunting down all the code sites which can use them.

eg,

Quoted 8 lines
Quoted 2 lines

hp2:/usr/src/linux-6.16-rc5> fgrep -r ">> 10" . | wc -l 886

Yeah I agree for sure, this is misplaced, but maybe one for a follow up patch?

There's more work to be done here, I already have an idea for a patch to fix another similar case...

OK.

I could put a couple of ifndefs in there to make this patch happy, but this patch is just a little cleanup and it's a very small part of any kernel-wide overhaul of these conversion operations.

So for now I think I'll retain this patch in mm-new to prevent this linux-next build error. I'll do this as a reminder until someone gets down and addresses all this in a broader fashion.