Bug 459609

Summary: KWin switches the discrete GPU on every time a new Wayland window is opened in a switchable graphics setup
Product: [Plasma] kwin Reporter: Prajna Sariputra <putr4.s>
Component: platform-drmAssignee: KWin default assignee <kwin-bugs-null>
Status: RESOLVED UPSTREAM    
Severity: normal CC: nate, xaver.hugl
Priority: NOR Keywords: wayland-only
Version First Reported In: 5.25.5   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Prajna Sariputra 2022-09-24 15:45:45 UTC
SUMMARY
With dynamic switchable graphics set up on my system with Nouveau KWin appears to poke the dGPU every time a native Wayland window is opened (including Dolphin, System Settings, Konsole, KRunner and the logout/shutdown screen at least), my laptop's dGPU light indicator turns on and there is a delay (about 2s) before the window appears. If the dGPU is already on and/or I force the dGPU to stay off then there is no delay, and the issue does not occur with XWayland windows (for example if I start Dolphin with `QT_QPA_PLATFORM=xcb` set) nor with an X11 session. The dGPU does turn off again after a few seconds.

While I suspect the delay part could just be something specific to my system I don't think KWin should even touch the dGPU unless said app is meant to use it (as it would use battery power unnecessarily), and when I tried this same setup with GNOME Wayland (I think it was with Fedora 34) it did not turn on the dGPU when not required other than on startup.


STEPS TO REPRODUCE
1. Start a Plasma Wayland session
2. Wait until the dGPU turns off (say a minute or so)
3. Start a Wayland native app that does not use the dGPU (say Dolphin or Konsole)
4. Close said app and reopen it

OBSERVED RESULT
There is a significant delay before the app window appears in step 4 instead of being quick, and the dGPU turns on despite not being required.

EXPECTED RESULT
The app opens immediately in step 4.

SOFTWARE/OS VERSIONS
Operating System: Arch Linux
KDE Plasma Version: 5.25.5
KDE Frameworks Version: 5.98.0
Qt Version: 5.15.6
Kernel Version: 5.19.9-arch1-1 (64-bit)
Graphics Platform: Wayland
Processors: 8 × Intel® Core™ i7-6700HQ CPU @ 2.60GHz
Memory: 15.5 GiB of RAM
Graphics Processor: Mesa Intel® HD Graphics 530 (plus a GTX 960M as the dGPU)
Manufacturer: Micro-Star International Co., Ltd.
Product Name: GP62 6QF
System Version: REV:1.0

ADDITIONAL INFORMATION
In addition to the delay and the light indicator the file `/sys/kernel/debug/vgaswitcheroo/switch` also says "DynPwr" instead of "DynOff" for the dGPU when the issue occurs.

Also, I did not try the proprietary driver in this scenario since it does not support power management for my old dGPU, plus I was not able to get any Wayland native apps to use the dGPU without crashing in that case anyway (I assume that could be a configuration error on my end).
Comment 1 Prajna Sariputra 2022-09-25 08:41:13 UTC
After further investigation it appears that it is just about anything that wants an EGL context that triggers the issue rather than anything Wayland related, since `eglinfo` also turns on the dGPU, and GTK3 apps (like `gtk3-demo`) don't touch the dGPU at all unless I try the OpenGL example in it (which I guess is why I never noticed the issue when I was using GNOME, as it was when most apps still use GTK3).

So, at this point I'm not sure if I should report this issue elsewhere (Mesa?) or if it is just how EGL is designed and there is no way around it, any advice would be appreciated.
Comment 2 Zamundaaa 2022-09-26 21:26:56 UTC
KWin doesn't touch a GPU unless it's used for compositing or an output is attached. Most likely, something is probing all the GPUs on application startup (and eglinfo is doing that as well, but there it's intentional), instead of only picking the primary one.
Do you have nvidia-utils installed? It caused this behavior for someone else before: https://www.reddit.com/r/kde/comments/u5qtjy/when_using_qt_programs_in_wayland_they_have_to/
Comment 3 Prajna Sariputra 2022-09-27 01:02:28 UTC
(In reply to Zamundaaa from comment #2)
> KWin doesn't touch a GPU unless it's used for compositing or an output is
> attached. Most likely, something is probing all the GPUs on application
> startup (and eglinfo is doing that as well, but there it's intentional),
> instead of only picking the primary one.
> Do you have nvidia-utils installed? It caused this behavior for someone else
> before:
> https://www.reddit.com/r/kde/comments/u5qtjy/
> when_using_qt_programs_in_wayland_they_have_to/

Ah, yes I do have nvidia-utils installed, however in my case I do actually need it there (occasionally if I need to use the dGPU properly I will switch to a Plasma X11 session running on said dGPU plus the proprietary driver, used to use bbswitch instead of nouveau for but that causes other issues I need to work around).

Fortunately, it appears that the thing with `/usr/bin/nvidia-modprobe` in that post is just a symptom, the true cause is that libglvnd when using EGL will load all installed ICDs[1], including the NVIDIA one in my case, which is what was poking the dGPU. So, aside from just getting rid of nvidia-utils another way to solve the issue is to set the `__EGL_VENDOR_LIBRARY_FILENAMES` environment variable to only have the Mesa ICD like so: `__EGL_VENDOR_LIBRARY_FILENAMES=/usr/share/glvnd/egl_vendor.d/50_mesa.json`.

Anyway, thanks for pointing me in the right direction! Also, I am setting the resolution to "not a bug" since it's clearly not a KDE issue, but maybe "upstream" could be a better fit since it's the proprietary driver messing about when it shouldn't?

[1]: https://gitlab.freedesktop.org/glvnd/libglvnd/-/blob/master/src/EGL/icd_enumeration.md