Skip to contentSkip to conversation

hwrng: optee: handle unlimited data rates

PATCH Superseded Jorge Ramirez-Ortiz 2 participants 14 messages Jul 23, 2020 – Aug 6, 2020

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 hwrng: optee: handle unlimited data rates

Data rates of MAX_UINT32 will schedule an unnecessary one jiffy timeout on the call to msleep. Avoid this scenario by using 0 as the unlimited data rate.

---
 drivers/char/hw_random/optee-rng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 49b2e02537dd..5bc4700c4dae 100644
@@ -128,7 +128,7 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
 		data += rng_size;
 		read += rng_size;
 
-		if (wait) {
+		if (wait && pvt_data->data_rate) {
 			if (timeout-- == 0)
 				return read;
 			msleep((1000 * (max - read)) / pvt_data->data_rate);
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 2/2 hwrng: optee: fix wait use case

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +2 -2
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
 	if (max > MAX_ENTROPY_REQ_SZ)
 		max = MAX_ENTROPY_REQ_SZ;
 
-	while (read == 0) {
+	while (read < max) {
 		rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
 		data += rng_size;
 		read += rng_size;
 
 		if (wait && pvt_data->data_rate) {
-			if (timeout-- == 0)
+			if ((timeout-- == 0) || (read == max))
 				return read;
 			msleep((1000 * (max - read)) / pvt_data->data_rate);
 		} else {
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 9 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

Quoted 16 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +0 -0
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 10 lines
drivers/char/hw_random/optee-rng.c +2 -2
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#3 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 6 lines · Sumit Garg
Quoted 5 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

Quoted 6 lines
Quoted 4 lines

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Quoted 9 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#4 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 49 lines · Foundries
Quoted 6 lines · Sumit Garg
Quoted 5 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

Quoted 6 lines
Quoted 4 lines

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

Quoted 7 lines

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

any further comments?

Quoted 9 lines
Quoted 9 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#5 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

In reply to Jorge Ramirez-Ortiz, Foundries

Apologies for my delayed response as I was busy with some other tasks along with holidays.

Quoted 11 lines · Foundries
Quoted 9 lines · Sumit Garg
Quoted 8 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

This case is already handled by core API: rng_dev_read().

Quoted 32 lines

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

Quoted 6 lines

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Wouldn't it lead to a call as msleep(0); that means no wait as well?

-Sumit

Quoted 10 lines
Quoted 10 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#6 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 2 lines · Sumit Garg

Apologies for my delayed response as I was busy with some other tasks along with holidays.

no pb! was just making sure this wasnt falling through some cracks.

Quoted 16 lines
Quoted 13 lines · Foundries
Quoted 11 lines · Sumit Garg
Quoted 9 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I am not sure how this is possible, can you elaborate?

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

This case is already handled by core API: rng_dev_read().

ah ok good point, you are right
but yeah, there is no consequence to the actual patch.

Quoted 33 lines
Quoted 33 lines

with this patch, this request is avoided.

Quoted 29 lines
Quoted 17 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +0 -0
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 10 lines
drivers/char/hw_random/optee-rng.c +2 -2
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

Quoted 8 lines
Quoted 6 lines

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Wouldn't it lead to a call as msleep(0); that means no wait as well?

I dont understand: there is no reason to wait if read == max and this patch will not wait: if read == max it calls 'return read'

am I misunderstanding your point?

Quoted 13 lines

-Sumit

Quoted 10 lines
Quoted 10 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#7 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 25 lines · Foundries
Quoted 2 lines · Sumit Garg

Apologies for my delayed response as I was busy with some other tasks along with holidays.

no pb! was just making sure this wasnt falling through some cracks.

Quoted 18 lines
Quoted 15 lines · Foundries
Quoted 12 lines · Sumit Garg
Quoted 9 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

This case is already handled by core API: rng_dev_read().

ah ok good point, you are right
but yeah, there is no consequence to the actual patch.

So, at least you could get rid of the corresponding text from commit message.

Quoted 40 lines
Quoted 40 lines
Quoted 39 lines

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Quoted 6 lines
Quoted 1 line

Wouldn't it lead to a call as msleep(0); that means no wait as well?

I dont understand: there is no reason to wait if read == max and this patch will not wait: if read == max it calls 'return read'

am I misunderstanding your point?

What I mean is that we shouldn't require this extra check here as there wasn't any wait if read == max with existing implementation too.

-Sumit

Quoted 14 lines
Quoted 14 lines

-Sumit

Quoted 10 lines
Quoted 10 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#8 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 81 lines · Sumit Garg
Quoted 25 lines · Foundries
Quoted 2 lines · Sumit Garg

Apologies for my delayed response as I was busy with some other tasks along with holidays.

no pb! was just making sure this wasnt falling through some cracks.

Quoted 18 lines
Quoted 15 lines · Foundries
Quoted 12 lines · Sumit Garg
Quoted 9 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

This case is already handled by core API: rng_dev_read().

ah ok good point, you are right
but yeah, there is no consequence to the actual patch.

So, at least you could get rid of the corresponding text from commit message.

Quoted 42 lines
Quoted 41 lines
Quoted 33 lines

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

Quoted 6 lines

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Wouldn't it lead to a call as msleep(0); that means no wait as well?

Quoted 7 lines
Quoted 4 lines

I dont understand: there is no reason to wait if read == max and this patch will not wait: if read == max it calls 'return read'

am I misunderstanding your point?

What I mean is that we shouldn't require this extra check here as there wasn't any wait if read == max with existing implementation too.

um, I am getting confused Sumit

with the exisiting implementation (the one we aim to replace), if get_optee_rng_data reads all the values requested on the first call (ie, read = 0) with wait set to true, the call will wait with msleep(0). Which is unnecessary and waits for a jiffy (ie, the call to msleep 0 will schedule a one jiffy timeout interrruptible)

with this alternative implementation, msleep(0) does not get called.

are we in synch?

Quoted 17 lines

-Sumit

Quoted 14 lines
Quoted 14 lines

-Sumit

Quoted 10 lines
Quoted 10 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#9 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 91 lines · Foundries
Quoted 81 lines · Sumit Garg
Quoted 25 lines · Foundries
Quoted 2 lines · Sumit Garg

Apologies for my delayed response as I was busy with some other tasks along with holidays.

no pb! was just making sure this wasnt falling through some cracks.

Quoted 18 lines
Quoted 15 lines · Foundries
Quoted 12 lines · Sumit Garg
Quoted 9 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

This case is already handled by core API: rng_dev_read().

ah ok good point, you are right
but yeah, there is no consequence to the actual patch.

So, at least you could get rid of the corresponding text from commit message.

Quoted 42 lines
Quoted 41 lines
Quoted 33 lines

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

Quoted 6 lines

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Wouldn't it lead to a call as msleep(0); that means no wait as well?

Quoted 7 lines
Quoted 4 lines

I dont understand: there is no reason to wait if read == max and this patch will not wait: if read == max it calls 'return read'

am I misunderstanding your point?

What I mean is that we shouldn't require this extra check here as there wasn't any wait if read == max with existing implementation too.

Quoted 7 lines

um, I am getting confused Sumit

with the exisiting implementation (the one we aim to replace), if get_optee_rng_data reads all the values requested on the first call (ie, read = 0) with wait set to true, the call will wait with msleep(0). Which is unnecessary and waits for a jiffy (ie, the call to msleep 0 will schedule a one jiffy timeout interrruptible)

with this alternative implementation, msleep(0) does not get called.

are we in synch?

Ah, I see msleep(0) also by default schedules timeout for 1 jiffy. So we are in sync now. Probably you can clarify this in commit message as well to avoid confusion.

-Sumit

Quoted 18 lines
Quoted 18 lines

-Sumit

Quoted 14 lines
Quoted 14 lines

-Sumit

Quoted 10 lines
Quoted 10 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#10 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 97 lines · Sumit Garg
Quoted 95 lines · Foundries
Quoted 81 lines · Sumit Garg
Quoted 25 lines · Foundries
Quoted 2 lines · Sumit Garg

Apologies for my delayed response as I was busy with some other tasks along with holidays.

no pb! was just making sure this wasnt falling through some cracks.

Quoted 18 lines
Quoted 15 lines · Foundries
Quoted 12 lines · Sumit Garg
Quoted 9 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

This case is already handled by core API: rng_dev_read().

ah ok good point, you are right
but yeah, there is no consequence to the actual patch.

So, at least you could get rid of the corresponding text from commit message.

Quoted 42 lines
Quoted 41 lines
Quoted 33 lines

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

Quoted 6 lines

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Wouldn't it lead to a call as msleep(0); that means no wait as well?

Quoted 7 lines
Quoted 4 lines

I dont understand: there is no reason to wait if read == max and this patch will not wait: if read == max it calls 'return read'

am I misunderstanding your point?

What I mean is that we shouldn't require this extra check here as there wasn't any wait if read == max with existing implementation too.

um, I am getting confused Sumit

with the exisiting implementation (the one we aim to replace), if get_optee_rng_data reads all the values requested on the first call (ie, read = 0) with wait set to true, the call will wait with msleep(0). Which is unnecessary and waits for a jiffy (ie, the call to msleep 0 will schedule a one jiffy timeout interrruptible)

Quoted 7 lines
Quoted 3 lines

with this alternative implementation, msleep(0) does not get called.

are we in synch?

Ah, I see msleep(0) also by default schedules timeout for 1 jiffy. So we are in sync now. Probably you can clarify this in commit message as well to avoid confusion.

ok will do.
shall I add your reviewed-by line or just resend?

Quoted 21 lines

-Sumit

Quoted 18 lines
Quoted 18 lines

-Sumit

Quoted 14 lines
Quoted 14 lines

-Sumit

Quoted 10 lines
Quoted 10 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#11 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

Quoted 101 lines · Foundries
Quoted 99 lines · Sumit Garg
Quoted 97 lines · Foundries
Quoted 81 lines · Sumit Garg
Quoted 25 lines · Foundries
Quoted 2 lines · Sumit Garg

Apologies for my delayed response as I was busy with some other tasks along with holidays.

no pb! was just making sure this wasnt falling through some cracks.

Quoted 18 lines
Quoted 15 lines · Foundries
Quoted 12 lines · Sumit Garg
Quoted 9 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

I am not sure how this is possible, can you elaborate?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

currently, if the user sets max 0, get_optee_rng_data will regardless issuese a call to the secure world requesting 0 bytes from the RNG

This case is already handled by core API: rng_dev_read().

ah ok good point, you are right
but yeah, there is no consequence to the actual patch.

So, at least you could get rid of the corresponding text from commit message.

Quoted 42 lines
Quoted 41 lines
Quoted 33 lines

with this patch, this request is avoided.

Quoted 21 lines
Quoted 21 lines

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
        if (max > MAX_ENTROPY_REQ_SZ)
                max = MAX_ENTROPY_REQ_SZ;
 
-       while (read == 0) {
+       while (read < max) {
                rng_size = get_optee_rng_data(pvt_data, data, (max - read));
 
Quoted 8 lines

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 132

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +1 -1
                data += rng_size;
                read += rng_size;
 
                if (wait && pvt_data->data_rate) {
-                       if (timeout-- == 0)
+                       if ((timeout-- == 0) || (read == max))

If read == max, would there be any sleep?

Quoted 6 lines

no but I see no reason why there should be a wait since we already have all the data that we need; the msleep is only required when we need to wait for the RNG to generate entropy for the number of bytes we are requesting. if we are requesting 0 bytes, the entropy is already available. at leat this is what makes sense to me.

Wouldn't it lead to a call as msleep(0); that means no wait as well?

Quoted 7 lines
Quoted 4 lines

I dont understand: there is no reason to wait if read == max and this patch will not wait: if read == max it calls 'return read'

am I misunderstanding your point?

What I mean is that we shouldn't require this extra check here as there wasn't any wait if read == max with existing implementation too.

um, I am getting confused Sumit

with the exisiting implementation (the one we aim to replace), if get_optee_rng_data reads all the values requested on the first call (ie, read = 0) with wait set to true, the call will wait with msleep(0). Which is unnecessary and waits for a jiffy (ie, the call to msleep 0 will schedule a one jiffy timeout interrruptible)

with this alternative implementation, msleep(0) does not get called.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

are we in synch?

Ah, I see msleep(0) also by default schedules timeout for 1 jiffy. So we are in sync now. Probably you can clarify this in commit message as well to avoid confusion.

ok will do.
shall I add your reviewed-by line or just resend?

Yes it's fine with me to add mine reviewed-by.

Quoted 21 lines
Quoted 21 lines

-Sumit

Quoted 18 lines
Quoted 18 lines

-Sumit

Quoted 14 lines
Quoted 14 lines

-Sumit

Quoted 10 lines
Quoted 10 lines

-Sumit

Quoted 6 lines
drivers/char/hw_random/optee-rng.c +0 -1
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
                } else {
--

2.17.1

#12 lore raw

Re: [PATCHv2 2/2] hwrng: optee: fix wait use case

In reply to Jorge Ramirez-Ortiz
Quoted 27 lines · Jorge Ramirez-Ortiz

The current code waits for data to be available before attempting a second read. However the second read would not be executed as the while loop exits.

This fix does not wait if all data has been read and reads a second time if only partial data was retrieved on the first read.

This fix also does not attempt to read if not data is requested.

---
v2: tidy up the while loop to avoid reading when no data is requested

 drivers/char/hw_random/optee-rng.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/hw_random/optee-rng.c +1 -1
index 5bc4700c4dae..a99d82949981 100644
@@ -122,14 +122,14 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
 	if (max > MAX_ENTROPY_REQ_SZ)
 		max = MAX_ENTROPY_REQ_SZ;
 
-	while (read == 0) {
+	while (read < max) {
 		rng_size = get_optee_rng_data(pvt_data, data, (max - read));

drivers/char/hw_random/optee-rng.c static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait) · line 135

Quoted 10 lines
drivers/char/hw_random/optee-rng.c +1 -1
 
 		data += rng_size;
 		read += rng_size;
 
 		if (wait && pvt_data->data_rate) {
-			if (timeout-- == 0)
+			if ((timeout-- == 0) || (read == max))
 				return read;
 			msleep((1000 * (max - read)) / pvt_data->data_rate);
 		} else {

any comments please?

Reviewed-by on v2 1/2
#13 lore raw
In reply to Jorge Ramirez-Ortiz
Quoted 10 lines · Jorge Ramirez-Ortiz

Data rates of MAX_UINT32 will schedule an unnecessary one jiffy timeout on the call to msleep. Avoid this scenario by using 0 as the unlimited data rate.

---
 drivers/char/hw_random/optee-rng.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Sounds good to me. FWIW:

-Sumit

Quoted 16 lines
drivers/char/hw_random/optee-rng.c +1 -2
index 49b2e02537dd..5bc4700c4dae 100644
@@ -128,7 +128,7 @@ static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
                data += rng_size;
                read += rng_size;
 
-               if (wait) {
+               if (wait && pvt_data->data_rate) {
                        if (timeout-- == 0)
                                return read;
                        msleep((1000 * (max - read)) / pvt_data->data_rate);
--

2.17.1