Skip to content

hwrng: nvt-rng: set a quality so the core feeds the entropy pool - #336

Merged
openipc-ai merged 1 commit into
OpenIPC:novatek-nt9856xfrom
andrein:nvt-rng-quality
Nov 29, 2026
Merged

openipc-ai merged 1 commit into
OpenIPC:novatek-nt9856xfrom
andrein:nvt-rng-quality

Conversation

@andrein

@andrein andrein commented Nov 28, 2026

Copy link
Copy Markdown

The driver registers its hwrng with no quality, so the hw_random core never starts its fill thread and the generator is never used to initialise the entropy pool. A system with no saved seed then waits for interrupt entropy. On a quiet board getrandom() blocks, and dropbear and majestic never open their ports.

This sets the quality to 500. The Novatek sources give no entropy rate for the block, and it offers no per-word handshake to pace reads on: measured on hardware, RNG_TDES_DONE is set in about one control-register read in ten, clears by itself whether or not the data register is read, and is found clear after the block has been idle for milliseconds. Half credit is a conservative figure next to the 900 to 1000 other drivers in this tree use.

Tested on a Reolink E340 (NT98566) started by kexec: the hwrng thread runs, crng init done is logged at 0.74 s, and both RTSP streams deliver. In QEMU, on a root filesystem with no saved seed, the pool is ready at 0.12 s; the same image with the unpatched kernel has not initialised it after 170 s.

Not changed here: the clock handling in cleanup and the probe error paths is incomplete.

🤖 Generated with Claude Code

The driver registers its hwrng with no quality. The hw_random core
starts the hwrng fill thread only when the selected generator has a
quality above zero or rng_core.default_quality is set, and that thread
is the only path that credits generator output to the entropy pool.
With a quality of zero the kernel never reads the generator on its own,
and a system with no saved seed waits for interrupt entropy. On a quiet
board the pool stays uninitialised, getrandom() blocks, and services
that need random numbers at start, such as dropbear and majestic, never
open their ports.

Set the quality to 500, which credits about half of the bits read. The
block is a ring oscillator followed by a CASR and a post-processing
stage. The Novatek sources give no entropy rate for it, and the driver
reads the data register back to back with no pacing. The block offers
no per-word handshake to pace on. Measured on hardware, RNG_TDES_DONE
is set in about one control register read in ten, clears by itself
whether or not the data register is read, and is found clear after the
block has been idle for milliseconds, while 800000 data words read back
to back in four runs, most of them with the bit clear, contain no
repeated neighbour. The other drivers in this tree that set a quality
use 900 to 1000, so half credit is a conservative figure for a
generator with no documented rate. The lower value costs nothing at
boot, because the first reads go to crng_fast_load() whatever the
credit and the pool is ready a few milliseconds after the driver
registers.

Tested on a Reolink E340 (NT98566) started by kexec with no rng_core
parameter. The hwrng thread runs, "random: crng init done" is logged at
0.74 s, 10 ms after the driver registers, and both RTSP streams deliver.
In QEMU with a model of the generator and a root filesystem with no
saved seed, the pool is ready at 0.12 s and the streamer serves video.
The same image with the unpatched kernel has not initialised the pool
after 170 s.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

PR Summary by Qodo

Credit Novatek hardware RNG output to the entropy pool

🐞 Bug fix 🕐 Less than 10 minutes

Grey Divider

AI Description

• Assign the Novatek RNG a conservative quality of 500 so the hwrng core starts its fill thread.
• Credit generator output toward entropy initialization, avoiding blocked startup services on
 systems without a saved seed.
Merge Risk 🟢 Low

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed quality declaration and the hwrng core's feeder together implement one bounded behavior: using this Novatek device to supply credited entropy to the kernel pool.
  • The core is a propagation hop, not an independent capability; no separate user-facing or unrelated functional scope was identified.
Sources

drivers/char/hw_random/nvt-rng.c[125-130]

	/*
	 * The entropy rate of the oscillator behind the CASR and the
	 * post-processing stage is undocumented, and reads are unpaced,
	 * so credit half of what is read.
	 */
	rng->rng.quality = 500;
Behavioral complexity 🟢 Low 🧩 Straightforward

  • Before, the driver left hwrng quality at zero, so the core used its default-off entropy estimate.
  • After, it assigns a constant 500.
  • The changed correctness condition is that the fixed estimate must conservatively represent the entropy available from unpaced reads; the hwrng core's selection, threading, and accounting mechanisms are existing infrastructure rather than newly implemented interacting logic.
  • The source does not introduce new branches, state machines, ordering, or transformations in this driver.
