2021-09-19 17:18:29

by Oleksandr Natalenko

[permalink] [raw]
Subject: [RFC v2 PATCH 0/3] mm: common PAGE_SIZE shift macros

There are various places where the K(x) macro (or its alternative and/or
counterpart) is defined. This submission gets rid of multiple definitions
and provides common ones.

Based on discussion with Miaohe Lin [1].

The patch is based on top of next-20210910 and is compile-tested using
allyesconfig.

RFC v1: [2].

Changes since RFC v1:

* change name from K(x) to PG2KB(x)
* also provide KB2PG(x)
* replace open-coded variants
* do not use separate header file for macro definitions

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Oleksandr Natalenko (3):
mm: add PG2KB/KB2PG helper macros
mm: replace custom PG2KB/KB2PG macros with common ones
mm: replace open-coded PG2KB/KB2PG variants with macros

arch/alpha/kernel/setup.c | 3 +-
arch/arc/include/asm/arcregs.h | 4 +-
arch/mips/mm/init.c | 2 +-
arch/powerpc/platforms/pseries/cmm.c | 17 ++--
arch/s390/appldata/appldata_mem.c | 19 ++--
arch/x86/kernel/cpu/mtrr/cleanup.c | 17 ++--
block/blk-sysfs.c | 9 +-
drivers/base/node.c | 69 ++++++++-------
drivers/gpu/drm/v3d/v3d_debugfs.c | 3 +-
drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c | 5 +-
drivers/md/md-bitmap.c | 4 +-
drivers/scsi/mpt3sas/mpt3sas_base.c | 5 +-
drivers/xen/xen-balloon.c | 11 ++-
fs/ceph/addr.c | 2 +-
fs/ceph/super.h | 3 +-
fs/fs-writeback.c | 2 +-
fs/nfs/write.c | 4 +-
fs/nfsd/nfscache.c | 3 +-
fs/proc/meminfo.c | 4 +-
fs/proc/task_mmu.c | 3 +-
include/linux/mm.h | 3 +
include/trace/events/writeback.h | 19 ++--
kernel/debug/kdb/kdb_main.c | 3 +-
kernel/events/core.c | 2 +-
kernel/fork.c | 2 +-
mm/backing-dev.c | 22 +++--
mm/hugetlb.c | 2 +-
mm/memcontrol.c | 18 ++--
mm/mmap.c | 6 +-
mm/nommu.c | 4 +-
mm/oom_kill.c | 16 ++--
mm/page-writeback.c | 4 +-
mm/page_alloc.c | 86 +++++++++----------
mm/shmem.c | 3 +-
mm/swap_state.c | 5 +-
mm/swapfile.c | 11 ++-
mm/util.c | 6 +-
37 files changed, 197 insertions(+), 204 deletions(-)

--
2.33.0


2021-09-19 17:18:28

by Oleksandr Natalenko

[permalink] [raw]
Subject: [RFC v2 PATCH 2/3] mm: replace custom PG2KB/KB2PG macros with common ones

Signed-off-by: Oleksandr Natalenko <[email protected]>
---
arch/arc/include/asm/arcregs.h | 4 +-
arch/powerpc/platforms/pseries/cmm.c | 17 +++---
arch/s390/appldata/appldata_mem.c | 19 +++----
drivers/base/node.c | 69 +++++++++++------------
drivers/scsi/mpt3sas/mpt3sas_base.c | 5 +-
drivers/xen/xen-balloon.c | 11 ++--
include/trace/events/writeback.h | 19 +++----
kernel/debug/kdb/kdb_main.c | 3 +-
mm/backing-dev.c | 20 +++----
mm/memcontrol.c | 18 +++---
mm/oom_kill.c | 16 +++---
mm/page_alloc.c | 84 ++++++++++++++--------------
12 files changed, 136 insertions(+), 149 deletions(-)

diff --git a/arch/arc/include/asm/arcregs.h b/arch/arc/include/asm/arcregs.h
index 2162023195c5..87becc380d6a 100644
--- a/arch/arc/include/asm/arcregs.h
+++ b/arch/arc/include/asm/arcregs.h
@@ -146,13 +146,13 @@

#ifndef __ASSEMBLY__

+#include <linux/mm.h>
#include <soc/arc/aux.h>

/* 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)
+#define PAGES_TO_MB(n_pages) (PG2KB(n_pages) >> 10)


/*
diff --git a/arch/powerpc/platforms/pseries/cmm.c b/arch/powerpc/platforms/pseries/cmm.c
index 45a3a3022a85..c22b9b66b930 100644
--- a/arch/powerpc/platforms/pseries/cmm.c
+++ b/arch/powerpc/platforms/pseries/cmm.c
@@ -12,6 +12,7 @@
#include <linux/fs.h>
#include <linux/gfp.h>
#include <linux/kthread.h>
+#include <linux/mm.h>
#include <linux/module.h>
#include <linux/oom.h>
#include <linux/reboot.h>
@@ -39,8 +40,6 @@
#define CMM_DISABLE 0
#define CMM_OOM_KB 1024
#define CMM_MIN_MEM_MB 256
-#define KB2PAGES(_p) ((_p)>>(PAGE_SHIFT-10))
-#define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))

#define CMM_MEM_HOTPLUG_PRI 1

@@ -215,13 +214,13 @@ static int cmm_oom_notify(struct notifier_block *self,
unsigned long dummy, void *parm)
{
unsigned long *freed = parm;
- long nr = KB2PAGES(oom_kb);
+ long nr = KB2PG(oom_kb);

cmm_dbg("OOM processing started\n");
nr = cmm_free_pages(nr);
loaned_pages_target = atomic_long_read(&loaned_pages);
- *freed += KB2PAGES(oom_kb) - nr;
- oom_freed_pages += KB2PAGES(oom_kb) - nr;
+ *freed += KB2PG(oom_kb) - nr;
+ oom_freed_pages += KB2PG(oom_kb) - nr;
cmm_dbg("OOM processing complete\n");
return NOTIFY_OK;
}
@@ -251,7 +250,7 @@ static void cmm_get_mpp(void)
PAGE_SIZE);
target = page_loan_request + __loaned_pages;
} else {
- target = KB2PAGES(simulate_loan_target_kb);
+ target = KB2PG(simulate_loan_target_kb);
page_loan_request = target - __loaned_pages;
}

@@ -342,13 +341,13 @@ static int cmm_thread(void *dummy)
} \
static DEVICE_ATTR(name, 0444, show_##name, NULL)

-CMM_SHOW(loaned_kb, "%lu\n", PAGES2KB(atomic_long_read(&loaned_pages)));
-CMM_SHOW(loaned_target_kb, "%lu\n", PAGES2KB(loaned_pages_target));
+CMM_SHOW(loaned_kb, "%lu\n", PG2KB(atomic_long_read(&loaned_pages)));
+CMM_SHOW(loaned_target_kb, "%lu\n", PG2KB(loaned_pages_target));

static ssize_t show_oom_pages(struct device *dev,
struct device_attribute *attr, char *buf)
{
- return sprintf(buf, "%lu\n", PAGES2KB(oom_freed_pages));
+ return sprintf(buf, "%lu\n", PG2KB(oom_freed_pages));
}

static ssize_t store_oom_pages(struct device *dev,
diff --git a/arch/s390/appldata/appldata_mem.c b/arch/s390/appldata/appldata_mem.c
index 21c3147bd92a..35f02d18d88c 100644
--- a/arch/s390/appldata/appldata_mem.c
+++ b/arch/s390/appldata/appldata_mem.c
@@ -8,6 +8,7 @@
* Author: Gerald Schaefer <[email protected]>
*/

