Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Alexandru Elisei <alexandru.elisei@arm.com>
To: Oliver Upton <oupton@kernel.org>
Cc: will@kernel.org, julien.thierry.kdev@gmail.com, maz@kernel.org,
	jean-philippe.brucker@arm.com, andre.przywara@arm.com,
	suzuki.poulose@arm.com, kvm@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev
Subject: Re: [PATCH kvmtool v2 7/7] arm64: Improve KVM_ARM_VCPU_PMU_V3_CTRL diagnostics
Date: Mon, 22 Jun 2026 10:04:32 +0100	[thread overview]
Message-ID: <ajj6oGxytFm2NE4T@raptor> (raw)
In-Reply-To: <ajRBpMnlcNkhWzQL@kernel.org>

Hi Oliver,

On Thu, Jun 18, 2026 at 12:06:12PM -0700, Oliver Upton wrote:
> On Thu, Jun 18, 2026 at 04:50:01PM +0100, Alexandru Elisei wrote:
> > kvmtool issues several ioctls when configuring the PMU, and each of them
> > can fail for different reasons. Be more specific about the ioctl that
> > failed when that happens.
> > 
> > Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
> > ---
> >  arm64/pmu.c | 31 ++++++++++++++++++++++++-------
> >  1 file changed, 24 insertions(+), 7 deletions(-)
> > 
> > diff --git a/arm64/pmu.c b/arm64/pmu.c
> > index 92cacd62479e..78c15f153fad 100644
> > --- a/arm64/pmu.c
> > +++ b/arm64/pmu.c
> > @@ -12,6 +12,24 @@
> >  
> >  #include "asm/pmu.h"
> >  
> > +static const char *pmu_attr_names[] = {
> > +	[KVM_ARM_VCPU_PMU_V3_IRQ]	= "KVM_ARM_VCPU_PMU_V3_IRQ",
> > +	[KVM_ARM_VCPU_PMU_V3_INIT]	= "KVM_ARM_VCPU_PMU_V3_INIT",
> > +	[KVM_ARM_VCPU_PMU_V3_FILTER]	= "KVM_ARM_VCPU_PMU_V3_FILTER",
> > +	[KVM_ARM_VCPU_PMU_V3_SET_PMU]	= "KVM_ARM_VCPU_PMU_V3_SET_PMU",
> > +	[KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS] = "KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTER",
> > +};
> > +
> > +static const char *pmu_get_attr_name(u64 attr)
> > +{
> > +	switch (attr) {
> > +	case KVM_ARM_VCPU_PMU_V3_IRQ ... KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS:
> > +		return pmu_attr_names[attr];
> > +	default:
> > +		return "UNKNOWN";
> > +	}
> > +}
> > +
> >  static bool pmu_has_attr(struct kvm_cpu *vcpu, u64 attr)
> >  {
> >  	struct kvm_device_attr pmu_attr = {
> > @@ -32,13 +50,12 @@ static void set_pmu_attr(struct kvm_cpu *vcpu, void *addr, u64 attr)
> >  	};
> >  	int ret;
> >  
> > -	if (pmu_has_attr(vcpu, attr)) {
> > -		ret = ioctl(vcpu->vcpu_fd, KVM_SET_DEVICE_ATTR, &pmu_attr);
> > -		if (ret)
> > -			die_perror("PMU KVM_SET_DEVICE_ATTR");
> > -	} else {
> > -		die_perror("PMU KVM_HAS_DEVICE_ATTR");
> > -	}
> > +	if (!pmu_has_attr(vcpu, attr))
> > +		die_perror("KVM_HAS_DEVICE_ATTR(%s)", pmu_get_attr_name(attr));
> > +
> > +	ret = ioctl(vcpu->vcpu_fd, KVM_SET_DEVICE_ATTR, &pmu_attr);
> > +	if (ret)
> > +		die_perror("KVM_SET_DEVICE_ATTR(%s)", pmu_get_attr_name(attr));
> >  }
> 
> The whole if (ret) die_perror(...) thing is a bit repetetive IMO. A
> treewide cleanup replacing this with macros would be nice, then you could
> stringize the ioctl under the hood.

Thank you for having a look, that's a great idea, it will avoid out of bounds
array access if KVM gets a new PMU ioctl and the name array is not updated at
the same time - that's quite possible since ioctl numbers are pulled by running
update_headers.sh, and the dependency is not obvious.

I think the compilation errors are a bit higher priority than this, and a
treewide change would more involved, possibly involving a change in behaviour
(the gic seems to propagate the error instead of calling die_perror(), for
example), would you mind if for this series I'll only introduce the macro for
the pmu and convert the rest of the code in a separate series?

Thanks,
Alex

