From: Eric Dumazet <eric.dumazet@gmail.com>
To: Hannes Frederic Sowa <hannes@stressinduktion.org>
Cc: netdev@vger.kernel.org, fw@strlen.de, edumazet@google.com,
davem@davemloft.net, ycheng@google.com
Subject: Re: [PATCH RFC] net: introduce support for lazy initialization of secret keys
Date: Wed, 25 Sep 2013 15:42:26 -0700 [thread overview]
Message-ID: <1380148946.3165.166.camel@edumazet-glaptop> (raw)
In-Reply-To: <20130925213403.GF4904@order.stressinduktion.org>
On Wed, 2013-09-25 at 23:34 +0200, Hannes Frederic Sowa wrote:
> net_get_random_once is a new macro which handles the initialization of
> secret keys at use-site. It is possible to call it in the fast path. Only
> the initialization depends on the spinlock and is rather slow. Otherwise
> it should get used just before the key is used to delay the entropy
> extration as late as possible to get better randomness. It returns true
> if the key got initialized.
So you don't like cmpxchg() ;)
> +/* BE CAREFUL: this function is not interrupt safe */
> +#define net_get_random_once(buf, nbytes) \
> + ({ \
> + static DEFINE_SPINLOCK(__lock); \
> + static bool __done = false; \
> + bool __ret = false; \
> + if (unlikely(!__done)) \
> + __ret = __net_get_random_once(buf, \
> + nbytes, \
> + &__done, \
> + &__lock); \
> + __ret; \
> + })
> +
>
No idea why its needed to have one spinlock per call point.
A single lock should be more than enough.
The spinlock could be private to __net_get_random_once()
+bool __net_get_random_once(void *buf, int nbytes, bool *done,
+ spinlock_t *lock)
+{
+ spin_lock_bh(lock);
+ if (*done) {
+ spin_unlock_bh(lock);
+ return false;
+ }
+
+ get_random_bytes(buf, nbytes);
I think you might need a memory barrier here.
(smp_wmb();)
+ *done = true;
+ spin_unlock_bh(lock);
BTW, build_ehash_secret() is called like that :
if (unlikely(!inet_ehash_secret))
if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
build_ehash_secret();
So it would be better to make sure inet_ehash_secret is not 0 by
accident.
next prev parent reply other threads:[~2013-09-25 22:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-25 21:34 [PATCH RFC] net: introduce support for lazy initialization of secret keys Hannes Frederic Sowa
2013-09-25 22:14 ` Florian Westphal
2013-09-25 22:42 ` Eric Dumazet [this message]
2013-09-25 22:58 ` Hannes Frederic Sowa
2013-09-26 2:50 ` Hannes Frederic Sowa
2013-09-26 3:03 ` Hannes Frederic Sowa
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=1380148946.3165.166.camel@edumazet-glaptop \
--to=eric.dumazet@gmail.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=fw@strlen.de \
--cc=hannes@stressinduktion.org \
--cc=netdev@vger.kernel.org \
--cc=ycheng@google.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