+#include <linux/mm.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
@@ -20,8 +21,6 @@
#include "appldata.h"


-#define P2K(x) ((x) << (PAGE_SHIFT - 10)) /* Converts #Pages to KB */
-
/*
* Memory data
*
@@ -94,17 +93,17 @@ static void appldata_get_mem_data(void *data)

si_meminfo(&val);
mem_data->sharedram = val.sharedram;
- mem_data->totalram = P2K(val.totalram);
- mem_data->freeram = P2K(val.freeram);
- mem_data->totalhigh = P2K(val.totalhigh);
- mem_data->freehigh = P2K(val.freehigh);
- mem_data->bufferram = P2K(val.bufferram);
- mem_data->cached = P2K(global_node_page_state(NR_FILE_PAGES)
+ mem_data->totalram = PG2KB(val.totalram);
+ mem_data->freeram = PG2KB(val.freeram);
+ mem_data->totalhigh = PG2KB(val.totalhigh);
+ mem_data->freehigh = PG2KB(val.freehigh);
+ mem_data->bufferram = PG2KB(val.bufferram);
+ mem_data->cached = PG2KB(global_node_page_state(NR_FILE_PAGES)
- val.bufferram);

si_swapinfo(&val);
- mem_data->totalswap = P2K(val.totalswap);
- mem_data->freeswap = P2K(val.freeswap);
+ mem_data->totalswap = PG2KB(val.totalswap);
+ mem_data->freeswap = PG2KB(val.freeswap);

mem_data->timestamp = get_tod_clock();
mem_data->sync_count_2++;
diff --git a/drivers/base/node.c b/drivers/base/node.c
index c56d34f8158f..fcacb3d58167 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -365,7 +365,6 @@ static void node_init_caches(unsigned int nid) { }
static void node_remove_caches(struct node *node) { }
#endif

-#define K(x) ((x) << (PAGE_SHIFT - 10))
static ssize_t node_read_meminfo(struct device *dev,
struct device_attribute *attr, char *buf)
{
@@ -395,20 +394,20 @@ static ssize_t node_read_meminfo(struct device *dev,
"Node %d Inactive(file): %8lu kB\n"
"Node %d Unevictable: %8lu kB\n"
"Node %d Mlocked: %8lu kB\n",
- nid, K(i.totalram),
- nid, K(i.freeram),
- nid, K(i.totalram - i.freeram),
- nid, K(swapcached),
- nid, K(node_page_state(pgdat, NR_ACTIVE_ANON) +
+ nid, PG2KB(i.totalram),
+ nid, PG2KB(i.freeram),
+ nid, PG2KB(i.totalram - i.freeram),
+ nid, PG2KB(swapcached),
+ nid, PG2KB(node_page_state(pgdat, NR_ACTIVE_ANON) +
node_page_state(pgdat, NR_ACTIVE_FILE)),
- nid, K(node_page_state(pgdat, NR_INACTIVE_ANON) +
+ nid, PG2KB(node_page_state(pgdat, NR_INACTIVE_ANON) +
node_page_state(pgdat, NR_INACTIVE_FILE)),
- nid, K(node_page_state(pgdat, NR_ACTIVE_ANON)),
- nid, K(node_page_state(pgdat, NR_INACTIVE_ANON)),
- nid, K(node_page_state(pgdat, NR_ACTIVE_FILE)),
- nid, K(node_page_state(pgdat, NR_INACTIVE_FILE)),
- nid, K(node_page_state(pgdat, NR_UNEVICTABLE)),
- nid, K(sum_zone_node_page_state(nid, NR_MLOCK)));
+ nid, PG2KB(node_page_state(pgdat, NR_ACTIVE_ANON)),
+ nid, PG2KB(node_page_state(pgdat, NR_INACTIVE_ANON)),
+ nid, PG2KB(node_page_state(pgdat, NR_ACTIVE_FILE)),
+ nid, PG2KB(node_page_state(pgdat, NR_INACTIVE_FILE)),
+ nid, PG2KB(node_page_state(pgdat, NR_UNEVICTABLE)),
+ nid, PG2KB(sum_zone_node_page_state(nid, NR_MLOCK)));

#ifdef CONFIG_HIGHMEM
len += sysfs_emit_at(buf, len,
@@ -416,10 +415,10 @@ static ssize_t node_read_meminfo(struct device *dev,
"Node %d HighFree: %8lu kB\n"
"Node %d LowTotal: %8lu kB\n"
"Node %d LowFree: %8lu kB\n",
- nid, K(i.totalhigh),
- nid, K(i.freehigh),
- nid, K(i.totalram - i.totalhigh),
- nid, K(i.freeram - i.freehigh));
+ nid, PG2KB(i.totalhigh),
+ nid, PG2KB(i.freehigh),
+ nid, PG2KB(i.totalram - i.totalhigh),
+ nid, PG2KB(i.freeram - i.freehigh));
#endif
len += sysfs_emit_at(buf, len,
"Node %d Dirty: %8lu kB\n"
@@ -448,32 +447,32 @@ static ssize_t node_read_meminfo(struct device *dev,
"Node %d FilePmdMapped: %8lu kB\n"
#endif
,
- nid, K(node_page_state(pgdat, NR_FILE_DIRTY)),
- nid, K(node_page_state(pgdat, NR_WRITEBACK)),
- nid, K(node_page_state(pgdat, NR_FILE_PAGES)),
- nid, K(node_page_state(pgdat, NR_FILE_MAPPED)),
- nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
- nid, K(i.sharedram),
+ nid, PG2KB(node_page_state(pgdat, NR_FILE_DIRTY)),
+ nid, PG2KB(node_page_state(pgdat, NR_WRITEBACK)),
+ nid, PG2KB(node_page_state(pgdat, NR_FILE_PAGES)),
+ nid, PG2KB(node_page_state(pgdat, NR_FILE_MAPPED)),
+ nid, PG2KB(node_page_state(pgdat, NR_ANON_MAPPED)),
+ nid, PG2KB(i.sharedram),
nid, node_page_state(pgdat, NR_KERNEL_STACK_KB),
#ifdef CONFIG_SHADOW_CALL_STACK
nid, node_page_state(pgdat, NR_KERNEL_SCS_KB),
#endif
- nid, K(node_page_state(pgdat, NR_PAGETABLE)),
+ nid, PG2KB(node_page_state(pgdat, NR_PAGETABLE)),
nid, 0UL,
- nid, K(sum_zone_node_page_state(nid, NR_BOUNCE)),
- nid, K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
- nid, K(sreclaimable +
+ nid, PG2KB(sum_zone_node_page_state(nid, NR_BOUNCE)),
+ nid, PG2KB(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
+ nid, PG2KB(sreclaimable +
node_page_state(pgdat, NR_KERNEL_MISC_RECLAIMABLE)),
- nid, K(sreclaimable + sunreclaimable),
- nid, K(sreclaimable),
- nid, K(sunreclaimable)
+ nid, PG2KB(sreclaimable + sunreclaimable),
+ nid, PG2KB(sreclaimable),
+ nid, PG2KB(sunreclaimable)
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
,
- nid, K(node_page_state(pgdat, NR_ANON_THPS)),
- nid, K(node_page_state(pgdat, NR_SHMEM_THPS)),
- nid, K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
- nid, K(node_page_state(pgdat, NR_FILE_THPS)),
- nid, K(node_page_state(pgdat, NR_FILE_PMDMAPPED))
+ nid, PG2KB(node_page_state(pgdat, NR_ANON_THPS)),
+ nid, PG2KB(node_page_state(pgdat, NR_SHMEM_THPS)),
+ nid, PG2KB(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
+ nid, PG2KB(node_page_state(pgdat, NR_FILE_THPS)),
+ nid, PG2KB(node_page_state(pgdat, NR_FILE_PMDMAPPED))
#endif
);
len += hugetlb_report_node_meminfo(buf, len, nid);
diff --git a/drivers/scsi/mpt3sas/mpt3sas_base.c b/drivers/scsi/mpt3sas/mpt3sas_base.c
index 27eb652b564f..bb11f31f8431 100644
--- a/drivers/scsi/mpt3sas/mpt3sas_base.c
+++ b/drivers/scsi/mpt3sas/mpt3sas_base.c
@@ -44,6 +44,7 @@
*/

