IB/uverbs: Fix race between uverbs_close and remove_one
[cascardo/linux.git] / drivers / infiniband / core / uverbs.h
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005, 2006 Cisco Systems.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
5  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
6  * Copyright (c) 2005 PathScale, Inc. All rights reserved.
7  *
8  * This software is available to you under a choice of one of two
9  * licenses.  You may choose to be licensed under the terms of the GNU
10  * General Public License (GPL) Version 2, available from the file
11  * COPYING in the main directory of this source tree, or the
12  * OpenIB.org BSD license below:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
22  *      - Redistributions in binary form must reproduce the above
23  *        copyright notice, this list of conditions and the following
24  *        disclaimer in the documentation and/or other materials
25  *        provided with the distribution.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34  * SOFTWARE.
35  */
36
37 #ifndef UVERBS_H
38 #define UVERBS_H
39
40 #include <linux/kref.h>
41 #include <linux/idr.h>
42 #include <linux/mutex.h>
43 #include <linux/completion.h>
44 #include <linux/cdev.h>
45
46 #include <rdma/ib_verbs.h>
47 #include <rdma/ib_umem.h>
48 #include <rdma/ib_user_verbs.h>
49
50 #define INIT_UDATA(udata, ibuf, obuf, ilen, olen)                       \
51         do {                                                            \
52                 (udata)->inbuf  = (const void __user *) (ibuf);         \
53                 (udata)->outbuf = (void __user *) (obuf);               \
54                 (udata)->inlen  = (ilen);                               \
55                 (udata)->outlen = (olen);                               \
56         } while (0)
57
58 #define INIT_UDATA_BUF_OR_NULL(udata, ibuf, obuf, ilen, olen)                   \
59         do {                                                                    \
60                 (udata)->inbuf  = (ilen) ? (const void __user *) (ibuf) : NULL; \
61                 (udata)->outbuf = (olen) ? (void __user *) (obuf) : NULL;       \
62                 (udata)->inlen  = (ilen);                                       \
63                 (udata)->outlen = (olen);                                       \
64         } while (0)
65
66 /*
67  * Our lifetime rules for these structs are the following:
68  *
69  * struct ib_uverbs_device: One reference is held by the module and
70  * released in ib_uverbs_remove_one().  Another reference is taken by
71  * ib_uverbs_open() each time the character special file is opened,
72  * and released in ib_uverbs_release_file() when the file is released.
73  *
74  * struct ib_uverbs_file: One reference is held by the VFS and
75  * released when the file is closed.  Another reference is taken when
76  * an asynchronous event queue file is created and released when the
77  * event file is closed.
78  *
79  * struct ib_uverbs_event_file: One reference is held by the VFS and
80  * released when the file is closed.  For asynchronous event files,
81  * another reference is held by the corresponding main context file
82  * and released when that file is closed.  For completion event files,
83  * a reference is taken when a CQ is created that uses the file, and
84  * released when the CQ is destroyed.
85  */
86
87 struct ib_uverbs_device {
88         atomic_t                                refcount;
89         int                                     num_comp_vectors;
90         struct completion                       comp;
91         struct device                          *dev;
92         struct ib_device        __rcu          *ib_dev;
93         int                                     devnum;
94         struct cdev                             cdev;
95         struct rb_root                          xrcd_tree;
96         struct mutex                            xrcd_tree_mutex;
97         struct kobject                          kobj;
98         struct srcu_struct                      disassociate_srcu;
99         struct mutex                            lists_mutex; /* protect lists */
100         struct list_head                        uverbs_file_list;
101         struct list_head                        uverbs_events_file_list;
102 };
103
104 struct ib_uverbs_event_file {
105         struct kref                             ref;
106         int                                     is_async;
107         struct ib_uverbs_file                  *uverbs_file;
108         spinlock_t                              lock;
109         int                                     is_closed;
110         wait_queue_head_t                       poll_wait;
111         struct fasync_struct                   *async_queue;
112         struct list_head                        event_list;
113         struct list_head                        list;
114 };
115
116 struct ib_uverbs_file {
117         struct kref                             ref;
118         struct mutex                            mutex;
119         struct mutex                            cleanup_mutex; /* protect cleanup */
120         struct ib_uverbs_device                *device;
121         struct ib_ucontext                     *ucontext;
122         struct ib_event_handler                 event_handler;
123         struct ib_uverbs_event_file            *async_file;
124         struct list_head                        list;
125         int                                     is_closed;
126 };
127
128 struct ib_uverbs_event {
129         union {
130                 struct ib_uverbs_async_event_desc       async;
131                 struct ib_uverbs_comp_event_desc        comp;
132         }                                       desc;
133         struct list_head                        list;
134         struct list_head                        obj_list;
135         u32                                    *counter;
136 };
137
138 struct ib_uverbs_mcast_entry {
139         struct list_head        list;
140         union ib_gid            gid;
141         u16                     lid;
142 };
143
144 struct ib_uevent_object {
145         struct ib_uobject       uobject;
146         struct list_head        event_list;
147         u32                     events_reported;
148 };
149
150 struct ib_uxrcd_object {
151         struct ib_uobject       uobject;
152         atomic_t                refcnt;
153 };
154
155 struct ib_usrq_object {
156         struct ib_uevent_object uevent;
157         struct ib_uxrcd_object *uxrcd;
158 };
159
160 struct ib_uqp_object {
161         struct ib_uevent_object uevent;
162         struct list_head        mcast_list;
163         struct ib_uxrcd_object *uxrcd;
164 };
165
166 struct ib_ucq_object {
167         struct ib_uobject       uobject;
168         struct ib_uverbs_file  *uverbs_file;
169         struct list_head        comp_list;
170         struct list_head        async_list;
171         u32                     comp_events_reported;
172         u32                     async_events_reported;
173 };
174
175 extern spinlock_t ib_uverbs_idr_lock;
176 extern struct idr ib_uverbs_pd_idr;
177 extern struct idr ib_uverbs_mr_idr;
178 extern struct idr ib_uverbs_mw_idr;
179 extern struct idr ib_uverbs_ah_idr;
180 extern struct idr ib_uverbs_cq_idr;
181 extern struct idr ib_uverbs_qp_idr;
182 extern struct idr ib_uverbs_srq_idr;
183 extern struct idr ib_uverbs_xrcd_idr;
184 extern struct idr ib_uverbs_rule_idr;
185
186 void idr_remove_uobj(struct idr *idp, struct ib_uobject *uobj);
187
188 struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
189                                         struct ib_device *ib_dev,
190                                         int is_async);
191 void ib_uverbs_free_async_event_file(struct ib_uverbs_file *uverbs_file);
192 struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd);
193
194 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
195                            struct ib_uverbs_event_file *ev_file,
196                            struct ib_ucq_object *uobj);
197 void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
198                               struct ib_uevent_object *uobj);
199
200 void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
201 void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);
202 void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr);
203 void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr);
204 void ib_uverbs_event_handler(struct ib_event_handler *handler,
205                              struct ib_event *event);
206 void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev, struct ib_xrcd *xrcd);
207
208 int uverbs_dealloc_mw(struct ib_mw *mw);
209
210 struct ib_uverbs_flow_spec {
211         union {
212                 union {
213                         struct ib_uverbs_flow_spec_hdr hdr;
214                         struct {
215                                 __u32 type;
216                                 __u16 size;
217                                 __u16 reserved;
218                         };
219                 };
220                 struct ib_uverbs_flow_spec_eth     eth;
221                 struct ib_uverbs_flow_spec_ipv4    ipv4;
222                 struct ib_uverbs_flow_spec_tcp_udp tcp_udp;
223         };
224 };
225
226 #define IB_UVERBS_DECLARE_CMD(name)                                     \
227         ssize_t ib_uverbs_##name(struct ib_uverbs_file *file,           \
228                                  struct ib_device *ib_dev,              \
229                                  const char __user *buf, int in_len,    \
230                                  int out_len)
231
232 IB_UVERBS_DECLARE_CMD(get_context);
233 IB_UVERBS_DECLARE_CMD(query_device);
234 IB_UVERBS_DECLARE_CMD(query_port);
235 IB_UVERBS_DECLARE_CMD(alloc_pd);
236 IB_UVERBS_DECLARE_CMD(dealloc_pd);
237 IB_UVERBS_DECLARE_CMD(reg_mr);
238 IB_UVERBS_DECLARE_CMD(rereg_mr);
239 IB_UVERBS_DECLARE_CMD(dereg_mr);
240 IB_UVERBS_DECLARE_CMD(alloc_mw);
241 IB_UVERBS_DECLARE_CMD(dealloc_mw);
242 IB_UVERBS_DECLARE_CMD(create_comp_channel);
243 IB_UVERBS_DECLARE_CMD(create_cq);
244 IB_UVERBS_DECLARE_CMD(resize_cq);
245 IB_UVERBS_DECLARE_CMD(poll_cq);
246 IB_UVERBS_DECLARE_CMD(req_notify_cq);
247 IB_UVERBS_DECLARE_CMD(destroy_cq);
248 IB_UVERBS_DECLARE_CMD(create_qp);
249 IB_UVERBS_DECLARE_CMD(open_qp);
250 IB_UVERBS_DECLARE_CMD(query_qp);
251 IB_UVERBS_DECLARE_CMD(modify_qp);
252 IB_UVERBS_DECLARE_CMD(destroy_qp);
253 IB_UVERBS_DECLARE_CMD(post_send);
254 IB_UVERBS_DECLARE_CMD(post_recv);
255 IB_UVERBS_DECLARE_CMD(post_srq_recv);
256 IB_UVERBS_DECLARE_CMD(create_ah);
257 IB_UVERBS_DECLARE_CMD(destroy_ah);
258 IB_UVERBS_DECLARE_CMD(attach_mcast);
259 IB_UVERBS_DECLARE_CMD(detach_mcast);
260 IB_UVERBS_DECLARE_CMD(create_srq);
261 IB_UVERBS_DECLARE_CMD(modify_srq);
262 IB_UVERBS_DECLARE_CMD(query_srq);
263 IB_UVERBS_DECLARE_CMD(destroy_srq);
264 IB_UVERBS_DECLARE_CMD(create_xsrq);
265 IB_UVERBS_DECLARE_CMD(open_xrcd);
266 IB_UVERBS_DECLARE_CMD(close_xrcd);
267
268 #define IB_UVERBS_DECLARE_EX_CMD(name)                          \
269         int ib_uverbs_ex_##name(struct ib_uverbs_file *file,    \
270                                 struct ib_device *ib_dev,               \
271                                 struct ib_udata *ucore,         \
272                                 struct ib_udata *uhw)
273
274 IB_UVERBS_DECLARE_EX_CMD(create_flow);
275 IB_UVERBS_DECLARE_EX_CMD(destroy_flow);
276 IB_UVERBS_DECLARE_EX_CMD(query_device);
277 IB_UVERBS_DECLARE_EX_CMD(create_cq);
278 IB_UVERBS_DECLARE_EX_CMD(create_qp);
279
280 #endif /* UVERBS_H */