1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define DEBUG_SUBSYSTEM S_RPC
34
35#include <obd_support.h>
36#include <obd_class.h>
37#include <lustre_net.h>
38#include <lu_object.h>
39#include <uapi/linux/lnet/lnet-types.h>
40#include "ptlrpc_internal.h"
41
42
43int test_req_buffer_pressure;
44module_param(test_req_buffer_pressure, int, 0444);
45MODULE_PARM_DESC(test_req_buffer_pressure, "set non-zero to put pressure on request buffer pools");
46module_param(at_min, int, 0644);
47MODULE_PARM_DESC(at_min, "Adaptive timeout minimum (sec)");
48module_param(at_max, int, 0644);
49MODULE_PARM_DESC(at_max, "Adaptive timeout maximum (sec)");
50module_param(at_history, int, 0644);
51MODULE_PARM_DESC(at_history,
52 "Adaptive timeouts remember the slowest event that took place within this period (sec)");
53module_param(at_early_margin, int, 0644);
54MODULE_PARM_DESC(at_early_margin, "How soon before an RPC deadline to send an early reply");
55module_param(at_extra, int, 0644);
56MODULE_PARM_DESC(at_extra, "How much extra time to give with each early reply");
57
58
59static int ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt);
60static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req);
61static void ptlrpc_at_remove_timed(struct ptlrpc_request *req);
62
63
64LIST_HEAD(ptlrpc_all_services);
65
66struct mutex ptlrpc_all_services_mutex;
67
68static struct ptlrpc_request_buffer_desc *
69ptlrpc_alloc_rqbd(struct ptlrpc_service_part *svcpt)
70{
71 struct ptlrpc_service *svc = svcpt->scp_service;
72 struct ptlrpc_request_buffer_desc *rqbd;
73
74 rqbd = kzalloc_node(sizeof(*rqbd), GFP_NOFS,
75 cfs_cpt_spread_node(svc->srv_cptable,
76 svcpt->scp_cpt));
77 if (!rqbd)
78 return NULL;
79
80 rqbd->rqbd_svcpt = svcpt;
81 rqbd->rqbd_refcount = 0;
82 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
83 rqbd->rqbd_cbid.cbid_arg = rqbd;
84 INIT_LIST_HEAD(&rqbd->rqbd_reqs);
85 rqbd->rqbd_buffer = libcfs_kvzalloc_cpt(svc->srv_cptable,
86 svcpt->scp_cpt,
87 svc->srv_buf_size,
88 GFP_KERNEL);
89 if (!rqbd->rqbd_buffer) {
90 kfree(rqbd);
91 return NULL;
92 }
93
94 spin_lock(&svcpt->scp_lock);
95 list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
96 svcpt->scp_nrqbds_total++;
97 spin_unlock(&svcpt->scp_lock);
98
99 return rqbd;
100}
101
102static void
103ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
104{
105 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
106
107 LASSERT(rqbd->rqbd_refcount == 0);
108 LASSERT(list_empty(&rqbd->rqbd_reqs));
109
110 spin_lock(&svcpt->scp_lock);
111 list_del(&rqbd->rqbd_list);
112 svcpt->scp_nrqbds_total--;
113 spin_unlock(&svcpt->scp_lock);
114
115 kvfree(rqbd->rqbd_buffer);
116 kfree(rqbd);
117}
118
119static int
120ptlrpc_grow_req_bufs(struct ptlrpc_service_part *svcpt, int post)
121{
122 struct ptlrpc_service *svc = svcpt->scp_service;
123 struct ptlrpc_request_buffer_desc *rqbd;
124 int rc = 0;
125 int i;
126
127 if (svcpt->scp_rqbd_allocating)
128 goto try_post;
129
130 spin_lock(&svcpt->scp_lock);
131
132 if (svcpt->scp_rqbd_allocating) {
133
134 LASSERT(svcpt->scp_rqbd_allocating == 1);
135 spin_unlock(&svcpt->scp_lock);
136 goto try_post;
137 }
138
139 svcpt->scp_rqbd_allocating++;
140 spin_unlock(&svcpt->scp_lock);
141
142 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
143
144
145
146 if (svcpt->scp_nrqbds_posted >= svc->srv_nbuf_per_group)
147 break;
148
149 rqbd = ptlrpc_alloc_rqbd(svcpt);
150
151 if (!rqbd) {
152 CERROR("%s: Can't allocate request buffer\n",
153 svc->srv_name);
154 rc = -ENOMEM;
155 break;
156 }
157 }
158
159 spin_lock(&svcpt->scp_lock);
160
161 LASSERT(svcpt->scp_rqbd_allocating == 1);
162 svcpt->scp_rqbd_allocating--;
163
164 spin_unlock(&svcpt->scp_lock);
165
166 CDEBUG(D_RPCTRACE,
167 "%s: allocate %d new %d-byte reqbufs (%d/%d left), rc = %d\n",
168 svc->srv_name, i, svc->srv_buf_size, svcpt->scp_nrqbds_posted,
169 svcpt->scp_nrqbds_total, rc);
170
171 try_post:
172 if (post && rc == 0)
173 rc = ptlrpc_server_post_idle_rqbds(svcpt);
174
175 return rc;
176}
177
178struct ptlrpc_hr_partition;
179
180struct ptlrpc_hr_thread {
181 int hrt_id;
182 spinlock_t hrt_lock;
183 wait_queue_head_t hrt_waitq;
184 struct list_head hrt_queue;
185 struct ptlrpc_hr_partition *hrt_partition;
186};
187
188struct ptlrpc_hr_partition {
189
190 atomic_t hrp_nstarted;
191
192 atomic_t hrp_nstopped;
193
194 int hrp_cpt;
195
196 int hrp_rotor;
197
198 int hrp_nthrs;
199
200 struct ptlrpc_hr_thread *hrp_thrs;
201};
202
203#define HRT_RUNNING 0
204#define HRT_STOPPING 1
205
206struct ptlrpc_hr_service {
207
208 struct cfs_cpt_table *hr_cpt_table;
209
210 wait_queue_head_t hr_waitq;
211 unsigned int hr_stopping;
212
213 unsigned int hr_rotor;
214
215 struct ptlrpc_hr_partition **hr_partitions;
216};
217
218
219static struct ptlrpc_hr_service ptlrpc_hr;
220
221
222
223
224static struct ptlrpc_hr_thread *
225ptlrpc_hr_select(struct ptlrpc_service_part *svcpt)
226{
227 struct ptlrpc_hr_partition *hrp;
228 unsigned int rotor;
229
230 if (svcpt->scp_cpt >= 0 &&
231 svcpt->scp_service->srv_cptable == ptlrpc_hr.hr_cpt_table) {
232
233 hrp = ptlrpc_hr.hr_partitions[svcpt->scp_cpt];
234
235 } else {
236 rotor = ptlrpc_hr.hr_rotor++;
237 rotor %= cfs_cpt_number(ptlrpc_hr.hr_cpt_table);
238
239 hrp = ptlrpc_hr.hr_partitions[rotor];
240 }
241
242 rotor = hrp->hrp_rotor++;
243 return &hrp->hrp_thrs[rotor % hrp->hrp_nthrs];
244}
245
246
247
248
249
250void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
251{
252 struct ptlrpc_hr_thread *hrt;
253
254 LASSERT(list_empty(&rs->rs_list));
255
256 hrt = ptlrpc_hr_select(rs->rs_svcpt);
257
258 spin_lock(&hrt->hrt_lock);
259 list_add_tail(&rs->rs_list, &hrt->hrt_queue);
260 spin_unlock(&hrt->hrt_lock);
261
262 wake_up(&hrt->hrt_waitq);
263}
264
265void
266ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs)
267{
268 assert_spin_locked(&rs->rs_svcpt->scp_rep_lock);
269 assert_spin_locked(&rs->rs_lock);
270 LASSERT(rs->rs_difficult);
271 rs->rs_scheduled_ever = 1;
272
273 if (rs->rs_scheduled) {
274 return;
275 }
276
277 rs->rs_scheduled = 1;
278 list_del_init(&rs->rs_list);
279 ptlrpc_dispatch_difficult_reply(rs);
280}
281EXPORT_SYMBOL(ptlrpc_schedule_difficult_reply);
282
283static int
284ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt)
285{
286 struct ptlrpc_request_buffer_desc *rqbd;
287 int rc;
288 int posted = 0;
289
290 for (;;) {
291 spin_lock(&svcpt->scp_lock);
292
293 if (list_empty(&svcpt->scp_rqbd_idle)) {
294 spin_unlock(&svcpt->scp_lock);
295 return posted;
296 }
297
298 rqbd = list_entry(svcpt->scp_rqbd_idle.next,
299 struct ptlrpc_request_buffer_desc,
300 rqbd_list);
301 list_del(&rqbd->rqbd_list);
302
303
304 svcpt->scp_nrqbds_posted++;
305 list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_posted);
306
307 spin_unlock(&svcpt->scp_lock);
308
309 rc = ptlrpc_register_rqbd(rqbd);
310 if (rc != 0)
311 break;
312
313 posted = 1;
314 }
315
316 spin_lock(&svcpt->scp_lock);
317
318 svcpt->scp_nrqbds_posted--;
319 list_del(&rqbd->rqbd_list);
320 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
321
322
323
324
325
326 spin_unlock(&svcpt->scp_lock);
327
328 return -1;
329}
330
331static void ptlrpc_at_timer(unsigned long castmeharder)
332{
333 struct ptlrpc_service_part *svcpt;
334
335 svcpt = (struct ptlrpc_service_part *)castmeharder;
336
337 svcpt->scp_at_check = 1;
338 svcpt->scp_at_checktime = cfs_time_current();
339 wake_up(&svcpt->scp_waitq);
340}
341
342static void
343ptlrpc_server_nthreads_check(struct ptlrpc_service *svc,
344 struct ptlrpc_service_conf *conf)
345{
346 struct ptlrpc_service_thr_conf *tc = &conf->psc_thr;
347 unsigned int init;
348 unsigned int total;
349 unsigned int nthrs;
350 int weight;
351
352
353
354
355
356
357
358
359
360
361 init = PTLRPC_NTHRS_INIT + (svc->srv_ops.so_hpreq_handler != NULL);
362 init = max_t(int, init, tc->tc_nthrs_init);
363
364
365
366
367 LASSERT(tc->tc_nthrs_max != 0);
368
369 if (tc->tc_nthrs_user != 0) {
370
371
372
373
374 total = min(tc->tc_nthrs_max * 8, tc->tc_nthrs_user);
375 nthrs = total / svc->srv_ncpts;
376 init = max(init, nthrs);
377 goto out;
378 }
379
380 total = tc->tc_nthrs_max;
381 if (tc->tc_nthrs_base == 0) {
382
383
384
385 nthrs = total / svc->srv_ncpts;
386 goto out;
387 }
388
389 nthrs = tc->tc_nthrs_base;
390 if (svc->srv_ncpts == 1) {
391 int i;
392
393
394
395
396
397 weight = cfs_cpt_weight(svc->srv_cptable, CFS_CPT_ANY);
398 for (i = 1; (weight >> (i + 1)) != 0 &&
399 (tc->tc_nthrs_base >> i) != 0; i++)
400 nthrs += tc->tc_nthrs_base >> i;
401 }
402
403 if (tc->tc_thr_factor != 0) {
404 int factor = tc->tc_thr_factor;
405 const int fade = 4;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420 if (cpumask_weight(topology_sibling_cpumask(0)) > 1) {
421
422 factor = factor - (factor >> 1) + (factor >> 3);
423 }
424
425 weight = cfs_cpt_weight(svc->srv_cptable, 0);
426 LASSERT(weight > 0);
427
428 for (; factor > 0 && weight > 0; factor--, weight -= fade)
429 nthrs += min(weight, fade) * factor;
430 }
431
432 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
433 nthrs = max(tc->tc_nthrs_base,
434 tc->tc_nthrs_max / svc->srv_ncpts);
435 }
436 out:
437 nthrs = max(nthrs, tc->tc_nthrs_init);
438 svc->srv_nthrs_cpt_limit = nthrs;
439 svc->srv_nthrs_cpt_init = init;
440
441 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
442 CDEBUG(D_OTHER, "%s: This service may have more threads (%d) than the given soft limit (%d)\n",
443 svc->srv_name, nthrs * svc->srv_ncpts,
444 tc->tc_nthrs_max);
445 }
446}
447
448
449
450
451static int
452ptlrpc_service_part_init(struct ptlrpc_service *svc,
453 struct ptlrpc_service_part *svcpt, int cpt)
454{
455 struct ptlrpc_at_array *array;
456 int size;
457 int index;
458 int rc;
459
460 svcpt->scp_cpt = cpt;
461 INIT_LIST_HEAD(&svcpt->scp_threads);
462
463
464 spin_lock_init(&svcpt->scp_lock);
465 INIT_LIST_HEAD(&svcpt->scp_rqbd_idle);
466 INIT_LIST_HEAD(&svcpt->scp_rqbd_posted);
467 INIT_LIST_HEAD(&svcpt->scp_req_incoming);
468 init_waitqueue_head(&svcpt->scp_waitq);
469
470 INIT_LIST_HEAD(&svcpt->scp_hist_reqs);
471 INIT_LIST_HEAD(&svcpt->scp_hist_rqbds);
472
473
474 spin_lock_init(&svcpt->scp_req_lock);
475
476
477 spin_lock_init(&svcpt->scp_rep_lock);
478 INIT_LIST_HEAD(&svcpt->scp_rep_active);
479 INIT_LIST_HEAD(&svcpt->scp_rep_idle);
480 init_waitqueue_head(&svcpt->scp_rep_waitq);
481 atomic_set(&svcpt->scp_nreps_difficult, 0);
482
483
484 spin_lock_init(&svcpt->scp_at_lock);
485 array = &svcpt->scp_at_array;
486
487 size = at_est2timeout(at_max);
488 array->paa_size = size;
489 array->paa_count = 0;
490 array->paa_deadline = -1;
491
492
493 array->paa_reqs_array =
494 kzalloc_node(sizeof(struct list_head) * size, GFP_NOFS,
495 cfs_cpt_spread_node(svc->srv_cptable, cpt));
496 if (!array->paa_reqs_array)
497 return -ENOMEM;
498
499 for (index = 0; index < size; index++)
500 INIT_LIST_HEAD(&array->paa_reqs_array[index]);
501
502 array->paa_reqs_count =
503 kzalloc_node(sizeof(__u32) * size, GFP_NOFS,
504 cfs_cpt_spread_node(svc->srv_cptable, cpt));
505 if (!array->paa_reqs_count)
506 goto free_reqs_array;
507
508 setup_timer(&svcpt->scp_at_timer, ptlrpc_at_timer,
509 (unsigned long)svcpt);
510
511
512
513
514 at_init(&svcpt->scp_at_estimate, 10, 0);
515
516
517 svcpt->scp_service = svc;
518
519 rc = ptlrpc_grow_req_bufs(svcpt, 0);
520
521
522
523 if (rc != 0)
524 goto free_reqs_count;
525
526 return 0;
527
528free_reqs_count:
529 kfree(array->paa_reqs_count);
530 array->paa_reqs_count = NULL;
531free_reqs_array:
532 kfree(array->paa_reqs_array);
533 array->paa_reqs_array = NULL;
534
535 return -ENOMEM;
536}
537
538
539
540
541
542
543struct ptlrpc_service *
544ptlrpc_register_service(struct ptlrpc_service_conf *conf,
545 struct kset *parent,
546 struct dentry *debugfs_entry)
547{
548 struct ptlrpc_service_cpt_conf *cconf = &conf->psc_cpt;
549 struct ptlrpc_service *service;
550 struct ptlrpc_service_part *svcpt;
551 struct cfs_cpt_table *cptable;
552 __u32 *cpts = NULL;
553 int ncpts;
554 int cpt;
555 int rc;
556 int i;
557
558 LASSERT(conf->psc_buf.bc_nbufs > 0);
559 LASSERT(conf->psc_buf.bc_buf_size >=
560 conf->psc_buf.bc_req_max_size + SPTLRPC_MAX_PAYLOAD);
561 LASSERT(conf->psc_thr.tc_ctx_tags != 0);
562
563 cptable = cconf->cc_cptable;
564 if (!cptable)
565 cptable = cfs_cpt_table;
566
567 if (!conf->psc_thr.tc_cpu_affinity) {
568 ncpts = 1;
569 } else {
570 ncpts = cfs_cpt_number(cptable);
571 if (cconf->cc_pattern) {
572 struct cfs_expr_list *el;
573
574 rc = cfs_expr_list_parse(cconf->cc_pattern,
575 strlen(cconf->cc_pattern),
576 0, ncpts - 1, &el);
577 if (rc != 0) {
578 CERROR("%s: invalid CPT pattern string: %s",
579 conf->psc_name, cconf->cc_pattern);
580 return ERR_PTR(-EINVAL);
581 }
582
583 rc = cfs_expr_list_values(el, ncpts, &cpts);
584 cfs_expr_list_free(el);
585 if (rc <= 0) {
586 CERROR("%s: failed to parse CPT array %s: %d\n",
587 conf->psc_name, cconf->cc_pattern, rc);
588 kfree(cpts);
589 return ERR_PTR(rc < 0 ? rc : -EINVAL);
590 }
591 ncpts = rc;
592 }
593 }
594
595 service = kzalloc(offsetof(struct ptlrpc_service, srv_parts[ncpts]),
596 GFP_NOFS);
597 if (!service) {
598 kfree(cpts);
599 return ERR_PTR(-ENOMEM);
600 }
601
602 service->srv_cptable = cptable;
603 service->srv_cpts = cpts;
604 service->srv_ncpts = ncpts;
605
606 service->srv_cpt_bits = 0;
607 while ((1 << service->srv_cpt_bits) < cfs_cpt_number(cptable))
608 service->srv_cpt_bits++;
609
610
611 spin_lock_init(&service->srv_lock);
612 service->srv_name = conf->psc_name;
613 service->srv_watchdog_factor = conf->psc_watchdog_factor;
614 INIT_LIST_HEAD(&service->srv_list);
615
616
617 service->srv_nbuf_per_group = test_req_buffer_pressure ?
618 1 : conf->psc_buf.bc_nbufs;
619 service->srv_max_req_size = conf->psc_buf.bc_req_max_size +
620 SPTLRPC_MAX_PAYLOAD;
621 service->srv_buf_size = conf->psc_buf.bc_buf_size;
622 service->srv_rep_portal = conf->psc_buf.bc_rep_portal;
623 service->srv_req_portal = conf->psc_buf.bc_req_portal;
624
625
626 service->srv_max_reply_size = 1;
627 while (service->srv_max_reply_size <
628 conf->psc_buf.bc_rep_max_size + SPTLRPC_MAX_PAYLOAD)
629 service->srv_max_reply_size <<= 1;
630
631 service->srv_thread_name = conf->psc_thr.tc_thr_name;
632 service->srv_ctx_tags = conf->psc_thr.tc_ctx_tags;
633 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
634 service->srv_ops = conf->psc_ops;
635
636 for (i = 0; i < ncpts; i++) {
637 if (!conf->psc_thr.tc_cpu_affinity)
638 cpt = CFS_CPT_ANY;
639 else
640 cpt = cpts ? cpts[i] : i;
641
642 svcpt = kzalloc_node(sizeof(*svcpt), GFP_NOFS,
643 cfs_cpt_spread_node(cptable, cpt));
644 if (!svcpt) {
645 rc = -ENOMEM;
646 goto failed;
647 }
648
649 service->srv_parts[i] = svcpt;
650 rc = ptlrpc_service_part_init(service, svcpt, cpt);
651 if (rc != 0)
652 goto failed;
653 }
654
655 ptlrpc_server_nthreads_check(service, conf);
656
657 rc = LNetSetLazyPortal(service->srv_req_portal);
658 LASSERT(rc == 0);
659
660 mutex_lock(&ptlrpc_all_services_mutex);
661 list_add(&service->srv_list, &ptlrpc_all_services);
662 mutex_unlock(&ptlrpc_all_services_mutex);
663
664 if (parent) {
665 rc = ptlrpc_sysfs_register_service(parent, service);
666 if (rc)
667 goto failed;
668 }
669
670 if (!IS_ERR_OR_NULL(debugfs_entry))
671 ptlrpc_ldebugfs_register_service(debugfs_entry, service);
672
673 rc = ptlrpc_service_nrs_setup(service);
674 if (rc != 0)
675 goto failed;
676
677 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
678 service->srv_name, service->srv_req_portal);
679
680 rc = ptlrpc_start_threads(service);
681 if (rc != 0) {
682 CERROR("Failed to start threads for service %s: %d\n",
683 service->srv_name, rc);
684 goto failed;
685 }
686
687 return service;
688failed:
689 ptlrpc_unregister_service(service);
690 return ERR_PTR(rc);
691}
692EXPORT_SYMBOL(ptlrpc_register_service);
693
694
695
696
697
698static void ptlrpc_server_free_request(struct ptlrpc_request *req)
699{
700 LASSERT(atomic_read(&req->rq_refcount) == 0);
701 LASSERT(list_empty(&req->rq_timed_list));
702
703
704
705
706 ptlrpc_req_drop_rs(req);
707
708 sptlrpc_svc_ctx_decref(req);
709
710 if (req != &req->rq_rqbd->rqbd_req) {
711
712
713
714
715 ptlrpc_request_cache_free(req);
716 }
717}
718
719
720
721
722
723static void ptlrpc_server_drop_request(struct ptlrpc_request *req)
724{
725 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
726 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
727 struct ptlrpc_service *svc = svcpt->scp_service;
728 int refcount;
729 struct list_head *tmp;
730 struct list_head *nxt;
731
732 if (!atomic_dec_and_test(&req->rq_refcount))
733 return;
734
735 if (req->rq_at_linked) {
736 spin_lock(&svcpt->scp_at_lock);
737
738
739
740 if (likely(req->rq_at_linked))
741 ptlrpc_at_remove_timed(req);
742 spin_unlock(&svcpt->scp_at_lock);
743 }
744
745 LASSERT(list_empty(&req->rq_timed_list));
746
747
748 if (req->rq_export) {
749 class_export_put(req->rq_export);
750 req->rq_export = NULL;
751 }
752
753 spin_lock(&svcpt->scp_lock);
754
755 list_add(&req->rq_list, &rqbd->rqbd_reqs);
756
757 refcount = --(rqbd->rqbd_refcount);
758 if (refcount == 0) {
759
760 list_del(&rqbd->rqbd_list);
761
762 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_hist_rqbds);
763 svcpt->scp_hist_nrqbds++;
764
765
766
767
768 while (svcpt->scp_hist_nrqbds > svc->srv_hist_nrqbds_cpt_max) {
769 rqbd = list_entry(svcpt->scp_hist_rqbds.next,
770 struct ptlrpc_request_buffer_desc,
771 rqbd_list);
772
773 list_del(&rqbd->rqbd_list);
774 svcpt->scp_hist_nrqbds--;
775
776
777
778
779 list_for_each(tmp, &rqbd->rqbd_reqs) {
780 req = list_entry(tmp, struct ptlrpc_request,
781 rq_list);
782
783 if (req->rq_history_seq >
784 svcpt->scp_hist_seq_culled) {
785 svcpt->scp_hist_seq_culled =
786 req->rq_history_seq;
787 }
788 list_del(&req->rq_history_list);
789 }
790
791 spin_unlock(&svcpt->scp_lock);
792
793 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
794 req = list_entry(rqbd->rqbd_reqs.next,
795 struct ptlrpc_request,
796 rq_list);
797 list_del(&req->rq_list);
798 ptlrpc_server_free_request(req);
799 }
800
801 spin_lock(&svcpt->scp_lock);
802
803
804
805
806 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) ==
807 0);
808 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
809 }
810
811 spin_unlock(&svcpt->scp_lock);
812 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
813
814 list_del(&req->rq_list);
815 list_del_init(&req->rq_history_list);
816
817
818 if (req->rq_history_seq > svcpt->scp_hist_seq_culled)
819 svcpt->scp_hist_seq_culled = req->rq_history_seq;
820
821 spin_unlock(&svcpt->scp_lock);
822
823 ptlrpc_server_free_request(req);
824 } else {
825 spin_unlock(&svcpt->scp_lock);
826 }
827}
828
829
830
831
832
833static void ptlrpc_server_finish_request(struct ptlrpc_service_part *svcpt,
834 struct ptlrpc_request *req)
835{
836 ptlrpc_server_hpreq_fini(req);
837
838 if (req->rq_session.lc_thread) {
839 lu_context_exit(&req->rq_session);
840 lu_context_fini(&req->rq_session);
841 }
842
843 ptlrpc_server_drop_request(req);
844}
845
846
847
848
849
850static void ptlrpc_server_finish_active_request(
851 struct ptlrpc_service_part *svcpt,
852 struct ptlrpc_request *req)
853{
854 spin_lock(&svcpt->scp_req_lock);
855 ptlrpc_nrs_req_stop_nolock(req);
856 svcpt->scp_nreqs_active--;
857 if (req->rq_hp)
858 svcpt->scp_nhreqs_active--;
859 spin_unlock(&svcpt->scp_req_lock);
860
861 ptlrpc_nrs_req_finalize(req);
862
863 if (req->rq_export)
864 class_export_rpc_dec(req->rq_export);
865
866 ptlrpc_server_finish_request(svcpt, req);
867}
868
869
870
871
872
873static int ptlrpc_check_req(struct ptlrpc_request *req)
874{
875 struct obd_device *obd = req->rq_export->exp_obd;
876 int rc = 0;
877
878 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
879 req->rq_export->exp_conn_cnt)) {
880 DEBUG_REQ(D_RPCTRACE, req,
881 "DROPPING req from old connection %d < %d",
882 lustre_msg_get_conn_cnt(req->rq_reqmsg),
883 req->rq_export->exp_conn_cnt);
884 return -EEXIST;
885 }
886 if (unlikely(!obd || obd->obd_fail)) {
887
888
889
890
891 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
892 req, obd ? obd->obd_name : "unknown");
893 rc = -ENODEV;
894 } else if (lustre_msg_get_flags(req->rq_reqmsg) &
895 (MSG_REPLAY | MSG_REQ_REPLAY_DONE)) {
896 DEBUG_REQ(D_ERROR, req, "Invalid replay without recovery");
897 class_fail_export(req->rq_export);
898 rc = -ENODEV;
899 } else if (lustre_msg_get_transno(req->rq_reqmsg) != 0) {
900 DEBUG_REQ(D_ERROR, req,
901 "Invalid req with transno %llu without recovery",
902 lustre_msg_get_transno(req->rq_reqmsg));
903 class_fail_export(req->rq_export);
904 rc = -ENODEV;
905 }
906
907 if (unlikely(rc < 0)) {
908 req->rq_status = rc;
909 ptlrpc_error(req);
910 }
911 return rc;
912}
913
914static void ptlrpc_at_set_timer(struct ptlrpc_service_part *svcpt)
915{
916 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
917 __s32 next;
918
919 if (array->paa_count == 0) {
920 del_timer(&svcpt->scp_at_timer);
921 return;
922 }
923
924
925 next = (__s32)(array->paa_deadline - ktime_get_real_seconds() -
926 at_early_margin);
927 if (next <= 0) {
928 ptlrpc_at_timer((unsigned long)svcpt);
929 } else {
930 mod_timer(&svcpt->scp_at_timer, cfs_time_shift(next));
931 CDEBUG(D_INFO, "armed %s at %+ds\n",
932 svcpt->scp_service->srv_name, next);
933 }
934}
935
936
937static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
938{
939 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
940 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
941 struct ptlrpc_request *rq = NULL;
942 __u32 index;
943
944 if (AT_OFF)
945 return 0;
946
947 if (req->rq_no_reply)
948 return 0;
949
950 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
951 return -ENOSYS;
952
953 spin_lock(&svcpt->scp_at_lock);
954 LASSERT(list_empty(&req->rq_timed_list));
955
956 div_u64_rem(req->rq_deadline, array->paa_size, &index);
957 if (array->paa_reqs_count[index] > 0) {
958
959
960
961 list_for_each_entry_reverse(rq, &array->paa_reqs_array[index],
962 rq_timed_list) {
963 if (req->rq_deadline >= rq->rq_deadline) {
964 list_add(&req->rq_timed_list,
965 &rq->rq_timed_list);
966 break;
967 }
968 }
969 }
970
971
972 if (list_empty(&req->rq_timed_list))
973 list_add(&req->rq_timed_list, &array->paa_reqs_array[index]);
974
975 spin_lock(&req->rq_lock);
976 req->rq_at_linked = 1;
977 spin_unlock(&req->rq_lock);
978 req->rq_at_index = index;
979 array->paa_reqs_count[index]++;
980 array->paa_count++;
981 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
982 array->paa_deadline = req->rq_deadline;
983 ptlrpc_at_set_timer(svcpt);
984 }
985 spin_unlock(&svcpt->scp_at_lock);
986
987 return 0;
988}
989
990static void
991ptlrpc_at_remove_timed(struct ptlrpc_request *req)
992{
993 struct ptlrpc_at_array *array;
994
995 array = &req->rq_rqbd->rqbd_svcpt->scp_at_array;
996
997
998 LASSERT(!list_empty(&req->rq_timed_list));
999 list_del_init(&req->rq_timed_list);
1000
1001 spin_lock(&req->rq_lock);
1002 req->rq_at_linked = 0;
1003 spin_unlock(&req->rq_lock);
1004
1005 array->paa_reqs_count[req->rq_at_index]--;
1006 array->paa_count--;
1007}
1008
1009
1010
1011
1012
1013static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
1014{
1015 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1016 struct ptlrpc_request *reqcopy;
1017 struct lustre_msg *reqmsg;
1018 long olddl = req->rq_deadline - ktime_get_real_seconds();
1019 time64_t newdl;
1020 int rc;
1021
1022
1023
1024
1025 DEBUG_REQ(D_ADAPTTO, req,
1026 "%ssending early reply (deadline %+lds, margin %+lds) for %d+%d",
1027 AT_OFF ? "AT off - not " : "",
1028 olddl, olddl - at_get(&svcpt->scp_at_estimate),
1029 at_get(&svcpt->scp_at_estimate), at_extra);
1030
1031 if (AT_OFF)
1032 return 0;
1033
1034 if (olddl < 0) {
1035 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), not sending early reply. Consider increasing at_early_margin (%d)?",
1036 olddl, at_early_margin);
1037
1038
1039 return -ETIMEDOUT;
1040 }
1041
1042 if (!(lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
1043 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, but no AT support");
1044 return -ENOSYS;
1045 }
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055 at_measured(&svcpt->scp_at_estimate, at_extra +
1056 ktime_get_real_seconds() - req->rq_arrival_time.tv_sec);
1057 newdl = req->rq_arrival_time.tv_sec + at_get(&svcpt->scp_at_estimate);
1058
1059
1060
1061
1062 if (req->rq_deadline >= newdl) {
1063 DEBUG_REQ(D_WARNING, req, "Couldn't add any time (%ld/%lld), not sending early reply\n",
1064 olddl, newdl - ktime_get_real_seconds());
1065 return -ETIMEDOUT;
1066 }
1067
1068 reqcopy = ptlrpc_request_cache_alloc(GFP_NOFS);
1069 if (!reqcopy)
1070 return -ENOMEM;
1071 reqmsg = libcfs_kvzalloc(req->rq_reqlen, GFP_NOFS);
1072 if (!reqmsg) {
1073 rc = -ENOMEM;
1074 goto out_free;
1075 }
1076
1077 *reqcopy = *req;
1078 reqcopy->rq_reply_state = NULL;
1079 reqcopy->rq_rep_swab_mask = 0;
1080 reqcopy->rq_pack_bulk = 0;
1081 reqcopy->rq_pack_udesc = 0;
1082 reqcopy->rq_packed_final = 0;
1083 sptlrpc_svc_ctx_addref(reqcopy);
1084
1085 reqcopy->rq_reqmsg = reqmsg;
1086 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1087
1088 LASSERT(atomic_read(&req->rq_refcount));
1089
1090 if (atomic_read(&req->rq_refcount) == 1) {
1091 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, abort sending early reply\n");
1092 rc = -EINVAL;
1093 goto out;
1094 }
1095
1096
1097 reqcopy->rq_export = class_conn2export(
1098 lustre_msg_get_handle(reqcopy->rq_reqmsg));
1099 if (!reqcopy->rq_export) {
1100 rc = -ENODEV;
1101 goto out;
1102 }
1103
1104
1105 class_export_rpc_inc(reqcopy->rq_export);
1106 if (reqcopy->rq_export->exp_obd &&
1107 reqcopy->rq_export->exp_obd->obd_fail) {
1108 rc = -ENODEV;
1109 goto out_put;
1110 }
1111
1112 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1113 if (rc)
1114 goto out_put;
1115
1116 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1117
1118 if (!rc) {
1119
1120 req->rq_deadline = newdl;
1121 req->rq_early_count++;
1122 } else {
1123 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1124 }
1125
1126
1127
1128
1129 ptlrpc_req_drop_rs(reqcopy);
1130
1131out_put:
1132 class_export_rpc_dec(reqcopy->rq_export);
1133 class_export_put(reqcopy->rq_export);
1134out:
1135 sptlrpc_svc_ctx_decref(reqcopy);
1136 kvfree(reqmsg);
1137out_free:
1138 ptlrpc_request_cache_free(reqcopy);
1139 return rc;
1140}
1141
1142
1143
1144
1145static void ptlrpc_at_check_timed(struct ptlrpc_service_part *svcpt)
1146{
1147 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1148 struct ptlrpc_request *rq, *n;
1149 struct list_head work_list;
1150 __u32 index, count;
1151 time64_t deadline;
1152 time64_t now = ktime_get_real_seconds();
1153 long delay;
1154 int first, counter = 0;
1155
1156 spin_lock(&svcpt->scp_at_lock);
1157 if (svcpt->scp_at_check == 0) {
1158 spin_unlock(&svcpt->scp_at_lock);
1159 return;
1160 }
1161 delay = cfs_time_sub(cfs_time_current(), svcpt->scp_at_checktime);
1162 svcpt->scp_at_check = 0;
1163
1164 if (array->paa_count == 0) {
1165 spin_unlock(&svcpt->scp_at_lock);
1166 return;
1167 }
1168
1169
1170 first = array->paa_deadline - now;
1171 if (first > at_early_margin) {
1172
1173 ptlrpc_at_set_timer(svcpt);
1174 spin_unlock(&svcpt->scp_at_lock);
1175 return;
1176 }
1177
1178
1179
1180
1181 INIT_LIST_HEAD(&work_list);
1182 deadline = -1;
1183 div_u64_rem(array->paa_deadline, array->paa_size, &index);
1184 count = array->paa_count;
1185 while (count > 0) {
1186 count -= array->paa_reqs_count[index];
1187 list_for_each_entry_safe(rq, n, &array->paa_reqs_array[index],
1188 rq_timed_list) {
1189 if (rq->rq_deadline > now + at_early_margin) {
1190
1191 if (deadline == -1 ||
1192 rq->rq_deadline < deadline)
1193 deadline = rq->rq_deadline;
1194 break;
1195 }
1196
1197 ptlrpc_at_remove_timed(rq);
1198
1199
1200
1201
1202
1203 if (likely(atomic_inc_not_zero(&rq->rq_refcount)))
1204 list_add(&rq->rq_timed_list, &work_list);
1205 counter++;
1206 }
1207
1208 if (++index >= array->paa_size)
1209 index = 0;
1210 }
1211 array->paa_deadline = deadline;
1212
1213 ptlrpc_at_set_timer(svcpt);
1214
1215 spin_unlock(&svcpt->scp_at_lock);
1216
1217 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early replies\n",
1218 first, at_extra, counter);
1219 if (first < 0) {
1220
1221
1222
1223 LCONSOLE_WARN("%s: This server is not able to keep up with request traffic (cpu-bound).\n",
1224 svcpt->scp_service->srv_name);
1225 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, delay=%ld(jiff)\n",
1226 counter, svcpt->scp_nreqs_incoming,
1227 svcpt->scp_nreqs_active,
1228 at_get(&svcpt->scp_at_estimate), delay);
1229 }
1230
1231
1232
1233
1234 while (!list_empty(&work_list)) {
1235 rq = list_entry(work_list.next, struct ptlrpc_request,
1236 rq_timed_list);
1237 list_del_init(&rq->rq_timed_list);
1238
1239 if (ptlrpc_at_send_early_reply(rq) == 0)
1240 ptlrpc_at_add_timed(rq);
1241
1242 ptlrpc_server_drop_request(rq);
1243 }
1244}
1245
1246
1247
1248
1249
1250static int ptlrpc_server_hpreq_init(struct ptlrpc_service_part *svcpt,
1251 struct ptlrpc_request *req)
1252{
1253 int rc = 0;
1254
1255 if (svcpt->scp_service->srv_ops.so_hpreq_handler) {
1256 rc = svcpt->scp_service->srv_ops.so_hpreq_handler(req);
1257 if (rc < 0)
1258 return rc;
1259 LASSERT(rc == 0);
1260 }
1261 if (req->rq_export && req->rq_ops) {
1262
1263
1264
1265
1266 if (req->rq_ops->hpreq_check) {
1267 rc = req->rq_ops->hpreq_check(req);
1268 if (rc == -ESTALE) {
1269 req->rq_status = rc;
1270 ptlrpc_error(req);
1271 }
1272
1273
1274
1275
1276 LASSERT(rc <= 1);
1277 }
1278
1279 spin_lock_bh(&req->rq_export->exp_rpc_lock);
1280 list_add(&req->rq_exp_list, &req->rq_export->exp_hp_rpcs);
1281 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1282 }
1283
1284 ptlrpc_nrs_req_initialize(svcpt, req, rc);
1285
1286 return rc;
1287}
1288
1289
1290static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req)
1291{
1292 if (req->rq_export && req->rq_ops) {
1293
1294
1295
1296 if (req->rq_ops->hpreq_fini)
1297 req->rq_ops->hpreq_fini(req);
1298
1299 spin_lock_bh(&req->rq_export->exp_rpc_lock);
1300 list_del_init(&req->rq_exp_list);
1301 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1302 }
1303}
1304
1305static int ptlrpc_server_request_add(struct ptlrpc_service_part *svcpt,
1306 struct ptlrpc_request *req)
1307{
1308 int rc;
1309
1310 rc = ptlrpc_server_hpreq_init(svcpt, req);
1311 if (rc < 0)
1312 return rc;
1313
1314 ptlrpc_nrs_req_add(svcpt, req, !!rc);
1315
1316 return 0;
1317}
1318
1319
1320
1321
1322
1323
1324static bool ptlrpc_server_allow_high(struct ptlrpc_service_part *svcpt,
1325 bool force)
1326{
1327 int running = svcpt->scp_nthrs_running;
1328
1329 if (!nrs_svcpt_has_hp(svcpt))
1330 return false;
1331
1332 if (force)
1333 return true;
1334
1335 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1336 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1337
1338 running = PTLRPC_NTHRS_INIT;
1339 if (svcpt->scp_service->srv_ops.so_hpreq_handler)
1340 running += 1;
1341 }
1342
1343 if (svcpt->scp_nreqs_active >= running - 1)
1344 return false;
1345
1346 if (svcpt->scp_nhreqs_active == 0)
1347 return true;
1348
1349 return !ptlrpc_nrs_req_pending_nolock(svcpt, false) ||
1350 svcpt->scp_hreq_count < svcpt->scp_service->srv_hpreq_ratio;
1351}
1352
1353static bool ptlrpc_server_high_pending(struct ptlrpc_service_part *svcpt,
1354 bool force)
1355{
1356 return ptlrpc_server_allow_high(svcpt, force) &&
1357 ptlrpc_nrs_req_pending_nolock(svcpt, true);
1358}
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369static bool ptlrpc_server_allow_normal(struct ptlrpc_service_part *svcpt,
1370 bool force)
1371{
1372 int running = svcpt->scp_nthrs_running;
1373
1374 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1375 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1376
1377 running = PTLRPC_NTHRS_INIT;
1378 if (svcpt->scp_service->srv_ops.so_hpreq_handler)
1379 running += 1;
1380 }
1381
1382 if (force ||
1383 svcpt->scp_nreqs_active < running - 2)
1384 return true;
1385
1386 if (svcpt->scp_nreqs_active >= running - 1)
1387 return false;
1388
1389 return svcpt->scp_nhreqs_active > 0 || !nrs_svcpt_has_hp(svcpt);
1390}
1391
1392static bool ptlrpc_server_normal_pending(struct ptlrpc_service_part *svcpt,
1393 bool force)
1394{
1395 return ptlrpc_server_allow_normal(svcpt, force) &&
1396 ptlrpc_nrs_req_pending_nolock(svcpt, false);
1397}
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407static inline bool
1408ptlrpc_server_request_pending(struct ptlrpc_service_part *svcpt, bool force)
1409{
1410 return ptlrpc_server_high_pending(svcpt, force) ||
1411 ptlrpc_server_normal_pending(svcpt, force);
1412}
1413
1414
1415
1416
1417
1418
1419static struct ptlrpc_request *
1420ptlrpc_server_request_get(struct ptlrpc_service_part *svcpt, bool force)
1421{
1422 struct ptlrpc_request *req = NULL;
1423
1424 spin_lock(&svcpt->scp_req_lock);
1425
1426 if (ptlrpc_server_high_pending(svcpt, force)) {
1427 req = ptlrpc_nrs_req_get_nolock(svcpt, true, force);
1428 if (req) {
1429 svcpt->scp_hreq_count++;
1430 goto got_request;
1431 }
1432 }
1433
1434 if (ptlrpc_server_normal_pending(svcpt, force)) {
1435 req = ptlrpc_nrs_req_get_nolock(svcpt, false, force);
1436 if (req) {
1437 svcpt->scp_hreq_count = 0;
1438 goto got_request;
1439 }
1440 }
1441
1442 spin_unlock(&svcpt->scp_req_lock);
1443 return NULL;
1444
1445got_request:
1446 svcpt->scp_nreqs_active++;
1447 if (req->rq_hp)
1448 svcpt->scp_nhreqs_active++;
1449
1450 spin_unlock(&svcpt->scp_req_lock);
1451
1452 if (likely(req->rq_export))
1453 class_export_rpc_inc(req->rq_export);
1454
1455 return req;
1456}
1457
1458
1459
1460
1461
1462
1463
1464static int
1465ptlrpc_server_handle_req_in(struct ptlrpc_service_part *svcpt,
1466 struct ptlrpc_thread *thread)
1467{
1468 struct ptlrpc_service *svc = svcpt->scp_service;
1469 struct ptlrpc_request *req;
1470 __u32 deadline;
1471 int rc;
1472
1473 spin_lock(&svcpt->scp_lock);
1474 if (list_empty(&svcpt->scp_req_incoming)) {
1475 spin_unlock(&svcpt->scp_lock);
1476 return 0;
1477 }
1478
1479 req = list_entry(svcpt->scp_req_incoming.next,
1480 struct ptlrpc_request, rq_list);
1481 list_del_init(&req->rq_list);
1482 svcpt->scp_nreqs_incoming--;
1483
1484
1485
1486 spin_unlock(&svcpt->scp_lock);
1487
1488
1489 rc = sptlrpc_svc_unwrap_request(req);
1490 switch (rc) {
1491 case SECSVC_OK:
1492 break;
1493 case SECSVC_COMPLETE:
1494 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1495 goto err_req;
1496 case SECSVC_DROP:
1497 goto err_req;
1498 default:
1499 LBUG();
1500 }
1501
1502
1503
1504
1505
1506 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1507 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1508 if (rc != 0) {
1509 CERROR("error unpacking request: ptl %d from %s x%llu\n",
1510 svc->srv_req_portal, libcfs_id2str(req->rq_peer),
1511 req->rq_xid);
1512 goto err_req;
1513 }
1514 }
1515
1516 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1517 if (rc) {
1518 CERROR("error unpacking ptlrpc body: ptl %d from %s x%llu\n",
1519 svc->srv_req_portal, libcfs_id2str(req->rq_peer),
1520 req->rq_xid);
1521 goto err_req;
1522 }
1523
1524 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1525 lustre_msg_get_opc(req->rq_reqmsg) == cfs_fail_val) {
1526 CERROR("drop incoming rpc opc %u, x%llu\n",
1527 cfs_fail_val, req->rq_xid);
1528 goto err_req;
1529 }
1530
1531 rc = -EINVAL;
1532 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1533 CERROR("wrong packet type received (type=%u) from %s\n",
1534 lustre_msg_get_type(req->rq_reqmsg),
1535 libcfs_id2str(req->rq_peer));
1536 goto err_req;
1537 }
1538
1539 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1540 case MDS_WRITEPAGE:
1541 case OST_WRITE:
1542 req->rq_bulk_write = 1;
1543 break;
1544 case MDS_READPAGE:
1545 case OST_READ:
1546 case MGS_CONFIG_READ:
1547 req->rq_bulk_read = 1;
1548 break;
1549 }
1550
1551 CDEBUG(D_RPCTRACE, "got req x%llu\n", req->rq_xid);
1552
1553 req->rq_export = class_conn2export(
1554 lustre_msg_get_handle(req->rq_reqmsg));
1555 if (req->rq_export) {
1556 rc = ptlrpc_check_req(req);
1557 if (rc == 0) {
1558 rc = sptlrpc_target_export_check(req->rq_export, req);
1559 if (rc)
1560 DEBUG_REQ(D_ERROR, req, "DROPPING req with illegal security flavor,");
1561 }
1562
1563 if (rc)
1564 goto err_req;
1565 }
1566
1567
1568 if (ktime_get_real_seconds() - req->rq_arrival_time.tv_sec > 5)
1569 DEBUG_REQ(D_WARNING, req, "Slow req_in handling %llds",
1570 (s64)(ktime_get_real_seconds() -
1571 req->rq_arrival_time.tv_sec));
1572
1573
1574 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1575 MSGHDR_AT_SUPPORT) ?
1576
1577 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1578 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1579 if (unlikely(deadline == 0)) {
1580 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1581 goto err_req;
1582 }
1583
1584 req->rq_svc_thread = thread;
1585 if (thread) {
1586
1587
1588
1589 rc = lu_context_init(&req->rq_session,
1590 LCT_SERVER_SESSION | LCT_NOREF);
1591 if (rc) {
1592 CERROR("%s: failure to initialize session: rc = %d\n",
1593 thread->t_name, rc);
1594 goto err_req;
1595 }
1596 req->rq_session.lc_thread = thread;
1597 lu_context_enter(&req->rq_session);
1598 req->rq_svc_thread->t_env->le_ses = &req->rq_session;
1599 }
1600
1601 ptlrpc_at_add_timed(req);
1602
1603
1604 rc = ptlrpc_server_request_add(svcpt, req);
1605 if (rc)
1606 goto err_req;
1607
1608 wake_up(&svcpt->scp_waitq);
1609 return 1;
1610
1611err_req:
1612 ptlrpc_server_finish_request(svcpt, req);
1613
1614 return 1;
1615}
1616
1617
1618
1619
1620
1621static int
1622ptlrpc_server_handle_request(struct ptlrpc_service_part *svcpt,
1623 struct ptlrpc_thread *thread)
1624{
1625 struct ptlrpc_service *svc = svcpt->scp_service;
1626 struct ptlrpc_request *request;
1627 struct timespec64 work_start;
1628 struct timespec64 work_end;
1629 struct timespec64 timediff;
1630 struct timespec64 arrived;
1631 unsigned long timediff_usecs;
1632 unsigned long arrived_usecs;
1633 int fail_opc = 0;
1634
1635 request = ptlrpc_server_request_get(svcpt, false);
1636 if (!request)
1637 return 0;
1638
1639 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1640 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1641 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1642 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1643
1644 if (unlikely(fail_opc)) {
1645 if (request->rq_export && request->rq_ops)
1646 OBD_FAIL_TIMEOUT(fail_opc, 4);
1647 }
1648
1649 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1650
1651 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1652 libcfs_debug_dumplog();
1653
1654 ktime_get_real_ts64(&work_start);
1655 timediff = timespec64_sub(work_start, request->rq_arrival_time);
1656 timediff_usecs = timediff.tv_sec * USEC_PER_SEC +
1657 timediff.tv_nsec / NSEC_PER_USEC;
1658 if (likely(svc->srv_stats)) {
1659 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1660 timediff_usecs);
1661 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1662 svcpt->scp_nreqs_incoming);
1663 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1664 svcpt->scp_nreqs_active);
1665 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1666 at_get(&svcpt->scp_at_estimate));
1667 }
1668
1669 if (likely(request->rq_export)) {
1670 if (unlikely(ptlrpc_check_req(request)))
1671 goto put_conn;
1672 }
1673
1674
1675
1676
1677 if (ktime_get_real_seconds() > request->rq_deadline) {
1678 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s: deadline %lld:%llds ago\n",
1679 libcfs_id2str(request->rq_peer),
1680 request->rq_deadline -
1681 request->rq_arrival_time.tv_sec,
1682 ktime_get_real_seconds() - request->rq_deadline);
1683 goto put_conn;
1684 }
1685
1686 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc %s:%s+%d:%d:x%llu:%s:%d\n",
1687 current_comm(),
1688 (request->rq_export ?
1689 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1690 (request->rq_export ?
1691 atomic_read(&request->rq_export->exp_refcount) : -99),
1692 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1693 libcfs_id2str(request->rq_peer),
1694 lustre_msg_get_opc(request->rq_reqmsg));
1695
1696 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
1697 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, cfs_fail_val);
1698
1699 CDEBUG(D_NET, "got req %llu\n", request->rq_xid);
1700
1701
1702 request->rq_svc_thread = thread;
1703 if (thread) {
1704 LASSERT(request->rq_session.lc_thread);
1705 request->rq_session.lc_thread = thread;
1706 request->rq_session.lc_cookie = 0x55;
1707 thread->t_env->le_ses = &request->rq_session;
1708 }
1709 svc->srv_ops.so_req_handler(request);
1710
1711 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1712
1713put_conn:
1714 if (unlikely(ktime_get_real_seconds() > request->rq_deadline)) {
1715 DEBUG_REQ(D_WARNING, request,
1716 "Request took longer than estimated (%lld:%llds); "
1717 "client may timeout.",
1718 (s64)request->rq_deadline -
1719 request->rq_arrival_time.tv_sec,
1720 (s64)ktime_get_real_seconds() - request->rq_deadline);
1721 }
1722
1723 ktime_get_real_ts64(&work_end);
1724 timediff = timespec64_sub(work_end, work_start);
1725 timediff_usecs = timediff.tv_sec * USEC_PER_SEC +
1726 timediff.tv_nsec / NSEC_PER_USEC;
1727 arrived = timespec64_sub(work_end, request->rq_arrival_time);
1728 arrived_usecs = arrived.tv_sec * USEC_PER_SEC +
1729 arrived.tv_nsec / NSEC_PER_USEC;
1730 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc %s:%s+%d:%d:x%llu:%s:%d Request processed in %ldus (%ldus total) trans %llu rc %d/%d\n",
1731 current_comm(),
1732 (request->rq_export ?
1733 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1734 (request->rq_export ?
1735 atomic_read(&request->rq_export->exp_refcount) : -99),
1736 lustre_msg_get_status(request->rq_reqmsg),
1737 request->rq_xid,
1738 libcfs_id2str(request->rq_peer),
1739 lustre_msg_get_opc(request->rq_reqmsg),
1740 timediff_usecs,
1741 arrived_usecs,
1742 (request->rq_repmsg ?
1743 lustre_msg_get_transno(request->rq_repmsg) :
1744 request->rq_transno),
1745 request->rq_status,
1746 (request->rq_repmsg ?
1747 lustre_msg_get_status(request->rq_repmsg) : -999));
1748 if (likely(svc->srv_stats && request->rq_reqmsg)) {
1749 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1750 int opc = opcode_offset(op);
1751
1752 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1753 LASSERT(opc < LUSTRE_MAX_OPCODES);
1754 lprocfs_counter_add(svc->srv_stats,
1755 opc + EXTRA_MAX_OPCODES,
1756 timediff_usecs);
1757 }
1758 }
1759 if (unlikely(request->rq_early_count)) {
1760 DEBUG_REQ(D_ADAPTTO, request,
1761 "sent %d early replies before finishing in %llds",
1762 request->rq_early_count,
1763 (s64)work_end.tv_sec -
1764 request->rq_arrival_time.tv_sec);
1765 }
1766
1767 ptlrpc_server_finish_active_request(svcpt, request);
1768
1769 return 1;
1770}
1771
1772
1773
1774
1775static int
1776ptlrpc_handle_rs(struct ptlrpc_reply_state *rs)
1777{
1778 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
1779 struct ptlrpc_service *svc = svcpt->scp_service;
1780 struct obd_export *exp;
1781 int nlocks;
1782 int been_handled;
1783
1784 exp = rs->rs_export;
1785
1786 LASSERT(rs->rs_difficult);
1787 LASSERT(rs->rs_scheduled);
1788 LASSERT(list_empty(&rs->rs_list));
1789
1790 spin_lock(&exp->exp_lock);
1791
1792 list_del_init(&rs->rs_exp_list);
1793 spin_unlock(&exp->exp_lock);
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817 if (!rs->rs_committed) {
1818 spin_lock(&exp->exp_uncommitted_replies_lock);
1819 list_del_init(&rs->rs_obd_list);
1820 spin_unlock(&exp->exp_uncommitted_replies_lock);
1821 }
1822
1823 spin_lock(&rs->rs_lock);
1824
1825 been_handled = rs->rs_handled;
1826 rs->rs_handled = 1;
1827
1828 nlocks = rs->rs_nlocks;
1829 rs->rs_nlocks = 0;
1830
1831 if (nlocks == 0 && !been_handled) {
1832
1833
1834
1835 CDEBUG(D_HA, "All locks stolen from rs %p x%lld.t%lld o%d NID %s\n",
1836 rs,
1837 rs->rs_xid, rs->rs_transno, rs->rs_opc,
1838 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1839 }
1840
1841 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1842 spin_unlock(&rs->rs_lock);
1843
1844 if (!been_handled && rs->rs_on_net) {
1845 LNetMDUnlink(rs->rs_md_h);
1846
1847 }
1848
1849 while (nlocks-- > 0)
1850 ldlm_lock_decref(&rs->rs_locks[nlocks],
1851 rs->rs_modes[nlocks]);
1852
1853 spin_lock(&rs->rs_lock);
1854 }
1855
1856 rs->rs_scheduled = 0;
1857
1858 if (!rs->rs_on_net) {
1859
1860 spin_unlock(&rs->rs_lock);
1861
1862 class_export_put(exp);
1863 rs->rs_export = NULL;
1864 ptlrpc_rs_decref(rs);
1865 if (atomic_dec_and_test(&svcpt->scp_nreps_difficult) &&
1866 svc->srv_is_stopping)
1867 wake_up_all(&svcpt->scp_waitq);
1868 return 1;
1869 }
1870
1871
1872 spin_unlock(&rs->rs_lock);
1873 return 1;
1874}
1875
1876static void
1877ptlrpc_check_rqbd_pool(struct ptlrpc_service_part *svcpt)
1878{
1879 int avail = svcpt->scp_nrqbds_posted;
1880 int low_water = test_req_buffer_pressure ? 0 :
1881 svcpt->scp_service->srv_nbuf_per_group / 2;
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891 if (avail <= low_water)
1892 ptlrpc_grow_req_bufs(svcpt, 1);
1893
1894 if (svcpt->scp_service->srv_stats) {
1895 lprocfs_counter_add(svcpt->scp_service->srv_stats,
1896 PTLRPC_REQBUF_AVAIL_CNTR, avail);
1897 }
1898}
1899
1900static int
1901ptlrpc_retry_rqbds(void *arg)
1902{
1903 struct ptlrpc_service_part *svcpt = arg;
1904
1905 svcpt->scp_rqbd_timeout = 0;
1906 return -ETIMEDOUT;
1907}
1908
1909static inline int
1910ptlrpc_threads_enough(struct ptlrpc_service_part *svcpt)
1911{
1912 return svcpt->scp_nreqs_active <
1913 svcpt->scp_nthrs_running - 1 -
1914 (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL);
1915}
1916
1917
1918
1919
1920
1921
1922static inline int
1923ptlrpc_threads_increasable(struct ptlrpc_service_part *svcpt)
1924{
1925 return svcpt->scp_nthrs_running +
1926 svcpt->scp_nthrs_starting <
1927 svcpt->scp_service->srv_nthrs_cpt_limit;
1928}
1929
1930
1931
1932
1933static inline int
1934ptlrpc_threads_need_create(struct ptlrpc_service_part *svcpt)
1935{
1936 return !ptlrpc_threads_enough(svcpt) &&
1937 ptlrpc_threads_increasable(svcpt);
1938}
1939
1940static inline int
1941ptlrpc_thread_stopping(struct ptlrpc_thread *thread)
1942{
1943 return thread_is_stopping(thread) ||
1944 thread->t_svcpt->scp_service->srv_is_stopping;
1945}
1946
1947static inline int
1948ptlrpc_rqbd_pending(struct ptlrpc_service_part *svcpt)
1949{
1950 return !list_empty(&svcpt->scp_rqbd_idle) &&
1951 svcpt->scp_rqbd_timeout == 0;
1952}
1953
1954static inline int
1955ptlrpc_at_check(struct ptlrpc_service_part *svcpt)
1956{
1957 return svcpt->scp_at_check;
1958}
1959
1960
1961
1962
1963
1964
1965static inline int
1966ptlrpc_server_request_incoming(struct ptlrpc_service_part *svcpt)
1967{
1968 return !list_empty(&svcpt->scp_req_incoming);
1969}
1970
1971static __attribute__((__noinline__)) int
1972ptlrpc_wait_event(struct ptlrpc_service_part *svcpt,
1973 struct ptlrpc_thread *thread)
1974{
1975
1976 struct l_wait_info lwi = LWI_TIMEOUT(svcpt->scp_rqbd_timeout,
1977 ptlrpc_retry_rqbds, svcpt);
1978
1979
1980
1981
1982
1983 cond_resched();
1984
1985 l_wait_event_exclusive_head(svcpt->scp_waitq,
1986 ptlrpc_thread_stopping(thread) ||
1987 ptlrpc_server_request_incoming(svcpt) ||
1988 ptlrpc_server_request_pending(svcpt,
1989 false) ||
1990 ptlrpc_rqbd_pending(svcpt) ||
1991 ptlrpc_at_check(svcpt), &lwi);
1992
1993 if (ptlrpc_thread_stopping(thread))
1994 return -EINTR;
1995
1996
1997
1998
1999
2000 return 0;
2001}
2002
2003
2004
2005
2006
2007
2008
2009static int ptlrpc_main(void *arg)
2010{
2011 struct ptlrpc_thread *thread = arg;
2012 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2013 struct ptlrpc_service *svc = svcpt->scp_service;
2014 struct ptlrpc_reply_state *rs;
2015 struct group_info *ginfo = NULL;
2016 struct lu_env *env;
2017 int counter = 0, rc = 0;
2018
2019 thread->t_pid = current_pid();
2020 unshare_fs_struct();
2021
2022
2023
2024
2025
2026 rc = cfs_cpt_bind(svc->srv_cptable, svcpt->scp_cpt);
2027 if (rc != 0) {
2028 CWARN("%s: failed to bind %s on CPT %d\n",
2029 svc->srv_name, thread->t_name, svcpt->scp_cpt);
2030 }
2031
2032 ginfo = groups_alloc(0);
2033 if (!ginfo) {
2034 rc = -ENOMEM;
2035 goto out;
2036 }
2037
2038 set_current_groups(ginfo);
2039 put_group_info(ginfo);
2040
2041 if (svc->srv_ops.so_thr_init) {
2042 rc = svc->srv_ops.so_thr_init(thread);
2043 if (rc)
2044 goto out;
2045 }
2046
2047 env = kzalloc(sizeof(*env), GFP_NOFS);
2048 if (!env) {
2049 rc = -ENOMEM;
2050 goto out_srv_fini;
2051 }
2052
2053 rc = lu_context_init(&env->le_ctx,
2054 svc->srv_ctx_tags | LCT_REMEMBER | LCT_NOREF);
2055 if (rc)
2056 goto out_srv_fini;
2057
2058 thread->t_env = env;
2059 env->le_ctx.lc_thread = thread;
2060 env->le_ctx.lc_cookie = 0x6;
2061
2062 while (!list_empty(&svcpt->scp_rqbd_idle)) {
2063 rc = ptlrpc_server_post_idle_rqbds(svcpt);
2064 if (rc >= 0)
2065 continue;
2066
2067 CERROR("Failed to post rqbd for %s on CPT %d: %d\n",
2068 svc->srv_name, svcpt->scp_cpt, rc);
2069 goto out_srv_fini;
2070 }
2071
2072
2073 rs = libcfs_kvzalloc(svc->srv_max_reply_size, GFP_NOFS);
2074 if (!rs) {
2075 rc = -ENOMEM;
2076 goto out_srv_fini;
2077 }
2078
2079 spin_lock(&svcpt->scp_lock);
2080
2081 LASSERT(thread_is_starting(thread));
2082 thread_clear_flags(thread, SVC_STARTING);
2083
2084 LASSERT(svcpt->scp_nthrs_starting == 1);
2085 svcpt->scp_nthrs_starting--;
2086
2087
2088
2089
2090
2091
2092 thread_add_flags(thread, SVC_RUNNING);
2093 svcpt->scp_nthrs_running++;
2094 spin_unlock(&svcpt->scp_lock);
2095
2096
2097 wake_up(&thread->t_ctl_waitq);
2098
2099
2100
2101
2102
2103
2104 spin_lock(&svcpt->scp_rep_lock);
2105 list_add(&rs->rs_list, &svcpt->scp_rep_idle);
2106 wake_up(&svcpt->scp_rep_waitq);
2107 spin_unlock(&svcpt->scp_rep_lock);
2108
2109 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
2110 svcpt->scp_nthrs_running);
2111
2112
2113 while (!ptlrpc_thread_stopping(thread)) {
2114 if (ptlrpc_wait_event(svcpt, thread))
2115 break;
2116
2117 ptlrpc_check_rqbd_pool(svcpt);
2118
2119 if (ptlrpc_threads_need_create(svcpt)) {
2120
2121 ptlrpc_start_thread(svcpt, 0);
2122 }
2123
2124
2125 if (ptlrpc_server_request_incoming(svcpt)) {
2126 lu_context_enter(&env->le_ctx);
2127 env->le_ses = NULL;
2128 ptlrpc_server_handle_req_in(svcpt, thread);
2129 lu_context_exit(&env->le_ctx);
2130
2131
2132 if (counter++ < 100)
2133 continue;
2134 counter = 0;
2135 }
2136
2137 if (ptlrpc_at_check(svcpt))
2138 ptlrpc_at_check_timed(svcpt);
2139
2140 if (ptlrpc_server_request_pending(svcpt, false)) {
2141 lu_context_enter(&env->le_ctx);
2142 ptlrpc_server_handle_request(svcpt, thread);
2143 lu_context_exit(&env->le_ctx);
2144 }
2145
2146 if (ptlrpc_rqbd_pending(svcpt) &&
2147 ptlrpc_server_post_idle_rqbds(svcpt) < 0) {
2148
2149
2150
2151
2152 svcpt->scp_rqbd_timeout = cfs_time_seconds(1) / 10;
2153 CDEBUG(D_RPCTRACE, "Posted buffers: %d\n",
2154 svcpt->scp_nrqbds_posted);
2155 }
2156 }
2157
2158
2159
2160
2161
2162
2163out_srv_fini:
2164
2165
2166
2167 if (svc->srv_ops.so_thr_done)
2168 svc->srv_ops.so_thr_done(thread);
2169
2170 if (env) {
2171 lu_context_fini(&env->le_ctx);
2172 kfree(env);
2173 }
2174out:
2175 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2176 thread, thread->t_pid, thread->t_id, rc);
2177
2178 spin_lock(&svcpt->scp_lock);
2179 if (thread_test_and_clear_flags(thread, SVC_STARTING))
2180 svcpt->scp_nthrs_starting--;
2181
2182 if (thread_test_and_clear_flags(thread, SVC_RUNNING)) {
2183
2184 svcpt->scp_nthrs_running--;
2185 }
2186
2187 thread->t_id = rc;
2188 thread_add_flags(thread, SVC_STOPPED);
2189
2190 wake_up(&thread->t_ctl_waitq);
2191 spin_unlock(&svcpt->scp_lock);
2192
2193 return rc;
2194}
2195
2196static int hrt_dont_sleep(struct ptlrpc_hr_thread *hrt,
2197 struct list_head *replies)
2198{
2199 int result;
2200
2201 spin_lock(&hrt->hrt_lock);
2202
2203 list_splice_init(&hrt->hrt_queue, replies);
2204 result = ptlrpc_hr.hr_stopping || !list_empty(replies);
2205
2206 spin_unlock(&hrt->hrt_lock);
2207 return result;
2208}
2209
2210
2211
2212
2213
2214static int ptlrpc_hr_main(void *arg)
2215{
2216 struct ptlrpc_hr_thread *hrt = arg;
2217 struct ptlrpc_hr_partition *hrp = hrt->hrt_partition;
2218 LIST_HEAD(replies);
2219 char threadname[20];
2220 int rc;
2221
2222 snprintf(threadname, sizeof(threadname), "ptlrpc_hr%02d_%03d",
2223 hrp->hrp_cpt, hrt->hrt_id);
2224 unshare_fs_struct();
2225
2226 rc = cfs_cpt_bind(ptlrpc_hr.hr_cpt_table, hrp->hrp_cpt);
2227 if (rc != 0) {
2228 CWARN("Failed to bind %s on CPT %d of CPT table %p: rc = %d\n",
2229 threadname, hrp->hrp_cpt, ptlrpc_hr.hr_cpt_table, rc);
2230 }
2231
2232 atomic_inc(&hrp->hrp_nstarted);
2233 wake_up(&ptlrpc_hr.hr_waitq);
2234
2235 while (!ptlrpc_hr.hr_stopping) {
2236 l_wait_condition(hrt->hrt_waitq, hrt_dont_sleep(hrt, &replies));
2237
2238 while (!list_empty(&replies)) {
2239 struct ptlrpc_reply_state *rs;
2240
2241 rs = list_entry(replies.prev, struct ptlrpc_reply_state,
2242 rs_list);
2243 list_del_init(&rs->rs_list);
2244 ptlrpc_handle_rs(rs);
2245 }
2246 }
2247
2248 atomic_inc(&hrp->hrp_nstopped);
2249 wake_up(&ptlrpc_hr.hr_waitq);
2250
2251 return 0;
2252}
2253
2254static void ptlrpc_stop_hr_threads(void)
2255{
2256 struct ptlrpc_hr_partition *hrp;
2257 int i;
2258 int j;
2259
2260 ptlrpc_hr.hr_stopping = 1;
2261
2262 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2263 if (!hrp->hrp_thrs)
2264 continue;
2265 for (j = 0; j < hrp->hrp_nthrs; j++)
2266 wake_up_all(&hrp->hrp_thrs[j].hrt_waitq);
2267 }
2268
2269 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2270 if (!hrp->hrp_thrs)
2271 continue;
2272 wait_event(ptlrpc_hr.hr_waitq,
2273 atomic_read(&hrp->hrp_nstopped) ==
2274 atomic_read(&hrp->hrp_nstarted));
2275 }
2276}
2277
2278static int ptlrpc_start_hr_threads(void)
2279{
2280 struct ptlrpc_hr_partition *hrp;
2281 int i;
2282 int j;
2283
2284 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2285 int rc = 0;
2286
2287 for (j = 0; j < hrp->hrp_nthrs; j++) {
2288 struct ptlrpc_hr_thread *hrt = &hrp->hrp_thrs[j];
2289 struct task_struct *task;
2290
2291 task = kthread_run(ptlrpc_hr_main,
2292 &hrp->hrp_thrs[j],
2293 "ptlrpc_hr%02d_%03d",
2294 hrp->hrp_cpt, hrt->hrt_id);
2295 if (IS_ERR(task)) {
2296 rc = PTR_ERR(task);
2297 break;
2298 }
2299 }
2300 wait_event(ptlrpc_hr.hr_waitq,
2301 atomic_read(&hrp->hrp_nstarted) == j);
2302
2303 if (rc < 0) {
2304 CERROR("cannot start reply handler thread %d:%d: rc = %d\n",
2305 i, j, rc);
2306 ptlrpc_stop_hr_threads();
2307 return rc;
2308 }
2309 }
2310 return 0;
2311}
2312
2313static void ptlrpc_svcpt_stop_threads(struct ptlrpc_service_part *svcpt)
2314{
2315 struct l_wait_info lwi = { 0 };
2316 struct ptlrpc_thread *thread;
2317 LIST_HEAD(zombie);
2318
2319 CDEBUG(D_INFO, "Stopping threads for service %s\n",
2320 svcpt->scp_service->srv_name);
2321
2322 spin_lock(&svcpt->scp_lock);
2323
2324 list_for_each_entry(thread, &svcpt->scp_threads, t_link) {
2325 CDEBUG(D_INFO, "Stopping thread %s #%u\n",
2326 svcpt->scp_service->srv_thread_name, thread->t_id);
2327 thread_add_flags(thread, SVC_STOPPING);
2328 }
2329
2330 wake_up_all(&svcpt->scp_waitq);
2331
2332 while (!list_empty(&svcpt->scp_threads)) {
2333 thread = list_entry(svcpt->scp_threads.next,
2334 struct ptlrpc_thread, t_link);
2335 if (thread_is_stopped(thread)) {
2336 list_del(&thread->t_link);
2337 list_add(&thread->t_link, &zombie);
2338 continue;
2339 }
2340 spin_unlock(&svcpt->scp_lock);
2341
2342 CDEBUG(D_INFO, "waiting for stopping-thread %s #%u\n",
2343 svcpt->scp_service->srv_thread_name, thread->t_id);
2344 l_wait_event(thread->t_ctl_waitq,
2345 thread_is_stopped(thread), &lwi);
2346
2347 spin_lock(&svcpt->scp_lock);
2348 }
2349
2350 spin_unlock(&svcpt->scp_lock);
2351
2352 while (!list_empty(&zombie)) {
2353 thread = list_entry(zombie.next,
2354 struct ptlrpc_thread, t_link);
2355 list_del(&thread->t_link);
2356 kfree(thread);
2357 }
2358}
2359
2360
2361
2362
2363static void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2364{
2365 struct ptlrpc_service_part *svcpt;
2366 int i;
2367
2368 ptlrpc_service_for_each_part(svcpt, i, svc) {
2369 if (svcpt->scp_service)
2370 ptlrpc_svcpt_stop_threads(svcpt);
2371 }
2372}
2373
2374int ptlrpc_start_threads(struct ptlrpc_service *svc)
2375{
2376 int rc = 0;
2377 int i;
2378 int j;
2379
2380
2381 LASSERT(svc->srv_nthrs_cpt_init >= PTLRPC_NTHRS_INIT);
2382
2383 for (i = 0; i < svc->srv_ncpts; i++) {
2384 for (j = 0; j < svc->srv_nthrs_cpt_init; j++) {
2385 rc = ptlrpc_start_thread(svc->srv_parts[i], 1);
2386 if (rc == 0)
2387 continue;
2388
2389 if (rc != -EMFILE)
2390 goto failed;
2391
2392 break;
2393 }
2394 }
2395
2396 return 0;
2397 failed:
2398 CERROR("cannot start %s thread #%d_%d: rc %d\n",
2399 svc->srv_thread_name, i, j, rc);
2400 ptlrpc_stop_all_threads(svc);
2401 return rc;
2402}
2403
2404int ptlrpc_start_thread(struct ptlrpc_service_part *svcpt, int wait)
2405{
2406 struct l_wait_info lwi = { 0 };
2407 struct ptlrpc_thread *thread;
2408 struct ptlrpc_service *svc;
2409 struct task_struct *task;
2410 int rc;
2411
2412 svc = svcpt->scp_service;
2413
2414 CDEBUG(D_RPCTRACE, "%s[%d] started %d min %d max %d\n",
2415 svc->srv_name, svcpt->scp_cpt, svcpt->scp_nthrs_running,
2416 svc->srv_nthrs_cpt_init, svc->srv_nthrs_cpt_limit);
2417
2418 again:
2419 if (unlikely(svc->srv_is_stopping))
2420 return -ESRCH;
2421
2422 if (!ptlrpc_threads_increasable(svcpt) ||
2423 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2424 svcpt->scp_nthrs_running == svc->srv_nthrs_cpt_init - 1))
2425 return -EMFILE;
2426
2427 thread = kzalloc_node(sizeof(*thread), GFP_NOFS,
2428 cfs_cpt_spread_node(svc->srv_cptable,
2429 svcpt->scp_cpt));
2430 if (!thread)
2431 return -ENOMEM;
2432 init_waitqueue_head(&thread->t_ctl_waitq);
2433
2434 spin_lock(&svcpt->scp_lock);
2435 if (!ptlrpc_threads_increasable(svcpt)) {
2436 spin_unlock(&svcpt->scp_lock);
2437 kfree(thread);
2438 return -EMFILE;
2439 }
2440
2441 if (svcpt->scp_nthrs_starting != 0) {
2442
2443
2444
2445 LASSERT(svcpt->scp_nthrs_starting == 1);
2446 spin_unlock(&svcpt->scp_lock);
2447 kfree(thread);
2448 if (wait) {
2449 CDEBUG(D_INFO, "Waiting for creating thread %s #%d\n",
2450 svc->srv_thread_name, svcpt->scp_thr_nextid);
2451 schedule();
2452 goto again;
2453 }
2454
2455 CDEBUG(D_INFO, "Creating thread %s #%d race, retry later\n",
2456 svc->srv_thread_name, svcpt->scp_thr_nextid);
2457 return -EAGAIN;
2458 }
2459
2460 svcpt->scp_nthrs_starting++;
2461 thread->t_id = svcpt->scp_thr_nextid++;
2462 thread_add_flags(thread, SVC_STARTING);
2463 thread->t_svcpt = svcpt;
2464
2465 list_add(&thread->t_link, &svcpt->scp_threads);
2466 spin_unlock(&svcpt->scp_lock);
2467
2468 if (svcpt->scp_cpt >= 0) {
2469 snprintf(thread->t_name, sizeof(thread->t_name), "%s%02d_%03d",
2470 svc->srv_thread_name, svcpt->scp_cpt, thread->t_id);
2471 } else {
2472 snprintf(thread->t_name, sizeof(thread->t_name), "%s_%04d",
2473 svc->srv_thread_name, thread->t_id);
2474 }
2475
2476 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", thread->t_name);
2477 task = kthread_run(ptlrpc_main, thread, "%s", thread->t_name);
2478 if (IS_ERR(task)) {
2479 rc = PTR_ERR(task);
2480 CERROR("cannot start thread '%s': rc = %d\n",
2481 thread->t_name, rc);
2482 spin_lock(&svcpt->scp_lock);
2483 --svcpt->scp_nthrs_starting;
2484 if (thread_is_stopping(thread)) {
2485
2486
2487
2488 thread_add_flags(thread, SVC_STOPPED);
2489 wake_up(&thread->t_ctl_waitq);
2490 spin_unlock(&svcpt->scp_lock);
2491 } else {
2492 list_del(&thread->t_link);
2493 spin_unlock(&svcpt->scp_lock);
2494 kfree(thread);
2495 }
2496 return rc;
2497 }
2498
2499 if (!wait)
2500 return 0;
2501
2502 l_wait_event(thread->t_ctl_waitq,
2503 thread_is_running(thread) || thread_is_stopped(thread),
2504 &lwi);
2505
2506 rc = thread_is_stopped(thread) ? thread->t_id : 0;
2507 return rc;
2508}
2509
2510int ptlrpc_hr_init(void)
2511{
2512 struct ptlrpc_hr_partition *hrp;
2513 struct ptlrpc_hr_thread *hrt;
2514 int rc;
2515 int i;
2516 int j;
2517 int weight;
2518
2519 memset(&ptlrpc_hr, 0, sizeof(ptlrpc_hr));
2520 ptlrpc_hr.hr_cpt_table = cfs_cpt_table;
2521
2522 ptlrpc_hr.hr_partitions = cfs_percpt_alloc(ptlrpc_hr.hr_cpt_table,
2523 sizeof(*hrp));
2524 if (!ptlrpc_hr.hr_partitions)
2525 return -ENOMEM;
2526
2527 init_waitqueue_head(&ptlrpc_hr.hr_waitq);
2528
2529 weight = cpumask_weight(topology_sibling_cpumask(0));
2530
2531 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2532 hrp->hrp_cpt = i;
2533
2534 atomic_set(&hrp->hrp_nstarted, 0);
2535 atomic_set(&hrp->hrp_nstopped, 0);
2536
2537 hrp->hrp_nthrs = cfs_cpt_weight(ptlrpc_hr.hr_cpt_table, i);
2538 hrp->hrp_nthrs /= weight;
2539 if (hrp->hrp_nthrs == 0)
2540 hrp->hrp_nthrs = 1;
2541
2542 hrp->hrp_thrs =
2543 kzalloc_node(hrp->hrp_nthrs * sizeof(*hrt), GFP_NOFS,
2544 cfs_cpt_spread_node(ptlrpc_hr.hr_cpt_table,
2545 i));
2546 if (!hrp->hrp_thrs) {
2547 rc = -ENOMEM;
2548 goto out;
2549 }
2550
2551 for (j = 0; j < hrp->hrp_nthrs; j++) {
2552 hrt = &hrp->hrp_thrs[j];
2553
2554 hrt->hrt_id = j;
2555 hrt->hrt_partition = hrp;
2556 init_waitqueue_head(&hrt->hrt_waitq);
2557 spin_lock_init(&hrt->hrt_lock);
2558 INIT_LIST_HEAD(&hrt->hrt_queue);
2559 }
2560 }
2561
2562 rc = ptlrpc_start_hr_threads();
2563out:
2564 if (rc != 0)
2565 ptlrpc_hr_fini();
2566 return rc;
2567}
2568
2569void ptlrpc_hr_fini(void)
2570{
2571 struct ptlrpc_hr_partition *hrp;
2572 int i;
2573
2574 if (!ptlrpc_hr.hr_partitions)
2575 return;
2576
2577 ptlrpc_stop_hr_threads();
2578
2579 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2580 kfree(hrp->hrp_thrs);
2581 }
2582
2583 cfs_percpt_free(ptlrpc_hr.hr_partitions);
2584 ptlrpc_hr.hr_partitions = NULL;
2585}
2586
2587
2588
2589
2590static void ptlrpc_wait_replies(struct ptlrpc_service_part *svcpt)
2591{
2592 while (1) {
2593 int rc;
2594 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2595 NULL, NULL);
2596
2597 rc = l_wait_event(svcpt->scp_waitq,
2598 atomic_read(&svcpt->scp_nreps_difficult) == 0,
2599 &lwi);
2600 if (rc == 0)
2601 break;
2602 CWARN("Unexpectedly long timeout %s %p\n",
2603 svcpt->scp_service->srv_name, svcpt->scp_service);
2604 }
2605}
2606
2607static void
2608ptlrpc_service_del_atimer(struct ptlrpc_service *svc)
2609{
2610 struct ptlrpc_service_part *svcpt;
2611 int i;
2612
2613
2614 ptlrpc_service_for_each_part(svcpt, i, svc) {
2615 if (svcpt->scp_service)
2616 del_timer(&svcpt->scp_at_timer);
2617 }
2618}
2619
2620static void
2621ptlrpc_service_unlink_rqbd(struct ptlrpc_service *svc)
2622{
2623 struct ptlrpc_service_part *svcpt;
2624 struct ptlrpc_request_buffer_desc *rqbd;
2625 struct l_wait_info lwi;
2626 int rc;
2627 int i;
2628
2629
2630
2631
2632 svc->srv_hist_nrqbds_cpt_max = 0;
2633
2634 rc = LNetClearLazyPortal(svc->srv_req_portal);
2635 LASSERT(rc == 0);
2636
2637 ptlrpc_service_for_each_part(svcpt, i, svc) {
2638 if (!svcpt->scp_service)
2639 break;
2640
2641
2642
2643
2644 list_for_each_entry(rqbd, &svcpt->scp_rqbd_posted,
2645 rqbd_list) {
2646 rc = LNetMDUnlink(rqbd->rqbd_md_h);
2647 LASSERT(rc == 0 || rc == -ENOENT);
2648 }
2649 }
2650
2651 ptlrpc_service_for_each_part(svcpt, i, svc) {
2652 if (!svcpt->scp_service)
2653 break;
2654
2655
2656
2657
2658 spin_lock(&svcpt->scp_lock);
2659 while (svcpt->scp_nrqbds_posted != 0) {
2660 spin_unlock(&svcpt->scp_lock);
2661
2662
2663
2664
2665 lwi = LWI_TIMEOUT_INTERVAL(
2666 cfs_time_seconds(LONG_UNLINK),
2667 cfs_time_seconds(1), NULL, NULL);
2668 rc = l_wait_event(svcpt->scp_waitq,
2669 svcpt->scp_nrqbds_posted == 0, &lwi);
2670 if (rc == -ETIMEDOUT) {
2671 CWARN("Service %s waiting for request buffers\n",
2672 svcpt->scp_service->srv_name);
2673 }
2674 spin_lock(&svcpt->scp_lock);
2675 }
2676 spin_unlock(&svcpt->scp_lock);
2677 }
2678}
2679
2680static void
2681ptlrpc_service_purge_all(struct ptlrpc_service *svc)
2682{
2683 struct ptlrpc_service_part *svcpt;
2684 struct ptlrpc_request_buffer_desc *rqbd;
2685 struct ptlrpc_request *req;
2686 struct ptlrpc_reply_state *rs;
2687 int i;
2688
2689 ptlrpc_service_for_each_part(svcpt, i, svc) {
2690 if (!svcpt->scp_service)
2691 break;
2692
2693 spin_lock(&svcpt->scp_rep_lock);
2694 while (!list_empty(&svcpt->scp_rep_active)) {
2695 rs = list_entry(svcpt->scp_rep_active.next,
2696 struct ptlrpc_reply_state, rs_list);
2697 spin_lock(&rs->rs_lock);
2698 ptlrpc_schedule_difficult_reply(rs);
2699 spin_unlock(&rs->rs_lock);
2700 }
2701 spin_unlock(&svcpt->scp_rep_lock);
2702
2703
2704
2705
2706
2707 while (!list_empty(&svcpt->scp_req_incoming)) {
2708 req = list_entry(svcpt->scp_req_incoming.next,
2709 struct ptlrpc_request, rq_list);
2710
2711 list_del(&req->rq_list);
2712 svcpt->scp_nreqs_incoming--;
2713 ptlrpc_server_finish_request(svcpt, req);
2714 }
2715
2716 while (ptlrpc_server_request_pending(svcpt, true)) {
2717 req = ptlrpc_server_request_get(svcpt, true);
2718 ptlrpc_server_finish_active_request(svcpt, req);
2719 }
2720
2721 LASSERT(list_empty(&svcpt->scp_rqbd_posted));
2722 LASSERT(svcpt->scp_nreqs_incoming == 0);
2723 LASSERT(svcpt->scp_nreqs_active == 0);
2724
2725
2726
2727 LASSERT(svcpt->scp_hist_nrqbds == 0);
2728
2729
2730
2731
2732
2733 while (!list_empty(&svcpt->scp_rqbd_idle)) {
2734 rqbd = list_entry(svcpt->scp_rqbd_idle.next,
2735 struct ptlrpc_request_buffer_desc,
2736 rqbd_list);
2737 ptlrpc_free_rqbd(rqbd);
2738 }
2739 ptlrpc_wait_replies(svcpt);
2740
2741 while (!list_empty(&svcpt->scp_rep_idle)) {
2742 rs = list_entry(svcpt->scp_rep_idle.next,
2743 struct ptlrpc_reply_state,
2744 rs_list);
2745 list_del(&rs->rs_list);
2746 kvfree(rs);
2747 }
2748 }
2749}
2750
2751static void
2752ptlrpc_service_free(struct ptlrpc_service *svc)
2753{
2754 struct ptlrpc_service_part *svcpt;
2755 struct ptlrpc_at_array *array;
2756 int i;
2757
2758 ptlrpc_service_for_each_part(svcpt, i, svc) {
2759 if (!svcpt->scp_service)
2760 break;
2761
2762
2763 del_timer(&svcpt->scp_at_timer);
2764 array = &svcpt->scp_at_array;
2765
2766 kfree(array->paa_reqs_array);
2767 array->paa_reqs_array = NULL;
2768 kfree(array->paa_reqs_count);
2769 array->paa_reqs_count = NULL;
2770 }
2771
2772 ptlrpc_service_for_each_part(svcpt, i, svc)
2773 kfree(svcpt);
2774
2775 if (svc->srv_cpts)
2776 cfs_expr_list_values_free(svc->srv_cpts, svc->srv_ncpts);
2777
2778 kfree(svc);
2779}
2780
2781int ptlrpc_unregister_service(struct ptlrpc_service *service)
2782{
2783 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
2784
2785 service->srv_is_stopping = 1;
2786
2787 mutex_lock(&ptlrpc_all_services_mutex);
2788 list_del_init(&service->srv_list);
2789 mutex_unlock(&ptlrpc_all_services_mutex);
2790
2791 ptlrpc_service_del_atimer(service);
2792 ptlrpc_stop_all_threads(service);
2793
2794 ptlrpc_service_unlink_rqbd(service);
2795 ptlrpc_service_purge_all(service);
2796 ptlrpc_service_nrs_cleanup(service);
2797
2798 ptlrpc_lprocfs_unregister_service(service);
2799 ptlrpc_sysfs_unregister_service(service);
2800
2801 ptlrpc_service_free(service);
2802
2803 return 0;
2804}
2805EXPORT_SYMBOL(ptlrpc_unregister_service);
2806