#include <linux/kernel.h>
+#include <linux/mm.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/init.h>
@@ -2988,8 +2989,6 @@ _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
}
}

-#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
-
/**
* _base_config_dma_addressing - set dma addressing
* @ioc: per adapter object
@@ -3026,7 +3025,7 @@ _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)

si_meminfo(&s);
ioc_info(ioc, "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
- ioc->dma_mask, convert_to_kb(s.totalram));
+ ioc->dma_mask, PG2KB(s.totalram));

return 0;
}
diff --git a/drivers/xen/xen-balloon.c b/drivers/xen/xen-balloon.c
index 8cd583db20b1..fdd39e5e1a1e 100644
--- a/drivers/xen/xen-balloon.c
+++ b/drivers/xen/xen-balloon.c
@@ -34,6 +34,7 @@

#include <linux/kernel.h>
#include <linux/errno.h>
+#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/init.h>
#include <linux/capability.h>
@@ -47,8 +48,6 @@
#include <xen/page.h>
#include <xen/mem-reservation.h>

-#define PAGES2KB(_p) ((_p)<<(PAGE_SHIFT-10))
-
#define BALLOON_CLASS_NAME "xen_memory"

#ifdef CONFIG_MEMORY_HOTPLUG
@@ -142,9 +141,9 @@ EXPORT_SYMBOL_GPL(xen_balloon_init);
} \
static DEVICE_ATTR_RO(name)

-BALLOON_SHOW(current_kb, "%lu\n", PAGES2KB(balloon_stats.current_pages));
-BALLOON_SHOW(low_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_low));
-BALLOON_SHOW(high_kb, "%lu\n", PAGES2KB(balloon_stats.balloon_high));
+BALLOON_SHOW(current_kb, "%lu\n", PG2KB(balloon_stats.current_pages));
+BALLOON_SHOW(low_kb, "%lu\n", PG2KB(balloon_stats.balloon_low));
+BALLOON_SHOW(high_kb, "%lu\n", PG2KB(balloon_stats.balloon_high));

static DEVICE_ULONG_ATTR(schedule_delay, 0444, balloon_stats.schedule_delay);
static DEVICE_ULONG_ATTR(max_schedule_delay, 0644, balloon_stats.max_schedule_delay);
@@ -155,7 +154,7 @@ static DEVICE_BOOL_ATTR(scrub_pages, 0644, xen_scrub_pages);
static ssize_t target_kb_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
- return sprintf(buf, "%lu\n", PAGES2KB(balloon_stats.target_pages));
+ return sprintf(buf, "%lu\n", PG2KB(balloon_stats.target_pages));
}

static ssize_t target_kb_store(struct device *dev,
diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h
index 7dccb66474f7..3a30e0cb3bee 100644
--- a/include/trace/events/writeback.h
+++ b/include/trace/events/writeback.h
@@ -5,6 +5,7 @@
#if !defined(_TRACE_WRITEBACK_H) || defined(TRACE_HEADER_MULTI_READ)
#define _TRACE_WRITEBACK_H

+#include <linux/mm.h>
#include <linux/tracepoint.h>
#include <linux/backing-dev.h>
#include <linux/writeback.h>
@@ -570,8 +571,6 @@ TRACE_EVENT(global_dirty_state,
)
);

-#define KBps(x) ((x) << (PAGE_SHIFT - 10))
-
TRACE_EVENT(bdi_dirty_ratelimit,

TP_PROTO(struct bdi_writeback *wb,
@@ -593,13 +592,13 @@ TRACE_EVENT(bdi_dirty_ratelimit,

TP_fast_assign(
strscpy_pad(__entry->bdi, bdi_dev_name(wb->bdi), 32);
- __entry->write_bw = KBps(wb->write_bandwidth);
- __entry->avg_write_bw = KBps(wb->avg_write_bandwidth);
- __entry->dirty_rate = KBps(dirty_rate);
- __entry->dirty_ratelimit = KBps(wb->dirty_ratelimit);
- __entry->task_ratelimit = KBps(task_ratelimit);
+ __entry->write_bw = PG2KB(wb->write_bandwidth);
+ __entry->avg_write_bw = PG2KB(wb->avg_write_bandwidth);
+ __entry->dirty_rate = PG2KB(dirty_rate);
+ __entry->dirty_ratelimit = PG2KB(wb->dirty_ratelimit);
+ __entry->task_ratelimit = PG2KB(task_ratelimit);
__entry->balanced_dirty_ratelimit =
- KBps(wb->balanced_dirty_ratelimit);
+ PG2KB(wb->balanced_dirty_ratelimit);
__entry->cgroup_ino = __trace_wb_assign_cgroup(wb);
),

@@ -666,8 +665,8 @@ TRACE_EVENT(balance_dirty_pages,
__entry->bdi_setpoint = __entry->setpoint *
bdi_thresh / (thresh + 1);
__entry->bdi_dirty = bdi_dirty;
- __entry->dirty_ratelimit = KBps(dirty_ratelimit);
- __entry->task_ratelimit = KBps(task_ratelimit);
+ __entry->dirty_ratelimit = PG2KB(dirty_ratelimit);
+ __entry->task_ratelimit = PG2KB(task_ratelimit);
__entry->dirtied = dirtied;
__entry->dirtied_pause = current->nr_dirtied_pause;
__entry->think = current->dirty_paused_when == 0 ? 0 :
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index fa6deda894a1..b1bbc8f39171 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -2522,10 +2522,9 @@ static int kdb_summary(int argc, const char **argv)
LOAD_INT(val.loads[2]), LOAD_FRAC(val.loads[2]));

/* Display in kilobytes */
-#define K(x) ((x) << (PAGE_SHIFT - 10))
kdb_printf("\nMemTotal: %8lu kB\nMemFree: %8lu kB\n"
"Buffers: %8lu kB\n",
- K(val.totalram), K(val.freeram), K(val.bufferram));
+ PG2KB(val.totalram), PG2KB(val.freeram), PG2KB(val.bufferram));
return 0;
}

diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index 4a9d4e27d0d9..b1d9c7bb35b3 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -33,8 +33,6 @@ LIST_HEAD(bdi_list);
/* bdi_wq serves all asynchronous writeback tasks */
struct workqueue_struct *bdi_wq;

