xref: /linux/drivers/infiniband/core/cq.c (revision a2917582887ac7c3aaccad60cb5eb876b1a83594)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 HGST, a Western Digital Company.
4  */
5 #include <linux/err.h>
6 #include <linux/slab.h>
7 #include <rdma/ib_verbs.h>
8 
9 #include "core_priv.h"
10 
11 #include <trace/events/rdma_core.h>
12 /* Max size for shared CQ, may require tuning */
13 #define IB_MAX_SHARED_CQ_SZ		4096U
14 
15 /* # of WCs to poll for with a single call to ib_poll_cq */
16 #define IB_POLL_BATCH			16
17 #define IB_POLL_BATCH_DIRECT		8
18 
19 /* # of WCs to iterate over before yielding */
20 #define IB_POLL_BUDGET_IRQ		256
21 #define IB_POLL_BUDGET_WORKQUEUE	65536
22 
23 #define IB_POLL_FLAGS \
24 	(IB_CQ_NEXT_COMP | IB_CQ_REPORT_MISSED_EVENTS)
25 
26 static const struct dim_cq_moder
27 rdma_dim_prof[RDMA_DIM_PARAMS_NUM_PROFILES] = {
28 	{1,   0, 1,  0},
29 	{1,   0, 4,  0},
30 	{2,   0, 4,  0},
31 	{2,   0, 8,  0},
32 	{4,   0, 8,  0},
33 	{16,  0, 8,  0},
34 	{16,  0, 16, 0},
35 	{32,  0, 16, 0},
36 	{32,  0, 32, 0},
37 };
38 
39 static void ib_cq_rdma_dim_work(struct work_struct *w)
40 {
41 	struct dim *dim = container_of(w, struct dim, work);
42 	struct ib_cq *cq = dim->priv;
43 
44 	u16 usec = rdma_dim_prof[dim->profile_ix].usec;
45 	u16 comps = rdma_dim_prof[dim->profile_ix].comps;
46 
47 	dim->state = DIM_START_MEASURE;
48 
49 	trace_cq_modify(cq, comps, usec);
50 	cq->device->ops.modify_cq(cq, comps, usec);
51 }
52 
53 static void rdma_dim_init(struct ib_cq *cq)
54 {
55 	struct dim *dim;
56 
57 	if (!cq->device->ops.modify_cq || !cq->device->use_cq_dim ||
58 	    cq->poll_ctx == IB_POLL_DIRECT)
59 		return;
60 
61 	dim = kzalloc_obj(struct dim);
62 	if (!dim)
63 		return;
64 
65 	dim->state = DIM_START_MEASURE;
66 	dim->tune_state = DIM_GOING_RIGHT;
67 	dim->profile_ix = RDMA_DIM_START_PROFILE;
68 	dim->priv = cq;
69 	cq->dim = dim;
70 
71 	INIT_WORK(&dim->work, ib_cq_rdma_dim_work);
72 }
73 
74 static void rdma_dim_destroy(struct ib_cq *cq)
75 {
76 	if (!cq->dim)
77 		return;
78 
79 	cancel_work_sync(&cq->dim->work);
80 	kfree(cq->dim);
81 }
82 
83 static int __poll_cq(struct ib_cq *cq, int num_entries, struct ib_wc *wc)
84 {
85 	int rc;
86 
87 	rc = ib_poll_cq(cq, num_entries, wc);
88 	trace_cq_poll(cq, num_entries, rc);
89 	return rc;
90 }
91 
92 static int __ib_process_cq(struct ib_cq *cq, int budget, struct ib_wc *wcs,
93 			   int batch)
94 {
95 	int i, n, completed = 0;
96 
97 	trace_cq_process(cq);
98 
99 	/*
100 	 * budget might be (-1) if the caller does not
101 	 * want to bound this call, thus we need unsigned
102 	 * minimum here.
103 	 */
104 	while ((n = __poll_cq(cq, min_t(u32, batch,
105 					budget - completed), wcs)) > 0) {
106 		for (i = 0; i < n; i++) {
107 			struct ib_wc *wc = &wcs[i];
108 
109 			if (wc->wr_cqe)
110 				wc->wr_cqe->done(cq, wc);
111 			else
112 				WARN_ON_ONCE(wc->status == IB_WC_SUCCESS);
113 		}
114 
115 		completed += n;
116 
117 		if (n != batch || (budget != -1 && completed >= budget))
118 			break;
119 	}
120 
121 	return completed;
122 }
123 
124 /**
125  * ib_process_cq_direct - process a CQ in caller context
126  * @cq:		CQ to process
127  * @budget:	number of CQEs to poll for
128  *
129  * This function is used to process all outstanding CQ entries.
130  * It does not offload CQ processing to a different context and does
131  * not ask for completion interrupts from the HCA.
132  * Using direct processing on CQ with non IB_POLL_DIRECT type may trigger
133  * concurrent processing.
134  *
135  * Note: do not pass -1 as %budget unless it is guaranteed that the number
136  * of completions that will be processed is small.
137  */
138 int ib_process_cq_direct(struct ib_cq *cq, int budget)
139 {
140 	struct ib_wc wcs[IB_POLL_BATCH_DIRECT];
141 
142 	return __ib_process_cq(cq, budget, wcs, IB_POLL_BATCH_DIRECT);
143 }
144 EXPORT_SYMBOL(ib_process_cq_direct);
145 
146 static void ib_cq_completion_direct(struct ib_cq *cq, void *private)
147 {
148 	WARN_ONCE(1, "got unsolicited completion for CQ 0x%p\n", cq);
149 }
150 
151 static int ib_poll_handler(struct irq_poll *iop, int budget)
152 {
153 	struct ib_cq *cq = container_of(iop, struct ib_cq, iop);
154 	struct dim *dim = cq->dim;
155 	int completed;
156 
157 	completed = __ib_process_cq(cq, budget, cq->wc, IB_POLL_BATCH);
158 	if (completed < budget) {
159 		irq_poll_complete(&cq->iop);
160 		if (ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0) {
161 			trace_cq_reschedule(cq);
162 			irq_poll_sched(&cq->iop);
163 		}
164 	}
165 
166 	if (dim)
167 		rdma_dim(dim, completed);
168 
169 	return completed;
170 }
171 
172 static void ib_cq_completion_softirq(struct ib_cq *cq, void *private)
173 {
174 	trace_cq_schedule(cq);
175 	irq_poll_sched(&cq->iop);
176 }
177 
178 static void ib_cq_poll_work(struct work_struct *work)
179 {
180 	struct ib_cq *cq = container_of(work, struct ib_cq, work);
181 	int completed;
182 
183 	completed = __ib_process_cq(cq, IB_POLL_BUDGET_WORKQUEUE, cq->wc,
184 				    IB_POLL_BATCH);
185 	if (completed >= IB_POLL_BUDGET_WORKQUEUE ||
186 	    ib_req_notify_cq(cq, IB_POLL_FLAGS) > 0)
187 		queue_work(cq->comp_wq, &cq->work);
188 	else if (cq->dim)
189 		rdma_dim(cq->dim, completed);
190 }
191 
192 static void ib_cq_completion_workqueue(struct ib_cq *cq, void *private)
193 {
194 	trace_cq_schedule(cq);
195 	queue_work(cq->comp_wq, &cq->work);
196 }
197 
198 /**
199  * __ib_alloc_cq - allocate a completion queue
200  * @dev:		device to allocate the CQ for
201  * @private:		driver private data, accessible from cq->cq_context
202  * @nr_cqe:		number of CQEs to allocate
203  * @comp_vector:	HCA completion vectors for this CQ
204  * @poll_ctx:		context to poll the CQ from.
205  * @caller:		module owner name.
206  *
207  * This is the proper interface to allocate a CQ for in-kernel users. A
208  * CQ allocated with this interface will automatically be polled from the
209  * specified context. The ULP must use wr->wr_cqe instead of wr->wr_id
210  * to use this CQ abstraction.
211  */
212 struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private, int nr_cqe,
213 			    int comp_vector, enum ib_poll_context poll_ctx,
214 			    const char *caller)
215 {
216 	struct ib_cq_init_attr cq_attr = {
217 		.cqe		= nr_cqe,
218 		.comp_vector	= comp_vector,
219 	};
220 	struct ib_cq *cq;
221 	int ret = -ENOMEM;
222 
223 	if (WARN_ON_ONCE(!nr_cqe))
224 		return ERR_PTR(-EINVAL);
225 
226 	cq = rdma_zalloc_drv_obj(dev, ib_cq);
227 	if (!cq)
228 		return ERR_PTR(ret);
229 
230 	cq->device = dev;
231 	cq->cq_context = private;
232 	cq->poll_ctx = poll_ctx;
233 	atomic_set(&cq->usecnt, 0);
234 	cq->comp_vector = comp_vector;
235 
236 	cq->wc = kmalloc_objs(*cq->wc, IB_POLL_BATCH);
237 	if (!cq->wc)
238 		goto out_free_cq;
239 
240 	rdma_restrack_new(&cq->res, RDMA_RESTRACK_CQ);
241 	rdma_restrack_set_name(&cq->res, caller);
242 
243 	ret = dev->ops.create_cq(cq, &cq_attr, NULL);
244 	if (ret)
245 		goto out_free_wc;
246 
247 	rdma_dim_init(cq);
248 
249 	switch (cq->poll_ctx) {
250 	case IB_POLL_DIRECT:
251 		cq->comp_handler = ib_cq_completion_direct;
252 		break;
253 	case IB_POLL_SOFTIRQ:
254 		cq->comp_handler = ib_cq_completion_softirq;
255 
256 		irq_poll_init(&cq->iop, IB_POLL_BUDGET_IRQ, ib_poll_handler);
257 		ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
258 		break;
259 	case IB_POLL_WORKQUEUE:
260 	case IB_POLL_UNBOUND_WORKQUEUE:
261 		cq->comp_handler = ib_cq_completion_workqueue;
262 		INIT_WORK(&cq->work, ib_cq_poll_work);
263 		ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
264 		cq->comp_wq = (cq->poll_ctx == IB_POLL_WORKQUEUE) ?
265 				ib_comp_wq : ib_comp_unbound_wq;
266 		break;
267 	default:
268 		ret = -EINVAL;
269 		goto out_destroy_cq;
270 	}
271 
272 	rdma_restrack_add(&cq->res);
273 	trace_cq_alloc(cq, nr_cqe, comp_vector, poll_ctx);
274 	return cq;
275 
276 out_destroy_cq:
277 	rdma_dim_destroy(cq);
278 	cq->device->ops.destroy_cq(cq, NULL);
279 out_free_wc:
280 	rdma_restrack_put(&cq->res);
281 	kfree(cq->wc);
282 out_free_cq:
283 	kfree(cq);
284 	trace_cq_alloc_error(nr_cqe, comp_vector, poll_ctx, ret);
285 	return ERR_PTR(ret);
286 }
287 EXPORT_SYMBOL(__ib_alloc_cq);
288 
289 /**
290  * __ib_alloc_cq_any - allocate a completion queue
291  * @dev:		device to allocate the CQ for
292  * @private:		driver private data, accessible from cq->cq_context
293  * @nr_cqe:		number of CQEs to allocate
294  * @poll_ctx:		context to poll the CQ from
295  * @caller:		module owner name
296  *
297  * Attempt to spread ULP Completion Queues over each device's interrupt
298  * vectors. A simple best-effort mechanism is used.
299  */
300 struct ib_cq *__ib_alloc_cq_any(struct ib_device *dev, void *private,
301 				int nr_cqe, enum ib_poll_context poll_ctx,
302 				const char *caller)
303 {
304 	static atomic_t counter;
305 	int comp_vector = 0;
306 
307 	if (dev->num_comp_vectors > 1)
308 		comp_vector =
309 			atomic_inc_return(&counter) %
310 			min_t(int, dev->num_comp_vectors, num_online_cpus());
311 
312 	return __ib_alloc_cq(dev, private, nr_cqe, comp_vector, poll_ctx,
313 			     caller);
314 }
315 EXPORT_SYMBOL(__ib_alloc_cq_any);
316 
317 /**
318  * ib_free_cq - free a completion queue
319  * @cq:		completion queue to free.
320  */
321 void ib_free_cq(struct ib_cq *cq)
322 {
323 	int ret = 0;
324 
325 	if (WARN_ON_ONCE(atomic_read(&cq->usecnt)))
326 		return;
327 	if (WARN_ON_ONCE(cq->cqe_used))
328 		return;
329 
330 	if (cq->device->ops.pre_destroy_cq) {
331 		ret = cq->device->ops.pre_destroy_cq(cq);
332 		WARN_ONCE(ret, "Disable of kernel CQ shouldn't fail");
333 	}
334 
335 	switch (cq->poll_ctx) {
336 	case IB_POLL_DIRECT:
337 		break;
338 	case IB_POLL_SOFTIRQ:
339 		irq_poll_disable(&cq->iop);
340 		break;
341 	case IB_POLL_WORKQUEUE:
342 	case IB_POLL_UNBOUND_WORKQUEUE:
343 		cancel_work_sync(&cq->work);
344 		break;
345 	default:
346 		WARN_ON_ONCE(1);
347 	}
348 
349 	rdma_dim_destroy(cq);
350 	trace_cq_free(cq);
351 	if (cq->device->ops.post_destroy_cq)
352 		cq->device->ops.post_destroy_cq(cq);
353 	else
354 		ret = cq->device->ops.destroy_cq(cq, NULL);
355 	WARN_ONCE(ret, "Destroy of kernel CQ shouldn't fail");
356 	rdma_restrack_del(&cq->res);
357 	kfree(cq->wc);
358 	kfree(cq);
359 }
360 EXPORT_SYMBOL(ib_free_cq);
361 
362 void ib_cq_pool_cleanup(struct ib_device *dev)
363 {
364 	struct ib_cq *cq, *n;
365 	unsigned int i;
366 
367 	for (i = 0; i < ARRAY_SIZE(dev->cq_pools); i++) {
368 		list_for_each_entry_safe(cq, n, &dev->cq_pools[i],
369 					 pool_entry) {
370 			WARN_ON(cq->cqe_used);
371 			list_del(&cq->pool_entry);
372 			cq->shared = false;
373 			ib_free_cq(cq);
374 		}
375 	}
376 }
377 
378 static int ib_alloc_cqs(struct ib_device *dev, unsigned int nr_cqes,
379 			enum ib_poll_context poll_ctx)
380 {
381 	LIST_HEAD(tmp_list);
382 	unsigned int nr_cqs, i;
383 	struct ib_cq *cq, *n;
384 	int ret;
385 
386 	if (poll_ctx > IB_POLL_LAST_POOL_TYPE) {
387 		WARN_ON_ONCE(poll_ctx > IB_POLL_LAST_POOL_TYPE);
388 		return -EINVAL;
389 	}
390 
391 	/*
392 	 * Allocate at least as many CQEs as requested, and otherwise
393 	 * a reasonable batch size so that we can share CQs between
394 	 * multiple users instead of allocating a larger number of CQs.
395 	 */
396 	nr_cqes = min_t(unsigned int, dev->attrs.max_cqe,
397 			max(nr_cqes, IB_MAX_SHARED_CQ_SZ));
398 	nr_cqs = min_t(unsigned int, dev->num_comp_vectors, num_online_cpus());
399 	for (i = 0; i < nr_cqs; i++) {
400 		cq = ib_alloc_cq(dev, NULL, nr_cqes, i, poll_ctx);
401 		if (IS_ERR(cq)) {
402 			ret = PTR_ERR(cq);
403 			goto out_free_cqs;
404 		}
405 		cq->shared = true;
406 		list_add_tail(&cq->pool_entry, &tmp_list);
407 	}
408 
409 	spin_lock_irq(&dev->cq_pools_lock);
410 	list_splice(&tmp_list, &dev->cq_pools[poll_ctx]);
411 	spin_unlock_irq(&dev->cq_pools_lock);
412 
413 	return 0;
414 
415 out_free_cqs:
416 	list_for_each_entry_safe(cq, n, &tmp_list, pool_entry) {
417 		cq->shared = false;
418 		ib_free_cq(cq);
419 	}
420 	return ret;
421 }
422 
423 /**
424  * ib_cq_pool_get() - Find the least used completion queue that matches
425  *   a given cpu hint (or least used for wild card affinity) and fits
426  *   nr_cqe.
427  * @dev: rdma device
428  * @nr_cqe: number of needed cqe entries
429  * @comp_vector_hint: completion vector hint (-1) for the driver to assign
430  *   a comp vector based on internal counter
431  * @poll_ctx: cq polling context
432  *
433  * Finds a cq that satisfies @comp_vector_hint and @nr_cqe requirements and
434  * claim entries in it for us.  In case there is no available cq, allocate
435  * a new cq with the requirements and add it to the device pool.
436  * IB_POLL_DIRECT cannot be used for shared cqs so it is not a valid value
437  * for @poll_ctx.
438  */
439 struct ib_cq *ib_cq_pool_get(struct ib_device *dev, unsigned int nr_cqe,
440 			     int comp_vector_hint,
441 			     enum ib_poll_context poll_ctx)
442 {
443 	static unsigned int default_comp_vector;
444 	unsigned int vector, num_comp_vectors;
445 	struct ib_cq *cq, *found = NULL;
446 	int ret;
447 
448 	if (poll_ctx > IB_POLL_LAST_POOL_TYPE) {
449 		WARN_ON_ONCE(poll_ctx > IB_POLL_LAST_POOL_TYPE);
450 		return ERR_PTR(-EINVAL);
451 	}
452 
453 	num_comp_vectors =
454 		min_t(unsigned int, dev->num_comp_vectors, num_online_cpus());
455 	/* Project the affinty to the device completion vector range */
456 	if (comp_vector_hint < 0) {
457 		comp_vector_hint =
458 			(READ_ONCE(default_comp_vector) + 1) % num_comp_vectors;
459 		WRITE_ONCE(default_comp_vector, comp_vector_hint);
460 	}
461 	vector = comp_vector_hint % num_comp_vectors;
462 
463 	/*
464 	 * Find the least used CQ with correct affinity and
465 	 * enough free CQ entries
466 	 */
467 	while (!found) {
468 		spin_lock_irq(&dev->cq_pools_lock);
469 		list_for_each_entry(cq, &dev->cq_pools[poll_ctx],
470 				    pool_entry) {
471 			/*
472 			 * Check to see if we have found a CQ with the
473 			 * correct completion vector
474 			 */
475 			if (vector != cq->comp_vector)
476 				continue;
477 			if (cq->cqe_used + nr_cqe > cq->cqe)
478 				continue;
479 			found = cq;
480 			break;
481 		}
482 
483 		if (found) {
484 			found->cqe_used += nr_cqe;
485 			spin_unlock_irq(&dev->cq_pools_lock);
486 
487 			return found;
488 		}
489 		spin_unlock_irq(&dev->cq_pools_lock);
490 
491 		/*
492 		 * Didn't find a match or ran out of CQs in the device
493 		 * pool, allocate a new array of CQs.
494 		 */
495 		ret = ib_alloc_cqs(dev, nr_cqe, poll_ctx);
496 		if (ret)
497 			return ERR_PTR(ret);
498 	}
499 
500 	return found;
501 }
502 EXPORT_SYMBOL(ib_cq_pool_get);
503 
504 /**
505  * ib_cq_pool_put - Return a CQ taken from a shared pool.
506  * @cq: The CQ to return.
507  * @nr_cqe: The max number of cqes that the user had requested.
508  */
509 void ib_cq_pool_put(struct ib_cq *cq, unsigned int nr_cqe)
510 {
511 	if (WARN_ON_ONCE(nr_cqe > cq->cqe_used))
512 		return;
513 
514 	spin_lock_irq(&cq->device->cq_pools_lock);
515 	cq->cqe_used -= nr_cqe;
516 	spin_unlock_irq(&cq->device->cq_pools_lock);
517 }
518 EXPORT_SYMBOL(ib_cq_pool_put);
519