Sources

drivers/char/hw_random/nvt-rng.c[125-130]

	/*
	 * The entropy rate of the oscillator behind the CASR and the
	 * post-processing stage is undocumented, and reads are unpaced,
	 * so credit half of what is read.
	 */
	rng->rng.quality = 500;
Potential damage 🟡 Medium 🩹 Recoverable

  • If this changed behavior regresses, the automatic Novatek contribution can be absent or unusable, reducing available entropy input.
  • The kernel retains other entropy mechanisms and other hwrng sources; the inspected code does not establish loss of those sources.
  • Reverting removes the new automatic crediting behavior, while any already-consumed random outputs would depend on the kernel's other entropy guarantees and cannot be retroactively reclassified.
  • Reverting the driver change restores the prior no-automatic-credit behavior.
  • No persistent state or external side effect is introduced by the quality field itself; recovery from an incorrectly trusted entropy estimate would require replacing or regenerating secrets that were created under that condition, if such a fault were established.
Sources

drivers/char/hw_random/nvt-rng.c[125-130]

	/*
	 * The entropy rate of the oscillator behind the CASR and the
	 * post-processing stage is undocumented, and reads are unpaced,
	 * so credit half of what is read.
	 */
	rng->rng.quality = 500;
Assessed commit 765c893
Diagram

graph TD
  A["Novatek RNG"] -->|"quality 500"| B["hwrng core"] --> C["fill thread"] --> D["entropy pool"] --> E["getrandom clients"]
Loading
High-Level Assessment

Setting quality on this driver is the targeted way to enable entropy crediting. A core-wide default_quality would also affect other generators and would not express the Novatek-specific estimate. The undocumented entropy rate remains the key review consideration.

Files changed (1) +6 / -0

Bug fix (1) +6 / -0
nvt-rng.cAssign a conservative quality to the Novatek RNG +6/-0

Assign a conservative quality to the Novatek RNG

• Sets the registered hwrng quality to 500, enabling the core's fill thread to credit approximately half the bits read. A comment explains the conservative estimate given the undocumented entropy rate and unpaced reads.

drivers/char/hw_random/nvt-rng.c

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Action required

1. Switching entropy sources can freeze the system 🐞 Bug ☼ Reliability
Description
Setting the Novatek generator's quality to 500 starts the fill thread, but hwrng_init() stops that
thread while holding rng_mutex when the replacement generator has zero quality. If the thread is
entering get_current_rng() during a sysfs selection or automatic fallback, it waits for the same
mutex while kthread_stop() waits for the thread to exit.
Code

drivers/char/hw_random/nvt-rng.c[130]

+	rng->rng.quality = 500;
Evidence
The added quality starts hwrng_fill; a switch to a zero-quality generator stops it under
rng_mutex. The fill thread must acquire that mutex through get_current_rng(), while
kthread_stop() waits for its exit, establishing the deadlock when those operations overlap.

drivers/char/hw_random/nvt-rng.c[125-130]
drivers/char/hw_random/core.c[139-149]
drivers/char/hw_random/core.c[108-120]
drivers/char/hw_random/core.c[417-430]
drivers/char/hw_random/core.c[309-333]
kernel/kthread.c[538-560]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new nonzero quality starts the hwrng fill thread. Switching to a zero-quality generator can deadlock because the core waits for that thread to exit while holding a mutex the thread needs.
## Fix Focus Areas
- drivers/char/hw_random/nvt-rng.c[125-130]
- drivers/char/hw_random/core.c[139-149]
- drivers/char/hw_random/core.c[417-446]
## Recommended Fix
Rework fill-thread shutdown so it does not wait for thread exit under `rng_mutex`. Preserve synchronization with concurrent generator selections and thread startup, and verify both sysfs selection and unregister fallback to a zero-quality generator.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can switch off images and animations for a plain-text comment

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

@andrein

andrein commented Nov 28, 2026

Copy link
Copy Markdown
Author
  1. Switching entropy sources can freeze the system

@qodo-free-for-open-source-projects the path you describe is in drivers/char/hw_random/core.c and is the same for every driver in this tree that sets a quality (omap-rng, mtk-rng, virtio-rng and others). It needs a switch from this generator to another one with zero quality while the fill thread is running. NT9856x registers a single hwrng, so there is no second generator to switch to; rng_available lists only f0680000.rng on the test camera. I have left the core alone in this PR.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown
  1. Switching entropy sources can freeze the system