-#define K(x) ((x) << (PAGE_SHIFT - 10))
-
#ifdef CONFIG_DEBUG_FS
#include <linux/debugfs.h>
#include <linux/seq_file.h>
@@ -87,14 +85,14 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
"b_dirty_time: %10lu\n"
"bdi_list: %10u\n"
"state: %10lx\n",
- (unsigned long) K(wb_stat(wb, WB_WRITEBACK)),
- (unsigned long) K(wb_stat(wb, WB_RECLAIMABLE)),
- K(wb_thresh),
- K(dirty_thresh),
- K(background_thresh),
- (unsigned long) K(wb_stat(wb, WB_DIRTIED)),
- (unsigned long) K(wb_stat(wb, WB_WRITTEN)),
- (unsigned long) K(wb->write_bandwidth),
+ (unsigned long) PG2KB(wb_stat(wb, WB_WRITEBACK)),
+ (unsigned long) PG2KB(wb_stat(wb, WB_RECLAIMABLE)),
+ PG2KB(wb_thresh),
+ PG2KB(dirty_thresh),
+ PG2KB(background_thresh),
+ (unsigned long) PG2KB(wb_stat(wb, WB_DIRTIED)),
+ (unsigned long) PG2KB(wb_stat(wb, WB_WRITTEN)),
+ (unsigned long) PG2KB(wb->write_bandwidth),
nr_dirty,
nr_io,
nr_more_io,
@@ -157,7 +155,7 @@ static ssize_t name##_show(struct device *dev, \
} \
static DEVICE_ATTR_RW(name);

-BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
+BDI_SHOW(read_ahead_kb, PG2KB(bdi->ra_pages))

static ssize_t min_ratio_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 47dc4959ead8..5071380c77f0 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -53,6 +53,7 @@
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/vmpressure.h>
+#include <linux/mm.h>
#include <linux/mm_inline.h>
#include <linux/swap_cgroup.h>
#include <linux/cpu.h>
@@ -1471,7 +1472,6 @@ static char *memory_stat_format(struct mem_cgroup *memcg)
return s.buffer;
}

