drm/nouveau: fix crash when DP link training fails
Subsystems (4)
cover drm/nouveau: fix crash when DP link training fails
On a Quadro 600 (GF108), any failed DisplayPort link training during a modeset crashes the kernel with a divide error in nv50_disp_super_2_2_dp(), because the supervisor divides by the lane count that training left at 0. Two ways to get there:
- Xorg (modesetting) builds 3840x2160@60 (533 MHz) from the EDID of a 4K monitor and sets it on the card's DP 1.1 output, which can carry at most 4 x 2.7 Gbit/s even at 6 bpc;
- a connector forced on while the monitor sleeps or is unplugged, then any mode set on it.
Patch 1 stops the crash. Patch 2 makes atomic check reject DP modes that cannot fit the link, so userspace gets -EINVAL instead of a dark screen.
Tested on Ubuntu's 7.0 kernel with a libdrm reproducer (SETCRTC with a user-defined mode) and with Xorg; the patches apply unchanged on drm-misc-fixes.
Written with help from Claude (Anthropic); reviewed and tested on hardware.
Frédéric Mahé (2):
drm/nouveau/disp/nv50-: don't divide by zero when the DP link is not
trained
drm/nouveau/dispnv50: reject DP modes that don't fit the link
drivers/gpu/drm/nouveau/dispnv50/disp.c | 15 +++++++++++++++ drivers/gpu/drm/nouveau/nvkm/engine/disp/nv50.c | 10 ++++++++++ 2 files changed, 25 insertions(+)
Signature · 1 line
2.53.0
PATCH 1/2 drm/nouveau/disp/nv50-: don't divide by zero when the DP link is not trained
Since link training moved out of the supervisor, ior->dp.nr is copied from the lane count chosen by the DRM side, and it stays 0 when training
fails: no lane/rate combination fits the mode, or the sink does not answer on AUX. nouveau_dp_train()'s result is ignored by nv50_sor_atomic_enable(), so the modeset goes on, and supervisor 2.2 divides by ior->dp.nr in nv50_disp_super_2_2_dp():
Oops: divide error: 0000 [#1] SMP PTI Workqueue: nvkm-disp nv50_disp_super [nouveau] RIP: 0010:nv50_disp_super_2_2_dp+0xa4/0x340 [nouveau] nv50_disp_super_2_2+0xcc/0x140 [nouveau] nv50_disp_super+0x137/0x250 [nouveau]
Seen on a Quadro 600 (GF108) when Xorg set 3840x2160@60 (533 MHz) on a DP 1.1 output, and when setting 1024x768 on a forced-on DP connector whose monitor was asleep.
Skip the transfer unit and watermark setup with a warning when the link has no lanes. Before the change below, the training loop never left ior->dp.nr at 0.
Written with help from Claude (Anthropic); reviewed and tested on hardware.
--- drivers/gpu/drm/nouveau/nvkm/engine/disp/nv50.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
drivers/gpu/drm/nouveau/nvkm/engine/disp/nv50.c +10 -0
@@ -1166,6 +1166,16 @@ nv50_disp_super_2_2_dp(struct nvkm_head *head, struct nvkm_ior *ior)u32 best_diff =64 * symbol;u64 h, v; +/* Link training failed or was never done (e.g. mode too fast for the +* link, or no sink answering): there is no link to configure, and +* the calculations below would divide by zero. +*/ +if (!ior->dp.nr || !ior->dp.bw) { +nvkm_warn (subdev,"head-%d: DP link not trained (nr %d bw %d)\n" , + head->id, ior->dp.nr, ior->dp.bw); +return ; + } +/* symbols/hblank - algorithm taken from comments in tegra driver */ h = head->asy.hblanke + head->asy.htotal - head->asy.hblanks -7 ; h = h * linkKBps;
Signature · 1 line
2.53.0
PATCH 2/2 drm/nouveau/dispnv50: reject DP modes that don't fit the link
In reply to Frédéric Mahénv50_dp_mode_valid() keeps modes that are too fast for the DP link out of the connector's mode list, but a user-supplied mode only goes through atomic check, where nv50_outp_atomic_fix_depth() lowers bpc to 6 and then accepts the mode even if it still does not fit. Link training then fails, the display stays dark and, before the previous fix, the kernel crashed.
This happens with Xorg's modesetting driver, which builds modes from the EDID itself: on a Quadro 600 (DP 1.1, 4 lanes at 2.7 Gbit/s) with a 4K monitor it sets 3840x2160@60 (533 MHz), which needs 1200 MB/s even at 18 bpp against 1080 MB/s for the link.
Return -EINVAL from atomic check in that case, so userspace can fall back to another mode. Only check when the link capabilities are known, so that a sink that is asleep or still resuming does not make every commit fail.
Written with help from Claude (Anthropic); reviewed and tested on hardware.
--- drivers/gpu/drm/nouveau/dispnv50/disp.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/gpu/drm/nouveau/dispnv50/disp.c +15 -0
@@ -423,6 +423,21 @@ nv50_outp_atomic_check(struct drm_encoder *encoder,/* We might have to reduce the bpc */ nv50_outp_atomic_fix_depth (encoder, crtc_state); +/* Reject a mode that does not fit the DP link even at the lowest bpc: +* link training would fail and the display would stay dark. Only +* when the link capabilities are known, so that a sink that is +* asleep or still resuming does not make every commit fail. +*/ +if (crtc_state->mode_changed && crtc_state->active) { +struct nouveau_encoder *nv_encoder =nouveau_encoder (encoder); + +if (nv_encoder->dcb->type ==DCB_OUTPUT_DP && + nv_encoder->dp.link_nr && nv_encoder->dp.link_bw && +nv50_dp_mode_valid (nv_encoder, &crtc_state->adjusted_mode, +NULL ) !=MODE_OK ) +return -EINVAL ; + } +return 0 ; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/2] drm/nouveau/dispnv50: reject DP modes that don't fit the link
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/nouveau: incomplete DP mode check condition in nv50_outp_atomic_check() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/nouveau: incomplete DP mode check condition in nv50_outp_atomic_check()
--
commit 943220be5958f2da589b892e0f39c0caa93bf128
Author: Frédéric Mahé <[email protected]>
drm/nouveau/dispnv50: reject DP modes that don't fit the link
This commit rejects DisplayPort modes in nv50_outp_atomic_check() when nv50_dp_mode_valid() indicates that the mode exceeds the link capacity even after reducing the bpc.
Quoted 12 lines
drivers/gpu/drm/nouveau/dispnv50/disp.c +4
-0@@ -423,6 +423,21 @@ nv50_outp_atomic_check(struct drm_encoder *encoder,/* We might have to reduce the bpc */ nv50_outp_atomic_fix_depth (encoder, crtc_state); +/* Reject a mode that does not fit the DP link even at the lowest bpc: +* link training would fail and the display would stay dark. Only +* when the link capabilities are known, so that a sink that is +* asleep or still resuming does not make every commit fail.
drivers/gpu/drm/nouveau/dispnv50/disp.c nv50_outp_atomic_check(struct drm_encoder *encoder, · line 439
Quoted 10 lines
drivers/gpu/drm/nouveau/dispnv50/disp.c +10
-0+*/ +if (crtc_state->mode_changed && crtc_state->active) { +struct nouveau_encoder *nv_encoder =nouveau_encoder (encoder); + +if (nv_encoder->dcb->type ==DCB_OUTPUT_DP && + nv_encoder->dp.link_nr && nv_encoder->dp.link_bw && +nv50_dp_mode_valid (nv_encoder, &crtc_state->adjusted_mode, +NULL ) !=MODE_OK ) +return -EINVAL ; + }
[Severity: Medium]
Should this condition be (crtc_state->mode_changed || crtc_state->connectors_changed), matching the bpc check earlier in nv50_outp_atomic_check()?
If connector routing or a connector property changes without changing the CRTC mode, crtc_state->connectors_changed is set while crtc_state->mode_changed remains false. This check is then skipped, even though nv50_disp_outp_atomic_check_set() tests drm_atomic_crtc_needs_modeset() and still enables the DP output and runs link training with the out-of-range mode.
Also, struct drm_crtc_state documents that atomic_check callbacks must not consult crtc_state->active to reject a commit. Gating on crtc_state->active allows an out-of-range mode to be committed while active is false; a later DPMS ON commit only sets active_changed (with mode_changed false), bypassing this check as well.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Hi,
After sending this I found David Hedges' series "drm/nouveau: recover DP links the sink drops after waking" (Cc'd). His patch 1/5 does what my patch 2/2 does, more completely, and touches the same function, so I am dropping 2/2 in favour of his. That also covers the points the Sashiko review raised on 2/2 (the check only ran when mode_changed and active were both set).
Patch 1/2 (divide by zero in nv50_disp_super_2_2_dp() when the DP link is not trained) is not in his series and still applies: it protects nvkm itself, whatever the DRM side does, and it is tagged for stable. I can resend it alone if that is easier.
I tested his series on a Quadro 600 (GF108, no GSP-RM) and will reply in his thread with the results.
Frédéric