From e2e9f531948e6a5725c2969f265f2cbcc9194059 Mon Sep 17 00:00:00 2001 From: Mattia Tadini Date: Sun, 15 Nov 2026 00:59:24 +0100 Subject: [PATCH] gpu-freq-sampler: stop writing to amdgpu's SMU mailbox The sampler sent 0x0E/0x0F on "queue 0" through the SMN window, at 0x03B10A08/48/68. Those are C2PMSG_66/82/90, the mailbox amdgpu itself uses through MMIO under its own mutex, which knows nothing about our flock. Every 2 s it cleared the response register and wrote its own command, which can land in the middle of a driver message such as the metrics table export. The bc250_smu library refuses queue 0 by default for the same reason. It is not needed either: patch 0125 has the driver answer current_gfxclk with QueryGfxclk under its lock, so gpu_metrics offset 76, hwmon freq1_input and pp_dpm_sclk all carry the real clock (checked on 7.3.4, same value as the old mailbox read). The sampler now takes the clock and the three power figures from the one gpu_metrics read it already did, and opens nothing in PCI config space. The unit's path condition follows: gpu_metrics instead of the root device's config space. Comments that described the old behaviour are updated, and translated where touched. Tests: tests/test_gpu_freq_sampler.py keeps the SMN window and the queue-0 addresses out of the code and checks the published values. --- apps/control-center/sfcc/pagine/monitor.py | 6 +- .../systemd/system/skillfish-gpu-freq.service | 4 +- .../skillfish/gddr6/bc250_smu/transport.py | 5 +- .../usr/local/bin/skillfish-gpu-freq-sampler | 223 +++++++----------- system/usr/local/bin/skillfish-hud-val | 8 +- tests/test_gpu_freq_sampler.py | 72 ++++++ 6 files changed, 166 insertions(+), 152 deletions(-) create mode 100644 tests/test_gpu_freq_sampler.py diff --git a/apps/control-center/sfcc/pagine/monitor.py b/apps/control-center/sfcc/pagine/monitor.py index f121a091..25306eaf 100644 --- a/apps/control-center/sfcc/pagine/monitor.py +++ b/apps/control-center/sfcc/pagine/monitor.py @@ -86,9 +86,9 @@ # rispondere, le letture hwmon si piantano nel kernel, fand sfonda il suo # watchdog e il watchdog hardware resetta la macchina. # -# La tabella la legge UNO SOLO, skillfish-gpu-freq-sampler, che alla SMU parla -# gia' ogni due secondi, e pubblica i tre numeri qui. Leggere questo file non -# costa niente e non parla con nessuno. +# Exactly one program reads the table, skillfish-gpu-freq-sampler, every two +# seconds, and publishes the three numbers here. Reading this file costs nothing +# and talks to nobody. POTENZE = "/run/skillfish-potenze" # se il campionatore non gira, un numero fermo da un'ora e' peggio di nessuno POTENZE_SCADENZA = 10.0 diff --git a/system/etc/systemd/system/skillfish-gpu-freq.service b/system/etc/systemd/system/skillfish-gpu-freq.service index 24d2860f..2895d02f 100644 --- a/system/etc/systemd/system/skillfish-gpu-freq.service +++ b/system/etc/systemd/system/skillfish-gpu-freq.service @@ -1,7 +1,7 @@ [Unit] -Description=SkillFishOS — GPU gfxclk sampler (SMU) for the HUD after the 8-core unlock +Description=SkillFishOS — GPU gfxclk and power sampler (one gpu_metrics reader for the HUD and Monitor) After=cyan-skillfish-governor.service -ConditionPathExists=/sys/bus/pci/devices/0000:00:00.0/config +ConditionPathExists=/sys/class/drm/card0/device/gpu_metrics [Service] Type=simple diff --git a/system/usr/lib/skillfish/gddr6/bc250_smu/transport.py b/system/usr/lib/skillfish/gddr6/bc250_smu/transport.py index 238d6547..681a34f4 100644 --- a/system/usr/lib/skillfish/gddr6/bc250_smu/transport.py +++ b/system/usr/lib/skillfish/gddr6/bc250_smu/transport.py @@ -9,8 +9,9 @@ class Bc250PciTransport: ⚠️ AN SMN ACCESS IS TWO SEPARATE WRITES: the address goes into config 0xB8, the value into 0xBC. Anything else driving the same window can land between them, and then our address takes somebody else's value - the SMU is handed a - command nobody sent. On this distribution skillfish-gpu-freq-sampler reads - the GPU clock through that very window every two seconds, and a malformed + command nobody sent. On this distribution the core unlock drives the same + window at boot (and skillfish-gpu-freq-sampler did, every two seconds, until + 26.11.2), and a malformed Q3/5 with a chip index out of range is enough to leave the SMU's handler spinning for ever: amdgpu stops getting its metrics table, the GPU sensors disappear, and only a reboot brings them back. diff --git a/system/usr/local/bin/skillfish-gpu-freq-sampler b/system/usr/local/bin/skillfish-gpu-freq-sampler index ec772bcb..87442966 100755 --- a/system/usr/local/bin/skillfish-gpu-freq-sampler +++ b/system/usr/local/bin/skillfish-gpu-freq-sampler @@ -1,165 +1,106 @@ #!/usr/bin/env python3 -# SkillFishOS — GPU gfxclk sampler. +# SkillFishOS — GPU gfxclk and power sampler. # -# After the BC-250 8-core unlock, amdgpu's derived GPU frequency (pp_dpm_sclk and -# gpu_metrics current_gfxclk) reads bogus (~100 MHz at idle instead of ~350). The -# SMU's own gfxclk getter stays correct, so this samples it directly (queue 0: -# msg 0x0E "request refresh" + 0x0F "read", result in the arg register) every 2 s -# and publishes MHz to /run/skillfish-gpu-freq. The HUD/Monitor read that file. +# Reads gpu_metrics ONCE every 2 s and publishes two small files that every other +# tool reads instead of the SMU: +# /run/skillfish-gpu-freq current gfxclk in MHz +# /run/skillfish-potenze "apu cpu gpu" in watts, "-" where the SMU says nothing # -# Concurrency: the GPU governor also touches the SMU. Reads are short and low-rate, -# and every sample is sanity-checked (50..2500 MHz) — a rare collision just keeps the -# previous value, it never publishes garbage and never blocks the governor. +# ⚠️ THIS PROGRAM MUST NOT TALK TO THE SMU MAILBOX ITSELF. Until 26.11.2 it sent +# 0x0E/0x0F on "queue 0" through the SMN window (PCI config 0xB8/0xBC of 00:00.0), +# at 0x03B10A08/48/68. Those are C2PMSG_66/82/90: THE SAME MAILBOX amdgpu uses, +# through MMIO and under its own mutex, which knows nothing about our flock. Every +# 2 s we cleared the response register and wrote our command; when amdgpu was in +# the middle of exporting the metrics table it found its answer gone and logged +# amdgpu: SMU: No response msg_reg: 6 resp_reg: 0 +# amdgpu: Failed to export SMU metrics table! +# On the .32 that happened on almost every boot from October on (up to 66 times), +# and the community guide reports the same signature freezing the display about +# one boot in three (15/11/2026). # -# ⚠️ THAT IS TRUE OF THE VALUE WE READ, AND NOT OF THE CARD. An SMN access is TWO -# writes to PCI config space — the address into 0xB8, the value into 0xBC — and -# any other program using that window can land between them. Then our address -# takes somebody else's value, or theirs takes ours, and the SMU is handed a -# command nobody sent. On 20/09/2026 that turned out to be how the GDDR6 memory -# collector, which drives the same window, ended up hanging the SMU outright: -# amdgpu then loses its metrics table, the GPU sensors go away, and only a reboot -# brings them back. +# It is not needed any more either: our kernel (patch 0125) answers current_gfxclk +# with QueryGfxclk, sent by the driver under its lock, so gpu_metrics, hwmon +# freq1_input and pp_dpm_sclk all carry the real clock (checked on 7.3.4: all +# 1850 MHz, same as the old mailbox read). # -# So every pair is taken under flock on the config file itself. The file is the -# lock: every tool that reaches the SMU has it open already, ours and anybody -# else's, so there is no lock path to agree on first. bc250_smu_oc upstream has -# the same flock but takes it around each single config access rather than around -# the pair, which leaves exactly the gap it was meant to close. +# ⚠️ EVERY READ OF gpu_metrics IS A MESSAGE TO THE SMU, and this board's SMU does +# not want many. On 20/09/2026 skillfish-hud-val read that file three times per +# call while conky, the dashboard sampler and the Monitor all called it: SMU +# traffic doubled and the board rebooted on its own three times in half an hour +# (SMU stops answering, hwmon hangs, fand misses its watchdog, the hardware +# watchdog resets). So exactly one program reads it, this one, and the others +# read the files above, which cost as much as a `cat`. # -# Register/primitive from bc250_smu (queue-0 mailbox) + rw-r-r-0644/bc250-core-unlock. -import fcntl, os, struct, time, sys - -CFG = "/sys/bus/pci/devices/0000:00:00.0/config" -Q0_CMD, Q0_RSP, Q0_ARG = 0x03B10A08, 0x03B10A68, 0x03B10A48 -DONE = {0x01, 0xFF, 0xFE, 0xFD, 0xFC} -OK = 0x01 -OUT = "/run/skillfish-gpu-freq" - -# --- i tre consumi, letti UNA VOLTA per tutti -------------------------------- -# ⚠️ OGNI LETTURA DI gpu_metrics E' UN MESSAGGIO ALLA SMU, e la SMU di questa -# scheda non ne vuole tanti. Il 20/09/2026 skillfish-hud-val leggeva quel file -# tre volte a ogni chiamata, e a chiamarlo c'erano conky ogni 2 s, il -# campionatore della dashboard e il Monitor: traffico verso la SMU raddoppiato, -# e la scheda si e' riavviata da sola TRE VOLTE in mezz'ora - SMU che smette di -# rispondere, hwmon che si pianta, fand che sfonda il suo watchdog e il watchdog -# hardware che resetta. -# -# Quindi lo legge uno solo: questo, che alla SMU ci parla gia' ogni due secondi. -# Tutti gli altri leggono il file qui sotto, che costa quanto un `cat`. Dieci -# lettori, un messaggio ogni due secondi invece di dieci al secondo. -# -# Gli offset sono misurati caricando un core alla volta, non letti in -# un'intestazione: 40 il pacchetto, 44 i core della CPU, 46 la GPU. 65535 vuol -# dire "non lo dichiaro" e non va mai stampato come 65 watt. -METRICHE = "/sys/class/drm/card0/device/gpu_metrics" -POTENZE = "/run/skillfish-potenze" -OFFSET = (40, 44, 46) # apu, cpu, gpu - - -def potenze(): - """Una riga: `apu cpu gpu` in watt, con `-` per quello che manca.""" +# Offsets are measured, not taken from a header: the 8-core unlock moves the +# per-core arrays (that is what patch 0006 is about). 40 the whole package, 44 the +# CPU cores, 46 the GPU, 76 current_gfxclk (gpu_metrics v2.x). 0xFFFF means "not +# reported" and must never be shown as 65 W. +import os, struct, sys, time + +METRICS = "/sys/class/drm/card0/device/gpu_metrics" +OUT_FREQ = "/run/skillfish-gpu-freq" +OUT_POWER = "/run/skillfish-potenze" +POWER_OFFSETS = (40, 44, 46) # apu, cpu, gpu +GFXCLK_OFFSET = 76 +GFXCLK_RANGE = (50, 2500) +INTERVAL = 2.0 + + +def read_metrics(): try: - with open(METRICHE, "rb") as f: + with open(METRICS, "rb") as f: d = f.read() except OSError: return None - fuori = [] - for o in OFFSET: - if o + 2 > len(d): - return None - v = int.from_bytes(d[o:o + 2], "little") - fuori.append("-" if v == 0xFFFF else "%.1f" % (v / 1000.0)) - return " ".join(fuori) - -if os.geteuid() != 0: - sys.exit("root required") -try: - fd = os.open(CFG, os.O_RDWR) -except FileNotFoundError: - sys.exit(0) # not a BC-250 - - -def rd(reg): - fcntl.flock(fd, fcntl.LOCK_EX) - try: - os.pwrite(fd, struct.pack("/dev/null)" # La SMU smette di rispondere, le letture hwmon si piantano nel kernel, fand # sfonda il suo watchdog e il watchdog hardware resetta la macchina. # -# La tabella la legge UNO SOLO: skillfish-gpu-freq-sampler, che alla SMU parla -# gia' ogni due secondi, e pubblica i tre numeri in /run/skillfish-potenze. Qui -# si fa un `cut`, che non costa niente e non parla con nessuno. +# Exactly one program reads the table, skillfish-gpu-freq-sampler, every two +# seconds, and publishes the three numbers in /run/skillfish-potenze. Here we +# just `cut`, which costs nothing and talks to nobody. # # ⚠️ E si guarda quanto e' vecchio il file: se il campionatore non gira, un # numero fermo da un'ora e' peggio di nessun numero. @@ -102,7 +102,7 @@ get(){ cpu_temp) v=$(cat "$(hw k10temp)/temp1_input" 2>/dev/null) [ -z "$v" ] && v=$(cpu_temp_generico) [ -n "$v" ] && echo "$v" | awk '{printf "%.0f",$1/1000}' ;; - gpu_freq) f=$(cat /run/skillfish-gpu-freq 2>/dev/null) # SMU sample (correct after 8-core unlock) + gpu_freq) f=$(cat /run/skillfish-gpu-freq 2>/dev/null) # current_gfxclk, published by the sampler if [ -n "$f" ]; then echo "${f}Mhz" else # AMD: il livello attivo e' quello segnato con l'asterisco diff --git a/tests/test_gpu_freq_sampler.py b/tests/test_gpu_freq_sampler.py new file mode 100644 index 00000000..96a11e35 --- /dev/null +++ b/tests/test_gpu_freq_sampler.py @@ -0,0 +1,72 @@ +"""skillfish-gpu-freq-sampler: one gpu_metrics reader, and no SMU mailbox of its own. + +Until 26.11.2 the sampler sent messages on the SMU's queue 0 through the SMN +window, which is the mailbox amdgpu itself uses: it raced the driver and caused +"Failed to export SMU metrics table!". These tests keep it from coming back and +check the values it publishes from a gpu_metrics table. +""" +import importlib.machinery +import importlib.util +import pathlib +import struct + +SAMPLER = (pathlib.Path(__file__).resolve().parents[1] + / "system" / "usr" / "local" / "bin" / "skillfish-gpu-freq-sampler") + + +def _load(): + loader = importlib.machinery.SourceFileLoader("sampler_mod", str(SAMPLER)) + spec = importlib.util.spec_from_loader("sampler_mod", loader) + mod = importlib.util.module_from_spec(spec) + loader.exec_module(mod) # main() only runs under __main__ + return mod + + +def _table(gfxclk=1850, apu=29500, cpu=150, gpu=4060, fmt=2): + d = bytearray(128) + d[2], d[3] = fmt, 2 + struct.pack_into("