-#define K(x) ((x) << (PAGE_SHIFT-10))
/**
* mem_cgroup_print_oom_context: Print OOM information relevant to
* memory controller.
@@ -1507,19 +1507,19 @@ void mem_cgroup_print_oom_meminfo(struct mem_cgroup *memcg)
char *buf;

pr_info("memory: usage %llukB, limit %llukB, failcnt %lu\n",
- K((u64)page_counter_read(&memcg->memory)),
- K((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt);
+ PG2KB((u64)page_counter_read(&memcg->memory)),
+ PG2KB((u64)READ_ONCE(memcg->memory.max)), memcg->memory.failcnt);
if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
pr_info("swap: usage %llukB, limit %llukB, failcnt %lu\n",
- K((u64)page_counter_read(&memcg->swap)),
- K((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt);
+ PG2KB((u64)page_counter_read(&memcg->swap)),
+ PG2KB((u64)READ_ONCE(memcg->swap.max)), memcg->swap.failcnt);
else {
pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %lu\n",
- K((u64)page_counter_read(&memcg->memsw)),
- K((u64)memcg->memsw.max), memcg->memsw.failcnt);
+ PG2KB((u64)page_counter_read(&memcg->memsw)),
+ PG2KB((u64)memcg->memsw.max), memcg->memsw.failcnt);
pr_info("kmem: usage %llukB, limit %llukB, failcnt %lu\n",
- K((u64)page_counter_read(&memcg->kmem)),
- K((u64)memcg->kmem.max), memcg->kmem.failcnt);
+ PG2KB((u64)page_counter_read(&memcg->kmem)),
+ PG2KB((u64)memcg->kmem.max), memcg->kmem.failcnt);
}

pr_info("Memory cgroup stats for ");
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 431d38c3bba8..9c7a6ec4b298 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -485,8 +485,6 @@ static DECLARE_WAIT_QUEUE_HEAD(oom_victims_wait);

static bool oom_killer_disabled __read_mostly;

-#define K(x) ((x) << (PAGE_SHIFT-10))
-
/*
* task->mm can be NULL if the task is the exited group leader. So to
* determine whether the task is using a particular mm, we examine all the
@@ -599,9 +597,9 @@ static bool oom_reap_task_mm(struct task_struct *tsk, struct mm_struct *mm)

pr_info("oom_reaper: reaped process %d (%s), now anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB\n",
task_pid_nr(tsk), tsk->comm,
- K(get_mm_counter(mm, MM_ANONPAGES)),
- K(get_mm_counter(mm, MM_FILEPAGES)),
- K(get_mm_counter(mm, MM_SHMEMPAGES)));
+ PG2KB(get_mm_counter(mm, MM_ANONPAGES)),
+ PG2KB(get_mm_counter(mm, MM_FILEPAGES)),
+ PG2KB(get_mm_counter(mm, MM_SHMEMPAGES)));
out_finish:
trace_finish_task_reaping(tsk->pid);
out_unlock:
@@ -894,10 +892,10 @@ static void __oom_kill_process(struct task_struct *victim, const char *message)
do_send_sig_info(SIGKILL, SEND_SIG_PRIV, victim, PIDTYPE_TGID);
mark_oom_victim(victim);
pr_err("%s: Killed process %d (%s) total-vm:%lukB, anon-rss:%lukB, file-rss:%lukB, shmem-rss:%lukB, UID:%u pgtables:%lukB oom_score_adj:%hd\n",
- message, task_pid_nr(victim), victim->comm, K(mm->total_vm),
- K(get_mm_counter(mm, MM_ANONPAGES)),
- K(get_mm_counter(mm, MM_FILEPAGES)),
- K(get_mm_counter(mm, MM_SHMEMPAGES)),
+ message, task_pid_nr(victim), victim->comm, PG2KB(mm->total_vm),
+ PG2KB(get_mm_counter(mm, MM_ANONPAGES)),
+ PG2KB(get_mm_counter(mm, MM_FILEPAGES)),
+ PG2KB(get_mm_counter(mm, MM_SHMEMPAGES)),
from_kuid(&init_user_ns, task_uid(victim)),
mm_pgtables_bytes(mm) >> 10, victim->signal->oom_score_adj);
task_unlock(victim);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 3b610b05d9b8..5fad2f6d7d5f 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -5836,8 +5836,6 @@ static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask
return !node_isset(nid, *nodemask);
}

-#define K(x) ((x) << (PAGE_SHIFT-10))
-
static void show_migration_types(unsigned char type)
{
static const char types[MIGRATE_TYPES] = {
@@ -5946,28 +5944,28 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
" all_unreclaimable? %s"
"\n",
pgdat->node_id,
- K(node_page_state(pgdat, NR_ACTIVE_ANON)),
- K(node_page_state(pgdat, NR_INACTIVE_ANON)),
- K(node_page_state(pgdat, NR_ACTIVE_FILE)),
- K(node_page_state(pgdat, NR_INACTIVE_FILE)),
- K(node_page_state(pgdat, NR_UNEVICTABLE)),
- K(node_page_state(pgdat, NR_ISOLATED_ANON)),
- K(node_page_state(pgdat, NR_ISOLATED_FILE)),
- K(node_page_state(pgdat, NR_FILE_MAPPED)),
- K(node_page_state(pgdat, NR_FILE_DIRTY)),
- K(node_page_state(pgdat, NR_WRITEBACK)),
- K(node_page_state(pgdat, NR_SHMEM)),
+ PG2KB(node_page_state(pgdat, NR_ACTIVE_ANON)),
+ PG2KB(node_page_state(pgdat, NR_INACTIVE_ANON)),
+ PG2KB(node_page_state(pgdat, NR_ACTIVE_FILE)),
+ PG2KB(node_page_state(pgdat, NR_INACTIVE_FILE)),
+ PG2KB(node_page_state(pgdat, NR_UNEVICTABLE)),
+ PG2KB(node_page_state(pgdat, NR_ISOLATED_ANON)),
+ PG2KB(node_page_state(pgdat, NR_ISOLATED_FILE)),
+ PG2KB(node_page_state(pgdat, NR_FILE_MAPPED)),
+ PG2KB(node_page_state(pgdat, NR_FILE_DIRTY)),
+ PG2KB(node_page_state(pgdat, NR_WRITEBACK)),
+ PG2KB(node_page_state(pgdat, NR_SHMEM)),
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
- K(node_page_state(pgdat, NR_SHMEM_THPS)),
- K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
- K(node_page_state(pgdat, NR_ANON_THPS)),
+ PG2KB(node_page_state(pgdat, NR_SHMEM_THPS)),
+ PG2KB(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)),
+ PG2KB(node_page_state(pgdat, NR_ANON_THPS)),
#endif
- K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
+ PG2KB(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
node_page_state(pgdat, NR_KERNEL_STACK_KB),
#ifdef CONFIG_SHADOW_CALL_STACK
node_page_state(pgdat, NR_KERNEL_SCS_KB),
#endif
- K(node_page_state(pgdat, NR_PAGETABLE)),
+ PG2KB(node_page_state(pgdat, NR_PAGETABLE)),
pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ?
"yes" : "no");
}
@@ -6005,24 +6003,24 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
" free_cma:%lukB"
"\n",
zone->name,
- K(zone_page_state(zone, NR_FREE_PAGES)),
- K(min_wmark_pages(zone)),
- K(low_wmark_pages(zone)),
- K(high_wmark_pages(zone)),
- K(zone->nr_reserved_highatomic),
- K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
- K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
- K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
- K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
- K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
- K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
- K(zone->present_pages),
- K(zone_managed_pages(zone)),
- K(zone_page_state(zone, NR_MLOCK)),
- K(zone_page_state(zone, NR_BOUNCE)),
- K(free_pcp),
- K(this_cpu_read(zone->per_cpu_pageset->count)),
- K(zone_page_state(zone, NR_FREE_CMA_PAGES)));
+ PG2KB(zone_page_state(zone, NR_FREE_PAGES)),
+ PG2KB(min_wmark_pages(zone)),
+ PG2KB(low_wmark_pages(zone)),
+ PG2KB(high_wmark_pages(zone)),
+ PG2KB(zone->nr_reserved_highatomic),
+ PG2KB(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
+ PG2KB(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
+ PG2KB(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
+ PG2KB(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
+ PG2KB(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
+ PG2KB(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
+ PG2KB(zone->present_pages),
+ PG2KB(zone_managed_pages(zone)),
+ PG2KB(zone_page_state(zone, NR_MLOCK)),
+ PG2KB(zone_page_state(zone, NR_BOUNCE)),
+ PG2KB(free_pcp),
+ PG2KB(this_cpu_read(zone->per_cpu_pageset->count)),
+ PG2KB(zone_page_state(zone, NR_FREE_CMA_PAGES)));
printk("lowmem_reserve[]:");
for (i = 0; i < MAX_NR_ZONES; i++)
printk(KERN_CONT " %ld", zone->lowmem_reserve[i]);
@@ -6056,11 +6054,11 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
spin_unlock_irqrestore(&zone->lock, flags);
for (order = 0; order < MAX_ORDER; order++) {
printk(KERN_CONT "%lu*%lukB ",
- nr[order], K(1UL) << order);
+ nr[order], PG2KB(1UL) << order);
if (nr[order])
show_migration_types(types[order]);
}
- printk(KERN_CONT "= %lukB\n", K(total));
+ printk(KERN_CONT "= %lukB\n", PG2KB(total));
}

hugetlb_show_meminfo();
@@ -8136,7 +8134,7 @@ unsigned long free_reserved_area(void *start, void *end, int poison, const char
}

if (pages && s)
- pr_info("Freeing %s memory: %ldK\n", s, K(pages));
+ pr_info("Freeing %s memory: %ldK\n", s, PG2KB(pages));

return pages;
}
@@ -8181,13 +8179,13 @@ void __init mem_init_print_info(void)
", %luK highmem"
#endif
")\n",
- K(nr_free_pages()), K(physpages),
+ PG2KB(nr_free_pages()), PG2KB(physpages),
codesize >> 10, datasize >> 10, rosize >> 10,
(init_data_size + init_code_size) >> 10, bss_size >> 10,
- K(physpages - totalram_pages() - totalcma_pages),
- K(totalcma_pages)
+ PG2KB(physpages - totalram_pages() - totalcma_pages),
+ PG2KB(totalcma_pages)
#ifdef CONFIG_HIGHMEM
- , K(totalhigh_pages())
+ , PG2KB(totalhigh_pages())
#endif
);
}
--
2.33.0

2021-09-19 19:23:00

by Oleksandr Natalenko

[permalink] [raw]
Subject: [RFC v2 PATCH 3/3] mm: replace open-coded PG2KB/KB2PG variants with macros

Signed-off-by: Oleksandr Natalenko <[email protected]>
---
arch/alpha/kernel/setup.c | 3 +--
arch/mips/mm/init.c | 2 +-
arch/x86/kernel/cpu/mtrr/cleanup.c | 17 +++++++++--------
block/blk-sysfs.c | 9 +++++----
drivers/gpu/drm/v3d/v3d_debugfs.c | 3 ++-
drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c | 5 +++--
drivers/md/md-bitmap.c | 4 ++--
fs/ceph/addr.c | 2 +-
fs/ceph/super.h | 3 ++-
fs/fs-writeback.c | 2 +-
fs/nfs/write.c | 4 ++--
fs/nfsd/nfscache.c | 3 ++-
fs/proc/meminfo.c | 4 ++--
fs/proc/task_mmu.c | 3 ++-
kernel/events/core.c | 2 +-
kernel/fork.c | 2 +-
mm/backing-dev.c | 2 +-
mm/hugetlb.c | 2 +-
mm/mmap.c | 6 +++---
mm/nommu.c | 4 ++--
mm/page-writeback.c | 4 ++--
mm/page_alloc.c | 2 +-
mm/shmem.c | 3 +--
mm/swap_state.c | 5 ++---
mm/swapfile.c | 11 +++++------
mm/util.c | 6 +++---
26 files changed, 58 insertions(+), 55 deletions(-)

diff --git a/arch/alpha/kernel/setup.c b/arch/alpha/kernel/setup.c
index b4fbbba30aa2..c4282224c950 100644
--- a/arch/alpha/kernel/setup.c
+++ b/arch/alpha/kernel/setup.c
@@ -355,8 +355,7 @@ setup_memory(void *kernel_end)
if (mem_size_limit && max_low_pfn >= mem_size_limit)
{
printk("setup: forcing memory size to %ldK (from %ldK).\n",
- mem_size_limit << (PAGE_SHIFT - 10),
- max_low_pfn << (PAGE_SHIFT - 10));
+ PG2KB(mem_size_limit), PG2KB(max_low_pfn));
max_low_pfn = mem_size_limit;
}

diff --git a/arch/mips/mm/init.c b/arch/mips/mm/init.c
index 19347dc6bbf8..351a44437377 100644
--- a/arch/mips/mm/init.c
+++ b/arch/mips/mm/init.c
@@ -414,7 +414,7 @@ void __init paging_init(void)
if (cpu_has_dc_aliases && max_low_pfn != highend_pfn) {
printk(KERN_WARNING "This processor doesn't support highmem."
" %ldk highmem ignored\n",
- (highend_pfn - max_low_pfn) << (PAGE_SHIFT - 10));
+ PG2KB(highend_pfn - max_low_pfn));
max_zone_pfns[ZONE_HIGHMEM] = max_low_pfn;
}
#endif
diff --git a/arch/x86/kernel/cpu/mtrr/cleanup.c b/arch/x86/kernel/cpu/mtrr/cleanup.c
index b5f43049fa5f..327aef0ff945 100644
--- a/arch/x86/kernel/cpu/mtrr/cleanup.c
+++ b/arch/x86/kernel/cpu/mtrr/cleanup.c
@@ -18,6 +18,7 @@
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/init.h>
+#include <linux/mm.h>
#include <linux/pci.h>
#include <linux/smp.h>
#include <linux/cpu.h>
@@ -204,8 +205,8 @@ static void __init
save_var_mtrr(unsigned int reg, unsigned long basek, unsigned long sizek,
unsigned char type)
{
- range_state[reg].base_pfn = basek >> (PAGE_SHIFT - 10);
- range_state[reg].size_pfn = sizek >> (PAGE_SHIFT - 10);
+ range_state[reg].base_pfn = KB2PG(basek);
+ range_state[reg].size_pfn = KB2PG(sizek);
range_state[reg].type = type;
}

@@ -216,8 +217,8 @@ static void __init set_var_mtrr_all(unsigned int address_bits)
unsigned int reg;

for (reg = 0; reg < num_var_ranges; reg++) {
- basek = range_state[reg].base_pfn << (PAGE_SHIFT - 10);
- sizek = range_state[reg].size_pfn << (PAGE_SHIFT - 10);
+ basek = PG2KB(range_state[reg].base_pfn);
+ sizek = PG2KB(range_state[reg].size_pfn);
type = range_state[reg].type;

set_var_mtrr(reg, basek, sizek, type, address_bits);
@@ -415,8 +416,8 @@ set_var_mtrr_range(struct var_mtrr_state *state, unsigned long base_pfn,
if (state->reg >= num_var_ranges)
return;

- basek = base_pfn << (PAGE_SHIFT - 10);
- sizek = size_pfn << (PAGE_SHIFT - 10);
+ basek = PG2KB(base_pfn);
+ sizek = PG2KB(size_pfn);

/* See if I can merge with the last range: */
if ((basek <= 1024) ||
@@ -533,12 +534,12 @@ static void __init print_out_mtrr_range_state(void)

for (i = 0; i < num_var_ranges; i++) {

- size_base = range_state[i].size_pfn << (PAGE_SHIFT - 10);
+ size_base = PG2KB(range_state[i].size_pfn);
if (!size_base)
continue;

size_base = to_size_factor(size_base, &size_factor);
- start_base = range_state[i].base_pfn << (PAGE_SHIFT - 10);
+ start_base = PG2KB(range_state[i].base_pfn);
start_base = to_size_factor(start_base, &start_factor);
type = range_state[i].type;

diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c
index 614d9d47de36..25e0bcc1208f 100644
--- a/block/blk-sysfs.c
+++ b/block/blk-sysfs.c
@@ -4,6 +4,7 @@
*/
#include <linux/kernel.h>
#include <linux/slab.h>
+#include <linux/mm.h>
#include <linux/module.h>
#include <linux/bio.h>
#include <linux/blkdev.h>
@@ -92,7 +93,7 @@ static ssize_t queue_ra_show(struct request_queue *q, char *page)

if (!q->disk)
return -EINVAL;
- ra_kb = q->disk->bdi->ra_pages << (PAGE_SHIFT - 10);
+ ra_kb = PG2KB(q->disk->bdi->ra_pages);
return queue_var_show(ra_kb, page);
}

@@ -107,7 +108,7 @@ queue_ra_store(struct request_queue *q, const char *page, size_t count)
ret = queue_var_store(&ra_kb, page, count);
if (ret < 0)
return ret;
- q->disk->bdi->ra_pages = ra_kb >> (PAGE_SHIFT - 10);
+ q->disk->bdi->ra_pages = KB2PG(ra_kb);
return ret;
}

@@ -240,7 +241,7 @@ queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
{
unsigned long max_sectors_kb,
max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
- page_kb = 1 << (PAGE_SHIFT - 10);
+ page_kb = PG2KB(1);
ssize_t ret = queue_var_store(&max_sectors_kb, page, count);

if (ret < 0)
@@ -255,7 +256,7 @@ queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
spin_lock_irq(&q->queue_lock);
q->limits.max_sectors = max_sectors_kb << 1;
if (q->disk)
- q->disk->bdi->io_pages = max_sectors_kb >> (PAGE_SHIFT - 10);
+ q->disk->bdi->io_pages = KB2PG(max_sectors_kb);
spin_unlock_irq(&q->queue_lock);

return ret;
diff --git a/drivers/gpu/drm/v3d/v3d_debugfs.c b/drivers/gpu/drm/v3d/v3d_debugfs.c
index e76b24bb8828..29461f9056b3 100644
--- a/drivers/gpu/drm/v3d/v3d_debugfs.c
+++ b/drivers/gpu/drm/v3d/v3d_debugfs.c
@@ -4,6 +4,7 @@
#include <linux/circ_buf.h>
#include <linux/ctype.h>
#include <linux/debugfs.h>
+#include <linux/mm.h>
#include <linux/pm_runtime.h>
#include <linux/seq_file.h>

@@ -203,7 +204,7 @@ static int v3d_debugfs_bo_stats(struct seq_file *m, void *unused)
seq_printf(m, "allocated bos: %d\n",
v3d->bo_stats.num_allocated);
seq_printf(m, "allocated bo size (kb): %ld\n",
- (long)v3d->bo_stats.pages_allocated << (PAGE_SHIFT - 10));
+ PG2KB((long)v3d->bo_stats.pages_allocated));
mutex_unlock(&v3d->bo_lock);

return 0;
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c b/drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c
index b2c4af331c9d..1083a054d235 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_gmrid_manager.c
@@ -32,6 +32,7 @@
#include <drm/ttm/ttm_bo_driver.h>
#include <drm/ttm/ttm_placement.h>
#include <linux/idr.h>
+#include <linux/mm.h>
#include <linux/spinlock.h>
#include <linux/kernel.h>

@@ -92,14 +93,14 @@ static int vmw_gmrid_man_get_node(struct ttm_resource_manager *man,
new_max_pages = gman->max_gmr_pages * 2;
if (new_max_pages > gman->max_gmr_pages && new_max_pages >= gman->used_gmr_pages) {
DRM_WARN("vmwgfx: increasing guest mob limits to %u kB.\n",
- ((new_max_pages) << (PAGE_SHIFT - 10)));
+ PG2KB(new_max_pages));

gman->max_gmr_pages = new_max_pages;
} else {
char buf[256];
snprintf(buf, sizeof(buf),
"vmwgfx, error: guest graphics is out of memory (mob limit at: %ukB).\n",
- ((gman->max_gmr_pages) << (PAGE_SHIFT - 10)));
+ PG2KB(gman->max_gmr_pages));
vmw_host_printf(buf);
DRM_WARN("%s", buf);
goto nospace;
diff --git a/drivers/md/md-bitmap.c b/drivers/md/md-bitmap.c
index e29c6298ef5c..85277df1da5a 100644
--- a/drivers/md/md-bitmap.c
+++ b/drivers/md/md-bitmap.c
@@ -17,6 +17,7 @@
*/

