mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wei Huang <wei.huang2@amd.com>
To: <dmaengine@vger.kernel.org>
Cc: <vkoul@kernel.org>, <Frank.Li@kernel.org>, <shuah@kernel.org>,
	<nathan.lynch@amd.com>, <wei.huang2@amd.com>,
	<mario.limonciello@amd.com>, <Stephen.Bates@amd.com>,
	<tycho@kernel.org>, <suravee.suthikulpanit@amd.com>,
	<rientjes@google.com>, <John.Kariuki@amd.com>, <jic23@kernel.org>,
	<weixugc@google.com>, <bhelgaas@google.com>,
	<Shyam.Iyer@dell.com>, <linux-kernel@vger.kernel.org>,
	<linux-kselftest@vger.kernel.org>, <linux-pci@vger.kernel.org>
Subject: [PATCH v1 37/44] selftests: sdxi: test context creation
Date: Thu, 19 Nov 2026 14:23:13 -0600	[thread overview]
Message-ID: <20261119202321.1734355-38-wei.huang2@amd.com> (raw)
In-Reply-To: <20261119202321.1734355-1-wei.huang2@amd.com>

Add basic context creation smoke test for the SDXI selftest suite.

Co-developed-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Wei Huang <wei.huang2@amd.com>
Signed-off-by: Nathan Lynch <nathan.lynch@amd.com>
---
 tools/testing/selftests/sdxi/.gitignore |   1 +
 tools/testing/selftests/sdxi/Makefile   |   2 +
 tools/testing/selftests/sdxi/context.c  | 343 ++++++++++++++++++++++++
 3 files changed, 346 insertions(+)
 create mode 100644 tools/testing/selftests/sdxi/.gitignore
 create mode 100644 tools/testing/selftests/sdxi/context.c

diff --git a/tools/testing/selftests/sdxi/.gitignore b/tools/testing/selftests/sdxi/.gitignore
new file mode 100644
index 000000000000..9e3f2c52330c
--- /dev/null
+++ b/tools/testing/selftests/sdxi/.gitignore
@@ -0,0 +1 @@
+/context
diff --git a/tools/testing/selftests/sdxi/Makefile b/tools/testing/selftests/sdxi/Makefile
index f2af46ac041f..dba5d7df3872 100644
--- a/tools/testing/selftests/sdxi/Makefile
+++ b/tools/testing/selftests/sdxi/Makefile
@@ -2,6 +2,8 @@ werror := -Werror
 CFLAGS += -std=gnu17 -Og -Wall -Wextra $(werror) $(TOOLS_INCLUDES) $(KHDR_INCLUDES)
 CPPFLAGS += -I../../../include/ -Ilib/
 
+TEST_GEN_PROGS := context
+
 include ../lib.mk
 
 HOSTAR ?= ar
