[PATCH v2 1/4] mm/slub: use a track count for user metadata sizing

Pengpeng Hou <[email protected]>
Newsgroups org.kernel.vger.linux-doc,org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <[email protected]>
SLAB_STORE_USER metadata consists of allocation and free tracks, but the
object-layout calculations repeat the literal count at each offset site.

Add TRACK_NR to the existing track-item enum and use it for metadata
initialization, original-size placement, object-extension offsets, padding
checks and cache sizing.  This is a behavior-preserving preparation for
extending the tracked history.

Assisted-by: Codex:gpt-5
Signed-off-by: Pengpeng Hou <[email protected]>
---
 mm/slub.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 0337e60db5ac..0653def0fe36 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -329,7 +329,7 @@ struct track {
 	unsigned long when;	/* When did the operation occur */
 };
 
-enum track_item { TRACK_ALLOC, TRACK_FREE };
+enum track_item { TRACK_ALLOC, TRACK_FREE, TRACK_NR };
 
 #ifdef SLAB_SUPPORTS_SYSFS
 static int sysfs_slab_add(struct kmem_cache *);
@@ -753,7 +753,7 @@ static inline void set_orig_size(struct kmem_cache *s,
 		return;
 
 	p += get_info_end(s);
-	p += sizeof(struct track) * 2;
+	p += sizeof(struct track) * TRACK_NR;
 
 	*(unsigned long *)p = orig_size;
 }
@@ -769,7 +769,7 @@ static inline unsigned long get_orig_size(struct kmem_cache *s, void *object)
 		return s->object_size;
 
 	p += get_info_end(s);
-	p += sizeof(struct track) * 2;
+	p += sizeof(struct track) * TRACK_NR;
 
 	return *(unsigned long *)p;
 }
@@ -887,7 +887,7 @@ static unsigned int obj_exts_offset_in_object(struct kmem_cache *s)
 	unsigned int offset = get_info_end(s);
 
 	if (kmem_cache_debug_flags(s, SLAB_STORE_USER))
-		offset += sizeof(struct track) * 2;
+		offset += sizeof(struct track) * TRACK_NR;
 
 	if (slub_debug_orig_size(s))
 		offset += sizeof(unsigned long);
@@ -1090,7 +1090,7 @@ static void init_tracking(struct kmem_cache *s, void *object)
 		return;
 
 	p = get_track(s, object, TRACK_ALLOC);
-	memset(p, 0, 2*sizeof(struct track));
+	memset(p, 0, sizeof(struct track) * TRACK_NR);
 }
 
 static void print_track(const char *s, struct track *t, unsigned long pr_time)
@@ -1199,7 +1199,7 @@ static void print_trailer(struct kmem_cache *s, struct slab *slab, u8 *p)
 	off = get_info_end(s);
 
 	if (s->flags & SLAB_STORE_USER)
-		off += 2 * sizeof(struct track);
+		off += sizeof(struct track) * TRACK_NR;
 
 	if (slub_debug_orig_size(s))
 		off += sizeof(unsigned long);
@@ -1403,7 +1403,7 @@ static int check_pad_bytes(struct kmem_cache *s, struct slab *slab, u8 *p)
 
 	if (s->flags & SLAB_STORE_USER) {
 		/* We also have user information there */
-		off += 2 * sizeof(struct track);
+		off += sizeof(struct track) * TRACK_NR;
 
 		if (s->flags & SLAB_KMALLOC)
 			off += sizeof(unsigned long);
@@ -7895,7 +7895,7 @@ static int calculate_sizes(struct kmem_cache_args *args, struct kmem_cache *s)
 		 * Need to store information about allocs and frees after
 		 * the object.
 		 */
-		size += 2 * sizeof(struct track);
+		size += sizeof(struct track) * TRACK_NR;
 
 		/* Save the original kmalloc request size */
 		if (flags & SLAB_KMALLOC)
-- 
2.50.1 (Apple Git-155)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.