Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] drm/nouveau: 32-bit hz in NVIF_HEAD_V0_STATE truncates 35-bit
  GB20x pixel clocks
- [Medium] drm/nouveau: nvkm_uhead_mthd_state() leaks unmasked
  ADJ1000DIV1001 bit into hz
- [Medium] drm/nouveau: nvkm_uhead_mthd_state() triggers WARN_ON on
  valid PIXEL_DEPTH
- [Low] drm/nouveau: unlocked head->arm access in
  nvkm_uhead_mthd_state()
--

commit e5531904317b6dc97d82a06286a52ba8457c1f53
Author: Munique Feitoza <[email protected]>

drm/nouveau/nvif: add NVIF_HEAD_V0_STATE method for head timing readback

This commit adds the NVIF_HEAD_V0_STATE method and nvif_head_state() helper
to expose hardware head timings and pixel clock from nvkm_head_func.state()
to the DRM client layer.

> diff --git a/drivers/gpu/drm/nouveau/include/nvif/head.h 
> b/drivers/gpu/drm/nouveau/include/nvif/head.h
> index 3ec36999e9560..699ac6f6c316b 100644
> --- a/drivers/gpu/drm/nouveau/include/nvif/head.h
> +++ b/drivers/gpu/drm/nouveau/include/nvif/head.h
> @@ -18,6 +18,19 @@ nvif_head_id(struct nvif_head *head)
>       return head->object.handle;
>  }
>  
> +struct nvif_head_state {
> +     u32 hz;

[Severity: Medium]
Can a 32-bit hz field here and in struct nvif_head_state_v0 truncate
Blackwell (GB20x) pixel clocks?

On Blackwell, headca7d_mode() programs 35-bit pixel clocks using
NVCA7D_HEAD_SET_PIXEL_CLOCK_FREQUENCY_HI for bits 34:31, while
gb202_head_state() only reads the low 31 bits:

drivers/gpu/drm/nouveau/nvkm/engine/disp/gb202.c:gb202_head_state() {
        ...
        /* Bit 31 is ADJ1000DIV1001, not a HERTZ bit. We don't have enough bits
         * to add the full clock in hz on Blackwell (35 bits), but state->hz
         * is unused and obsolete under GSP so this is fine.
         */
        state->hz = nvkm_rd32(device, 0x68200c + hoff) & 0x7fffffff;
        ...
}

Exposing head->arm.hz through NVIF_HEAD_V0_STATE invalidates that assumption:
any active GB20x mode at or above 2^31 Hz (~2.147 GHz) will read back a
truncated clock, corrupting mode->clock and vblank timestamping constants.
Widening hz to 64 bits in nvkm_head_state, nvif_head_state, and
nvif_head_state_v0 and reading the high register in gb202_head_state() would
avoid locking the interface to 32 bits.

[ ... ]

> diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/disp/uhead.c 
> b/drivers/gpu/drm/nouveau/nvkm/engine/disp/uhead.c
> index f072cec160402..4181713fabb6f 100644
> --- a/drivers/gpu/drm/nouveau/nvkm/engine/disp/uhead.c
> +++ b/drivers/gpu/drm/nouveau/nvkm/engine/disp/uhead.c
> @@ -71,6 +71,30 @@ nvkm_uhead_mthd_scanoutpos(struct nvkm_head *head, void 
> *argv, u32 argc)
>       return 0;
>  }
>  
> +static int
> +nvkm_uhead_mthd_state(struct nvkm_head *head, void *argv, u32 argc)
> +{
> +     union nvif_head_state_args *args = argv;
> +
> +     if (argc != sizeof(args->v0) || args->v0.version != 0)
> +             return -ENOSYS;
> +
> +     if (!head->func->state)
> +             return -EOPNOTSUPP;
> +
> +     head->func->state(head, &head->arm);

[Severity: Medium]
Can calling head->func->state() on an untouched firmware-configured head
trigger a spurious WARN_ON(1) during probe?

nvkm_uhead_mthd_state() ignores head->arm.or.depth, but gf119_head_state(),
gv100_head_state(), and gb202_head_state() all decode PIXEL_DEPTH and hit
WARN_ON(1) in their default branch for valid hardware encodings outside
18/24/30-bit 4:4:4:

drivers/gpu/drm/nouveau/nvkm/engine/disp/gv100.c:gv100_head_state() {
        ...
        data = nvkm_rd32(device, 0x682004 + hoff);
        switch ((data & 0x000000f0) >> 4) {
        case 5: state->or.depth = 30; break;
        case 4: state->or.depth = 24; break;
        case 1: state->or.depth = 18; break;
        default:
                state->or.depth = 18;
                WARN_ON(1);
                break;
        }
        ...
}

When UEFI GOP or VBIOS boots with a valid 4:2:2 or 36/48-bit 4:4:4 mode
(for example on a 4K60 HDMI 2.0 TV), reading back head state at probe time
will emit a kernel warning splat or panic under panic_on_warn.

> +     args->v0.hz      = head->arm.hz;

[Severity: Medium]
Does head->arm.hz leak the unmasked ADJ1000DIV1001 bit into args->v0.hz on
GF119 through AD10x GPUs?

In HEAD_SET_PIXEL_CLOCK_FREQUENCY (cl907d.h and clc37d.h), bits 30:0 hold
HERTZ and bit 31 is the ADJ1000DIV1001 flag, but gf119_head_state() and
gv100_head_state() read the raw 32-bit register directly into state->hz
without masking bit 31 or applying the 1000/1001 adjustment.

If firmware configures a 1000/1001-adjusted mode such as 59.94 Hz before
nouveau loads, nvif_head_state() returns a clock inflated by 2,147,483,648 Hz,
which corrupts mode->clock and vblank timestamping constants in callers like
nv50_head_read_hw_mode().

> +     args->v0.htotal  = head->arm.htotal;
> +     args->v0.hsynce  = head->arm.hsynce;
> +     args->v0.hblanke = head->arm.hblanke;
> +     args->v0.hblanks = head->arm.hblanks;
> +     args->v0.vtotal  = head->arm.vtotal;
> +     args->v0.vsynce  = head->arm.vsynce;
> +     args->v0.vblanke = head->arm.vblanke;
> +     args->v0.vblanks = head->arm.vblanks;

[Severity: Low]
Can nvkm_uhead_mthd_state() race with nvkm_uhead_mthd_scanoutpos() on the
shared head->arm struct?

In commit "drm/nouveau/dispnv50: read back hardware mode timings on probe",
nv50_display_read_hw_state() loops over all non-MST encoders without skipping
an already-inherited head.  If firmware boots in clone mode with two ORs on
the same head and drm.vblankoffdelay=0, the first encoder enables the vblank
IRQ before the second calls nvif_head_state():

nv50_display_read_hw_state()
  nv50_head_read_hw_mode() [encoder 1]
    drm_crtc_vblank_on()          /* enables vblank IRQ if vblankoffdelay=0 */
  nv50_head_read_hw_mode() [encoder 2, same head]
    nvif_head_state()
      nvkm_uhead_mthd_state()
        head->func->state(head, &head->arm)
        <hardirq>
          nv50_head_vblank_handler()
            ...
              nvkm_uhead_mthd_scanoutpos()
                head->func->state(head, &head->arm)  <-- data race

Neither path shares a lock, so both concurrently overwrite and read head->arm
(a KCSAN data race, and unsafe if nvif_head_state() is called during active
vblanks or modesets).  Passing an explicit armed/assembly flag to
head->func->state() would let callers use a stack-local struct nvkm_head_state
instead of mutating head->arm.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=1

Reply via email to