public inbox for linux-bcachefs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] bcachefs-tools: Use sysinfo(2) directly to implement si_meminfo()
@ 2023-12-09 12:49 Chris Webb
  2023-12-09 12:50 ` [PATCH 2/2] bcachefs-tools: Avoid glibc-specific mallinfo() in shrinker Chris Webb
  0 siblings, 1 reply; 4+ messages in thread
From: Chris Webb @ 2023-12-09 12:49 UTC (permalink / raw)
  To: linux-bcachefs

Use a single sysinfo(2) call to fill out struct sysinfo instead of
multiple libc sysconf(3) requests, which will only make sysinfo(2) calls
internally anyway. This also enables us to access other struct sysinfo
fields, not just the three filled-out previously.

As we provide our own definition of struct sysinfo in include/linux/mm.h
to match the kernel, which is not guaranteed to align with the definition
libc provides in <sys/sysinfo.h>, use syscall(SYS_sysinfo, ...) directly
instead of the libc wrapper.

Signed-off-by: Chris Webb <chris@arachsys.com>
---
 include/linux/mm.h |  8 +++++++-
 linux/shrinker.c   | 15 ---------------
 2 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 4bf80ba..744a14c 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2,6 +2,7 @@
 #ifndef _TOOLS_LINUX_MM_H
 #define _TOOLS_LINUX_MM_H
 
+#include <sys/syscall.h>
 #include <linux/types.h>
 
 struct sysinfo {
@@ -20,6 +21,11 @@ struct sysinfo {
 	__u32 mem_unit;			/* Memory unit size in bytes */
 };
 
-extern void si_meminfo(struct sysinfo * val);
+
+
+static inline void si_meminfo(struct sysinfo *val)
+{
+	BUG_ON(syscall(SYS_sysinfo, val));
+}
 
 #endif /* _TOOLS_LINUX_MM_H */
diff --git a/linux/shrinker.c b/linux/shrinker.c
index 91f633b..7658fb7 100644
--- a/linux/shrinker.c
+++ b/linux/shrinker.c
@@ -33,21 +33,6 @@ void unregister_shrinker(struct shrinker *shrinker)
 	mutex_unlock(&shrinker_lock);
 }
 
-struct meminfo {
-	u64		total;
-	u64		available;
-};
-
-void si_meminfo(struct sysinfo *val)
-{
-	long page_size = sysconf(_SC_PAGESIZE);
-	memset(val, 0, sizeof(*val));
-	val->mem_unit = 1;
-
-	val->totalram = sysconf(_SC_PHYS_PAGES) * page_size;
-	val->freeram  = sysconf(_SC_AVPHYS_PAGES) * page_size;
-}
-
 static void run_shrinkers_allocation_failed(gfp_t gfp_mask)
 {
 	struct shrinker *shrinker;

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-12-10  4:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-09 12:49 [PATCH 1/2] bcachefs-tools: Use sysinfo(2) directly to implement si_meminfo() Chris Webb
2023-12-09 12:50 ` [PATCH 2/2] bcachefs-tools: Avoid glibc-specific mallinfo() in shrinker Chris Webb
2023-12-09 13:10   ` Chris Webb
2023-12-10  4:28   ` Kent Overstreet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox