Skip to contentSkip to conversation

hwrng: core - drop unnecessary forward declarations

PATCH Superseded Thorsten Blum 3 participants 10 messages Apr 30 – May 5

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 hwrng: core - drop unnecessary forward declarations

The forward declarations for drop_current_rng() and rng_get_data() are not needed - remove them.

---
 drivers/char/hw_random/core.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/char/hw_random/core.c +0 -4
index aba92d777f72..68add1a97f31 100644
@@ -54,13 +54,9 @@ module_param(default_quality, ushort, 0644);
 MODULE_PARM_DESC(default_quality,
 		 "default maximum entropy content of hwrng per 1024 bits of input");
 
-static void drop_current_rng(void);
 static int hwrng_init(struct hwrng *rng);
 static int hwrng_fillfn(void *unused);
 
-static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
-			       int wait);
-
 static size_t rng_buffer_size(void)
 {
 	return RNG_BUFFER_SIZE;
#1 lore raw

PATCH 2/4 hwrng: core - use bool for wait parameter in rng_get_data

The 'wait' parameter in rng_get_data() is a boolean flag - use bool instead of int to better reflect its actual type.

---
 drivers/char/hw_random/core.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/char/hw_random/core.c +2 -3
index 68add1a97f31..905a63525831 100644
@@ -211,7 +211,7 @@ static int rng_dev_open(struct inode *inode, struct file *filp)
 }
 
 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
-			int wait) {
+			bool wait) {
 	int present;
 
 	BUG_ON(!mutex_is_locked(&reading_mutex));
@@ -534,8 +534,7 @@ static int hwrng_fillfn(void *unused)
 		}
 
 		mutex_lock(&reading_mutex);
-		rc = rng_get_data(rng, rng_fillbuf,
-				  rng_buffer_size(), 1);
+		rc = rng_get_data(rng, rng_fillbuf, rng_buffer_size(), true);
 		if (current_quality != rng->quality)
 			rng->quality = current_quality; /* obsolete */
 		quality = rng->quality;
#2 lore raw

Re: [PATCH 2/4] hwrng: core - use bool for wait parameter in rng_get_data

Quoted 2 lines · Thorsten Blum

The 'wait' parameter in rng_get_data() is a boolean flag - use bool instead of int to better reflect its actual type.

...

drivers/char/hw_random/core.c static int rng_dev_open(struct inode *inode, struct file *filp) · line 214

Quoted 3 lines

static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,

drivers/char/hw_random/core.c +1 -1
-			int wait) {
+			bool wait) {

You want to fix the checkpatch warnings while at it and indentation.

static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
bool wait)
{

...

drivers/char/hw_random/core.c static int hwrng_fillfn(void *unused) · line 537

Quoted 3 lines
drivers/char/hw_random/core.c +1 -2
-		rc = rng_get_data(rng, rng_fillbuf,
-				  rng_buffer_size(), 1);
+		rc = rng_get_data(rng, rng_fillbuf, rng_buffer_size(), true);

Is it the only user? Why parameter is needed at all?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH 2/4] hwrng: core - use bool for wait parameter in rng_get_data

Quoted 11 lines · Andy Shevchenko
Quoted 2 lines · Thorsten Blum

The 'wait' parameter in rng_get_data() is a boolean flag - use bool instead of int to better reflect its actual type.

...

drivers/char/hw_random/core.c static int rng_dev_open(struct inode *inode, struct file *filp) · line 214

Quoted 3 lines

static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,

