History

RDMA/core: Make ib_uverbs_async_event_file into a uobject

This makes async events aligned with completion events as both are full uobjects of FD type and use the same uobject lifecycle. A bunch of duplicate code is consolidated and the general flow between the two FDs is now very similar.

Jason Gunthorpe committed 6 years ago

Commit 3e032c01 parent 39e83af9 files · +96 −143Browse files

Unified · Split

9 files
  1. drivers/infiniband/core/Makefile +2 −1
  2. drivers/infiniband/core/rdma_core.h +1 −0
  3. drivers/infiniband/core/uverbs.h +6 −9
  4. drivers/infiniband/core/uverbs_cmd.c +12 −18
  5. drivers/infiniband/core/uverbs_main.c +23 −107
  6. drivers/infiniband/core/uverbs_std_types.c +17 −8
  7. drivers/infiniband/core/uverbs_std_types_async_fd.c +33 −0
  8. drivers/infiniband/core/uverbs_uapi.c +1 −0
  9. include/uapi/rdma/ib_user_ioctl_cmds.h +1 −0
drivers/infiniband/core/Makefile+2−1
@@ -35,6 +35,7 @@ ib_uverbs-y := uverbs_main.o uverbs_cmd.o uverbs_marshall.o \ · 34 unmodified lines
3535 uverbs_std_types_cq.o \
3636 uverbs_std_types_flow_action.o uverbs_std_types_dm.o \
3737 uverbs_std_types_mr.o uverbs_std_types_counters.o \
38− uverbs_uapi.o uverbs_std_types_device.o
38+ uverbs_uapi.o uverbs_std_types_device.o \
39+ uverbs_std_types_async_fd.o
3940 ib_uverbs-$(CONFIG_INFINIBAND_USER_MEM) += umem.o
4041 ib_uverbs-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += umem_odp.o
drivers/infiniband/core/rdma_core.h+1−0
@@ -151,6 +151,7 @@ void uapi_compute_bundle_size(struct uverbs_api_ioctl_method *method_elm, · 150 unmodified lines
151151 unsigned int num_attrs);
152152 void uverbs_user_mmap_disassociate(struct ib_uverbs_file *ufile);
153153
154+extern const struct uapi_definition uverbs_def_obj_async_fd[];
154155 extern const struct uapi_definition uverbs_def_obj_counters[];
155156 extern const struct uapi_definition uverbs_def_obj_cq[];
156157 extern const struct uapi_definition uverbs_def_obj_device[];
drivers/infiniband/core/uverbs.h+6−9
@@ -111,7 +111,6 @@ struct ib_uverbs_device { · 110 unmodified lines
111111 struct srcu_struct disassociate_srcu;
112112 struct mutex lists_mutex; /* protect lists */
113113 struct list_head uverbs_file_list;
114− struct list_head uverbs_events_file_list;
115114 struct uverbs_api *uapi;
116115 };
117116
@@ -124,10 +123,9 @@ struct ib_uverbs_event_queue { · 6 unmodified lines
124123 };
125124
126125 struct ib_uverbs_async_event_file {
126+ struct ib_uobject uobj;
127127 struct ib_uverbs_event_queue ev_queue;
128− struct ib_uverbs_file *uverbs_file;
129− struct kref ref;
130− struct list_head list;
128+ struct ib_event_handler event_handler;
131129 };
132130
133131 struct ib_uverbs_completion_event_file {
@@ -144,8 +142,7 @@ struct ib_uverbs_file { · 10 unmodified lines
144142 * ucontext_lock held
145143 */
146144 struct ib_ucontext *ucontext;
147− struct ib_event_handler event_handler;
148− struct ib_uverbs_async_event_file *async_file;
145+ struct ib_uverbs_async_event_file *async_file;
149146 struct list_head list;
150147
151148 /*
@@ -217,10 +214,10 @@ struct ib_ucq_object { · 65 unmodified lines
217214 };
218215
219216 extern const struct file_operations uverbs_event_fops;
217+extern const struct file_operations uverbs_async_event_fops;
220218 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue);
221−struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
222− struct ib_device *ib_dev);
223−void ib_uverbs_free_async_event_file(struct ib_uverbs_file *uverbs_file);
219+void ib_uverbs_init_async_event_file(struct ib_uverbs_async_event_file *ev_file);
220+void ib_uverbs_free_event_queue(struct ib_uverbs_event_queue *event_queue);
224221 void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res);
225222
226223 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
drivers/infiniband/core/uverbs_cmd.c+12−18
@@ -209,9 +209,9 @@ static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs) · 208 unmodified lines
209209 struct ib_uverbs_get_context cmd;
210210 struct ib_uverbs_get_context_resp resp;
211211 struct ib_ucontext *ucontext;
212− struct file *filp;
213212 struct ib_rdmacg_object cg_obj;
214213 struct ib_device *ib_dev;
214+ struct ib_uobject *uobj;
215215 int ret;
216216
217217 ret = uverbs_request(attrs, &cmd, sizeof(cmd));
@@ -254,30 +254,28 @@ static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs) · 36 unmodified lines
254254
255255 xa_init_flags(&ucontext->mmap_xa, XA_FLAGS_ALLOC);
256256
257− ret = get_unused_fd_flags(O_CLOEXEC);
258− if (ret < 0)
257+ uobj = uobj_alloc(UVERBS_OBJECT_ASYNC_EVENT, attrs, &ib_dev);
258+ if (IS_ERR(uobj)) {
259+ ret = PTR_ERR(uobj);
259260 goto err_free;
260− resp.async_fd = ret;
261−
262− filp = ib_uverbs_alloc_async_event_file(file, ib_dev);
263− if (IS_ERR(filp)) {
264− ret = PTR_ERR(filp);
265− goto err_fd;
266261 }
267262
263+ resp.async_fd = uobj->id;
268264 resp.num_comp_vectors = file->device->num_comp_vectors;
269265
270266 ret = uverbs_response(attrs, &resp, sizeof(resp));
271267 if (ret)
272− goto err_file;
268+ goto err_uobj;
273269
274270 ret = ib_dev->ops.alloc_ucontext(ucontext, &attrs->driver_udata);
275271 if (ret)
276− goto err_file;
272+ goto err_uobj;
277273
278274 rdma_restrack_uadd(&ucontext->res);
279275
280− fd_install(resp.async_fd, filp);
276+ ib_uverbs_init_async_event_file(
277+ container_of(uobj, struct ib_uverbs_async_event_file, uobj));
278+ rdma_alloc_commit_uobject(uobj, attrs);
281279
282280 /*
283281 * Make sure that ib_uverbs_get_ucontext() sees the pointer update
@@ -289,12 +287,8 @@ static int ib_uverbs_get_context(struct uverbs_attr_bundle *attrs) · 5 unmodified lines
289287
290288 return 0;
291289
292−err_file:
293− ib_uverbs_free_async_event_file(file);
294− fput(filp);
295−
296−err_fd:
297− put_unused_fd(resp.async_fd);
290+err_uobj:
291+ rdma_alloc_abort_uobject(uobj, attrs);
298292
299293 err_free:
300294 kfree(ucontext);
drivers/infiniband/core/uverbs_main.c+23−107
@@ -125,14 +125,6 @@ static void ib_uverbs_release_dev(struct device *device) · 124 unmodified lines
125125 kfree(dev);
126126 }
127127
128−static void ib_uverbs_release_async_event_file(struct kref *ref)
129−{
130− struct ib_uverbs_async_event_file *file =
131− container_of(ref, struct ib_uverbs_async_event_file, ref);
132−
133− kfree(file);
134−}
135−
136128 void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
137129 struct ib_uverbs_completion_event_file *ev_file,
138130 struct ib_ucq_object *uobj)
@@ -203,8 +195,7 @@ void ib_uverbs_release_file(struct kref *ref) · 64 unmodified lines
203195 ib_uverbs_comp_dev(file->device);
204196
205197 if (file->async_file)
206− kref_put(&file->async_file->ref,
207− ib_uverbs_release_async_event_file);
198+ uverbs_uobject_put(&file->async_file->uobj);
208199 put_device(&file->device->dev);
209200
210201 if (file->disassociate_page)
@@ -339,35 +330,6 @@ static int ib_uverbs_comp_event_fasync(int fd, struct file *filp, int on) · 128 unmodified lines
339330 return fasync_helper(fd, filp, on, &comp_ev_file->ev_queue.async_queue);
340331 }
341332
342−static int ib_uverbs_async_event_close(struct inode *inode, struct file *filp)
343−{
344− struct ib_uverbs_async_event_file *file = filp->private_data;
345− struct ib_uverbs_file *uverbs_file = file->uverbs_file;
346− struct ib_uverbs_event *entry, *tmp;
347− int closed_already = 0;
348−
349− mutex_lock(&uverbs_file->device->lists_mutex);
350− spin_lock_irq(&file->ev_queue.lock);
351− closed_already = file->ev_queue.is_closed;
352− file->ev_queue.is_closed = 1;
353− list_for_each_entry_safe(entry, tmp, &file->ev_queue.event_list, list) {
354− if (entry->counter)
355− list_del(&entry->obj_list);
356− kfree(entry);
357− }
358− spin_unlock_irq(&file->ev_queue.lock);
359− if (!closed_already) {
360− list_del(&file->list);
361− ib_unregister_event_handler(&uverbs_file->event_handler);
362− }
363− mutex_unlock(&uverbs_file->device->lists_mutex);
364−
365− kref_put(&uverbs_file->ref, ib_uverbs_release_file);
366− kref_put(&file->ref, ib_uverbs_release_async_event_file);
367−
368− return 0;
369−}
370−
371333 const struct file_operations uverbs_event_fops = {
372334 .owner = THIS_MODULE,
373335 .read = ib_uverbs_comp_event_read,
@@ -377,11 +339,11 @@ const struct file_operations uverbs_event_fops = { · 3 unmodified lines
377339 .llseek = no_llseek,
378340 };
379341
380−static const struct file_operations uverbs_async_event_fops = {
342+const struct file_operations uverbs_async_event_fops = {
381343 .owner = THIS_MODULE,
382344 .read = ib_uverbs_async_event_read,
383345 .poll = ib_uverbs_async_event_poll,
384− .release = ib_uverbs_async_event_close,
346+ .release = uverbs_uobject_fd_release,
385347 .fasync = ib_uverbs_async_event_fasync,
386348 .llseek = no_llseek,
387349 };
@@ -491,17 +453,11 @@ static void ib_uverbs_event_handler(struct ib_event_handler *handler, · 103 unmodified lines
491453 struct ib_event *event)
492454 {
493455 ib_uverbs_async_handler(
494− container_of(handler, struct ib_uverbs_file, event_handler)
495− ->async_file,
456+ container_of(handler, struct ib_uverbs_async_event_file,
457+ event_handler),
496458 event->element.port_num, event->event, NULL, NULL);
497459 }
498460
499−void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
500−{
501− kref_put(&file->async_file->ref, ib_uverbs_release_async_event_file);
502− file->async_file = NULL;
503−}
504−
505461 void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue)
506462 {
507463 spin_lock_init(&ev_queue->lock);
@@ -511,45 +467,23 @@ void ib_uverbs_init_event_queue(struct ib_uverbs_event_queue *ev_queue) · 3 unmodified lines
511467 ev_queue->async_queue = NULL;
512468 }
513469
514−struct file *ib_uverbs_alloc_async_event_file(struct ib_uverbs_file *uverbs_file,
515− struct ib_device *ib_dev)
470+void ib_uverbs_init_async_event_file(
471+ struct ib_uverbs_async_event_file *async_file)
516472 {
517− struct ib_uverbs_async_event_file *ev_file;
518− struct file *filp;
519−
520− ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
521− if (!ev_file)
522− return ERR_PTR(-ENOMEM);
523−
524− ib_uverbs_init_event_queue(&ev_file->ev_queue);
525− ev_file->uverbs_file = uverbs_file;
526− kref_get(&ev_file->uverbs_file->ref);
527− kref_init(&ev_file->ref);
528− filp = anon_inode_getfile("[infinibandevent]", &uverbs_async_event_fops,
529− ev_file, O_RDONLY);
530− if (IS_ERR(filp))
531− goto err_put_refs;
532−
533− mutex_lock(&uverbs_file->device->lists_mutex);
534− list_add_tail(&ev_file->list,
535− &uverbs_file->device->uverbs_events_file_list);
536− mutex_unlock(&uverbs_file->device->lists_mutex);
537−
538− WARN_ON(uverbs_file->async_file);
539− uverbs_file->async_file = ev_file;
540− kref_get(&uverbs_file->async_file->ref);
541− INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
542− ib_dev,
543− ib_uverbs_event_handler);
544− ib_register_event_handler(&uverbs_file->event_handler);
545− /* At that point async file stuff was fully set */
473+ struct ib_uverbs_file *uverbs_file = async_file->uobj.ufile;
474+ struct ib_device *ib_dev = async_file->uobj.context->device;
546475
547− return filp;
476+ ib_uverbs_init_event_queue(&async_file->ev_queue);
548477
549−err_put_refs:
550− kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
551− kref_put(&ev_file->ref, ib_uverbs_release_async_event_file);
552− return filp;
478+ if (!WARN_ON(uverbs_file->async_file)) {
479+ uverbs_file->async_file = async_file;
480+ /* Pairs with the put in ib_uverbs_release_file */
481+ uverbs_uobject_get(&async_file->uobj);
482+ }
483+
484+ INIT_IB_EVENT_HANDLER(&async_file->event_handler, ib_dev,
485+ ib_uverbs_event_handler);
486+ ib_register_event_handler(&async_file->event_handler);
553487 }
554488
555489 static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
@@ -1178,7 +1112,6 @@ static void ib_uverbs_add_one(struct ib_device *device) · 622 unmodified lines
11781112 mutex_init(&uverbs_dev->xrcd_tree_mutex);
11791113 mutex_init(&uverbs_dev->lists_mutex);
11801114 INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
1181− INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
11821115 rcu_assign_pointer(uverbs_dev->ib_dev, device);
11831116 uverbs_dev->num_comp_vectors = device->num_comp_vectors;
11841117
@@ -1223,7 +1156,6 @@ static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev, · 38 unmodified lines
12231156 struct ib_device *ib_dev)
12241157 {
12251158 struct ib_uverbs_file *file;
1226− struct ib_uverbs_async_event_file *event_file;
12271159 struct ib_event event;
12281160
12291161 /* Pending running commands to terminate */
@@ -1246,31 +1178,15 @@ static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev, · 16 unmodified lines
12461178 */
12471179 mutex_unlock(&uverbs_dev->lists_mutex);
12481180
1249− ib_uverbs_event_handler(&file->event_handler, &event);
1181+ if (file->async_file)
1182+ ib_uverbs_event_handler(
1183+ &file->async_file->event_handler, &event);
1184+
12501185 uverbs_destroy_ufile_hw(file, RDMA_REMOVE_DRIVER_REMOVE);
12511186 kref_put(&file->ref, ib_uverbs_release_file);
12521187
12531188 mutex_lock(&uverbs_dev->lists_mutex);
12541189 }
1255−
1256− while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
1257− event_file = list_first_entry(&uverbs_dev->
1258− uverbs_events_file_list,
1259− struct ib_uverbs_async_event_file,
1260− list);
1261− spin_lock_irq(&event_file->ev_queue.lock);
1262− event_file->ev_queue.is_closed = 1;
1263− spin_unlock_irq(&event_file->ev_queue.lock);
1264−
1265− list_del(&event_file->list);
1266− ib_unregister_event_handler(
1267− &event_file->uverbs_file->event_handler);
1268− event_file->uverbs_file->event_handler.device =
1269− NULL;
1270−
1271− wake_up_interruptible(&event_file->ev_queue.poll_wait);
1272− kill_fasync(&event_file->ev_queue.async_queue, SIGIO, POLL_IN);
1273− }
12741190 mutex_unlock(&uverbs_dev->lists_mutex);
12751191
12761192 uverbs_disassociate_api(uverbs_dev->uapi);
drivers/infiniband/core/uverbs_std_types.c+17−8
@@ -202,17 +202,15 @@ static int uverbs_free_pd(struct ib_uobject *uobject, · 201 unmodified lines
202202 return 0;
203203 }
204204
205−static int
206−uverbs_completion_event_file_destroy_uobj(struct ib_uobject *uobj,
207− enum rdma_remove_reason why)
205+void ib_uverbs_free_event_queue(struct ib_uverbs_event_queue *event_queue)
208206 {
209− struct ib_uverbs_completion_event_file *file =
210− container_of(uobj, struct ib_uverbs_completion_event_file,
211− uobj);
212− struct ib_uverbs_event_queue *event_queue = &file->ev_queue;
213207 struct ib_uverbs_event *entry, *tmp;
214208
215209 spin_lock_irq(&event_queue->lock);
210+ /*
211+ * The user must ensure that no new items are added to the event_list
212+ * once is_closed is set.
213+ */
216214 event_queue->is_closed = 1;
217215 spin_unlock_irq(&event_queue->lock);
218216 wake_up_interruptible(&event_queue->poll_wait);
@@ -225,8 +223,19 @@ uverbs_completion_event_file_destroy_uobj(struct ib_uobject *uobj, · 6 unmodified lines
225223 kfree(entry);
226224 }
227225 spin_unlock_irq(&event_queue->lock);
226+}
227+
228+static int
229+uverbs_completion_event_file_destroy_uobj(struct ib_uobject *uobj,
230+ enum rdma_remove_reason why)
231+{
232+ struct ib_uverbs_completion_event_file *file =
233+ container_of(uobj, struct ib_uverbs_completion_event_file,
234+ uobj);
235+
236+ ib_uverbs_free_event_queue(&file->ev_queue);
228237 return 0;
229−};
238+}
230239
231240 int uverbs_destroy_def_handler(struct uverbs_attr_bundle *attrs)
232241 {
drivers/infiniband/core/uverbs_std_types_async_fd.c+33−0
@@ -0,0 +1,33 @@
1+// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2+/*
3+ * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved.
4+ */
5+
6+#include <rdma/uverbs_std_types.h>
7+#include <rdma/uverbs_ioctl.h>
8+#include "rdma_core.h"
9+#include "uverbs.h"
10+
11+static int uverbs_async_event_destroy_uobj(struct ib_uobject *uobj,
12+ enum rdma_remove_reason why)
13+{
14+ struct ib_uverbs_async_event_file *event_file =
15+ container_of(uobj, struct ib_uverbs_async_event_file, uobj);
16+
17+ ib_unregister_event_handler(&event_file->event_handler);
18+ ib_uverbs_free_event_queue(&event_file->ev_queue);
19+ return 0;
20+}
21+
22+DECLARE_UVERBS_NAMED_OBJECT(
23+ UVERBS_OBJECT_ASYNC_EVENT,
24+ UVERBS_TYPE_ALLOC_FD(sizeof(struct ib_uverbs_async_event_file),
25+ uverbs_async_event_destroy_uobj,
26+ &uverbs_async_event_fops,
27+ "[infinibandevent]",
28+ O_RDONLY));
29+
30+const struct uapi_definition uverbs_def_obj_async_fd[] = {
31+ UAPI_DEF_CHAIN_OBJ_TREE_NAMED(UVERBS_OBJECT_ASYNC_EVENT),
32+ {}
33+};
drivers/infiniband/core/uverbs_uapi.c+1−0
@@ -626,6 +626,7 @@ void uverbs_destroy_api(struct uverbs_api *uapi) · 625 unmodified lines
626626 }
627627
628628 static const struct uapi_definition uverbs_core_api[] = {
629+ UAPI_DEF_CHAIN(uverbs_def_obj_async_fd),
629630 UAPI_DEF_CHAIN(uverbs_def_obj_counters),
630631 UAPI_DEF_CHAIN(uverbs_def_obj_cq),
631632 UAPI_DEF_CHAIN(uverbs_def_obj_device),
include/uapi/rdma/ib_user_ioctl_cmds.h+1−0
@@ -56,6 +56,7 @@ enum uverbs_default_objects { · 55 unmodified lines
5656 UVERBS_OBJECT_FLOW_ACTION,
5757 UVERBS_OBJECT_DM,
5858 UVERBS_OBJECT_COUNTERS,
59+ UVERBS_OBJECT_ASYNC_EVENT,
5960 };
6061
6162 enum {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t