Skip to contentSkip to conversation

lib/rhashtable: consider param->min_size when setting initial table size

PATCH Superseded Davidlohr Bueso 4 participants 6 messages Jul 14, 2018 – Jul 18, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/rhashtable: consider param->min_size when setting initial table size

rhashtable_init() currently does not take into account the user-passed min_size parameter unless param->nelem_hint is set as well. As such, the default size (number of buckets) will always be HASH_DEFAULT_SIZE even if the smallest allowed size is larger than that. Remediate this by unconditionally calling into rounded_hashtable_size() and handling things accordingly.

---
 lib/rhashtable.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
lib/rhashtable.c +11 -6
index 411c4041ce83..89c3cfc8334d 100644
@@ -962,8 +962,16 @@ EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
 
 static size_t rounded_hashtable_size(const struct rhashtable_params *params)
 {
-	return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
-		   (unsigned long)params->min_size);
+	size_t retsize;
+
+	if (params->nelem_hint)
+		retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
+			      (unsigned long)params->min_size);
+	else
+		retsize = max(HASH_DEFAULT_SIZE,
+			      (unsigned long)params->min_size);
+
+	return retsize;
 }
 
 static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
@@ -1020,8 +1028,6 @@ int rhashtable_init(struct rhashtable *ht,
 	struct bucket_table *tbl;
 	size_t size;
 
-	size = HASH_DEFAULT_SIZE;
-
 	if ((!params->key_len && !params->obj_hashfn) ||
 	    (params->obj_hashfn && !params->obj_cmpfn))
 		return -EINVAL;
@@ -1048,8 +1054,7 @@ int rhashtable_init(struct rhashtable *ht,
 
 	ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
 
-	if (params->nelem_hint)
-		size = rounded_hashtable_size(&ht->p);
+	size = rounded_hashtable_size(&ht->p);
 
 	if (params->locks_mul)
 		ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
Signature · 1 line
2.16.4
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Davidlohr Bueso

rhashtable_init() currently does not take into account the user-passed min_size parameter unless param->nelem_hint is set as well. As such, the default size (number of buckets) will always be HASH_DEFAULT_SIZE even if the smallest allowed size is larger than that. Remediate this by unconditionally calling into rounded_hashtable_size() and handling things accordingly.

Normally these patches go through netdev so could you please resend it with my ack to [email protected]?

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw

PATCH RESEND 1/1 lib/rhashtable: consider param->min_size when setting initial table size

rhashtable_init() currently does not take into account the user-passed min_size parameter unless param->nelem_hint is set as well. As such, the default size (number of buckets) will always be HASH_DEFAULT_SIZE even if the smallest allowed size is larger than that. Remediate this by unconditionally calling into rounded_hashtable_size() and handling things accordingly.

---
Resending per Herbert's request with Cc to netdev.

lib/rhashtable.c | 17 +++++++++++------

1 file changed, 11 insertions(+), 6 deletions(-)
lib/rhashtable.c +0 -0
index 62d3a976614e..310e29b51507 100644
@@ -953,8 +953,16 @@ EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
 

static size_t rounded_hashtable_size(const struct rhashtable_params *params) {
- return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
- (unsigned long)params->min_size);
+ size_t retsize;
+
+ if (params->nelem_hint)
+ retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
+ (unsigned long)params->min_size);
+ else
+ retsize = max(HASH_DEFAULT_SIZE,
+ (unsigned long)params->min_size);
+
+ return retsize;
}

static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) @@ -1010,8 +1018,6 @@ int rhashtable_init(struct rhashtable *ht,

	struct bucket_table *tbl;
	size_t size;

- size = HASH_DEFAULT_SIZE;
-

	if ((!params->key_len && !params->obj_hashfn) ||
	    (params->obj_hashfn && !params->obj_cmpfn))
		return -EINVAL;

@@ -1035,8 +1041,7 @@ int rhashtable_init(struct rhashtable *ht,

	ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);

- if (params->nelem_hint)
- size = rounded_hashtable_size(&ht->p);
+ size = rounded_hashtable_size(&ht->p);

	if (params->locks_mul)
		ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
Signature · 1 line
2.16.4
#4 lore raw
In reply to Herbert Xu
Quoted 8 lines · Herbert Xu
Quoted 7 lines · Davidlohr Bueso

rhashtable_init() currently does not take into account the user-passed min_size parameter unless param->nelem_hint is set as well. As such, the default size (number of buckets) will always be HASH_DEFAULT_SIZE even if the smallest allowed size is larger than that. Remediate this by unconditionally calling into rounded_hashtable_size() and handling things accordingly.

Quoted 6 lines
Quoted 1 line

Normally these patches go through netdev so could you please resend it with my ack to [email protected]?

So I've done the resend, but at least would think that routing the patch through Andrew would work best as he picked up the rhashtable changes regarding ipc and this touches the same call.

Thanks,
Davidlohr

maintainer Reviewed-by
#5 lore raw
Quoted 12 lines · On Tue, 17 Jul 2018 15:30:57 -0700 Davidlohr Bueso <[email protected]> wrote:
Quoted 10 lines · Herbert Xu
Quoted 8 lines · Davidlohr Bueso

rhashtable_init() currently does not take into account the user-passed min_size parameter unless param->nelem_hint is set as well. As such, the default size (number of buckets) will always be HASH_DEFAULT_SIZE even if the smallest allowed size is larger than that. Remediate this by unconditionally calling into rounded_hashtable_size() and handling things accordingly.

Quoted 9 lines
Quoted 4 lines

Normally these patches go through netdev so could you please resend it with my ack to [email protected]?

So I've done the resend, but at least would think that routing the patch through Andrew would work best as he picked up the rhashtable changes regarding ipc and this touches the same call.

Either approach works.

maintainer
#3 lore raw
In reply to Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>
Date: Mon, 16 Jul 2018 13:26:13 -0700

Quoted 11 lines

rhashtable_init() currently does not take into account the user-passed min_size parameter unless param->nelem_hint is set as well. As such, the default size (number of buckets) will always be HASH_DEFAULT_SIZE even if the smallest allowed size is larger than that. Remediate this by unconditionally calling into rounded_hashtable_size() and handling things accordingly.

---
Resending per Herbert's request with Cc to netdev.

Applied and queued up for -stable, thanks.