From: timur@codeaurora.org (Timur Tabi)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 1/2] hwrng: msm: add a spinlock and support for blocking reads
Date: Fri, 22 Jun 2018 08:11:07 -0500 [thread overview]
Message-ID: <33df2915-ae2e-3cbd-fa84-b3d83bcc82b6@codeaurora.org> (raw)
In-Reply-To: <152964579304.16708.2651020848216712299@swboyd.mtv.corp.google.com>
On 6/22/18 12:36 AM, Stephen Boyd wrote:
> Quoting Timur Tabi (2018-06-21 08:17:55)
>> @@ -96,11 +110,39 @@ static int msm_rng_read(struct hwrng *hwrng, void *data, size_t max, bool wait)
>>
>> /* read random data from hardware */
>> do {
>> - val = readl_relaxed(rng->base + PRNG_STATUS);
>> - if (!(val & PRNG_STATUS_DATA_AVAIL))
>> - break;
>> + spin_lock(&rng->lock);
>> +
>> + /*
>> + * First check the status bit. If 'wait' is true, then wait
>> + * up to TIMEOUT microseconds for data to be available.
>> + */
>> + if (wait) {
>> + int ret;
> Please don't do local variables like this. Put them at the top of the
> function.
Ok.
>
>> +
>> + ret = readl_poll_timeout_atomic(rng->base + PRNG_STATUS,
>> + val, val & PRNG_STATUS_DATA_AVAIL, 0, TIMEOUT);
>> + if (ret) {
>> + /* Timed out */
>> + spin_unlock(&rng->lock);
>> + break;
>> + }
>> + } else {
>> + val = readl_relaxed(rng->base + PRNG_STATUS);
>> + if (!(val & PRNG_STATUS_DATA_AVAIL)) {
>> + spin_unlock(&rng->lock);
>> + break;
>> + }
>> + }
>>
>> val = readl_relaxed(rng->base + PRNG_DATA_OUT);
>> + spin_unlock(&rng->lock);
> Maybe this should be written as:
>
> spin_lock()
> if (wait) {
> has_data = readl_poll_timeout_atomic(...) == 0;
> } else {
> val = readl_relaxed(rng->base + PRNG_STATUS);
> has_data = val & PRNG_STATUS_DATA_AVAIL;
> }
>
> if (has_data)
> val = readl_relaxed(rng->base + PRNG_DATA_OUT);
> spin_unlock();
>
> if (!has_data)
> break;
That would work, but I don't really see this as better, just different.
>> + /*
>> + * Zero is technically a valid random number, but it's also
>> + * the value returned if the PRNG is not enabled properly.
>> + * To avoid accidentally returning all zeros, treat it as
>> + * invalid and just return what we've already read.
>> + */
>> if (!val)
>> break;
> Is this a related change? Looks like a nice comment that isn't related
> to locking.
It's slightly related in that the locking is needed to reduce the number
of times we read zero from the DATA_OUT register.
--
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc. Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.
next prev parent reply other threads:[~2018-06-22 13:11 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-21 15:17 [PATCH 1/2] hwrng: msm: add a spinlock and support for blocking reads Timur Tabi
2018-06-21 15:17 ` [PATCH 2/2] hwrng: msm: add ACPI support Timur Tabi
2018-06-22 4:23 ` Vinod
2018-06-22 4:26 ` Timur Tabi
2018-06-22 4:44 ` Vinod
2018-06-22 4:46 ` Timur Tabi
2018-06-22 4:48 ` Vinod
2018-06-22 4:17 ` [PATCH 1/2] hwrng: msm: add a spinlock and support for blocking reads Vinod
2018-06-22 4:18 ` Timur Tabi
2018-06-22 4:24 ` Vinod
2018-06-22 4:28 ` Timur Tabi
2018-06-22 5:36 ` Stephen Boyd
2018-06-22 13:11 ` Timur Tabi [this message]
2018-06-22 15:38 ` Stanimir Varbanov
2018-06-22 15:41 ` Timur Tabi
2018-06-22 17:51 ` Stephen Boyd
2018-06-22 18:03 ` Timur Tabi
2018-06-22 21:17 ` Timur Tabi
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=33df2915-ae2e-3cbd-fa84-b3d83bcc82b6@codeaurora.org \
--to=timur@codeaurora.org \
--cc=linux-arm-kernel@lists.infradead.org \
/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).