Re: [PATCH v5 08/21] KVM: selftests: Add helpers to write proc IRQ affinity for IRQ test
From: Sean Christopherson
Date: Thu Jun 04 2026 - 15:35:43 EST
On Thu, Jun 04, 2026, Josh Hilke wrote:
> diff --git a/tools/testing/selftests/kvm/lib/proc_util.c b/tools/testing/selftests/kvm/lib/proc_util.c
> index 84d30f055a0a..938c90d94f4d 100644
> --- a/tools/testing/selftests/kvm/lib/proc_util.c
> +++ b/tools/testing/selftests/kvm/lib/proc_util.c
> @@ -38,3 +38,23 @@ unsigned int vfio_msix_to_host_irq(const char *device_bdf, int msix)
> return (unsigned int)irq;
> }
>
> +FILE *open_proc_irq_smp_affinity_list(unsigned int irq)
> +{
> + char path[PATH_MAX];
> + FILE *fp;
> +
> + snprintf(path, sizeof(path), "/proc/irq/%u/smp_affinity_list", irq);
> + fp = fopen(path, "w");
> + TEST_ASSERT(fp, "fopen(%s) failed", path);
> +
> + return fp;
> +}
> +
> +void write_proc_irq_smp_affinity_list(FILE *fp, unsigned int irq, int irq_cpu)
> +{
> + int ret;
> +
> + ret = fprintf(fp, "%d\n", irq_cpu);
> + TEST_ASSERT(ret > 0, "Failed to affinitize IRQ-%u to CPU %d", irq, irq_cpu);
> + fflush(fp);
As Sashiko points out, this is prone to failing silently. But rather than just
"fix" that issue, this can be simplified to avoid the complexity of flushing
buffers in the first place.
That will also allow fixing another flaw: if the caller passes in the wrong,
file, debugging the resulting failure will be annoying. I see no reason to
risk that, it's not like opening a single file is going to meaningfully impact
overall runtime.
All in all, just this?
void proc_irq_set_smp_affinity(unsigned int irq, int cpu)
{
char path[PATH_MAX];
int r, fd;
snprintf(path, sizeof(path), "/proc/irq/%u/smp_affinity_list", irq);
fd = open(path, O_RDWR);
TEST_ASSERT(fd >= 0, "Failed to open %s", path);
r = dprintf(fd, "%d\n", cpu);
TEST_ASSERT(r > 0, "Failed to affinitize IRQ-%u to CPU %d", irq, cpu);
kvm_close(fd);
}