From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795409408; b=kMZ+iOh2JMt3WZAMI+OrqpCY8ay9oCYpDVao+yphOE8C3a1OM/bvc++fNuxGL3qPTqy7 T5DO2qOUR9PSF+p3hbAaxWzBgsU8V8nI2Db4Rp9pUS4/7dhX4jnuzWe5S0LzJ/Ltd+/KZ TzFSWSGSxvDuHxDzclfb1z7M19tPocTU8Bfl6L2+pk10ZNKUIU04zac78tx8sPUGVlB1U 3NL2PxDOlUHWLK6NHrmyXDgsJC/KOHuv9orw/w6KQMtBvr8uCGpdv4+ETY7KhjAWl3Z/V jRLdwTy8qMLP+snTHtJYupsKcT9fDDMCgFIF1Mpw9wr+eF0owYnGfY/4g5wJZcwrlRg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795409408; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=TZMq+/zpLeRacFwEoqnJChMXkGO66CqIjVvdz5YYwDY=; b=N8zOiExpDxvEKZAgYfPrzDuQDoP/czjYCCyJixmQ7OEul+Ck1jvorrhYhxC7AR/smpft uQ3vQx+Xd9m2EcOxNx/dTBjQex86KrgfvQfpW3EW2VOtNOYkFlLmLcwD67inIwCdTzxRz pExAqYJhS9SigHa35OF5pblnGkWPi8hEee6iQzq0Wk3AYgbAflPMTHXLiGY+962oD6iOu 1CJcasfwBFepDurm5EoLyQUCPDt3WYzYCGwlZuB9vSssqz+3U23Wn1EErbSLfrQcaQRV9 +VM8dymuiydtGzoMINc5T0mb3CTA/LFbymLst5RxFPrh9rPr78rXzlR5GjWoUk5BVSg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=b15CGNlM; arc=none smtp.remote-ip=209.85.216.70 Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E155606A6AB for ; Mon, 23 Nov 2026 04:50:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3E155606A6AB Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=b15CGNlM Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-3b18165dc53so3377435a91.3 for ; Sun, 22 Nov 2026 20:50:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795409407; x=1796014207; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=TZMq+/zpLeRacFwEoqnJChMXkGO66CqIjVvdz5YYwDY=; b=b15CGNlMkt6eYAZ+UH62i6njhgUTa2NnbQdvL+xaLQczC0wggzr4wENHh3ODkbuKKj y+SkgaKJOoCPIbVTqCk2w0ZC4laF5I/Tbyrz7Ne5pdO7+9NdXuCyymuy68p7TkYOFB0V oiSMXpkW+PwhkGVyLDapa0JoCLyikQGpURfGG4oWx1tnWt1s0Fy2plRvu9U8+1wdl2wS 2WuxHRciGjWItBp36aft+SkF6sUlD7EDG8KqTR0sFytLsaT3Vq7y4SSIOucSstswdXE7 L8Si4TP6qV+ah41pjloYFbV8EdruGb1boiTqZVdbN84cVOoKnM7WqRGvhYrPMY34kTCh PrKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795409407; x=1796014207; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=TZMq+/zpLeRacFwEoqnJChMXkGO66CqIjVvdz5YYwDY=; b=Pkgncwu73fYYRVzc+Pw6e6Wb6KnL2sRd/0KAHci6xC5+INe/m8/HjSPHLe1z8bzgR4 dgHbbogxEbBzqpn7UUzgAcYBfxcQ4ql7nqgLrGnH3YJJb72gYNnBV/+nKfw1QrZubwVG bUQDmcOemt18knSRsCocwQxyHXFT1G5dpPjaggiUvtssGQlx/PLbbA00sqxrP/+bKp3N DekHRTyql+aU6S3HGcuamm/6DACD/jHTC2BCH5txKU4bPi8244lz+IMNP0O3834mPQPv Q0tMiDBNsvDE9U80SwJx5VRyVsbrG1N4WqJnoQt7Wcz98XvA7kR8jYn5cJgvRWcd9oKi m5UA== X-Forwarded-Encrypted: i=1; AL+hErE8qC2Hbn3XQLqWRgNg7q0h15b/axkcUJcaKqEJBM6eK82y1zJc6vTekE9C1JcpDLi6UBFTX++7iNAacp8=@vger.kernel.org X-Gm-Message-State: APuU4Z+TkNEySJ9MyM49hYrCMstgy/0PgmJUFvA3O7g5nhWXlUDzoxrZ 6WnevTzGHzFTJ5rjZJcT72xKeXmKVexvDrSThcFmI/9g2z8X8nossuNu818mddY0R7eqC155jvq 6UyvoyxcmPd5LDw== X-Received: from pjbct21.prod.google.com ([2002:a17:90a:f595:b0:3a5:f1d:43a6]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4d89:b0:3b4:3c33:3e5d with SMTP id 98e67ed59e1d1-3b43c334caemr4000575a91.67.1795409406304; Sun, 22 Nov 2026 20:50:06 -0800 (PST) Date: Mon, 23 Nov 2026 04:49:49 +0000 In-Reply-To: <20261123044949.3610702-1-skhawaja@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261123044949.3610702-1-skhawaja@google.com> X-Mailer: git-send-email 2.56.1.809.ga0b61035b5-goog Message-ID: <20261123044949.3610702-19-skhawaja@google.com> Subject: [PATCH v8 18/18] iommufd/selftest: Add test to verify iommufd preservation From: Samiullah Khawaja To: Joerg Roedel , Will Deacon , Lu Baolu , Jason Gunthorpe , David Woodhouse Cc: Samiullah Khawaja , YiFei Zhu , Robin Murphy , Kevin Tian , Alex Williamson , Shuah Khan , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Pratyush Yadav , Pasha Tatashin , David Matlack , Andrew Morton , Pranjal Shrivastava , Vipin Sharma , Ankit Soni , Nicolin Chen , John Starks Content-Type: text/plain; charset="UTF-8" Test iommufd preservation by setting up an iommufd with a HWPT and preserving it across live update. The test takes the VFIO cdev path of a device bound to the vfio-pci driver, binds it to the iommufd and attaches it to the HWPT that is marked for preservation. It also checks that the iommufd can't be preserved without its memfd or with hole-punched pages. Add IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV to preserve the device, since the vfio cdev preservation is not available yet. On restore, the test verifies that the restored device can't be bound to a new iommufd, that the iommufd can't be retrieved, as the iommufd restore is not supported yet, and that the LUO session finish fails. Signed-off-by: Samiullah Khawaja Signed-off-by: YiFei Zhu --- drivers/iommu/iommufd/device.c | 47 ++- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/iommufd_test.h | 1 + drivers/iommu/iommufd/selftest.c | 29 ++ tools/testing/selftests/iommu/Makefile | 21 ++ .../iommu/iommufd_liveupdate_kexec_test.c | 276 ++++++++++++++++++ 6 files changed, 359 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index ebfc96445f71..95e31d398b84 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -1774,17 +1774,8 @@ static bool _iommufd_device_has_pasid_attachments(struct iommufd_device *idev) return false; } -/** - * iommufd_device_preserve() - Preserve an iommufd device across live update - * @s: Live update session - * @idev: Target iommufd device - * @iommufd_tokenp: Pointer to store outgoing iommufd token - * - * Return: 0 on success, or negative error code. - */ -int iommufd_device_preserve(struct liveupdate_session *s, - struct iommufd_device *idev, - u64 *iommufd_tokenp) +/* Preserve @idev using @token as its DMA owner token. */ +int __iommufd_device_preserve(struct iommufd_device *idev, u64 token) { struct iommufd_hwpt_paging *hwpt_paging; struct iommufd_hw_pagetable *hwpt; @@ -1828,10 +1819,6 @@ int iommufd_device_preserve(struct liveupdate_session *s, } hwpt_paging = to_hwpt_paging(hwpt); - ret = liveupdate_get_token_outgoing(s, idev->ictx->file, iommufd_tokenp); - if (ret) - goto out; - if (!hwpt_paging->liveupdate_preserved) { ret = -EINVAL; goto out; @@ -1839,7 +1826,7 @@ int iommufd_device_preserve(struct liveupdate_session *s, ret = iommu_preserve_device(hwpt_paging->common.domain, idev->dev, - *iommufd_tokenp); + token); if (!ret) idev->liveupdate_preserved = true; @@ -1847,6 +1834,34 @@ int iommufd_device_preserve(struct liveupdate_session *s, mutex_unlock(&igroup->lock); return ret; } + +/** + * iommufd_device_preserve() - Preserve an iommufd device across live update + * @s: Live update session + * @idev: Target iommufd device + * @iommufd_tokenp: Pointer to store outgoing iommufd token + * + * Return: 0 on success, or negative error code. + */ +int iommufd_device_preserve(struct liveupdate_session *s, + struct iommufd_device *idev, + u64 *iommufd_tokenp) +{ + int ret; + + if (!idev) + return -EINVAL; + + /* + * The iommufd must be preserved in the same session, which also makes + * sure its HWPT preservation is done before the device is preserved. + */ + ret = liveupdate_get_token_outgoing(s, idev->ictx->file, iommufd_tokenp); + if (ret) + return ret; + + return __iommufd_device_preserve(idev, *iommufd_tokenp); +} EXPORT_SYMBOL_NS_GPL(iommufd_device_preserve, "IOMMUFD"); /** diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index df231d1e7d09..b342ee5b8ed3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -749,6 +749,7 @@ int iommufd_liveupdate_register(void); void iommufd_liveupdate_unregister(void); int iommufd_hwpt_liveupdate_mark_preserve(struct iommufd_ucmd *ucmd); +int __iommufd_device_preserve(struct iommufd_device *idev, u64 token); #else static inline int iommufd_liveupdate_register(void) { diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 52b78cbcc920..2f68abe52bfe 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -31,6 +31,7 @@ enum { IOMMU_TEST_OP_PASID_CHECK_HWPT, IOMMU_TEST_OP_DMABUF_GET, IOMMU_TEST_OP_DMABUF_REVOKE, + IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV, }; enum { diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 037e54f3d2ba..d904482124e1 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -2115,6 +2115,33 @@ static int iommufd_test_dmabuf_revoke(struct iommufd_ucmd *ucmd, int fd, return rc; } +/* + * Preserve the IOMMU state of a device bound to this iommufd, as a vfio cdev + * preserved in the same live update session would. The iommufd must already + * be preserved, so the HWPT of the device is preserved. + */ +static int iommufd_test_liveupdate_preserve_dev(struct iommufd_ucmd *ucmd, + u32 dev_id) +{ +#ifdef CONFIG_IOMMU_LIVEUPDATE + struct iommufd_device *idev; + int rc; + + idev = iommufd_get_device(ucmd, dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + mutex_lock(&ucmd->ictx->liveupdate_mutex); + rc = __iommufd_device_preserve(idev, 0); + mutex_unlock(&ucmd->ictx->liveupdate_mutex); + + iommufd_put_object(ucmd->ictx, &idev->obj); + return rc; +#else + return -EOPNOTSUPP; +#endif +} + int iommufd_test(struct iommufd_ucmd *ucmd) { struct iommu_test_cmd *cmd = ucmd->cmd; @@ -2200,6 +2227,8 @@ int iommufd_test(struct iommufd_ucmd *ucmd) return iommufd_test_dmabuf_revoke(ucmd, cmd->dmabuf_revoke.dmabuf_fd, cmd->dmabuf_revoke.revoked); + case IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV: + return iommufd_test_liveupdate_preserve_dev(ucmd, cmd->id); default: return -EOPNOTSUPP; } diff --git a/tools/testing/selftests/iommu/Makefile b/tools/testing/selftests/iommu/Makefile index 84abeb2f0949..89be2ff830c6 100644 --- a/tools/testing/selftests/iommu/Makefile +++ b/tools/testing/selftests/iommu/Makefile @@ -7,4 +7,25 @@ TEST_GEN_PROGS := TEST_GEN_PROGS += iommufd TEST_GEN_PROGS += iommufd_fail_nth +TEST_GEN_PROGS_EXTENDED += iommufd_liveupdate_kexec_test + include ../lib.mk +include ../liveupdate/lib/libliveupdate.mk + +CFLAGS += -I$(top_srcdir)/tools/include +CFLAGS += -MD +CFLAGS += $(EXTRA_CFLAGS) + +TEST_O := $(patsubst %, %.o, $(TEST_GEN_PROGS_EXTENDED)) +$(TEST_O): $(OUTPUT)/%.o: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + +TEST_DEP_FILES := $(patsubst %.o, %.d, $(LIBLIVEUPDATE_O) $(TEST_O)) +-include $(TEST_DEP_FILES) + +$(TEST_GEN_PROGS_EXTENDED): $(OUTPUT)/%: $(OUTPUT)/%.o $(LIBLIVEUPDATE_O) + $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $< $(LIBLIVEUPDATE_O) $(LDLIBS) -o $@ + +EXTRA_CLEAN += $(TEST_O) +EXTRA_CLEAN += $(TEST_DEP_FILES) +EXTRA_CLEAN += $(patsubst %, %.d, $(TEST_GEN_PROGS)) diff --git a/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c b/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c new file mode 100644 index 000000000000..3202272ec27c --- /dev/null +++ b/tools/testing/selftests/iommu/iommufd_liveupdate_kexec_test.c @@ -0,0 +1,276 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (c) 2026, Google LLC. + * Samiullah Khawaja + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#define __EXPORTED_HEADERS__ +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "../../../../drivers/iommu/iommufd/iommufd_test.h" + +#define ksft_assert(condition) \ + do { \ + if (!(condition)) \ + fail_exit("Failed: %s", #condition); \ + } while (0) + +static const char *device_cdev_path; +static const char state_session[] = "state-iommufd-cdev"; +static const char iommufd_session[] = "iommufd-test-cdev"; + +static const uint64_t STATE_TOKEN; +static const uint64_t IOMMUFD_TOKEN = 0x123456; +static const uint64_t HWPT_TOKEN = 0x789012; +static const uint64_t MEMFD_TOKEN = 0x890123; + +static int open_cdev(const char *vfio_cdev_path) +{ + int cdev_fd; + + cdev_fd = open(vfio_cdev_path, O_RDWR); + if (cdev_fd < 0) + ksft_exit_skip("Failed to open VFIO cdev: %s\n", vfio_cdev_path); + + return cdev_fd; +} + +static int open_iommufd(void) +{ + int iommufd; + + iommufd = open("/dev/iommu", O_RDWR); + if (iommufd < 0) + ksft_exit_skip("Failed to open /dev/iommu. IOMMUFD support not enabled.\n"); + + return iommufd; +} + +static int create_memfd(size_t size) +{ + int fd, ret; + + fd = memfd_create("buffer", 0); + if (fd < 0) + fail_exit("memfd_create failed"); + + ret = ftruncate(fd, size); + if (ret) + fail_exit("ftruncate failed"); + + return fd; +} + +#define test_ioctl(fd, cmd, arg) \ + do { \ + if (ioctl(fd, cmd, arg)) \ + fail_exit("ioctl(%s) failed", #cmd); \ + } while (0) + +#define test_luo_session_preserve_fd(session, fd, token) \ + do { \ + if (luo_session_preserve_fd(session, fd, token)) \ + fail_exit("luo_session_preserve_fd(%s) failed", #token); \ + } while (0) + +static uint32_t setup_iommufd(int iommufd, int memfd, int cdev_fd, + uint32_t *dev_id) +{ + struct vfio_device_bind_iommufd bind = { + .argsz = sizeof(bind), + .flags = 0, + .iommufd = iommufd, + }; + struct iommu_ioas_alloc alloc_data = { + .size = sizeof(alloc_data), + .flags = 0, + }; + struct iommu_hwpt_alloc hwpt_alloc = { + .size = sizeof(hwpt_alloc), + .flags = 0, + }; + struct vfio_device_attach_iommufd_pt attach_data = { + .argsz = sizeof(attach_data), + .flags = 0, + }; + struct iommu_hwpt_liveupdate_mark_preserve mark_preserve = { + .size = sizeof(mark_preserve), + .hwpt_token = HWPT_TOKEN, + }; + struct iommu_ioas_map_file map_file = { + .size = sizeof(map_file), + .length = SZ_1M, + .flags = IOMMU_IOAS_MAP_WRITEABLE | + IOMMU_IOAS_MAP_READABLE | + IOMMU_IOAS_MAP_FIXED_IOVA, + .iova = SZ_4G, + .fd = memfd, + .start = 0, + }; + + test_ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind); + + test_ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data); + + hwpt_alloc.dev_id = bind.out_devid; + hwpt_alloc.pt_id = alloc_data.out_ioas_id; + test_ioctl(iommufd, IOMMU_HWPT_ALLOC, &hwpt_alloc); + + attach_data.pt_id = hwpt_alloc.out_hwpt_id; + test_ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data); + + map_file.ioas_id = alloc_data.out_ioas_id; + test_ioctl(iommufd, IOMMU_IOAS_MAP_FILE, &map_file); + + map_file.iova = SZ_4G + SZ_1M; + map_file.start = SZ_1M; + test_ioctl(iommufd, IOMMU_IOAS_MAP_FILE, &map_file); + + mark_preserve.hwpt_id = attach_data.pt_id; + test_ioctl(iommufd, IOMMU_HWPT_LIVEUPDATE_MARK_PRESERVE, &mark_preserve); + + *dev_id = bind.out_devid; + return alloc_data.out_ioas_id; +} + +/* Preserve the IOMMU state of the device, using the iommufd selftest support. */ +static int preserve_device(int iommufd, uint32_t dev_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_LIVEUPDATE_PRESERVE_DEV, + .id = dev_id, + }; + + return ioctl(iommufd, IOMMU_TEST_CMD, &cmd); +} + +static void before_kexec(int luo_fd) +{ + struct iommu_ioas_unmap unmap = { + .size = sizeof(unmap), + .iova = SZ_4G + SZ_1M, + .length = SZ_1M, + }; + int iommufd, cdev_fd, memfd, session; + uint32_t ioas_id, dev_id; + + create_state_file(luo_fd, state_session, STATE_TOKEN, /*next_stage=*/2); + + session = luo_create_session(luo_fd, iommufd_session); + if (session < 0) + fail_exit("luo_create_session failed"); + + iommufd = open_iommufd(); + memfd = create_memfd(SZ_2M); + cdev_fd = open_cdev(device_cdev_path); + + ioas_id = setup_iommufd(iommufd, memfd, cdev_fd, &dev_id); + + /* Cannot preserve the device before its HWPT is preserved. */ + if (!preserve_device(iommufd, dev_id)) + fail_exit("Preserving device before iommufd should fail"); + if (errno == ENOTTY) + ksft_exit_skip("IOMMUFD selftest support not enabled\n"); + + /* Cannot preserve iommufd without preserving memfd. */ + if (!luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN)) + fail_exit("Preserving iommufd without memfd should fail"); + + if (fallocate(memfd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + SZ_1M, SZ_1M)) + fail_exit("fallocate punch hole failed"); + + test_luo_session_preserve_fd(session, memfd, MEMFD_TOKEN); + + /* Cannot preserve iommufd when mapped memfd pages have a hole punched. */ + if (!luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN)) + fail_exit("Preserving iommufd with hole-punched pages should fail"); + + unmap.ioas_id = ioas_id; + test_ioctl(iommufd, IOMMU_IOAS_UNMAP, &unmap); + + test_luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN); + + if (preserve_device(iommufd, dev_id)) + fail_exit("Preserving device failed"); + + if (!preserve_device(iommufd, dev_id) || errno != EBUSY) + fail_exit("Preserving device twice should fail with EBUSY"); + + /* Closing the session unpreserves the device and the iommufd. */ + close(session); + session = luo_create_session(luo_fd, iommufd_session); + if (session < 0) + fail_exit("luo_create_session failed"); + + test_luo_session_preserve_fd(session, memfd, MEMFD_TOKEN); + test_luo_session_preserve_fd(session, iommufd, IOMMUFD_TOKEN); + + if (preserve_device(iommufd, dev_id)) + fail_exit("Preserving device failed"); + + close(luo_fd); + daemonize_and_wait(); +} + +static void after_kexec(int luo_fd, int state_session_fd) +{ + struct vfio_device_bind_iommufd bind = { + .argsz = sizeof(bind), + .flags = 0, + }; + int iommufd, cdev_fd, session, stage; + + restore_and_read_stage(state_session_fd, STATE_TOKEN, &stage); + ksft_assert(stage == 2); + + /* The restored device is owned until its iommufd is restored. */ + cdev_fd = open_cdev(device_cdev_path); + bind.iommufd = open_iommufd(); + if (!ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind) || errno != EPERM) + fail_exit("Binding the restored device should fail with EPERM"); + + session = luo_retrieve_session(luo_fd, iommufd_session); + if (session < 0) + fail_exit("luo_retrieve_session failed"); + + iommufd = luo_session_retrieve_fd(session, IOMMUFD_TOKEN); + if (iommufd >= 0) + fail_exit("iommufd should not be retrievable yet"); + + /* Should fail */ + if (luo_session_finish(session) == 0) + fail_exit("luo_session_finish should fail if iommufd is not restored"); +} + +int main(int argc, char *argv[]) +{ + /* Skip the stage option, luo_test() parses it again. */ + while (getopt(argc, argv, "s:") != -1) + ; + + if (optind >= argc) { + printf("Usage: %s [-s ] \n", argv[0]); + return 1; + } + + device_cdev_path = argv[optind]; + + return luo_test(argc, argv, state_session, before_kexec, after_kexec); +} -- 2.56.1.809.ga0b61035b5-goog