mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "David E. Box" <david.e.box@linux.intel.com>
To: ilpo.jarvinen@linux.intel.com, david.e.box@linux.intel.com,
	linux-kernel@vger.kernel.org,
	platform-driver-x86@vger.kernel.org, rodrigo.vivi@intel.com,
	michael.j.ruhl@intel.com, ayaz.siddiqui@intel.com,
	syed.abdul.muqthyar.ahmed@intel.com,
	intel-xe@lists.freedesktop.org, hansg@kernel.org
Subject: [PATCH v4 1/1] platform/x86/intel/pmt: Notify userspace when crashlogs complete
Date: Tue,  1 Dec 2026 16:03:49 -0800	[thread overview]
Message-ID: <3caea6f79f46dad364f6c4c1d91298e3a0f9d97e.1796164100.git.david.e.box@linux.intel.com> (raw)
In-Reply-To: <cover.1796164100.git.david.e.box@linux.intel.com>

A crashlog may already be complete when the driver binds, or it may
complete asynchronously after a manual trigger. Userspace currently has to
poll the status attributes to find out when data is ready.

Emit a KOBJ_CHANGE uevent on the crashlog<x> device carrying
INTEL_PMT_CRASHLOG_COMPLETE:

  - at probe, set to 1 if a completed crashlog is present
  - after a manual trigger, set to 1 when the capture completes, or 0
    if it has not completed within 5 seconds of polling

Successfully writing 0 to trigger, clearing, rearming or disabling the
crashlog while the trigger poll is running ends it without an event.

Trigger completion is polled every 100 ms from a delayed work item on
the freezable workqueue, so the device is not accessed during suspend.
The probe-time check runs in probe, in the same control_mutex section
that enables triggers for the entry, so a trigger cannot be accepted
before it. Its status read may go through the parent's read_reg()
callback, which client drivers may call from probe.

Manual triggers are rejected with -EBUSY until probe has set up the
entry. The work items are disabled and drained on removal.

Document the uevent in the sysfs-class-intel_pmt ABI file.

