vfio: Fix NULL pointer dereference caused by uninitialized group->iommufd
[platform/kernel/linux-rpi.git] / drivers / vfio / vfio.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
4  *     Author: Alex Williamson <alex.williamson@redhat.com>
5  */
6 #ifndef __VFIO_VFIO_H__
7 #define __VFIO_VFIO_H__
8
9 #include <linux/file.h>
10 #include <linux/device.h>
11 #include <linux/cdev.h>
12 #include <linux/module.h>
13
14 struct iommufd_ctx;
15 struct iommu_group;
16 struct vfio_device;
17 struct vfio_container;
18
19 void vfio_device_put_registration(struct vfio_device *device);
20 bool vfio_device_try_get_registration(struct vfio_device *device);
21 int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd);
22 void vfio_device_close(struct vfio_device *device,
23                        struct iommufd_ctx *iommufd);
24
25 extern const struct file_operations vfio_device_fops;
26
27 enum vfio_group_type {
28         /*
29          * Physical device with IOMMU backing.
30          */
31         VFIO_IOMMU,
32
33         /*
34          * Virtual device without IOMMU backing. The VFIO core fakes up an
35          * iommu_group as the iommu_group sysfs interface is part of the
36          * userspace ABI.  The user of these devices must not be able to
37          * directly trigger unmediated DMA.
38          */
39         VFIO_EMULATED_IOMMU,
40
41         /*
42          * Physical device without IOMMU backing. The VFIO core fakes up an
43          * iommu_group as the iommu_group sysfs interface is part of the
44          * userspace ABI.  Users can trigger unmediated DMA by the device,
45          * usage is highly dangerous, requires an explicit opt-in and will
46          * taint the kernel.
47          */
48         VFIO_NO_IOMMU,
49 };
50
51 struct vfio_group {
52         struct device                   dev;
53         struct cdev                     cdev;
54         /*
55          * When drivers is non-zero a driver is attached to the struct device
56          * that provided the iommu_group and thus the iommu_group is a valid
57          * pointer. When drivers is 0 the driver is being detached. Once users
58          * reaches 0 then the iommu_group is invalid.
59          */
60         refcount_t                      drivers;
61         unsigned int                    container_users;
62         struct iommu_group              *iommu_group;
63         struct vfio_container           *container;
64         struct list_head                device_list;
65         struct mutex                    device_lock;
66         struct list_head                vfio_next;
67 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
68         struct list_head                container_next;
69 #endif
70         enum vfio_group_type            type;
71         struct mutex                    group_lock;
72         struct kvm                      *kvm;
73         struct file                     *opened_file;
74         struct blocking_notifier_head   notifier;
75         struct iommufd_ctx              *iommufd;
76         spinlock_t                      kvm_ref_lock;
77 };
78
79 int vfio_device_set_group(struct vfio_device *device,
80                           enum vfio_group_type type);
81 void vfio_device_remove_group(struct vfio_device *device);
82 void vfio_device_group_register(struct vfio_device *device);
83 void vfio_device_group_unregister(struct vfio_device *device);
84 int vfio_device_group_use_iommu(struct vfio_device *device);
85 void vfio_device_group_unuse_iommu(struct vfio_device *device);
86 void vfio_device_group_close(struct vfio_device *device);
87 bool vfio_device_has_container(struct vfio_device *device);
88 int __init vfio_group_init(void);
89 void vfio_group_cleanup(void);
90
91 #if IS_ENABLED(CONFIG_VFIO_CONTAINER)
92 /**
93  * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
94  */
95 struct vfio_iommu_driver_ops {
96         char            *name;
97         struct module   *owner;
98         void            *(*open)(unsigned long arg);
99         void            (*release)(void *iommu_data);
100         long            (*ioctl)(void *iommu_data, unsigned int cmd,
101                                  unsigned long arg);
102         int             (*attach_group)(void *iommu_data,
103                                         struct iommu_group *group,
104                                         enum vfio_group_type);
105         void            (*detach_group)(void *iommu_data,
106                                         struct iommu_group *group);
107         int             (*pin_pages)(void *iommu_data,
108                                      struct iommu_group *group,
109                                      dma_addr_t user_iova,
110                                      int npage, int prot,
111                                      struct page **pages);
112         void            (*unpin_pages)(void *iommu_data,
113                                        dma_addr_t user_iova, int npage);
114         void            (*register_device)(void *iommu_data,
115                                            struct vfio_device *vdev);
116         void            (*unregister_device)(void *iommu_data,
117                                              struct vfio_device *vdev);
118         int             (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
119                                   void *data, size_t count, bool write);
120         struct iommu_domain *(*group_iommu_domain)(void *iommu_data,
121                                                    struct iommu_group *group);
122 };
123
124 struct vfio_iommu_driver {
125         const struct vfio_iommu_driver_ops      *ops;
126         struct list_head                        vfio_next;
127 };
128
129 int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
130 void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops);
131
132 struct vfio_container *vfio_container_from_file(struct file *filep);
133 int vfio_group_use_container(struct vfio_group *group);
134 void vfio_group_unuse_container(struct vfio_group *group);
135 int vfio_container_attach_group(struct vfio_container *container,
136                                 struct vfio_group *group);
137 void vfio_group_detach_container(struct vfio_group *group);
138 void vfio_device_container_register(struct vfio_device *device);
139 void vfio_device_container_unregister(struct vfio_device *device);
140 int vfio_device_container_pin_pages(struct vfio_device *device,
141                                     dma_addr_t iova, int npage,
142                                     int prot, struct page **pages);
143 void vfio_device_container_unpin_pages(struct vfio_device *device,
144                                        dma_addr_t iova, int npage);
145 int vfio_device_container_dma_rw(struct vfio_device *device,
146                                  dma_addr_t iova, void *data,
147                                  size_t len, bool write);
148
149 int __init vfio_container_init(void);
150 void vfio_container_cleanup(void);
151 #else
152 static inline struct vfio_container *
153 vfio_container_from_file(struct file *filep)
154 {
155         return NULL;
156 }
157
158 static inline int vfio_group_use_container(struct vfio_group *group)
159 {
160         return -EOPNOTSUPP;
161 }
162
163 static inline void vfio_group_unuse_container(struct vfio_group *group)
164 {
165 }
166
167 static inline int vfio_container_attach_group(struct vfio_container *container,
168                                               struct vfio_group *group)
169 {
170         return -EOPNOTSUPP;
171 }
172
173 static inline void vfio_group_detach_container(struct vfio_group *group)
174 {
175 }
176
177 static inline void vfio_device_container_register(struct vfio_device *device)
178 {
179 }
180
181 static inline void vfio_device_container_unregister(struct vfio_device *device)
182 {
183 }
184
185 static inline int vfio_device_container_pin_pages(struct vfio_device *device,
186                                                   dma_addr_t iova, int npage,
187                                                   int prot, struct page **pages)
188 {
189         return -EOPNOTSUPP;
190 }
191
192 static inline void vfio_device_container_unpin_pages(struct vfio_device *device,
193                                                      dma_addr_t iova, int npage)
194 {
195 }
196
197 static inline int vfio_device_container_dma_rw(struct vfio_device *device,
198                                                dma_addr_t iova, void *data,
199                                                size_t len, bool write)
200 {
201         return -EOPNOTSUPP;
202 }
203
204 static inline int vfio_container_init(void)
205 {
206         return 0;
207 }
208 static inline void vfio_container_cleanup(void)
209 {
210 }
211 #endif
212
213 #if IS_ENABLED(CONFIG_IOMMUFD)
214 int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx);
215 void vfio_iommufd_unbind(struct vfio_device *device);
216 #else
217 static inline int vfio_iommufd_bind(struct vfio_device *device,
218                                     struct iommufd_ctx *ictx)
219 {
220         return -EOPNOTSUPP;
221 }
222
223 static inline void vfio_iommufd_unbind(struct vfio_device *device)
224 {
225 }
226 #endif
227
228 #if IS_ENABLED(CONFIG_VFIO_VIRQFD)
229 int __init vfio_virqfd_init(void);
230 void vfio_virqfd_exit(void);
231 #else
232 static inline int __init vfio_virqfd_init(void)
233 {
234         return 0;
235 }
236 static inline void vfio_virqfd_exit(void)
237 {
238 }
239 #endif
240
241 #ifdef CONFIG_VFIO_NOIOMMU
242 extern bool vfio_noiommu __read_mostly;
243 #else
244 enum { vfio_noiommu = false };
245 #endif
246
247 #ifdef CONFIG_HAVE_KVM
248 void _vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
249 void vfio_device_put_kvm(struct vfio_device *device);
250 #else
251 static inline void _vfio_device_get_kvm_safe(struct vfio_device *device,
252                                              struct kvm *kvm)
253 {
254 }
255
256 static inline void vfio_device_put_kvm(struct vfio_device *device)
257 {
258 }
259 #endif
260
261 #endif