#include <linux/blkdev.h>
+#include <linux/mm.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/slab.h>
@@ -2038,8 +2039,7 @@ void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
"%lu%s chunk",
counts->pages - counts->missing_pages,
counts->pages,
- (counts->pages - counts->missing_pages)
- << (PAGE_SHIFT - 10),
+ PG2KB(counts->pages - counts->missing_pages),
chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
chunk_kb ? "KB" : "B");
if (bitmap->storage.file) {
diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c
index 99b80b5c7a93..e9d296cffb83 100644
--- a/fs/ceph/addr.c
+++ b/fs/ceph/addr.c
@@ -57,7 +57,7 @@
* accounting is preserved.
*/

-#define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
+#define CONGESTION_ON_THRESH(congestion_kb) KB2PG(congestion_kb)
#define CONGESTION_OFF_THRESH(congestion_kb) \
(CONGESTION_ON_THRESH(congestion_kb) - \
(CONGESTION_ON_THRESH(congestion_kb) >> 2))
diff --git a/fs/ceph/super.h b/fs/ceph/super.h
index a40eb14c282a..328d40843dbc 100644
--- a/fs/ceph/super.h
+++ b/fs/ceph/super.h
@@ -9,6 +9,7 @@
#include <linux/completion.h>
#include <linux/exportfs.h>
#include <linux/fs.h>
+#include <linux/mm.h>
#include <linux/mempool.h>
#include <linux/pagemap.h>
#include <linux/wait.h>
@@ -908,7 +909,7 @@ static inline int default_congestion_kb(void)
* This allows larger machines to have larger/more transfers.
* Limit the default to 256M
*/
- congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
+ congestion_kb = PG2KB(16*int_sqrt(totalram_pages()));
if (congestion_kb > 256*1024)
congestion_kb = 256*1024;

diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
index 81ec192ce067..1b694e6a27c0 100644
--- a/fs/fs-writeback.c
+++ b/fs/fs-writeback.c
@@ -34,7 +34,7 @@
/*
* 4MB minimal write chunk size
*/
-#define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_SHIFT - 10))
+#define MIN_WRITEBACK_PAGES KB2PG(4096UL)

/*
* Passed into wb_writeback(), essentially a subset of writeback_control
diff --git a/fs/nfs/write.c b/fs/nfs/write.c
index eae9bf114041..9b565808c12f 100644
--- a/fs/nfs/write.c
+++ b/fs/nfs/write.c
@@ -401,7 +401,7 @@ static int wb_priority(struct writeback_control *wbc)

int nfs_congestion_kb;

-#define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
+#define NFS_CONGESTION_ON_THRESH KB2PG(nfs_congestion_kb)
#define NFS_CONGESTION_OFF_THRESH \
(NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))

@@ -2173,7 +2173,7 @@ int __init nfs_init_writepagecache(void)
* This allows larger machines to have larger/more transfers.
* Limit the default to 256M
*/
- nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10);
+ nfs_congestion_kb = PG2KB(16*int_sqrt(totalram_pages()));
if (nfs_congestion_kb > 256*1024)
nfs_congestion_kb = 256*1024;

diff --git a/fs/nfsd/nfscache.c b/fs/nfsd/nfscache.c
index 96cdf77925f3..aeb464f88fa7 100644
--- a/fs/nfsd/nfscache.c
+++ b/fs/nfsd/nfscache.c
@@ -9,6 +9,7 @@
* Copyright (C) 1995, 1996 Olaf Kirch <[email protected]>
*/

+#include <linux/mm.h>
#include <linux/sunrpc/svc_xprt.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
@@ -70,7 +71,7 @@ nfsd_cache_size_limit(void)
unsigned int limit;
unsigned long low_pages = totalram_pages() - totalhigh_pages();

- limit = (16 * int_sqrt(low_pages)) << (PAGE_SHIFT-10);
+ limit = PG2KB(16 * int_sqrt(low_pages));
return min_t(unsigned int, limit, 256*1024);
}

diff --git a/fs/proc/meminfo.c b/fs/proc/meminfo.c
index 6fa761c9cc78..5443be3060a8 100644
--- a/fs/proc/meminfo.c
+++ b/fs/proc/meminfo.c
@@ -25,7 +25,7 @@ void __attribute__((weak)) arch_report_meminfo(struct seq_file *m)

static void show_val_kb(struct seq_file *m, const char *s, unsigned long num)
{
- seq_put_decimal_ull_width(m, s, num << (PAGE_SHIFT - 10), 8);
+ seq_put_decimal_ull_width(m, s, PG2KB(num), 8);
seq_write(m, " kB\n", 4);
}

@@ -124,7 +124,7 @@ static int meminfo_proc_show(struct seq_file *m, void *v)

#ifdef CONFIG_MEMORY_FAILURE
seq_printf(m, "HardwareCorrupted: %5lu kB\n",
- atomic_long_read(&num_poisoned_pages) << (PAGE_SHIFT - 10));
+ PG2KB(atomic_long_read(&num_poisoned_pages)));
#endif

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index cf25be3e0321..11798f84292c 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -3,6 +3,7 @@
#include <linux/vmacache.h>
#include <linux/hugetlb.h>
#include <linux/huge_mm.h>
+#include <linux/mm.h>
#include <linux/mount.h>
#include <linux/seq_file.h>
#include <linux/highmem.h>
@@ -26,7 +27,7 @@
#include "internal.h"

