mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ashok Raj <ashok.raj@oss.qualcomm.com>
To: Ahmed Azeem <ahmed.azeem@arm.com>
Cc: linux-arm-kernel@lists.infradead.org,
	Sudeep Holla <sudeep.holla@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Lorenzo Pieralisi <lpieralisi@kernel.org>,
	linux-api@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, Divin Raj <divin.raj@arm.com>,
	Bertrand Marquis <Bertrand.Marquis@arm.com>,
	Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>,
	Ashok Raj <ashok.raj@oss.qualcomm.com>
Subject: Re: [PATCH RFC 3/4] firmware: arm: Add PFDI run, result, and error injection support
Date: Fri, 20 Nov 2026 19:52:28 -0800	[thread overview]
Message-ID: <awEVfD12XgmNhqxt@hu-ashoraj-lv.qualcomm.com> (raw)
In-Reply-To: <20261119-topics-ahmaze01-upstream-pfdi-rfc-v1-3-4772d18b9115@arm.com>

On Thu, Nov 19, 2026 at 03:36:05PM +0000, Ahmed Azeem wrote:
> From: Divin Raj <divin.raj@arm.com>
> 
> Extend the PFDI driver with ioctls to execute tests and manage results.
> 
> Add support for PFDI_IOCTL_PE_TEST_RUN, PFDI_IOCTL_PE_TEST_RESULT, and
> PFDI_IOCTL_FORCE_ERROR, along with per-CPU state to serialize test
> execution. When firmware reports PFDI_RET_FAULT_FOUND, return the
> failing test identifier to userspace.
> 
> Signed-off-by: Divin Raj <divin.raj@arm.com>
> Signed-off-by: Ahmed Azeem <ahmed.azeem@arm.com>
> ---
>  drivers/firmware/arm_pfdi.c | 225 ++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 225 insertions(+)
> 
> diff --git a/drivers/firmware/arm_pfdi.c b/drivers/firmware/arm_pfdi.c
> index 8c6d315b0572..22a69b12dbc8 100644
> --- a/drivers/firmware/arm_pfdi.c
> +++ b/drivers/firmware/arm_pfdi.c
> @@ -18,10 +18,16 @@
>  #include <linux/slab.h>
>  #include <linux/smp.h>
>  #include <linux/uaccess.h>
> +#include <linux/wait.h>
>  
>  struct pfdi_per_cpu_info {
> +	atomic_t run_inprogress;
>  	/* Ensures a single userspace owner per CPU while the node is open. */
>  	struct semaphore open_sem;
> +	/* Serializes per-CPU run state and device availability. */
> +	struct mutex state_lock;
> +	struct pfdi_test_range ongoing_run_range;
> +	wait_queue_head_t run_wait;
>  };
>  
>  struct pfdi_dev_data {
> @@ -36,6 +42,8 @@ struct pfdi_cpu_worker {
>  	u32 feature_fid;
>  	/* Protects worker lifetime and in-flight work state. */
>  	struct mutex worker_lock;
> +	struct pfdi_force_error error_info;
> +	struct pfdi_test_run test_run;
>  	struct arm_smccc_res smc_ret;
>  	int result;
>  };
> @@ -59,6 +67,10 @@ static DEFINE_PER_CPU(struct pfdi_per_cpu_info, pfdi_cpu_info);
>  
>  static enum cpuhp_state pfdi_cpuhp_state;
>  
> +static unsigned int pfdi_wait_timeout_ms = 60;
> +module_param_named(wait_timeout_ms, pfdi_wait_timeout_ms, uint, 0444);
> +MODULE_PARM_DESC(wait_timeout_ms,
> +		 "Maximum time in ms to wait for an in-progress PFDI run");
>  static int pfdi_to_linux_errno(int err)
>  {
>  	switch (err) {
> @@ -95,6 +107,18 @@ static int pfdi_smc_feature(u32 smc_fid)
>  	return (int)pfdi_smc_call(PFDI_FN_FEATURES, smc_fid, 0, 0, 0).a0;
>  }
>  
> +static bool pfdi_fid_supported(u32 smc_fid)
> +{
> +	unsigned int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(pfdi_supported_fids); i++) {
> +		if (pfdi_supported_fids[i] == smc_fid)
> +			return true;
> +	}
> +
> +	return false;
> +}
> +
>  static int pfdi_validate_cpu_features(void)
>  {
>  	unsigned int i;
> @@ -275,6 +299,172 @@ static int pfdi_pe_fw_check(unsigned int cpu)
>  	return pfdi_to_linux_errno(per_cpu(pfdi_worker_ctx, cpu).result);
>  }
>  
> +static void pfdi_pe_get_result_fn(struct kthread_work *work)
> +{
> +	struct pfdi_cpu_worker *worker_ctx;
> +
> +	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
> +	worker_ctx->smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_RESULT,
> +					    0, 0, 0, 0);
> +	complete(&worker_ctx->done);
> +}
> +
> +static int pfdi_pe_get_result(unsigned int cpu, s64 *ft_id)
> +{
> +	int ret;
> +	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
> +
> +	ret = pfdi_thread_worker(cpu, pfdi_pe_get_result_fn,
> +				 MAX_SCHEDULE_TIMEOUT);
> +	if (ret)
> +		return ret;
> +
> +	ret = (int)worker_ctx->smc_ret.a0;
> +	if (ret == PFDI_RET_FAULT_FOUND)
> +		*ft_id = worker_ctx->smc_ret.a1;
> +
> +	return pfdi_to_linux_errno(ret);
> +}
> +
> +static void pfdi_pe_force_error_fn(struct kthread_work *work)
> +{
> +	struct pfdi_cpu_worker *worker_ctx;
> +
> +	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
> +	worker_ctx->result = pfdi_smc_call(PFDI_FN_FORCE_ERROR,
> +					   worker_ctx->error_info.smc_fid,
> +					   worker_ctx->error_info.error_id,
> +					   0, 0).a0;
> +	complete(&worker_ctx->done);
> +}
> +
> +static int pfdi_pe_force_error(unsigned int cpu,
> +			       const struct pfdi_force_error *error_info)
> +{
> +	int ret;
> +	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
> +
> +	if (!pfdi_fid_supported(error_info->smc_fid))
> +		return -EINVAL;
> +
> +	if (error_info->error_id < PFDI_RET_TEST_COUNT_ZERO ||
> +	    error_info->error_id > SMCCC_RET_SUCCESS)
> +		return -EINVAL;
> +
> +	worker_ctx->error_info = *error_info;
> +
> +	ret = pfdi_thread_worker(cpu, pfdi_pe_force_error_fn,
> +				 MAX_SCHEDULE_TIMEOUT);
> +	if (ret)
> +		return ret;
> +
> +	return pfdi_to_linux_errno(worker_ctx->result);
> +}
> +
> +static bool pfdi_ranges_equal(const struct pfdi_test_range *lhs,
> +			      const struct pfdi_test_range *rhs)
> +{
> +	return lhs->start == rhs->start && lhs->end == rhs->end;
> +}
> +
> +static bool pfdi_run_available(const struct pfdi_per_cpu_info *cpu_info)
> +{
> +	return !atomic_read(&cpu_info->run_inprogress);
> +}
> +
> +static int pfdi_validate_test_range(unsigned int cpu,
> +				    const struct pfdi_test_range *range)
> +{
> +	s64 count;
> +	int ret;
> +
> +	if (range->start == -1 || range->end == -1)
> +		return pfdi_ranges_equal(range,
> +					 &(struct pfdi_test_range){ .start = -1,
> +								    .end = -1 }) ?
> +		       0 : -EINVAL;
> +
> +	if (range->start < 0 || range->end < 0 || range->start > range->end)
> +		return -EINVAL;
> +
> +	ret = pfdi_pe_get_test_count(cpu, &count);
> +	if (ret)
> +		return ret;
> +
> +	if (range->start >= count || range->end >= count)
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
> +static void pfdi_pe_run_fn(struct kthread_work *work)
> +{
> +	struct arm_smccc_res smc_ret;
> +	struct pfdi_cpu_worker *worker_ctx;
> +
> +	worker_ctx = container_of(work, struct pfdi_cpu_worker, work);
> +	smc_ret = pfdi_smc_call(PFDI_FN_PE_TEST_RUN,
> +				worker_ctx->test_run.range.start,
> +				worker_ctx->test_run.range.end, 0, 0);
> +
> +	if ((int)smc_ret.a0 == PFDI_RET_FAULT_FOUND)
> +		worker_ctx->test_run.ft_id = smc_ret.a1;
> +
> +	worker_ctx->result = (int)smc_ret.a0;
> +	complete(&worker_ctx->done);
> +}
> +
> +static int pfdi_pe_run_tests(unsigned int cpu, struct pfdi_test_run *test_run)
> +{
> +	int ret;
> +	unsigned long wait_timeout;
> +	struct pfdi_cpu_worker *worker_ctx = &per_cpu(pfdi_worker_ctx, cpu);
> +	struct pfdi_per_cpu_info *cpu_info = &per_cpu(pfdi_cpu_info, cpu);
> +
> +	ret = pfdi_validate_test_range(cpu, &test_run->range);
> +	if (ret)
> +		return ret;
> +
> +	wait_timeout = msecs_to_jiffies(pfdi_wait_timeout_ms);
> +
> +	mutex_lock(&cpu_info->state_lock);
> +	while (atomic_read(&cpu_info->run_inprogress)) {
> +		if (pfdi_ranges_equal(&test_run->range,
> +				      &cpu_info->ongoing_run_range)) {
> +			mutex_unlock(&cpu_info->state_lock);
> +			return -EBUSY;
> +		}
> +
> +		mutex_unlock(&cpu_info->state_lock);
> +			ret = wait_event_interruptible_timeout(cpu_info->run_wait,
> +							       pfdi_run_available(cpu_info),
> +							       wait_timeout);
> +		if (ret <= 0)
> +			return ret ? ret : -ETIME;
> +
> +		mutex_lock(&cpu_info->state_lock);
> +	}
> +
> +	atomic_set(&cpu_info->run_inprogress, 1);
> +	cpu_info->ongoing_run_range = test_run->range;
> +	worker_ctx->test_run = *test_run;
> +	mutex_unlock(&cpu_info->state_lock);
> +
> +	ret = pfdi_thread_worker(cpu, pfdi_pe_run_fn, wait_timeout);
> +
> +	mutex_lock(&cpu_info->state_lock);
> +	if (worker_ctx->result == PFDI_RET_FAULT_FOUND)
> +		test_run->ft_id = worker_ctx->test_run.ft_id;
> +	atomic_set(&cpu_info->run_inprogress, 0);
> +	wake_up_all(&cpu_info->run_wait);
> +	mutex_unlock(&cpu_info->state_lock);
> +
> +	if (ret)
> +		return ret;
> +
> +	return pfdi_to_linux_errno(worker_ctx->result);
> +}
> +
>  static int arm_pfdi_open(struct inode *inode, struct file *file)
>  {
>  	struct miscdevice *misc_dev = file->private_data;
> @@ -310,9 +500,12 @@ static long arm_pfdi_ioctl(struct file *file, unsigned int cmd,
>  	u32 feature_fid;
>  	u32 version;
>  	s64 count;
> +	s64 ft_id;
>  	u64 lib_info;
>  	struct miscdevice *misc_dev = file->private_data;
>  	struct pfdi_dev_data *device_data;
> +	struct pfdi_test_run test_run;
> +	struct pfdi_force_error error_info;
>  	unsigned int cpu;
>  
>  	if (!misc_dev || !misc_dev->this_device)
> @@ -350,9 +543,35 @@ static long arm_pfdi_ioctl(struct file *file, unsigned int cmd,
>  					 sizeof(count)))
>  			err = -EFAULT;
>  		break;
> +	case PFDI_IOCTL_PE_TEST_RUN:
> +		if (copy_from_user(&test_run, (void __user *)arg,
> +				   sizeof(test_run)))
> +			return -EFAULT;
> +
> +		err = pfdi_pe_run_tests(cpu, &test_run);
> +		if (err == -EFAULT &&
> +		    copy_to_user((void __user *)arg, &test_run,
> +				 sizeof(test_run)))
> +			err = -EFAULT;
> +		break;

Hi Ahmed, Divin,

One UAPI concern with PFDI_IOCTL_PE_TEST_RUN is the use of -EFAULT
to represent PFDI_RET_FAULT_FOUND.

The ioctl returns -EFAULT when copy_from_user() cannot read the request,
and when copy_to_user() cannot write the failing test-part ID.  It also
returns -EFAULT when the PFDI test completed and firmware reported
PFDI_RET_FAULT_FOUND.

This leaves userspace unable to distinguish a bad request/result pointer
from a completed test that detected a platform fault.  In the latter case,
the failing test-part ID is meaningful, while in the former case it is not.

Could the test outcome instead be returned explicitly in the request/result
structure, with ft_id valid when the reported PFDI result is
PFDI_RET_FAULT_FOUND?  The ioctl return value could then remain reserved
for Linux-side failures such as an inaccessible userspace buffer, an
interrupted wait, or device removal.

Apart from avoiding the overloaded meaning of -EFAULT, this would give the
new UAPI a natural place to reserve flags and result fields for future
extension.

Hope this helps. 

> +	case PFDI_IOCTL_PE_TEST_RESULT:
> +		err = pfdi_pe_get_result(cpu, &ft_id);
> +		if (err == -EFAULT && copy_to_user((void __user *)arg, &ft_id,
> +						   sizeof(ft_id)))
> +			err = -EFAULT;
> +		break;
>  	case PFDI_IOCTL_FW_CHECK:
>  		err = pfdi_pe_fw_check(cpu);
>  		break;
> +	case PFDI_IOCTL_FORCE_ERROR:
> +		if (copy_from_user(&error_info, (void __user *)arg,
> +				   sizeof(error_info)))
> +			return -EFAULT;
> +		if (error_info.reserved)
> +			return -EINVAL;
> +
> +		err = pfdi_pe_force_error(cpu, &error_info);
> +		break;
>  	default:
>  		err = -ENOTTY;
>  		break;


Cheers,
Ashok

  reply	other threads:[~2026-11-21  3:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 15:36 [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support Ahmed Azeem
2026-11-19 15:36 ` [PATCH RFC 1/4] uapi: firmware: arm: Add PFDI userspace ABI Ahmed Azeem
2026-11-19 21:35   ` Jonathan Cameron
2026-11-21  4:14   ` Ashok Raj
2026-11-19 15:36 ` [PATCH RFC 2/4] firmware: arm: Add PFDI misc driver skeleton Ahmed Azeem
2026-11-19 15:36 ` [PATCH RFC 3/4] firmware: arm: Add PFDI run, result, and error injection support Ahmed Azeem
2026-11-21  3:52   ` Ashok Raj [this message]
2026-11-19 15:36 ` [PATCH RFC 4/4] firmware: arm: Harden PFDI worker and hotplug handling Ahmed Azeem
2026-11-21  0:44 ` [PATCH RFC 0/4] firmware: arm: Add Platform Fault Detection Interface support Jithu Joseph

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=awEVfD12XgmNhqxt@hu-ashoraj-lv.qualcomm.com \
    --to=ashok.raj@oss.qualcomm.com \
    --cc=Bertrand.Marquis@arm.com \
    --cc=ahmed.azeem@arm.com \
    --cc=corbet@lwn.net \
    --cc=divin.raj@arm.com \
    --cc=jonathan.cameron@oss.qualcomm.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lpieralisi@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.org \
    --cc=sudeep.holla@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

all inboxes | Powered by JetHome®