Skip to contentSkip to conversation

mm, slab: Make kmalloc_info[] contain all types of names

PATCH Superseded Pengfei Li 2 participants 7 messages Sep 9, 2019 – Sep 10, 2019

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Pengfei Li

author
#0 lore raw

v2 cover mm, slab: Make kmalloc_info[] contain all types of names

Changes in v2
--
1. remove __initconst (patch 1/5)
2. squash patch 2/5
3. add ack tag from Vlastimil Babka

There are three types of kmalloc, KMALLOC_NORMAL, KMALLOC_RECLAIM and KMALLOC_DMA.

The name of KMALLOC_NORMAL is contained in kmalloc_info[].name, but the names of KMALLOC_RECLAIM and KMALLOC_DMA are dynamically generated by kmalloc_cache_name().

Patch1 predefines the names of all types of kmalloc to save the time spent dynamically generating names.

The other 4 patches did some cleanup work.

These changes make sense, and the time spent by new_kmalloc_cache() has been reduced by approximately 36.3%.

Time spent by
new_kmalloc_cache()
5.3-rc7 66264
5.3-rc7+patch 42188

Pengfei Li (4):
mm, slab: Make kmalloc_info[] contain all types of names mm, slab: Remove unused kmalloc_size()
mm, slab_common: Make 'type' is enum kmalloc_cache_type mm, slab_common: Make initializing KMALLOC_DMA start from 1

 include/linux/slab.h |  20 ---------
 mm/slab.c            |   7 +--
 mm/slab.h            |   2 +-
 mm/slab_common.c     | 103 +++++++++++++++++++++++--------------------
 4 files changed, 60 insertions(+), 72 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH v2 1/4 mm, slab: Make kmalloc_info[] contain all types of names

There are three types of kmalloc, KMALLOC_NORMAL, KMALLOC_RECLAIM and KMALLOC_DMA.

The name of KMALLOC_NORMAL is contained in kmalloc_info[].name, but the names of KMALLOC_RECLAIM and KMALLOC_DMA are dynamically generated by kmalloc_cache_name().

This patch predefines the names of all types of kmalloc to save the time spent dynamically generating names.

Besides, remove the kmalloc_cache_name() that is no longer used.

---
 mm/slab.c        |  2 +-
 mm/slab.h        |  2 +-
 mm/slab_common.c | 93 ++++++++++++++++++++++++++----------------------
 3 files changed, 52 insertions(+), 45 deletions(-)
mm/slab.c +1 -1
index 9df370558e5d..c42b6211f42e 100644
@@ -1247,7 +1247,7 @@ void __init kmem_cache_init(void)
 	 * structures first.  Without this, further allocations will bug.
 	 */
 	kmalloc_caches[KMALLOC_NORMAL][INDEX_NODE] = create_kmalloc_cache(
-				kmalloc_info[INDEX_NODE].name,
+				kmalloc_info[INDEX_NODE].name[KMALLOC_NORMAL],
 				kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS,
 				0, kmalloc_size(INDEX_NODE));
 	slab_state = PARTIAL_NODE;
mm/slab.h +1 -1
index 9057b8056b07..2fc8f956906a 100644
@@ -76,7 +76,7 @@ extern struct kmem_cache *kmem_cache;
 
 /* A table of kmalloc cache names and sizes */
 extern const struct kmalloc_info_struct {
-	const char *name;
+	const char *name[NR_KMALLOC_TYPES];
 	unsigned int size;
 } kmalloc_info[];
 
mm/slab_common.c +50 -43

123 lines not on this page · raw message

Signature · 1 line
2.21.0
author
#2 lore raw

PATCH v2 2/4 mm, slab: Remove unused kmalloc_size()

In reply to Pengfei Li

The size of kmalloc can be obtained from kmalloc_info[], so remove kmalloc_size() that will not be used anymore.

---
 include/linux/slab.h | 20 --------------------
 mm/slab.c            |  5 +++--
 mm/slab_common.c     |  5 ++---
 3 files changed, 5 insertions(+), 25 deletions(-)