#define SEQ_PUT_DEC(str, val) \
- seq_put_decimal_ull_width(m, str, (val) << (PAGE_SHIFT-10), 8)
+ seq_put_decimal_ull_width(m, str, PG2KB(val), 8)
void task_mem(struct seq_file *m, struct mm_struct *mm)
{
unsigned long text, lib, swap, anon, file, shmem;
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 349f80aa9e7d..ee290e405d00 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6267,7 +6267,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
user_extra = nr_pages + 1;

accounting:
- user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
+ user_lock_limit = KB2PG(sysctl_perf_event_mlock);

/*
* Increase the limit linearly with more CPUs:
diff --git a/kernel/fork.c b/kernel/fork.c
index 723125f2cbed..04302967685f 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2325,7 +2325,7 @@ static __latent_entropy struct task_struct *copy_process(
}

p->nr_dirtied = 0;
- p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
+ p->nr_dirtied_pause = KB2PG(128);
p->dirty_paused_when = 0;

p->pdeath_signal = 0;
diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index b1d9c7bb35b3..c0820114e1dd 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -140,7 +140,7 @@ static ssize_t read_ahead_kb_store(struct device *dev,
if (ret < 0)
return ret;

- bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
+ bdi->ra_pages = KB2PG(read_ahead_kb);

return count;
}
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 6378c1066459..f496cef8abef 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4050,7 +4050,7 @@ void hugetlb_show_meminfo(void)
void hugetlb_report_usage(struct seq_file *m, struct mm_struct *mm)
{
seq_printf(m, "HugetlbPages:\t%8lu kB\n",
- atomic_long_read(&mm->hugetlb_usage) << (PAGE_SHIFT - 10));
+ PG2KB(atomic_long_read(&mm->hugetlb_usage)));
}

/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
diff --git a/mm/mmap.c b/mm/mmap.c
index b22a07f5e761..95c4883d2a87 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -3703,7 +3703,7 @@ static int init_user_reserve(void)
{
unsigned long free_kbytes;

- free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
+ free_kbytes = PG2KB(global_zone_page_state(NR_FREE_PAGES));

sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
return 0;
@@ -3724,7 +3724,7 @@ static int init_admin_reserve(void)
{
unsigned long free_kbytes;

- free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
+ free_kbytes = PG2KB(global_zone_page_state(NR_FREE_PAGES));

sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
return 0;
@@ -3768,7 +3768,7 @@ static int reserve_mem_notifier(struct notifier_block *nb,

break;
case MEM_OFFLINE:
- free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
+ free_kbytes = PG2KB(global_zone_page_state(NR_FREE_PAGES));

if (sysctl_user_reserve_kbytes > free_kbytes) {
init_user_reserve();
diff --git a/mm/nommu.c b/mm/nommu.c
index 8943dc0e2132..783526838176 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1803,7 +1803,7 @@ static int __meminit init_user_reserve(void)
{
unsigned long free_kbytes;

- free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
+ free_kbytes = PG2KB(global_zone_page_state(NR_FREE_PAGES));

sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
return 0;
@@ -1824,7 +1824,7 @@ static int __meminit init_admin_reserve(void)
{
unsigned long free_kbytes;

- free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
+ free_kbytes = PG2KB(global_zone_page_state(NR_FREE_PAGES));

sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
return 0;
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 9c64490171e0..1e3ab56ded46 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -50,7 +50,7 @@
* Try to keep balance_dirty_pages() call intervals higher than this many pages
* by raising pause time to max_pause when falls below it.
*/
-#define DIRTY_POLL_THRESH (128 >> (PAGE_SHIFT - 10))
+#define DIRTY_POLL_THRESH KB2PG(128)

/*
* Estimate write bandwidth at 200ms intervals.
@@ -1893,7 +1893,7 @@ void balance_dirty_pages_ratelimited(struct address_space *mapping)

ratelimit = current->nr_dirtied_pause;
if (wb->dirty_exceeded)
- ratelimit = min(ratelimit, 32 >> (PAGE_SHIFT - 10));
+ ratelimit = min(ratelimit, KB2PG(32));

preempt_disable();
/*
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 5fad2f6d7d5f..8617559ca211 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -8350,7 +8350,7 @@ static void setup_per_zone_lowmem_reserve(void)

static void __setup_per_zone_wmarks(void)
{
- unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
+ unsigned long pages_min = KB2PG(min_free_kbytes);
unsigned long lowmem_pages = 0;
struct zone *zone;
unsigned long flags;
diff --git a/mm/shmem.c b/mm/shmem.c
index 6f975d4e08f4..95a9e0e81779 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -3565,8 +3565,7 @@ static int shmem_show_options(struct seq_file *seq, struct dentry *root)
struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);

if (sbinfo->max_blocks != shmem_default_max_blocks())
- seq_printf(seq, ",size=%luk",
- sbinfo->max_blocks << (PAGE_SHIFT - 10));
+ seq_printf(seq, ",size=%luk", PG2KB(sbinfo->max_blocks));
if (sbinfo->max_inodes != shmem_default_max_inodes())
seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
if (sbinfo->mode != (0777 | S_ISVTX))
diff --git a/mm/swap_state.c b/mm/swap_state.c
index 8d4104242100..0a11ca2aba50 100644
--- a/mm/swap_state.c
+++ b/mm/swap_state.c
@@ -76,9 +76,8 @@ void show_swap_cache_info(void)
printk("Swap cache stats: add %lu, delete %lu, find %lu/%lu\n",
swap_cache_info.add_total, swap_cache_info.del_total,
swap_cache_info.find_success, swap_cache_info.find_total);
- printk("Free swap = %ldkB\n",
- get_nr_swap_pages() << (PAGE_SHIFT - 10));
- printk("Total swap = %lukB\n", total_swap_pages << (PAGE_SHIFT - 10));
+ printk("Free swap = %ldkB\n", PG2KB(get_nr_swap_pages()));
+ printk("Total swap = %lukB\n", PG2KB(total_swap_pages));
}

void *get_shadow_from_swap_cache(swp_entry_t entry)
diff --git a/mm/swapfile.c b/mm/swapfile.c
index e3dcaeecc50f..1a8f95edd3c3 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2770,8 +2770,8 @@ static int swap_show(struct seq_file *swap, void *v)
return 0;
}

- bytes = si->pages << (PAGE_SHIFT - 10);
- inuse = si->inuse_pages << (PAGE_SHIFT - 10);
+ bytes = PG2KB(si->pages);
+ inuse = PG2KB(si->inuse_pages);

file = si->swap_file;
len = seq_file_path(swap, file, " \t\n\\");
@@ -2996,8 +2996,7 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
}
if (last_page > maxpages) {
pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
- maxpages << (PAGE_SHIFT - 10),
- last_page << (PAGE_SHIFT - 10));
+ PG2KB(maxpages), PG2KB(last_page));
}
if (maxpages > last_page) {
maxpages = last_page + 1;
@@ -3338,8 +3337,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);

pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n",
- p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
- nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
+ PG2KB(p->pages), name->name, p->prio,
+ nr_extents, PG2KB((unsigned long long)span),
(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
(p->flags & SWP_DISCARDABLE) ? "D" : "",
(p->flags & SWP_AREA_DISCARD) ? "s" : "",
diff --git a/mm/util.c b/mm/util.c
index 4ac87f1b30f1..6d301ddfc395 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -848,7 +848,7 @@ unsigned long vm_commit_limit(void)
unsigned long allowed;

if (sysctl_overcommit_kbytes)
- allowed = sysctl_overcommit_kbytes >> (PAGE_SHIFT - 10);
+ allowed = KB2PG(sysctl_overcommit_kbytes);
else
allowed = ((totalram_pages() - hugetlb_total_pages())
* sysctl_overcommit_ratio / 100);
@@ -921,13 +921,13 @@ int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
* Reserve some for root
*/
if (!cap_sys_admin)
- allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
+ allowed -= KB2PG(sysctl_admin_reserve_kbytes);

/*
* Don't let a single process grow so big a user can't recover
*/
if (mm) {
- long reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
+ long reserve = KB2PG(sysctl_user_reserve_kbytes);

allowed -= min_t(long, mm->total_vm / 32, reserve);
}
--
2.33.0

2021-09-19 19:51:25

by Oleksandr Natalenko

[permalink] [raw]
Subject: [RFC v2 PATCH 1/3] mm: add PG2KB/KB2PG helper macros

Signed-off-by: Oleksandr Natalenko <[email protected]>
---
include/linux/mm.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 00bb2d938df4..0a7e950ac8aa 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -98,6 +98,9 @@ extern int mmap_rnd_compat_bits __read_mostly;
#include <asm/page.h>
#include <asm/processor.h>

+#define PG2KB(x) ((x) << (PAGE_SHIFT - 10))
+#define KB2PG(x) ((x) >> (PAGE_SHIFT - 10))
+
/*
* Architectures that support memory tagging (assigning tags to memory regions,
* embedding these tags into addresses that point to these memory regions, and
--
2.33.0