Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Shekhar Chauhan <shekhar.chauhan@intel.com>
To: Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>,
	<igt-dev@lists.freedesktop.org>
Cc: <harish.chegondi@intel.com>
Subject: Re: [PATCH v2 6/8] tests/intel/xe_oa: Check that blocked OA stream waiters are woken up on unbind
Date: Thu, 26 Nov 2026 12:12:29 +0530	[thread overview]
Message-ID: <fedcbd2b-7a76-4dbb-bb09-6216e0cfda72@intel.com> (raw)
In-Reply-To: <20261104005505.3232485-16-umesh.nerlige.ramappa@intel.com>


On 11/4/2026 6:25, Umesh Nerlige Ramappa wrote:
> Add a variant that opens the stream disabled. A disabled stream
> produces no OA data, so a reader blocked in read() and a poller blocked
> in poll() stay blocked until the stream dies. Check that unbind wakes
> them up with ENODEV and POLLERR | POLLHUP.
>
> Signed-off-by: Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>
> ---
>   tests/intel/xe_oa.c | 137 ++++++++++++++++++++++++++++++++++++++++++--
>   1 file changed, 131 insertions(+), 6 deletions(-)
>
> diff --git a/tests/intel/xe_oa.c b/tests/intel/xe_oa.c
> index 8474d506612b..f1fe5f44c408 100644
> --- a/tests/intel/xe_oa.c
> +++ b/tests/intel/xe_oa.c
> @@ -17,6 +17,7 @@
>   #include <dirent.h>
>   #include <time.h>
>   #include <poll.h>
> +#include <pthread.h>
>   #include <math.h>
>   
>   #include "drm.h"
> @@ -2864,6 +2865,7 @@ static bool mem_is_zero(const void *vaddr, size_t len)
>   }
>   
>   #define UNBIND_CLOSE_BEFORE_REBIND	(1 << 0)
> +#define UNBIND_STREAM_DISABLED		(1 << 1)
>   
>   #define UNBIND_MAX_STREAMS	16
>   
> @@ -2878,12 +2880,25 @@ struct unbind_stream {
>   	size_t size;
>   };
>   
> +struct unbind_waiter {
> +	pthread_t thread;
> +	int fd;
> +	size_t report_size;
> +	int ret;
> +	int err;
> +	short revents;
> +};
> +
>   static char unbind_pci_slot[NAME_MAX];
>   static bool unbind_state_stale;
>   static struct unbind_stream unbind_streams[UNBIND_MAX_STREAMS];
>   static int unbind_num_streams;
> +static struct unbind_waiter unbind_reader_waiter, unbind_poller_waiter;
> +static bool unbind_waiters_started;
> +static volatile bool unbind_waiters_stop;
>   
> -static void unbind_stream_open(struct unbind_stream *s, const struct drm_xe_oa_unit *oau)
> +static void unbind_stream_open(struct unbind_stream *s, const struct drm_xe_oa_unit *oau,
> +			       unsigned int flags)
>   {
>   	struct intel_xe_perf_metric_set *test_set = oa_unit_metric_set(oau);
>   	uint64_t fmt = test_set->perf_oa_format;
> @@ -2904,6 +2919,10 @@ static void unbind_stream_open(struct unbind_stream *s, const struct drm_xe_oa_u
>   	s->properties[n++] = __ff(fmt);
>   	s->properties[n++] = DRM_XE_OA_PROPERTY_OA_PERIOD_EXPONENT;
>   	s->properties[n++] = oa_exponent_default;
> +	if (flags & UNBIND_STREAM_DISABLED) {
> +		s->properties[n++] = DRM_XE_OA_PROPERTY_OA_DISABLED;
> +		s->properties[n++] = true;
> +	}
>   	igt_assert_lte(n, ARRAY_SIZE(s->properties));
>   
>   	s->param.num_properties = n / 2;
> @@ -2983,6 +3002,93 @@ static void unbind_stream_check_dead(struct unbind_stream *s, const char *when)
>   	igt_assert_eq(errno, ENODEV);
>   }
>   
> +/*
> + * Blocking read until it fails. Run on a disabled stream, which produces no OA data, so the
> + * read blocks until the stream dies.
> + */
> +static void *unbind_reader(void *arg)
> +{
> +	struct unbind_waiter *w = arg;
> +	size_t size = w->report_size * 64;
> +	uint8_t *buf = malloc(size);
> +
> +	/* No igt_assert outside the main thread, report through the waiter result */
> +	if (!buf) {
> +		w->ret = -1;
> +		w->err = ENOMEM;
> +		return NULL;
> +	}
> +
> +	do {
> +		w->ret = read(w->fd, buf, size);
> +		w->err = errno;
> +	} while (!unbind_waiters_stop &&
> +		 (w->ret > 0 || (w->ret < 0 && (w->err == EINTR || w->err == EIO))));
> +
> +	free(buf);
> +	return NULL;
> +}
> +
> +/* Poll until the stream reports an error, also on a disabled stream so it blocks */
> +static void *unbind_poller(void *arg)
> +{
> +	struct unbind_waiter *w = arg;
> +	struct pollfd pfd = { .fd = w->fd, .events = POLLIN };
> +
> +	do {
> +		pfd.revents = 0;
> +		w->ret = poll(&pfd, 1, -1);
> +		w->err = errno;
> +	} while (!unbind_waiters_stop &&
> +		 ((w->ret > 0 && !(pfd.revents & (POLLERR | POLLHUP))) ||
> +		  (w->ret < 0 && w->err == EINTR)));
> +
> +	w->revents = pfd.revents;
> +	return NULL;
> +}
> +
> +static void unbind_waiters_start(struct unbind_stream *s)
> +{
> +	struct unbind_waiter *reader = &unbind_reader_waiter, *poller = &unbind_poller_waiter;
> +
> +	memset(reader, 0, sizeof(*reader));
> +	memset(poller, 0, sizeof(*poller));
> +	reader->fd = s->fd;
> +	poller->fd = s->fd;
> +	reader->report_size = s->report_size;
> +	unbind_waiters_stop = false;
> +
> +	igt_assert_eq(pthread_create(&reader->thread, NULL, unbind_reader, reader), 0);
> +	if (pthread_create(&poller->thread, NULL, unbind_poller, poller)) {
> +		unbind_waiters_stop = true;
> +		pthread_join(reader->thread, NULL);
> +		igt_assert_f(false, "failed to create poller thread\n");
> +	}
> +	unbind_waiters_started = true;
> +
> +	/* Let the waiters block */
> +	usleep(100 * 1000);
> +}
> +
> +static void unbind_waiters_join(void)
> +{
> +	struct unbind_waiter *reader = &unbind_reader_waiter, *poller = &unbind_poller_waiter;
> +
> +	igt_set_timeout(60, "OA stream waiters not woken up on unbind");
> +	pthread_join(reader->thread, NULL);
> +	pthread_join(poller->thread, NULL);
> +	unbind_waiters_started = false;
> +	igt_reset_timeout();
> +
> +	igt_debug("reader: ret=%d errno=%s\n", reader->ret, strerror(reader->err));
> +	igt_assert_eq(reader->ret, -1);
> +	igt_assert_eq(reader->err, ENODEV);
> +
> +	igt_debug("poller: ret=%d revents=0x%x\n", poller->ret, poller->revents);
> +	igt_assert_eq(poller->ret, 1);
> +	igt_assert_eq(poller->revents, POLLERR | POLLHUP);
> +}
> +
>   /*
>    * Restore a bound device and fresh device state if an unbind/rebind test bailed out
>    * half way, so the remaining subtests can run.
> @@ -2992,6 +3098,13 @@ static void unbind_rebind_recover(void)
>   	char path[PATH_MAX];
>   	int i;
>   
> +	if (unbind_waiters_started) {
> +		unbind_waiters_stop = true;
> +		pthread_join(unbind_reader_waiter.thread, NULL);
> +		pthread_join(unbind_poller_waiter.thread, NULL);
> +		unbind_waiters_started = false;
> +	}

If unbind fails for any reason, then both threads are still blocked 
inside read() or poll().  So, setting unbind_waiters_stop = true, won't 
wake them up.  And, pthread_join() will hang forever.  To avoid this, 
can we instead call pthread_cancel() before pthread_join() so that they 
wake up?

-shekhar

> +
>   	for (i = 0; i < unbind_num_streams; i++) {
>   		struct unbind_stream *s = &unbind_streams[i];
>   
> @@ -3019,24 +3132,32 @@ static void unbind_rebind_recover(void)
>   /**
>    * SUBTEST: unbind-rebind-stream
>    * Description: Open OA streams with mmapped OA buffers and test the effects of unbind
> - *		and rebind: streams die, mmaps read a zeroed dummy page and refuse access
> - *		through /proc/self/mem, no new streams can be opened on the removed device
> - *		and the device works after rebind.
> + *		and rebind: streams die, waiters are woken up, mmaps read a zeroed dummy
> + *		page and refuse access through /proc/self/mem, no new streams can be
> + *		opened on the removed device and the device works after rebind.
>    */
>   static void test_unbind_rebind(const struct drm_xe_oa_unit *oau0, unsigned int flags)
>   {
>   	struct unbind_stream *streams = unbind_streams;
>   	struct unbind_stream *primary = &streams[0];
> +	bool disabled = flags & UNBIND_STREAM_DISABLED;
>   	int ret, i;
>   
>   	igt_device_get_pci_slot_name(drm_fd, unbind_pci_slot);
>   	igt_debug("Device %s\n", unbind_pci_slot);
>   
>   	unbind_num_streams = 0;
> -	unbind_stream_open(&streams[unbind_num_streams++], oau0);
> +	unbind_stream_open(&streams[unbind_num_streams++], oau0, flags);
>   	igt_debug("Opened %d OA stream(s)\n", unbind_num_streams);
>   
> -	unbind_stream_check_live(primary);
> +	/*
> +	 * A disabled stream produces no OA data, so a reader and a poller stay blocked on it
> +	 * until unbind kills the stream
> +	 */
> +	if (disabled)
> +		unbind_waiters_start(primary);
> +	else
> +		unbind_stream_check_live(primary);
>   
>   	/* Guard against a wedged unbind/bind */
>   	igt_set_timeout(60, "xe unbind timed out (wedged?)");
> @@ -3047,6 +3168,9 @@ static void test_unbind_rebind(const struct drm_xe_oa_unit *oau0, unsigned int f
>   	igt_reset_timeout();
>   	igt_assert_f(ret == 0, "xe unbind failed: %s\n", strerror(-ret));
>   
> +	if (disabled)
> +		unbind_waiters_join();
> +
>   	for (i = 0; i < unbind_num_streams; i++)
>   		unbind_stream_check_dead(&streams[i], "after-unbind");
>   
> @@ -5566,6 +5690,7 @@ int igt_main_args("b:td", long_options, help_str, opt_handler, NULL)
>   		} unbind_variants[] = {
>   			{ "close", UNBIND_CLOSE_BEFORE_REBIND },
>   			{ "open", 0 },
> +			{ "disabled", UNBIND_STREAM_DISABLED },
>   		};
>   
>   		for (int i = 0; i < ARRAY_SIZE(unbind_variants); i++) {

-- 
Shekhar Chauhan
Linux Graphics Software Engineer
Linux Platform Enabling
Intel Corporation


  reply	other threads:[~2026-11-26  6:56 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04  0:55 [PATCH v2 0/8] Test unbinding PCI device while OA stream is open Umesh Nerlige Ramappa
2026-11-04  0:55 ` [PATCH v2 1/8] tests/intel/xe_oa: Move device setup and teardown into helpers Umesh Nerlige Ramappa
2026-11-04  0:55 ` [PATCH v2 2/8] tests/intel/xe_oa: Add unbind/rebind test with an open OA stream Umesh Nerlige Ramappa
2026-11-26  7:09   ` Shekhar Chauhan
2026-11-04  0:55 ` [PATCH v2 3/8] tests/intel/xe_oa: Check that a removed device can't open OA streams Umesh Nerlige Ramappa
2026-11-26  7:10   ` Shekhar Chauhan
2026-11-04  0:55 ` [PATCH v2 4/8] tests/intel/xe_oa: Check OA buffer mmaps across unbind Umesh Nerlige Ramappa
2026-11-26  7:10   ` Shekhar Chauhan
2026-11-04  0:55 ` [PATCH v2 5/8] tests/intel/xe_oa: Check vm_ops->access on " Umesh Nerlige Ramappa
2026-11-26  5:55   ` Shekhar Chauhan
2026-11-04  0:55 ` [PATCH v2 6/8] tests/intel/xe_oa: Check that blocked OA stream waiters are woken up on unbind Umesh Nerlige Ramappa
2026-11-26  6:42   ` Shekhar Chauhan [this message]
2026-11-04  0:55 ` [PATCH v2 7/8] tests/intel/xe_oa: Add an exec queue variant to the unbind/rebind test Umesh Nerlige Ramappa
2026-11-26  7:11   ` Shekhar Chauhan
2026-11-04  0:55 ` [PATCH v2 8/8] tests/intel/xe_oa: Add an all OA units " Umesh Nerlige Ramappa
2026-11-26  7:06   ` Shekhar Chauhan
2026-11-04  2:19 ` ✓ Xe.CI.BAT: success for Test unbinding PCI device while OA stream is open (rev2) Patchwork
2026-11-04  2:39 ` ✗ i915.CI.BAT: failure " Patchwork
2026-11-04 17:42 ` ✗ Xe.CI.FULL: " Patchwork

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=fedcbd2b-7a76-4dbb-bb09-6216e0cfda72@intel.com \
    --to=shekhar.chauhan@intel.com \
    --cc=harish.chegondi@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=umesh.nerlige.ramappa@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