Skip to contentSkip to conversation

hw_random: timeriomem-rng: add configurable read width and data mask

PATCH In review Jad Keskes 3 participants 3 messages Jun 15 – Jun 17

Subsystems (3)
lore
Share & tools
lore

Jad Keskes

author
#0 lore raw

PATCH v2 1/1 hw_random: timeriomem-rng: add configurable read width and data mask

The TODO for supporting read sizes other than 32 bits and masking has been sitting in this driver since 2009. Implement it.

Add width (8, 16, or 32 bits) and mask properties to the platform data and device tree bindings. The read loop dispatches on width using readb/readw/readl so a configured 8-bit access doesn't trigger a bus error on hardware that rejects 32-bit reads to that address. The mask is ANDed with the value before storing.

These are platform properties, not runtime policy -- width depends on SoC integration, mask reflects which output bits carry entropy.

The alignment check in probe is updated to verify the resource is aligned to the configured width instead of hardcoding 4-byte alignment.

---

v2:
- Remove old timeriomem_rng.yaml to avoid dt_binding_check conflict
- Use IS_ALIGNED() instead of modulo for 32-bit PAE safety

 .../bindings/rng/timeriomem-rng.yaml          | 76 ++++++++++++++++++
 .../bindings/rng/timeriomem_rng.yaml          | 48 ------------
 drivers/char/hw_random/timeriomem-rng.c       | 78 +++++++++++++++----
 include/linux/timeriomem-rng.h                | 12 +++
 4 files changed, 153 insertions(+), 61 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rng/timeriomem-rng.yaml
 delete mode 100644 Documentation/devicetree/bindings/rng/timeriomem_rng.yaml
Documentation/devicetree/bindings/rng/timeriomem-rng.yaml new file +76 -0
new file mode 100644
index 000000000000..0d8460e9f916
@@ -0,0 +1,76 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rng/timeriomem-rng.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Timer IOMEM Hardware Random Number Generator
+
+description: |
+  This binding covers platforms that have a single IO memory address which
+  provides periodic random data.  The driver reads from the address at a
+  fixed interval, returning a configurable-width value masked to the desired
+  bits.
+
+maintainers:
+  - Alexander Clouter <[email protected]>
+
+properties:
+  compatible:
+    enum:
+      - timeriomem_rng
+
+  reg:
+    maxItems: 1
+
+  period:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      Interval in microseconds between reads.  New random data is expected to
+      be available at this rate.
+
+  quality:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    default: 0
+    description:
+      Estimated entropy per 1024 bits of data, in the same scale as the
+      kernel's hwrng core (0-1024).
+
+  width:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    default: 32
+    enum: [8, 16, 32]
+    description:
+      Access width in bits.  Determines whether the read is performed as
+      an 8-bit, 16-bit, or 32-bit bus access.
+
+  mask:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    default: 0xFFFFFFFF
+    description:
+      Mask applied to the value read from the register.  Bits set to 0 in
+      the mask are cleared in the output data.  Default (no mask) passes
+      all bits through.
+
+required:
+  - compatible
+  - reg
+  - period
+
+additionalProperties: false
+
+examples:
+  - |
+    rng@f0001000 {
+        compatible = "timeriomem_rng";
+        reg = <0xf0001000 0x4>;
+        period = <100000>;
+    };
+
+    rng@f0002000 {
+        compatible = "timeriomem_rng";
+        reg = <0xf0002000 0x1>;
+        period = <50000>;
+        width = <8>;
+        mask = <0xFF>;
+    };
Documentation/devicetree/bindings/rng/timeriomem_rng.yaml deleted +0 -48

48 lines not on this page · raw message

drivers/char/hw_random/timeriomem-rng.c +65 -13

123 lines not on this page · raw message

include/linux/timeriomem-rng.h +12 -0
index 672df7fbf6c1..b4202ad2f507 100644
@@ -16,6 +16,18 @@ struct timeriomem_rng_data {
 
 	/* bits of entropy per 1024 bits read */
 	unsigned int		quality;
+
+	/* read width (8, 16, or 32), 0 means 32 */
+	unsigned int		width;
+
+	/* set to true if width is explicitly provided */
+	bool			width_set;
+
+	/* mask applied to raw read value */
+	u32			mask;
+
+	/* set to true if mask is explicitly provided */
+	bool			mask_set;
 };
 
 #endif /* _LINUX_TIMERIOMEM_RNG_H */
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but an improper teardown sequence allows a concurrent reader to restart the timer after it was cancelled, leading to a Use-After-Free and potential Deadlock. --

