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 21/44] dmaengine: sdxi: implement AKey management ioctls
Date: Thu, 19 Nov 2026 14:22:57 -0600	[thread overview]
Message-ID: <20261119202321.1734355-22-wei.huang2@amd.com> (raw)
In-Reply-To: <20261119202321.1734355-1-wei.huang2@amd.com>

Implement context-scoped AKey bitmap, read, and write operations. For
security reasons, we constraint caller's requests to local
non-privileged PASID and validate interrupt entries against the bound
eventfd vector.

Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
 drivers/dma/sdxi/ioctl.c |   9 +-
 drivers/dma/sdxi/keys.c  | 234 +++++++++++++++++++++++++++++++++++++++
 drivers/dma/sdxi/keys.h  |   4 +
 3 files changed, 243 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/sdxi/ioctl.c b/drivers/dma/sdxi/ioctl.c
index 4f4b900d2542..f0b298b92430 100644
--- a/drivers/dma/sdxi/ioctl.c
+++ b/drivers/dma/sdxi/ioctl.c
@@ -98,7 +98,8 @@ static int sdxi_ioctl_get_uapi_info(struct file *file, struct sdxi_file *sf,
 			SDXI_UAPI_CAP_DEV_ATTR_MGMT |
 			SDXI_UAPI_CAP_RKEY_MGMT |
 			SDXI_UAPI_CAP_CXT_CREATE |
-			SDXI_UAPI_CAP_CXT_EVENTFD,
+			SDXI_UAPI_CAP_CXT_EVENTFD |
+			SDXI_UAPI_CAP_AKEY_MGMT,
 		},
 	};
 
@@ -581,11 +582,11 @@ static const struct sdxi_ioctl_desc sdxi_ioctls[] = {
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
 	SDXI_IOCTL_DEF(0x09, SDXI_CXT_EVENTFD, sdxi_ioctl_cxt_eventfd,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
-	SDXI_IOCTL_DEF(0x0a, SDXI_GET_AKEY_LIST, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0a, SDXI_GET_AKEY_LIST, sdxi_ioctl_get_akey_list,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
-	SDXI_IOCTL_DEF(0x0b, SDXI_GET_AKEYS, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0b, SDXI_GET_AKEYS, sdxi_ioctl_get_akeys,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
-	SDXI_IOCTL_DEF(0x0c, SDXI_SET_AKEYS, sdxi_ioctl_unsupported,
+	SDXI_IOCTL_DEF(0x0c, SDXI_SET_AKEYS, sdxi_ioctl_set_akeys,
 		       SDXI_IOCTL_F_CONTEXT, SDXI_IOCTL_A_CONTEXT_OWNER),
 	SDXI_IOCTL_DEF(0x0d, SDXI_ADMIN_DISC, sdxi_ioctl_unsupported,
 		       SDXI_IOCTL_F_DEVICE, SDXI_IOCTL_A_PUBLIC_DEVICE),
diff --git a/drivers/dma/sdxi/keys.c b/drivers/dma/sdxi/keys.c
index 75a2855ed13d..0d570daefd34 100644
--- a/drivers/dma/sdxi/keys.c
+++ b/drivers/dma/sdxi/keys.c
@@ -6,6 +6,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/capability.h>
 #include <linux/dma-mapping.h>
 #include <linux/kernel.h>
 #include <linux/log2.h>
@@ -14,7 +15,9 @@
 #include <uapi/linux/sdxi.h>
 
 #include "admin.h"
+#include "context.h"
 #include "hw.h"
+#include "intr.h"
 #include "ioctl.h"
 #include "keys.h"
 #include "mmio.h"
@@ -163,6 +166,87 @@ static void sdxi_rkey_store(struct sdxi_rkey_ent *destination,
 	WRITE_ONCE(destination->intr_num, source->intr_num);
 }
 
+static int sdxi_akey_validate(const struct sdxi_akey_ent *entry)
+{
+	u16 intr_num = le16_to_cpu(entry->intr_num);
+	u32 pasid = le32_to_cpu(entry->pasid);
+
+	if (intr_num & BIT(15))
+		return -EINVAL;
+	if (pasid & ~(SDXI_AKEY_ENT_PASID | SDXI_AKEY_ENT_PR |
+		      SDXI_AKEY_ENT_PH))
+		return -EINVAL;
+	if (memchr_inv(entry->rsvd_0, 0, sizeof(entry->rsvd_0)) ||
+	    memchr_inv(entry->rsvd_1, 0, sizeof(entry->rsvd_1)))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int sdxi_akey_validate_user(const struct sdxi_cxt *cxt,
+				   const struct sdxi_akey_ent *entry)
+{
+	u16 intr_num = le16_to_cpu(entry->intr_num);
+	u32 pasid = le32_to_cpu(entry->pasid);
+
+	if (!(intr_num & SDXI_AKEY_ENT_VL)) {
+		if (intr_num || entry->tgt_sfunc || pasid || entry->stag ||
+		    entry->rkey)
+			return -EINVAL;
+		return 0;
+	}
+
+	if (entry->tgt_sfunc || !(intr_num & SDXI_AKEY_ENT_PV) ||
+	    intr_num & SDXI_AKEY_ENT_STE)
+		return -EPERM;
+	if (FIELD_GET(SDXI_AKEY_ENT_PASID, pasid) != cxt->pasid ||
+	    pasid & (SDXI_AKEY_ENT_PR | SDXI_AKEY_ENT_PH))
+		return -EPERM;
+	if (entry->stag || entry->rkey)
+		return -EPERM;
+	if (!(intr_num & SDXI_AKEY_ENT_IV) &&
+	    FIELD_GET(SDXI_AKEY_ENT_INTR_NUM, intr_num))
+		return -EINVAL;
+
+	return 0;
+}
+
+static bool sdxi_akey_driver_owned(const struct sdxi_cxt *cxt, u32 index)
+{
+	u16 intr_akey = READ_ONCE(cxt->intr_akey);
+
+	return index < 2 || (intr_akey && index == intr_akey);
+}
+
+static int sdxi_akey_validate_interrupt(const struct sdxi_cxt *cxt,
+					const struct sdxi_akey_ent *entry)
+{
+	u16 intr_num = le16_to_cpu(entry->intr_num);
+
+	if (!(intr_num & SDXI_AKEY_ENT_IV))
+		return 0;
+	if (!sdxi_cxt_has_intr_fd(cxt))
+		return -ENOTCONN;
+	if (FIELD_GET(SDXI_AKEY_ENT_INTR_NUM, intr_num) != cxt->intr_vector)
+		return -EINVAL;
+
+	return 0;
+}
+
+static void sdxi_akey_store(struct sdxi_akey_ent *destination,
+			    const struct sdxi_akey_ent *source)
+{
+	struct sdxi_akey_ent entry = *source;
+	u16 intr_num = le16_to_cpu(source->intr_num);
+
+	entry.intr_num = cpu_to_le16(intr_num & ~SDXI_AKEY_ENT_VL);
+	WRITE_ONCE(destination->intr_num, 0);
+	dma_wmb();
+	memcpy(destination, &entry, sizeof(entry));
+	dma_wmb();
+	WRITE_ONCE(destination->intr_num, source->intr_num);
+}
+
 int sdxi_keys_init(struct sdxi_dev *sdxi, u64 cap1)
 {
 	size_t table_size;
@@ -327,3 +411,153 @@ int sdxi_ioctl_set_rkeys(struct file *file, struct sdxi_file *sf, void *data)
 	mutex_unlock(&sdxi->rkey_lock);
 	return ret;
 }
+
+int sdxi_ioctl_get_akey_list(struct file *file, struct sdxi_file *sf,
+			     void *data)
+{
+	struct sdxi_key_list_args *args = data;
+	struct sdxi_cxt *cxt = sf->cxt;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  &args->rsvd0, 1);
+	if (ret)
+		return ret;
+	if (!cxt || sdxi_cxt_is_fatal(cxt))
+		return -ENODEV;
+
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+	ret = sdxi_key_list_fill(args, cxt->akey_table,
+				 ARRAY_SIZE(cxt->akey_table->entry),
+				 sizeof(cxt->akey_table->entry[0]));
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	return ret;
+}
+
+int sdxi_ioctl_get_akeys(struct file *file, struct sdxi_file *sf, void *data)
+{
+	struct sdxi_get_keys_args *args = data;
+	struct sdxi_cxt *cxt = sf->cxt;
+	u32 entries;
+	u32 i;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  &args->rsvd0, 1);
+	if (ret)
+		return ret;
+	if (!cxt || sdxi_cxt_is_fatal(cxt))
+		return -ENODEV;
+
+	entries = ARRAY_SIZE(cxt->akey_table->entry);
+	if (args->nkeys > entries)
+		return -EINVAL;
+	ret = sdxi_key_check_indices(args->entries, args->nkeys, entries,
+				     NULL, NULL);
+	if (ret)
+		return ret;
+
+	args->argsz = sizeof(*args);
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+	for (i = 0; i < args->nkeys; i++)
+		memcpy(args->entries[i].data,
+		       &cxt->akey_table->entry[args->entries[i].index],
+		       sizeof(args->entries[i].data));
+	ret = 0;
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	return ret;
+}
+
+int sdxi_ioctl_set_akeys(struct file *file, struct sdxi_file *sf, void *data)
+{
+	struct sdxi_set_keys_args *args = data;
+	struct sdxi_cxt *cxt = sf->cxt;
+	u32 entries;
+	u16 first;
+	u16 last;
+	u32 i;
+	int ret;
+
+	ret = sdxi_key_args_check(args->argsz, sizeof(*args), args->flags,
+				  args->rsvd0, ARRAY_SIZE(args->rsvd0));
+	if (ret)
+		return ret;
+
+	args->argsz = sizeof(*args);
+	sdxi_set_keys_init(args);
+
+	if (!cxt || sdxi_cxt_is_fatal(cxt))
+		return -ENODEV;
+	entries = ARRAY_SIZE(cxt->akey_table->entry);
+	if (!args->nkeys) {
+		args->commit_state = SDXI_KEY_COMMIT_ALL;
+		return 0;
+	}
+	if (args->nkeys > entries)
+		return -EINVAL;
+
+	ret = sdxi_key_check_indices(args->entries, args->nkeys, entries,
+				     &first, &last);
+	if (ret)
+		return ret;
+
+	mutex_lock(&cxt->akey_lock);
+	if (!cxt->akey_table) {
+		ret = -ENODEV;
+		goto out_unlock;
+	}
+
+	for (i = 0; i < args->nkeys; i++) {
+		struct sdxi_akey_ent entry;
+
+		if (sdxi_akey_driver_owned(cxt, args->entries[i].index)) {
+			ret = -EPERM;
+			goto out_unlock;
+		}
+
+		memcpy(&entry, args->entries[i].data, sizeof(entry));
+		ret = sdxi_akey_validate(&entry);
+		if (ret)
+			goto out_unlock;
+		if (!capable(CAP_SYS_ADMIN)) {
+			ret = sdxi_akey_validate_user(cxt, &entry);
+			if (ret)
+				goto out_unlock;
+		}
+		ret = sdxi_akey_validate_interrupt(cxt, &entry);
+		if (ret)
+			goto out_unlock;
+	}
+
+	for (i = 0; i < args->nkeys; i++) {
+		struct sdxi_akey_ent entry;
+
+		memcpy(&entry, args->entries[i].data, sizeof(entry));
+		sdxi_akey_store(&cxt->akey_table->entry[args->entries[i].index],
+				&entry);
+	}
+	dma_wmb();
+
+	ret = sdxi_adm_akey_update_sync(cxt->sdxi, cxt->id, cxt->id,
+					first, last,
+					sdxi_set_keys_timeout(args), NULL);
+	if (ret) {
+		args->commit_state = SDXI_KEY_COMMIT_PARTIAL;
+		goto out_unlock;
+	}
+
+	args->commit_state = SDXI_KEY_COMMIT_ALL;
+out_unlock:
+	mutex_unlock(&cxt->akey_lock);
+	return ret;
+}
diff --git a/drivers/dma/sdxi/keys.h b/drivers/dma/sdxi/keys.h
index fed5c20a2cfc..6e36527b610a 100644
--- a/drivers/dma/sdxi/keys.h
+++ b/drivers/dma/sdxi/keys.h
@@ -20,5 +20,9 @@ int sdxi_ioctl_get_rkey_list(struct file *file, struct sdxi_file *sf,
 			     void *data);
 int sdxi_ioctl_get_rkeys(struct file *file, struct sdxi_file *sf, void *data);
 int sdxi_ioctl_set_rkeys(struct file *file, struct sdxi_file *sf, void *data);
+int sdxi_ioctl_get_akey_list(struct file *file, struct sdxi_file *sf,
+			     void *data);
+int sdxi_ioctl_get_akeys(struct file *file, struct sdxi_file *sf, void *data);
+int sdxi_ioctl_set_akeys(struct file *file, struct sdxi_file *sf, void *data);
 
 #endif /* DMA_SDXI_KEYS_H */
-- 
2.56.1


  parent reply	other threads:[~2026-11-19 20:27 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 ` Wei Huang [this message]
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 ` [PATCH v1 37/44] selftests: sdxi: test context creation Wei Huang
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-22-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®