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, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_RPC
34 #include <linux/kthread.h>
35 #include <obd_support.h>
36 #include <obd_class.h>
37 #include <lustre_net.h>
38 #include <lu_object.h>
39 #include <lnet/types.h>
40 #include "ptlrpc_internal.h"
42 /* The following are visible and mutable through /sys/module/ptlrpc */
43 int test_req_buffer_pressure = 0;
44 module_param(test_req_buffer_pressure, int, 0444);
45 MODULE_PARM_DESC(test_req_buffer_pressure, "set non-zero to put pressure on request buffer pools");
46 module_param(at_min, int, 0644);
47 MODULE_PARM_DESC(at_min, "Adaptive timeout minimum (sec)");
48 module_param(at_max, int, 0644);
49 MODULE_PARM_DESC(at_max, "Adaptive timeout maximum (sec)");
50 module_param(at_history, int, 0644);
51 MODULE_PARM_DESC(at_history,
52 "Adaptive timeouts remember the slowest event that took place within this period (sec)");
53 module_param(at_early_margin, int, 0644);
54 MODULE_PARM_DESC(at_early_margin, "How soon before an RPC deadline to send an early reply");
55 module_param(at_extra, int, 0644);
56 MODULE_PARM_DESC(at_extra, "How much extra time to give with each early reply");
59 static int ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt);
60 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req);
61 static void ptlrpc_at_remove_timed(struct ptlrpc_request *req);
63 /** Holds a list of all PTLRPC services */
64 struct list_head ptlrpc_all_services;
65 /** Used to protect the \e ptlrpc_all_services list */
66 struct mutex ptlrpc_all_services_mutex;
68 static struct ptlrpc_request_buffer_desc *
69 ptlrpc_alloc_rqbd(struct ptlrpc_service_part *svcpt)
71 struct ptlrpc_service *svc = svcpt->scp_service;
72 struct ptlrpc_request_buffer_desc *rqbd;
74 OBD_CPT_ALLOC_PTR(rqbd, svc->srv_cptable, svcpt->scp_cpt);
78 rqbd->rqbd_svcpt = svcpt;
79 rqbd->rqbd_refcount = 0;
80 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
81 rqbd->rqbd_cbid.cbid_arg = rqbd;
82 INIT_LIST_HEAD(&rqbd->rqbd_reqs);
83 OBD_CPT_ALLOC_LARGE(rqbd->rqbd_buffer, svc->srv_cptable,
84 svcpt->scp_cpt, svc->srv_buf_size);
85 if (rqbd->rqbd_buffer == NULL) {
90 spin_lock(&svcpt->scp_lock);
91 list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
92 svcpt->scp_nrqbds_total++;
93 spin_unlock(&svcpt->scp_lock);
99 ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
101 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
103 LASSERT(rqbd->rqbd_refcount == 0);
104 LASSERT(list_empty(&rqbd->rqbd_reqs));
106 spin_lock(&svcpt->scp_lock);
107 list_del(&rqbd->rqbd_list);
108 svcpt->scp_nrqbds_total--;
109 spin_unlock(&svcpt->scp_lock);
111 OBD_FREE_LARGE(rqbd->rqbd_buffer, svcpt->scp_service->srv_buf_size);
116 ptlrpc_grow_req_bufs(struct ptlrpc_service_part *svcpt, int post)
118 struct ptlrpc_service *svc = svcpt->scp_service;
119 struct ptlrpc_request_buffer_desc *rqbd;
123 if (svcpt->scp_rqbd_allocating)
126 spin_lock(&svcpt->scp_lock);
127 /* check again with lock */
128 if (svcpt->scp_rqbd_allocating) {
129 /* NB: we might allow more than one thread in the future */
130 LASSERT(svcpt->scp_rqbd_allocating == 1);
131 spin_unlock(&svcpt->scp_lock);
135 svcpt->scp_rqbd_allocating++;
136 spin_unlock(&svcpt->scp_lock);
139 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. */
142 if (svcpt->scp_nrqbds_posted >= svc->srv_nbuf_per_group)
145 rqbd = ptlrpc_alloc_rqbd(svcpt);
148 CERROR("%s: Can't allocate request buffer\n",
155 spin_lock(&svcpt->scp_lock);
157 LASSERT(svcpt->scp_rqbd_allocating == 1);
158 svcpt->scp_rqbd_allocating--;
160 spin_unlock(&svcpt->scp_lock);
163 "%s: allocate %d new %d-byte reqbufs (%d/%d left), rc = %d\n",
164 svc->srv_name, i, svc->srv_buf_size, svcpt->scp_nrqbds_posted,
165 svcpt->scp_nrqbds_total, rc);
169 rc = ptlrpc_server_post_idle_rqbds(svcpt);
175 * Part of Rep-Ack logic.
176 * Puts a lock and its mode into reply state assotiated to request reply.
179 ptlrpc_save_lock(struct ptlrpc_request *req,
180 struct lustre_handle *lock, int mode, int no_ack)
182 struct ptlrpc_reply_state *rs = req->rq_reply_state;
186 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
188 if (req->rq_export->exp_disconnected) {
189 ldlm_lock_decref(lock, mode);
191 idx = rs->rs_nlocks++;
192 rs->rs_locks[idx] = *lock;
193 rs->rs_modes[idx] = mode;
194 rs->rs_difficult = 1;
195 rs->rs_no_ack = !!no_ack;
198 EXPORT_SYMBOL(ptlrpc_save_lock);
201 struct ptlrpc_hr_partition;
203 struct ptlrpc_hr_thread {
204 int hrt_id; /* thread ID */
206 wait_queue_head_t hrt_waitq;
207 struct list_head hrt_queue; /* RS queue */
208 struct ptlrpc_hr_partition *hrt_partition;
211 struct ptlrpc_hr_partition {
212 /* # of started threads */
213 atomic_t hrp_nstarted;
214 /* # of stopped threads */
215 atomic_t hrp_nstopped;
216 /* cpu partition id */
218 /* round-robin rotor for choosing thread */
220 /* total number of threads on this partition */
223 struct ptlrpc_hr_thread *hrp_thrs;
226 #define HRT_RUNNING 0
227 #define HRT_STOPPING 1
229 struct ptlrpc_hr_service {
230 /* CPU partition table, it's just cfs_cpt_table for now */
231 struct cfs_cpt_table *hr_cpt_table;
232 /** controller sleep waitq */
233 wait_queue_head_t hr_waitq;
234 unsigned int hr_stopping;
235 /** roundrobin rotor for non-affinity service */
236 unsigned int hr_rotor;
238 struct ptlrpc_hr_partition **hr_partitions;
242 struct list_head rsb_replies;
243 unsigned int rsb_n_replies;
244 struct ptlrpc_service_part *rsb_svcpt;
247 /** reply handling service. */
248 static struct ptlrpc_hr_service ptlrpc_hr;
251 * maximum mumber of replies scheduled in one batch
253 #define MAX_SCHEDULED 256
256 * Initialize a reply batch.
260 static void rs_batch_init(struct rs_batch *b)
262 memset(b, 0, sizeof *b);
263 INIT_LIST_HEAD(&b->rsb_replies);
267 * Choose an hr thread to dispatch requests to.
269 static struct ptlrpc_hr_thread *
270 ptlrpc_hr_select(struct ptlrpc_service_part *svcpt)
272 struct ptlrpc_hr_partition *hrp;
275 if (svcpt->scp_cpt >= 0 &&
276 svcpt->scp_service->srv_cptable == ptlrpc_hr.hr_cpt_table) {
277 /* directly match partition */
278 hrp = ptlrpc_hr.hr_partitions[svcpt->scp_cpt];
281 rotor = ptlrpc_hr.hr_rotor++;
282 rotor %= cfs_cpt_number(ptlrpc_hr.hr_cpt_table);
284 hrp = ptlrpc_hr.hr_partitions[rotor];
287 rotor = hrp->hrp_rotor++;
288 return &hrp->hrp_thrs[rotor % hrp->hrp_nthrs];
292 * Dispatch all replies accumulated in the batch to one from
293 * dedicated reply handling threads.
297 static void rs_batch_dispatch(struct rs_batch *b)
299 if (b->rsb_n_replies != 0) {
300 struct ptlrpc_hr_thread *hrt;
302 hrt = ptlrpc_hr_select(b->rsb_svcpt);
304 spin_lock(&hrt->hrt_lock);
305 list_splice_init(&b->rsb_replies, &hrt->hrt_queue);
306 spin_unlock(&hrt->hrt_lock);
308 wake_up(&hrt->hrt_waitq);
309 b->rsb_n_replies = 0;
314 * Add a reply to a batch.
315 * Add one reply object to a batch, schedule batched replies if overload.
320 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
322 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
324 if (svcpt != b->rsb_svcpt || b->rsb_n_replies >= MAX_SCHEDULED) {
325 if (b->rsb_svcpt != NULL) {
326 rs_batch_dispatch(b);
327 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
329 spin_lock(&svcpt->scp_rep_lock);
330 b->rsb_svcpt = svcpt;
332 spin_lock(&rs->rs_lock);
333 rs->rs_scheduled_ever = 1;
334 if (rs->rs_scheduled == 0) {
335 list_move(&rs->rs_list, &b->rsb_replies);
336 rs->rs_scheduled = 1;
339 rs->rs_committed = 1;
340 spin_unlock(&rs->rs_lock);
344 * Reply batch finalization.
345 * Dispatch remaining replies from the batch
346 * and release remaining spinlock.
350 static void rs_batch_fini(struct rs_batch *b)
352 if (b->rsb_svcpt != NULL) {
353 rs_batch_dispatch(b);
354 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
358 #define DECLARE_RS_BATCH(b) struct rs_batch b
362 * Put reply state into a queue for processing because we received
363 * ACK from the client
365 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
367 struct ptlrpc_hr_thread *hrt;
370 LASSERT(list_empty(&rs->rs_list));
372 hrt = ptlrpc_hr_select(rs->rs_svcpt);
374 spin_lock(&hrt->hrt_lock);
375 list_add_tail(&rs->rs_list, &hrt->hrt_queue);
376 spin_unlock(&hrt->hrt_lock);
378 wake_up(&hrt->hrt_waitq);
383 ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs)
387 assert_spin_locked(&rs->rs_svcpt->scp_rep_lock);
388 assert_spin_locked(&rs->rs_lock);
389 LASSERT (rs->rs_difficult);
390 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
392 if (rs->rs_scheduled) { /* being set up or already notified */
397 rs->rs_scheduled = 1;
398 list_del_init(&rs->rs_list);
399 ptlrpc_dispatch_difficult_reply(rs);
402 EXPORT_SYMBOL(ptlrpc_schedule_difficult_reply);
404 void ptlrpc_commit_replies(struct obd_export *exp)
406 struct ptlrpc_reply_state *rs, *nxt;
407 DECLARE_RS_BATCH(batch);
410 rs_batch_init(&batch);
411 /* Find any replies that have been committed and get their service
412 * to attend to complete them. */
414 /* CAVEAT EMPTOR: spinlock ordering!!! */
415 spin_lock(&exp->exp_uncommitted_replies_lock);
416 list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
418 LASSERT (rs->rs_difficult);
419 /* VBR: per-export last_committed */
420 LASSERT(rs->rs_export);
421 if (rs->rs_transno <= exp->exp_last_committed) {
422 list_del_init(&rs->rs_obd_list);
423 rs_batch_add(&batch, rs);
426 spin_unlock(&exp->exp_uncommitted_replies_lock);
427 rs_batch_fini(&batch);
432 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_entry(svcpt->scp_rqbd_idle.next,
447 struct ptlrpc_request_buffer_desc,
449 list_del(&rqbd->rqbd_list);
451 /* assume we will post successfully */
452 svcpt->scp_nrqbds_posted++;
453 list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_posted);
455 spin_unlock(&svcpt->scp_lock);
457 rc = ptlrpc_register_rqbd(rqbd);
464 spin_lock(&svcpt->scp_lock);
466 svcpt->scp_nrqbds_posted--;
467 list_del(&rqbd->rqbd_list);
468 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
470 /* Don't complain if no request buffers are posted right now; LNET
471 * won't drop requests because we set the portal lazy! */
473 spin_unlock(&svcpt->scp_lock);
478 static void ptlrpc_at_timer(unsigned long castmeharder)
480 struct ptlrpc_service_part *svcpt;
482 svcpt = (struct ptlrpc_service_part *)castmeharder;
484 svcpt->scp_at_check = 1;
485 svcpt->scp_at_checktime = cfs_time_current();
486 wake_up(&svcpt->scp_waitq);
490 ptlrpc_server_nthreads_check(struct ptlrpc_service *svc,
491 struct ptlrpc_service_conf *conf)
493 struct ptlrpc_service_thr_conf *tc = &conf->psc_thr;
500 * Common code for estimating & validating threads number.
501 * CPT affinity service could have percpt thread-pool instead
502 * of a global thread-pool, which means user might not always
503 * get the threads number they give it in conf::tc_nthrs_user
504 * even they did set. It's because we need to validate threads
505 * number for each CPT to guarantee each pool will have enough
506 * threads to keep the service healthy.
508 init = PTLRPC_NTHRS_INIT + (svc->srv_ops.so_hpreq_handler != NULL);
509 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 */
513 LASSERT(tc->tc_nthrs_max != 0);
515 if (tc->tc_nthrs_user != 0) {
516 /* In case there is a reason to test a service with many
517 * threads, we give a less strict check here, it can
518 * be up to 8 * nthrs_max */
519 total = min(tc->tc_nthrs_max * 8, tc->tc_nthrs_user);
520 nthrs = total / svc->srv_ncpts;
521 init = max(init, nthrs);
525 total = tc->tc_nthrs_max;
526 if (tc->tc_nthrs_base == 0) {
527 /* don't care about base threads number per partition,
528 * this is most for non-affinity service */
529 nthrs = total / svc->srv_ncpts;
533 nthrs = tc->tc_nthrs_base;
534 if (svc->srv_ncpts == 1) {
537 /* NB: Increase the base number if it's single partition
538 * and total number of cores/HTs is larger or equal to 4.
539 * result will always < 2 * nthrs_base */
540 weight = cfs_cpt_weight(svc->srv_cptable, CFS_CPT_ANY);
541 for (i = 1; (weight >> (i + 1)) != 0 && /* >= 4 cores/HTs */
542 (tc->tc_nthrs_base >> i) != 0; i++)
543 nthrs += tc->tc_nthrs_base >> i;
546 if (tc->tc_thr_factor != 0) {
547 int factor = tc->tc_thr_factor;
551 * User wants to increase number of threads with for
552 * each CPU core/HT, most likely the factor is larger than
553 * one thread/core because service threads are supposed to
554 * be blocked by lock or wait for IO.
557 * Amdahl's law says that adding processors wouldn't give
558 * a linear increasing of parallelism, so it's nonsense to
559 * have too many threads no matter how many cores/HTs
562 if (cfs_cpu_ht_nsiblings(0) > 1) { /* weight is # of HTs */
563 /* depress thread factor for hyper-thread */
564 factor = factor - (factor >> 1) + (factor >> 3);
567 weight = cfs_cpt_weight(svc->srv_cptable, 0);
570 for (; factor > 0 && weight > 0; factor--, weight -= fade)
571 nthrs += min(weight, fade) * factor;
574 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
575 nthrs = max(tc->tc_nthrs_base,
576 tc->tc_nthrs_max / svc->srv_ncpts);
579 nthrs = max(nthrs, tc->tc_nthrs_init);
580 svc->srv_nthrs_cpt_limit = nthrs;
581 svc->srv_nthrs_cpt_init = init;
583 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
584 CDEBUG(D_OTHER, "%s: This service may have more threads (%d) "
585 "than the given soft limit (%d)\n",
586 svc->srv_name, nthrs * svc->srv_ncpts,
592 * Initialize percpt data for a service
595 ptlrpc_service_part_init(struct ptlrpc_service *svc,
596 struct ptlrpc_service_part *svcpt, int cpt)
598 struct ptlrpc_at_array *array;
603 svcpt->scp_cpt = cpt;
604 INIT_LIST_HEAD(&svcpt->scp_threads);
606 /* rqbd and incoming request queue */
607 spin_lock_init(&svcpt->scp_lock);
608 INIT_LIST_HEAD(&svcpt->scp_rqbd_idle);
609 INIT_LIST_HEAD(&svcpt->scp_rqbd_posted);
610 INIT_LIST_HEAD(&svcpt->scp_req_incoming);
611 init_waitqueue_head(&svcpt->scp_waitq);
612 /* history request & rqbd list */
613 INIT_LIST_HEAD(&svcpt->scp_hist_reqs);
614 INIT_LIST_HEAD(&svcpt->scp_hist_rqbds);
616 /* acitve requests and hp requests */
617 spin_lock_init(&svcpt->scp_req_lock);
620 spin_lock_init(&svcpt->scp_rep_lock);
621 INIT_LIST_HEAD(&svcpt->scp_rep_active);
622 INIT_LIST_HEAD(&svcpt->scp_rep_idle);
623 init_waitqueue_head(&svcpt->scp_rep_waitq);
624 atomic_set(&svcpt->scp_nreps_difficult, 0);
626 /* adaptive timeout */
627 spin_lock_init(&svcpt->scp_at_lock);
628 array = &svcpt->scp_at_array;
630 size = at_est2timeout(at_max);
631 array->paa_size = size;
632 array->paa_count = 0;
633 array->paa_deadline = -1;
635 /* allocate memory for scp_at_array (ptlrpc_at_array) */
636 OBD_CPT_ALLOC(array->paa_reqs_array,
637 svc->srv_cptable, cpt, sizeof(struct list_head) * size);
638 if (array->paa_reqs_array == NULL)
641 for (index = 0; index < size; index++)
642 INIT_LIST_HEAD(&array->paa_reqs_array[index]);
644 OBD_CPT_ALLOC(array->paa_reqs_count,
645 svc->srv_cptable, cpt, sizeof(__u32) * size);
646 if (array->paa_reqs_count == NULL)
649 cfs_timer_init(&svcpt->scp_at_timer, ptlrpc_at_timer, svcpt);
650 /* At SOW, service time should be quick; 10s seems generous. If client
651 * timeout is less than this, we'll be sending an early reply. */
652 at_init(&svcpt->scp_at_estimate, 10, 0);
654 /* assign this before call ptlrpc_grow_req_bufs */
655 svcpt->scp_service = svc;
656 /* Now allocate the request buffers, but don't post them now */
657 rc = ptlrpc_grow_req_bufs(svcpt, 0);
658 /* We shouldn't be under memory pressure at startup, so
659 * fail if we can't allocate all our buffers at this time. */
666 if (array->paa_reqs_count != NULL) {
667 OBD_FREE(array->paa_reqs_count, sizeof(__u32) * size);
668 array->paa_reqs_count = NULL;
671 if (array->paa_reqs_array != NULL) {
672 OBD_FREE(array->paa_reqs_array,
673 sizeof(struct list_head) * array->paa_size);
674 array->paa_reqs_array = NULL;
681 * Initialize service on a given portal.
682 * This includes starting serving threads , allocating and posting rqbds and
685 struct ptlrpc_service *
686 ptlrpc_register_service(struct ptlrpc_service_conf *conf,
687 struct proc_dir_entry *proc_entry)
689 struct ptlrpc_service_cpt_conf *cconf = &conf->psc_cpt;
690 struct ptlrpc_service *service;
691 struct ptlrpc_service_part *svcpt;
692 struct cfs_cpt_table *cptable;
700 LASSERT(conf->psc_buf.bc_nbufs > 0);
701 LASSERT(conf->psc_buf.bc_buf_size >=
702 conf->psc_buf.bc_req_max_size + SPTLRPC_MAX_PAYLOAD);
703 LASSERT(conf->psc_thr.tc_ctx_tags != 0);
705 cptable = cconf->cc_cptable;
707 cptable = cfs_cpt_table;
709 if (!conf->psc_thr.tc_cpu_affinity) {
712 ncpts = cfs_cpt_number(cptable);
713 if (cconf->cc_pattern != NULL) {
714 struct cfs_expr_list *el;
716 rc = cfs_expr_list_parse(cconf->cc_pattern,
717 strlen(cconf->cc_pattern),
720 CERROR("%s: invalid CPT pattern string: %s",
721 conf->psc_name, cconf->cc_pattern);
722 RETURN(ERR_PTR(-EINVAL));
725 rc = cfs_expr_list_values(el, ncpts, &cpts);
726 cfs_expr_list_free(el);
728 CERROR("%s: failed to parse CPT array %s: %d\n",
729 conf->psc_name, cconf->cc_pattern, rc);
731 OBD_FREE(cpts, sizeof(*cpts) * ncpts);
732 RETURN(ERR_PTR(rc < 0 ? rc : -EINVAL));
738 OBD_ALLOC(service, offsetof(struct ptlrpc_service, srv_parts[ncpts]));
739 if (service == NULL) {
741 OBD_FREE(cpts, sizeof(*cpts) * ncpts);
742 RETURN(ERR_PTR(-ENOMEM));
745 service->srv_cptable = cptable;
746 service->srv_cpts = cpts;
747 service->srv_ncpts = ncpts;
749 service->srv_cpt_bits = 0; /* it's zero already, easy to read... */
750 while ((1 << service->srv_cpt_bits) < cfs_cpt_number(cptable))
751 service->srv_cpt_bits++;
754 spin_lock_init(&service->srv_lock);
755 service->srv_name = conf->psc_name;
756 service->srv_watchdog_factor = conf->psc_watchdog_factor;
757 INIT_LIST_HEAD(&service->srv_list); /* for safty of cleanup */
759 /* buffer configuration */
760 service->srv_nbuf_per_group = test_req_buffer_pressure ?
761 1 : conf->psc_buf.bc_nbufs;
762 service->srv_max_req_size = conf->psc_buf.bc_req_max_size +
764 service->srv_buf_size = conf->psc_buf.bc_buf_size;
765 service->srv_rep_portal = conf->psc_buf.bc_rep_portal;
766 service->srv_req_portal = conf->psc_buf.bc_req_portal;
768 /* Increase max reply size to next power of two */
769 service->srv_max_reply_size = 1;
770 while (service->srv_max_reply_size <
771 conf->psc_buf.bc_rep_max_size + SPTLRPC_MAX_PAYLOAD)
772 service->srv_max_reply_size <<= 1;
774 service->srv_thread_name = conf->psc_thr.tc_thr_name;
775 service->srv_ctx_tags = conf->psc_thr.tc_ctx_tags;
776 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
777 service->srv_ops = conf->psc_ops;
779 for (i = 0; i < ncpts; i++) {
780 if (!conf->psc_thr.tc_cpu_affinity)
783 cpt = cpts != NULL ? cpts[i] : i;
785 OBD_CPT_ALLOC(svcpt, cptable, cpt, sizeof(*svcpt));
787 GOTO(failed, rc = -ENOMEM);
789 service->srv_parts[i] = svcpt;
790 rc = ptlrpc_service_part_init(service, svcpt, cpt);
795 ptlrpc_server_nthreads_check(service, conf);
797 rc = LNetSetLazyPortal(service->srv_req_portal);
800 mutex_lock(&ptlrpc_all_services_mutex);
801 list_add(&service->srv_list, &ptlrpc_all_services);
802 mutex_unlock(&ptlrpc_all_services_mutex);
804 if (proc_entry != NULL)
805 ptlrpc_lprocfs_register_service(proc_entry, service);
807 rc = ptlrpc_service_nrs_setup(service);
811 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
812 service->srv_name, service->srv_req_portal);
814 rc = ptlrpc_start_threads(service);
816 CERROR("Failed to start threads for service %s: %d\n",
817 service->srv_name, rc);
823 ptlrpc_unregister_service(service);
826 EXPORT_SYMBOL(ptlrpc_register_service);
829 * to actually free the request, must be called without holding svc_lock.
830 * note it's caller's responsibility to unlink req->rq_list.
832 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
834 LASSERT(atomic_read(&req->rq_refcount) == 0);
835 LASSERT(list_empty(&req->rq_timed_list));
837 /* DEBUG_REQ() assumes the reply state of a request with a valid
838 * ref will not be destroyed until that reference is dropped. */
839 ptlrpc_req_drop_rs(req);
841 sptlrpc_svc_ctx_decref(req);
843 if (req != &req->rq_rqbd->rqbd_req) {
844 /* NB request buffers use an embedded
845 * req if the incoming req unlinked the
846 * MD; this isn't one of them! */
847 ptlrpc_request_cache_free(req);
852 * drop a reference count of the request. if it reaches 0, we either
853 * put it into history list, or free it immediately.
855 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
857 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
858 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
859 struct ptlrpc_service *svc = svcpt->scp_service;
861 struct list_head *tmp;
862 struct list_head *nxt;
864 if (!atomic_dec_and_test(&req->rq_refcount))
867 if (req->rq_session.lc_state == LCS_ENTERED) {
868 lu_context_exit(&req->rq_session);
869 lu_context_fini(&req->rq_session);
872 if (req->rq_at_linked) {
873 spin_lock(&svcpt->scp_at_lock);
874 /* recheck with lock, in case it's unlinked by
875 * ptlrpc_at_check_timed() */
876 if (likely(req->rq_at_linked))
877 ptlrpc_at_remove_timed(req);
878 spin_unlock(&svcpt->scp_at_lock);
881 LASSERT(list_empty(&req->rq_timed_list));
883 /* finalize request */
884 if (req->rq_export) {
885 class_export_put(req->rq_export);
886 req->rq_export = NULL;
889 spin_lock(&svcpt->scp_lock);
891 list_add(&req->rq_list, &rqbd->rqbd_reqs);
893 refcount = --(rqbd->rqbd_refcount);
895 /* request buffer is now idle: add to history */
896 list_del(&rqbd->rqbd_list);
898 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_hist_rqbds);
899 svcpt->scp_hist_nrqbds++;
901 /* cull some history?
902 * I expect only about 1 or 2 rqbds need to be recycled here */
903 while (svcpt->scp_hist_nrqbds > svc->srv_hist_nrqbds_cpt_max) {
904 rqbd = list_entry(svcpt->scp_hist_rqbds.next,
905 struct ptlrpc_request_buffer_desc,
908 list_del(&rqbd->rqbd_list);
909 svcpt->scp_hist_nrqbds--;
911 /* remove rqbd's reqs from svc's req history while
912 * I've got the service lock */
913 list_for_each(tmp, &rqbd->rqbd_reqs) {
914 req = list_entry(tmp, struct ptlrpc_request,
916 /* Track the highest culled req seq */
917 if (req->rq_history_seq >
918 svcpt->scp_hist_seq_culled) {
919 svcpt->scp_hist_seq_culled =
922 list_del(&req->rq_history_list);
925 spin_unlock(&svcpt->scp_lock);
927 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
928 req = list_entry(rqbd->rqbd_reqs.next,
929 struct ptlrpc_request,
931 list_del(&req->rq_list);
932 ptlrpc_server_free_request(req);
935 spin_lock(&svcpt->scp_lock);
937 * now all reqs including the embedded req has been
938 * disposed, schedule request buffer for re-use.
940 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
941 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
944 spin_unlock(&svcpt->scp_lock);
945 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
946 /* If we are low on memory, we are not interested in history */
947 list_del(&req->rq_list);
948 list_del_init(&req->rq_history_list);
950 /* Track the highest culled req seq */
951 if (req->rq_history_seq > svcpt->scp_hist_seq_culled)
952 svcpt->scp_hist_seq_culled = req->rq_history_seq;
954 spin_unlock(&svcpt->scp_lock);
956 ptlrpc_server_free_request(req);
958 spin_unlock(&svcpt->scp_lock);
962 /** Change request export and move hp request from old export to new */
963 void ptlrpc_request_change_export(struct ptlrpc_request *req,
964 struct obd_export *export)
966 if (req->rq_export != NULL) {
967 LASSERT(!list_empty(&req->rq_exp_list));
968 /* remove rq_exp_list from last export */
969 spin_lock_bh(&req->rq_export->exp_rpc_lock);
970 list_del_init(&req->rq_exp_list);
971 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
972 /* export has one reference already, so it`s safe to
973 * add req to export queue here and get another
974 * reference for request later */
975 spin_lock_bh(&export->exp_rpc_lock);
976 if (req->rq_ops != NULL) /* hp request */
977 list_add(&req->rq_exp_list, &export->exp_hp_rpcs);
979 list_add(&req->rq_exp_list, &export->exp_reg_rpcs);
980 spin_unlock_bh(&export->exp_rpc_lock);
982 class_export_rpc_dec(req->rq_export);
983 class_export_put(req->rq_export);
986 /* request takes one export refcount */
987 req->rq_export = class_export_get(export);
988 class_export_rpc_inc(export);
994 * to finish a request: stop sending more early replies, and release
997 static void ptlrpc_server_finish_request(struct ptlrpc_service_part *svcpt,
998 struct ptlrpc_request *req)
1000 ptlrpc_server_hpreq_fini(req);
1002 ptlrpc_server_drop_request(req);
1006 * to finish an active request: stop sending more early replies, and release
1007 * the request. should be called after we finished handling the request.
1009 static void ptlrpc_server_finish_active_request(
1010 struct ptlrpc_service_part *svcpt,
1011 struct ptlrpc_request *req)
1013 spin_lock(&svcpt->scp_req_lock);
1014 ptlrpc_nrs_req_stop_nolock(req);
1015 svcpt->scp_nreqs_active--;
1017 svcpt->scp_nhreqs_active--;
1018 spin_unlock(&svcpt->scp_req_lock);
1020 ptlrpc_nrs_req_finalize(req);
1022 if (req->rq_export != NULL)
1023 class_export_rpc_dec(req->rq_export);
1025 ptlrpc_server_finish_request(svcpt, req);
1029 * This function makes sure dead exports are evicted in a timely manner.
1030 * This function is only called when some export receives a message (i.e.,
1031 * the network is up.)
1033 void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
1035 struct obd_export *oldest_exp;
1036 time_t oldest_time, new_time;
1042 /* Compensate for slow machines, etc, by faking our request time
1043 into the future. Although this can break the strict time-ordering
1044 of the list, we can be really lazy here - we don't have to evict
1045 at the exact right moment. Eventually, all silent exports
1046 will make it to the top of the list. */
1048 /* Do not pay attention on 1sec or smaller renewals. */
1049 new_time = cfs_time_current_sec() + extra_delay;
1050 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
1053 exp->exp_last_request_time = new_time;
1055 /* exports may get disconnected from the chain even though the
1056 export has references, so we must keep the spin lock while
1057 manipulating the lists */
1058 spin_lock(&exp->exp_obd->obd_dev_lock);
1060 if (list_empty(&exp->exp_obd_chain_timed)) {
1061 /* this one is not timed */
1062 spin_unlock(&exp->exp_obd->obd_dev_lock);
1066 list_move_tail(&exp->exp_obd_chain_timed,
1067 &exp->exp_obd->obd_exports_timed);
1069 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
1070 struct obd_export, exp_obd_chain_timed);
1071 oldest_time = oldest_exp->exp_last_request_time;
1072 spin_unlock(&exp->exp_obd->obd_dev_lock);
1074 if (exp->exp_obd->obd_recovering) {
1075 /* be nice to everyone during recovery */
1080 /* Note - racing to start/reset the obd_eviction timer is safe */
1081 if (exp->exp_obd->obd_eviction_timer == 0) {
1082 /* Check if the oldest entry is expired. */
1083 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
1085 /* We need a second timer, in case the net was down and
1086 * it just came back. Since the pinger may skip every
1087 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
1088 * we better wait for 3. */
1089 exp->exp_obd->obd_eviction_timer =
1090 cfs_time_current_sec() + 3 * PING_INTERVAL;
1091 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
1092 exp->exp_obd->obd_name,
1093 obd_export_nid2str(oldest_exp), oldest_time);
1096 if (cfs_time_current_sec() >
1097 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
1098 /* The evictor won't evict anyone who we've heard from
1099 * recently, so we don't have to check before we start
1101 if (!ping_evictor_wake(exp))
1102 exp->exp_obd->obd_eviction_timer = 0;
1110 * Sanity check request \a req.
1111 * Return 0 if all is ok, error code otherwise.
1113 static int ptlrpc_check_req(struct ptlrpc_request *req)
1115 struct obd_device *obd = req->rq_export->exp_obd;
1118 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
1119 req->rq_export->exp_conn_cnt)) {
1120 DEBUG_REQ(D_RPCTRACE, req,
1121 "DROPPING req from old connection %d < %d",
1122 lustre_msg_get_conn_cnt(req->rq_reqmsg),
1123 req->rq_export->exp_conn_cnt);
1126 if (unlikely(obd == NULL || obd->obd_fail)) {
1127 /* Failing over, don't handle any more reqs,
1128 * send error response instead. */
1129 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
1130 req, (obd != NULL) ? obd->obd_name : "unknown");
1132 } else if (lustre_msg_get_flags(req->rq_reqmsg) &
1133 (MSG_REPLAY | MSG_REQ_REPLAY_DONE) &&
1134 !obd->obd_recovering) {
1135 DEBUG_REQ(D_ERROR, req,
1136 "Invalid replay without recovery");
1137 class_fail_export(req->rq_export);
1139 } else if (lustre_msg_get_transno(req->rq_reqmsg) != 0 &&
1140 !obd->obd_recovering) {
1141 DEBUG_REQ(D_ERROR, req, "Invalid req with transno "
1142 "%llu without recovery",
1143 lustre_msg_get_transno(req->rq_reqmsg));
1144 class_fail_export(req->rq_export);
1148 if (unlikely(rc < 0)) {
1149 req->rq_status = rc;
1155 static void ptlrpc_at_set_timer(struct ptlrpc_service_part *svcpt)
1157 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1160 if (array->paa_count == 0) {
1161 cfs_timer_disarm(&svcpt->scp_at_timer);
1165 /* Set timer for closest deadline */
1166 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
1169 ptlrpc_at_timer((unsigned long)svcpt);
1171 cfs_timer_arm(&svcpt->scp_at_timer, cfs_time_shift(next));
1172 CDEBUG(D_INFO, "armed %s at %+ds\n",
1173 svcpt->scp_service->srv_name, next);
1177 /* Add rpc to early reply check list */
1178 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
1180 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1181 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1182 struct ptlrpc_request *rq = NULL;
1188 if (req->rq_no_reply)
1191 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
1194 spin_lock(&svcpt->scp_at_lock);
1195 LASSERT(list_empty(&req->rq_timed_list));
1197 index = (unsigned long)req->rq_deadline % array->paa_size;
1198 if (array->paa_reqs_count[index] > 0) {
1199 /* latest rpcs will have the latest deadlines in the list,
1200 * so search backward. */
1201 list_for_each_entry_reverse(rq,
1202 &array->paa_reqs_array[index],
1204 if (req->rq_deadline >= rq->rq_deadline) {
1205 list_add(&req->rq_timed_list,
1206 &rq->rq_timed_list);
1212 /* Add the request at the head of the list */
1213 if (list_empty(&req->rq_timed_list))
1214 list_add(&req->rq_timed_list,
1215 &array->paa_reqs_array[index]);
1217 spin_lock(&req->rq_lock);
1218 req->rq_at_linked = 1;
1219 spin_unlock(&req->rq_lock);
1220 req->rq_at_index = index;
1221 array->paa_reqs_count[index]++;
1223 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
1224 array->paa_deadline = req->rq_deadline;
1225 ptlrpc_at_set_timer(svcpt);
1227 spin_unlock(&svcpt->scp_at_lock);
1233 ptlrpc_at_remove_timed(struct ptlrpc_request *req)
1235 struct ptlrpc_at_array *array;
1237 array = &req->rq_rqbd->rqbd_svcpt->scp_at_array;
1239 /* NB: must call with hold svcpt::scp_at_lock */
1240 LASSERT(!list_empty(&req->rq_timed_list));
1241 list_del_init(&req->rq_timed_list);
1243 spin_lock(&req->rq_lock);
1244 req->rq_at_linked = 0;
1245 spin_unlock(&req->rq_lock);
1247 array->paa_reqs_count[req->rq_at_index]--;
1252 * Attempt to extend the request deadline by sending an early reply to the
1255 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
1257 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1258 struct ptlrpc_request *reqcopy;
1259 struct lustre_msg *reqmsg;
1260 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
1266 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_RECONNECT)) {
1267 /* don't send early reply */
1271 /* deadline is when the client expects us to reply, margin is the
1272 difference between clients' and servers' expectations */
1273 DEBUG_REQ(D_ADAPTTO, req,
1274 "%ssending early reply (deadline %+lds, margin %+lds) for "
1275 "%d+%d", AT_OFF ? "AT off - not " : "",
1276 olddl, olddl - at_get(&svcpt->scp_at_estimate),
1277 at_get(&svcpt->scp_at_estimate), at_extra);
1283 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
1284 "not sending early reply. Consider increasing "
1285 "at_early_margin (%d)?", olddl, at_early_margin);
1287 /* Return an error so we're not re-added to the timed list. */
1291 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
1292 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
1293 "but no AT support");
1297 if (req->rq_export &&
1298 lustre_msg_get_flags(req->rq_reqmsg) &
1299 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
1300 /* During recovery, we don't want to send too many early
1301 * replies, but on the other hand we want to make sure the
1302 * client has enough time to resend if the rpc is lost. So
1303 * during the recovery period send at least 4 early replies,
1304 * spacing them every at_extra if we can. at_estimate should
1305 * always equal this fixed value during recovery. */
1306 /* Don't account request processing time into AT history
1307 * during recovery, it is not service time we need but
1308 * includes also waiting time for recovering clients */
1309 newdl = cfs_time_current_sec() + min(at_extra,
1310 req->rq_export->exp_obd->obd_recovery_timeout / 4);
1312 /* We want to extend the request deadline by at_extra seconds,
1313 * so we set our service estimate to reflect how much time has
1314 * passed since this request arrived plus an additional
1315 * at_extra seconds. The client will calculate the new deadline
1316 * based on this service estimate (plus some additional time to
1317 * account for network latency). See ptlrpc_at_recv_early_reply
1319 at_measured(&svcpt->scp_at_estimate, at_extra +
1320 cfs_time_current_sec() -
1321 req->rq_arrival_time.tv_sec);
1322 newdl = req->rq_arrival_time.tv_sec +
1323 at_get(&svcpt->scp_at_estimate);
1326 /* Check to see if we've actually increased the deadline -
1327 * we may be past adaptive_max */
1328 if (req->rq_deadline >= newdl) {
1329 DEBUG_REQ(D_WARNING, req, "Couldn't add any time "
1330 "(%ld/%ld), not sending early reply\n",
1331 olddl, newdl - cfs_time_current_sec());
1335 reqcopy = ptlrpc_request_cache_alloc(GFP_NOFS);
1336 if (reqcopy == NULL)
1338 OBD_ALLOC_LARGE(reqmsg, req->rq_reqlen);
1340 GOTO(out_free, rc = -ENOMEM);
1343 reqcopy->rq_reply_state = NULL;
1344 reqcopy->rq_rep_swab_mask = 0;
1345 reqcopy->rq_pack_bulk = 0;
1346 reqcopy->rq_pack_udesc = 0;
1347 reqcopy->rq_packed_final = 0;
1348 sptlrpc_svc_ctx_addref(reqcopy);
1349 /* We only need the reqmsg for the magic */
1350 reqcopy->rq_reqmsg = reqmsg;
1351 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1354 * tgt_brw_read() and tgt_brw_write() may have decided not to reply.
1355 * Without this check, we would fail the rq_no_reply assertion in
1356 * ptlrpc_send_reply().
1358 if (reqcopy->rq_no_reply)
1359 GOTO(out, rc = -ETIMEDOUT);
1361 LASSERT(atomic_read(&req->rq_refcount));
1362 /** if it is last refcount then early reply isn't needed */
1363 if (atomic_read(&req->rq_refcount) == 1) {
1364 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
1365 "abort sending early reply\n");
1366 GOTO(out, rc = -EINVAL);
1369 /* Connection ref */
1370 reqcopy->rq_export = class_conn2export(
1371 lustre_msg_get_handle(reqcopy->rq_reqmsg));
1372 if (reqcopy->rq_export == NULL)
1373 GOTO(out, rc = -ENODEV);
1376 class_export_rpc_inc(reqcopy->rq_export);
1377 if (reqcopy->rq_export->exp_obd &&
1378 reqcopy->rq_export->exp_obd->obd_fail)
1379 GOTO(out_put, rc = -ENODEV);
1381 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1385 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1388 /* Adjust our own deadline to what we told the client */
1389 req->rq_deadline = newdl;
1390 req->rq_early_count++; /* number sent, server side */
1392 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1395 /* Free the (early) reply state from lustre_pack_reply.
1396 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1397 ptlrpc_req_drop_rs(reqcopy);
1400 class_export_rpc_dec(reqcopy->rq_export);
1401 class_export_put(reqcopy->rq_export);
1403 sptlrpc_svc_ctx_decref(reqcopy);
1404 OBD_FREE_LARGE(reqmsg, req->rq_reqlen);
1406 ptlrpc_request_cache_free(reqcopy);
1410 /* Send early replies to everybody expiring within at_early_margin
1411 asking for at_extra time */
1412 static int ptlrpc_at_check_timed(struct ptlrpc_service_part *svcpt)
1414 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1415 struct ptlrpc_request *rq, *n;
1416 struct list_head work_list;
1419 time_t now = cfs_time_current_sec();
1420 cfs_duration_t delay;
1421 int first, counter = 0;
1424 spin_lock(&svcpt->scp_at_lock);
1425 if (svcpt->scp_at_check == 0) {
1426 spin_unlock(&svcpt->scp_at_lock);
1429 delay = cfs_time_sub(cfs_time_current(), svcpt->scp_at_checktime);
1430 svcpt->scp_at_check = 0;
1432 if (array->paa_count == 0) {
1433 spin_unlock(&svcpt->scp_at_lock);
1437 /* The timer went off, but maybe the nearest rpc already completed. */
1438 first = array->paa_deadline - now;
1439 if (first > at_early_margin) {
1440 /* We've still got plenty of time. Reset the timer. */
1441 ptlrpc_at_set_timer(svcpt);
1442 spin_unlock(&svcpt->scp_at_lock);
1446 /* We're close to a timeout, and we don't know how much longer the
1447 server will take. Send early replies to everyone expiring soon. */
1448 INIT_LIST_HEAD(&work_list);
1450 index = (unsigned long)array->paa_deadline % array->paa_size;
1451 count = array->paa_count;
1453 count -= array->paa_reqs_count[index];
1454 list_for_each_entry_safe(rq, n,
1455 &array->paa_reqs_array[index],
1457 if (rq->rq_deadline > now + at_early_margin) {
1458 /* update the earliest deadline */
1459 if (deadline == -1 ||
1460 rq->rq_deadline < deadline)
1461 deadline = rq->rq_deadline;
1465 ptlrpc_at_remove_timed(rq);
1467 * ptlrpc_server_drop_request() may drop
1468 * refcount to 0 already. Let's check this and
1469 * don't add entry to work_list
1471 if (likely(atomic_inc_not_zero(&rq->rq_refcount)))
1472 list_add(&rq->rq_timed_list, &work_list);
1476 if (++index >= array->paa_size)
1479 array->paa_deadline = deadline;
1480 /* we have a new earliest deadline, restart the timer */
1481 ptlrpc_at_set_timer(svcpt);
1483 spin_unlock(&svcpt->scp_at_lock);
1485 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1486 "replies\n", first, at_extra, counter);
1488 /* We're already past request deadlines before we even get a
1489 chance to send early replies */
1490 LCONSOLE_WARN("%s: This server is not able to keep up with "
1491 "request traffic (cpu-bound).\n",
1492 svcpt->scp_service->srv_name);
1493 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1494 "delay="CFS_DURATION_T"(jiff)\n",
1495 counter, svcpt->scp_nreqs_incoming,
1496 svcpt->scp_nreqs_active,
1497 at_get(&svcpt->scp_at_estimate), delay);
1500 /* we took additional refcount so entries can't be deleted from list, no
1501 * locking is needed */
1502 while (!list_empty(&work_list)) {
1503 rq = list_entry(work_list.next, struct ptlrpc_request,
1505 list_del_init(&rq->rq_timed_list);
1507 if (ptlrpc_at_send_early_reply(rq) == 0)
1508 ptlrpc_at_add_timed(rq);
1510 ptlrpc_server_drop_request(rq);
1513 RETURN(1); /* return "did_something" for liblustre */
1516 /* Check if we are already handling earlier incarnation of this request.
1517 * Called under &req->rq_export->exp_rpc_lock locked */
1518 static int ptlrpc_server_check_resend_in_progress(struct ptlrpc_request *req)
1520 struct ptlrpc_request *tmp = NULL;
1522 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) ||
1523 (atomic_read(&req->rq_export->exp_rpc_count) == 0))
1526 /* bulk request are aborted upon reconnect, don't try to
1528 if (req->rq_bulk_write || req->rq_bulk_read)
1531 /* This list should not be longer than max_requests in
1532 * flights on the client, so it is not all that long.
1533 * Also we only hit this codepath in case of a resent
1534 * request which makes it even more rarely hit */
1535 list_for_each_entry(tmp, &req->rq_export->exp_reg_rpcs,
1537 /* Found duplicate one */
1538 if (tmp->rq_xid == req->rq_xid)
1541 list_for_each_entry(tmp, &req->rq_export->exp_hp_rpcs,
1543 /* Found duplicate one */
1544 if (tmp->rq_xid == req->rq_xid)
1550 DEBUG_REQ(D_HA, req, "Found duplicate req in processing");
1551 DEBUG_REQ(D_HA, tmp, "Request being processed");
1556 * Check if a request should be assigned with a high priority.
1558 * \retval < 0: error occurred
1559 * 0: normal RPC request
1560 * +1: high priority request
1562 static int ptlrpc_server_hpreq_init(struct ptlrpc_service_part *svcpt,
1563 struct ptlrpc_request *req)
1568 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL) {
1569 rc = svcpt->scp_service->srv_ops.so_hpreq_handler(req);
1576 if (req->rq_export != NULL && req->rq_ops != NULL) {
1577 /* Perform request specific check. We should do this
1578 * check before the request is added into exp_hp_rpcs
1579 * list otherwise it may hit swab race at LU-1044. */
1580 if (req->rq_ops->hpreq_check != NULL) {
1581 rc = req->rq_ops->hpreq_check(req);
1582 if (rc == -ESTALE) {
1583 req->rq_status = rc;
1586 /** can only return error,
1587 * 0 for normal request,
1588 * or 1 for high priority request */
1596 /** Remove the request from the export list. */
1597 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req)
1600 if (req->rq_export) {
1601 /* refresh lock timeout again so that client has more
1602 * room to send lock cancel RPC. */
1603 if (req->rq_ops && req->rq_ops->hpreq_fini)
1604 req->rq_ops->hpreq_fini(req);
1606 spin_lock_bh(&req->rq_export->exp_rpc_lock);
1607 list_del_init(&req->rq_exp_list);
1608 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1613 static int ptlrpc_hpreq_check(struct ptlrpc_request *req)
1618 static struct ptlrpc_hpreq_ops ptlrpc_hpreq_common = {
1619 .hpreq_check = ptlrpc_hpreq_check,
1622 /* Hi-Priority RPC check by RPC operation code. */
1623 int ptlrpc_hpreq_handler(struct ptlrpc_request *req)
1625 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1627 /* Check for export to let only reconnects for not yet evicted
1628 * export to become a HP rpc. */
1629 if ((req->rq_export != NULL) &&
1630 (opc == OBD_PING || opc == MDS_CONNECT || opc == OST_CONNECT))
1631 req->rq_ops = &ptlrpc_hpreq_common;
1635 EXPORT_SYMBOL(ptlrpc_hpreq_handler);
1637 static int ptlrpc_server_request_add(struct ptlrpc_service_part *svcpt,
1638 struct ptlrpc_request *req)
1644 rc = ptlrpc_server_hpreq_init(svcpt, req);
1649 ptlrpc_nrs_req_initialize(svcpt, req, hp);
1651 if (req->rq_export != NULL) {
1652 struct obd_export *exp = req->rq_export;
1654 /* do search for duplicated xid and the adding to the list
1656 spin_lock_bh(&exp->exp_rpc_lock);
1657 rc = ptlrpc_server_check_resend_in_progress(req);
1659 spin_unlock_bh(&exp->exp_rpc_lock);
1661 ptlrpc_nrs_req_finalize(req);
1665 if (hp || req->rq_ops != NULL)
1666 list_add(&req->rq_exp_list, &exp->exp_hp_rpcs);
1668 list_add(&req->rq_exp_list, &exp->exp_reg_rpcs);
1669 spin_unlock_bh(&exp->exp_rpc_lock);
1672 /* the current thread is not the processing thread for this request
1673 * since that, but request is in exp_hp_list and can be find there.
1674 * Remove all relations between request and old thread. */
1675 req->rq_svc_thread->t_env->le_ses = NULL;
1676 req->rq_svc_thread = NULL;
1677 req->rq_session.lc_thread = NULL;
1679 ptlrpc_nrs_req_add(svcpt, req, hp);
1685 * Allow to handle high priority request
1686 * User can call it w/o any lock but need to hold
1687 * ptlrpc_service_part::scp_req_lock to get reliable result
1689 static bool ptlrpc_server_allow_high(struct ptlrpc_service_part *svcpt,
1692 int running = svcpt->scp_nthrs_running;
1694 if (!nrs_svcpt_has_hp(svcpt))
1700 if (ptlrpc_nrs_req_throttling_nolock(svcpt, true))
1703 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1704 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1705 /* leave just 1 thread for normal RPCs */
1706 running = PTLRPC_NTHRS_INIT;
1707 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1711 if (svcpt->scp_nreqs_active >= running - 1)
1714 if (svcpt->scp_nhreqs_active == 0)
1717 return !ptlrpc_nrs_req_pending_nolock(svcpt, false) ||
1718 svcpt->scp_hreq_count < svcpt->scp_service->srv_hpreq_ratio;
1721 static bool ptlrpc_server_high_pending(struct ptlrpc_service_part *svcpt,
1724 return ptlrpc_server_allow_high(svcpt, force) &&
1725 ptlrpc_nrs_req_pending_nolock(svcpt, true);
1729 * Only allow normal priority requests on a service that has a high-priority
1730 * queue if forced (i.e. cleanup), if there are other high priority requests
1731 * already being processed (i.e. those threads can service more high-priority
1732 * requests), or if there are enough idle threads that a later thread can do
1733 * a high priority request.
1734 * User can call it w/o any lock but need to hold
1735 * ptlrpc_service_part::scp_req_lock to get reliable result
1737 static bool ptlrpc_server_allow_normal(struct ptlrpc_service_part *svcpt,
1740 int running = svcpt->scp_nthrs_running;
1741 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1742 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1743 /* leave just 1 thread for normal RPCs */
1744 running = PTLRPC_NTHRS_INIT;
1745 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1752 if (ptlrpc_nrs_req_throttling_nolock(svcpt, false))
1755 if (svcpt->scp_nreqs_active < running - 2)
1758 if (svcpt->scp_nreqs_active >= running - 1)
1761 return svcpt->scp_nhreqs_active > 0 || !nrs_svcpt_has_hp(svcpt);
1764 static bool ptlrpc_server_normal_pending(struct ptlrpc_service_part *svcpt,
1767 return ptlrpc_server_allow_normal(svcpt, force) &&
1768 ptlrpc_nrs_req_pending_nolock(svcpt, false);
1772 * Returns true if there are requests available in incoming
1773 * request queue for processing and it is allowed to fetch them.
1774 * User can call it w/o any lock but need to hold ptlrpc_service::scp_req_lock
1775 * to get reliable result
1776 * \see ptlrpc_server_allow_normal
1777 * \see ptlrpc_server_allow high
1780 ptlrpc_server_request_pending(struct ptlrpc_service_part *svcpt, bool force)
1782 return ptlrpc_server_high_pending(svcpt, force) ||
1783 ptlrpc_server_normal_pending(svcpt, force);
1787 * Fetch a request for processing from queue of unprocessed requests.
1788 * Favors high-priority requests.
1789 * Returns a pointer to fetched request.
1791 static struct ptlrpc_request *
1792 ptlrpc_server_request_get(struct ptlrpc_service_part *svcpt, bool force)
1794 struct ptlrpc_request *req = NULL;
1797 spin_lock(&svcpt->scp_req_lock);
1799 if (ptlrpc_server_high_pending(svcpt, force)) {
1800 req = ptlrpc_nrs_req_get_nolock(svcpt, true, force);
1802 svcpt->scp_hreq_count++;
1807 if (ptlrpc_server_normal_pending(svcpt, force)) {
1808 req = ptlrpc_nrs_req_get_nolock(svcpt, false, force);
1810 svcpt->scp_hreq_count = 0;
1815 spin_unlock(&svcpt->scp_req_lock);
1819 svcpt->scp_nreqs_active++;
1821 svcpt->scp_nhreqs_active++;
1823 spin_unlock(&svcpt->scp_req_lock);
1825 if (likely(req->rq_export))
1826 class_export_rpc_inc(req->rq_export);
1832 * Handle freshly incoming reqs, add to timed early reply list,
1833 * pass on to regular request queue.
1834 * All incoming requests pass through here before getting into
1835 * ptlrpc_server_handle_req later on.
1838 ptlrpc_server_handle_req_in(struct ptlrpc_service_part *svcpt,
1839 struct ptlrpc_thread *thread)
1841 struct ptlrpc_service *svc = svcpt->scp_service;
1842 struct ptlrpc_request *req;
1847 spin_lock(&svcpt->scp_lock);
1848 if (list_empty(&svcpt->scp_req_incoming)) {
1849 spin_unlock(&svcpt->scp_lock);
1853 req = list_entry(svcpt->scp_req_incoming.next,
1854 struct ptlrpc_request, rq_list);
1855 list_del_init(&req->rq_list);
1856 svcpt->scp_nreqs_incoming--;
1857 /* Consider this still a "queued" request as far as stats are
1859 spin_unlock(&svcpt->scp_lock);
1861 /* go through security check/transform */
1862 rc = sptlrpc_svc_unwrap_request(req);
1866 case SECSVC_COMPLETE:
1867 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1876 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
1877 * redo it wouldn't be harmful.
1879 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1880 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1882 CERROR("error unpacking request: ptl %d from %s "
1883 "x%llu\n", svc->srv_req_portal,
1884 libcfs_id2str(req->rq_peer), req->rq_xid);
1889 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1891 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1892 "%llu\n", svc->srv_req_portal,
1893 libcfs_id2str(req->rq_peer), req->rq_xid);
1897 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1898 lustre_msg_get_opc(req->rq_reqmsg) == cfs_fail_val) {
1899 CERROR("drop incoming rpc opc %u, x%llu\n",
1900 cfs_fail_val, req->rq_xid);
1905 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1906 CERROR("wrong packet type received (type=%u) from %s\n",
1907 lustre_msg_get_type(req->rq_reqmsg),
1908 libcfs_id2str(req->rq_peer));
1912 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1916 req->rq_bulk_write = 1;
1920 case MGS_CONFIG_READ:
1921 req->rq_bulk_read = 1;
1925 CDEBUG(D_RPCTRACE, "got req x%llu\n", req->rq_xid);
1927 req->rq_export = class_conn2export(
1928 lustre_msg_get_handle(req->rq_reqmsg));
1929 if (req->rq_export) {
1930 rc = ptlrpc_check_req(req);
1932 rc = sptlrpc_target_export_check(req->rq_export, req);
1934 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1935 "illegal security flavor,");
1940 ptlrpc_update_export_timer(req->rq_export, 0);
1943 /* req_in handling should/must be fast */
1944 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1945 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1946 cfs_time_sub(cfs_time_current_sec(),
1947 req->rq_arrival_time.tv_sec));
1949 /* Set rpc server deadline and add it to the timed list */
1950 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1951 MSGHDR_AT_SUPPORT) ?
1952 /* The max time the client expects us to take */
1953 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1955 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1956 if (unlikely(deadline == 0)) {
1957 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1961 /* Skip early reply */
1962 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_RESEND))
1963 req->rq_deadline += obd_timeout;
1965 req->rq_svc_thread = thread;
1966 if (thread != NULL) {
1967 /* initialize request session, it is needed for request
1968 * processing by target */
1969 rc = lu_context_init(&req->rq_session, LCT_SERVER_SESSION |
1972 CERROR("%s: failure to initialize session: rc = %d\n",
1973 thread->t_name, rc);
1976 req->rq_session.lc_thread = thread;
1977 lu_context_enter(&req->rq_session);
1978 thread->t_env->le_ses = &req->rq_session;
1981 ptlrpc_at_add_timed(req);
1983 /* Move it over to the request processing queue */
1984 rc = ptlrpc_server_request_add(svcpt, req);
1988 wake_up(&svcpt->scp_waitq);
1992 ptlrpc_server_finish_request(svcpt, req);
1998 * Main incoming request handling logic.
1999 * Calls handler function from service to do actual processing.
2002 ptlrpc_server_handle_request(struct ptlrpc_service_part *svcpt,
2003 struct ptlrpc_thread *thread)
2005 struct ptlrpc_service *svc = svcpt->scp_service;
2006 struct ptlrpc_request *request;
2007 struct timeval work_start;
2008 struct timeval work_end;
2014 request = ptlrpc_server_request_get(svcpt, false);
2015 if (request == NULL)
2018 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
2019 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
2020 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
2021 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
2023 if (unlikely(fail_opc)) {
2024 if (request->rq_export && request->rq_ops)
2025 OBD_FAIL_TIMEOUT(fail_opc, 4);
2028 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
2030 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
2031 libcfs_debug_dumplog();
2033 do_gettimeofday(&work_start);
2034 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
2035 if (likely(svc->srv_stats != NULL)) {
2036 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
2038 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
2039 svcpt->scp_nreqs_incoming);
2040 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
2041 svcpt->scp_nreqs_active);
2042 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
2043 at_get(&svcpt->scp_at_estimate));
2046 if (likely(request->rq_export)) {
2047 if (unlikely(ptlrpc_check_req(request)))
2049 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
2052 /* Discard requests queued for longer than the deadline.
2053 The deadline is increased if we send an early reply. */
2054 if (cfs_time_current_sec() > request->rq_deadline) {
2055 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
2056 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
2057 libcfs_id2str(request->rq_peer),
2058 cfs_time_sub(request->rq_deadline,
2059 request->rq_arrival_time.tv_sec),
2060 cfs_time_sub(cfs_time_current_sec(),
2061 request->rq_deadline));
2065 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
2066 "%s:%s+%d:%d:x%llu:%s:%d\n", current_comm(),
2067 (request->rq_export ?
2068 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
2069 (request->rq_export ?
2070 atomic_read(&request->rq_export->exp_refcount) : -99),
2071 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
2072 libcfs_id2str(request->rq_peer),
2073 lustre_msg_get_opc(request->rq_reqmsg));
2075 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
2076 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, cfs_fail_val);
2078 CDEBUG(D_NET, "got req %llu\n", request->rq_xid);
2080 /* re-assign request and sesson thread to the current one */
2081 request->rq_svc_thread = thread;
2082 if (thread != NULL) {
2083 LASSERT(request->rq_session.lc_thread == NULL);
2084 request->rq_session.lc_thread = thread;
2085 thread->t_env->le_ses = &request->rq_session;
2087 svc->srv_ops.so_req_handler(request);
2089 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
2092 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
2093 DEBUG_REQ(D_WARNING, request, "Request took longer "
2094 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
2095 " client may timeout.",
2096 cfs_time_sub(request->rq_deadline,
2097 request->rq_arrival_time.tv_sec),
2098 cfs_time_sub(cfs_time_current_sec(),
2099 request->rq_deadline));
2102 do_gettimeofday(&work_end);
2103 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
2104 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
2105 "%s:%s+%d:%d:x%llu:%s:%d Request procesed in "
2106 "%ldus (%ldus total) trans %llu rc %d/%d\n",
2108 (request->rq_export ?
2109 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
2110 (request->rq_export ?
2111 atomic_read(&request->rq_export->exp_refcount) : -99),
2112 lustre_msg_get_status(request->rq_reqmsg),
2114 libcfs_id2str(request->rq_peer),
2115 lustre_msg_get_opc(request->rq_reqmsg),
2117 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
2118 (request->rq_repmsg ?
2119 lustre_msg_get_transno(request->rq_repmsg) :
2120 request->rq_transno),
2122 (request->rq_repmsg ?
2123 lustre_msg_get_status(request->rq_repmsg) : -999));
2124 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
2125 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
2126 int opc = opcode_offset(op);
2127 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
2128 LASSERT(opc < LUSTRE_MAX_OPCODES);
2129 lprocfs_counter_add(svc->srv_stats,
2130 opc + EXTRA_MAX_OPCODES,
2134 if (unlikely(request->rq_early_count)) {
2135 DEBUG_REQ(D_ADAPTTO, request,
2136 "sent %d early replies before finishing in "
2138 request->rq_early_count,
2139 cfs_time_sub(work_end.tv_sec,
2140 request->rq_arrival_time.tv_sec));
2143 ptlrpc_server_finish_active_request(svcpt, request);
2149 * An internal function to process a single reply state object.
2152 ptlrpc_handle_rs(struct ptlrpc_reply_state *rs)
2154 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
2155 struct ptlrpc_service *svc = svcpt->scp_service;
2156 struct obd_export *exp;
2161 exp = rs->rs_export;
2163 LASSERT(rs->rs_difficult);
2164 LASSERT(rs->rs_scheduled);
2165 LASSERT(list_empty(&rs->rs_list));
2167 spin_lock(&exp->exp_lock);
2168 /* Noop if removed already */
2169 list_del_init(&rs->rs_exp_list);
2170 spin_unlock(&exp->exp_lock);
2172 /* The disk commit callback holds exp_uncommitted_replies_lock while it
2173 * iterates over newly committed replies, removing them from
2174 * exp_uncommitted_replies. It then drops this lock and schedules the
2175 * replies it found for handling here.
2177 * We can avoid contention for exp_uncommitted_replies_lock between the
2178 * HRT threads and further commit callbacks by checking rs_committed
2179 * which is set in the commit callback while it holds both
2180 * rs_lock and exp_uncommitted_reples.
2182 * If we see rs_committed clear, the commit callback _may_ not have
2183 * handled this reply yet and we race with it to grab
2184 * exp_uncommitted_replies_lock before removing the reply from
2185 * exp_uncommitted_replies. Note that if we lose the race and the
2186 * reply has already been removed, list_del_init() is a noop.
2188 * If we see rs_committed set, we know the commit callback is handling,
2189 * or has handled this reply since store reordering might allow us to
2190 * see rs_committed set out of sequence. But since this is done
2191 * holding rs_lock, we can be sure it has all completed once we hold
2192 * rs_lock, which we do right next.
2194 if (!rs->rs_committed) {
2195 spin_lock(&exp->exp_uncommitted_replies_lock);
2196 list_del_init(&rs->rs_obd_list);
2197 spin_unlock(&exp->exp_uncommitted_replies_lock);
2200 spin_lock(&rs->rs_lock);
2202 been_handled = rs->rs_handled;
2205 nlocks = rs->rs_nlocks; /* atomic "steal", but */
2206 rs->rs_nlocks = 0; /* locks still on rs_locks! */
2208 if (nlocks == 0 && !been_handled) {
2209 /* If we see this, we should already have seen the warning
2210 * in mds_steal_ack_locks() */
2211 CDEBUG(D_HA, "All locks stolen from rs %p x%lld.t%lld"
2214 rs->rs_xid, rs->rs_transno, rs->rs_opc,
2215 libcfs_nid2str(exp->exp_connection->c_peer.nid));
2218 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
2219 spin_unlock(&rs->rs_lock);
2221 if (!been_handled && rs->rs_on_net) {
2222 LNetMDUnlink(rs->rs_md_h);
2223 /* Ignore return code; we're racing with completion */
2226 while (nlocks-- > 0)
2227 ldlm_lock_decref(&rs->rs_locks[nlocks],
2228 rs->rs_modes[nlocks]);
2230 spin_lock(&rs->rs_lock);
2233 rs->rs_scheduled = 0;
2235 if (!rs->rs_on_net) {
2237 spin_unlock(&rs->rs_lock);
2239 class_export_put (exp);
2240 rs->rs_export = NULL;
2241 ptlrpc_rs_decref(rs);
2242 if (atomic_dec_and_test(&svcpt->scp_nreps_difficult) &&
2243 svc->srv_is_stopping)
2244 wake_up_all(&svcpt->scp_waitq);
2248 /* still on the net; callback will schedule */
2249 spin_unlock(&rs->rs_lock);
2255 ptlrpc_check_rqbd_pool(struct ptlrpc_service_part *svcpt)
2257 int avail = svcpt->scp_nrqbds_posted;
2258 int low_water = test_req_buffer_pressure ? 0 :
2259 svcpt->scp_service->srv_nbuf_per_group / 2;
2261 /* NB I'm not locking; just looking. */
2263 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
2264 * allowed the request history to grow out of control. We could put a
2265 * sanity check on that here and cull some history if we need the
2268 if (avail <= low_water)
2269 ptlrpc_grow_req_bufs(svcpt, 1);
2271 if (svcpt->scp_service->srv_stats) {
2272 lprocfs_counter_add(svcpt->scp_service->srv_stats,
2273 PTLRPC_REQBUF_AVAIL_CNTR, avail);
2278 ptlrpc_retry_rqbds(void *arg)
2280 struct ptlrpc_service_part *svcpt = (struct ptlrpc_service_part *)arg;
2282 svcpt->scp_rqbd_timeout = 0;
2287 ptlrpc_threads_enough(struct ptlrpc_service_part *svcpt)
2289 return svcpt->scp_nreqs_active <
2290 svcpt->scp_nthrs_running - 1 -
2291 (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL);
2295 * allowed to create more threads
2296 * user can call it w/o any lock but need to hold
2297 * ptlrpc_service_part::scp_lock to get reliable result
2300 ptlrpc_threads_increasable(struct ptlrpc_service_part *svcpt)
2302 return svcpt->scp_nthrs_running +
2303 svcpt->scp_nthrs_starting <
2304 svcpt->scp_service->srv_nthrs_cpt_limit;
2308 * too many requests and allowed to create more threads
2311 ptlrpc_threads_need_create(struct ptlrpc_service_part *svcpt)
2313 return !ptlrpc_threads_enough(svcpt) &&
2314 ptlrpc_threads_increasable(svcpt);
2318 ptlrpc_thread_stopping(struct ptlrpc_thread *thread)
2320 return thread_is_stopping(thread) ||
2321 thread->t_svcpt->scp_service->srv_is_stopping;
2325 ptlrpc_rqbd_pending(struct ptlrpc_service_part *svcpt)
2327 return !list_empty(&svcpt->scp_rqbd_idle) &&
2328 svcpt->scp_rqbd_timeout == 0;
2332 ptlrpc_at_check(struct ptlrpc_service_part *svcpt)
2334 return svcpt->scp_at_check;
2338 * requests wait on preprocessing
2339 * user can call it w/o any lock but need to hold
2340 * ptlrpc_service_part::scp_lock to get reliable result
2343 ptlrpc_server_request_incoming(struct ptlrpc_service_part *svcpt)
2345 return !list_empty(&svcpt->scp_req_incoming);
2348 static __attribute__((__noinline__)) int
2349 ptlrpc_wait_event(struct ptlrpc_service_part *svcpt,
2350 struct ptlrpc_thread *thread)
2352 /* Don't exit while there are replies to be handled */
2353 struct l_wait_info lwi = LWI_TIMEOUT(svcpt->scp_rqbd_timeout,
2354 ptlrpc_retry_rqbds, svcpt);
2356 lc_watchdog_disable(thread->t_watchdog);
2360 l_wait_event_exclusive_head(svcpt->scp_waitq,
2361 ptlrpc_thread_stopping(thread) ||
2362 ptlrpc_server_request_incoming(svcpt) ||
2363 ptlrpc_server_request_pending(svcpt, false) ||
2364 ptlrpc_rqbd_pending(svcpt) ||
2365 ptlrpc_at_check(svcpt), &lwi);
2367 if (ptlrpc_thread_stopping(thread))
2370 lc_watchdog_touch(thread->t_watchdog,
2371 ptlrpc_server_get_timeout(svcpt));
2376 * Main thread body for service threads.
2377 * Waits in a loop waiting for new requests to process to appear.
2378 * Every time an incoming requests is added to its queue, a waitq
2379 * is woken up and one of the threads will handle it.
2381 static int ptlrpc_main(void *arg)
2383 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
2384 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2385 struct ptlrpc_service *svc = svcpt->scp_service;
2386 struct ptlrpc_reply_state *rs;
2387 struct group_info *ginfo = NULL;
2389 int counter = 0, rc = 0;
2392 thread->t_pid = current_pid();
2393 unshare_fs_struct();
2395 /* NB: we will call cfs_cpt_bind() for all threads, because we
2396 * might want to run lustre server only on a subset of system CPUs,
2397 * in that case ->scp_cpt is CFS_CPT_ANY */
2398 rc = cfs_cpt_bind(svc->srv_cptable, svcpt->scp_cpt);
2400 CWARN("%s: failed to bind %s on CPT %d\n",
2401 svc->srv_name, thread->t_name, svcpt->scp_cpt);
2404 ginfo = groups_alloc(0);
2410 set_current_groups(ginfo);
2411 put_group_info(ginfo);
2413 if (svc->srv_ops.so_thr_init != NULL) {
2414 rc = svc->srv_ops.so_thr_init(thread);
2425 rc = lu_context_init(&env->le_ctx,
2426 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
2430 thread->t_env = env;
2431 env->le_ctx.lc_thread = thread;
2432 env->le_ctx.lc_cookie = 0x6;
2434 while (!list_empty(&svcpt->scp_rqbd_idle)) {
2435 rc = ptlrpc_server_post_idle_rqbds(svcpt);
2439 CERROR("Failed to post rqbd for %s on CPT %d: %d\n",
2440 svc->srv_name, svcpt->scp_cpt, rc);
2444 /* Alloc reply state structure for this one */
2445 OBD_ALLOC_LARGE(rs, svc->srv_max_reply_size);
2451 spin_lock(&svcpt->scp_lock);
2453 LASSERT(thread_is_starting(thread));
2454 thread_clear_flags(thread, SVC_STARTING);
2456 LASSERT(svcpt->scp_nthrs_starting == 1);
2457 svcpt->scp_nthrs_starting--;
2459 /* SVC_STOPPING may already be set here if someone else is trying
2460 * to stop the service while this new thread has been dynamically
2461 * forked. We still set SVC_RUNNING to let our creator know that
2462 * we are now running, however we will exit as soon as possible */
2463 thread_add_flags(thread, SVC_RUNNING);
2464 svcpt->scp_nthrs_running++;
2465 spin_unlock(&svcpt->scp_lock);
2467 /* wake up our creator in case he's still waiting. */
2468 wake_up(&thread->t_ctl_waitq);
2470 thread->t_watchdog = lc_watchdog_add(ptlrpc_server_get_timeout(svcpt),
2473 spin_lock(&svcpt->scp_rep_lock);
2474 list_add(&rs->rs_list, &svcpt->scp_rep_idle);
2475 wake_up(&svcpt->scp_rep_waitq);
2476 spin_unlock(&svcpt->scp_rep_lock);
2478 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
2479 svcpt->scp_nthrs_running);
2481 /* XXX maintain a list of all managed devices: insert here */
2482 while (!ptlrpc_thread_stopping(thread)) {
2483 if (ptlrpc_wait_event(svcpt, thread))
2486 ptlrpc_check_rqbd_pool(svcpt);
2488 if (ptlrpc_threads_need_create(svcpt)) {
2489 /* Ignore return code - we tried... */
2490 ptlrpc_start_thread(svcpt, 0);
2493 /* reset le_ses to initial state */
2495 /* Process all incoming reqs before handling any */
2496 if (ptlrpc_server_request_incoming(svcpt)) {
2497 lu_context_enter(&env->le_ctx);
2498 ptlrpc_server_handle_req_in(svcpt, thread);
2499 lu_context_exit(&env->le_ctx);
2501 /* but limit ourselves in case of flood */
2502 if (counter++ < 100)
2507 if (ptlrpc_at_check(svcpt))
2508 ptlrpc_at_check_timed(svcpt);
2510 if (ptlrpc_server_request_pending(svcpt, false)) {
2511 lu_context_enter(&env->le_ctx);
2512 ptlrpc_server_handle_request(svcpt, thread);
2513 lu_context_exit(&env->le_ctx);
2516 if (ptlrpc_rqbd_pending(svcpt) &&
2517 ptlrpc_server_post_idle_rqbds(svcpt) < 0) {
2518 /* I just failed to repost request buffers.
2519 * Wait for a timeout (unless something else
2520 * happens) before I try again */
2521 svcpt->scp_rqbd_timeout = cfs_time_seconds(1) / 10;
2522 CDEBUG(D_RPCTRACE, "Posted buffers: %d\n",
2523 svcpt->scp_nrqbds_posted);
2527 lc_watchdog_delete(thread->t_watchdog);
2528 thread->t_watchdog = NULL;
2532 * deconstruct service specific state created by ptlrpc_start_thread()
2534 if (svc->srv_ops.so_thr_done != NULL)
2535 svc->srv_ops.so_thr_done(thread);
2538 lu_context_fini(&env->le_ctx);
2542 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2543 thread, thread->t_pid, thread->t_id, rc);
2545 spin_lock(&svcpt->scp_lock);
2546 if (thread_test_and_clear_flags(thread, SVC_STARTING))
2547 svcpt->scp_nthrs_starting--;
2549 if (thread_test_and_clear_flags(thread, SVC_RUNNING)) {
2550 /* must know immediately */
2551 svcpt->scp_nthrs_running--;
2555 thread_add_flags(thread, SVC_STOPPED);
2557 wake_up(&thread->t_ctl_waitq);
2558 spin_unlock(&svcpt->scp_lock);
2563 static int hrt_dont_sleep(struct ptlrpc_hr_thread *hrt,
2564 struct list_head *replies)
2568 spin_lock(&hrt->hrt_lock);
2570 list_splice_init(&hrt->hrt_queue, replies);
2571 result = ptlrpc_hr.hr_stopping || !list_empty(replies);
2573 spin_unlock(&hrt->hrt_lock);
2578 * Main body of "handle reply" function.
2579 * It processes acked reply states
2581 static int ptlrpc_hr_main(void *arg)
2583 struct ptlrpc_hr_thread *hrt = (struct ptlrpc_hr_thread *)arg;
2584 struct ptlrpc_hr_partition *hrp = hrt->hrt_partition;
2585 struct list_head replies;
2586 char threadname[20];
2589 INIT_LIST_HEAD(&replies);
2590 snprintf(threadname, sizeof(threadname), "ptlrpc_hr%02d_%03d",
2591 hrp->hrp_cpt, hrt->hrt_id);
2592 unshare_fs_struct();
2594 rc = cfs_cpt_bind(ptlrpc_hr.hr_cpt_table, hrp->hrp_cpt);
2596 CWARN("Failed to bind %s on CPT %d of CPT table %p: rc = %d\n",
2597 threadname, hrp->hrp_cpt, ptlrpc_hr.hr_cpt_table, rc);
2600 atomic_inc(&hrp->hrp_nstarted);
2601 wake_up(&ptlrpc_hr.hr_waitq);
2603 while (!ptlrpc_hr.hr_stopping) {
2604 l_wait_condition(hrt->hrt_waitq, hrt_dont_sleep(hrt, &replies));
2606 while (!list_empty(&replies)) {
2607 struct ptlrpc_reply_state *rs;
2609 rs = list_entry(replies.prev,
2610 struct ptlrpc_reply_state,
2612 list_del_init(&rs->rs_list);
2613 ptlrpc_handle_rs(rs);
2617 atomic_inc(&hrp->hrp_nstopped);
2618 wake_up(&ptlrpc_hr.hr_waitq);
2623 static void ptlrpc_stop_hr_threads(void)
2625 struct ptlrpc_hr_partition *hrp;
2629 ptlrpc_hr.hr_stopping = 1;
2631 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2632 if (hrp->hrp_thrs == NULL)
2633 continue; /* uninitialized */
2634 for (j = 0; j < hrp->hrp_nthrs; j++)
2635 wake_up_all(&hrp->hrp_thrs[j].hrt_waitq);
2638 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2639 if (hrp->hrp_thrs == NULL)
2640 continue; /* uninitialized */
2641 wait_event(ptlrpc_hr.hr_waitq,
2642 atomic_read(&hrp->hrp_nstopped) ==
2643 atomic_read(&hrp->hrp_nstarted));
2647 static int ptlrpc_start_hr_threads(void)
2649 struct ptlrpc_hr_partition *hrp;
2654 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2657 for (j = 0; j < hrp->hrp_nthrs; j++) {
2658 struct ptlrpc_hr_thread *hrt = &hrp->hrp_thrs[j];
2659 struct task_struct *task;
2661 task = kthread_run(ptlrpc_hr_main,
2663 "ptlrpc_hr%02d_%03d",
2672 wait_event(ptlrpc_hr.hr_waitq,
2673 atomic_read(&hrp->hrp_nstarted) == j);
2676 CERROR("cannot start reply handler thread %d:%d: "
2677 "rc = %d\n", i, j, rc);
2678 ptlrpc_stop_hr_threads();
2686 static void ptlrpc_svcpt_stop_threads(struct ptlrpc_service_part *svcpt)
2688 struct l_wait_info lwi = { 0 };
2689 struct ptlrpc_thread *thread;
2690 struct list_head zombie;
2694 CDEBUG(D_INFO, "Stopping threads for service %s\n",
2695 svcpt->scp_service->srv_name);
2697 INIT_LIST_HEAD(&zombie);
2698 spin_lock(&svcpt->scp_lock);
2699 /* let the thread know that we would like it to stop asap */
2700 list_for_each_entry(thread, &svcpt->scp_threads, t_link) {
2701 CDEBUG(D_INFO, "Stopping thread %s #%u\n",
2702 svcpt->scp_service->srv_thread_name, thread->t_id);
2703 thread_add_flags(thread, SVC_STOPPING);
2706 wake_up_all(&svcpt->scp_waitq);
2708 while (!list_empty(&svcpt->scp_threads)) {
2709 thread = list_entry(svcpt->scp_threads.next,
2710 struct ptlrpc_thread, t_link);
2711 if (thread_is_stopped(thread)) {
2712 list_del(&thread->t_link);
2713 list_add(&thread->t_link, &zombie);
2716 spin_unlock(&svcpt->scp_lock);
2718 CDEBUG(D_INFO, "waiting for stopping-thread %s #%u\n",
2719 svcpt->scp_service->srv_thread_name, thread->t_id);
2720 l_wait_event(thread->t_ctl_waitq,
2721 thread_is_stopped(thread), &lwi);
2723 spin_lock(&svcpt->scp_lock);
2726 spin_unlock(&svcpt->scp_lock);
2728 while (!list_empty(&zombie)) {
2729 thread = list_entry(zombie.next,
2730 struct ptlrpc_thread, t_link);
2731 list_del(&thread->t_link);
2732 OBD_FREE_PTR(thread);
2738 * Stops all threads of a particular service \a svc
2740 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2742 struct ptlrpc_service_part *svcpt;
2746 ptlrpc_service_for_each_part(svcpt, i, svc) {
2747 if (svcpt->scp_service != NULL)
2748 ptlrpc_svcpt_stop_threads(svcpt);
2754 int ptlrpc_start_threads(struct ptlrpc_service *svc)
2761 /* We require 2 threads min, see note in ptlrpc_server_handle_request */
2762 LASSERT(svc->srv_nthrs_cpt_init >= PTLRPC_NTHRS_INIT);
2764 for (i = 0; i < svc->srv_ncpts; i++) {
2765 for (j = 0; j < svc->srv_nthrs_cpt_init; j++) {
2766 rc = ptlrpc_start_thread(svc->srv_parts[i], 1);
2772 /* We have enough threads, don't start more. b=15759 */
2779 CERROR("cannot start %s thread #%d_%d: rc %d\n",
2780 svc->srv_thread_name, i, j, rc);
2781 ptlrpc_stop_all_threads(svc);
2785 int ptlrpc_start_thread(struct ptlrpc_service_part *svcpt, int wait)
2787 struct l_wait_info lwi = { 0 };
2788 struct ptlrpc_thread *thread;
2789 struct ptlrpc_service *svc;
2790 struct task_struct *task;
2794 LASSERT(svcpt != NULL);
2796 svc = svcpt->scp_service;
2798 CDEBUG(D_RPCTRACE, "%s[%d] started %d min %d max %d\n",
2799 svc->srv_name, svcpt->scp_cpt, svcpt->scp_nthrs_running,
2800 svc->srv_nthrs_cpt_init, svc->srv_nthrs_cpt_limit);
2803 if (unlikely(svc->srv_is_stopping))
2806 if (!ptlrpc_threads_increasable(svcpt) ||
2807 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2808 svcpt->scp_nthrs_running == svc->srv_nthrs_cpt_init - 1))
2811 OBD_CPT_ALLOC_PTR(thread, svc->srv_cptable, svcpt->scp_cpt);
2814 init_waitqueue_head(&thread->t_ctl_waitq);
2816 spin_lock(&svcpt->scp_lock);
2817 if (!ptlrpc_threads_increasable(svcpt)) {
2818 spin_unlock(&svcpt->scp_lock);
2819 OBD_FREE_PTR(thread);
2823 if (svcpt->scp_nthrs_starting != 0) {
2824 /* serialize starting because some modules (obdfilter)
2825 * might require unique and contiguous t_id */
2826 LASSERT(svcpt->scp_nthrs_starting == 1);
2827 spin_unlock(&svcpt->scp_lock);
2828 OBD_FREE_PTR(thread);
2830 CDEBUG(D_INFO, "Waiting for creating thread %s #%d\n",
2831 svc->srv_thread_name, svcpt->scp_thr_nextid);
2836 CDEBUG(D_INFO, "Creating thread %s #%d race, retry later\n",
2837 svc->srv_thread_name, svcpt->scp_thr_nextid);
2841 svcpt->scp_nthrs_starting++;
2842 thread->t_id = svcpt->scp_thr_nextid++;
2843 thread_add_flags(thread, SVC_STARTING);
2844 thread->t_svcpt = svcpt;
2846 list_add(&thread->t_link, &svcpt->scp_threads);
2847 spin_unlock(&svcpt->scp_lock);
2849 if (svcpt->scp_cpt >= 0) {
2850 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s%02d_%03d",
2851 svc->srv_thread_name, svcpt->scp_cpt, thread->t_id);
2853 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s_%04d",
2854 svc->srv_thread_name, thread->t_id);
2857 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", thread->t_name);
2858 task = kthread_run(ptlrpc_main, thread, "%s", thread->t_name);
2861 CERROR("cannot start thread '%s': rc = %d\n",
2862 thread->t_name, rc);
2863 spin_lock(&svcpt->scp_lock);
2864 --svcpt->scp_nthrs_starting;
2865 if (thread_is_stopping(thread)) {
2866 /* this ptlrpc_thread is being hanled
2867 * by ptlrpc_svcpt_stop_threads now
2869 thread_add_flags(thread, SVC_STOPPED);
2870 wake_up(&thread->t_ctl_waitq);
2871 spin_unlock(&svcpt->scp_lock);
2873 list_del(&thread->t_link);
2874 spin_unlock(&svcpt->scp_lock);
2875 OBD_FREE_PTR(thread);
2883 l_wait_event(thread->t_ctl_waitq,
2884 thread_is_running(thread) || thread_is_stopped(thread),
2887 rc = thread_is_stopped(thread) ? thread->t_id : 0;
2891 int ptlrpc_hr_init(void)
2893 struct ptlrpc_hr_partition *hrp;
2894 struct ptlrpc_hr_thread *hrt;
2901 memset(&ptlrpc_hr, 0, sizeof(ptlrpc_hr));
2902 ptlrpc_hr.hr_cpt_table = cfs_cpt_table;
2904 ptlrpc_hr.hr_partitions = cfs_percpt_alloc(ptlrpc_hr.hr_cpt_table,
2906 if (ptlrpc_hr.hr_partitions == NULL)
2909 init_waitqueue_head(&ptlrpc_hr.hr_waitq);
2911 weight = cfs_cpu_ht_nsiblings(0);
2913 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2916 atomic_set(&hrp->hrp_nstarted, 0);
2917 atomic_set(&hrp->hrp_nstopped, 0);
2919 hrp->hrp_nthrs = cfs_cpt_weight(ptlrpc_hr.hr_cpt_table, i);
2921 hrp->hrp_nthrs /= weight;
2922 if (hrp->hrp_nthrs == 0)
2925 OBD_CPT_ALLOC(hrp->hrp_thrs, ptlrpc_hr.hr_cpt_table, i,
2926 hrp->hrp_nthrs * sizeof(*hrt));
2927 if (hrp->hrp_thrs == NULL)
2928 GOTO(out, rc = -ENOMEM);
2930 for (j = 0; j < hrp->hrp_nthrs; j++) {
2931 hrt = &hrp->hrp_thrs[j];
2934 hrt->hrt_partition = hrp;
2935 init_waitqueue_head(&hrt->hrt_waitq);
2936 spin_lock_init(&hrt->hrt_lock);
2937 INIT_LIST_HEAD(&hrt->hrt_queue);
2941 rc = ptlrpc_start_hr_threads();
2948 void ptlrpc_hr_fini(void)
2950 struct ptlrpc_hr_partition *hrp;
2953 if (ptlrpc_hr.hr_partitions == NULL)
2956 ptlrpc_stop_hr_threads();
2958 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2959 if (hrp->hrp_thrs != NULL) {
2960 OBD_FREE(hrp->hrp_thrs,
2961 hrp->hrp_nthrs * sizeof(hrp->hrp_thrs[0]));
2965 cfs_percpt_free(ptlrpc_hr.hr_partitions);
2966 ptlrpc_hr.hr_partitions = NULL;
2971 * Wait until all already scheduled replies are processed.
2973 static void ptlrpc_wait_replies(struct ptlrpc_service_part *svcpt)
2977 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2980 rc = l_wait_event(svcpt->scp_waitq,
2981 atomic_read(&svcpt->scp_nreps_difficult) == 0, &lwi);
2984 CWARN("Unexpectedly long timeout %s %p\n",
2985 svcpt->scp_service->srv_name, svcpt->scp_service);
2990 ptlrpc_service_del_atimer(struct ptlrpc_service *svc)
2992 struct ptlrpc_service_part *svcpt;
2995 /* early disarm AT timer... */
2996 ptlrpc_service_for_each_part(svcpt, i, svc) {
2997 if (svcpt->scp_service != NULL)
2998 cfs_timer_disarm(&svcpt->scp_at_timer);
3003 ptlrpc_service_unlink_rqbd(struct ptlrpc_service *svc)
3005 struct ptlrpc_service_part *svcpt;
3006 struct ptlrpc_request_buffer_desc *rqbd;
3007 struct l_wait_info lwi;
3011 /* All history will be culled when the next request buffer is
3012 * freed in ptlrpc_service_purge_all() */
3013 svc->srv_hist_nrqbds_cpt_max = 0;
3015 rc = LNetClearLazyPortal(svc->srv_req_portal);
3018 ptlrpc_service_for_each_part(svcpt, i, svc) {
3019 if (svcpt->scp_service == NULL)
3022 /* Unlink all the request buffers. This forces a 'final'
3023 * event with its 'unlink' flag set for each posted rqbd */
3024 list_for_each_entry(rqbd, &svcpt->scp_rqbd_posted,
3026 rc = LNetMDUnlink(rqbd->rqbd_md_h);
3027 LASSERT(rc == 0 || rc == -ENOENT);
3031 ptlrpc_service_for_each_part(svcpt, i, svc) {
3032 if (svcpt->scp_service == NULL)
3035 /* Wait for the network to release any buffers
3036 * it's currently filling */
3037 spin_lock(&svcpt->scp_lock);
3038 while (svcpt->scp_nrqbds_posted != 0) {
3039 spin_unlock(&svcpt->scp_lock);
3040 /* Network access will complete in finite time but
3041 * the HUGE timeout lets us CWARN for visibility
3042 * of sluggish NALs */
3043 lwi = LWI_TIMEOUT_INTERVAL(
3044 cfs_time_seconds(LONG_UNLINK),
3045 cfs_time_seconds(1), NULL, NULL);
3046 rc = l_wait_event(svcpt->scp_waitq,
3047 svcpt->scp_nrqbds_posted == 0, &lwi);
3048 if (rc == -ETIMEDOUT) {
3049 CWARN("Service %s waiting for "
3050 "request buffers\n",
3051 svcpt->scp_service->srv_name);
3053 spin_lock(&svcpt->scp_lock);
3055 spin_unlock(&svcpt->scp_lock);
3060 ptlrpc_service_purge_all(struct ptlrpc_service *svc)
3062 struct ptlrpc_service_part *svcpt;
3063 struct ptlrpc_request_buffer_desc *rqbd;
3064 struct ptlrpc_request *req;
3065 struct ptlrpc_reply_state *rs;
3068 ptlrpc_service_for_each_part(svcpt, i, svc) {
3069 if (svcpt->scp_service == NULL)
3072 spin_lock(&svcpt->scp_rep_lock);
3073 while (!list_empty(&svcpt->scp_rep_active)) {
3074 rs = list_entry(svcpt->scp_rep_active.next,
3075 struct ptlrpc_reply_state, rs_list);
3076 spin_lock(&rs->rs_lock);
3077 ptlrpc_schedule_difficult_reply(rs);
3078 spin_unlock(&rs->rs_lock);
3080 spin_unlock(&svcpt->scp_rep_lock);
3082 /* purge the request queue. NB No new replies (rqbds
3083 * all unlinked) and no service threads, so I'm the only
3084 * thread noodling the request queue now */
3085 while (!list_empty(&svcpt->scp_req_incoming)) {
3086 req = list_entry(svcpt->scp_req_incoming.next,
3087 struct ptlrpc_request, rq_list);
3089 list_del(&req->rq_list);
3090 svcpt->scp_nreqs_incoming--;
3091 ptlrpc_server_finish_request(svcpt, req);
3094 while (ptlrpc_server_request_pending(svcpt, true)) {
3095 req = ptlrpc_server_request_get(svcpt, true);
3096 ptlrpc_server_finish_active_request(svcpt, req);
3099 LASSERT(list_empty(&svcpt->scp_rqbd_posted));
3100 LASSERT(svcpt->scp_nreqs_incoming == 0);
3101 LASSERT(svcpt->scp_nreqs_active == 0);
3102 /* history should have been culled by
3103 * ptlrpc_server_finish_request */
3104 LASSERT(svcpt->scp_hist_nrqbds == 0);
3106 /* Now free all the request buffers since nothing
3107 * references them any more... */
3109 while (!list_empty(&svcpt->scp_rqbd_idle)) {
3110 rqbd = list_entry(svcpt->scp_rqbd_idle.next,
3111 struct ptlrpc_request_buffer_desc,
3113 ptlrpc_free_rqbd(rqbd);
3115 ptlrpc_wait_replies(svcpt);
3117 while (!list_empty(&svcpt->scp_rep_idle)) {
3118 rs = list_entry(svcpt->scp_rep_idle.next,
3119 struct ptlrpc_reply_state,
3121 list_del(&rs->rs_list);
3122 OBD_FREE_LARGE(rs, svc->srv_max_reply_size);
3128 ptlrpc_service_free(struct ptlrpc_service *svc)
3130 struct ptlrpc_service_part *svcpt;
3131 struct ptlrpc_at_array *array;
3134 ptlrpc_service_for_each_part(svcpt, i, svc) {
3135 if (svcpt->scp_service == NULL)
3138 /* In case somebody rearmed this in the meantime */
3139 cfs_timer_disarm(&svcpt->scp_at_timer);
3140 array = &svcpt->scp_at_array;
3142 if (array->paa_reqs_array != NULL) {
3143 OBD_FREE(array->paa_reqs_array,
3144 sizeof(struct list_head) * array->paa_size);
3145 array->paa_reqs_array = NULL;
3148 if (array->paa_reqs_count != NULL) {
3149 OBD_FREE(array->paa_reqs_count,
3150 sizeof(__u32) * array->paa_size);
3151 array->paa_reqs_count = NULL;
3155 ptlrpc_service_for_each_part(svcpt, i, svc)
3156 OBD_FREE_PTR(svcpt);
3158 if (svc->srv_cpts != NULL)
3159 cfs_expr_list_values_free(svc->srv_cpts, svc->srv_ncpts);
3161 OBD_FREE(svc, offsetof(struct ptlrpc_service,
3162 srv_parts[svc->srv_ncpts]));
3165 int ptlrpc_unregister_service(struct ptlrpc_service *service)
3169 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
3171 service->srv_is_stopping = 1;
3173 mutex_lock(&ptlrpc_all_services_mutex);
3174 list_del_init(&service->srv_list);
3175 mutex_unlock(&ptlrpc_all_services_mutex);
3177 ptlrpc_service_del_atimer(service);
3178 ptlrpc_stop_all_threads(service);
3180 ptlrpc_service_unlink_rqbd(service);
3181 ptlrpc_service_purge_all(service);
3182 ptlrpc_service_nrs_cleanup(service);
3184 ptlrpc_lprocfs_unregister_service(service);
3186 ptlrpc_service_free(service);
3190 EXPORT_SYMBOL(ptlrpc_unregister_service);
3193 * Returns 0 if the service is healthy.
3195 * Right now, it just checks to make sure that requests aren't languishing
3196 * in the queue. We'll use this health check to govern whether a node needs
3197 * to be shot, so it's intentionally non-aggressive. */
3198 static int ptlrpc_svcpt_health_check(struct ptlrpc_service_part *svcpt)
3200 struct ptlrpc_request *request = NULL;
3201 struct timeval right_now;
3204 do_gettimeofday(&right_now);
3206 spin_lock(&svcpt->scp_req_lock);
3207 /* How long has the next entry been waiting? */
3208 if (ptlrpc_server_high_pending(svcpt, true))
3209 request = ptlrpc_nrs_req_peek_nolock(svcpt, true);
3210 else if (ptlrpc_server_normal_pending(svcpt, true))
3211 request = ptlrpc_nrs_req_peek_nolock(svcpt, false);
3213 if (request == NULL) {
3214 spin_unlock(&svcpt->scp_req_lock);
3218 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
3219 spin_unlock(&svcpt->scp_req_lock);
3221 if ((timediff / ONE_MILLION) >
3222 (AT_OFF ? obd_timeout * 3 / 2 : at_max)) {
3223 CERROR("%s: unhealthy - request has been waiting %lds\n",
3224 svcpt->scp_service->srv_name, timediff / ONE_MILLION);
3232 ptlrpc_service_health_check(struct ptlrpc_service *svc)
3234 struct ptlrpc_service_part *svcpt;
3240 ptlrpc_service_for_each_part(svcpt, i, svc) {
3241 int rc = ptlrpc_svcpt_health_check(svcpt);
3248 EXPORT_SYMBOL(ptlrpc_service_health_check);