Enable atomic modesetting by default
Subsystems (3)
Lyude Paul
maintainerv5 cover Enable atomic modesetting by default
Nouveau is one of the very few modern hardware drivers in the kernel that doesn't have atomic modesetting enabled by default, in part because when it was originally written by Ben there wasn't much in the way of good atomic modesetting clients to actually test things out with.
Nowadays however, atomic modesetting is very much the norm - and support in userspace for non-atomic drivers is starting to bitrot a bit - leading to its own set of issues. At the same time, many of those issues are fixed by just turning on atomic in nouveau. Plus, I've been running nouveau with atomic modesetting on by default for most of the machines I work on, and I've already fixed quite a number of issues to the point where things seem quite stable (excluding a single screen flashing bug on my desktop, which I am not particularly convinced has anything to do with atomic modesetting).
This patch series enables atomic modesetting by default, at least for NV50 and newer. NV04 is left unchanged, as atomic modesetting was never implemented for it. This series also enforces leaving atomic modesetting disabled on <NV50, and adds the value of nouveau.atomic to our kernel debug output.
Previous version of this patch series:
https://patchwork.freedesktop.org/series/171310/
Lyude Paul (5):
drm/nouveau: Fix cleanup bug in nouveau_drm_device_new() drm/nouveau: Print the nouveau.atomic parameter in
nouveau_display_options()
drm/nouveau: Fix drm_driver struct/nouveau.atomic parameter handling drm/nouveau/kms: Only allow enabling atomic modesetting on nv50+ drm/nouveau/kms/nv50-: Enable atomic modesetting by default
drivers/gpu/drm/nouveau/nouveau_drm.c | 40 ++++++++++++++------------- drivers/gpu/drm/nouveau/nouveau_drv.h | 1 + 2 files changed, 22 insertions(+), 19 deletions(-)
base-commit: 35b6cf07851a2b430b0eefbcb7b5a856f9030264
Signature · 1 line
2.55.0
Lyude Paul
maintainerPATCH v5 1/5 drm/nouveau: Fix cleanup bug in nouveau_drm_device_new()
Sashiko caught this while reviewing the patches for enabling atomic by default - if we fail to allocate the DRM device pointer, we'll attempt to free the error pointer that it returns rather than the actual struct. Let's fix this while we're at it.
---
V3:
* Don't use devm, that will just break during unbind (Sashiko). V5:
* Fix silly rebasing error
* Go back to the old style of error handling since we're not using a second allocation for drm_driver anymore.
drivers/gpu/drm/nouveau/nouveau_drm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +2 -1
@@ -749,7 +749,8 @@ nouveau_drm_device_new(const struct drm_driver *drm_driver, struct device *paren drm->dev =drm_dev_alloc (drm_driver, parent);if (IS_ERR (drm->dev)) { ret =PTR_ERR (drm->dev); - goto done; +kfree (drm); +return ERR_PTR (ret); } drm->dev->dev_private = drm;
Signature · 1 line
2.55.0
Lyude Paul
maintainerPATCH v5 2/5 drm/nouveau: Print the nouveau.atomic parameter in nouveau_display_options()
In reply to Lyude PaulSeems like we never remembered to start printing the value for this, so let's start to aid in troubleshooting in case we run into any issues with atomic.
--- drivers/gpu/drm/nouveau/nouveau_drm.c | 1 + 1 file changed, 1 insertion(+)
drivers/gpu/drm/nouveau/nouveau_drm.c +1 -0
@@ -1417,6 +1417,7 @@ static void nouveau_display_options(void)DRM_DEBUG_DRIVER ("... debug : %s\n" , nouveau_debug);DRM_DEBUG_DRIVER ("... noaccel : %d\n" , nouveau_noaccel);DRM_DEBUG_DRIVER ("... modeset : %d\n" , nouveau_modeset); +DRM_DEBUG_DRIVER ("... atomic : %d\n" , nouveau_atomic);DRM_DEBUG_DRIVER ("... runpm : %d\n" , nouveau_runtime_pm);DRM_DEBUG_DRIVER ("... vram_pushbuf : %d\n" , nouveau_vram_pushbuf);DRM_DEBUG_DRIVER ("... hdmimhz : %d\n" , nouveau_hdmimhz);
Signature · 1 line
2.55.0
Lyude Paul
maintainerPATCH v5 3/5 drm/nouveau: Fix drm_driver struct/nouveau.atomic parameter handling
In reply to Lyude PaulThe way we handled the nouveau.atomic module parameter before was fairly broken, and had a number of issues:
- It was only ever actually parsed in the case of PCI devices.
- When nouveau.atomic was enabled, it would add the cap for atomic modesetting to the global driver_pci structure. This meant that if one GPU on a system supported atomic and another didn't, it would still get enabled for both.
Looking into this exposed further silliness in the way that we actually handle the drm_driver struct. We have one global structure for platform devices, and another for PCI devices - both of which are literally identical.
So before we start preparing to enable atomic modesetting by default, let's fix this. Instead of sharing driver_pci and driver_platform, we move the drm_driver struct we use over to the nouveau_drm struct, and then copy the contents of driver_stub over to it. We then convert driver_stub to a const, and move the handling of the nouveau.atomic module parameter into nouveau_drm_device_new(), and conditionally add the atomic modesetting capability to the embedded drm_driver struct.
Doing this is also preferable, as the next step for enabling atomic modesetting by default will be ensuring that we don't enable it for legacy devices that still don't support it. This requires only checking the atomic modesetting module parameter after the NVIF device is ready, as this allows us to check the GPU family that nouveau is running on.
---
V2:
* s/driver_pci/drm_driver/
* Dynamically allocate drm_driver struct, get rid of duplicate global driver structs to fix another Sashiko issue.
V3:
* Don't use devm (sashiko)
V4:
* Don't return 0 by mistake (thanks C)
drivers/gpu/drm/nouveau/nouveau_drm.c | 27 +++++++++++---------------- drivers/gpu/drm/nouveau/nouveau_drv.h | 1 + 2 files changed, 12 insertions(+), 16 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +11 -16
@@ -111,9 +111,7 @@ MODULE_PARM_DESC(runpm, "disable (0), force enable (1), optimus only default (-1static int nouveau_runtime_pm =-1 ;module_param_named (runpm, nouveau_runtime_pm, int,0400 ); -static struct drm_driver driver_stub; -static struct drm_driver driver_pci; -static struct drm_driver driver_platform; +static const struct drm_driver driver_stub;#ifdef CONFIG_DEBUG_FS struct dentry *nouveau_debugfs_root;
@@ -727,8 +725,7 @@ nouveau_drm_device_del(struct nouveau_drm *drm) }static struct nouveau_drm * -nouveau_drm_device_new (const struct drm_driver *drm_driver,struct device *parent, -struct nvkm_device *device) +nouveau_drm_device_new (struct device *parent,struct nvkm_device *device) {static const struct nvif_mclass mmus[] = {
@@ -744,9 +741,10 @@ nouveau_drm_device_new(const struct drm_driver *drm_driver, struct device *parenif (!drm)return ERR_PTR (-ENOMEM ); + drm->drm_driver = driver_stub; drm->nvkm = device; - drm->dev =drm_dev_alloc (drm_driver, parent); + drm->dev =drm_dev_alloc (&drm->drm_driver, parent);if (IS_ERR (drm->dev)) { ret =PTR_ERR (drm->dev);kfree (drm);
@@ -771,6 +769,9 @@ nouveau_drm_device_new(const struct drm_driver *drm_driver, struct device *paren goto done; } +if (nouveau_atomic) + drm->drm_driver.driver_features |=DRIVER_ATOMIC ; + ret =nvif_device_map (&drm->device);if (ret) {NV_ERROR (drm,"Failed to map PRI: %d\n" , ret);
@@ -874,16 +875,13 @@ static int nouveau_drm_probe(struct pci_dev *pdev,return ret;/* Remove conflicting drivers (vesafb, efifb etc). */ - ret =aperture_remove_conflicting_pci_devices (pdev, driver_pci.name); + ret =aperture_remove_conflicting_pci_devices (pdev, driver_stub.name);if (ret) goto fail_nvkm;pci_set_master (pdev); -if (nouveau_atomic) - driver_pci.driver_features |=DRIVER_ATOMIC ; - - drm =nouveau_drm_device_new (&driver_pci, &pdev->dev, device); + drm =nouveau_drm_device_new (&pdev->dev, device);if (IS_ERR (drm)) { ret =PTR_ERR (drm); goto fail_nvkm;
@@ -1360,7 +1358,7 @@ nouveau_driver_fops = { .fop_flags = FOP_UNSIGNED_OFFSET, }; -static struct drm_driver +static const struct drm_driver driver_stub = { .driver_features =DRIVER_GEM | DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE |
@@ -1457,7 +1455,7 @@ nouveau_platform_device_create(const struct nvkm_device_tegra_func *func,if (err) goto err_free; - drm =nouveau_drm_device_new (&driver_platform, &pdev->dev, *pdevice); + drm =nouveau_drm_device_new (&pdev->dev, *pdevice);if (IS_ERR (drm)) { err =PTR_ERR (drm); goto err_free;
@@ -1482,9 +1480,6 @@ nouveau_drm_init(void) {int ret; - driver_pci = driver_stub; - driver_platform = driver_stub; -nouveau_display_options ();if (nouveau_modeset ==-1 ) {
drivers/gpu/drm/nouveau/nouveau_drv.h +1 -0
@@ -216,6 +216,7 @@ struct nouveau_drm {struct nouveau_cli client;struct drm_device *dev; +struct drm_driver drm_driver;struct list_head clients;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 3/5] drm/nouveau: Fix drm_driver struct/nouveau.atomic parameter handling
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Critical] Embedding drm_driver inside nouveau_drm causes a Use-After-Free of dev->driver in the DRM core when the device is unbound or hot-unplugged while userspace holds an open file descriptor.
Pre-existing issues:
- [Critical] The NOUVEAU_NVIF ioctl bypasses DRM core unplug checks, leading to a Use-After-Free on nouveau_cli if executed after the device is removed. --
--- Patch [3]: [PATCH v5 3/5] drm/nouveau: Fix drm_driver struct/nouveau.atomic parameter handling --- commit 04d65df2ae87442da9882dee1a1a2dd86491acc9
Author: Lyude Paul <[email protected]>
drm/nouveau: Fix drm_driver struct/nouveau.atomic parameter handling
This commit moves the global drm_driver structures for PCI and platform devices into the nouveau_drm struct. It initializes the embedded driver struct by copying a const stub and dynamically adds the atomic modesetting capability.
Quoted 4 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +0
-0
[ ... ]
drivers/gpu/drm/nouveau/nouveau_drm.c nouveau_drm_device_new(const struct drm_driver *drm_driver, struct device *paren · line 747
Quoted 9 lines
drivers/gpu/drm/nouveau/nouveau_drm.c +2
-1@@ -744,9 +741,10 @@ nouveau_drm_device_new(const struct drm_driver *drm_driver, struct device *parenif (!drm)return ERR_PTR (-ENOMEM ); + drm->drm_driver = driver_stub; drm->nvkm = device; - drm->dev =drm_dev_alloc (drm_driver, parent); + drm->dev =drm_dev_alloc (&drm->drm_driver, parent);
[Severity: Critical]
Does embedding the drm_driver inside the nouveau_drm struct cause a use-after-free of dev->driver during device unbind?
When the device is unbound via sysfs or hot-unplugged, the driver's remove routine calls nouveau_drm_device_del(), which drops its reference to drm_device (via drm_dev_put()) and unconditionally frees the nouveau_drm structure, along with the embedded drm_driver.
However, userspace might still hold an open file descriptor, keeping the drm_device alive. Later, when the file is closed, the DRM core's drm_file_free() dereferences dev->driver->postclose:
drivers/gpu/drm/drm_file.c:drm_file_free() {
...
if (dev->driver->postclose)
dev->driver->postclose(dev, file);
...
}Since dev->driver points to the freed embedded structure, does this result in a use-after-free?
[Severity: Critical]
This is a pre-existing issue, but does the NOUVEAU_NVIF ioctl bypass DRM core unplug checks and cause a use-after-free on nouveau_cli?
In nouveau_drm_ioctl(), this specific command routes directly to the ABI16 handler, bypassing drm_ioctl() and its drm_dev_is_unplugged() check:
drivers/gpu/drm/nouveau/nouveau_drm.c:nouveau_drm_ioctl() {
...
case DRM_NOUVEAU_NVIF:
ret = nouveau_abi16_ioctl(filp, (void __user *)arg, _IOC_SIZE(cmd));
break;
...
}If the device is unbound or unplugged, nouveau_drm_device_fini() iterates over drm->clients and frees all cli structures:
drivers/gpu/drm/nouveau/nouveau_drm.c:nouveau_drm_device_fini() {
...
list_for_each_entry_safe(cli, temp_cli, &drm->clients, head) {
list_del(&cli->head);
...
kfree(cli);
}
...
}If a userspace process then calls the DRM_NOUVEAU_NVIF ioctl, nouveau_abi16_ioctl() calls nouveau_abi16_get(), which retrieves the freed cli struct from file->driver_priv and attempts to lock its mutex:
drivers/gpu/drm/nouveau/nouveau_abi16.c:nouveau_abi16_get() {
...
struct nouveau_cli *cli = nouveau_cli(file_priv);
mutex_lock(&cli->mutex);
...
}Can this lead to memory corruption if executed after the device is removed?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Dave Airlie
maintainerRe: [PATCH v5 3/5] drm/nouveau: Fix drm_driver struct/nouveau.atomic parameter handling
Quoted 5 lines · On Fri, 31 Jul 2026 at 07:13, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Critical] Embeddingdrm_driverinsidenouveau_drmcauses a Use-After-Free ofdev->driverin the DRM core when the device is unbound or hot-unplugged while userspace holds an open file descriptor.
I'm going to agree with sashiko here. We don't want to embed it.
Just create two copies of the struct, one with atomic and one without, or use some macros to do that, because anything else is just going to have messy lifetimes.
I think you could also move the atomic flag handling earlier.
Dave.
Lyude Paul
maintainerPATCH v5 4/5 drm/nouveau/kms: Only allow enabling atomic modesetting on nv50+
In reply to Lyude PaulAtomic modesetting support was never added for pre-nv50 chipsets, so make sure we don't allow it to be forced on. Additionally, print a small warning when it's not supported.
---
V2:
* Check against info.family, not info.chipset
drivers/gpu/drm/nouveau/nouveau_drm.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +6 -2
@@ -769,8 +769,12 @@ nouveau_drm_device_new(struct device *parent, struct nvkm_device *device) goto done; } -if (nouveau_atomic) - drm->drm_driver.driver_features |=DRIVER_ATOMIC ; +if (nouveau_atomic) { +if (drm->device.info.family >=NV_DEVICE_INFO_V0_TESLA ) + drm->drm_driver.driver_features |=DRIVER_ATOMIC ; +else +NV_WARN (drm,"Atomic modesetting not supported (needs nv50+)\n" ); + } ret =nvif_device_map (&drm->device);if (ret) {
Signature · 1 line
2.55.0
Lyude Paul
maintainerPATCH v5 5/5 drm/nouveau/kms/nv50-: Enable atomic modesetting by default
In reply to Lyude PaulNouveau is one of the very few modern hardware drivers in the kernel that doesn't have atomic modesetting enabled by default, in part because when it was originally written by Ben there wasn't much in the way of good atomic modesetting clients to actually test things out with.
Nowadays however, atomic modesetting is very much the norm - and support in userspace for non-atomic drivers is starting to bitrot a bit - leading to its own set of issues. At the same time, many of those issues are fixed by just turning on atomic in nouveau. Plus, I've been running nouveau with atomic modesetting on by default for most of the machines I work on, and I've already fixed quite a number of issues to the point where things seem quite stable (excluding a single screen flashing bug on my desktop, which I am not particularly convinced has anything to do with atomic modesetting).
Now that we've protected against breaking things for chipsets where atomic isn't supported (<nv50) - let's enable it by default on generations of hardware that support it. We'll leave the module parameter around for the time being, as turning it off may be helpful in the possibility that we hit regressions.
--- drivers/gpu/drm/nouveau/nouveau_drm.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/gpu/drm/nouveau/nouveau_drm.c +5 -4
@@ -103,8 +103,9 @@ MODULE_PARM_DESC(modeset, "enable driver (default: auto, "int nouveau_modeset =-1 ;module_param_named (modeset, nouveau_modeset, int,0400 ); -MODULE_PARM_DESC (atomic,"Expose atomic ioctl (default: disabled)" ); -static int nouveau_atomic =0 ; +MODULE_PARM_DESC (atomic,"Expose atomic ioctl (default: auto, " +"0 = disabled, 1 = enabled)" ); +static int nouveau_atomic =-1 ;module_param_named (atomic, nouveau_atomic, int,0400 );MODULE_PARM_DESC (runpm,"disable (0), force enable (1), optimus only default (-1)" );
@@ -769,10 +770,10 @@ nouveau_drm_device_new(struct device *parent, struct nvkm_device *device) goto done; } -if (nouveau_atomic) { +if (nouveau_atomic !=0 ) {if (drm->device.info.family >=NV_DEVICE_INFO_V0_TESLA ) drm->drm_driver.driver_features |=DRIVER_ATOMIC ; -else +else if (nouveau_atomic ==1 )NV_WARN (drm,"Atomic modesetting not supported (needs nv50+)\n" ); }
Signature · 1 line
2.55.0