From: Patrick McHardy <kaber@trash.net>
To: Josh Triplett <josh@joshtriplett.org>
Cc: pablo@netfilter.org, netfilter-devel@vger.kernel.org,
paulmck@linux.vnet.ibm.com
Subject: Re: [PATCH] netfilter: nft_hash: bug fixes and resizing
Date: Thu, 27 Feb 2014 16:06:15 +0000 [thread overview]
Message-ID: <20140227160613.GA1399@macbook.localnet> (raw)
In-Reply-To: <20140227154746.GA26756@thin>
On Thu, Feb 27, 2014 at 07:47:46AM -0800, Josh Triplett wrote:
> On Thu, Feb 27, 2014 at 02:56:20PM +0000, Patrick McHardy wrote:
> >
> > Expansion is triggered when the load factors exceeds 75%, shrinking when
> > the load factor goes below 30%. Both operations are allowed to fail and
> > will be retried on the next insertion or removal if their respective
> > conditions still hold.
>
> Since this hash table uses chaining, does it really make sense to expand
> at 75% load? You might just want to expand at 100%, which is even
> easier to check for. But that seems like a question for benchmarks to
> answer.
Yeah, but the probability of collisions increase greatly as we approach 100%.
It can easily be changed of course.
> This looks correct to me. Thank you very much for this work!
Thanks a lot for your review.
> One suggestion and one minor micro-optimization (unrelated to the
> algorithm implementation) below. With those changed:
> Reviewed-by: Josh Triplett <josh@joshtriplett.org>
>
> > + nft_hash_for_each_entry(he, he->next) {
> > + if (nft_hash_data(&he->key, ntbl->size, set->klen) != h)
> > + continue;
> > + next = he;
> > + break;
> > + }
>
> > + nft_hash_for_each_entry(he, tbl->buckets[h]) {
> > + if (nft_hash_data(&he->key, ntbl->size, set->klen) != i)
> > + continue;
> > + RCU_INIT_POINTER(ntbl->buckets[i], he);
> > + break;
> > + }
>
> In both of these cases, you could reverse the sense of the if with
> s/!=/==/, move the "statement; break;" into the if, and the continue
> would become redundant. (You then wouldn't even need the braces around
> the loop body.)
Yeah, but if possible I always try to write inner loops in the style of
"filter out everything uninteresting", "do something with the rest".
The main reason is do avoid deep indentation. I guess it doesn't matter
since for the compiler these should be equivalent.
> Second, in the load-factor calculation:
>
> > + /* Expand table when exceeding 75% load */
> > + if (tbl->elements * 4 / 3 > tbl->size)
> > + nft_hash_tbl_expand(set, priv);
>
> I just checked, and GCC ends up using an imul to implement this, due to
> the division by 3. I'd suggest rewriting it to:
>
> if (tbl->elements > (tbl->size >> 2) * 3)
>
> Dividing tbl->size by 4 first avoids the possibility of integer
> overflow, and GCC translates the *3 into a single lea instruction.
Thanks, I'll change that.
> (Also, do you need an NFT_HASH_MAX_SIZE here?)
>
> Similar considerations apply to the calculation for shrinking.
I think we should be fine without a maximum. The size calculation for
memory allocation is done using 64 bits, so it won't overflow. If the
size gets too big, memory allocation will simply fail and we'll keep
the old table. However adding a vmalloc fallback should probably be
done, it will get into sizes where kmalloc might frequently fail quite
quickly.
Thanks again!
next prev parent reply other threads:[~2014-02-27 16:06 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-27 14:56 [PATCH] netfilter: hash resizing Patrick McHardy
2014-02-27 14:56 ` [PATCH] netfilter: nft_hash: bug fixes and resizing Patrick McHardy
2014-02-27 15:47 ` Josh Triplett
2014-02-27 16:06 ` Patrick McHardy [this message]
2014-02-27 19:47 ` Paul E. McKenney
2014-02-28 11:28 ` Patrick McHardy
2014-02-28 16:09 ` Paul E. McKenney
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=20140227160613.GA1399@macbook.localnet \
--to=kaber@trash.net \
--cc=josh@joshtriplett.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
--cc=paulmck@linux.vnet.ibm.com \
/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).