> 
> diff --git a/arm64/pmu.c b/arm64/pmu.c
> index 5f31d6b..0d9f3df 100644
> --- a/arm64/pmu.c
> +++ b/arm64/pmu.c
> @@ -23,23 +23,19 @@ static bool pmu_has_attr(struct kvm_cpu *vcpu, u64 attr)
>  	return ret == 0;
>  }
>  
> -static void set_pmu_attr(struct kvm_cpu *vcpu, void *addr, u64 attr)
> -{
> -	struct kvm_device_attr pmu_attr = {
> -		.group	= KVM_ARM_VCPU_PMU_V3_CTRL,
> -		.addr	= (u64)addr,
> -		.attr	= attr,
> -	};
> -	int ret;
> -
> -	if (pmu_has_attr(vcpu, attr)) {
> -		ret = ioctl(vcpu->vcpu_fd, KVM_SET_DEVICE_ATTR, &pmu_attr);
> -		if (ret)
> -			die_perror("PMU KVM_SET_DEVICE_ATTR");
> -	} else {
> -		die_perror("PMU KVM_HAS_DEVICE_ATTR");
> -	}
> -}
> +#define kvm_set_device_attr(fd, _group, _attr, _addr)					\
> +do {											\
> +	struct kvm_device_attr __attr = {						\
> +		.group	= (_group),							\
> +		.attr	= (_attr),							\
> +		.addr	= (u64)(_addr),							\
> +	};										\
> +	int r;										\
> +											\
> +	r = ioctl((fd), KVM_SET_DEVICE_ATTR, &__attr);					\
> +	if (r)										\
> +		die_perror("KVM_SET_DEVICE_ATTR(group:"#_group", attr:"#_attr")");	\
> +} while (0)
>  
>  #define SYS_EVENT_SOURCE	"/sys/bus/event_source/devices/"
>  /*
> @@ -218,14 +214,18 @@ void pmu__generate_fdt_nodes(void *fdt, struct kvm *kvm)
>  
>  	for (i = 0; i < kvm->nrcpus; i++) {
>  		vcpu = kvm->cpus[i];
> -		set_pmu_attr(vcpu, &irq, KVM_ARM_VCPU_PMU_V3_IRQ);
> +		kvm_set_device_attr(vcpu->vcpu_fd, KVM_ARM_VCPU_PMU_V3_CTRL,
> +				    KVM_ARM_VCPU_PMU_V3_IRQ, &irq);
>  		/*
>  		 * PMU IDs 0-5 are reserved; a positive value means a PMU was
>  		 * found.
>  		 */
>  		if (pmu_id > 0)
> -			set_pmu_attr(vcpu, &pmu_id, KVM_ARM_VCPU_PMU_V3_SET_PMU);
> -		set_pmu_attr(vcpu, NULL, KVM_ARM_VCPU_PMU_V3_INIT);
> +			kvm_set_device_attr(vcpu->vcpu_fd, KVM_ARM_VCPU_PMU_V3_CTRL,
> +					    KVM_ARM_VCPU_PMU_V3_SET_PMU, &pmu_id);
> +
> +		kvm_set_device_attr(vcpu->vcpu_fd, KVM_ARM_VCPU_PMU_V3_CTRL,
> +				    KVM_ARM_VCPU_PMU_V3_INIT, NULL);
>  	}
>  
>  	_FDT(fdt_begin_node(fdt, "pmu"));


      reply	other threads:[~2026-06-22  9:04 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-18 15:49 [PATCH kvmtool v2 0/7] x86 compilation fixes and arm64 PMU improvements Alexandru Elisei
2026-06-18 15:49 ` [PATCH kvmtool v2 1/7] x86: Define bioscall only in 32-bit mode Alexandru Elisei
2026-06-18 15:49 ` [PATCH kvmtool v2 2/7] virtio: Do not modify const strings in virtio_9p_rootdir_parser() Alexandru Elisei
2026-06-18 15:49 ` [PATCH kvmtool v2 3/7] disk/core: Do not modify const strings in disk_img_name_parser() Alexandru Elisei
2026-06-18 15:49 ` [PATCH kvmtool v2 4/7] arm64: Initialise the PMU after the GIC Alexandru Elisei
2026-06-18 15:49 ` [PATCH kvmtool v2 5/7] util: Set exit status to errno in die_perror() Alexandru Elisei
2026-06-18 15:50 ` [PATCH kvmtool v2 6/7] util: Allow die_perror() to take a variable list of argument Alexandru Elisei
2026-06-18 15:50 ` [PATCH kvmtool v2 7/7] arm64: Improve KVM_ARM_VCPU_PMU_V3_CTRL diagnostics Alexandru Elisei
2026-06-18 19:06   ` Oliver Upton
2026-06-22  9:04     ` Alexandru Elisei [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ajj6oGxytFm2NE4T@raptor \
    --to=alexandru.elisei@arm.com \
    --cc=andre.przywara@arm.com \
    --cc=jean-philippe.brucker@arm.com \
    --cc=julien.thierry.kdev@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox