[PATCH 07/15] misc: nnpi: Disallow host memory resource access if no NNP-I devices exist

From: Guy Zadicario
Date: Wed May 12 2021 - 03:12:08 EST


There is no point allowing a user application to create host resources
on a system that does not equipped with any NNP-I devices.
Fail openning the nnpi_host character device when no NNP-I devices are
attached.
It is OK to do that check without any synchronization as a race would not
be an issue.

Signed-off-by: Guy Zadicario <guy.zadicario@xxxxxxxxx>
Reviewed-by: Alexander Shishkin <alexander.shishkin@xxxxxxxxxxxxxxx>
---
drivers/misc/intel-nnpi/device.c | 5 +++++
drivers/misc/intel-nnpi/device.h | 2 ++
drivers/misc/intel-nnpi/host_chardev.c | 7 +++++++
3 files changed, 14 insertions(+)

diff --git a/drivers/misc/intel-nnpi/device.c b/drivers/misc/intel-nnpi/device.c
index 0f98398..a3c6a1d 100644
--- a/drivers/misc/intel-nnpi/device.c
+++ b/drivers/misc/intel-nnpi/device.c
@@ -13,6 +13,11 @@

static DEFINE_IDA(dev_ida);

+bool nnpdev_no_devices(void)
+{
+ return ida_is_empty(&dev_ida);
+}
+
/**
* nnpdev_init() - initialize NNP-I device structure.
* @nnpdev: device to be initialized
diff --git a/drivers/misc/intel-nnpi/device.h b/drivers/misc/intel-nnpi/device.h
index 7d7ef60..562bbc4 100644
--- a/drivers/misc/intel-nnpi/device.h
+++ b/drivers/misc/intel-nnpi/device.h
@@ -36,6 +36,8 @@ struct nnp_device_ops {
int (*cmdq_write_mesg)(struct nnp_device *nnpdev, u64 *msg, u32 size);
};

+bool nnpdev_no_devices(void);
+
/*
* Functions exported by the device framework module which are
* called by the lower layer NNP-I device driver module
diff --git a/drivers/misc/intel-nnpi/host_chardev.c b/drivers/misc/intel-nnpi/host_chardev.c
index 6048fda..fad5954 100644
--- a/drivers/misc/intel-nnpi/host_chardev.c
+++ b/drivers/misc/intel-nnpi/host_chardev.c
@@ -217,6 +217,13 @@ static int host_open(struct inode *inode, struct file *f)
if (!is_host_file(f))
return -EINVAL;

+ /*
+ * No point to serve host resource creation while no
+ * NNP-I devices exist in the system.
+ */
+ if (nnpdev_no_devices())
+ return -ENODEV;
+
user_info = kzalloc(sizeof(*user_info), GFP_KERNEL);
if (!user_info)
return -ENOMEM;
--
1.8.3.1

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.