include/linux/slab.h +0 -20
index 56c9c7eed34e..e773e5764b7b 100644
@@ -557,26 +557,6 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags)
 	return __kmalloc(size, flags);
 }
 
-/*
- * Determine size used for the nth kmalloc cache.
- * return size or 0 if a kmalloc cache for that
- * size does not exist
- */
-static __always_inline unsigned int kmalloc_size(unsigned int n)
-{
-#ifndef CONFIG_SLOB
-	if (n > 2)
-		return 1U << n;
-
-	if (n == 1 && KMALLOC_MIN_SIZE <= 32)
-		return 96;
-
-	if (n == 2 && KMALLOC_MIN_SIZE <= 64)
-		return 192;
-#endif
-	return 0;
-}
-
 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
 {
 #ifndef CONFIG_SLOB
mm/slab.c +3 -2
index c42b6211f42e..7bc4e90e1147 100644
@@ -1248,8 +1248,9 @@ void __init kmem_cache_init(void)
 	 */
 	kmalloc_caches[KMALLOC_NORMAL][INDEX_NODE] = create_kmalloc_cache(
 				kmalloc_info[INDEX_NODE].name[KMALLOC_NORMAL],
-				kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS,
-				0, kmalloc_size(INDEX_NODE));
+				kmalloc_info[INDEX_NODE].size,
+				ARCH_KMALLOC_FLAGS, 0,
+				kmalloc_info[INDEX_NODE].size);
 	slab_state = PARTIAL_NODE;
 	setup_kmalloc_cache_index_table();
 
mm/slab_common.c +2 -3
index 61c1e2e54263..cae27210e4c3 100644
@@ -1239,11 +1239,10 @@ void __init create_kmalloc_caches(slab_flags_t flags)
 		struct kmem_cache *s = kmalloc_caches[KMALLOC_NORMAL][i];
 
 		if (s) {
-			unsigned int size = kmalloc_size(i);
-
 			kmalloc_caches[KMALLOC_DMA][i] = create_kmalloc_cache(
 				kmalloc_info[i].name[KMALLOC_DMA],
-				size, SLAB_CACHE_DMA | flags, 0, 0);
+				kmalloc_info[i].size,
+				SLAB_CACHE_DMA | flags, 0, 0);
 		}
 	}
 #endif
Signature · 1 line
2.21.0
author
#3 lore raw

PATCH v2 3/4 mm, slab_common: Make 'type' is enum kmalloc_cache_type

In reply to Pengfei Li

The 'type' of the function new_kmalloc_cache should be enum kmalloc_cache_type instead of int, so correct it.

---
 mm/slab_common.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/slab_common.c +3 -2
index cae27210e4c3..d64a64660f86 100644
@@ -1192,7 +1192,7 @@ void __init setup_kmalloc_cache_index_table(void)
 }
 
 static void __init