@qodo-free-for-open-source-projects the path you describe is in drivers/char/hw_random/core.c and is the same for every driver in this tree that sets a quality (omap-rng, mtk-rng, virtio-rng and others). It needs a switch from this generator to another one with zero quality while the fill thread is running. NT9856x registers a single hwrng, so there is no second generator to switch to; rng_available lists only f0680000.rng on the test camera. I have left the core alone in this PR.

You're right that the deadlock needs a second, zero-quality hwrng to switch to, and this board has only one. The trigger is in drivers/char/hw_random/core.c: hwrng_init() stops the fill thread under rng_mutex. That code affects any driver that sets a quality, so the underlying issue predates this PR. This change makes NT9856x a driver that starts the fill thread, which is why finding 1 appears here.

On this hardware (one registered device, f0680000.rng), the sysfs-switch and fallback paths in finding 1 don't appear reachable. I haven't verified every core.c path, including selecting "none" through sysfs or unregistering the only device.

Options for finding 1:

  • Dismiss it as intentional or deferred if you agree, since the core fix belongs in a separate change. I'll do this only if you ask.
  • I can check the core.c sysfs and unregister paths to confirm the reachability claim before you decide.

@andrein

andrein commented Nov 28, 2026

Copy link
Copy Markdown
Author

@qodo-free-for-open-source-projects Checked both paths in this tree's core.c. hwrng_attr_current_store() has no "none" selection here: an empty string calls enable_best_rng(), which picks the only registered generator again with its quality above zero, and any name that is not on the list returns -ENODEV. hwrng_unregister() drops rng_mutex before it calls kthread_stop() when the list becomes empty. Neither reaches the stop-under-mutex path with one generator. Please mark finding 1 as deferred: the fix belongs in the core, in a separate change.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Checked both paths in this tree's core.c. hwrng_attr_current_store() has no "none" selection here: an empty string calls enable_best_rng(), which picks the only registered generator again with its quality above zero, and any name that is not on the list returns -ENODEV. hwrng_unregister() drops rng_mutex before it calls kthread_stop() when the list becomes empty. Neither reaches the stop-under-mutex path with one generator. Please mark finding 1 as deferred: the fix belongs in the core, in a separate change.

I can't mark finding 1 as deferred yet. The findings catalog for this PR is empty, so I have no stable ID to pass to the dismiss tool. Could you confirm the finding is the one titled "Switching entropy sources can freeze the system" in the published review (drivers/char/hw_random/nvt-rng.c, lines 125-130)? Once you confirm, I can mark it deferred if the catalog lists it.

Your analysis covers the single-generator case. The finding's trigger is a switch to a zero-quality generator. If a second generator with quality 0 is registered and selected through sysfs, or a fallback lands on one, the stop-under-mutex path could still be reached. You may want to note that scope in the deferral, or confirm that a zero-quality generator can't be registered alongside this one.

@openipc-ai openipc-ai left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Three things in its favour beyond what the commit message says:

  • Mainline already trusts this driver fully. Since 16bdbae39428 ("hwrng: core - treat default_quality as a maximum and default to 1024", v6.2), a hwrng that registers no quality gets 1024. On 4.19 the same driver gets 0, and the fill thread never starts. 500 is more cautious than what current kernels would do.
  • We measured the generator independently on another NT98566 (an SC501AI board, 2026-11-27, during #330). We loaded nvt-rng as a module and read a 4 MiB sample from /dev/hwrng at about 2.5 MB/s. It had 119 repeated 32-bit words where the birthday bound expects 128, and the chi² was unremarkable. That fits your "no repeated neighbour" result and rules out a short-period output. It does not measure entropy, but it is the right shape for half credit.
  • It reaches a shipped image. nt98566.nand.config (firmware's nt98566_ultimate) sets CONFIG_HW_RANDOM_NVT=y, so this changes that image's boot, for the better: crng readiness stops depending on interrupt timing.

The "before" is shown in QEMU only. That proves the mechanism, but an unpatched boot log from the E340 (the crng init done time, or its absence) would make the hardware case complete if you have one. This doesn't block the merge.

@openipc-ai
openipc-ai merged commit f42a1c7 into OpenIPC:novatek-nt9856x Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants