[PATCH v2 1/2] util: Use a memoized value for PAGE_SIZE
Vincent Donnefort
vdonnefort at google.com
Fri Nov 27 08:58:41 PST 2026
The PAGE_SIZE macro invokes sysconf on every evaluation. Promote
host_page_size() to util, memoize the sysconf(_SC_PAGE_SIZE) result and
back PAGE_SIZE with it.
Signed-off-by: Vincent Donnefort <vdonnefort at google.com>
---
builtin-run.c | 14 +-------------
include/kvm/kvm.h | 4 ----
include/kvm/util.h | 6 ++++++
util/util.c | 15 +++++++++++++++
4 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/builtin-run.c b/builtin-run.c
index 75e8ced1f421..f06231e382ad 100644
--- a/builtin-run.c
+++ b/builtin-run.c
@@ -401,18 +401,6 @@ static void kernel_usage_with_options(void)
KVM_BINARY_NAME);
}
-static long host_page_size(void)
-{
- long page_size = sysconf(_SC_PAGE_SIZE);
-
- if (page_size < 0) {
- pr_warning("sysconf(_SC_PAGE_SIZE) failed");
- return 0;
- }
-
- return page_size;
-}
-
static long host_ram_nrpages(void)
{
long nr_pages = sysconf(_SC_PHYS_PAGES);
@@ -427,7 +415,7 @@ static long host_ram_nrpages(void)
static u64 host_ram_size(void)
{
- long page_size = host_page_size();
+ unsigned long page_size = host_page_size();
long nr_pages = host_ram_nrpages();
return (u64)nr_pages * page_size;
diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h
index 72a8c3f32b5f..2b7e4b7f4a9f 100644
--- a/include/kvm/kvm.h
+++ b/include/kvm/kvm.h
@@ -23,10 +23,6 @@
#define HOME_DIR getenv("HOME")
#define KVM_BINARY_NAME "lkvm"
-#ifndef PAGE_SIZE
-#define PAGE_SIZE (sysconf(_SC_PAGE_SIZE))
-#endif
-
/*
* We are reusing the existing DEVICE_BUS_MMIO and DEVICE_BUS_IOPORT constants
* from kvm/devices.h to differentiate between registering an I/O port and an
diff --git a/include/kvm/util.h b/include/kvm/util.h
index 0f5a4bba5714..dc0972061108 100644
--- a/include/kvm/util.h
+++ b/include/kvm/util.h
@@ -154,4 +154,10 @@ void *mmap_guest_memfd(struct kvm *kvm, u64 size);
int kvm_create_vcpu_thread(struct kvm *kvm, pthread_t *thread,
void *(*start_routine)(void *), void *arg);
+unsigned long host_page_size(void);
+
+#ifndef PAGE_SIZE
+#define PAGE_SIZE host_page_size()
+#endif
+
#endif /* KVM__UTIL_H */
diff --git a/util/util.c b/util/util.c
index ff888bf9e061..c4ee03304503 100644
--- a/util/util.c
+++ b/util/util.c
@@ -235,3 +235,18 @@ int kvm_create_vcpu_thread(struct kvm *kvm, pthread_t *thread,
return -ret;
}
+
+unsigned long host_page_size(void)
+{
+ static long page_size;
+
+ if (!page_size) {
+ page_size = sysconf(_SC_PAGE_SIZE);
+ if (page_size < 0) {
+ pr_warning("sysconf(_SC_PAGE_SIZE) failed");
+ page_size = 0;
+ }
+ }
+
+ return page_size;
+}
--
2.56.1.839.g22a8290db9-goog
More information about the linux-arm-kernel
mailing list