# Fix: `nv_drm_revoke_modeset_permission` kernel WARNING

Fixes a kernel `WARN_ON` in `nv_drm_revoke_modeset_permission` on the nvidia-drm 550 branch with Debian kernel 6.12. The warning fires every time a DRM file descriptor is closed (by processes like `glxtest`, `kioworker`, `kscreenlocker_greet`), producing:

```
WARNING at nvidia-drm/nvidia-drm-drv.c:1221 nv_drm_revoke_modeset_permission+0x327/0x340
```

Two patches are needed: one fixing a conftest compile failure (primary cause) and one fixing latent error-handling bugs in the driver.

## Files

| File | Description |
|------|-------------|
| [plan.md](plan.md) | Full root-cause analysis with source references to NVIDIA and kernel code. Covers the conftest failure, the bogus `WARN_ON` in the fallback macro, the wrong mutex assertion for atomic drivers, and two secondary bugs (iterator leak, NULL state dereference). Start here to understand the problem. |
| [plan.html](plan.html) | HTML rendering of `plan.md` (generated via pandoc). Same content with clickable hyperlinks, suitable for viewing in a browser. |
| [nvidia-graphics-drivers-550-debian-sources.patch](nvidia-graphics-drivers-550-debian-sources.patch) | Git-format patch against the [Debian nvidia-graphics-drivers](https://salsa.debian.org/nvidia-team/nvidia-graphics-drivers) source package. Contains both fix patches (0084 + 0085), updates to `debian/patches/module/series`, and a changelog entry. Apply this to build a fixed `.deb`. |
| [conftest.sh.patched](conftest.sh.patched) | The full `conftest.sh` from `kernel-open/conftest.sh` (tag 550.163.01) with the fix already applied: prerequisite DRM headers (`drm_device.h`, `drm_mode_config.h`) are included before `drm_connector.h`, and a `return 0;` is added to avoid `-Werror=return-type`. |
| [nvidia-drm-drv.c.patched](nvidia-drm-drv.c.patched) | The full `kernel-open/nvidia-drm/nvidia-drm-drv.c` (tag 550.163.01) with error-handling fixes applied: `goto done` replaced with `break` in the connector loop, explicit error check after iterator end, and NULL guards on `drm_atomic_state_put`/`drm_atomic_state_free`. |

## How to use

### Option A: Patch DKMS sources directly

Back up the original files first, then copy the patched versions into the DKMS source tree and rebuild:

```bash
sudo -s

# Copy patched files to the DKMS source tree
cp -v conftest.sh.patched /usr/src/nvidia-current-550.163.01/
cp -v nvidia-drm-drv.c.patched /usr/src/nvidia-current-550.163.01/nvidia-drm/

cd /usr/src/nvidia-current-550.163.01

# Back up originals
mv -i conftest.sh conftest.sh.orig
mv -i nvidia-drm/nvidia-drm-drv.c nvidia-drm/nvidia-drm-drv.c.orig

# Put patched files in place (-i prompts before overwriting)
mv -i conftest.sh.patched conftest.sh
mv -i nvidia-drm/nvidia-drm-drv.c.patched nvidia-drm/nvidia-drm-drv.c

# Rebuild the DKMS module
dkms remove nvidia-current/550.163.01 --all
dkms install nvidia-current/550.163.01
update-initramfs -u  # or: dracut -f
reboot
```

To revert, restore the backups:

```bash
sudo -s
cd /usr/src/nvidia-current-550.163.01
mv -i conftest.sh.orig conftest.sh
mv -i nvidia-drm/nvidia-drm-drv.c.orig nvidia-drm/nvidia-drm-drv.c

# Rebuild the DKMS module
dkms remove nvidia-current/550.163.01 --all
dkms install nvidia-current/550.163.01
update-initramfs -u  # or: dracut -f
reboot
```

### Option B: Apply the Debian source patch

If you maintain a local build of the Debian `nvidia-graphics-drivers` package:

```bash
cd nvidia-graphics-drivers
git am nvidia-graphics-drivers-550-debian-sources.patch
dpkg-buildpackage -us -uc
```

### Verification

After rebooting with the patched module:

```bash
# Conftest should now detect the struct:
grep NV_DRM_CONNECTOR_LIST_ITER /var/lib/dkms/nvidia-current/550.163.01/build/conftest/types.h
# Expected: #define NV_DRM_CONNECTOR_LIST_ITER_PRESENT

# WARNING should no longer appear:
dmesg | grep nv_drm_revoke_modeset_permission
# Expected: no output
```

---

*Analysis and patches created with assistance from Claude Opus 4.6 (Anthropic) via Cursor IDE.*