diff --git a/tools/testing/selftests/sdxi/context.c b/tools/testing/selftests/sdxi/context.c
new file mode 100644
index 000000000000..8649b76f60e3
--- /dev/null
+++ b/tools/testing/selftests/sdxi/context.c
@@ -0,0 +1,343 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * SDXI context ioctl selftests.
+ *
+ * Copyright (C) 2026 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/eventfd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/sdxi.h>
+
+#include "lib.h"
+#include "lib/fixture-context.h"
+#include "../kselftest_harness.h"
+
+#define AKEY_ENT_VL		(1U << 0)
+#define AKEY_ENT_IV		(1U << 1)
+#define AKEY_ENT_PV		(1U << 2)
+#define AKEY_ENT_STE		(1U << 3)
+#define AKEY_ENT_PH_SHIFT	30
+#define AKEY_ENT_INTR_SHIFT	4
+#define AKEY_ENT_INTR_MAX	((1U << 11) - 1)
+
+static void akey_pack_intr(uint32_t data[4], uint32_t pasid,
+			   uint32_t intr_num, bool iv)
+{
+	memset(data, 0, sizeof(uint32_t) * 4);
+	data[0] = AKEY_ENT_VL | AKEY_ENT_PV;
+	if (iv)
+		data[0] |= AKEY_ENT_IV;
+	data[0] |= (intr_num & AKEY_ENT_INTR_MAX) << AKEY_ENT_INTR_SHIFT;
+	data[1] = pasid;
+}
+
+static int set_single_akey(int cxt_fd, uint16_t index, const uint32_t data[4])
+{
+	struct sdxi_set_keys_args *args;
+	size_t bytes = sizeof(*args) + sizeof(args->entries[0]);
+	int rc;
+
+	args = calloc(1, bytes);
+	if (!args)
+		return -ENOMEM;
+
+	args->argsz = sizeof(*args);
+	args->nkeys = 1;
+	args->entries[0].index = index;
+	memcpy(args->entries[0].data, data, sizeof(args->entries[0].data));
+
+	rc = ioctl(cxt_fd, SDXI_SET_AKEYS, args);
+	rc = rc ? -errno : 0;
+
+	free(args);
+	return rc;
+}
+
+static int get_single_akey(int cxt_fd, uint16_t index, uint32_t data[4])
+{
+	struct sdxi_get_keys_args *args;
+	size_t bytes = sizeof(*args) + sizeof(args->entries[0]);
+	int rc;
+
+	args = calloc(1, bytes);
+	if (!args)
+		return -ENOMEM;
+
+	args->argsz = sizeof(*args);
+	args->nkeys = 1;
+	args->entries[0].index = index;
+
+	rc = ioctl(cxt_fd, SDXI_GET_AKEYS, args);
+	rc = rc ? -errno : 0;
+	if (!rc)
+		memcpy(data, args->entries[0].data, sizeof(args->entries[0].data));
+
+	free(args);
+	return rc;
+}
+
+// This basically duplicates the normal context fixture, maybe this
+// could be fixed by using a variant?
+FIXTURE(sdxi_context_nomap) {
+	int cxt_fd;
+	int device_fd;
+	void *write_idx;
+	size_t write_idx_bytes;
+	void *cxt_sts;
+	size_t cxt_sts_bytes;
+	void *ring;
+	size_t ring_bytes;
+};
+
+FIXTURE_SETUP(sdxi_context_nomap)
+{
+	int device_fd;
+	long page_size;
+	size_t ring_bytes;
+	void *cxt_sts;
+	void *ring;
+	void *write_idx;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(device_fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return,
+			     "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return,
+			     "insufficient privileges");
+	}
+
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(struct sdxi_create_cxt_args),
+		.ds_ring_entries = 1024,
+	};
+
+	page_size = sysconf(_SC_PAGESIZE);
+	ASSERT_GT(page_size, 0);
+	ring_bytes = (size_t)arg.ds_ring_entries * sizeof(struct sdxi_desc);
+
+	cxt_sts = mmap(NULL, (size_t)page_size, PROT_READ | PROT_WRITE,
+		       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	write_idx = mmap(NULL, (size_t)page_size, PROT_READ | PROT_WRITE,
+			 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	ring = mmap(NULL, ring_bytes, PROT_READ | PROT_WRITE,
+		    MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+
+	ASSERT_NE(cxt_sts, MAP_FAILED);
+	ASSERT_NE(write_idx, MAP_FAILED);
+	ASSERT_NE(ring, MAP_FAILED);
+
+	/* Fault in and invalidate the ring before the device can fetch it. */
+	memset(ring, 0, ring_bytes);
+
+	arg.cxt_sts_base = (uint64_t)(uintptr_t)cxt_sts;
+	arg.write_index_base = (uint64_t)(uintptr_t)write_idx;
+	arg.ds_ring_base = (uint64_t)(uintptr_t)ring;
+
+	ASSERT_EQ(ioctl(device_fd, SDXI_CREATE_CXT, &arg), 0);
+
+	self->cxt_fd = arg.cxt_fd;
+	self->device_fd = device_fd;
+	self->write_idx = write_idx;
+	self->write_idx_bytes = (size_t)page_size;
+	self->cxt_sts = cxt_sts;
+	self->cxt_sts_bytes = (size_t)page_size;
+	self->ring = ring;
+	self->ring_bytes = ring_bytes;
+}
+
+FIXTURE_TEARDOWN(sdxi_context_nomap) {
+	int cxt_fd = self->cxt_fd;
+	int device_fd = self->device_fd;
+	struct sdxi_quiesce_cxt_args quiesce = {};
+
+	quiesce.argsz = sizeof(quiesce);
+
+	EXPECT_EQ(ioctl(cxt_fd, SDXI_QUIESCE_CXT, &quiesce),
+		  0);
+	EXPECT_EQ(munmap(self->write_idx, self->write_idx_bytes), 0);
+	EXPECT_EQ(munmap(self->cxt_sts, self->cxt_sts_bytes), 0);
+	EXPECT_EQ(munmap(self->ring, self->ring_bytes), 0);
+	EXPECT_EQ(close(cxt_fd), 0);
+	EXPECT_EQ(close(device_fd), 0);
+}
+
+TEST_F(sdxi_context_nomap, context_create_and_destroy_nomap)
+{
+	// Just run the fixture setup and teardown
+}
+
+TEST(sdxi_create_cxt_rejects_nonzero_flags)
+{
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(arg),
+		.flags = 1,
+		.ds_ring_entries = 1024,
+	};
+	int device_fd;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(device_fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return, "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return, "insufficient privileges");
+	}
+
+	errno = 0;
+	EXPECT_EQ(ioctl(device_fd, SDXI_CREATE_CXT, &arg), -1);
+	EXPECT_EQ(errno, EINVAL);
+	EXPECT_EQ(close(device_fd), 0);
+}
+
+TEST(sdxi_create_cxt_requires_user_dataplane)
+{
+	struct sdxi_create_cxt_args arg = {
+		.argsz = sizeof(arg),
+		.ds_ring_entries = 1024,
+	};
+	int device_fd;
+
+	errno = 0;
+	device_fd = open("/dev/sdxi0", O_RDONLY | O_CLOEXEC);
+	ASSERT_GE(device_fd, 0) {
+		if (errno == ENOENT)
+			SKIP(return, "/dev/sdxi0 does not exist");
+		if (errno == EACCES && geteuid() != 0)
+			SKIP(return, "insufficient privileges");
+	}
+
+	errno = 0;
+	EXPECT_EQ(ioctl(device_fd, SDXI_CREATE_CXT, &arg), -1);
+	EXPECT_EQ(errno, EINVAL);
+	EXPECT_EQ(close(device_fd), 0);
+}
+
+TEST_F(sdxi_context, context_create_and_destroy)
+{
+	EXPECT_TRUE(sdxi_context_running(&self->context));
+}
+
+TEST_F(sdxi_context, cxt_eventfd_bind_unbind)
+{
+	struct sdxi_cxt_eventfd_args args = {};
+	int efd;
+	int rc;
+
+	args.argsz = sizeof(args);
+
+	efd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
+	ASSERT_GE(efd, 0);
+
+	args.eventfd = efd;
+	rc = ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args);
+	if (rc != 0) {
+		if (errno == EOPNOTSUPP) {
+			close(efd);
+			SKIP(return, "context interrupts not supported");
+		}
+		ASSERT_EQ(rc, 0);
+	}
+
+	EXPECT_GE(args.intr_akey, 2);
+	EXPECT_GT(args.intr_num, 0U);
+
+	args.eventfd = -1;
+	args.argsz = sizeof(args);
+	EXPECT_EQ(ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args), 0);
+	EXPECT_EQ(args.intr_akey, 0);
+	EXPECT_EQ(args.intr_num, 0U);
+
+	close(efd);
+}
+
+TEST_F(sdxi_context, akey_iv_requires_cxt_eventfd)
+{
+	uint32_t data[4];
+	int rc;
+
+	akey_pack_intr(data, self->context.pasid, 1, true);
+	rc = set_single_akey(self->context.cxt_fd, 2, data);
+	if (rc == -EOPNOTSUPP)
+		SKIP(return, "context interrupts not supported");
+
+	EXPECT_EQ(rc, -ENOTCONN);
+}
+
+TEST_F(sdxi_context, akey_iv_requires_matching_intr_num)
+{
+	struct sdxi_cxt_eventfd_args args = {};
+	uint32_t data[4];
+	uint32_t wrong_intr;
+	uint16_t test_akey;
+	int efd;
+	int rc;
+
+	args.argsz = sizeof(args);
+
+	efd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
+	ASSERT_GE(efd, 0);
+
+	args.eventfd = efd;
+	rc = ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args);
+	if (rc != 0) {
+		if (errno == EOPNOTSUPP) {
+			close(efd);
+			SKIP(return, "context interrupts not supported");
+		}
+		ASSERT_EQ(rc, 0);
+	}
+
+	ASSERT_NE(args.intr_akey, UINT16_MAX);
+	test_akey = args.intr_akey + 1;
+
+	wrong_intr = (args.intr_num == 1U) ? 2U : 1U;
+	akey_pack_intr(data, self->context.pasid, wrong_intr, true);
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, test_akey, data),
+		  -EINVAL);
+
+	akey_pack_intr(data, self->context.pasid, args.intr_num, true);
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, test_akey, data), 0);
+
+	args.eventfd = -1;
+	args.argsz = sizeof(args);
+	EXPECT_EQ(ioctl(self->context.cxt_fd, SDXI_CXT_EVENTFD, &args), 0);
+	close(efd);
+}
+
+TEST_F(sdxi_context, akey_user_rejects_dsh_fields)
+{
+	uint32_t data[4];
+
+	if (geteuid() == 0)
+		SKIP(return, "non-admin AKey validation test");
+
+	ASSERT_EQ(get_single_akey(self->context.cxt_fd, 1, data), 0);
+
+	data[0] |= AKEY_ENT_STE;
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, 2, data), -EPERM);
+
+	ASSERT_EQ(get_single_akey(self->context.cxt_fd, 1, data), 0);
+
+	data[1] |= 1U << AKEY_ENT_PH_SHIFT;
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, 2, data), -EPERM);
+
+	ASSERT_EQ(get_single_akey(self->context.cxt_fd, 1, data), 0);
+
+	data[2] = 1;
+	EXPECT_EQ(set_single_akey(self->context.cxt_fd, 2, data), -EPERM);
+}
+
+TEST_HARNESS_MAIN
-- 
2.56.1


  parent reply	other threads:[~2026-11-19 20:31 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 20:22 [PATCH v1 00/44] dmaengine: sdxi: add userspace control and management support Wei Huang
2026-11-19 20:22 ` [PATCH v1 01/44] dmaengine: sdxi: cache hardware version for feature gating Wei Huang
2026-11-19 20:22 ` [PATCH v1 02/44] dmaengine: sdxi: define key and error MMIO registers Wei Huang
2026-11-19 20:22 ` [PATCH v1 03/44] dmaengine: sdxi: define SDXI hardware layouts Wei Huang
2026-11-19 20:22 ` [PATCH v1 04/44] dmaengine: sdxi: encode admin descriptors Wei Huang
2026-11-19 20:22 ` [PATCH v1 05/44] dmaengine: sdxi: support timeout-aware completion waits Wei Huang
2026-11-19 20:22 ` [PATCH v1 06/44] dmaengine: sdxi: support the full context table hierarchy Wei Huang
2026-11-19 20:22 ` [PATCH v1 07/44] dmaengine: sdxi: implement synchronous admin operations Wei Huang
2026-11-19 20:22 ` [PATCH v1 08/44] dmaengine: sdxi: add core RKey table management Wei Huang
2026-11-19 20:22 ` [PATCH v1 09/44] dmaengine: sdxi: handle hardware error logs Wei Huang
2026-11-19 20:22 ` [PATCH v1 10/44] dmaengine: sdxi: separate context and submission resources Wei Huang
2026-11-19 20:22 ` [PATCH v1 11/44] dmaengine: sdxi: support userspace context lifetimes Wei Huang
2026-11-19 20:22 ` [PATCH v1 12/44] dmaengine: sdxi: route context interrupts to eventfd Wei Huang
2026-11-19 20:22 ` [PATCH v1 13/44] dmaengine: sdxi: kill user contexts on fatal errors Wei Huang
2026-11-19 20:22 ` [PATCH v1 14/44] dmaengine: sdxi: define the ioctl UAPI surface Wei Huang
2026-11-19 20:22 ` [PATCH v1 15/44] dmaengine: sdxi: add internal ioctl definitions Wei Huang
2026-11-19 20:22 ` [PATCH v1 16/44] dmaengine: sdxi: register core character devices Wei Huang
2026-11-19 20:22 ` [PATCH v1 17/44] dmaengine: sdxi: implement fixed-size ioctl dispatch Wei Huang
2026-11-19 20:22 ` [PATCH v1 18/44] dmaengine: sdxi: implement userspace context file operations Wei Huang
2026-11-19 20:22 ` [PATCH v1 19/44] dmaengine: sdxi: validate and copy key ioctl payloads Wei Huang
2026-11-19 20:22 ` [PATCH v1 20/44] dmaengine: sdxi: implement RKey management ioctls Wei Huang
2026-11-19 20:22 ` [PATCH v1 21/44] dmaengine: sdxi: implement AKey " Wei Huang
2026-11-19 20:22 ` [PATCH v1 22/44] dmaengine: sdxi: expose operation-group discovery Wei Huang
2026-11-19 20:22 ` [PATCH v1 23/44] dmaengine: sdxi: implement typed vendor admin transport Wei Huang
2026-11-19 20:23 ` [PATCH v1 24/44] dmaengine: sdxi: expose character device attributes Wei Huang
2026-11-19 20:23 ` [PATCH v1 25/44] dmaengine: sdxi: add PF and VF device naming Wei Huang
2026-11-19 20:23 ` [PATCH v1 26/44] dmaengine: sdxi: add SDXI trace events Wei Huang
2026-11-19 20:23 ` [PATCH v1 27/44] dmaengine: sdxi: support suspend and resume Wei Huang
2026-11-19 20:23 ` [PATCH v1 28/44] dmaengine: sdxi: amd: define QoS request formats Wei Huang
2026-11-19 20:23 ` [PATCH v1 29/44] dmaengine: sdxi: amd: add QoS validation scaffolding Wei Huang
2026-11-19 20:23 ` [PATCH v1 30/44] dmaengine: sdxi: amd: build QoS admin descriptors Wei Huang
2026-11-19 20:23 ` [PATCH v1 31/44] dmaengine: sdxi: amd: prevalidate QoS operations Wei Huang
2026-11-19 20:23 ` [PATCH v1 32/44] dmaengine: sdxi: amd: execute QoS vendor operations Wei Huang
2026-11-19 20:23 ` [PATCH v1 33/44] dmaengine: sdxi: amd: register QoS vendor handler Wei Huang
2026-11-19 20:23 ` [PATCH v1 34/44] dmaengine: sdxi: amd: test QoS validation with KUnit Wei Huang
2026-11-19 20:23 ` [PATCH v1 35/44] selftests: sdxi: add build scaffold Wei Huang
2026-11-19 20:23 ` [PATCH v1 36/44] selftests: sdxi: add shared descriptor and context helpers Wei Huang
2026-11-19 20:23 ` Wei Huang [this message]
2026-11-19 20:23 ` [PATCH v1 38/44] selftests: sdxi: test NOP descriptor submission Wei Huang
2026-11-19 20:23 ` [PATCH v1 39/44] selftests: sdxi: test copy submission and ring wrap Wei Huang
2026-11-19 20:23 ` [PATCH v1 40/44] selftests: sdxi: exercise dataplane error paths Wei Huang
2026-11-19 20:23 ` [PATCH v1 41/44] selftests: sdxi: test device ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 42/44] selftests: sdxi: test vendor admin ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 43/44] selftests: sdxi: test RKey ioctls Wei Huang
2026-11-19 20:23 ` [PATCH v1 44/44] MAINTAINERS: cover SDXI UAPI, vendor files, and selftests Wei Huang

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=20261119202321.1734355-38-wei.huang2@amd.com \
    --to=wei.huang2@amd.com \
    --cc=Frank.Li@kernel.org \
    --cc=John.Kariuki@amd.com \
    --cc=Shyam.Iyer@dell.com \
    --cc=Stephen.Bates@amd.com \
    --cc=bhelgaas@google.com \
    --cc=dmaengine@vger.kernel.org \
    --cc=jic23@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=nathan.lynch@amd.com \
    --cc=rientjes@google.com \
    --cc=shuah@kernel.org \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=tycho@kernel.org \
    --cc=vkoul@kernel.org \
    --cc=weixugc@google.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®