-new_kmalloc_cache(int idx, int type, slab_flags_t flags)
+new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags)
 {
 	if (type == KMALLOC_RECLAIM)
 		flags |= SLAB_RECLAIM_ACCOUNT;
@@ -1210,7 +1210,8 @@ new_kmalloc_cache(int idx, int type, slab_flags_t flags)
  */
 void __init create_kmalloc_caches(slab_flags_t flags)
 {
-	int i, type;
+	int i;
+	enum kmalloc_cache_type type;
 
 	for (type = KMALLOC_NORMAL; type <= KMALLOC_RECLAIM; type++) {
 		for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
Signature · 1 line
2.21.0
Acked-by on v2 3/4
#4 lore raw

Re: [PATCH v2 3/4] mm, slab_common: Make 'type' is enum kmalloc_cache_type

On Tue, Sep 10, 2019 at 01:07:14AM +0800, Pengfei Li wrote:

Hi Pengfei!

Quoted 2 lines

The 'type' of the function new_kmalloc_cache should be enum kmalloc_cache_type instead of int, so correct it.

I think you mean type of the 'i' variable, not the type of new_kmalloc_cache() function. Also the name of the patch is misleading. How about
mm, slab_common: use enum kmalloc_cache_type to iterate over kmalloc caches ? Or something like this.

The rest of the series looks good to me.

Please, feel free to use

for patches [1-3] in the series after fixing this commit message and restoring __initconst.

Patch [4] needs some additional clarifications, IMO.

Thank you!

Quoted 37 lines
---
 mm/slab_common.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/slab_common.c +3 -2
index cae27210e4c3..d64a64660f86 100644
@@ -1192,7 +1192,7 @@ void __init setup_kmalloc_cache_index_table(void)
 }
 
 static void __init
-new_kmalloc_cache(int idx, int type, slab_flags_t flags)
+new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags)
 {
 	if (type == KMALLOC_RECLAIM)
 		flags |= SLAB_RECLAIM_ACCOUNT;
@@ -1210,7 +1210,8 @@ new_kmalloc_cache(int idx, int type, slab_flags_t flags)
  */
 void __init create_kmalloc_caches(slab_flags_t flags)
 {
-	int i, type;
+	int i;
+	enum kmalloc_cache_type type;
 
 	for (type = KMALLOC_NORMAL; type <= KMALLOC_RECLAIM; type++) {
 		for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
Signature · 1 line
2.21.0
author
#5 lore raw

Re: [PATCH v2 3/4] mm, slab_common: Make 'type' is enum kmalloc_cache_type

Quoted 5 lines · Roman Gushchin

On Tue, Sep 10, 2019 at 01:07:14AM +0800, Pengfei Li wrote:

Hi Pengfei!

Quoted 9 lines
Quoted 2 lines

The 'type' of the function new_kmalloc_cache should be enum kmalloc_cache_type instead of int, so correct it.

I think you mean type of the 'i' variable, not the type of new_kmalloc_cache() function. Also the name of the patch is misleading. How about
mm, slab_common: use enum kmalloc_cache_type to iterate over kmalloc caches ? Or something like this.

Ok, this name is really better :)

Quoted 7 lines

The rest of the series looks good to me.

Please, feel free to use

for patches [1-3] in the series after fixing this commit message and restoring __initconst.

Thanks!

Quoted 2 lines

Patch [4] needs some additional clarifications, IMO.

I will add more clarification in v3.

Quoted 39 lines

Thank you!

Quoted 37 lines
---
 mm/slab_common.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/slab_common.c +3 -3
index cae27210e4c3..d64a64660f86 100644
@@ -1192,7 +1192,7 @@ void __init setup_kmalloc_cache_index_table(void)
 }
 
 static void __init
-new_kmalloc_cache(int idx, int type, slab_flags_t flags)
+new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags)
 {
      if (type == KMALLOC_RECLAIM)
              flags |= SLAB_RECLAIM_ACCOUNT;
@@ -1210,7 +1210,8 @@ new_kmalloc_cache(int idx, int type, slab_flags_t flags)
  */
 void __init create_kmalloc_caches(slab_flags_t flags)
 {
-     int i, type;
+     int i;
+     enum kmalloc_cache_type type;
 
      for (type = KMALLOC_NORMAL; type <= KMALLOC_RECLAIM; type++) {
              for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
--

2.21.0

author
#6 lore raw

PATCH v2 4/4 mm, slab_common: Make initializing KMALLOC_DMA start from 1

In reply to Pengfei Li

kmalloc_caches[KMALLOC_NORMAL][0] will never be initialized, so the loop should start at 1 instead of 0

---
 mm/slab_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slab_common.c +1 -1
index d64a64660f86..6b3e526934d9 100644
@@ -1236,7 +1236,7 @@ void __init create_kmalloc_caches(slab_flags_t flags)
 	slab_state = UP;
 
 #ifdef CONFIG_ZONE_DMA
-	for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
+	for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
 		struct kmem_cache *s = kmalloc_caches[KMALLOC_NORMAL][i];
 
 		if (s) {
Signature · 1 line
2.21.0