Re: [PATCHv2] hw_random: add timeriomem-rng driver

From: Herbert Xu
Date: Wed Feb 11 2009 - 08:27:36 EST


On Tue, Feb 10, 2009 at 07:39:04PM +0000, Alexander Clouter wrote:
>
> My original code[1] approach, with timer_pending() in there, would have
> been okay then, right?
> ----
> if (timer_pending(&timeriomem_rng_timer)) {
> if (!wait)
> return 0;
>
> del_timer_sync(&timeriomem_rng_timer);
> delay = timeriomem_rng_timer.expires - jiffies;
>
> schedule_timeout_uninterruptible(delay);
> }

Yes probably but it is racy. Also it's better if we can avoid
the expires - jiffies calculation if only because then we won't
have to tell Andrew why it can't be negative :)

> > Instead of deleting the timer above, why not create a rng->present
> > variable which you set to zero here, and the timer sets it to
> > non-zero. Then you just need to return rng->present in your
> > data_present function.
>
> Sounds fair, better than the timer_pending approach and easier to
> understand.

Oh I forgot about the blocking case. We can probably add a
completion object to handle that. So something like

if (!wait || rng->present)
return rng->present;

wait_for_completion(&rng->completion);
return 1;

The timer would do

rng->present = 1;
complete(&rng->completion);

And the reader would do

rng->present = 0;
INIT_COMPLETION(rng->completion);
add_timer(...)

Note that everything but the timer is completely serialised by
the hwrng mutex so we only need to worry about racing against
the timer.

Cheers,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@xxxxxxxxxxxxxxxxxxx>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/