4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
32 #define DEBUG_SUBSYSTEM S_RPC
34 #include <linux/fs_struct.h>
35 #include <linux/kthread.h>
36 #include <linux/ratelimit.h>
38 #include <obd_support.h>
39 #include <obd_class.h>
40 #include <lustre_net.h>
41 #include <lu_object.h>
42 #include <uapi/linux/lnet/lnet-types.h>
43 #include "ptlrpc_internal.h"
44 #include <linux/delay.h>
46 /* The following are visible and mutable through /sys/module/ptlrpc */
47 int test_req_buffer_pressure = 0;
48 module_param(test_req_buffer_pressure, int, 0444);
49 MODULE_PARM_DESC(test_req_buffer_pressure, "set non-zero to put pressure on request buffer pools");
50 module_param(at_min, int, 0644);
51 MODULE_PARM_DESC(at_min, "Adaptive timeout minimum (sec)");
52 module_param(at_max, int, 0644);
53 MODULE_PARM_DESC(at_max, "Adaptive timeout maximum (sec)");
54 module_param(at_history, int, 0644);
55 MODULE_PARM_DESC(at_history,
56 "Adaptive timeouts remember the slowest event that took place within this period (sec)");
57 module_param(at_early_margin, int, 0644);
58 MODULE_PARM_DESC(at_early_margin, "How soon before an RPC deadline to send an early reply");
59 module_param(at_extra, int, 0644);
60 MODULE_PARM_DESC(at_extra, "How much extra time to give with each early reply");
63 static int ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt);
64 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req);
65 static void ptlrpc_at_remove_timed(struct ptlrpc_request *req);
66 static int ptlrpc_start_threads(struct ptlrpc_service *svc);
67 static int ptlrpc_start_thread(struct ptlrpc_service_part *svcpt, int wait);
69 /** Holds a list of all PTLRPC services */
70 LIST_HEAD(ptlrpc_all_services);
71 /** Used to protect the \e ptlrpc_all_services list */
72 struct mutex ptlrpc_all_services_mutex;
74 static struct ptlrpc_request_buffer_desc *
75 ptlrpc_alloc_rqbd(struct ptlrpc_service_part *svcpt)
77 struct ptlrpc_service *svc = svcpt->scp_service;
78 struct ptlrpc_request_buffer_desc *rqbd;
80 OBD_CPT_ALLOC_PTR(rqbd, svc->srv_cptable, svcpt->scp_cpt);
84 rqbd->rqbd_svcpt = svcpt;
85 rqbd->rqbd_refcount = 0;
86 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
87 rqbd->rqbd_cbid.cbid_arg = rqbd;
88 INIT_LIST_HEAD(&rqbd->rqbd_reqs);
89 OBD_CPT_ALLOC_LARGE(rqbd->rqbd_buffer, svc->srv_cptable,
90 svcpt->scp_cpt, svc->srv_buf_size);
91 if (rqbd->rqbd_buffer == NULL) {
96 spin_lock(&svcpt->scp_lock);
97 list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
98 svcpt->scp_nrqbds_total++;
99 spin_unlock(&svcpt->scp_lock);
104 static void ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
106 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
108 LASSERT(rqbd->rqbd_refcount == 0);
109 LASSERT(list_empty(&rqbd->rqbd_reqs));
111 OBD_FREE_LARGE(rqbd->rqbd_buffer, svcpt->scp_service->srv_buf_size);
115 static int ptlrpc_grow_req_bufs(struct ptlrpc_service_part *svcpt, int post)
117 struct ptlrpc_service *svc = svcpt->scp_service;
118 struct ptlrpc_request_buffer_desc *rqbd;
122 if (svcpt->scp_rqbd_allocating)
125 spin_lock(&svcpt->scp_lock);
126 /* check again with lock */
127 if (svcpt->scp_rqbd_allocating) {
128 /* NB: we might allow more than one thread in the future */
129 LASSERT(svcpt->scp_rqbd_allocating == 1);
130 spin_unlock(&svcpt->scp_lock);
134 svcpt->scp_rqbd_allocating++;
135 spin_unlock(&svcpt->scp_lock);
138 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
140 * NB: another thread might have recycled enough rqbds, we
141 * need to make sure it wouldn't over-allocate, see LU-1212.
143 if (svcpt->scp_nrqbds_posted >= svc->srv_nbuf_per_group ||
144 (svc->srv_nrqbds_max != 0 &&
145 svcpt->scp_nrqbds_total > svc->srv_nrqbds_max))
148 rqbd = ptlrpc_alloc_rqbd(svcpt);
151 CERROR("%s: Can't allocate request buffer\n",
158 spin_lock(&svcpt->scp_lock);
160 LASSERT(svcpt->scp_rqbd_allocating == 1);
161 svcpt->scp_rqbd_allocating--;
163 spin_unlock(&svcpt->scp_lock);
166 "%s: allocate %d new %d-byte reqbufs (%d/%d left), rc = %d\n",
167 svc->srv_name, i, svc->srv_buf_size, svcpt->scp_nrqbds_posted,
168 svcpt->scp_nrqbds_total, rc);
172 rc = ptlrpc_server_post_idle_rqbds(svcpt);
178 * Part of Rep-Ack logic.
179 * Puts a lock and its mode into reply state assotiated to request reply.
181 void ptlrpc_save_lock(struct ptlrpc_request *req, struct lustre_handle *lock,
184 struct ptlrpc_reply_state *rs = req->rq_reply_state;
188 CDEBUG(D_RPCTRACE, "nlocks %d\n", rs->rs_nlocks);
189 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
191 idx = rs->rs_nlocks++;
192 rs->rs_locks[idx] = *lock;
193 rs->rs_difficult = 1;
194 rs->rs_no_ack = no_ack;
196 EXPORT_SYMBOL(ptlrpc_save_lock);
199 struct ptlrpc_hr_partition;
201 struct ptlrpc_hr_thread {
202 int hrt_id; /* thread ID */
204 wait_queue_head_t hrt_waitq;
205 struct list_head hrt_queue;
206 struct ptlrpc_hr_partition *hrt_partition;
209 struct ptlrpc_hr_partition {
210 /* # of started threads */
211 atomic_t hrp_nstarted;
212 /* # of stopped threads */
213 atomic_t hrp_nstopped;
214 /* cpu partition id */
216 /* round-robin rotor for choosing thread */
218 /* total number of threads on this partition */
221 struct ptlrpc_hr_thread *hrp_thrs;
224 #define HRT_RUNNING 0
225 #define HRT_STOPPING 1
227 struct ptlrpc_hr_service {
228 /* CPU partition table, it's just cfs_cpt_tab for now */
229 struct cfs_cpt_table *hr_cpt_table;
230 /** controller sleep waitq */
231 wait_queue_head_t hr_waitq;
232 unsigned int hr_stopping;
233 /** roundrobin rotor for non-affinity service */
234 unsigned int hr_rotor;
236 struct ptlrpc_hr_partition **hr_partitions;
240 struct list_head rsb_replies;
241 unsigned int rsb_n_replies;
242 struct ptlrpc_service_part *rsb_svcpt;
245 /** reply handling service. */
246 static struct ptlrpc_hr_service ptlrpc_hr;
249 * maximum mumber of replies scheduled in one batch
251 #define MAX_SCHEDULED 256
254 * Initialize a reply batch.
258 static void rs_batch_init(struct rs_batch *b)
260 memset(b, 0, sizeof(*b));
261 INIT_LIST_HEAD(&b->rsb_replies);
265 * Choose an hr thread to dispatch requests to.
268 struct ptlrpc_hr_thread *ptlrpc_hr_select(struct ptlrpc_service_part *svcpt)
270 struct ptlrpc_hr_partition *hrp;
273 if (svcpt->scp_cpt >= 0 &&
274 svcpt->scp_service->srv_cptable == ptlrpc_hr.hr_cpt_table) {
275 /* directly match partition */
276 hrp = ptlrpc_hr.hr_partitions[svcpt->scp_cpt];
279 rotor = ptlrpc_hr.hr_rotor++;
280 rotor %= cfs_cpt_number(ptlrpc_hr.hr_cpt_table);
282 hrp = ptlrpc_hr.hr_partitions[rotor];
285 rotor = hrp->hrp_rotor++;
286 return &hrp->hrp_thrs[rotor % hrp->hrp_nthrs];
290 * Dispatch all replies accumulated in the batch to one from
291 * dedicated reply handling threads.
295 static void rs_batch_dispatch(struct rs_batch *b)
297 if (b->rsb_n_replies != 0) {
298 struct ptlrpc_hr_thread *hrt;
300 hrt = ptlrpc_hr_select(b->rsb_svcpt);
302 spin_lock(&hrt->hrt_lock);
303 list_splice_init(&b->rsb_replies, &hrt->hrt_queue);
304 spin_unlock(&hrt->hrt_lock);
306 wake_up(&hrt->hrt_waitq);
307 b->rsb_n_replies = 0;
312 * Add a reply to a batch.
313 * Add one reply object to a batch, schedule batched replies if overload.
318 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
320 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
322 if (svcpt != b->rsb_svcpt || b->rsb_n_replies >= MAX_SCHEDULED) {
323 if (b->rsb_svcpt != NULL) {
324 rs_batch_dispatch(b);
325 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
327 spin_lock(&svcpt->scp_rep_lock);
328 b->rsb_svcpt = svcpt;
330 spin_lock(&rs->rs_lock);
331 rs->rs_scheduled_ever = 1;
332 if (rs->rs_scheduled == 0) {
333 list_move(&rs->rs_list, &b->rsb_replies);
334 rs->rs_scheduled = 1;
337 rs->rs_committed = 1;
338 spin_unlock(&rs->rs_lock);
342 * Reply batch finalization.
343 * Dispatch remaining replies from the batch
344 * and release remaining spinlock.
348 static void rs_batch_fini(struct rs_batch *b)
350 if (b->rsb_svcpt != NULL) {
351 rs_batch_dispatch(b);
352 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
356 #define DECLARE_RS_BATCH(b) struct rs_batch b
360 * Put reply state into a queue for processing because we received
361 * ACK from the client
363 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
365 struct ptlrpc_hr_thread *hrt;
369 LASSERT(list_empty(&rs->rs_list));
371 hrt = ptlrpc_hr_select(rs->rs_svcpt);
373 spin_lock(&hrt->hrt_lock);
374 list_add_tail(&rs->rs_list, &hrt->hrt_queue);
375 spin_unlock(&hrt->hrt_lock);
377 wake_up(&hrt->hrt_waitq);
381 void ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs)
385 assert_spin_locked(&rs->rs_svcpt->scp_rep_lock);
386 assert_spin_locked(&rs->rs_lock);
387 LASSERT(rs->rs_difficult);
388 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
390 if (rs->rs_scheduled) { /* being set up or already notified */
395 rs->rs_scheduled = 1;
396 list_del_init(&rs->rs_list);
397 ptlrpc_dispatch_difficult_reply(rs);
400 EXPORT_SYMBOL(ptlrpc_schedule_difficult_reply);
402 void ptlrpc_commit_replies(struct obd_export *exp)
404 struct ptlrpc_reply_state *rs, *nxt;
405 DECLARE_RS_BATCH(batch);
409 rs_batch_init(&batch);
411 * Find any replies that have been committed and get their service
412 * to attend to complete them.
415 /* CAVEAT EMPTOR: spinlock ordering!!! */
416 spin_lock(&exp->exp_uncommitted_replies_lock);
417 list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
419 LASSERT(rs->rs_difficult);
420 /* VBR: per-export last_committed */
421 LASSERT(rs->rs_export);
422 if (rs->rs_transno <= exp->exp_last_committed) {
423 list_del_init(&rs->rs_obd_list);
424 rs_batch_add(&batch, rs);
427 spin_unlock(&exp->exp_uncommitted_replies_lock);
428 rs_batch_fini(&batch);
432 static int ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt)
434 struct ptlrpc_request_buffer_desc *rqbd;
439 spin_lock(&svcpt->scp_lock);
441 if (list_empty(&svcpt->scp_rqbd_idle)) {
442 spin_unlock(&svcpt->scp_lock);
446 rqbd = list_first_entry(&svcpt->scp_rqbd_idle,
447 struct ptlrpc_request_buffer_desc,
450 /* assume we will post successfully */
451 svcpt->scp_nrqbds_posted++;
452 list_move(&rqbd->rqbd_list, &svcpt->scp_rqbd_posted);
454 spin_unlock(&svcpt->scp_lock);
456 rc = ptlrpc_register_rqbd(rqbd);
463 spin_lock(&svcpt->scp_lock);
465 svcpt->scp_nrqbds_posted--;
466 list_move_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
469 * Don't complain if no request buffers are posted right now; LNET
470 * won't drop requests because we set the portal lazy!
473 spin_unlock(&svcpt->scp_lock);
478 static void ptlrpc_at_timer(cfs_timer_cb_arg_t data)
480 struct ptlrpc_service_part *svcpt;
482 svcpt = cfs_from_timer(svcpt, data, scp_at_timer);
484 svcpt->scp_at_check = 1;
485 svcpt->scp_at_checktime = ktime_get();
486 wake_up(&svcpt->scp_waitq);
489 static void ptlrpc_server_nthreads_check(struct ptlrpc_service *svc,
490 struct ptlrpc_service_conf *conf)
492 struct ptlrpc_service_thr_conf *tc = &conf->psc_thr;
499 * Common code for estimating & validating threads number.
500 * CPT affinity service could have percpt thread-pool instead
501 * of a global thread-pool, which means user might not always
502 * get the threads number they give it in conf::tc_nthrs_user
503 * even they did set. It's because we need to validate threads
504 * number for each CPT to guarantee each pool will have enough
505 * threads to keep the service healthy.
507 init = PTLRPC_NTHRS_INIT + (svc->srv_ops.so_hpreq_handler != NULL);
508 init = max_t(int, init, tc->tc_nthrs_init);
511 * NB: please see comments in lustre_lnet.h for definition
512 * details of these members
514 LASSERT(tc->tc_nthrs_max != 0);
516 if (tc->tc_nthrs_user != 0) {
518 * In case there is a reason to test a service with many
519 * threads, we give a less strict check here, it can
520 * be up to 8 * nthrs_max
522 total = min(tc->tc_nthrs_max * 8, tc->tc_nthrs_user);
523 nthrs = total / svc->srv_ncpts;
524 init = max(init, nthrs);
528 total = tc->tc_nthrs_max;
529 if (tc->tc_nthrs_base == 0) {
531 * don't care about base threads number per partition,
532 * this is most for non-affinity service
534 nthrs = total / svc->srv_ncpts;
538 nthrs = tc->tc_nthrs_base;
539 if (svc->srv_ncpts == 1) {
543 * NB: Increase the base number if it's single partition
544 * and total number of cores/HTs is larger or equal to 4.
545 * result will always < 2 * nthrs_base
547 weight = cfs_cpt_weight(svc->srv_cptable, CFS_CPT_ANY);
548 for (i = 1; (weight >> (i + 1)) != 0 && /* >= 4 cores/HTs */
549 (tc->tc_nthrs_base >> i) != 0; i++)
550 nthrs += tc->tc_nthrs_base >> i;
553 if (tc->tc_thr_factor != 0) {
554 int factor = tc->tc_thr_factor;
558 * User wants to increase number of threads with for
559 * each CPU core/HT, most likely the factor is larger than
560 * one thread/core because service threads are supposed to
561 * be blocked by lock or wait for IO.
564 * Amdahl's law says that adding processors wouldn't give
565 * a linear increasing of parallelism, so it's nonsense to
566 * have too many threads no matter how many cores/HTs
571 (topology_sibling_cpumask(smp_processor_id())) > 1) {
572 /* weight is # of HTs */
573 /* depress thread factor for hyper-thread */
574 factor = factor - (factor >> 1) + (factor >> 3);
578 weight = cfs_cpt_weight(svc->srv_cptable, 0);
580 for (; factor > 0 && weight > 0; factor--, weight -= fade)
581 nthrs += min(weight, fade) * factor;
584 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
585 nthrs = max(tc->tc_nthrs_base,
586 tc->tc_nthrs_max / svc->srv_ncpts);
589 nthrs = max(nthrs, tc->tc_nthrs_init);
590 svc->srv_nthrs_cpt_limit = nthrs;
591 svc->srv_nthrs_cpt_init = init;
593 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
595 "%s: This service may have more threads (%d) than the given soft limit (%d)\n",
596 svc->srv_name, nthrs * svc->srv_ncpts,
602 * Initialize percpt data for a service
604 static int ptlrpc_service_part_init(struct ptlrpc_service *svc,
605 struct ptlrpc_service_part *svcpt, int cpt)
607 struct ptlrpc_at_array *array;
612 svcpt->scp_cpt = cpt;
613 INIT_LIST_HEAD(&svcpt->scp_threads);
615 /* rqbd and incoming request queue */
616 spin_lock_init(&svcpt->scp_lock);
617 mutex_init(&svcpt->scp_mutex);
618 INIT_LIST_HEAD(&svcpt->scp_rqbd_idle);
619 INIT_LIST_HEAD(&svcpt->scp_rqbd_posted);
620 INIT_LIST_HEAD(&svcpt->scp_req_incoming);
621 init_waitqueue_head(&svcpt->scp_waitq);
622 /* history request & rqbd list */
623 INIT_LIST_HEAD(&svcpt->scp_hist_reqs);
624 INIT_LIST_HEAD(&svcpt->scp_hist_rqbds);
626 /* acitve requests and hp requests */
627 spin_lock_init(&svcpt->scp_req_lock);
630 spin_lock_init(&svcpt->scp_rep_lock);
631 INIT_LIST_HEAD(&svcpt->scp_rep_active);
632 INIT_LIST_HEAD(&svcpt->scp_rep_idle);
633 init_waitqueue_head(&svcpt->scp_rep_waitq);
634 atomic_set(&svcpt->scp_nreps_difficult, 0);
636 /* adaptive timeout */
637 spin_lock_init(&svcpt->scp_at_lock);
638 array = &svcpt->scp_at_array;
640 size = at_est2timeout(obd_get_at_max(NULL));
641 array->paa_size = size;
642 array->paa_count = 0;
643 array->paa_deadline = -1;
645 /* allocate memory for scp_at_array (ptlrpc_at_array) */
646 OBD_CPT_ALLOC(array->paa_reqs_array,
647 svc->srv_cptable, cpt, sizeof(struct list_head) * size);
648 if (array->paa_reqs_array == NULL)
651 for (index = 0; index < size; index++)
652 INIT_LIST_HEAD(&array->paa_reqs_array[index]);
654 OBD_CPT_ALLOC(array->paa_reqs_count,
655 svc->srv_cptable, cpt, sizeof(__u32) * size);
656 if (array->paa_reqs_count == NULL)
659 cfs_timer_setup(&svcpt->scp_at_timer, ptlrpc_at_timer,
660 (unsigned long)svcpt, 0);
663 * At SOW, service time should be quick; 10s seems generous. If client
664 * timeout is less than this, we'll be sending an early reply.
666 at_init(&svcpt->scp_at_estimate, 10, 0);
668 /* assign this before call ptlrpc_grow_req_bufs */
669 svcpt->scp_service = svc;
670 /* Now allocate the request buffers, but don't post them now */
671 rc = ptlrpc_grow_req_bufs(svcpt, 0);
673 * We shouldn't be under memory pressure at startup, so
674 * fail if we can't allocate all our buffers at this time.
682 if (array->paa_reqs_count != NULL) {
683 OBD_FREE_PTR_ARRAY(array->paa_reqs_count, size);
684 array->paa_reqs_count = NULL;
687 if (array->paa_reqs_array != NULL) {
688 OBD_FREE_PTR_ARRAY(array->paa_reqs_array, array->paa_size);
689 array->paa_reqs_array = NULL;
696 * Initialize service on a given portal.
697 * This includes starting serving threads , allocating and posting rqbds and
700 struct ptlrpc_service *ptlrpc_register_service(struct ptlrpc_service_conf *conf,
702 struct dentry *debugfs_entry)
704 struct ptlrpc_service_cpt_conf *cconf = &conf->psc_cpt;
705 struct ptlrpc_service *service;
706 struct ptlrpc_service_part *svcpt;
707 struct cfs_cpt_table *cptable;
716 LASSERT(conf->psc_buf.bc_nbufs > 0);
717 LASSERT(conf->psc_buf.bc_buf_size >=
718 conf->psc_buf.bc_req_max_size + SPTLRPC_MAX_PAYLOAD);
719 LASSERT(conf->psc_thr.tc_ctx_tags != 0);
721 cptable = cconf->cc_cptable;
723 cptable = cfs_cpt_tab;
725 if (conf->psc_thr.tc_cpu_bind > 1) {
726 CERROR("%s: Invalid cpu bind value %d, only 1 or 0 allowed\n",
727 conf->psc_name, conf->psc_thr.tc_cpu_bind);
728 RETURN(ERR_PTR(-EINVAL));
731 if (!cconf->cc_affinity) {
734 ncpts = cfs_cpt_number(cptable);
735 if (cconf->cc_pattern != NULL) {
736 struct cfs_expr_list *el;
738 rc = cfs_expr_list_parse(cconf->cc_pattern,
739 strlen(cconf->cc_pattern),
742 CERROR("%s: invalid CPT pattern string: %s\n",
743 conf->psc_name, cconf->cc_pattern);
744 RETURN(ERR_PTR(-EINVAL));
747 rc = cfs_expr_list_values(el, ncpts, &cpts);
748 cfs_expr_list_free(el);
750 CERROR("%s: failed to parse CPT array %s: %d\n",
751 conf->psc_name, cconf->cc_pattern, rc);
753 OBD_FREE_PTR_ARRAY(cpts, ncpts);
754 RETURN(ERR_PTR(rc < 0 ? rc : -EINVAL));
760 OBD_ALLOC(service, offsetof(struct ptlrpc_service, srv_parts[ncpts]));
761 if (service == NULL) {
763 OBD_FREE_PTR_ARRAY(cpts, ncpts);
764 RETURN(ERR_PTR(-ENOMEM));
767 service->srv_cptable = cptable;
768 service->srv_cpts = cpts;
769 service->srv_ncpts = ncpts;
770 service->srv_cpt_bind = conf->psc_thr.tc_cpu_bind;
772 service->srv_cpt_bits = 0; /* it's zero already, easy to read... */
773 while ((1 << service->srv_cpt_bits) < cfs_cpt_number(cptable))
774 service->srv_cpt_bits++;
777 spin_lock_init(&service->srv_lock);
778 service->srv_name = conf->psc_name;
779 service->srv_watchdog_factor = conf->psc_watchdog_factor;
780 INIT_LIST_HEAD(&service->srv_list); /* for safty of cleanup */
782 /* buffer configuration */
783 service->srv_nbuf_per_group = test_req_buffer_pressure ?
784 1 : conf->psc_buf.bc_nbufs;
785 /* do not limit max number of rqbds by default */
786 service->srv_nrqbds_max = 0;
788 service->srv_max_req_size = conf->psc_buf.bc_req_max_size +
790 service->srv_buf_size = conf->psc_buf.bc_buf_size;
791 service->srv_rep_portal = conf->psc_buf.bc_rep_portal;
792 service->srv_req_portal = conf->psc_buf.bc_req_portal;
794 /* With slab/alloc_pages buffer size will be rounded up to 2^n */
795 if (service->srv_buf_size & (service->srv_buf_size - 1)) {
796 int round = size_roundup_power2(service->srv_buf_size);
798 service->srv_buf_size = round;
801 /* Increase max reply size to next power of two */
802 service->srv_max_reply_size = 1;
803 while (service->srv_max_reply_size <
804 conf->psc_buf.bc_rep_max_size + SPTLRPC_MAX_PAYLOAD)
805 service->srv_max_reply_size <<= 1;
807 service->srv_thread_name = conf->psc_thr.tc_thr_name;
808 service->srv_ctx_tags = conf->psc_thr.tc_ctx_tags;
809 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
810 service->srv_ops = conf->psc_ops;
812 for (i = 0; i < ncpts; i++) {
813 if (!cconf->cc_affinity)
816 cpt = cpts != NULL ? cpts[i] : i;
818 OBD_CPT_ALLOC(svcpt, cptable, cpt, sizeof(*svcpt));
820 GOTO(failed, rc = -ENOMEM);
822 service->srv_parts[i] = svcpt;
823 rc = ptlrpc_service_part_init(service, svcpt, cpt);
828 ptlrpc_server_nthreads_check(service, conf);
830 rc = LNetSetLazyPortal(service->srv_req_portal);
833 mutex_lock(&ptlrpc_all_services_mutex);
834 list_add(&service->srv_list, &ptlrpc_all_services);
835 mutex_unlock(&ptlrpc_all_services_mutex);
838 rc = ptlrpc_sysfs_register_service(parent, service);
843 if (debugfs_entry != NULL)
844 ptlrpc_ldebugfs_register_service(debugfs_entry, service);
846 rc = ptlrpc_service_nrs_setup(service);
850 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
851 service->srv_name, service->srv_req_portal);
853 rc = ptlrpc_start_threads(service);
855 CERROR("Failed to start threads for service %s: %d\n",
856 service->srv_name, rc);
862 ptlrpc_unregister_service(service);
865 EXPORT_SYMBOL(ptlrpc_register_service);
868 * to actually free the request, must be called without holding svc_lock.
869 * note it's caller's responsibility to unlink req->rq_list.
871 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
873 LASSERT(atomic_read(&req->rq_refcount) == 0);
874 LASSERT(list_empty(&req->rq_timed_list));
877 * DEBUG_REQ() assumes the reply state of a request with a valid
878 * ref will not be destroyed until that reference is dropped.
880 ptlrpc_req_drop_rs(req);
882 sptlrpc_svc_ctx_decref(req);
884 if (req != &req->rq_rqbd->rqbd_req) {
886 * NB request buffers use an embedded
887 * req if the incoming req unlinked the
888 * MD; this isn't one of them!
890 ptlrpc_request_cache_free(req);
895 * drop a reference count of the request. if it reaches 0, we either
896 * put it into history list, or free it immediately.
898 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
900 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
901 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
902 struct ptlrpc_service *svc = svcpt->scp_service;
905 if (!atomic_dec_and_test(&req->rq_refcount))
908 if (req->rq_session.lc_state == LCS_ENTERED) {
909 lu_context_exit(&req->rq_session);
910 lu_context_fini(&req->rq_session);
913 if (req->rq_at_linked) {
914 spin_lock(&svcpt->scp_at_lock);
916 * recheck with lock, in case it's unlinked by
917 * ptlrpc_at_check_timed()
919 if (likely(req->rq_at_linked))
920 ptlrpc_at_remove_timed(req);
921 spin_unlock(&svcpt->scp_at_lock);
924 LASSERT(list_empty(&req->rq_timed_list));
926 /* finalize request */
927 if (req->rq_export) {
928 class_export_put(req->rq_export);
929 req->rq_export = NULL;
932 spin_lock(&svcpt->scp_lock);
934 list_add(&req->rq_list, &rqbd->rqbd_reqs);
936 refcount = --(rqbd->rqbd_refcount);
938 /* request buffer is now idle: add to history */
939 list_move_tail(&rqbd->rqbd_list, &svcpt->scp_hist_rqbds);
940 svcpt->scp_hist_nrqbds++;
944 * I expect only about 1 or 2 rqbds need to be recycled here
946 while (svcpt->scp_hist_nrqbds > svc->srv_hist_nrqbds_cpt_max) {
947 rqbd = list_first_entry(&svcpt->scp_hist_rqbds,
948 struct ptlrpc_request_buffer_desc,
951 list_del(&rqbd->rqbd_list);
952 svcpt->scp_hist_nrqbds--;
955 * remove rqbd's reqs from svc's req history while
956 * I've got the service lock
958 list_for_each_entry(req, &rqbd->rqbd_reqs, rq_list) {
959 /* Track the highest culled req seq */
960 if (req->rq_history_seq >
961 svcpt->scp_hist_seq_culled) {
962 svcpt->scp_hist_seq_culled =
965 list_del(&req->rq_history_list);
968 spin_unlock(&svcpt->scp_lock);
970 while ((req = list_first_entry_or_null(
972 struct ptlrpc_request, rq_list))) {
973 list_del(&req->rq_list);
974 ptlrpc_server_free_request(req);
977 spin_lock(&svcpt->scp_lock);
979 * now all reqs including the embedded req has been
980 * disposed, schedule request buffer for re-use
981 * or free it to drain some in excess.
983 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
984 if (svcpt->scp_nrqbds_posted >=
985 svc->srv_nbuf_per_group ||
986 (svc->srv_nrqbds_max != 0 &&
987 svcpt->scp_nrqbds_total > svc->srv_nrqbds_max) ||
988 test_req_buffer_pressure) {
989 /* like in ptlrpc_free_rqbd() */
990 svcpt->scp_nrqbds_total--;
991 OBD_FREE_LARGE(rqbd->rqbd_buffer,
995 list_add_tail(&rqbd->rqbd_list,
996 &svcpt->scp_rqbd_idle);
1000 spin_unlock(&svcpt->scp_lock);
1001 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
1002 /* If we are low on memory, we are not interested in history */
1003 list_del(&req->rq_list);
1004 list_del_init(&req->rq_history_list);
1006 /* Track the highest culled req seq */
1007 if (req->rq_history_seq > svcpt->scp_hist_seq_culled)
1008 svcpt->scp_hist_seq_culled = req->rq_history_seq;
1010 spin_unlock(&svcpt->scp_lock);
1012 ptlrpc_server_free_request(req);
1014 spin_unlock(&svcpt->scp_lock);
1018 static void ptlrpc_add_exp_list_nolock(struct ptlrpc_request *req,
1019 struct obd_export *export, bool hp)
1021 __u16 tag = lustre_msg_get_tag(req->rq_reqmsg);
1024 list_add(&req->rq_exp_list, &export->exp_hp_rpcs);
1026 list_add(&req->rq_exp_list, &export->exp_reg_rpcs);
1027 if (tag && export->exp_used_slots)
1028 set_bit(tag - 1, export->exp_used_slots);
1031 static void ptlrpc_del_exp_list(struct ptlrpc_request *req)
1033 __u16 tag = lustre_msg_get_tag(req->rq_reqmsg);
1035 spin_lock(&req->rq_export->exp_rpc_lock);
1036 list_del_init(&req->rq_exp_list);
1037 if (tag && !req->rq_obsolete && req->rq_export->exp_used_slots)
1038 clear_bit(tag - 1, req->rq_export->exp_used_slots);
1039 spin_unlock(&req->rq_export->exp_rpc_lock);
1042 /** Change request export and move hp request from old export to new */
1043 void ptlrpc_request_change_export(struct ptlrpc_request *req,
1044 struct obd_export *export)
1046 if (req->rq_export != NULL) {
1047 LASSERT(!list_empty(&req->rq_exp_list));
1048 /* remove rq_exp_list from last export */
1049 ptlrpc_del_exp_list(req);
1050 /* export has one reference already, so it's safe to
1051 * add req to export queue here and get another
1052 * reference for request later
1054 spin_lock(&export->exp_rpc_lock);
1055 ptlrpc_add_exp_list_nolock(req, export, req->rq_ops != NULL);
1056 spin_unlock(&export->exp_rpc_lock);
1058 class_export_rpc_dec(req->rq_export);
1059 class_export_put(req->rq_export);
1062 /* request takes one export refcount */
1063 req->rq_export = class_export_get(export);
1064 class_export_rpc_inc(export);
1068 * to finish a request: stop sending more early replies, and release
1071 static void ptlrpc_server_finish_request(struct ptlrpc_service_part *svcpt,
1072 struct ptlrpc_request *req)
1074 ptlrpc_server_hpreq_fini(req);
1076 ptlrpc_server_drop_request(req);
1080 * to finish an active request: stop sending more early replies, and release
1081 * the request. should be called after we finished handling the request.
1083 static void ptlrpc_server_finish_active_request(
1084 struct ptlrpc_service_part *svcpt,
1085 struct ptlrpc_request *req)
1087 spin_lock(&svcpt->scp_req_lock);
1088 ptlrpc_nrs_req_stop_nolock(req);
1089 svcpt->scp_nreqs_active--;
1091 svcpt->scp_nhreqs_active--;
1092 spin_unlock(&svcpt->scp_req_lock);
1094 ptlrpc_nrs_req_finalize(req);
1096 if (req->rq_export != NULL)
1097 class_export_rpc_dec(req->rq_export);
1099 ptlrpc_server_finish_request(svcpt, req);
1103 * This function makes sure dead exports are evicted in a timely manner.
1104 * This function is only called when some export receives a message (i.e.,
1105 * the network is up.)
1107 void ptlrpc_update_export_timer(struct obd_export *exp, time64_t extra_delay)
1109 struct obd_export *oldest_exp, *newest_exp;
1110 time64_t oldest_time, current_time;
1117 * Compensate for slow machines, etc, by faking our request time
1118 * into the future. Although this can break the strict time-ordering
1119 * of the list, we can be really lazy here - we don't have to evict
1120 * at the exact right moment. Eventually, all silent exports
1121 * will make it to the top of the list.
1124 /* Do not pay attention on 1sec or smaller renewals. */
1125 current_time = ktime_get_real_seconds();
1127 if (exp->exp_last_request_time + 1 >= current_time + extra_delay)
1130 exp->exp_last_request_time = current_time + extra_delay;
1133 * exports may get disconnected from the chain even though the
1134 * export has references, so we must keep the spin lock while
1135 * manipulating the lists
1137 spin_lock(&exp->exp_obd->obd_dev_lock);
1139 if (list_empty(&exp->exp_obd_chain_timed)) {
1140 /* this one is not timed */
1141 spin_unlock(&exp->exp_obd->obd_dev_lock);
1145 newest_exp = list_last_entry(&exp->exp_obd->obd_exports_timed,
1146 struct obd_export, exp_obd_chain_timed);
1148 list_move_tail(&exp->exp_obd_chain_timed,
1149 &exp->exp_obd->obd_exports_timed);
1151 if (exp->exp_obd->obd_recovering) {
1152 /* be nice to everyone during recovery */
1153 spin_unlock(&exp->exp_obd->obd_dev_lock);
1157 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
1158 struct obd_export, exp_obd_chain_timed);
1160 oldest_time = oldest_exp->exp_last_request_time;
1162 /* Check if the oldest entry is expired. */
1163 if (exp->exp_obd->obd_eviction_timer == 0 &&
1164 current_time > oldest_time + PING_EVICT_TIMEOUT + extra_delay) {
1166 if (current_time < newest_exp->exp_last_request_time +
1167 PING_EVICT_TIMEOUT / 2) {
1168 /* If import is active - evict stale clients */
1172 * We need a second timer, in case the net was down and
1173 * it just came back. Since the pinger may skip every
1174 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
1175 * we better wait for 3.
1177 exp->exp_obd->obd_eviction_timer =
1178 ktime_get_real_seconds() + 3 * PING_INTERVAL;
1179 CDEBUG(D_HA, "%s: Think about evicting %s from %lld\n",
1180 exp->exp_obd->obd_name,
1181 obd_export_nid2str(oldest_exp), oldest_time);
1186 spin_unlock(&exp->exp_obd->obd_dev_lock);
1189 /* Evict stale clients */
1190 ping_evictor_wake(exp);
1192 if (ktime_get_real_seconds() >
1193 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
1195 * The evictor won't evict anyone who we've heard from
1196 * recently, so we don't have to check before we start
1199 if (!ping_evictor_wake(exp))
1200 exp->exp_obd->obd_eviction_timer = 0;
1208 * Sanity check request \a req.
1209 * Return 0 if all is ok, error code otherwise.
1211 static int ptlrpc_check_req(struct ptlrpc_request *req)
1213 struct obd_device *obd = req->rq_export->exp_obd;
1216 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
1217 req->rq_export->exp_conn_cnt)) {
1218 DEBUG_REQ(D_RPCTRACE, req,
1219 "DROPPING req from old connection %d < %d",
1220 lustre_msg_get_conn_cnt(req->rq_reqmsg),
1221 req->rq_export->exp_conn_cnt);
1224 if (unlikely(obd == NULL || obd->obd_fail)) {
1226 * Failing over, don't handle any more reqs,
1227 * send error response instead.
1229 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
1230 req, (obd != NULL) ? obd->obd_name : "unknown");
1232 } else if (lustre_msg_get_flags(req->rq_reqmsg) &
1233 (MSG_REPLAY | MSG_REQ_REPLAY_DONE) &&
1234 !obd->obd_recovering) {
1235 DEBUG_REQ(D_ERROR, req,
1236 "Invalid replay without recovery");
1237 class_fail_export(req->rq_export);
1239 } else if (lustre_msg_get_transno(req->rq_reqmsg) != 0 &&
1240 !obd->obd_recovering) {
1241 DEBUG_REQ(D_ERROR, req,
1242 "Invalid req with transno %llu without recovery",
1243 lustre_msg_get_transno(req->rq_reqmsg));
1244 class_fail_export(req->rq_export);
1248 if (unlikely(rc < 0)) {
1249 req->rq_status = rc;
1255 static void ptlrpc_at_set_timer(struct ptlrpc_service_part *svcpt)
1257 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1260 if (array->paa_count == 0) {
1261 timer_delete(&svcpt->scp_at_timer);
1265 /* Set timer for closest deadline */
1266 next = array->paa_deadline - ktime_get_real_seconds() -
1269 ptlrpc_at_timer(cfs_timer_cb_arg(svcpt, scp_at_timer));
1271 mod_timer(&svcpt->scp_at_timer,
1272 jiffies + nsecs_to_jiffies(next * NSEC_PER_SEC));
1273 CDEBUG(D_INFO, "armed %s at %+llds\n",
1274 svcpt->scp_service->srv_name, next);
1278 /* Add rpc to early reply check list */
1279 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
1281 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1282 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1283 struct ptlrpc_request *rq = NULL;
1285 struct obd_device *obd = NULL;
1288 obd = req->rq_export->exp_obd;
1290 if (obd_at_off(obd))
1293 if (req->rq_no_reply)
1296 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
1299 spin_lock(&svcpt->scp_at_lock);
1300 LASSERT(list_empty(&req->rq_timed_list));
1302 div_u64_rem(req->rq_deadline, array->paa_size, &index);
1303 if (array->paa_reqs_count[index] > 0) {
1305 * latest rpcs will have the latest deadlines in the list,
1306 * so search backward.
1308 list_for_each_entry_reverse(rq, &array->paa_reqs_array[index],
1310 if (req->rq_deadline >= rq->rq_deadline) {
1311 list_add(&req->rq_timed_list,
1312 &rq->rq_timed_list);
1318 /* Add the request at the head of the list */
1319 if (list_empty(&req->rq_timed_list))
1320 list_add(&req->rq_timed_list, &array->paa_reqs_array[index]);
1322 spin_lock(&req->rq_lock);
1323 req->rq_at_linked = 1;
1324 spin_unlock(&req->rq_lock);
1325 req->rq_at_index = index;
1326 array->paa_reqs_count[index]++;
1328 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
1329 array->paa_deadline = req->rq_deadline;
1330 ptlrpc_at_set_timer(svcpt);
1332 spin_unlock(&svcpt->scp_at_lock);
1337 static void ptlrpc_at_remove_timed(struct ptlrpc_request *req)
1339 struct ptlrpc_at_array *array;
1341 array = &req->rq_rqbd->rqbd_svcpt->scp_at_array;
1343 /* NB: must call with hold svcpt::scp_at_lock */
1344 LASSERT(!list_empty(&req->rq_timed_list));
1345 list_del_init(&req->rq_timed_list);
1347 spin_lock(&req->rq_lock);
1348 req->rq_at_linked = 0;
1349 spin_unlock(&req->rq_lock);
1351 array->paa_reqs_count[req->rq_at_index]--;
1356 * Attempt to extend the request deadline by sending an early reply to the
1359 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
1361 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1362 struct ptlrpc_request *reqcopy;
1363 struct lustre_msg *reqmsg;
1364 timeout_t olddl = req->rq_deadline - ktime_get_real_seconds();
1367 struct obd_device *obd = NULL;
1372 obd = req->rq_export->exp_obd;
1374 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_RECONNECT) ||
1375 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND)) {
1376 /* don't send early reply */
1381 * deadline is when the client expects us to reply, margin is the
1382 * difference between clients' and servers' expectations
1384 DEBUG_REQ(D_ADAPTTO, req,
1385 "%ssending early reply (deadline %+ds, margin %+ds) for %d+%d",
1386 obd_at_off(obd) ? "AT off - not " : "",
1387 olddl, olddl - obd_at_get(obd, &svcpt->scp_at_estimate),
1388 obd_at_get(obd, &svcpt->scp_at_estimate), at_extra);
1390 if (obd_at_off(obd))
1394 /* below message is checked in replay-ost-single.sh test_9 */
1395 DEBUG_REQ(D_WARNING, req,
1396 "Already past deadline (%+ds), not sending early reply. Consider increasing at_early_margin (%d)?",
1397 olddl, at_early_margin);
1399 /* Return an error so we're not re-added to the timed list. */
1403 if ((lustre_msghdr_get_flags(req->rq_reqmsg) &
1404 MSGHDR_AT_SUPPORT) == 0) {
1405 DEBUG_REQ(D_INFO, req,
1406 "Wanted to ask client for more time, but no AT support");
1410 if (req->rq_export &&
1411 lustre_msg_get_flags(req->rq_reqmsg) &
1412 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
1413 struct obd_device *obd_exp = req->rq_export->exp_obd;
1416 * During recovery, we don't want to send too many early
1417 * replies, but on the other hand we want to make sure the
1418 * client has enough time to resend if the rpc is lost. So
1419 * during the recovery period send at least 4 early replies,
1420 * spacing them every at_extra if we can. at_estimate should
1421 * always equal this fixed value during recovery.
1425 * Don't account request processing time into AT history
1426 * during recovery, it is not service time we need but
1427 * includes also waiting time for recovering clients
1429 newdl = min_t(time64_t, at_extra,
1430 obd_exp->obd_recovery_timeout / 4) +
1431 ktime_get_real_seconds();
1434 * We want to extend the request deadline by at_extra seconds,
1435 * so we set our service estimate to reflect how much time has
1436 * passed since this request arrived plus an additional
1437 * at_extra seconds. The client will calculate the new deadline
1438 * based on this service estimate (plus some additional time to
1439 * account for network latency). See ptlrpc_at_recv_early_reply
1441 obd_at_measure(obd, &svcpt->scp_at_estimate, at_extra +
1442 ktime_get_real_seconds() -
1443 req->rq_arrival_time.tv_sec);
1444 newdl = req->rq_arrival_time.tv_sec +
1445 obd_at_get(obd, &svcpt->scp_at_estimate);
1449 * Check to see if we've actually increased the deadline -
1450 * we may be past adaptive_max
1452 if (req->rq_deadline >= newdl) {
1453 DEBUG_REQ(D_WARNING, req,
1454 "Could not add any time (%d/%lld), not sending early reply",
1455 olddl, newdl - ktime_get_real_seconds());
1459 reqcopy = ptlrpc_request_cache_alloc(GFP_NOFS);
1460 if (reqcopy == NULL)
1462 OBD_ALLOC_LARGE(reqmsg, req->rq_reqlen);
1464 GOTO(out_free, rc = -ENOMEM);
1467 spin_lock_init(&reqcopy->rq_early_free_lock);
1468 reqcopy->rq_reply_state = NULL;
1469 reqcopy->rq_rep_swab_mask = 0;
1470 reqcopy->rq_pack_bulk = 0;
1471 reqcopy->rq_pack_udesc = 0;
1472 reqcopy->rq_packed_final = 0;
1473 sptlrpc_svc_ctx_addref(reqcopy);
1474 /* We only need the reqmsg for the magic */
1475 reqcopy->rq_reqmsg = reqmsg;
1476 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1479 * tgt_brw_read() and tgt_brw_write() may have decided not to reply.
1480 * Without this check, we would fail the rq_no_reply assertion in
1481 * ptlrpc_send_reply().
1483 if (reqcopy->rq_no_reply)
1484 GOTO(out, rc = -ETIMEDOUT);
1486 LASSERT(atomic_read(&req->rq_refcount));
1487 /* if it is last refcount then early reply isn't needed */
1488 if (atomic_read(&req->rq_refcount) == 1) {
1489 DEBUG_REQ(D_ADAPTTO, reqcopy,
1490 "Normal reply already sent, abort early reply");
1491 GOTO(out, rc = -EINVAL);
1494 /* Connection ref */
1495 reqcopy->rq_export = class_conn2export(
1496 lustre_msg_get_handle(reqcopy->rq_reqmsg));
1497 if (reqcopy->rq_export == NULL)
1498 GOTO(out, rc = -ENODEV);
1501 class_export_rpc_inc(reqcopy->rq_export);
1502 if (reqcopy->rq_export->exp_obd &&
1503 reqcopy->rq_export->exp_obd->obd_fail)
1504 GOTO(out_put, rc = -ENODEV);
1506 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1510 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1513 /* Adjust our own deadline to what we told the client */
1514 req->rq_deadline = newdl;
1515 req->rq_early_count++; /* number sent, server side */
1517 DEBUG_REQ(D_ERROR, req, "Early reply send failed: rc = %d", rc);
1521 * Free the (early) reply state from lustre_pack_reply.
1522 * (ptlrpc_send_reply takes it's own rs ref, so this is safe here)
1524 ptlrpc_req_drop_rs(reqcopy);
1527 class_export_rpc_dec(reqcopy->rq_export);
1528 class_export_put(reqcopy->rq_export);
1530 sptlrpc_svc_ctx_decref(reqcopy);
1531 OBD_FREE_LARGE(reqmsg, req->rq_reqlen);
1533 ptlrpc_request_cache_free(reqcopy);
1538 * Send early replies to everybody expiring within at_early_margin
1539 * asking for at_extra time
1541 static int ptlrpc_at_check_timed(struct ptlrpc_service_part *svcpt)
1543 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1544 struct ptlrpc_request *rq, *n;
1545 LIST_HEAD(work_list);
1548 time64_t now = ktime_get_real_seconds();
1550 int first, counter = 0;
1553 spin_lock(&svcpt->scp_at_lock);
1554 if (svcpt->scp_at_check == 0) {
1555 spin_unlock(&svcpt->scp_at_lock);
1558 delay_ms = ktime_ms_delta(ktime_get(), svcpt->scp_at_checktime);
1559 svcpt->scp_at_check = 0;
1561 if (array->paa_count == 0) {
1562 spin_unlock(&svcpt->scp_at_lock);
1566 /* The timer went off, but maybe the nearest rpc already completed. */
1567 first = array->paa_deadline - now;
1568 if (first > at_early_margin) {
1569 /* We've still got plenty of time. Reset the timer. */
1570 ptlrpc_at_set_timer(svcpt);
1571 spin_unlock(&svcpt->scp_at_lock);
1576 * We're close to a timeout, and we don't know how much longer the
1577 * server will take. Send early replies to everyone expiring soon.
1580 div_u64_rem(array->paa_deadline, array->paa_size, &index);
1581 count = array->paa_count;
1583 count -= array->paa_reqs_count[index];
1584 list_for_each_entry_safe(rq, n,
1585 &array->paa_reqs_array[index],
1587 if (rq->rq_deadline > now + at_early_margin) {
1588 /* update the earliest deadline */
1589 if (deadline == -1 ||
1590 rq->rq_deadline < deadline)
1591 deadline = rq->rq_deadline;
1596 * ptlrpc_server_drop_request() may drop
1597 * refcount to 0 already. Let's check this and
1598 * don't add entry to work_list
1600 if (likely(atomic_inc_not_zero(&rq->rq_refcount))) {
1601 ptlrpc_at_remove_timed(rq);
1602 list_add(&rq->rq_timed_list, &work_list);
1604 ptlrpc_at_remove_timed(rq);
1610 if (++index >= array->paa_size)
1613 array->paa_deadline = deadline;
1614 /* we have a new earliest deadline, restart the timer */
1615 ptlrpc_at_set_timer(svcpt);
1617 spin_unlock(&svcpt->scp_at_lock);
1620 "timeout in %+ds, asking for %d secs on %d early replies\n",
1621 first, at_extra, counter);
1624 * We're already past request deadlines before we even get a
1625 * chance to send early replies
1627 timeout_t atg = obd_at_get((struct obd_device *)NULL,
1628 &svcpt->scp_at_estimate);
1629 LCONSOLE_WARN("'%s' is processing requests too slowly, client may timeout. Late by %ds, missed %d early replies (reqs waiting=%d active=%d, at_estimate=%d, delay=%lldms)\n",
1630 svcpt->scp_service->srv_name, -first, counter,
1631 svcpt->scp_nreqs_incoming,
1632 svcpt->scp_nreqs_active,
1638 * we took additional refcount so entries can't be deleted from list, no
1641 while ((rq = list_first_entry_or_null(&work_list,
1642 struct ptlrpc_request,
1643 rq_timed_list)) != NULL) {
1644 list_del_init(&rq->rq_timed_list);
1646 if (ptlrpc_at_send_early_reply(rq) == 0)
1647 ptlrpc_at_add_timed(rq);
1649 ptlrpc_server_drop_request(rq);
1652 RETURN(1); /* return "did_something" for liblustre */
1656 * Check if we are already handling earlier incarnation of this request.
1657 * Called under &req->rq_export->exp_rpc_lock locked
1659 static struct ptlrpc_request*
1660 ptlrpc_server_check_resend_in_progress(struct ptlrpc_request *req)
1662 struct ptlrpc_request *tmp = NULL;
1664 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
1668 * This list should not be longer than max_requests in
1669 * flights on the client, so it is not all that long.
1670 * Also we only hit this codepath in case of a resent
1671 * request which makes it even more rarely hit
1673 list_for_each_entry(tmp, &req->rq_export->exp_reg_rpcs,
1675 /* Found duplicate one */
1676 if (tmp->rq_xid == req->rq_xid)
1679 list_for_each_entry(tmp, &req->rq_export->exp_hp_rpcs,
1681 /* Found duplicate one */
1682 if (tmp->rq_xid == req->rq_xid)
1688 DEBUG_REQ(D_HA, req, "Found duplicate req in processing");
1689 DEBUG_REQ(D_HA, tmp, "Request being processed");
1693 #ifdef HAVE_SERVER_SUPPORT
1694 static void ptlrpc_server_mark_obsolete(struct ptlrpc_request *req)
1696 spin_lock(&req->rq_lock);
1697 req->rq_obsolete = 1;
1698 spin_unlock(&req->rq_lock);
1702 ptlrpc_server_mark_in_progress_obsolete(struct ptlrpc_request *req)
1704 struct ptlrpc_request *tmp = NULL;
1707 if (!tgt_is_increasing_xid_client(req->rq_export) ||
1708 req->rq_export->exp_used_slots == NULL)
1711 tag = lustre_msg_get_tag(req->rq_reqmsg);
1715 if (!test_bit(tag - 1, req->rq_export->exp_used_slots))
1718 /* This list should not be longer than max_requests in
1719 * flights on the client, so it is not all that long.
1720 * Also we only hit this codepath in case of a resent
1721 * request which makes it even more rarely hit */
1722 list_for_each_entry(tmp, &req->rq_export->exp_reg_rpcs, rq_exp_list) {
1723 if (tag == lustre_msg_get_tag(tmp->rq_reqmsg) &&
1724 req->rq_xid > tmp->rq_xid)
1725 ptlrpc_server_mark_obsolete(tmp);
1728 list_for_each_entry(tmp, &req->rq_export->exp_hp_rpcs, rq_exp_list) {
1729 if (tag == lustre_msg_get_tag(tmp->rq_reqmsg) &&
1730 req->rq_xid > tmp->rq_xid)
1731 ptlrpc_server_mark_obsolete(tmp);
1737 * Check if a request should be assigned with a high priority.
1739 * \retval < 0: error occurred
1740 * 0: normal RPC request
1741 * +1: high priority request
1743 static int ptlrpc_server_hpreq_init(struct ptlrpc_service_part *svcpt,
1744 struct ptlrpc_request *req)
1749 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL) {
1750 rc = svcpt->scp_service->srv_ops.so_hpreq_handler(req);
1757 if (req->rq_export != NULL && req->rq_ops != NULL) {
1759 * Perform request specific check. We should do this
1760 * check before the request is added into exp_hp_rpcs
1761 * list otherwise it may hit swab race at LU-1044.
1763 if (req->rq_ops->hpreq_check != NULL) {
1764 rc = req->rq_ops->hpreq_check(req);
1765 if (rc == -ESTALE) {
1766 req->rq_status = rc;
1770 * can only return error,
1771 * 0 for normal request,
1772 * or 1 for high priority request
1781 /** Remove the request from the export list. */
1782 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req)
1785 if (req->rq_export) {
1787 * refresh lock timeout again so that client has more
1788 * room to send lock cancel RPC.
1790 if (req->rq_ops && req->rq_ops->hpreq_fini)
1791 req->rq_ops->hpreq_fini(req);
1793 ptlrpc_del_exp_list(req);
1798 static int ptlrpc_hpreq_check(struct ptlrpc_request *req)
1803 static struct ptlrpc_hpreq_ops ptlrpc_hpreq_common = {
1804 .hpreq_check = ptlrpc_hpreq_check,
1807 /* Hi-Priority RPC check by RPC operation code. */
1808 int ptlrpc_hpreq_handler(struct ptlrpc_request *req)
1810 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1813 * Check for export to let only reconnects for not yet evicted
1814 * export to become a HP rpc.
1816 if ((req->rq_export != NULL) &&
1817 (opc == OBD_PING || opc == MDS_CONNECT || opc == OST_CONNECT))
1818 req->rq_ops = &ptlrpc_hpreq_common;
1822 EXPORT_SYMBOL(ptlrpc_hpreq_handler);
1824 static int ptlrpc_server_request_add(struct ptlrpc_service_part *svcpt,
1825 struct ptlrpc_request *req)
1829 struct ptlrpc_request *orig;
1833 rc = ptlrpc_server_hpreq_init(svcpt, req);
1838 ptlrpc_nrs_req_initialize(svcpt, req, hp);
1840 while (req->rq_export != NULL) {
1841 struct obd_export *exp = req->rq_export;
1844 * do search for duplicated xid and the adding to the list
1847 spin_lock_bh(&exp->exp_rpc_lock);
1848 #ifdef HAVE_SERVER_SUPPORT
1849 ptlrpc_server_mark_in_progress_obsolete(req);
1851 orig = ptlrpc_server_check_resend_in_progress(req);
1852 if (orig && CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_RESEND_RACE)) {
1853 spin_unlock_bh(&exp->exp_rpc_lock);
1855 CFS_RACE(OBD_FAIL_PTLRPC_RESEND_RACE);
1856 msleep(4 * MSEC_PER_SEC);
1860 if (orig && likely(atomic_inc_not_zero(&orig->rq_refcount))) {
1863 spin_unlock_bh(&exp->exp_rpc_lock);
1866 * When the client resend request and the server has
1867 * the previous copy of it, we need to update deadlines,
1868 * to be sure that the client and the server have equal
1869 * request deadlines.
1872 spin_lock(&orig->rq_rqbd->rqbd_svcpt->scp_at_lock);
1873 linked = orig->rq_at_linked;
1875 ptlrpc_at_remove_timed(orig);
1876 spin_unlock(&orig->rq_rqbd->rqbd_svcpt->scp_at_lock);
1877 orig->rq_deadline = req->rq_deadline;
1878 orig->rq_rep_mbits = req->rq_rep_mbits;
1880 ptlrpc_at_add_timed(orig);
1881 ptlrpc_server_drop_request(orig);
1882 ptlrpc_nrs_req_finalize(req);
1884 /* don't mark slot unused for resend in progress */
1885 spin_lock(&req->rq_lock);
1886 req->rq_obsolete = 1;
1887 spin_unlock(&req->rq_lock);
1892 ptlrpc_add_exp_list_nolock(req, exp, hp || req->rq_ops != NULL);
1894 spin_unlock_bh(&exp->exp_rpc_lock);
1899 * the current thread is not the processing thread for this request
1900 * since that, but request is in exp_hp_list and can be find there.
1901 * Remove all relations between request and old thread.
1903 req->rq_svc_thread->t_env->le_ses = NULL;
1904 req->rq_svc_thread = NULL;
1905 req->rq_session.lc_thread = NULL;
1907 ptlrpc_nrs_req_add(svcpt, req, hp);
1913 * Allow to handle high priority request
1914 * User can call it w/o any lock but need to hold
1915 * ptlrpc_service_part::scp_req_lock to get reliable result
1917 static bool ptlrpc_server_allow_high(struct ptlrpc_service_part *svcpt,
1920 int running = svcpt->scp_nthrs_running;
1922 if (!nrs_svcpt_has_hp(svcpt))
1928 if (ptlrpc_nrs_req_throttling_nolock(svcpt, true))
1931 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1932 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1933 /* leave just 1 thread for normal RPCs */
1934 running = PTLRPC_NTHRS_INIT;
1935 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1939 if (svcpt->scp_nreqs_active >= running - 1)
1942 if (svcpt->scp_nhreqs_active == 0)
1945 return !ptlrpc_nrs_req_pending_nolock(svcpt, false) ||
1946 svcpt->scp_hreq_count < svcpt->scp_service->srv_hpreq_ratio;
1949 static bool ptlrpc_server_high_pending(struct ptlrpc_service_part *svcpt,
1952 return ptlrpc_server_allow_high(svcpt, force) &&
1953 ptlrpc_nrs_req_pending_nolock(svcpt, true);
1957 * Only allow normal priority requests on a service that has a high-priority
1958 * queue if forced (i.e. cleanup), if there are other high priority requests
1959 * already being processed (i.e. those threads can service more high-priority
1960 * requests), or if there are enough idle threads that a later thread can do
1961 * a high priority request.
1962 * User can call it w/o any lock but need to hold
1963 * ptlrpc_service_part::scp_req_lock to get reliable result
1965 static bool ptlrpc_server_allow_normal(struct ptlrpc_service_part *svcpt,
1968 int running = svcpt->scp_nthrs_running;
1970 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1971 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1972 /* leave just 1 thread for normal RPCs */
1973 running = PTLRPC_NTHRS_INIT;
1974 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1981 if (ptlrpc_nrs_req_throttling_nolock(svcpt, false))
1984 if (svcpt->scp_nreqs_active < running - 2)
1987 if (svcpt->scp_nreqs_active >= running - 1)
1990 return svcpt->scp_nhreqs_active > 0 || !nrs_svcpt_has_hp(svcpt);
1993 static bool ptlrpc_server_normal_pending(struct ptlrpc_service_part *svcpt,
1996 return ptlrpc_server_allow_normal(svcpt, force) &&
1997 ptlrpc_nrs_req_pending_nolock(svcpt, false);
2001 * Returns true if there are requests available in incoming
2002 * request queue for processing and it is allowed to fetch them.
2003 * User can call it w/o any lock but need to hold ptlrpc_service::scp_req_lock
2004 * to get reliable result
2005 * \see ptlrpc_server_allow_normal
2006 * \see ptlrpc_server_allow high
2009 bool ptlrpc_server_request_pending(struct ptlrpc_service_part *svcpt,
2012 return ptlrpc_server_high_pending(svcpt, force) ||
2013 ptlrpc_server_normal_pending(svcpt, force);
2017 * Fetch a request for processing from queue of unprocessed requests.
2018 * Favors high-priority requests.
2019 * Returns a pointer to fetched request.
2021 static struct ptlrpc_request *
2022 ptlrpc_server_request_get(struct ptlrpc_service_part *svcpt, bool force)
2024 struct ptlrpc_request *req = NULL;
2028 spin_lock(&svcpt->scp_req_lock);
2030 if (ptlrpc_server_high_pending(svcpt, force)) {
2031 req = ptlrpc_nrs_req_get_nolock(svcpt, true, force);
2033 svcpt->scp_hreq_count++;
2038 if (ptlrpc_server_normal_pending(svcpt, force)) {
2039 req = ptlrpc_nrs_req_get_nolock(svcpt, false, force);
2041 svcpt->scp_hreq_count = 0;
2046 spin_unlock(&svcpt->scp_req_lock);
2050 svcpt->scp_nreqs_active++;
2052 svcpt->scp_nhreqs_active++;
2054 spin_unlock(&svcpt->scp_req_lock);
2056 if (likely(req->rq_export))
2057 class_export_rpc_inc(req->rq_export);
2063 * Handle freshly incoming reqs, add to timed early reply list,
2064 * pass on to regular request queue.
2065 * All incoming requests pass through here before getting into
2066 * ptlrpc_server_handle_req later on.
2068 static int ptlrpc_server_handle_req_in(struct ptlrpc_service_part *svcpt,
2069 struct ptlrpc_thread *thread)
2071 struct ptlrpc_service *svc = svcpt->scp_service;
2072 struct ptlrpc_request *req;
2079 spin_lock(&svcpt->scp_lock);
2080 if (list_empty(&svcpt->scp_req_incoming)) {
2081 spin_unlock(&svcpt->scp_lock);
2085 req = list_first_entry(&svcpt->scp_req_incoming,
2086 struct ptlrpc_request, rq_list);
2087 list_del_init(&req->rq_list);
2088 svcpt->scp_nreqs_incoming--;
2090 * Consider this still a "queued" request as far as stats are
2093 spin_unlock(&svcpt->scp_lock);
2095 /* go through security check/transform */
2096 CDEBUG(D_RPCTRACE, "unwrap req x%llu\n", req->rq_xid);
2097 rc = sptlrpc_svc_unwrap_request(req);
2101 case SECSVC_COMPLETE:
2102 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
2111 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
2112 * redo it wouldn't be harmful.
2114 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
2115 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
2117 CERROR("error unpacking request: ptl %d from %s x%llu\n",
2118 svc->srv_req_portal, libcfs_idstr(&req->rq_peer),
2124 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
2126 CERROR("error unpacking ptlrpc body: ptl %d from %s x %llu\n",
2127 svc->srv_req_portal, libcfs_idstr(&req->rq_peer),
2132 opc = lustre_msg_get_opc(req->rq_reqmsg);
2133 if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
2134 opc == cfs_fail_val) {
2135 CERROR("drop incoming rpc opc %u, x%llu\n",
2136 cfs_fail_val, req->rq_xid);
2141 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
2142 CERROR("wrong packet type received (type=%u) from %s\n",
2143 lustre_msg_get_type(req->rq_reqmsg),
2144 libcfs_idstr(&req->rq_peer));
2152 req->rq_bulk_write = 1;
2156 case MGS_CONFIG_READ:
2157 req->rq_bulk_read = 1;
2161 CDEBUG(D_RPCTRACE, "got req x%llu\n", req->rq_xid);
2163 req->rq_export = class_conn2export(
2164 lustre_msg_get_handle(req->rq_reqmsg));
2165 if (req->rq_export) {
2166 rc = ptlrpc_check_req(req);
2168 rc = sptlrpc_target_export_check(req->rq_export, req);
2170 DEBUG_REQ(D_ERROR, req,
2171 "DROPPING req with illegal security flavor");
2176 ptlrpc_update_export_timer(req->rq_export, 0);
2179 /* req_in handling should/must be fast */
2180 if (ktime_get_real_seconds() - req->rq_arrival_time.tv_sec > 5)
2181 DEBUG_REQ(D_WARNING, req, "Slow req_in handling %llds",
2182 ktime_get_real_seconds() -
2183 req->rq_arrival_time.tv_sec);
2185 /* Set rpc server deadline and add it to the timed list */
2186 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
2187 MSGHDR_AT_SUPPORT) ?
2188 /* The max time the client expects us to take */
2189 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
2191 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
2192 if (unlikely(deadline == 0)) {
2193 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
2197 /* Skip early reply */
2198 if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_RESEND))
2199 req->rq_deadline += obd_timeout;
2201 req->rq_svc_thread = thread;
2202 if (thread != NULL) {
2204 * initialize request session, it is needed for request
2205 * processing by target
2207 rc = lu_context_init(&req->rq_session, LCT_SERVER_SESSION |
2210 CERROR("%s: failure to initialize session: rc = %d\n",
2211 thread->t_name, rc);
2214 req->rq_session.lc_thread = thread;
2215 lu_context_enter(&req->rq_session);
2216 thread->t_env->le_ses = &req->rq_session;
2220 if (unlikely(CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND) &&
2221 (opc == LDLM_ENQUEUE) &&
2222 (lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT)))
2223 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_ENQ_RESEND, 6);
2225 ptlrpc_at_add_timed(req);
2227 if (opc != OST_CONNECT && opc != MDS_CONNECT &&
2228 opc != MGS_CONNECT && req->rq_export != NULL) {
2229 if (exp_connect_flags2(req->rq_export) & OBD_CONNECT2_REP_MBITS)
2230 req->rq_rep_mbits = lustre_msg_get_mbits(req->rq_reqmsg);
2233 /* Move it over to the request processing queue */
2234 rc = ptlrpc_server_request_add(svcpt, req);
2238 wake_up(&svcpt->scp_waitq);
2242 CDEBUG(D_RPCTRACE, "finish req x%llu\n", req->rq_xid);
2243 ptlrpc_server_finish_request(svcpt, req);
2249 * Main incoming request handling logic.
2250 * Calls handler function from service to do actual processing.
2252 static int ptlrpc_server_handle_request(struct ptlrpc_service_part *svcpt,
2253 struct ptlrpc_thread *thread)
2255 struct ptlrpc_service *svc = svcpt->scp_service;
2256 struct ptlrpc_request *request;
2263 struct obd_device *obd = NULL;
2267 request = ptlrpc_server_request_get(svcpt, false);
2268 if (request == NULL)
2271 if (request->rq_export)
2272 obd = request->rq_export->exp_obd;
2274 if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
2275 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
2276 else if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
2277 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
2279 if (unlikely(fail_opc)) {
2280 if (request->rq_export && request->rq_ops)
2281 CFS_FAIL_TIMEOUT(fail_opc, 4);
2284 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
2286 if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
2287 libcfs_debug_dumplog();
2289 work_start = ktime_get_real();
2290 arrived = timespec64_to_ktime(request->rq_arrival_time);
2291 timediff_usecs = ktime_us_delta(work_start, arrived);
2292 if (likely(svc->srv_stats != NULL)) {
2293 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
2295 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
2296 svcpt->scp_nreqs_incoming);
2297 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
2298 svcpt->scp_nreqs_active);
2299 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
2300 obd_at_get(obd, &svcpt->scp_at_estimate));
2303 if (likely(request->rq_export)) {
2304 if (unlikely(ptlrpc_check_req(request)))
2306 ptlrpc_update_export_timer(request->rq_export,
2307 div_u64(timediff_usecs,
2312 * Discard requests queued for longer than the deadline.
2313 * The deadline is increased if we send an early reply.
2315 if (ktime_get_real_seconds() > request->rq_deadline) {
2316 DEBUG_REQ(D_ERROR, request,
2317 "Dropping timed-out request from %s: deadline %lld/%llds ago",
2318 libcfs_idstr(&request->rq_peer),
2319 request->rq_deadline -
2320 request->rq_arrival_time.tv_sec,
2321 ktime_get_real_seconds() - request->rq_deadline);
2326 "Handling RPC req@%p pname:cluuid+ref:pid:xid:nid:opc:job %s:%s+%d:%d:x%llu:%s:%d:%s\n",
2327 request, current->comm,
2328 (request->rq_export ?
2329 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
2330 (request->rq_export ?
2331 refcount_read(&request->rq_export->exp_handle.h_ref) : -99),
2332 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
2333 libcfs_idstr(&request->rq_peer),
2334 lustre_msg_get_opc(request->rq_reqmsg),
2335 lustre_msg_get_jobid(request->rq_reqmsg) ?: "");
2337 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
2338 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, cfs_fail_val);
2340 CDEBUG(D_NET, "got req %llu\n", request->rq_xid);
2342 /* re-assign request and sesson thread to the current one */
2343 request->rq_svc_thread = thread;
2344 if (thread != NULL) {
2345 LASSERT(request->rq_session.lc_thread == NULL);
2346 request->rq_session.lc_thread = thread;
2347 thread->t_env->le_ses = &request->rq_session;
2349 svc->srv_ops.so_req_handler(request);
2351 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
2354 if (unlikely(ktime_get_real_seconds() > request->rq_deadline)) {
2355 DEBUG_REQ(D_WARNING, request,
2356 "Request took longer than estimated (%lld/%llds); client may timeout",
2357 request->rq_deadline -
2358 request->rq_arrival_time.tv_sec,
2359 ktime_get_real_seconds() - request->rq_deadline);
2362 work_end = ktime_get_real();
2363 timediff_usecs = ktime_us_delta(work_end, work_start);
2364 arrived_usecs = ktime_us_delta(work_end, arrived);
2366 "Handled RPC req@%p pname:cluuid+ref:pid:xid:nid:opc:job %s:%s+%d:%d:x%llu:%s:%d:%s Request processed in %lldus (%lldus total) trans %llu rc %d/%d\n",
2367 request, current->comm,
2368 (request->rq_export ?
2369 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
2370 (request->rq_export ?
2371 refcount_read(&request->rq_export->exp_handle.h_ref) : -99),
2372 lustre_msg_get_status(request->rq_reqmsg),
2374 libcfs_idstr(&request->rq_peer),
2375 lustre_msg_get_opc(request->rq_reqmsg),
2376 lustre_msg_get_jobid(request->rq_reqmsg) ?: "",
2379 (request->rq_repmsg ?
2380 lustre_msg_get_transno(request->rq_repmsg) :
2381 request->rq_transno),
2383 (request->rq_repmsg ?
2384 lustre_msg_get_status(request->rq_repmsg) : -999));
2385 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
2386 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
2387 int opc = opcode_offset(op);
2389 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
2390 LASSERT(opc < LUSTRE_MAX_OPCODES);
2391 lprocfs_counter_add(svc->srv_stats,
2392 opc + EXTRA_MAX_OPCODES,
2396 if (unlikely(request->rq_early_count)) {
2397 DEBUG_REQ(D_ADAPTTO, request,
2398 "sent %d early replies before finishing in %llds",
2399 request->rq_early_count,
2400 div_u64(arrived_usecs, USEC_PER_SEC));
2403 ptlrpc_server_finish_active_request(svcpt, request);
2409 * An internal function to process a single reply state object.
2411 static int ptlrpc_handle_rs(struct ptlrpc_reply_state *rs)
2413 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
2414 struct ptlrpc_service *svc = svcpt->scp_service;
2415 struct obd_export *exp;
2421 exp = rs->rs_export;
2423 LASSERT(rs->rs_difficult);
2424 LASSERT(rs->rs_scheduled);
2425 LASSERT(list_empty(&rs->rs_list));
2428 * The disk commit callback holds exp_uncommitted_replies_lock while it
2429 * iterates over newly committed replies, removing them from
2430 * exp_uncommitted_replies. It then drops this lock and schedules the
2431 * replies it found for handling here.
2433 * We can avoid contention for exp_uncommitted_replies_lock between the
2434 * HRT threads and further commit callbacks by checking rs_committed
2435 * which is set in the commit callback while it holds both
2436 * rs_lock and exp_uncommitted_reples.
2438 * If we see rs_committed clear, the commit callback _may_ not have
2439 * handled this reply yet and we race with it to grab
2440 * exp_uncommitted_replies_lock before removing the reply from
2441 * exp_uncommitted_replies. Note that if we lose the race and the
2442 * reply has already been removed, list_del_init() is a noop.
2444 * If we see rs_committed set, we know the commit callback is handling,
2445 * or has handled this reply since store reordering might allow us to
2446 * see rs_committed set out of sequence. But since this is done
2447 * holding rs_lock, we can be sure it has all completed once we hold
2448 * rs_lock, which we do right next.
2450 if (!rs->rs_committed) {
2451 spin_lock(&exp->exp_uncommitted_replies_lock);
2452 list_del_init(&rs->rs_obd_list);
2453 spin_unlock(&exp->exp_uncommitted_replies_lock);
2456 spin_lock(&exp->exp_lock);
2457 /* Noop if removed already */
2458 list_del_init(&rs->rs_exp_list);
2459 spin_unlock(&exp->exp_lock);
2461 spin_lock(&rs->rs_lock);
2463 been_handled = rs->rs_handled;
2466 nlocks = rs->rs_nlocks; /* atomic "steal", but */
2467 rs->rs_nlocks = 0; /* locks still on rs_locks! */
2469 if (nlocks == 0 && !been_handled) {
2471 * If we see this, we should already have seen the warning
2472 * in mds_steal_ack_locks()
2475 "All locks stolen from rs %p x%lld.t%lld o%d NID %s\n",
2476 rs, rs->rs_xid, rs->rs_transno, rs->rs_opc,
2477 libcfs_nidstr(&exp->exp_connection->c_peer.nid));
2480 if ((rs->rs_sent && !rs->rs_unlinked) || nlocks > 0) {
2481 spin_unlock(&rs->rs_lock);
2483 /* We can unlink if the LNET_EVENT_SEND has occurred.
2484 * If rs_unlinked is set then MD is already unlinked and no
2485 * need to do so here.
2487 if ((rs->rs_sent && !rs->rs_unlinked)) {
2488 LNetMDUnlink(rs->rs_md_h);
2489 /* Ignore return code; we're racing with completion */
2492 while (nlocks-- > 0)
2493 ldlm_lock_decref(&rs->rs_locks[nlocks], LCK_TXN);
2495 spin_lock(&rs->rs_lock);
2498 rs->rs_scheduled = 0;
2500 if (rs->rs_unlinked) {
2502 spin_unlock(&rs->rs_lock);
2504 class_export_put(exp);
2505 rs->rs_export = NULL;
2506 ptlrpc_rs_decref(rs);
2507 if (atomic_dec_and_test(&svcpt->scp_nreps_difficult) &&
2508 svc->srv_is_stopping)
2509 wake_up_all(&svcpt->scp_waitq);
2513 /* still on the net; callback will schedule */
2514 spin_unlock(&rs->rs_lock);
2519 static void ptlrpc_check_rqbd_pool(struct ptlrpc_service_part *svcpt)
2521 int avail = svcpt->scp_nrqbds_posted;
2522 int low_water = test_req_buffer_pressure ? 0 :
2523 svcpt->scp_service->srv_nbuf_per_group / 2;
2525 /* NB I'm not locking; just looking. */
2528 * CAVEAT EMPTOR: We might be allocating buffers here because we've
2529 * allowed the request history to grow out of control. We could put a
2530 * sanity check on that here and cull some history if we need the
2534 if (avail <= low_water)
2535 ptlrpc_grow_req_bufs(svcpt, 1);
2537 if (svcpt->scp_service->srv_stats) {
2538 lprocfs_counter_add(svcpt->scp_service->srv_stats,
2539 PTLRPC_REQBUF_AVAIL_CNTR, avail);
2543 static inline int ptlrpc_threads_enough(struct ptlrpc_service_part *svcpt)
2545 return svcpt->scp_nreqs_active <
2546 svcpt->scp_nthrs_running - 1 -
2547 (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL);
2551 * allowed to create more threads
2552 * user can call it w/o any lock but need to hold
2553 * ptlrpc_service_part::scp_lock to get reliable result
2555 static inline int ptlrpc_threads_increasable(struct ptlrpc_service_part *svcpt)
2557 return svcpt->scp_nthrs_running +
2558 svcpt->scp_nthrs_starting <
2559 svcpt->scp_service->srv_nthrs_cpt_limit;
2563 * too many requests and allowed to create more threads
2565 static inline int ptlrpc_threads_need_create(struct ptlrpc_service_part *svcpt)
2567 return !ptlrpc_threads_enough(svcpt) &&
2568 ptlrpc_threads_increasable(svcpt);
2571 static inline int ptlrpc_thread_stopping(struct ptlrpc_thread *thread)
2573 return thread_is_stopping(thread) ||
2574 thread->t_svcpt->scp_service->srv_is_stopping;
2577 /* stop the highest numbered thread if there are too many threads running */
2578 static inline bool ptlrpc_thread_should_stop(struct ptlrpc_thread *thread)
2580 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2582 return thread->t_id >= svcpt->scp_service->srv_nthrs_cpt_limit &&
2583 thread->t_id == svcpt->scp_thr_nextid - 1;
2586 static void ptlrpc_stop_thread(struct ptlrpc_thread *thread)
2588 CDEBUG(D_INFO, "Stopping thread %s #%u\n",
2589 thread->t_svcpt->scp_service->srv_thread_name, thread->t_id);
2590 thread_add_flags(thread, SVC_STOPPING);
2593 static inline void ptlrpc_thread_stop(struct ptlrpc_thread *thread)
2595 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2597 spin_lock(&svcpt->scp_lock);
2598 if (ptlrpc_thread_should_stop(thread)) {
2599 ptlrpc_stop_thread(thread);
2600 svcpt->scp_thr_nextid--;
2602 spin_unlock(&svcpt->scp_lock);
2605 static inline int ptlrpc_rqbd_pending(struct ptlrpc_service_part *svcpt)
2607 return !list_empty(&svcpt->scp_rqbd_idle) &&
2608 svcpt->scp_rqbd_timeout == 0;
2612 ptlrpc_at_check(struct ptlrpc_service_part *svcpt)
2614 return svcpt->scp_at_check;
2618 * If a thread runs too long or spends to much time on a single request,
2619 * we want to know about it, so we set up a delayed work item as a watchdog.
2620 * If it fires, we display a stack trace of the delayed thread,
2621 * providing we aren't rate-limited
2623 * Watchdog stack traces are limited to 3 per 'libcfs_watchdog_ratelimit'
2626 static struct ratelimit_state watchdog_limit;
2628 static void ptlrpc_watchdog_fire(struct work_struct *w)
2630 struct ptlrpc_thread *thread = container_of(w, struct ptlrpc_thread,
2632 u64 ms_lapse = ktime_ms_delta(ktime_get(), thread->t_touched);
2633 u32 ms_frac = do_div(ms_lapse, MSEC_PER_SEC);
2635 /* ___ratelimit() returns true if the action is NOT ratelimited */
2636 if (__ratelimit(&watchdog_limit)) {
2637 /* below message is checked in sanity-quota.sh test_6,18 */
2638 LCONSOLE_WARN("%s: service thread pid %u was inactive for %llu.%03u seconds. The thread might be hung, or it might only be slow and will resume later. Dumping the stack trace for debugging purposes:\n",
2639 thread->t_task->comm, thread->t_task->pid,
2642 libcfs_debug_dumpstack(thread->t_task);
2644 /* below message is checked in sanity-quota.sh test_6,18 */
2645 LCONSOLE_WARN("%s: service thread pid %u was inactive for %llu.%03u seconds. Watchdog stack traces are limited to 3 per %u seconds, skipping this one.\n",
2646 thread->t_task->comm, thread->t_task->pid,
2647 ms_lapse, ms_frac, libcfs_watchdog_ratelimit);
2651 void ptlrpc_watchdog_init(struct delayed_work *work, timeout_t timeout)
2653 INIT_DELAYED_WORK(work, ptlrpc_watchdog_fire);
2654 schedule_delayed_work(work, cfs_time_seconds(timeout));
2657 void ptlrpc_watchdog_disable(struct delayed_work *work)
2659 cancel_delayed_work_sync(work);
2662 void ptlrpc_watchdog_touch(struct delayed_work *work, timeout_t timeout)
2664 struct ptlrpc_thread *thread = container_of(&work->work,
2665 struct ptlrpc_thread,
2667 thread->t_touched = ktime_get();
2668 mod_delayed_work(system_wq, work, cfs_time_seconds(timeout));
2672 * requests wait on preprocessing
2673 * user can call it w/o any lock but need to hold
2674 * ptlrpc_service_part::scp_lock to get reliable result
2677 ptlrpc_server_request_incoming(struct ptlrpc_service_part *svcpt)
2679 return !list_empty(&svcpt->scp_req_incoming);
2682 static __attribute__((__noinline__)) int
2683 ptlrpc_wait_event(struct ptlrpc_service_part *svcpt,
2684 struct ptlrpc_thread *thread)
2686 ptlrpc_watchdog_disable(&thread->t_watchdog);
2690 if (svcpt->scp_rqbd_timeout == 0)
2691 /* Don't exit while there are replies to be handled */
2692 wait_event_idle_exclusive_lifo(
2694 ptlrpc_thread_stopping(thread) ||
2695 ptlrpc_server_request_incoming(svcpt) ||
2696 ptlrpc_server_request_pending(svcpt, false) ||
2697 ptlrpc_rqbd_pending(svcpt) ||
2698 ptlrpc_at_check(svcpt));
2699 else if (wait_event_idle_exclusive_lifo_timeout(
2701 ptlrpc_thread_stopping(thread) ||
2702 ptlrpc_server_request_incoming(svcpt) ||
2703 ptlrpc_server_request_pending(svcpt, false) ||
2704 ptlrpc_rqbd_pending(svcpt) ||
2705 ptlrpc_at_check(svcpt),
2706 svcpt->scp_rqbd_timeout) == 0)
2707 svcpt->scp_rqbd_timeout = 0;
2709 if (ptlrpc_thread_stopping(thread))
2712 ptlrpc_watchdog_touch(&thread->t_watchdog,
2713 ptlrpc_server_get_timeout(svcpt));
2717 #ifdef HAVE_SERVER_SUPPORT
2718 # ifdef HAVE_FLUSH_DELAYED_FPUT
2719 # define cfs_flush_delayed_fput() flush_delayed_fput()
2721 void (*cfs_flush_delayed_fput)(void);
2722 # endif /* HAVE_FLUSH_DELAYED_FPUT */
2723 #else /* !HAVE_SERVER_SUPPORT */
2724 #define cfs_flush_delayed_fput() do {} while (0)
2725 #endif /* HAVE_SERVER_SUPPORT */
2728 * Main thread body for service threads.
2729 * Waits in a loop waiting for new requests to process to appear.
2730 * Every time an incoming requests is added to its queue, a waitq
2731 * is woken up and one of the threads will handle it.
2733 static int ptlrpc_main(void *arg)
2735 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
2736 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2737 struct ptlrpc_service *svc = svcpt->scp_service;
2738 struct ptlrpc_reply_state *rs;
2739 struct group_info *ginfo = NULL;
2741 int counter = 0, rc = 0;
2744 unshare_fs_struct();
2746 thread->t_task = current;
2747 thread->t_pid = current->pid;
2749 if (svc->srv_cpt_bind) {
2750 rc = cfs_cpt_bind(svc->srv_cptable, svcpt->scp_cpt);
2752 CWARN("%s: failed to bind %s on CPT %d\n",
2753 svc->srv_name, thread->t_name, svcpt->scp_cpt);
2757 ginfo = groups_alloc(0);
2759 GOTO(out, rc = -ENOMEM);
2761 set_current_groups(ginfo);
2762 put_group_info(ginfo);
2764 if (svc->srv_ops.so_thr_init != NULL) {
2765 rc = svc->srv_ops.so_thr_init(thread);
2772 GOTO(out_srv_fini, rc = -ENOMEM);
2773 rc = lu_env_add(env);
2777 rc = lu_context_init(&env->le_ctx,
2778 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
2780 GOTO(out_env_remove, rc);
2782 thread->t_env = env;
2783 env->le_ctx.lc_thread = thread;
2784 env->le_ctx.lc_cookie = 0x6;
2786 while (!list_empty(&svcpt->scp_rqbd_idle)) {
2787 rc = ptlrpc_server_post_idle_rqbds(svcpt);
2791 CERROR("Failed to post rqbd for %s on CPT %d: %d\n",
2792 svc->srv_name, svcpt->scp_cpt, rc);
2793 GOTO(out_ctx_fini, rc);
2796 /* Alloc reply state structure for this one */
2797 OBD_ALLOC_LARGE(rs, svc->srv_max_reply_size);
2799 GOTO(out_ctx_fini, rc = -ENOMEM);
2801 spin_lock(&svcpt->scp_lock);
2803 LASSERT(thread_is_starting(thread));
2804 thread_clear_flags(thread, SVC_STARTING);
2806 LASSERT(svcpt->scp_nthrs_starting == 1);
2807 svcpt->scp_nthrs_starting--;
2810 * SVC_STOPPING may already be set here if someone else is trying
2811 * to stop the service while this new thread has been dynamically
2812 * forked. We still set SVC_RUNNING to let our creator know that
2813 * we are now running, however we will exit as soon as possible
2815 thread_add_flags(thread, SVC_RUNNING);
2816 svcpt->scp_nthrs_running++;
2817 spin_unlock(&svcpt->scp_lock);
2819 /* wake up our creator in case he's still waiting. */
2820 wake_up(&thread->t_ctl_waitq);
2822 thread->t_touched = ktime_get();
2823 ptlrpc_watchdog_init(&thread->t_watchdog,
2824 ptlrpc_server_get_timeout(svcpt));
2826 spin_lock(&svcpt->scp_rep_lock);
2827 list_add(&rs->rs_list, &svcpt->scp_rep_idle);
2828 wake_up(&svcpt->scp_rep_waitq);
2829 spin_unlock(&svcpt->scp_rep_lock);
2831 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
2832 svcpt->scp_nthrs_running);
2834 #ifdef HAVE_SERVER_SUPPORT
2835 #ifndef HAVE_FLUSH_DELAYED_FPUT
2836 if (unlikely(cfs_flush_delayed_fput == NULL))
2837 cfs_flush_delayed_fput =
2838 cfs_kallsyms_lookup_name("flush_delayed_fput");
2841 /* XXX maintain a list of all managed devices: insert here */
2842 while (!ptlrpc_thread_stopping(thread)) {
2845 if (ptlrpc_wait_event(svcpt, thread))
2848 ptlrpc_check_rqbd_pool(svcpt);
2850 if (ptlrpc_threads_need_create(svcpt)) {
2851 /* Ignore return code - we tried... */
2852 ptlrpc_start_thread(svcpt, 0);
2856 /* reset le_ses to initial state */
2858 /* Refill the context before execution to make sure
2859 * all thread keys are allocated */
2861 /* Process all incoming reqs before handling any */
2862 if (ptlrpc_server_request_incoming(svcpt)) {
2863 lu_context_enter(&env->le_ctx);
2864 ptlrpc_server_handle_req_in(svcpt, thread);
2865 lu_context_exit(&env->le_ctx);
2867 /* but limit ourselves in case of flood */
2868 if (counter++ < 100)
2874 if (ptlrpc_at_check(svcpt))
2875 ptlrpc_at_check_timed(svcpt);
2877 if (ptlrpc_server_request_pending(svcpt, false)) {
2878 lu_context_enter(&env->le_ctx);
2879 ptlrpc_server_handle_request(svcpt, thread);
2880 lu_context_exit(&env->le_ctx);
2884 if (ptlrpc_rqbd_pending(svcpt) &&
2885 ptlrpc_server_post_idle_rqbds(svcpt) < 0) {
2887 * I just failed to repost request buffers.
2888 * Wait for a timeout (unless something else
2889 * happens) before I try again
2891 svcpt->scp_rqbd_timeout = cfs_time_seconds(1) / 10;
2892 CDEBUG(D_RPCTRACE, "Posted buffers: %d\n",
2893 svcpt->scp_nrqbds_posted);
2897 /* If nothing to do, flush old alloc_file_pseudo() descriptors.
2898 * This has internal atomicity so it is OK to call often.
2899 * We could also do other idle tasks at this time.
2902 cfs_flush_delayed_fput();
2905 * If the number of threads has been tuned downward and this
2906 * thread should be stopped, then stop in reverse order so the
2907 * the threads always have contiguous thread index values.
2909 if (unlikely(ptlrpc_thread_should_stop(thread)))
2910 ptlrpc_thread_stop(thread);
2913 ptlrpc_watchdog_disable(&thread->t_watchdog);
2916 lu_context_fini(&env->le_ctx);
2922 /* deconstruct service thread state created by ptlrpc_start_thread() */
2923 if (svc->srv_ops.so_thr_done != NULL)
2924 svc->srv_ops.so_thr_done(thread);
2926 CDEBUG(D_RPCTRACE, "%s: service thread [%p:%u] %d exiting: rc = %d\n",
2927 thread->t_name, thread, thread->t_pid, thread->t_id, rc);
2928 spin_lock(&svcpt->scp_lock);
2929 if (thread_test_and_clear_flags(thread, SVC_STARTING))
2930 svcpt->scp_nthrs_starting--;
2932 if (thread_test_and_clear_flags(thread, SVC_RUNNING)) {
2933 /* must know immediately */
2934 svcpt->scp_nthrs_running--;
2938 thread_add_flags(thread, SVC_STOPPED);
2940 wake_up(&thread->t_ctl_waitq);
2941 spin_unlock(&svcpt->scp_lock);
2946 static int hrt_dont_sleep(struct ptlrpc_hr_thread *hrt,
2947 struct list_head *replies)
2951 spin_lock(&hrt->hrt_lock);
2953 list_splice_init(&hrt->hrt_queue, replies);
2954 result = ptlrpc_hr.hr_stopping || !list_empty(replies);
2956 spin_unlock(&hrt->hrt_lock);
2961 * Main body of "handle reply" function.
2962 * It processes acked reply states
2964 static int ptlrpc_hr_main(void *arg)
2966 struct ptlrpc_hr_thread *hrt = (struct ptlrpc_hr_thread *)arg;
2967 struct ptlrpc_hr_partition *hrp = hrt->hrt_partition;
2972 unshare_fs_struct();
2977 rc = cfs_cpt_bind(ptlrpc_hr.hr_cpt_table, hrp->hrp_cpt);
2979 char threadname[20];
2981 snprintf(threadname, sizeof(threadname), "ptlrpc_hr%02d_%03d",
2982 hrp->hrp_cpt, hrt->hrt_id);
2983 CWARN("Failed to bind %s on CPT %d of CPT table %p: rc = %d\n",
2984 threadname, hrp->hrp_cpt, ptlrpc_hr.hr_cpt_table, rc);
2987 rc = lu_context_init(&env->le_ctx, LCT_MD_THREAD | LCT_DT_THREAD |
2988 LCT_REMEMBER | LCT_NOREF);
2992 rc = lu_env_add(env);
2994 GOTO(out_ctx_fini, rc);
2996 atomic_inc(&hrp->hrp_nstarted);
2997 wake_up(&ptlrpc_hr.hr_waitq);
2999 while (!ptlrpc_hr.hr_stopping) {
3000 wait_event_idle(hrt->hrt_waitq, hrt_dont_sleep(hrt, &replies));
3002 while (!list_empty(&replies)) {
3003 struct ptlrpc_reply_state *rs;
3005 rs = list_entry(replies.prev,
3006 struct ptlrpc_reply_state,
3008 list_del_init(&rs->rs_list);
3009 /* refill keys if needed */
3011 lu_context_enter(&env->le_ctx);
3012 ptlrpc_handle_rs(rs);
3013 lu_context_exit(&env->le_ctx);
3017 atomic_inc(&hrp->hrp_nstopped);
3018 wake_up(&ptlrpc_hr.hr_waitq);
3022 lu_context_fini(&env->le_ctx);
3028 static void ptlrpc_stop_hr_threads(void)
3030 struct ptlrpc_hr_partition *hrp;
3034 ptlrpc_hr.hr_stopping = 1;
3036 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
3037 if (hrp->hrp_thrs == NULL)
3038 continue; /* uninitialized */
3039 for (j = 0; j < hrp->hrp_nthrs; j++)
3040 wake_up(&hrp->hrp_thrs[j].hrt_waitq);
3043 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
3044 if (hrp->hrp_thrs == NULL)
3045 continue; /* uninitialized */
3046 wait_event(ptlrpc_hr.hr_waitq,
3047 atomic_read(&hrp->hrp_nstopped) ==
3048 atomic_read(&hrp->hrp_nstarted));
3052 static int ptlrpc_start_hr_threads(void)
3054 struct ptlrpc_hr_partition *hrp;
3060 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
3063 for (j = 0; j < hrp->hrp_nthrs; j++) {
3064 struct ptlrpc_hr_thread *hrt = &hrp->hrp_thrs[j];
3065 struct task_struct *task;
3067 task = kthread_run(ptlrpc_hr_main,
3069 "ptlrpc_hr%02d_%03d",
3078 wait_event(ptlrpc_hr.hr_waitq,
3079 atomic_read(&hrp->hrp_nstarted) == j);
3082 CERROR("cannot start reply handler thread %d:%d: rc = %d\n",
3084 ptlrpc_stop_hr_threads();
3092 static void ptlrpc_svcpt_stop_threads(struct ptlrpc_service_part *svcpt)
3094 struct ptlrpc_thread *thread;
3099 CDEBUG(D_INFO, "Stopping threads for service %s\n",
3100 svcpt->scp_service->srv_name);
3102 spin_lock(&svcpt->scp_lock);
3103 /* let the thread know that we would like it to stop asap */
3104 list_for_each_entry(thread, &svcpt->scp_threads, t_link)
3105 ptlrpc_stop_thread(thread);
3107 wake_up_all(&svcpt->scp_waitq);
3109 while ((thread = list_first_entry_or_null(&svcpt->scp_threads,
3110 struct ptlrpc_thread,
3112 if (thread_is_stopped(thread)) {
3113 list_move(&thread->t_link, &zombie);
3116 spin_unlock(&svcpt->scp_lock);
3118 CDEBUG(D_INFO, "waiting for stopping-thread %s #%u\n",
3119 svcpt->scp_service->srv_thread_name, thread->t_id);
3120 wait_event_idle(thread->t_ctl_waitq,
3121 thread_is_stopped(thread));
3123 spin_lock(&svcpt->scp_lock);
3126 spin_unlock(&svcpt->scp_lock);
3128 while ((thread = list_first_entry_or_null(&zombie,
3129 struct ptlrpc_thread,
3131 list_del(&thread->t_link);
3132 OBD_FREE_PTR(thread);
3138 * Stops all threads of a particular service \a svc
3140 static void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
3142 struct ptlrpc_service_part *svcpt;
3147 ptlrpc_service_for_each_part(svcpt, i, svc) {
3148 if (svcpt->scp_service != NULL)
3149 ptlrpc_svcpt_stop_threads(svcpt);
3155 static int ptlrpc_start_threads(struct ptlrpc_service *svc)
3163 /* We require 2 threads min, see note in ptlrpc_server_handle_request */
3164 LASSERT(svc->srv_nthrs_cpt_init >= PTLRPC_NTHRS_INIT);
3166 for (i = 0; i < svc->srv_ncpts; i++) {
3167 for (j = 0; j < svc->srv_nthrs_cpt_init; j++) {
3168 rc = ptlrpc_start_thread(svc->srv_parts[i], 1);
3174 /* We have enough threads, don't start more. b=15759 */
3181 CERROR("cannot start %s thread #%d_%d: rc %d\n",
3182 svc->srv_thread_name, i, j, rc);
3183 ptlrpc_stop_all_threads(svc);
3187 static int ptlrpc_start_thread(struct ptlrpc_service_part *svcpt, int wait)
3189 struct ptlrpc_thread *thread;
3190 struct ptlrpc_service *svc;
3191 struct task_struct *task;
3196 LASSERT(svcpt != NULL);
3198 svc = svcpt->scp_service;
3200 CDEBUG(D_RPCTRACE, "%s[%d] started %d min %d max %d\n",
3201 svc->srv_name, svcpt->scp_cpt, svcpt->scp_nthrs_running,
3202 svc->srv_nthrs_cpt_init, svc->srv_nthrs_cpt_limit);
3205 if (unlikely(svc->srv_is_stopping))
3208 if (!ptlrpc_threads_increasable(svcpt) ||
3209 (CFS_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
3210 svcpt->scp_nthrs_running == svc->srv_nthrs_cpt_init - 1))
3213 OBD_CPT_ALLOC_PTR(thread, svc->srv_cptable, svcpt->scp_cpt);
3216 init_waitqueue_head(&thread->t_ctl_waitq);
3218 spin_lock(&svcpt->scp_lock);
3219 if (!ptlrpc_threads_increasable(svcpt)) {
3220 spin_unlock(&svcpt->scp_lock);
3221 OBD_FREE_PTR(thread);
3225 if (svcpt->scp_nthrs_starting != 0) {
3227 * serialize starting because some modules (obdfilter)
3228 * might require unique and contiguous t_id
3230 LASSERT(svcpt->scp_nthrs_starting == 1);
3231 spin_unlock(&svcpt->scp_lock);
3232 OBD_FREE_PTR(thread);
3234 CDEBUG(D_INFO, "Waiting for creating thread %s #%d\n",
3235 svc->srv_thread_name, svcpt->scp_thr_nextid);
3240 CDEBUG(D_INFO, "Creating thread %s #%d race, retry later\n",
3241 svc->srv_thread_name, svcpt->scp_thr_nextid);
3245 svcpt->scp_nthrs_starting++;
3246 thread->t_id = svcpt->scp_thr_nextid++;
3247 thread_add_flags(thread, SVC_STARTING);
3248 thread->t_svcpt = svcpt;
3250 list_add(&thread->t_link, &svcpt->scp_threads);
3251 spin_unlock(&svcpt->scp_lock);
3253 if (svcpt->scp_cpt >= 0) {
3254 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s%02d_%03d",
3255 svc->srv_thread_name, svcpt->scp_cpt, thread->t_id);
3257 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s_%04d",
3258 svc->srv_thread_name, thread->t_id);
3261 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", thread->t_name);
3262 task = kthread_run(ptlrpc_main, thread, "%s", thread->t_name);
3265 CERROR("cannot start thread '%s': rc = %d\n",
3266 thread->t_name, rc);
3267 spin_lock(&svcpt->scp_lock);
3268 --svcpt->scp_nthrs_starting;
3269 if (thread_is_stopping(thread)) {
3271 * this ptlrpc_thread is being hanled
3272 * by ptlrpc_svcpt_stop_threads now
3274 thread_add_flags(thread, SVC_STOPPED);
3275 wake_up(&thread->t_ctl_waitq);
3276 spin_unlock(&svcpt->scp_lock);
3278 list_del(&thread->t_link);
3279 spin_unlock(&svcpt->scp_lock);
3280 OBD_FREE_PTR(thread);
3288 wait_event_idle(thread->t_ctl_waitq,
3289 thread_is_running(thread) || thread_is_stopped(thread));
3291 rc = thread_is_stopped(thread) ? thread->t_id : 0;
3295 int ptlrpc_hr_init(void)
3297 struct ptlrpc_hr_partition *hrp;
3298 struct ptlrpc_hr_thread *hrt;
3306 memset(&ptlrpc_hr, 0, sizeof(ptlrpc_hr));
3307 ptlrpc_hr.hr_cpt_table = cfs_cpt_tab;
3309 ptlrpc_hr.hr_partitions = cfs_percpt_alloc(ptlrpc_hr.hr_cpt_table,
3311 if (ptlrpc_hr.hr_partitions == NULL)
3314 ratelimit_state_init(&watchdog_limit,
3315 cfs_time_seconds(libcfs_watchdog_ratelimit), 3);
3317 init_waitqueue_head(&ptlrpc_hr.hr_waitq);
3320 weight = cpumask_weight(topology_sibling_cpumask(smp_processor_id()));
3323 cfs_percpt_for_each(hrp, cpt, ptlrpc_hr.hr_partitions) {
3326 atomic_set(&hrp->hrp_nstarted, 0);
3327 atomic_set(&hrp->hrp_nstopped, 0);
3329 hrp->hrp_nthrs = cfs_cpt_weight(ptlrpc_hr.hr_cpt_table, cpt);
3330 hrp->hrp_nthrs /= weight;
3331 if (hrp->hrp_nthrs == 0)
3334 OBD_CPT_ALLOC(hrp->hrp_thrs, ptlrpc_hr.hr_cpt_table, cpt,
3335 hrp->hrp_nthrs * sizeof(*hrt));
3336 if (hrp->hrp_thrs == NULL)
3337 GOTO(out, rc = -ENOMEM);
3339 for (i = 0; i < hrp->hrp_nthrs; i++) {
3340 hrt = &hrp->hrp_thrs[i];
3343 hrt->hrt_partition = hrp;
3344 init_waitqueue_head(&hrt->hrt_waitq);
3345 spin_lock_init(&hrt->hrt_lock);
3346 INIT_LIST_HEAD(&hrt->hrt_queue);
3350 rc = ptlrpc_start_hr_threads();
3357 void ptlrpc_hr_fini(void)
3359 struct ptlrpc_hr_partition *hrp;
3362 if (ptlrpc_hr.hr_partitions == NULL)
3365 ptlrpc_stop_hr_threads();
3367 cfs_percpt_for_each(hrp, cpt, ptlrpc_hr.hr_partitions) {
3369 OBD_FREE_PTR_ARRAY(hrp->hrp_thrs, hrp->hrp_nthrs);
3372 cfs_percpt_free(ptlrpc_hr.hr_partitions);
3373 ptlrpc_hr.hr_partitions = NULL;
3378 * Wait until all already scheduled replies are processed.
3380 static void ptlrpc_wait_replies(struct ptlrpc_service_part *svcpt)
3383 if (wait_event_idle_timeout(
3385 atomic_read(&svcpt->scp_nreps_difficult) == 0,
3386 cfs_time_seconds(10)) > 0)
3388 CWARN("Unexpectedly long timeout %s %p\n",
3389 svcpt->scp_service->srv_name, svcpt->scp_service);
3394 ptlrpc_service_del_atimer(struct ptlrpc_service *svc)
3396 struct ptlrpc_service_part *svcpt;
3399 /* early disarm AT timer... */
3400 ptlrpc_service_for_each_part(svcpt, i, svc) {
3401 if (svcpt->scp_service != NULL)
3402 timer_delete(&svcpt->scp_at_timer);
3407 ptlrpc_service_unlink_rqbd(struct ptlrpc_service *svc)
3409 struct ptlrpc_service_part *svcpt;
3410 struct ptlrpc_request_buffer_desc *rqbd;
3415 * All history will be culled when the next request buffer is
3416 * freed in ptlrpc_service_purge_all()
3418 svc->srv_hist_nrqbds_cpt_max = 0;
3420 rc = LNetClearLazyPortal(svc->srv_req_portal);
3423 ptlrpc_service_for_each_part(svcpt, i, svc) {
3424 if (svcpt->scp_service == NULL)
3428 * Unlink all the request buffers. This forces a 'final'
3429 * event with its 'unlink' flag set for each posted rqbd
3431 list_for_each_entry(rqbd, &svcpt->scp_rqbd_posted,
3433 rc = LNetMDUnlink(rqbd->rqbd_md_h);
3434 LASSERT(rc == 0 || rc == -ENOENT);
3438 ptlrpc_service_for_each_part(svcpt, i, svc) {
3439 if (svcpt->scp_service == NULL)
3443 * Wait for the network to release any buffers
3444 * it's currently filling
3446 spin_lock(&svcpt->scp_lock);
3447 while (svcpt->scp_nrqbds_posted != 0) {
3448 int seconds = PTLRPC_REQ_LONG_UNLINK;
3450 spin_unlock(&svcpt->scp_lock);
3452 * Network access will complete in finite time but
3453 * the HUGE timeout lets us CWARN for visibility
3456 while (seconds > 0 &&
3457 wait_event_idle_timeout(
3459 svcpt->scp_nrqbds_posted == 0,
3460 cfs_time_seconds(1)) == 0)
3463 CWARN("Service %s waiting for request buffers\n",
3464 svcpt->scp_service->srv_name);
3466 spin_lock(&svcpt->scp_lock);
3468 spin_unlock(&svcpt->scp_lock);
3473 ptlrpc_service_purge_all(struct ptlrpc_service *svc)
3475 struct ptlrpc_service_part *svcpt;
3476 struct ptlrpc_request_buffer_desc *rqbd;
3477 struct ptlrpc_request *req;
3478 struct ptlrpc_reply_state *rs;
3481 ptlrpc_service_for_each_part(svcpt, i, svc) {
3482 if (svcpt->scp_service == NULL)
3485 spin_lock(&svcpt->scp_rep_lock);
3486 while ((rs = list_first_entry_or_null(&svcpt->scp_rep_active,
3487 struct ptlrpc_reply_state,
3488 rs_list)) != NULL) {
3489 spin_lock(&rs->rs_lock);
3490 ptlrpc_schedule_difficult_reply(rs);
3491 spin_unlock(&rs->rs_lock);
3493 spin_unlock(&svcpt->scp_rep_lock);
3496 * purge the request queue. NB No new replies (rqbds
3497 * all unlinked) and no service threads, so I'm the only
3498 * thread noodling the request queue now
3500 while ((req = list_first_entry_or_null(&svcpt->scp_req_incoming,
3501 struct ptlrpc_request,
3502 rq_list)) != NULL) {
3503 list_del(&req->rq_list);
3504 svcpt->scp_nreqs_incoming--;
3505 ptlrpc_server_finish_request(svcpt, req);
3508 while (ptlrpc_server_request_pending(svcpt, true)) {
3509 req = ptlrpc_server_request_get(svcpt, true);
3511 ptlrpc_server_finish_active_request(svcpt, req);
3515 * The portal may be shared by several services (eg:OUT_PORTAL).
3516 * So the request could be referenced by other target. So we
3517 * have to wait the ptlrpc_server_drop_request invoked.
3519 * TODO: move the req_buffer as global rather than per service.
3521 spin_lock(&svcpt->scp_lock);
3522 while (!list_empty(&svcpt->scp_rqbd_posted)) {
3523 spin_unlock(&svcpt->scp_lock);
3524 wait_event_idle_timeout(svcpt->scp_waitq,
3525 list_empty(&svcpt->scp_rqbd_posted),
3526 cfs_time_seconds(1));
3527 spin_lock(&svcpt->scp_lock);
3529 spin_unlock(&svcpt->scp_lock);
3531 LASSERT(svcpt->scp_nreqs_incoming == 0);
3532 LASSERT(svcpt->scp_nreqs_active == 0);
3534 * history should have been culled by
3535 * ptlrpc_server_finish_request
3537 LASSERT(svcpt->scp_hist_nrqbds == 0);
3540 * Now free all the request buffers since nothing
3541 * references them any more...
3543 spin_lock(&svcpt->scp_lock);
3544 while ((rqbd = list_first_entry_or_null(&svcpt->scp_rqbd_idle,
3545 struct ptlrpc_request_buffer_desc,
3546 rqbd_list)) != NULL) {
3547 list_del(&rqbd->rqbd_list);
3548 svcpt->scp_nrqbds_total--;
3549 spin_unlock(&svcpt->scp_lock);
3551 ptlrpc_free_rqbd(rqbd);
3552 spin_lock(&svcpt->scp_lock);
3554 spin_unlock(&svcpt->scp_lock);
3556 ptlrpc_wait_replies(svcpt);
3558 while ((rs = list_first_entry_or_null(&svcpt->scp_rep_idle,
3559 struct ptlrpc_reply_state,
3560 rs_list)) != NULL) {
3561 list_del(&rs->rs_list);
3562 OBD_FREE_LARGE(rs, svc->srv_max_reply_size);
3568 ptlrpc_service_free(struct ptlrpc_service *svc)
3570 struct ptlrpc_service_part *svcpt;
3571 struct ptlrpc_at_array *array;
3574 ptlrpc_service_for_each_part(svcpt, i, svc) {
3575 if (svcpt->scp_service == NULL)
3578 /* In case somebody rearmed this in the meantime */
3579 timer_delete(&svcpt->scp_at_timer);
3580 array = &svcpt->scp_at_array;
3582 if (array->paa_reqs_array != NULL) {
3583 OBD_FREE_PTR_ARRAY(array->paa_reqs_array,
3585 array->paa_reqs_array = NULL;
3588 if (array->paa_reqs_count != NULL) {
3589 OBD_FREE_PTR_ARRAY(array->paa_reqs_count,
3591 array->paa_reqs_count = NULL;
3595 ptlrpc_service_for_each_part(svcpt, i, svc)
3596 OBD_FREE_PTR(svcpt);
3598 if (svc->srv_cpts != NULL)
3599 cfs_expr_list_values_free(svc->srv_cpts, svc->srv_ncpts);
3601 OBD_FREE(svc, offsetof(struct ptlrpc_service,
3602 srv_parts[svc->srv_ncpts]));
3605 int ptlrpc_unregister_service(struct ptlrpc_service *service)
3609 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
3611 service->srv_is_stopping = 1;
3613 mutex_lock(&ptlrpc_all_services_mutex);
3614 list_del_init(&service->srv_list);
3615 mutex_unlock(&ptlrpc_all_services_mutex);
3617 ptlrpc_service_del_atimer(service);
3618 ptlrpc_stop_all_threads(service);
3620 ptlrpc_service_unlink_rqbd(service);
3621 ptlrpc_service_purge_all(service);
3622 ptlrpc_service_nrs_cleanup(service);
3624 ptlrpc_lprocfs_unregister_service(service);
3625 ptlrpc_sysfs_unregister_service(service);
3627 ptlrpc_service_free(service);
3631 EXPORT_SYMBOL(ptlrpc_unregister_service);
3634 * Returns 0 if the service is healthy.
3636 * Right now, it just checks to make sure that requests aren't languishing
3637 * in the queue. We'll use this health check to govern whether a node needs
3638 * to be shot, so it's intentionally non-aggressive.
3640 static int ptlrpc_svcpt_health_check(struct ptlrpc_service_part *svcpt)
3642 struct ptlrpc_request *request = NULL;
3643 struct timespec64 right_now;
3644 struct timespec64 timediff;
3645 struct obd_device *obd = NULL;
3647 ktime_get_real_ts64(&right_now);
3649 spin_lock(&svcpt->scp_req_lock);
3650 /* How long has the next entry been waiting? */
3651 if (ptlrpc_server_high_pending(svcpt, true))
3652 request = ptlrpc_nrs_req_peek_nolock(svcpt, true);
3653 else if (ptlrpc_server_normal_pending(svcpt, true))
3654 request = ptlrpc_nrs_req_peek_nolock(svcpt, false);
3656 if (request == NULL) {
3657 spin_unlock(&svcpt->scp_req_lock);
3661 timediff = timespec64_sub(right_now, request->rq_arrival_time);
3662 spin_unlock(&svcpt->scp_req_lock);
3664 if (request->rq_export)
3665 obd = request->rq_export->exp_obd;
3667 if ((timediff.tv_sec) >
3668 (obd_at_off(obd) ? obd_timeout * 3 / 2 : obd_get_at_max(obd))) {
3669 CERROR("%s: unhealthy - request has been waiting %llds\n",
3670 svcpt->scp_service->srv_name, (s64)timediff.tv_sec);
3678 ptlrpc_service_health_check(struct ptlrpc_service *svc)
3680 struct ptlrpc_service_part *svcpt;
3686 ptlrpc_service_for_each_part(svcpt, i, svc) {
3687 int rc = ptlrpc_svcpt_health_check(svcpt);
3694 EXPORT_SYMBOL(ptlrpc_service_health_check);
3697 ptlrpc_server_get_timeout(struct ptlrpc_service_part *svcpt)
3701 if (!obd_at_off(NULL))
3702 at = obd_at_get(NULL, &svcpt->scp_at_estimate);
3704 return svcpt->scp_service->srv_watchdog_factor *
3705 max_t(int, at, obd_timeout);