drivers/char/hw_random/core.c +1 -1
-			int wait) {
+			bool wait) {

You want to fix the checkpatch warnings while at it and indentation.

I just checked again, but I don't get any checkpatch warnings.

Quoted 11 lines

static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
bool wait)
{

...

drivers/char/hw_random/core.c static int hwrng_fillfn(void *unused) · line 537

Quoted 3 lines
drivers/char/hw_random/core.c +1 -2
-		rc = rng_get_data(rng, rng_fillbuf,
-				  rng_buffer_size(), 1);
+		rc = rng_get_data(rng, rng_fillbuf, rng_buffer_size(), true);

Is it the only user? Why parameter is needed at all?

The other one in rng_dev_read() already uses a boolean expression, hence no changes.

Thanks,
Thorsten

#4 lore raw

Re: [PATCH 2/4] hwrng: core - use bool for wait parameter in rng_get_data

Quoted 2 lines · Thorsten Blum
Quoted 1 line · Andy Shevchenko

On Thu, Apr 30, 2026 at 01:00:49PM +0200, Thorsten Blum wrote:

...

Quoted 7 lines
Quoted 5 lines

drivers/char/hw_random/core.c static int rng_dev_open(struct inode *inode, struct file *filp) · line 214

Quoted 3 lines

static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,

drivers/char/hw_random/core.c +1 -1
-			int wait) {
+			bool wait) {

You want to fix the checkpatch warnings while at it and indentation.

I just checked again, but I don't get any checkpatch warnings.

Hmm... I remember for sure that checkpatch warns when it's a { on the line of the prototype... Maybe it's about different scenario. Whatever, the coding style is to have { on the new line.

...

Quoted 8 lines
Quoted 5 lines

drivers/char/hw_random/core.c static int hwrng_fillfn(void *unused) · line 537

Quoted 3 lines
drivers/char/hw_random/core.c +1 -2
-		rc = rng_get_data(rng, rng_fillbuf,
-				  rng_buffer_size(), 1);
+		rc = rng_get_data(rng, rng_fillbuf, rng_buffer_size(), true);

Is it the only user? Why parameter is needed at all?

The other one in rng_dev_read() already uses a boolean expression, hence no changes.

Good, thanks for clarification.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

PATCH 3/4 hwrng: core - use MAX to simplify RNG_BUFFER_SIZE

In reply to Thorsten Blum

Replace the open-coded variant with MAX().

---
 drivers/char/hw_random/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/char/hw_random/core.c +2 -1
index 905a63525831..f8f7a2ee73c1 100644
@@ -17,6 +17,7 @@
 #include <linux/hw_random.h>
 #include <linux/kernel.h>
 #include <linux/kthread.h>
+#include <linux/minmax.h>
 #include <linux/miscdevice.h>
 #include <linux/module.h>
 #include <linux/random.h>
@@ -30,7 +31,7 @@
 
 #define RNG_MODULE_NAME		"hw_random"
 
-#define RNG_BUFFER_SIZE (SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES)
+#define RNG_BUFFER_SIZE		MAX(32, SMP_CACHE_BYTES)
 
 static struct hwrng __rcu *current_rng;
 /* the current rng has been explicitly chosen by user via sysfs */
#6 lore raw

Re: [PATCH 3/4] hwrng: core - use MAX to simplify RNG_BUFFER_SIZE

Quoted 1 line · Thorsten Blum

Replace the open-coded variant with MAX().

Hmm... While this patch is correct the very similar is being used outside of hwrng. Can we perhaps have a common definition somewhere near to SMP_CACHE_BYTES?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#7 lore raw

Re: [PATCH 3/4] hwrng: core - use MAX to simplify RNG_BUFFER_SIZE

In reply to Thorsten Blum
Quoted 12 lines · Thorsten Blum

Replace the open-coded variant with MAX().

---
 drivers/char/hw_random/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/char/hw_random/core.c +0 -0
index 905a63525831..f8f7a2ee73c1 100644

drivers/char/hw_random/core.c line 20

Quoted 4 lines
drivers/char/hw_random/core.c +1 -0
@@ -17,6 +17,7 @@
 #include <linux/hw_random.h>
 #include <linux/kernel.h>
 #include <linux/kthread.h>
+#include <linux/minmax.h>

No need for minmax.h because kernel.h already includes it.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#8 lore raw

PATCH 4/4 hwrng: core - use sysfs_emit_at in rng_available_show

In reply to Thorsten Blum

Replace strlcat() with sysfs_emit_at() in rng_available_show() and add 'int len' to keep track of the number of bytes written. sysfs_emit_at() is preferred for formatting sysfs output because it provides safer bounds checking.

Inline mutex_lock_interruptible() and drop the now-unused local error variable. Remove the unnecessary 'buf' NUL initialization. Return 'len' directly instead of strlen(buf).

---
 drivers/char/hw_random/core.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/char/hw_random/core.c +7 -10
index f8f7a2ee73c1..1d1ea4ebde31 100644
@@ -26,6 +26,7 @@
 #include <linux/sched/signal.h>
 #include <linux/slab.h>
 #include <linux/string.h>
+#include <linux/sysfs.h>
 #include <linux/uaccess.h>
 #include <linux/workqueue.h>
 
@@ -415,21 +416,17 @@ static ssize_t rng_available_show(struct device *dev,
 				  struct device_attribute *attr,
 				  char *buf)
 {
-	int err;
 	struct hwrng *rng;
+	int len = 0;
 
-	err = mutex_lock_interruptible(&rng_mutex);
-	if (err)
+	if (mutex_lock_interruptible(&rng_mutex))
 		return -ERESTARTSYS;
-	buf[0] = '\0';
-	list_for_each_entry(rng, &rng_list, list) {
-		strlcat(buf, rng->name, PAGE_SIZE);
-		strlcat(buf, " ", PAGE_SIZE);
-	}
-	strlcat(buf, "none\n", PAGE_SIZE);
+	list_for_each_entry(rng, &rng_list, list)
+		len += sysfs_emit_at(buf, len, "%s ", rng->name);
+	len += sysfs_emit_at(buf, len, "none\n");
 	mutex_unlock(&rng_mutex);
 
-	return strlen(buf);
+	return len;
 }
 
 static ssize_t rng_selected_show(struct device *dev,
Reviewed-by on v1 4/4
#9 lore raw

Re: [PATCH 4/4] hwrng: core - use sysfs_emit_at in rng_available_show

Quoted 8 lines · Thorsten Blum

Replace strlcat() with sysfs_emit_at() in rng_available_show() and add 'int len' to keep track of the number of bytes written. sysfs_emit_at() is preferred for formatting sysfs output because it provides safer bounds checking.

Inline mutex_lock_interruptible() and drop the now-unused local error variable. Remove the unnecessary 'buf' NUL initialization. Return 'len' directly instead of strlen(buf).

This is almost the same I came up with (but only today), hence

Also note, when series is more than a single patch, it's highly recommended to have a cover letter. Some maintainers do even require that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko