Skip to content

Commit

Permalink
iommufd/selftest: Make the mock iommu driver into a real driver
Browse files Browse the repository at this point in the history
I've avoided doing this because there is no way to make this happen
without an intrusion into the core code. Up till now this has avoided
needing the core code's probe path with some hackery - but now that
default domains are becoming mandatory it is unavoidable.

This became a serious problem when the core code stopped allowing
partially registered iommu drivers in commit 14891af ("iommu: Move
the iommu driver sysfs setup into iommu_init/deinit_device()") which
breaks the selftest. That series was developed along with a second series
that contained this patch so it was not noticed.

Make it so that iommufd selftest can create a real iommu driver and bind
it only to is own private bus. Add iommu_device_register_bus() as a core
code helper to make this possible. It simply sets the right pointers and
registers the notifier block. The mock driver then works like any normal
driver should, with probe triggered by the bus ops

When the bus->iommu_ops stuff is fully unwound we can probably do better
here and remove this special case.

Link: https://lore.kernel.org/r/15-v6-e8114faedade+425-iommu_all_defdom_jgg@nvidia.com
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
  • Loading branch information
Jason Gunthorpe committed Aug 14, 2023
1 parent c157fd8 commit 23a1b46
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 59 deletions.
9 changes: 9 additions & 0 deletions drivers/iommu/iommu-priv.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES.
*/
#ifndef __LINUX_IOMMU_PRIV_H
#define __LINUX_IOMMU_PRIV_H

Expand All @@ -7,4 +9,11 @@
int iommu_group_replace_domain(struct iommu_group *group,
struct iommu_domain *new_domain);

int iommu_device_register_bus(struct iommu_device *iommu,
const struct iommu_ops *ops, struct bus_type *bus,
struct notifier_block *nb);
void iommu_device_unregister_bus(struct iommu_device *iommu,
struct bus_type *bus,
struct notifier_block *nb);

#endif /* __LINUX_IOMMU_PRIV_H */
43 changes: 43 additions & 0 deletions drivers/iommu/iommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "iommu-priv.h"

#include "iommu-sva.h"
#include "iommu-priv.h"

static struct kset *iommu_group_kset;
static DEFINE_IDA(iommu_group_ida);
Expand Down Expand Up @@ -288,6 +289,48 @@ void iommu_device_unregister(struct iommu_device *iommu)
}
EXPORT_SYMBOL_GPL(iommu_device_unregister);

#if IS_ENABLED(CONFIG_IOMMUFD_TEST)
void iommu_device_unregister_bus(struct iommu_device *iommu,
struct bus_type *bus,
struct notifier_block *nb)
{
bus_unregister_notifier(bus, nb);
iommu_device_unregister(iommu);
}
EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);

/*
* Register an iommu driver against a single bus. This is only used by iommufd
* selftest to create a mock iommu driver. The caller must provide
* some memory to hold a notifier_block.
*/
int iommu_device_register_bus(struct iommu_device *iommu,
const struct iommu_ops *ops, struct bus_type *bus,
struct notifier_block *nb)
{
int err;

iommu->ops = ops;
nb->notifier_call = iommu_bus_notifier;
err = bus_register_notifier(bus, nb);
if (err)
return err;

spin_lock(&iommu_device_lock);
list_add_tail(&iommu->list, &iommu_device_list);
spin_unlock(&iommu_device_lock);

bus->iommu_ops = ops;
err = bus_iommu_probe(bus);
if (err) {
iommu_device_unregister_bus(iommu, bus, nb);
return err;
}
return 0;
}
EXPORT_SYMBOL_GPL(iommu_device_register_bus);
#endif

static struct dev_iommu *dev_iommu_get(struct device *dev)
{
struct dev_iommu *param = dev->iommu;
Expand Down
5 changes: 3 additions & 2 deletions drivers/iommu/iommufd/iommufd_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ extern size_t iommufd_test_memory_limit;
void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
unsigned int ioas_id, u64 *iova, u32 *flags);
bool iommufd_should_fail(void);
void __init iommufd_test_init(void);
int __init iommufd_test_init(void);
void iommufd_test_exit(void);
bool iommufd_selftest_is_mock_dev(struct device *dev);
#else
Expand All @@ -347,8 +347,9 @@ static inline bool iommufd_should_fail(void)
{
return false;
}
static inline void __init iommufd_test_init(void)
static inline int __init iommufd_test_init(void)
{
return 0;
}
static inline void iommufd_test_exit(void)
{
Expand Down
8 changes: 7 additions & 1 deletion drivers/iommu/iommufd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,14 @@ static int __init iommufd_init(void)
if (ret)
goto err_misc;
}
iommufd_test_init();
ret = iommufd_test_init();
if (ret)
goto err_vfio_misc;
return 0;

err_vfio_misc:
if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER))
misc_deregister(&vfio_misc_dev);
err_misc:
misc_deregister(&iommu_misc_dev);
return ret;
Expand Down
138 changes: 82 additions & 56 deletions drivers/iommu/iommufd/selftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#include <linux/file.h>
#include <linux/anon_inodes.h>
#include <linux/fault-inject.h>
#include <linux/platform_device.h>
#include <uapi/linux/iommufd.h>

#include "../iommu-priv.h"
#include "io_pagetable.h"
#include "iommufd_private.h"
#include "iommufd_test.h"

static DECLARE_FAULT_ATTR(fail_iommufd);
static struct dentry *dbgfs_root;
static struct platform_device *selftest_iommu_dev;

size_t iommufd_test_memory_limit = 65536;

Expand Down Expand Up @@ -135,7 +138,7 @@ static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED)
return &mock_blocking_domain;

if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_UNMANAGED))
if (iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)
return NULL;

mock = kzalloc(sizeof(*mock), GFP_KERNEL);
Expand Down Expand Up @@ -276,12 +279,22 @@ static void mock_domain_set_plaform_dma_ops(struct device *dev)
*/
}

static struct iommu_device mock_iommu_device = {
};

static struct iommu_device *mock_probe_device(struct device *dev)
{
return &mock_iommu_device;
}

static const struct iommu_ops mock_ops = {
.owner = THIS_MODULE,
.pgsize_bitmap = MOCK_IO_PAGE_SIZE,
.domain_alloc = mock_domain_alloc,
.capable = mock_domain_capable,
.set_platform_dma_ops = mock_domain_set_plaform_dma_ops,
.device_group = generic_device_group,
.probe_device = mock_probe_device,
.default_domain_ops =
&(struct iommu_domain_ops){
.free = mock_domain_free,
Expand All @@ -292,10 +305,6 @@ static const struct iommu_ops mock_ops = {
},
};

static struct iommu_device mock_iommu_device = {
.ops = &mock_ops,
};

static inline struct iommufd_hw_pagetable *
get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
struct mock_iommu_domain **mock)
Expand All @@ -316,22 +325,29 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
return hwpt;
}

static struct bus_type iommufd_mock_bus_type = {
.name = "iommufd_mock",
.iommu_ops = &mock_ops,
struct mock_bus_type {
struct bus_type bus;
struct notifier_block nb;
};

static struct mock_bus_type iommufd_mock_bus_type = {
.bus = {
.name = "iommufd_mock",
},
};

static atomic_t mock_dev_num;

static void mock_dev_release(struct device *dev)
{
struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);

atomic_dec(&mock_dev_num);
kfree(mdev);
}

static struct mock_dev *mock_dev_create(void)
{
struct iommu_group *iommu_group;
struct dev_iommu *dev_iommu;
struct mock_dev *mdev;
int rc;

Expand All @@ -341,63 +357,26 @@ static struct mock_dev *mock_dev_create(void)

device_initialize(&mdev->dev);
mdev->dev.release = mock_dev_release;
mdev->dev.bus = &iommufd_mock_bus_type;

iommu_group = iommu_group_alloc();
if (IS_ERR(iommu_group)) {
rc = PTR_ERR(iommu_group);
goto err_put;
}
mdev->dev.bus = &iommufd_mock_bus_type.bus;

rc = dev_set_name(&mdev->dev, "iommufd_mock%u",
iommu_group_id(iommu_group));
atomic_inc_return(&mock_dev_num));
if (rc)
goto err_group;

/*
* The iommu core has no way to associate a single device with an iommu
* driver (heck currently it can't even support two iommu_drivers
* registering). Hack it together with an open coded dev_iommu_get().
* Notice that the normal notifier triggered iommu release process also
* does not work here because this bus is not in iommu_buses.
*/
mdev->dev.iommu = kzalloc(sizeof(*dev_iommu), GFP_KERNEL);
if (!mdev->dev.iommu) {
rc = -ENOMEM;
goto err_group;
}
mutex_init(&mdev->dev.iommu->lock);
mdev->dev.iommu->iommu_dev = &mock_iommu_device;
goto err_put;

rc = device_add(&mdev->dev);
if (rc)
goto err_dev_iommu;

rc = iommu_group_add_device(iommu_group, &mdev->dev);
if (rc)
goto err_del;
iommu_group_put(iommu_group);
goto err_put;
return mdev;

err_del:
device_del(&mdev->dev);
err_dev_iommu:
kfree(mdev->dev.iommu);
mdev->dev.iommu = NULL;
err_group:
iommu_group_put(iommu_group);
err_put:
put_device(&mdev->dev);
return ERR_PTR(rc);
}

static void mock_dev_destroy(struct mock_dev *mdev)
{
iommu_group_remove_device(&mdev->dev);
device_del(&mdev->dev);
kfree(mdev->dev.iommu);
mdev->dev.iommu = NULL;
put_device(&mdev->dev);
device_unregister(&mdev->dev);
}

bool iommufd_selftest_is_mock_dev(struct device *dev)
Expand Down Expand Up @@ -444,9 +423,14 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
cmd->mock_domain.out_hwpt_id = pt_id;
cmd->mock_domain.out_stdev_id = sobj->obj.id;
cmd->mock_domain.out_idev_id = idev_id;
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
if (rc)
goto out_detach;
iommufd_object_finalize(ucmd->ictx, &sobj->obj);
return iommufd_ucmd_respond(ucmd, sizeof(*cmd));
return 0;

out_detach:
iommufd_device_detach(idev);
out_unbind:
iommufd_device_unbind(idev);
out_mdev:
Expand Down Expand Up @@ -1051,15 +1035,57 @@ bool iommufd_should_fail(void)
return should_fail(&fail_iommufd, 1);
}

void __init iommufd_test_init(void)
int __init iommufd_test_init(void)
{
struct platform_device_info pdevinfo = {
.name = "iommufd_selftest_iommu",
};
int rc;

dbgfs_root =
fault_create_debugfs_attr("fail_iommufd", NULL, &fail_iommufd);
WARN_ON(bus_register(&iommufd_mock_bus_type));

selftest_iommu_dev = platform_device_register_full(&pdevinfo);
if (IS_ERR(selftest_iommu_dev)) {
rc = PTR_ERR(selftest_iommu_dev);
goto err_dbgfs;
}

rc = bus_register(&iommufd_mock_bus_type.bus);
if (rc)
goto err_platform;

rc = iommu_device_sysfs_add(&mock_iommu_device,
&selftest_iommu_dev->dev, NULL, "%s",
dev_name(&selftest_iommu_dev->dev));
if (rc)
goto err_bus;

rc = iommu_device_register_bus(&mock_iommu_device, &mock_ops,
&iommufd_mock_bus_type.bus,
&iommufd_mock_bus_type.nb);
if (rc)
goto err_sysfs;
return 0;

err_sysfs:
iommu_device_sysfs_remove(&mock_iommu_device);
err_bus:
bus_unregister(&iommufd_mock_bus_type.bus);
err_platform:
platform_device_del(selftest_iommu_dev);
err_dbgfs:
debugfs_remove_recursive(dbgfs_root);
return rc;
}

void iommufd_test_exit(void)
{
iommu_device_sysfs_remove(&mock_iommu_device);
iommu_device_unregister_bus(&mock_iommu_device,
&iommufd_mock_bus_type.bus,
&iommufd_mock_bus_type.nb);
bus_unregister(&iommufd_mock_bus_type.bus);
platform_device_del(selftest_iommu_dev);
debugfs_remove_recursive(dbgfs_root);
bus_unregister(&iommufd_mock_bus_type);
}

0 comments on commit 23a1b46

Please sign in to comment.