netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Borkmann <daniel@iogearbox.net>
To: Herbert Xu <herbert@gondor.apana.org.au>,
	David Miller <davem@davemloft.net>
Cc: tgraf@suug.ch, netdev@vger.kernel.org
Subject: Re: rhashtable: Move hash_rnd into bucket_table
Date: Mon, 09 Mar 2015 17:26:52 +0100	[thread overview]
Message-ID: <54FDC9CC.6080709@iogearbox.net> (raw)
In-Reply-To: <20150309095833.GA4028@gondor.apana.org.au>

On 03/09/2015 10:58 AM, Herbert Xu wrote:
> Currently hash_rnd is a parameter that users can set.  However,
> no existing users set this parameter.  It is also something that
> people are unlikely to want to set directly since it's just a
> random number.

I believe the original purpose was to have reproduceability, but
yeah, there are no users of it currently.

> In preparation for allowing the reseeding/rehashing of rhashtable,
> this patch moves hash_rnd into bucket_table so that it's now an
> internal state rather than a parameter.
>
> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
>
> diff --git a/include/linux/rhashtable.h b/include/linux/rhashtable.h
> index d438eeb..5ef8ea5 100644
> --- a/include/linux/rhashtable.h
> +++ b/include/linux/rhashtable.h
> @@ -49,12 +49,14 @@ struct rhash_head {
>   /**
>    * struct bucket_table - Table of hash buckets
>    * @size: Number of hash buckets
> + * @hash_rnd: Random seed to fold into hash
>    * @locks_mask: Mask to apply before accessing locks[]
>    * @locks: Array of spinlocks protecting individual buckets
>    * @buckets: size * hash buckets
>    */
>   struct bucket_table {
>   	size_t			size;
> +	u32			hash_rnd;
>   	unsigned int		locks_mask;
>   	spinlock_t		*locks;
>
> @@ -72,7 +74,6 @@ struct rhashtable;
>    * @key_len: Length of key
>    * @key_offset: Offset of key in struct to be hashed
>    * @head_offset: Offset of rhash_head in struct to be hashed
> - * @hash_rnd: Seed to use while hashing
>    * @max_shift: Maximum number of shifts while expanding
>    * @min_shift: Minimum number of shifts while shrinking
>    * @nulls_base: Base value to generate nulls marker
> @@ -85,7 +86,6 @@ struct rhashtable_params {
>   	size_t			key_len;
>   	size_t			key_offset;
>   	size_t			head_offset;
> -	u32			hash_rnd;
>   	size_t			max_shift;
>   	size_t			min_shift;
>   	u32			nulls_base;
> diff --git a/lib/rhashtable.c b/lib/rhashtable.c
> index b5344ef..4ce267f 100644
> --- a/lib/rhashtable.c
> +++ b/lib/rhashtable.c
> @@ -66,25 +66,28 @@ static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
>   	return hash & (tbl->size - 1);
>   }
>
> -static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
> +static u32 obj_raw_hashfn(struct rhashtable *ht, const void *ptr)
>   {
> +	struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);

const

>   	u32 hash;
>
>   	if (unlikely(!ht->p.key_len))
> -		hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
> +		hash = ht->p.obj_hashfn(ptr, tbl->hash_rnd);
>   	else
>   		hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
> -				    ht->p.hash_rnd);
> +				    tbl->hash_rnd);
>
>   	return hash >> HASH_RESERVED_SPACE;
>   }
>
>   static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len)
>   {
> -	return ht->p.hashfn(key, len, ht->p.hash_rnd) >> HASH_RESERVED_SPACE;
> +	struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
> +

const

> +	return ht->p.hashfn(key, len, tbl->hash_rnd) >> HASH_RESERVED_SPACE;
>   }
>
> -static u32 head_hashfn(const struct rhashtable *ht,
> +static u32 head_hashfn(struct rhashtable *ht,
>   		       const struct bucket_table *tbl,
>   		       const struct rhash_head *he)
>   {
> @@ -92,7 +95,7 @@ static u32 head_hashfn(const struct rhashtable *ht,
>   }
>
>   #ifdef CONFIG_PROVE_LOCKING
> -static void debug_dump_buckets(const struct rhashtable *ht,
> +static void debug_dump_buckets(struct rhashtable *ht,
>   			       const struct bucket_table *tbl)
>   {
>   	struct rhash_head *he;
> @@ -1099,14 +1102,13 @@ int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
>   	if (tbl == NULL)
>   		return -ENOMEM;
>
> +	get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
> +

Have you tested this patch? ;-)

If I see this correctly, the first time you shrink or expand, your hash_rnd
from then onwards will constantly be 0.

You need to copy over the above old hash_rnd to the new one from the newly
allocated tbl. So, bucket_table_alloc() needs a change as well. Of course,
with rehashing, the get_random_bytes() would directly move there.

>   	atomic_set(&ht->nelems, 0);
>   	atomic_set(&ht->shift, ilog2(tbl->size));
>   	RCU_INIT_POINTER(ht->tbl, tbl);
>   	RCU_INIT_POINTER(ht->future_tbl, tbl);
>
> -	if (!ht->p.hash_rnd)
> -		get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
> -
>   	INIT_WORK(&ht->run_work, rht_deferred_worker);
>
>   	return 0;
>

  reply	other threads:[~2015-03-09 16:27 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-31 10:21 rhashtable: Move hash_rnd into bucket_table Herbert Xu
2015-01-31 10:23 ` [RFC] rhashtable: rhashtable_rehash Herbert Xu
2015-02-02 11:16   ` Thomas Graf
2015-03-09 10:13     ` Herbert Xu
2015-01-31 11:17 ` rhashtable: Move hash_rnd into bucket_table Thomas Graf
2015-02-03  3:19 ` David Miller
2015-02-03  3:26   ` David Miller
2015-02-03 20:17     ` Herbert Xu
2015-02-03 20:32       ` [PATCH 0/4] rhashtable: Add iterators and use them Herbert Xu
2015-02-03 20:33         ` [PATCH 1/4] rhashtable: Fix potential crash on destroy in rhashtable_shrink Herbert Xu
2015-02-03 20:33         ` [PATCH 2/4] rhashtable: Introduce rhashtable_walk_* Herbert Xu
2015-02-03 20:33         ` [PATCH 3/4] netlink: Use rhashtable walk iterator Herbert Xu
2015-02-05  0:25           ` Thomas Graf
2015-02-03 20:33         ` [PATCH 4/4] netfilter: " Herbert Xu
2015-02-05  4:35         ` [PATCH 0/4] rhashtable: Add iterators and use them David Miller
2015-03-09  9:58       ` rhashtable: Move hash_rnd into bucket_table Herbert Xu
2015-03-09 16:26         ` Daniel Borkmann [this message]
2015-03-09 22:21           ` Herbert Xu
2015-03-09 19:51         ` David Miller
2015-03-09 19:55           ` David Miller
2015-03-09 22:26         ` [PATCH 0/2] rhashtable: Arbitrary rehashing Herbert Xu
2015-03-09 22:27           ` [PATCH 1/2] rhashtable: Move hash_rnd into bucket_table Herbert Xu
2015-03-10 17:20             ` Thomas Graf
2015-03-10 21:48               ` Herbert Xu
2015-03-09 22:27           ` [PATCH 2/2] rhashtable: Add arbitrary rehash function Herbert Xu
2015-03-10 18:17             ` Thomas Graf
2015-03-10 22:01               ` Herbert Xu
2015-03-10 22:43             ` [PATCH v2] " Herbert Xu
2015-03-11 20:37               ` David Miller
2015-03-11 21:07                 ` Herbert Xu
2015-03-09 22:32           ` [PATCH 0/2] rhashtable: Arbitrary rehashing Josh Triplett
2015-03-10 18:20           ` Thomas Graf
2015-03-12 16:46           ` Thomas Graf
2015-03-13  1:54             ` Herbert Xu
2015-03-13  3:02               ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=54FDC9CC.6080709@iogearbox.net \
    --to=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=netdev@vger.kernel.org \
    --cc=tgraf@suug.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).