Blame SOURCES/0001-Fix-for-kmem-s-S-option-on-Linux-5.7-and-later-kerne.patch

3e5f3d
From 647a5c33e1c94054d7b63168cd6c12901591cb77 Mon Sep 17 00:00:00 2001
3e5f3d
From: Lianbo Jiang <lijiang@redhat.com>
3e5f3d
Date: Thu, 27 May 2021 18:02:11 +0800
3e5f3d
Subject: [PATCH] Fix for "kmem -s|-S" option on Linux 5.7 and later kernels
3e5f3d
3e5f3d
Linux 5.7 and later kernels that contain kernel commit 1ad53d9fa3f6
3e5f3d
("slub: improve bit diffusion for freelist ptr obfuscation") changed
3e5f3d
the calculation formula in the freelist_ptr(), which added a swab()
3e5f3d
call to mix bits a little more.  When kernel is configured with the
3e5f3d
"CONFIG_SLAB_FREELIST_HARDENED=y", without the patch, the "kmem -s|-S"
3e5f3d
options display wrong statistics and state whether slab objects are
3e5f3d
in use or free and can print the following errors:
3e5f3d
3e5f3d
  crash> kmem -s
3e5f3d
  CACHE             OBJSIZE  ALLOCATED     TOTAL  SLABS  SSIZE  NAME
3e5f3d
  87201e00              528          0         0      0     8k  xfs_dqtrx
3e5f3d
  87201f00              496          0         0      0     8k  xfs_dquot
3e5f3d
  kmem: xfs_buf: slab: 37202e6e900 invalid freepointer: b844bab900001d70
3e5f3d
  kmem: xfs_buf: slab: 3720250fd80 invalid freepointer: b8603f9400001370
3e5f3d
  ...
3e5f3d
3e5f3d
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
3e5f3d
---
3e5f3d
 memory.c | 9 +++++++--
3e5f3d
 1 file changed, 7 insertions(+), 2 deletions(-)
3e5f3d
3e5f3d
diff --git a/memory.c b/memory.c
3e5f3d
index 8c6bbe409922..a3cf8a86728d 100644
3e5f3d
--- a/memory.c
3e5f3d
+++ b/memory.c
3e5f3d
@@ -20,6 +20,7 @@
3e5f3d
 #include <sys/mman.h>
3e5f3d
 #include <ctype.h>
3e5f3d
 #include <netinet/in.h>
3e5f3d
+#include <byteswap.h>
3e5f3d
 
3e5f3d
 struct meminfo {           /* general purpose memory information structure */
3e5f3d
         ulong cache;       /* used by the various memory searching/dumping */
3e5f3d
@@ -19336,10 +19337,14 @@ count_free_objects(struct meminfo *si, ulong freelist)
3e5f3d
 static ulong
3e5f3d
 freelist_ptr(struct meminfo *si, ulong ptr, ulong ptr_addr)
3e5f3d
 {
3e5f3d
-	if (VALID_MEMBER(kmem_cache_random))
3e5f3d
+	if (VALID_MEMBER(kmem_cache_random)) {
3e5f3d
 		/* CONFIG_SLAB_FREELIST_HARDENED */
3e5f3d
+
3e5f3d
+		if (THIS_KERNEL_VERSION >= LINUX(5,7,0))
3e5f3d
+			ptr_addr = (sizeof(long) == 8) ? bswap_64(ptr_addr)
3e5f3d
+						       : bswap_32(ptr_addr);
3e5f3d
 		return (ptr ^ si->random ^ ptr_addr);
3e5f3d
-	else
3e5f3d
+	} else
3e5f3d
 		return ptr;
3e5f3d
 }
3e5f3d
 
3e5f3d
-- 
3e5f3d
2.30.2
3e5f3d