xref: /linux-6.15/drivers/vfio/mdev/mdev_core.c (revision 417fd5bf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Mediated device Core Driver
4  *
5  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6  *     Author: Neo Jia <[email protected]>
7  *             Kirti Wankhede <[email protected]>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/uuid.h>
14 #include <linux/sysfs.h>
15 #include <linux/mdev.h>
16 
17 #include "mdev_private.h"
18 
19 #define DRIVER_VERSION		"0.1"
20 #define DRIVER_AUTHOR		"NVIDIA Corporation"
21 #define DRIVER_DESC		"Mediated device Core Driver"
22 
23 static LIST_HEAD(parent_list);
24 static DEFINE_MUTEX(parent_list_lock);
25 static struct class_compat *mdev_bus_compat_class;
26 
27 static LIST_HEAD(mdev_list);
28 static DEFINE_MUTEX(mdev_list_lock);
29 
30 struct device *mdev_parent_dev(struct mdev_device *mdev)
31 {
32 	return mdev->parent->dev;
33 }
34 EXPORT_SYMBOL(mdev_parent_dev);
35 
36 /* Should be called holding parent_list_lock */
37 static struct mdev_parent *__find_parent_device(struct device *dev)
38 {
39 	struct mdev_parent *parent;
40 
41 	list_for_each_entry(parent, &parent_list, next) {
42 		if (parent->dev == dev)
43 			return parent;
44 	}
45 	return NULL;
46 }
47 
48 static void mdev_release_parent(struct kref *kref)
49 {
50 	struct mdev_parent *parent = container_of(kref, struct mdev_parent,
51 						  ref);
52 	struct device *dev = parent->dev;
53 
54 	kfree(parent);
55 	put_device(dev);
56 }
57 
58 static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
59 {
60 	if (parent)
61 		kref_get(&parent->ref);
62 
63 	return parent;
64 }
65 
66 static void mdev_put_parent(struct mdev_parent *parent)
67 {
68 	if (parent)
69 		kref_put(&parent->ref, mdev_release_parent);
70 }
71 
72 /* Caller must hold parent unreg_sem read or write lock */
73 static void mdev_device_remove_common(struct mdev_device *mdev)
74 {
75 	struct mdev_parent *parent;
76 	int ret;
77 
78 	mdev_remove_sysfs_files(mdev);
79 	device_del(&mdev->dev);
80 	parent = mdev->parent;
81 	lockdep_assert_held(&parent->unreg_sem);
82 	ret = parent->ops->remove(mdev);
83 	if (ret)
84 		dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
85 
86 	/* Balances with device_initialize() */
87 	put_device(&mdev->dev);
88 	mdev_put_parent(parent);
89 }
90 
91 static int mdev_device_remove_cb(struct device *dev, void *data)
92 {
93 	struct mdev_device *mdev = mdev_from_dev(dev);
94 
95 	if (mdev)
96 		mdev_device_remove_common(mdev);
97 	return 0;
98 }
99 
100 /*
101  * mdev_register_device : Register a device
102  * @dev: device structure representing parent device.
103  * @ops: Parent device operation structure to be registered.
104  *
105  * Add device to list of registered parent devices.
106  * Returns a negative value on error, otherwise 0.
107  */
108 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
109 {
110 	int ret;
111 	struct mdev_parent *parent;
112 	char *env_string = "MDEV_STATE=registered";
113 	char *envp[] = { env_string, NULL };
114 
115 	/* check for mandatory ops */
116 	if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
117 		return -EINVAL;
118 
119 	dev = get_device(dev);
120 	if (!dev)
121 		return -EINVAL;
122 
123 	/* Not mandatory, but its absence could be a problem */
124 	if (!ops->request)
125 		dev_info(dev, "Driver cannot be asked to release device\n");
126 
127 	mutex_lock(&parent_list_lock);
128 
129 	/* Check for duplicate */
130 	parent = __find_parent_device(dev);
131 	if (parent) {
132 		parent = NULL;
133 		ret = -EEXIST;
134 		goto add_dev_err;
135 	}
136 
137 	parent = kzalloc(sizeof(*parent), GFP_KERNEL);
138 	if (!parent) {
139 		ret = -ENOMEM;
140 		goto add_dev_err;
141 	}
142 
143 	kref_init(&parent->ref);
144 	init_rwsem(&parent->unreg_sem);
145 
146 	parent->dev = dev;
147 	parent->ops = ops;
148 
149 	if (!mdev_bus_compat_class) {
150 		mdev_bus_compat_class = class_compat_register("mdev_bus");
151 		if (!mdev_bus_compat_class) {
152 			ret = -ENOMEM;
153 			goto add_dev_err;
154 		}
155 	}
156 
157 	ret = parent_create_sysfs_files(parent);
158 	if (ret)
159 		goto add_dev_err;
160 
161 	ret = class_compat_create_link(mdev_bus_compat_class, dev, NULL);
162 	if (ret)
163 		dev_warn(dev, "Failed to create compatibility class link\n");
164 
165 	list_add(&parent->next, &parent_list);
166 	mutex_unlock(&parent_list_lock);
167 
168 	dev_info(dev, "MDEV: Registered\n");
169 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
170 
171 	return 0;
172 
173 add_dev_err:
174 	mutex_unlock(&parent_list_lock);
175 	if (parent)
176 		mdev_put_parent(parent);
177 	else
178 		put_device(dev);
179 	return ret;
180 }
181 EXPORT_SYMBOL(mdev_register_device);
182 
183 /*
184  * mdev_unregister_device : Unregister a parent device
185  * @dev: device structure representing parent device.
186  *
187  * Remove device from list of registered parent devices. Give a chance to free
188  * existing mediated devices for given device.
189  */
190 
191 void mdev_unregister_device(struct device *dev)
192 {
193 	struct mdev_parent *parent;
194 	char *env_string = "MDEV_STATE=unregistered";
195 	char *envp[] = { env_string, NULL };
196 
197 	mutex_lock(&parent_list_lock);
198 	parent = __find_parent_device(dev);
199 
200 	if (!parent) {
201 		mutex_unlock(&parent_list_lock);
202 		return;
203 	}
204 	dev_info(dev, "MDEV: Unregistering\n");
205 
206 	list_del(&parent->next);
207 	mutex_unlock(&parent_list_lock);
208 
209 	down_write(&parent->unreg_sem);
210 
211 	class_compat_remove_link(mdev_bus_compat_class, dev, NULL);
212 
213 	device_for_each_child(dev, NULL, mdev_device_remove_cb);
214 
215 	parent_remove_sysfs_files(parent);
216 	up_write(&parent->unreg_sem);
217 
218 	mdev_put_parent(parent);
219 
220 	/* We still have the caller's reference to use for the uevent */
221 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
222 }
223 EXPORT_SYMBOL(mdev_unregister_device);
224 
225 static void mdev_device_free(struct mdev_device *mdev)
226 {
227 	mutex_lock(&mdev_list_lock);
228 	list_del(&mdev->next);
229 	mutex_unlock(&mdev_list_lock);
230 
231 	dev_dbg(&mdev->dev, "MDEV: destroying\n");
232 	kfree(mdev);
233 }
234 
235 static void mdev_device_release(struct device *dev)
236 {
237 	struct mdev_device *mdev = to_mdev_device(dev);
238 
239 	mdev_device_free(mdev);
240 }
241 
242 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
243 {
244 	int ret;
245 	struct mdev_device *mdev, *tmp;
246 	struct mdev_parent *parent;
247 
248 	parent = mdev_get_parent(type->parent);
249 	if (!parent)
250 		return -EINVAL;
251 
252 	mutex_lock(&mdev_list_lock);
253 
254 	/* Check for duplicate */
255 	list_for_each_entry(tmp, &mdev_list, next) {
256 		if (guid_equal(&tmp->uuid, uuid)) {
257 			mutex_unlock(&mdev_list_lock);
258 			ret = -EEXIST;
259 			goto mdev_fail;
260 		}
261 	}
262 
263 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
264 	if (!mdev) {
265 		mutex_unlock(&mdev_list_lock);
266 		ret = -ENOMEM;
267 		goto mdev_fail;
268 	}
269 
270 	guid_copy(&mdev->uuid, uuid);
271 	list_add(&mdev->next, &mdev_list);
272 	mutex_unlock(&mdev_list_lock);
273 
274 	mdev->parent = parent;
275 
276 	/* Check if parent unregistration has started */
277 	if (!down_read_trylock(&parent->unreg_sem)) {
278 		mdev_device_free(mdev);
279 		ret = -ENODEV;
280 		goto mdev_fail;
281 	}
282 
283 	device_initialize(&mdev->dev);
284 	mdev->dev.parent = parent->dev;
285 	mdev->dev.bus     = &mdev_bus_type;
286 	mdev->dev.release = mdev_device_release;
287 	dev_set_name(&mdev->dev, "%pUl", uuid);
288 	mdev->dev.groups = parent->ops->mdev_attr_groups;
289 	mdev->type = type;
290 
291 	ret = parent->ops->create(&type->kobj, mdev);
292 	if (ret)
293 		goto ops_create_fail;
294 
295 	ret = device_add(&mdev->dev);
296 	if (ret)
297 		goto add_fail;
298 
299 	ret = mdev_create_sysfs_files(mdev);
300 	if (ret)
301 		goto sysfs_fail;
302 
303 	mdev->active = true;
304 	dev_dbg(&mdev->dev, "MDEV: created\n");
305 	up_read(&parent->unreg_sem);
306 
307 	return 0;
308 
309 sysfs_fail:
310 	device_del(&mdev->dev);
311 add_fail:
312 	parent->ops->remove(mdev);
313 ops_create_fail:
314 	up_read(&parent->unreg_sem);
315 	put_device(&mdev->dev);
316 mdev_fail:
317 	mdev_put_parent(parent);
318 	return ret;
319 }
320 
321 int mdev_device_remove(struct mdev_device *mdev)
322 {
323 	struct mdev_device *tmp;
324 	struct mdev_parent *parent;
325 
326 	mutex_lock(&mdev_list_lock);
327 	list_for_each_entry(tmp, &mdev_list, next) {
328 		if (tmp == mdev)
329 			break;
330 	}
331 
332 	if (tmp != mdev) {
333 		mutex_unlock(&mdev_list_lock);
334 		return -ENODEV;
335 	}
336 
337 	if (!mdev->active) {
338 		mutex_unlock(&mdev_list_lock);
339 		return -EAGAIN;
340 	}
341 
342 	mdev->active = false;
343 	mutex_unlock(&mdev_list_lock);
344 
345 	parent = mdev->parent;
346 	/* Check if parent unregistration has started */
347 	if (!down_read_trylock(&parent->unreg_sem))
348 		return -ENODEV;
349 
350 	mdev_device_remove_common(mdev);
351 	up_read(&parent->unreg_sem);
352 	return 0;
353 }
354 
355 static int __init mdev_init(void)
356 {
357 	return mdev_bus_register();
358 }
359 
360 static void __exit mdev_exit(void)
361 {
362 	if (mdev_bus_compat_class)
363 		class_compat_unregister(mdev_bus_compat_class);
364 
365 	mdev_bus_unregister();
366 }
367 
368 module_init(mdev_init)
369 module_exit(mdev_exit)
370 
371 MODULE_VERSION(DRIVER_VERSION);
372 MODULE_LICENSE("GPL v2");
373 MODULE_AUTHOR(DRIVER_AUTHOR);
374 MODULE_DESCRIPTION(DRIVER_DESC);
375 MODULE_SOFTDEP("post: vfio_mdev");
376