WindowServer and kerneltask using a lot of CPU in Big Sur

I will copy my answer from WindowServer high CPU usage, spikes when moving cursor on M1 MacBook Big Sur, as the problem and probable cause appear to be the same:

Big Sur has many known issues related to UI performance. It’s almost certain that you’re experiencing one of these bugs, and so you’re unlikely to be able to do much about it yourself. We can try to confirm or refute this hypothesis if you’d like.

Does this occur if you’re running in Safe Mode? (Shift during boot) If so, do the following in Safe Mode. Otherwise, do it when the problem occurs.

  1. Run the following command to enable kernel symbolication: sudo nvram boot-args="keepsyms=1" (this may require you to disable SIP first).
  2. Reboot your machine and don’t open any apps except for Terminal.
  3. Do the minimum required in order to reproduce the problem.
  4. In Terminal, run the following, making sure you’re only running it during the window of time that you experience the system slowdown. (In particular, do NOT have Activity Monitor running when you do this.): sudo spindump -reveal -noProcessingWhileSampling
  5. After waiting for 10 seconds to collect a sample and another minute or two to symbolicate and format, you’ll get a file in /tmp/spindump.txt that contains a stackshot of every process.
  6. Upload the file to PasteBin or some equivalent place and we can take a look to see what’s causing the CPU usage spikes.