Signed-off-by: David E. Box <david.e.box@linux.intel.com>
---
V4 changes:
  - Run the probe-time check from probe again, in the control_mutex
    section that enables triggers for the entry, as in v2. The struct
    pmt_callbacks contract added by commit ce0567d217b1
    ("platform/x86/intel/pmt: Introduce a callback for discovery data")
    makes this safe, and it removes the control_mutex/parent lock
    inversion reported by sashiko on v3. This drops the pending_work
    item, the triggered flag and the separate loop enabling triggers
    after all entries are created.
  - Dropped Michael's Tested-by, since the probe path changed from the
    version he tested.
  - Rebased onto linux-next, which has that commit and
    commit 9202f1ae4590 ("platform/x86/intel/pmt: Use the pre-populated
    header info for crashlog"), merged through drm-xe-next.
  - ABI: Date December 2026 and KernelVersion 7.6, since the dependency
    reaches mainline in the 7.5 merge window.
  - ABI and commit message: only a successful write of 0 to trigger, of
    clear or rearm, or disabling ends the poll, and the window is 5
    seconds of polling (sashiko).
  - Commit message: drop the note that the probe-time event is the first
    point at which userspace can read the attributes. That follows from
    the attributes being created after KOBJ_ADD, which is better fixed
    in the class driver (sashiko).
  - Comments: name pmt_crashlog_end_poll() as what ends a poll cycle,
    and say why the uevent work does not need to be freezable.

V3 changes:
  - Run the probe-time check from a work item queued at the end of
    probe instead of from probe, so a read_reg() callback is never
    called from a synchronous probe that may run under the parent's
    lock (reported and tested by Michael on CRI).
  - Queue the probe-time check on the freezable workqueue, like the
    poll (sashiko).
  - Enable triggers once probe has created every entry, rather than per
    entry, so abort_probe never waits on a work item blocked on its
    caller's lock.
  - If a trigger is accepted before the probe-time check runs, the
    check skips its notification. The trigger's poll reports the
    capture, so it is not also reported as found at probe.
  - Disabling the crashlog while the trigger poll runs ends the poll
    without an event, like clear and rearm (sashiko).
  - ABI: note that events may be merged, list writing 0 to trigger and
    disabling as ending the poll, and update Date and KernelVersion.
  - Rebased onto review-ilpo-next. The patch also applies cleanly to
    for-next.

V2 changes:
  - Replaced the two uevent variables with a single
    INTEL_PMT_CRASHLOG_COMPLETE. INTEL_PMT_CRASHLOG_EVENT=PRESENT and
    COMPLETE=1 were both fixed and carried no information beyond the
    event itself.
  - Emit COMPLETE=0 when a manual trigger does not complete within the
    poll window, so userspace is not left waiting.
  - Reject manual triggers until probe has finished setting up the
    entry. This closes a window where a trigger during
    intel_pmt_dev_create() could queue work against a device freed by
    its error path (reported by sashiko on v1).
  - Dropped the cancel_*_sync() calls on the intel_pmt_dev_create()
    failure and skip paths. They ran after the device was already
    freed, but with triggers now rejected until setup completes no work
    can be pending there.
  - End the trigger poll without an event when the log is cleared or
    rearmed while it is running.
  - Cancel a stale poll only after the new trigger has been written, so
    a failed trigger leaves the previous poll in place.
  - Run the probe-time check under control_mutex, in the same critical
    section that enables triggers.
  - Run the poll on the freezable workqueue so it does not read the
    device during suspend.
  - Rebased onto review-ilpo-next and adapted to the crashlog register
    helpers now returning errors (Michal's additions). A failed status
    read counts as not complete.
  - Rewrote the ABI

 .../ABI/testing/sysfs-class-intel_pmt         |  35 ++++
 drivers/platform/x86/intel/pmt/crashlog.c     | 149 +++++++++++++++++-
 2 files changed, 178 insertions(+), 6 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-class-intel_pmt b/Documentation/ABI/testing/sysfs-class-intel_pmt
index ed4c886a21b1..d41194f4098a 100644
--- a/Documentation/ABI/testing/sysfs-class-intel_pmt
+++ b/Documentation/ABI/testing/sysfs-class-intel_pmt
@@ -66,6 +66,41 @@ Description:
 		can be determined from an XML file of specified GUID for the
 		parent device.
 
+What:		/sys/class/intel_pmt/crashlog<x>
+Date:		December 2026
+KernelVersion:	7.6
+Contact:	David Box <david.e.box@linux.intel.com>
+Description:
+		The driver emits a KOBJ_CHANGE uevent for the crashlog<x>
+		device with the following environment variable:
+
+		INTEL_PMT_CRASHLOG_COMPLETE
+			The crashlog complete status at the end of a check:
+			1 if set, 0 if not. It reports the status only, not
+			what caused the crashlog to be captured.
+
+		The event is currently sent when a completed crashlog is
+		found at probe, and after a write of 1 to trigger. It
+		returns 1 when the crashlog completes, or 0 for trigger if
+		it has not completed within the poll time. A successful
+		write of 0 to trigger, of clear or rearm, or disabling the
+		crashlog while polling ends the poll without an event.
+
+		The status may have changed by the time the event is
+		received, the same crashlog may be reported more than
+		once, and events sent in quick succession may be merged
+		into one carrying the latest status. Consumers should read
+		the status attributes before acting. Consumers that start
+		after the device was probed should check the complete
+		status of existing devices, for example when handling a
+		coldplug add event, since an event emitted before the
+		uevent listener starts is not replayed.
+
+		Other KOBJ_CHANGE events may be added in the future with
+		different variables. Consumers should match on
+		INTEL_PMT_CRASHLOG_COMPLETE and ignore variables they do not
+		recognize.
+
 What:		/sys/class/intel_pmt/crashlog<x>/crashlog
 Date:		October 2020
 KernelVersion:	5.10
diff --git a/drivers/platform/x86/intel/pmt/crashlog.c b/drivers/platform/x86/intel/pmt/crashlog.c
index e2af09732dd6..1c902bbaeaa8 100644
--- a/drivers/platform/x86/intel/pmt/crashlog.c
+++ b/drivers/platform/x86/intel/pmt/crashlog.c
@@ -13,13 +13,16 @@
 #include <linux/intel_vsec.h>
 #include <linux/io.h>
 #include <linux/kernel.h>
+#include <linux/kobject.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/pci.h>
 #include <linux/printk.h>
 #include <linux/slab.h>
+#include <linux/sysfs.h>
 #include <linux/uaccess.h>
 #include <linux/overflow.h>
+#include <linux/workqueue.h>
 
 #include "class.h"
 
@@ -115,6 +118,12 @@ struct crashlog_entry {
 	struct intel_pmt_entry		entry;
 	struct mutex			control_mutex;
 	const struct crashlog_info	*info;
+	struct work_struct		uevent_work;
+	struct delayed_work		uevent_poll_work;
+	u8				uevent_poll_tries;
+	bool				uevent_polling;
+	bool				uevent_complete;
+	bool				trigger_ready;
 };
 
 struct pmt_crashlog_priv {
@@ -122,6 +131,9 @@ struct pmt_crashlog_priv {
 	struct crashlog_entry	entry[];
 };
 
+#define PMT_CRASHLOG_UEVENT_POLL_MS		100
+#define PMT_CRASHLOG_UEVENT_POLL_MAX_TRIES	50
+
 /*
  * I/O
  */
@@ -271,6 +283,84 @@ static int pmt_crashlog_set_rearm(struct crashlog_entry *crashlog)
 	return pmt_crashlog_rmw(crashlog, crashlog->info->control.rearm, true);
 }
 
+/*
+ * Runs on the system workqueue: unlike the trigger poll it does not touch the
+ * device, so it does not need to be held off during suspend.
+ */
+static void pmt_crashlog_uevent_fn(struct work_struct *work)
+{
+	struct crashlog_entry *crashlog =
+		container_of(work, struct crashlog_entry, uevent_work);
+	char *envp[] = {
+		READ_ONCE(crashlog->uevent_complete) ?
+			"INTEL_PMT_CRASHLOG_COMPLETE=1" :
+			"INTEL_PMT_CRASHLOG_COMPLETE=0",
+		NULL,
+	};
+
+	kobject_uevent_env(crashlog->entry.kobj, KOBJ_CHANGE, envp);
+}
+
+static void pmt_crashlog_uevent_poll_fn(struct work_struct *work)
+{
+	struct crashlog_entry *crashlog =
+		container_of(to_delayed_work(work), struct crashlog_entry,
+			     uevent_poll_work);
+	bool complete;
+	int ret;
+
+	guard(mutex)(&crashlog->control_mutex);
+
+	/* The cycle was ended by pmt_crashlog_end_poll() */
+	if (!crashlog->uevent_polling)
+		return;
+
+	/* A newer trigger_store() re-armed us. That cycle owns the notification */
+	if (delayed_work_pending(&crashlog->uevent_poll_work))
+		return;
+
+	ret = pmt_crashlog_complete(crashlog, &complete);
+	if (!ret && complete) {
+		crashlog->uevent_polling = false;
+		WRITE_ONCE(crashlog->uevent_complete, true);
+		schedule_work(&crashlog->uevent_work);
+		return;
+	}
+
+	if (++crashlog->uevent_poll_tries < PMT_CRASHLOG_UEVENT_POLL_MAX_TRIES) {
+		queue_delayed_work(system_freezable_wq, &crashlog->uevent_poll_work,
+				   msecs_to_jiffies(PMT_CRASHLOG_UEVENT_POLL_MS));
+		return;
+	}
+
+	/* Trigger did not complete within the poll window */
+	crashlog->uevent_polling = false;
+	WRITE_ONCE(crashlog->uevent_complete, false);
+	schedule_work(&crashlog->uevent_work);
+}
+
+static void pmt_crashlog_end_poll(struct crashlog_entry *crashlog)
+{
+	lockdep_assert_held(&crashlog->control_mutex);
+
+	crashlog->uevent_polling = false;
+	cancel_delayed_work(&crashlog->uevent_poll_work);
+}
+
+static void pmt_crashlog_notify_pending(struct crashlog_entry *crashlog)
+{
+	bool complete;
+	int ret;
+
+	lockdep_assert_held(&crashlog->control_mutex);
+
+	ret = pmt_crashlog_complete(crashlog, &complete);
+	if (!ret && complete) {
+		WRITE_ONCE(crashlog->uevent_complete, true);
+		schedule_work(&crashlog->uevent_work);
+	}
+}
+
 /*
  * sysfs
  */
@@ -309,8 +399,12 @@ clear_store(struct device *dev, struct device_attribute *attr,
 	guard(mutex)(&crashlog->control_mutex);
 
 	ret = pmt_crashlog_set_clear(crashlog);
+	if (ret)
+		return ret;
 
-	return ret ?: count;
+	pmt_crashlog_end_poll(crashlog);
+
+	return count;
 }
 static DEVICE_ATTR_RW(clear);
 
@@ -399,8 +493,13 @@ enable_store(struct device *dev, struct device_attribute *attr,
 	guard(mutex)(&crashlog->control_mutex);
 
 	ret = pmt_crashlog_set_disable(crashlog, !enabled);
+	if (ret)
+		return ret;
 
-	return ret ?: count;
+	if (!enabled)
+		pmt_crashlog_end_poll(crashlog);
+
+	return count;
 }
 static DEVICE_ATTR_RW(enable);
 
@@ -454,8 +553,12 @@ rearm_store(struct device *dev, struct device_attribute *attr, const char *buf,
 	guard(mutex)(&crashlog->control_mutex);
 
 	ret = pmt_crashlog_set_rearm(crashlog);
+	if (ret)
+		return ret;
 
-	return ret ?: count;
+	pmt_crashlog_end_poll(crashlog);
+
+	return count;
 }
 static DEVICE_ATTR_RW(rearm);
 
@@ -502,9 +605,17 @@ trigger_store(struct device *dev, struct device_attribute *attr,
 
 	if (!trigger) {
 		ret = pmt_crashlog_set_clear(crashlog);
-		return ret ?: count;
+		if (ret)
+			return ret;
+
+		pmt_crashlog_end_poll(crashlog);
+		return count;
 	}
 
+	/* reject triggers until probe has finished setting up this entry */
+	if (!crashlog->trigger_ready)
+		return -EBUSY;
+
 	/* we cannot trigger a new crash if one is still pending */
 	ret = pmt_crashlog_complete(crashlog, &complete);
 	if (ret)
@@ -513,8 +624,21 @@ trigger_store(struct device *dev, struct device_attribute *attr,
 		return -EEXIST;
 
 	ret = pmt_crashlog_set_execute(crashlog);
+	if (ret)
+		return ret;
 
-	return ret ?: count;
+	/*
+	 * Use non-sync since uevent_poll_fn() takes this same mutex, so
+	 * cancel_delayed_work_sync() here could deadlock against it.
+	 */
+	cancel_delayed_work(&crashlog->uevent_poll_work);
+
+	crashlog->uevent_polling = true;
+	crashlog->uevent_poll_tries = 0;
+	queue_delayed_work(system_freezable_wq, &crashlog->uevent_poll_work,
+			   msecs_to_jiffies(PMT_CRASHLOG_UEVENT_POLL_MS));
+
+	return count;
 }
 static DEVICE_ATTR_RW(trigger);
 
@@ -640,6 +764,8 @@ static void pmt_crashlog_remove(struct auxiliary_device *auxdev)
 	for (i = 0; i < priv->num_entries; i++) {
 		struct crashlog_entry *crashlog = &priv->entry[i];
 
+		disable_delayed_work_sync(&crashlog->uevent_poll_work);
+		disable_work_sync(&crashlog->uevent_work);
 		intel_pmt_dev_destroy(&crashlog->entry, &pmt_crashlog_ns);
 		mutex_destroy(&crashlog->control_mutex);
 	}
@@ -661,7 +787,13 @@ static int pmt_crashlog_probe(struct auxiliary_device *auxdev,
 	auxiliary_set_drvdata(auxdev, priv);
 
 	for (i = 0; i < intel_vsec_dev->num_resources; i++) {
-		struct intel_pmt_entry *entry = &priv->entry[priv->num_entries].entry;
+		struct crashlog_entry *crashlog = &priv->entry[priv->num_entries];
+		struct intel_pmt_entry *entry = &crashlog->entry;
+
+		/* trigger_store() rejects triggers until trigger_ready is set below */
+		INIT_WORK(&crashlog->uevent_work, pmt_crashlog_uevent_fn);
+		INIT_DELAYED_WORK(&crashlog->uevent_poll_work,
+				  pmt_crashlog_uevent_poll_fn);
 
 		ret = intel_pmt_dev_create(entry, &pmt_crashlog_ns, intel_vsec_dev, i);
 		if (ret < 0)
@@ -670,6 +802,11 @@ static int pmt_crashlog_probe(struct auxiliary_device *auxdev,
 			continue;
 
 		priv->num_entries++;
+
+		scoped_guard(mutex, &crashlog->control_mutex) {
+			crashlog->trigger_ready = true;
+			pmt_crashlog_notify_pending(crashlog);
+		}
 	}
 
 	return 0;
-- 
2.53.0


           reply	other threads:[~2026-12-02  0:03 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <cover.1796164100.git.david.e.box@linux.intel.com>]

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=3caea6f79f46dad364f6c4c1d91298e3a0f9d97e.1796164100.git.david.e.box@linux.intel.com \
    --to=david.e.box@linux.intel.com \
    --cc=ayaz.siddiqui@intel.com \
    --cc=hansg@kernel.org \
    --cc=ilpo.jarvinen@linux.intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michael.j.ruhl@intel.com \
    --cc=platform-driver-x86@vger.kernel.org \
    --cc=rodrigo.vivi@intel.com \
    --cc=syed.abdul.muqthyar.ahmed@intel.com \
    /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®