commit f3fa66cce2c0ad3af71293c86473cf060b400645
Author: Jad Keskes <[email protected]>

hw_random: timeriomem-rng: add configurable read width and data mask

This commit adds support for reading 8, 16, or 32 bits from the hardware random number generator. It applies an optional mask to the data and configures these properties via device tree or platform data.

Quoted 4 lines
drivers/char/hw_random/timeriomem-rng.c +0 -0
index e61f063932090..4557326618c90 100644

[ ... ]

Quoted 15 lines
drivers/char/hw_random/timeriomem-rng.c +6 -5
@@ -71,11 +76,28 @@ static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
 			usleep_range(period_us,
 					period_us + max(1, period_us / 100));
 
-		*(u32 *)data = readl(priv->io_base);
-		retval += sizeof(u32);
-		data += sizeof(u32);
-		max -= sizeof(u32);
-	} while (wait && max > sizeof(u32));
+		switch (priv->width) {
+		case 8: {
+			u8 val = readb(priv->io_base) & priv->mask;
+			*(u8 *)data = val;
+			break;
+		}

[ ... ]

drivers/char/hw_random/timeriomem-rng.c static int timeriomem_rng_read(struct hwrng *hwrng, void *data, · line 100

Quoted 6 lines
drivers/char/hw_random/timeriomem-rng.c +6 -0
+		}
+
+		retval += chunk;
+		data += chunk;
+		max -= chunk;
+	} while (wait && max > chunk);

[Severity: High]
This isn't a bug introduced by this patch, but does the current driver teardown sequence allow a concurrent reader to cause a use-after-free or deadlock?

Because the driver uses devm_hwrng_register(), the hwrng is unregistered by devres after timeriomem_rng_remove() completes.

If timeriomem_rng_remove() calls hrtimer_cancel(&priv->timer) while a reader is inside timeriomem_rng_read() (for example, sleeping between reads), the devres cleanup will subsequently call hwrng_unregister() which blocks waiting for the active reader to finish.

However, the reader will eventually finish its loop and unconditionally run the following at the end of timeriomem_rng_read():

	hrtimer_forward_now(&priv->timer, priv->period);
	hrtimer_restart(&priv->timer);

Can this sequence restart the timer after it was cancelled by the remove function, allowing the timer to eventually fire and dereference the freed priv pointer?

Additionally, if a reader is waiting in wait_for_completion(&priv->completion) when the timer is cancelled, could this prevent the timer from firing, leaving the reader hanging in TASK_UNINTERRUPTIBLE and causing the subsequent hwrng_unregister() to deadlock?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Jad Keskes
Quoted 18 lines · Jad Keskes

The TODO for supporting read sizes other than 32 bits and masking has been sitting in this driver since 2009. Implement it.

Add width (8, 16, or 32 bits) and mask properties to the platform data and device tree bindings. The read loop dispatches on width using readb/readw/readl so a configured 8-bit access doesn't trigger a bus error on hardware that rejects 32-bit reads to that address. The mask is ANDed with the value before storing.

These are platform properties, not runtime policy -- width depends on SoC integration, mask reflects which output bits carry entropy.

The alignment check in probe is updated to verify the resource is aligned to the configured width instead of hardcoding 4-byte alignment.

---

Quoted 7 lines

v2:
- Remove old timeriomem_rng.yaml to avoid dt_binding_check conflict
- Use IS_ALIGNED() instead of modulo for 32-bit PAE safety

 .../bindings/rng/timeriomem-rng.yaml          | 76 ++++++++++++++++++
 .../bindings/rng/timeriomem_rng.yaml          | 48 ------------

I don't undetstand this diff... what are you doing exactly? And more important WHY?

Please run scripts/checkpatch.pl on the patches and fix reported warnings. After that, run also 'scripts/checkpatch.pl --strict' on the patches and (probably) fix more warnings. Some warnings can be ignored, especially from --strict run, but the code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.

Quoted 5 lines
 drivers/char/hw_random/timeriomem-rng.c       | 78 +++++++++++++++----
 include/linux/timeriomem-rng.h                | 12 +++
 4 files changed, 153 insertions(+), 61 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/rng/timeriomem-rng.yaml
 delete mode 100644 Documentation/devicetree/bindings/rng/timeriomem_rng.yaml

Best regards,
Krzysztof