diff mbox

[net-next,v2] rhashtable: Warn if min_size or max_size are not a power of two

Message ID 20150319194608.GA27962@casper.infradead.org
State Superseded, archived
Delegated to: David Miller
Headers show

Commit Message

Thomas Graf March 19, 2015, 7:46 p.m. UTC
The current code correctly limits table size to the next power of two.
This check is solely to catch programming errors.

Signed-off-by: Thomas Graf <tgraf@suug.ch>
---
v2: use is_power_of_2() instead of roundup_pow_of_two()

 lib/rhashtable.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Herbert Xu March 19, 2015, 9:02 p.m. UTC | #1
On Thu, Mar 19, 2015 at 07:46:08PM +0000, Thomas Graf wrote:
> The current code correctly limits table size to the next power of two.
> This check is solely to catch programming errors.
> 
> Signed-off-by: Thomas Graf <tgraf@suug.ch>

I don't see the point of this.  A maximum size of 3 says that
the table size should never exceed 3 which makes perfect sense.
And our current code will respect that.

So why force it to be a power of 2 just because our table sizes
happen to be powers of 2?

Cheers,
Thomas Graf March 19, 2015, 9:15 p.m. UTC | #2
On 03/20/15 at 08:02am, Herbert Xu wrote:
> On Thu, Mar 19, 2015 at 07:46:08PM +0000, Thomas Graf wrote:
> > The current code correctly limits table size to the next power of two.
> > This check is solely to catch programming errors.
> > 
> > Signed-off-by: Thomas Graf <tgraf@suug.ch>
> 
> I don't see the point of this.  A maximum size of 3 says that
> the table size should never exceed 3 which makes perfect sense.
> And our current code will respect that.
> 
> So why force it to be a power of 2 just because our table sizes
> happen to be powers of 2?

rht_grow_above_75() checks the old table size:

(!ht->p.max_size || tbl->size < ht->p.max_size);

If you specify max_size = 3, it grows the table to tbl->size = 4.
This can be avoided if max_size is a power of two.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Herbert Xu March 19, 2015, 9:49 p.m. UTC | #3
On Thu, Mar 19, 2015 at 09:15:46PM +0000, Thomas Graf wrote:
>
> rht_grow_above_75() checks the old table size:
> 
> (!ht->p.max_size || tbl->size < ht->p.max_size);
> 
> If you specify max_size = 3, it grows the table to tbl->size = 4.
> This can be avoided if max_size is a power of two.

OK let's fix the test then.  The easiest way would be to round
max_size up to the next power of 2.

Cheers,
Herbert Xu March 19, 2015, 10:02 p.m. UTC | #4
On Fri, Mar 20, 2015 at 08:49:33AM +1100, Herbert Xu wrote:
>
> OK let's fix the test then.  The easiest way would be to round
> max_size up to the next power of 2.

s/up/down/
David Laight March 20, 2015, 10:57 a.m. UTC | #5
From: Herbert Xu
> Sent: 19 March 2015 21:50
> On Thu, Mar 19, 2015 at 09:15:46PM +0000, Thomas Graf wrote:
> >
> > rht_grow_above_75() checks the old table size:
> >
> > (!ht->p.max_size || tbl->size < ht->p.max_size);
> >
> > If you specify max_size = 3, it grows the table to tbl->size = 4.
> > This can be avoided if max_size is a power of two.
> 
> OK let's fix the test then.  The easiest way would be to round
> max_size up to the next power of 2.

Isn't that round-up implicit in the test above, no reason to
change the user-supplied limit.

To round down just test tbl->size * 2 <= ht->p.max_size.

Maybe just document the fact that it will grow if the current size is
less than the requested maximum.

	David

--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 5f8fe3e..5474507 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -933,6 +933,9 @@  int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
 	if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
 		return -EINVAL;
 
+	WARN_ON(params->min_size && !is_power_of_2(params->min_size));
+	WARN_ON(params->max_size && !is_power_of_2(params->max_size));
+
 	params->min_size = max(params->min_size, HASH_MIN_SIZE);
 
 	if (params->nelem_hint)