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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <liblustre.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_net.h>
44 #include <lu_object.h>
45 #include <lnet/types.h>
46 #include "ptlrpc_internal.h"
48 /* The following are visible and mutable through /sys/module/ptlrpc */
49 int test_req_buffer_pressure = 0;
50 CFS_MODULE_PARM(test_req_buffer_pressure, "i", int, 0444,
51 "set non-zero to put pressure on request buffer pools");
52 CFS_MODULE_PARM(at_min, "i", int, 0644,
53 "Adaptive timeout minimum (sec)");
54 CFS_MODULE_PARM(at_max, "i", int, 0644,
55 "Adaptive timeout maximum (sec)");
56 CFS_MODULE_PARM(at_history, "i", int, 0644,
57 "Adaptive timeouts remember the slowest event that took place "
58 "within this period (sec)");
59 CFS_MODULE_PARM(at_early_margin, "i", int, 0644,
60 "How soon before an RPC deadline to send an early reply");
61 CFS_MODULE_PARM(at_extra, "i", int, 0644,
62 "How much extra time to give with each early reply");
66 static int ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt);
67 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req);
68 static void ptlrpc_at_remove_timed(struct ptlrpc_request *req);
70 /** Holds a list of all PTLRPC services */
71 struct list_head ptlrpc_all_services;
72 /** Used to protect the \e ptlrpc_all_services list */
73 struct mutex ptlrpc_all_services_mutex;
75 struct ptlrpc_request_buffer_desc *
76 ptlrpc_alloc_rqbd(struct ptlrpc_service_part *svcpt)
78 struct ptlrpc_service *svc = svcpt->scp_service;
79 struct ptlrpc_request_buffer_desc *rqbd;
81 OBD_CPT_ALLOC_PTR(rqbd, svc->srv_cptable, svcpt->scp_cpt);
85 rqbd->rqbd_svcpt = svcpt;
86 rqbd->rqbd_refcount = 0;
87 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
88 rqbd->rqbd_cbid.cbid_arg = rqbd;
89 INIT_LIST_HEAD(&rqbd->rqbd_reqs);
90 OBD_CPT_ALLOC_LARGE(rqbd->rqbd_buffer, svc->srv_cptable,
91 svcpt->scp_cpt, svc->srv_buf_size);
92 if (rqbd->rqbd_buffer == NULL) {
97 spin_lock(&svcpt->scp_lock);
98 list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
99 svcpt->scp_nrqbds_total++;
100 spin_unlock(&svcpt->scp_lock);
106 ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
108 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
110 LASSERT(rqbd->rqbd_refcount == 0);
111 LASSERT(list_empty(&rqbd->rqbd_reqs));
113 spin_lock(&svcpt->scp_lock);
114 list_del(&rqbd->rqbd_list);
115 svcpt->scp_nrqbds_total--;
116 spin_unlock(&svcpt->scp_lock);
118 OBD_FREE_LARGE(rqbd->rqbd_buffer, svcpt->scp_service->srv_buf_size);
123 ptlrpc_grow_req_bufs(struct ptlrpc_service_part *svcpt, int post)
125 struct ptlrpc_service *svc = svcpt->scp_service;
126 struct ptlrpc_request_buffer_desc *rqbd;
130 if (svcpt->scp_rqbd_allocating)
133 spin_lock(&svcpt->scp_lock);
134 /* check again with lock */
135 if (svcpt->scp_rqbd_allocating) {
136 /* NB: we might allow more than one thread in the future */
137 LASSERT(svcpt->scp_rqbd_allocating == 1);
138 spin_unlock(&svcpt->scp_lock);
142 svcpt->scp_rqbd_allocating++;
143 spin_unlock(&svcpt->scp_lock);
146 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
147 /* NB: another thread might have recycled enough rqbds, we
148 * need to make sure it wouldn't over-allocate, see LU-1212. */
149 if (svcpt->scp_nrqbds_posted >= svc->srv_nbuf_per_group)
152 rqbd = ptlrpc_alloc_rqbd(svcpt);
155 CERROR("%s: Can't allocate request buffer\n",
162 spin_lock(&svcpt->scp_lock);
164 LASSERT(svcpt->scp_rqbd_allocating == 1);
165 svcpt->scp_rqbd_allocating--;
167 spin_unlock(&svcpt->scp_lock);
170 "%s: allocate %d new %d-byte reqbufs (%d/%d left), rc = %d\n",
171 svc->srv_name, i, svc->srv_buf_size, svcpt->scp_nrqbds_posted,
172 svcpt->scp_nrqbds_total, rc);
176 rc = ptlrpc_server_post_idle_rqbds(svcpt);
182 * Part of Rep-Ack logic.
183 * Puts a lock and its mode into reply state assotiated to request reply.
186 ptlrpc_save_lock(struct ptlrpc_request *req,
187 struct lustre_handle *lock, int mode, int no_ack)
189 struct ptlrpc_reply_state *rs = req->rq_reply_state;
193 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
195 if (req->rq_export->exp_disconnected) {
196 ldlm_lock_decref(lock, mode);
198 idx = rs->rs_nlocks++;
199 rs->rs_locks[idx] = *lock;
200 rs->rs_modes[idx] = mode;
201 rs->rs_difficult = 1;
202 rs->rs_no_ack = !!no_ack;
205 EXPORT_SYMBOL(ptlrpc_save_lock);
209 struct ptlrpc_hr_partition;
211 struct ptlrpc_hr_thread {
212 int hrt_id; /* thread ID */
214 wait_queue_head_t hrt_waitq;
215 struct list_head hrt_queue; /* RS queue */
216 struct ptlrpc_hr_partition *hrt_partition;
219 struct ptlrpc_hr_partition {
220 /* # of started threads */
221 atomic_t hrp_nstarted;
222 /* # of stopped threads */
223 atomic_t hrp_nstopped;
224 /* cpu partition id */
226 /* round-robin rotor for choosing thread */
228 /* total number of threads on this partition */
231 struct ptlrpc_hr_thread *hrp_thrs;
234 #define HRT_RUNNING 0
235 #define HRT_STOPPING 1
237 struct ptlrpc_hr_service {
238 /* CPU partition table, it's just cfs_cpt_table for now */
239 struct cfs_cpt_table *hr_cpt_table;
240 /** controller sleep waitq */
241 wait_queue_head_t hr_waitq;
242 unsigned int hr_stopping;
243 /** roundrobin rotor for non-affinity service */
244 unsigned int hr_rotor;
246 struct ptlrpc_hr_partition **hr_partitions;
250 struct list_head rsb_replies;
251 unsigned int rsb_n_replies;
252 struct ptlrpc_service_part *rsb_svcpt;
255 /** reply handling service. */
256 static struct ptlrpc_hr_service ptlrpc_hr;
259 * maximum mumber of replies scheduled in one batch
261 #define MAX_SCHEDULED 256
264 * Initialize a reply batch.
268 static void rs_batch_init(struct rs_batch *b)
270 memset(b, 0, sizeof *b);
271 INIT_LIST_HEAD(&b->rsb_replies);
275 * Choose an hr thread to dispatch requests to.
277 static struct ptlrpc_hr_thread *
278 ptlrpc_hr_select(struct ptlrpc_service_part *svcpt)
280 struct ptlrpc_hr_partition *hrp;
283 if (svcpt->scp_cpt >= 0 &&
284 svcpt->scp_service->srv_cptable == ptlrpc_hr.hr_cpt_table) {
285 /* directly match partition */
286 hrp = ptlrpc_hr.hr_partitions[svcpt->scp_cpt];
289 rotor = ptlrpc_hr.hr_rotor++;
290 rotor %= cfs_cpt_number(ptlrpc_hr.hr_cpt_table);
292 hrp = ptlrpc_hr.hr_partitions[rotor];
295 rotor = hrp->hrp_rotor++;
296 return &hrp->hrp_thrs[rotor % hrp->hrp_nthrs];
300 * Dispatch all replies accumulated in the batch to one from
301 * dedicated reply handling threads.
305 static void rs_batch_dispatch(struct rs_batch *b)
307 if (b->rsb_n_replies != 0) {
308 struct ptlrpc_hr_thread *hrt;
310 hrt = ptlrpc_hr_select(b->rsb_svcpt);
312 spin_lock(&hrt->hrt_lock);
313 list_splice_init(&b->rsb_replies, &hrt->hrt_queue);
314 spin_unlock(&hrt->hrt_lock);
316 wake_up(&hrt->hrt_waitq);
317 b->rsb_n_replies = 0;
322 * Add a reply to a batch.
323 * Add one reply object to a batch, schedule batched replies if overload.
328 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
330 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
332 if (svcpt != b->rsb_svcpt || b->rsb_n_replies >= MAX_SCHEDULED) {
333 if (b->rsb_svcpt != NULL) {
334 rs_batch_dispatch(b);
335 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
337 spin_lock(&svcpt->scp_rep_lock);
338 b->rsb_svcpt = svcpt;
340 spin_lock(&rs->rs_lock);
341 rs->rs_scheduled_ever = 1;
342 if (rs->rs_scheduled == 0) {
343 list_move(&rs->rs_list, &b->rsb_replies);
344 rs->rs_scheduled = 1;
347 rs->rs_committed = 1;
348 spin_unlock(&rs->rs_lock);
352 * Reply batch finalization.
353 * Dispatch remaining replies from the batch
354 * and release remaining spinlock.
358 static void rs_batch_fini(struct rs_batch *b)
360 if (b->rsb_svcpt != NULL) {
361 rs_batch_dispatch(b);
362 spin_unlock(&b->rsb_svcpt->scp_rep_lock);
366 #define DECLARE_RS_BATCH(b) struct rs_batch b
368 #else /* __KERNEL__ */
370 #define rs_batch_init(b) do{}while(0)
371 #define rs_batch_fini(b) do{}while(0)
372 #define rs_batch_add(b, r) ptlrpc_schedule_difficult_reply(r)
373 #define DECLARE_RS_BATCH(b)
375 #endif /* __KERNEL__ */
378 * Put reply state into a queue for processing because we received
379 * ACK from the client
381 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
384 struct ptlrpc_hr_thread *hrt;
387 LASSERT(list_empty(&rs->rs_list));
389 hrt = ptlrpc_hr_select(rs->rs_svcpt);
391 spin_lock(&hrt->hrt_lock);
392 list_add_tail(&rs->rs_list, &hrt->hrt_queue);
393 spin_unlock(&hrt->hrt_lock);
395 wake_up(&hrt->hrt_waitq);
398 list_add_tail(&rs->rs_list, &rs->rs_svcpt->scp_rep_queue);
403 ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs)
407 assert_spin_locked(&rs->rs_svcpt->scp_rep_lock);
408 assert_spin_locked(&rs->rs_lock);
409 LASSERT (rs->rs_difficult);
410 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
412 if (rs->rs_scheduled) { /* being set up or already notified */
417 rs->rs_scheduled = 1;
418 list_del_init(&rs->rs_list);
419 ptlrpc_dispatch_difficult_reply(rs);
422 EXPORT_SYMBOL(ptlrpc_schedule_difficult_reply);
424 void ptlrpc_commit_replies(struct obd_export *exp)
426 struct ptlrpc_reply_state *rs, *nxt;
427 DECLARE_RS_BATCH(batch);
430 rs_batch_init(&batch);
431 /* Find any replies that have been committed and get their service
432 * to attend to complete them. */
434 /* CAVEAT EMPTOR: spinlock ordering!!! */
435 spin_lock(&exp->exp_uncommitted_replies_lock);
436 list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
438 LASSERT (rs->rs_difficult);
439 /* VBR: per-export last_committed */
440 LASSERT(rs->rs_export);
441 if (rs->rs_transno <= exp->exp_last_committed) {
442 list_del_init(&rs->rs_obd_list);
443 rs_batch_add(&batch, rs);
446 spin_unlock(&exp->exp_uncommitted_replies_lock);
447 rs_batch_fini(&batch);
450 EXPORT_SYMBOL(ptlrpc_commit_replies);
453 ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt)
455 struct ptlrpc_request_buffer_desc *rqbd;
460 spin_lock(&svcpt->scp_lock);
462 if (list_empty(&svcpt->scp_rqbd_idle)) {
463 spin_unlock(&svcpt->scp_lock);
467 rqbd = list_entry(svcpt->scp_rqbd_idle.next,
468 struct ptlrpc_request_buffer_desc,
470 list_del(&rqbd->rqbd_list);
472 /* assume we will post successfully */
473 svcpt->scp_nrqbds_posted++;
474 list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_posted);
476 spin_unlock(&svcpt->scp_lock);
478 rc = ptlrpc_register_rqbd(rqbd);
485 spin_lock(&svcpt->scp_lock);
487 svcpt->scp_nrqbds_posted--;
488 list_del(&rqbd->rqbd_list);
489 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
491 /* Don't complain if no request buffers are posted right now; LNET
492 * won't drop requests because we set the portal lazy! */
494 spin_unlock(&svcpt->scp_lock);
499 static void ptlrpc_at_timer(unsigned long castmeharder)
501 struct ptlrpc_service_part *svcpt;
503 svcpt = (struct ptlrpc_service_part *)castmeharder;
505 svcpt->scp_at_check = 1;
506 svcpt->scp_at_checktime = cfs_time_current();
507 wake_up(&svcpt->scp_waitq);
511 ptlrpc_server_nthreads_check(struct ptlrpc_service *svc,
512 struct ptlrpc_service_conf *conf)
515 struct ptlrpc_service_thr_conf *tc = &conf->psc_thr;
522 * Common code for estimating & validating threads number.
523 * CPT affinity service could have percpt thread-pool instead
524 * of a global thread-pool, which means user might not always
525 * get the threads number they give it in conf::tc_nthrs_user
526 * even they did set. It's because we need to validate threads
527 * number for each CPT to guarantee each pool will have enough
528 * threads to keep the service healthy.
530 init = PTLRPC_NTHRS_INIT + (svc->srv_ops.so_hpreq_handler != NULL);
531 init = max_t(int, init, tc->tc_nthrs_init);
533 /* NB: please see comments in lustre_lnet.h for definition
534 * details of these members */
535 LASSERT(tc->tc_nthrs_max != 0);
537 if (tc->tc_nthrs_user != 0) {
538 /* In case there is a reason to test a service with many
539 * threads, we give a less strict check here, it can
540 * be up to 8 * nthrs_max */
541 total = min(tc->tc_nthrs_max * 8, tc->tc_nthrs_user);
542 nthrs = total / svc->srv_ncpts;
543 init = max(init, nthrs);
547 total = tc->tc_nthrs_max;
548 if (tc->tc_nthrs_base == 0) {
549 /* don't care about base threads number per partition,
550 * this is most for non-affinity service */
551 nthrs = total / svc->srv_ncpts;
555 nthrs = tc->tc_nthrs_base;
556 if (svc->srv_ncpts == 1) {
559 /* NB: Increase the base number if it's single partition
560 * and total number of cores/HTs is larger or equal to 4.
561 * result will always < 2 * nthrs_base */
562 weight = cfs_cpt_weight(svc->srv_cptable, CFS_CPT_ANY);
563 for (i = 1; (weight >> (i + 1)) != 0 && /* >= 4 cores/HTs */
564 (tc->tc_nthrs_base >> i) != 0; i++)
565 nthrs += tc->tc_nthrs_base >> i;
568 if (tc->tc_thr_factor != 0) {
569 int factor = tc->tc_thr_factor;
573 * User wants to increase number of threads with for
574 * each CPU core/HT, most likely the factor is larger then
575 * one thread/core because service threads are supposed to
576 * be blocked by lock or wait for IO.
579 * Amdahl's law says that adding processors wouldn't give
580 * a linear increasing of parallelism, so it's nonsense to
581 * have too many threads no matter how many cores/HTs
584 if (cfs_cpu_ht_nsiblings(0) > 1) { /* weight is # of HTs */
585 /* depress thread factor for hyper-thread */
586 factor = factor - (factor >> 1) + (factor >> 3);
589 weight = cfs_cpt_weight(svc->srv_cptable, 0);
592 for (; factor > 0 && weight > 0; factor--, weight -= fade)
593 nthrs += min(weight, fade) * factor;
596 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
597 nthrs = max(tc->tc_nthrs_base,
598 tc->tc_nthrs_max / svc->srv_ncpts);
601 nthrs = max(nthrs, tc->tc_nthrs_init);
602 svc->srv_nthrs_cpt_limit = nthrs;
603 svc->srv_nthrs_cpt_init = init;
605 if (nthrs * svc->srv_ncpts > tc->tc_nthrs_max) {
606 CDEBUG(D_OTHER, "%s: This service may have more threads (%d) "
607 "than the given soft limit (%d)\n",
608 svc->srv_name, nthrs * svc->srv_ncpts,
615 * Initialize percpt data for a service
618 ptlrpc_service_part_init(struct ptlrpc_service *svc,
619 struct ptlrpc_service_part *svcpt, int cpt)
621 struct ptlrpc_at_array *array;
626 svcpt->scp_cpt = cpt;
627 INIT_LIST_HEAD(&svcpt->scp_threads);
629 /* rqbd and incoming request queue */
630 spin_lock_init(&svcpt->scp_lock);
631 INIT_LIST_HEAD(&svcpt->scp_rqbd_idle);
632 INIT_LIST_HEAD(&svcpt->scp_rqbd_posted);
633 INIT_LIST_HEAD(&svcpt->scp_req_incoming);
634 init_waitqueue_head(&svcpt->scp_waitq);
635 /* history request & rqbd list */
636 INIT_LIST_HEAD(&svcpt->scp_hist_reqs);
637 INIT_LIST_HEAD(&svcpt->scp_hist_rqbds);
639 /* acitve requests and hp requests */
640 spin_lock_init(&svcpt->scp_req_lock);
643 spin_lock_init(&svcpt->scp_rep_lock);
644 INIT_LIST_HEAD(&svcpt->scp_rep_active);
646 INIT_LIST_HEAD(&svcpt->scp_rep_queue);
648 INIT_LIST_HEAD(&svcpt->scp_rep_idle);
649 init_waitqueue_head(&svcpt->scp_rep_waitq);
650 atomic_set(&svcpt->scp_nreps_difficult, 0);
652 /* adaptive timeout */
653 spin_lock_init(&svcpt->scp_at_lock);
654 array = &svcpt->scp_at_array;
656 size = at_est2timeout(at_max);
657 array->paa_size = size;
658 array->paa_count = 0;
659 array->paa_deadline = -1;
661 /* allocate memory for scp_at_array (ptlrpc_at_array) */
662 OBD_CPT_ALLOC(array->paa_reqs_array,
663 svc->srv_cptable, cpt, sizeof(struct list_head) * size);
664 if (array->paa_reqs_array == NULL)
667 for (index = 0; index < size; index++)
668 INIT_LIST_HEAD(&array->paa_reqs_array[index]);
670 OBD_CPT_ALLOC(array->paa_reqs_count,
671 svc->srv_cptable, cpt, sizeof(__u32) * size);
672 if (array->paa_reqs_count == NULL)
675 cfs_timer_init(&svcpt->scp_at_timer, ptlrpc_at_timer, svcpt);
676 /* At SOW, service time should be quick; 10s seems generous. If client
677 * timeout is less than this, we'll be sending an early reply. */
678 at_init(&svcpt->scp_at_estimate, 10, 0);
680 /* assign this before call ptlrpc_grow_req_bufs */
681 svcpt->scp_service = svc;
682 /* Now allocate the request buffers, but don't post them now */
683 rc = ptlrpc_grow_req_bufs(svcpt, 0);
684 /* We shouldn't be under memory pressure at startup, so
685 * fail if we can't allocate all our buffers at this time. */
692 if (array->paa_reqs_count != NULL) {
693 OBD_FREE(array->paa_reqs_count, sizeof(__u32) * size);
694 array->paa_reqs_count = NULL;
697 if (array->paa_reqs_array != NULL) {
698 OBD_FREE(array->paa_reqs_array,
699 sizeof(struct list_head) * array->paa_size);
700 array->paa_reqs_array = NULL;
707 * Initialize service on a given portal.
708 * This includes starting serving threads , allocating and posting rqbds and
711 struct ptlrpc_service *
712 ptlrpc_register_service(struct ptlrpc_service_conf *conf,
713 struct proc_dir_entry *proc_entry)
715 struct ptlrpc_service_cpt_conf *cconf = &conf->psc_cpt;
716 struct ptlrpc_service *service;
717 struct ptlrpc_service_part *svcpt;
718 struct cfs_cpt_table *cptable;
726 LASSERT(conf->psc_buf.bc_nbufs > 0);
727 LASSERT(conf->psc_buf.bc_buf_size >=
728 conf->psc_buf.bc_req_max_size + SPTLRPC_MAX_PAYLOAD);
729 LASSERT(conf->psc_thr.tc_ctx_tags != 0);
731 cptable = cconf->cc_cptable;
733 cptable = cfs_cpt_table;
735 if (!conf->psc_thr.tc_cpu_affinity) {
738 ncpts = cfs_cpt_number(cptable);
739 if (cconf->cc_pattern != NULL) {
740 struct cfs_expr_list *el;
742 rc = cfs_expr_list_parse(cconf->cc_pattern,
743 strlen(cconf->cc_pattern),
746 CERROR("%s: invalid CPT pattern string: %s",
747 conf->psc_name, cconf->cc_pattern);
748 RETURN(ERR_PTR(-EINVAL));
751 rc = cfs_expr_list_values(el, ncpts, &cpts);
752 cfs_expr_list_free(el);
754 CERROR("%s: failed to parse CPT array %s: %d\n",
755 conf->psc_name, cconf->cc_pattern, rc);
757 OBD_FREE(cpts, sizeof(*cpts) * ncpts);
758 RETURN(ERR_PTR(rc < 0 ? rc : -EINVAL));
764 OBD_ALLOC(service, offsetof(struct ptlrpc_service, srv_parts[ncpts]));
765 if (service == NULL) {
767 OBD_FREE(cpts, sizeof(*cpts) * ncpts);
768 RETURN(ERR_PTR(-ENOMEM));
771 service->srv_cptable = cptable;
772 service->srv_cpts = cpts;
773 service->srv_ncpts = ncpts;
775 service->srv_cpt_bits = 0; /* it's zero already, easy to read... */
776 while ((1 << service->srv_cpt_bits) < cfs_cpt_number(cptable))
777 service->srv_cpt_bits++;
780 spin_lock_init(&service->srv_lock);
781 service->srv_name = conf->psc_name;
782 service->srv_watchdog_factor = conf->psc_watchdog_factor;
783 INIT_LIST_HEAD(&service->srv_list); /* for safty of cleanup */
785 /* buffer configuration */
786 service->srv_nbuf_per_group = test_req_buffer_pressure ?
787 1 : conf->psc_buf.bc_nbufs;
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 /* Increase max reply size to next power of two */
795 service->srv_max_reply_size = 1;
796 while (service->srv_max_reply_size <
797 conf->psc_buf.bc_rep_max_size + SPTLRPC_MAX_PAYLOAD)
798 service->srv_max_reply_size <<= 1;
800 service->srv_thread_name = conf->psc_thr.tc_thr_name;
801 service->srv_ctx_tags = conf->psc_thr.tc_ctx_tags;
802 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
803 service->srv_ops = conf->psc_ops;
805 for (i = 0; i < ncpts; i++) {
806 if (!conf->psc_thr.tc_cpu_affinity)
809 cpt = cpts != NULL ? cpts[i] : i;
811 OBD_CPT_ALLOC(svcpt, cptable, cpt, sizeof(*svcpt));
813 GOTO(failed, rc = -ENOMEM);
815 service->srv_parts[i] = svcpt;
816 rc = ptlrpc_service_part_init(service, svcpt, cpt);
821 ptlrpc_server_nthreads_check(service, conf);
823 rc = LNetSetLazyPortal(service->srv_req_portal);
826 mutex_lock(&ptlrpc_all_services_mutex);
827 list_add(&service->srv_list, &ptlrpc_all_services);
828 mutex_unlock(&ptlrpc_all_services_mutex);
830 if (proc_entry != NULL)
831 ptlrpc_lprocfs_register_service(proc_entry, service);
833 rc = ptlrpc_service_nrs_setup(service);
837 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
838 service->srv_name, service->srv_req_portal);
841 rc = ptlrpc_start_threads(service);
843 CERROR("Failed to start threads for service %s: %d\n",
844 service->srv_name, rc);
851 ptlrpc_unregister_service(service);
854 EXPORT_SYMBOL(ptlrpc_register_service);
857 * to actually free the request, must be called without holding svc_lock.
858 * note it's caller's responsibility to unlink req->rq_list.
860 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
862 LASSERT(atomic_read(&req->rq_refcount) == 0);
863 LASSERT(list_empty(&req->rq_timed_list));
865 /* DEBUG_REQ() assumes the reply state of a request with a valid
866 * ref will not be destroyed until that reference is dropped. */
867 ptlrpc_req_drop_rs(req);
869 sptlrpc_svc_ctx_decref(req);
871 if (req != &req->rq_rqbd->rqbd_req) {
872 /* NB request buffers use an embedded
873 * req if the incoming req unlinked the
874 * MD; this isn't one of them! */
875 ptlrpc_request_cache_free(req);
880 * drop a reference count of the request. if it reaches 0, we either
881 * put it into history list, or free it immediately.
883 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
885 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
886 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
887 struct ptlrpc_service *svc = svcpt->scp_service;
889 struct list_head *tmp;
890 struct list_head *nxt;
892 if (!atomic_dec_and_test(&req->rq_refcount))
895 if (req->rq_session.lc_state == LCS_ENTERED) {
896 lu_context_exit(&req->rq_session);
897 lu_context_fini(&req->rq_session);
900 if (req->rq_at_linked) {
901 spin_lock(&svcpt->scp_at_lock);
902 /* recheck with lock, in case it's unlinked by
903 * ptlrpc_at_check_timed() */
904 if (likely(req->rq_at_linked))
905 ptlrpc_at_remove_timed(req);
906 spin_unlock(&svcpt->scp_at_lock);
909 LASSERT(list_empty(&req->rq_timed_list));
911 /* finalize request */
912 if (req->rq_export) {
913 class_export_put(req->rq_export);
914 req->rq_export = NULL;
917 spin_lock(&svcpt->scp_lock);
919 list_add(&req->rq_list, &rqbd->rqbd_reqs);
921 refcount = --(rqbd->rqbd_refcount);
923 /* request buffer is now idle: add to history */
924 list_del(&rqbd->rqbd_list);
926 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_hist_rqbds);
927 svcpt->scp_hist_nrqbds++;
929 /* cull some history?
930 * I expect only about 1 or 2 rqbds need to be recycled here */
931 while (svcpt->scp_hist_nrqbds > svc->srv_hist_nrqbds_cpt_max) {
932 rqbd = list_entry(svcpt->scp_hist_rqbds.next,
933 struct ptlrpc_request_buffer_desc,
936 list_del(&rqbd->rqbd_list);
937 svcpt->scp_hist_nrqbds--;
939 /* remove rqbd's reqs from svc's req history while
940 * I've got the service lock */
941 list_for_each(tmp, &rqbd->rqbd_reqs) {
942 req = list_entry(tmp, struct ptlrpc_request,
944 /* Track the highest culled req seq */
945 if (req->rq_history_seq >
946 svcpt->scp_hist_seq_culled) {
947 svcpt->scp_hist_seq_culled =
950 list_del(&req->rq_history_list);
953 spin_unlock(&svcpt->scp_lock);
955 list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
956 req = list_entry(rqbd->rqbd_reqs.next,
957 struct ptlrpc_request,
959 list_del(&req->rq_list);
960 ptlrpc_server_free_request(req);
963 spin_lock(&svcpt->scp_lock);
965 * now all reqs including the embedded req has been
966 * disposed, schedule request buffer for re-use.
968 LASSERT(atomic_read(&rqbd->rqbd_req.rq_refcount) == 0);
969 list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
972 spin_unlock(&svcpt->scp_lock);
973 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
974 /* If we are low on memory, we are not interested in history */
975 list_del(&req->rq_list);
976 list_del_init(&req->rq_history_list);
978 /* Track the highest culled req seq */
979 if (req->rq_history_seq > svcpt->scp_hist_seq_culled)
980 svcpt->scp_hist_seq_culled = req->rq_history_seq;
982 spin_unlock(&svcpt->scp_lock);
984 ptlrpc_server_free_request(req);
986 spin_unlock(&svcpt->scp_lock);
990 /** Change request export and move hp request from old export to new */
991 void ptlrpc_request_change_export(struct ptlrpc_request *req,
992 struct obd_export *export)
994 if (req->rq_export != NULL) {
995 LASSERT(!list_empty(&req->rq_exp_list));
996 /* remove rq_exp_list from last export */
997 spin_lock_bh(&req->rq_export->exp_rpc_lock);
998 list_del_init(&req->rq_exp_list);
999 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1000 /* export has one reference already, so it`s safe to
1001 * add req to export queue here and get another
1002 * reference for request later */
1003 spin_lock_bh(&export->exp_rpc_lock);
1004 if (req->rq_ops != NULL) /* hp request */
1005 list_add(&req->rq_exp_list, &export->exp_hp_rpcs);
1007 list_add(&req->rq_exp_list, &export->exp_reg_rpcs);
1008 spin_unlock_bh(&export->exp_rpc_lock);
1010 class_export_rpc_dec(req->rq_export);
1011 class_export_put(req->rq_export);
1014 /* request takes one export refcount */
1015 req->rq_export = class_export_get(export);
1016 class_export_rpc_inc(export);
1022 * to finish a request: stop sending more early replies, and release
1025 static void ptlrpc_server_finish_request(struct ptlrpc_service_part *svcpt,
1026 struct ptlrpc_request *req)
1028 ptlrpc_server_hpreq_fini(req);
1030 ptlrpc_server_drop_request(req);
1034 * to finish a active request: stop sending more early replies, and release
1035 * the request. should be called after we finished handling the request.
1037 static void ptlrpc_server_finish_active_request(
1038 struct ptlrpc_service_part *svcpt,
1039 struct ptlrpc_request *req)
1041 spin_lock(&svcpt->scp_req_lock);
1042 ptlrpc_nrs_req_stop_nolock(req);
1043 svcpt->scp_nreqs_active--;
1045 svcpt->scp_nhreqs_active--;
1046 spin_unlock(&svcpt->scp_req_lock);
1048 ptlrpc_nrs_req_finalize(req);
1050 if (req->rq_export != NULL)
1051 class_export_rpc_dec(req->rq_export);
1053 ptlrpc_server_finish_request(svcpt, req);
1057 * This function makes sure dead exports are evicted in a timely manner.
1058 * This function is only called when some export receives a message (i.e.,
1059 * the network is up.)
1061 void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
1063 struct obd_export *oldest_exp;
1064 time_t oldest_time, new_time;
1070 /* Compensate for slow machines, etc, by faking our request time
1071 into the future. Although this can break the strict time-ordering
1072 of the list, we can be really lazy here - we don't have to evict
1073 at the exact right moment. Eventually, all silent exports
1074 will make it to the top of the list. */
1076 /* Do not pay attention on 1sec or smaller renewals. */
1077 new_time = cfs_time_current_sec() + extra_delay;
1078 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
1081 exp->exp_last_request_time = new_time;
1083 /* exports may get disconnected from the chain even though the
1084 export has references, so we must keep the spin lock while
1085 manipulating the lists */
1086 spin_lock(&exp->exp_obd->obd_dev_lock);
1088 if (list_empty(&exp->exp_obd_chain_timed)) {
1089 /* this one is not timed */
1090 spin_unlock(&exp->exp_obd->obd_dev_lock);
1094 list_move_tail(&exp->exp_obd_chain_timed,
1095 &exp->exp_obd->obd_exports_timed);
1097 oldest_exp = list_entry(exp->exp_obd->obd_exports_timed.next,
1098 struct obd_export, exp_obd_chain_timed);
1099 oldest_time = oldest_exp->exp_last_request_time;
1100 spin_unlock(&exp->exp_obd->obd_dev_lock);
1102 if (exp->exp_obd->obd_recovering) {
1103 /* be nice to everyone during recovery */
1108 /* Note - racing to start/reset the obd_eviction timer is safe */
1109 if (exp->exp_obd->obd_eviction_timer == 0) {
1110 /* Check if the oldest entry is expired. */
1111 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
1113 /* We need a second timer, in case the net was down and
1114 * it just came back. Since the pinger may skip every
1115 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
1116 * we better wait for 3. */
1117 exp->exp_obd->obd_eviction_timer =
1118 cfs_time_current_sec() + 3 * PING_INTERVAL;
1119 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
1120 exp->exp_obd->obd_name,
1121 obd_export_nid2str(oldest_exp), oldest_time);
1124 if (cfs_time_current_sec() >
1125 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
1126 /* The evictor won't evict anyone who we've heard from
1127 * recently, so we don't have to check before we start
1129 if (!ping_evictor_wake(exp))
1130 exp->exp_obd->obd_eviction_timer = 0;
1136 EXPORT_SYMBOL(ptlrpc_update_export_timer);
1139 * Sanity check request \a req.
1140 * Return 0 if all is ok, error code otherwise.
1142 static int ptlrpc_check_req(struct ptlrpc_request *req)
1144 struct obd_device *obd = req->rq_export->exp_obd;
1147 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
1148 req->rq_export->exp_conn_cnt)) {
1149 DEBUG_REQ(D_RPCTRACE, req,
1150 "DROPPING req from old connection %d < %d",
1151 lustre_msg_get_conn_cnt(req->rq_reqmsg),
1152 req->rq_export->exp_conn_cnt);
1155 if (unlikely(obd == NULL || obd->obd_fail)) {
1156 /* Failing over, don't handle any more reqs,
1157 * send error response instead. */
1158 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
1159 req, (obd != NULL) ? obd->obd_name : "unknown");
1161 } else if (lustre_msg_get_flags(req->rq_reqmsg) &
1162 (MSG_REPLAY | MSG_REQ_REPLAY_DONE) &&
1163 !obd->obd_recovering) {
1164 DEBUG_REQ(D_ERROR, req,
1165 "Invalid replay without recovery");
1166 class_fail_export(req->rq_export);
1168 } else if (lustre_msg_get_transno(req->rq_reqmsg) != 0 &&
1169 !obd->obd_recovering) {
1170 DEBUG_REQ(D_ERROR, req, "Invalid req with transno "
1171 LPU64" without recovery",
1172 lustre_msg_get_transno(req->rq_reqmsg));
1173 class_fail_export(req->rq_export);
1177 if (unlikely(rc < 0)) {
1178 req->rq_status = rc;
1184 static void ptlrpc_at_set_timer(struct ptlrpc_service_part *svcpt)
1186 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1189 if (array->paa_count == 0) {
1190 cfs_timer_disarm(&svcpt->scp_at_timer);
1194 /* Set timer for closest deadline */
1195 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
1198 ptlrpc_at_timer((unsigned long)svcpt);
1200 cfs_timer_arm(&svcpt->scp_at_timer, cfs_time_shift(next));
1201 CDEBUG(D_INFO, "armed %s at %+ds\n",
1202 svcpt->scp_service->srv_name, next);
1206 /* Add rpc to early reply check list */
1207 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
1209 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1210 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1211 struct ptlrpc_request *rq = NULL;
1217 if (req->rq_no_reply)
1220 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
1223 spin_lock(&svcpt->scp_at_lock);
1224 LASSERT(list_empty(&req->rq_timed_list));
1226 index = (unsigned long)req->rq_deadline % array->paa_size;
1227 if (array->paa_reqs_count[index] > 0) {
1228 /* latest rpcs will have the latest deadlines in the list,
1229 * so search backward. */
1230 list_for_each_entry_reverse(rq,
1231 &array->paa_reqs_array[index],
1233 if (req->rq_deadline >= rq->rq_deadline) {
1234 list_add(&req->rq_timed_list,
1235 &rq->rq_timed_list);
1241 /* Add the request at the head of the list */
1242 if (list_empty(&req->rq_timed_list))
1243 list_add(&req->rq_timed_list,
1244 &array->paa_reqs_array[index]);
1246 spin_lock(&req->rq_lock);
1247 req->rq_at_linked = 1;
1248 spin_unlock(&req->rq_lock);
1249 req->rq_at_index = index;
1250 array->paa_reqs_count[index]++;
1252 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
1253 array->paa_deadline = req->rq_deadline;
1254 ptlrpc_at_set_timer(svcpt);
1256 spin_unlock(&svcpt->scp_at_lock);
1262 ptlrpc_at_remove_timed(struct ptlrpc_request *req)
1264 struct ptlrpc_at_array *array;
1266 array = &req->rq_rqbd->rqbd_svcpt->scp_at_array;
1268 /* NB: must call with hold svcpt::scp_at_lock */
1269 LASSERT(!list_empty(&req->rq_timed_list));
1270 list_del_init(&req->rq_timed_list);
1272 spin_lock(&req->rq_lock);
1273 req->rq_at_linked = 0;
1274 spin_unlock(&req->rq_lock);
1276 array->paa_reqs_count[req->rq_at_index]--;
1281 * Attempt to extend the request deadline by sending an early reply to the
1284 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
1286 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1287 struct ptlrpc_request *reqcopy;
1288 struct lustre_msg *reqmsg;
1289 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
1293 /* deadline is when the client expects us to reply, margin is the
1294 difference between clients' and servers' expectations */
1295 DEBUG_REQ(D_ADAPTTO, req,
1296 "%ssending early reply (deadline %+lds, margin %+lds) for "
1297 "%d+%d", AT_OFF ? "AT off - not " : "",
1298 olddl, olddl - at_get(&svcpt->scp_at_estimate),
1299 at_get(&svcpt->scp_at_estimate), at_extra);
1305 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
1306 "not sending early reply. Consider increasing "
1307 "at_early_margin (%d)?", olddl, at_early_margin);
1309 /* Return an error so we're not re-added to the timed list. */
1313 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
1314 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
1315 "but no AT support");
1319 if (req->rq_export &&
1320 lustre_msg_get_flags(req->rq_reqmsg) &
1321 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
1322 /* During recovery, we don't want to send too many early
1323 * replies, but on the other hand we want to make sure the
1324 * client has enough time to resend if the rpc is lost. So
1325 * during the recovery period send at least 4 early replies,
1326 * spacing them every at_extra if we can. at_estimate should
1327 * always equal this fixed value during recovery. */
1328 at_measured(&svcpt->scp_at_estimate, min(at_extra,
1329 req->rq_export->exp_obd->obd_recovery_timeout / 4));
1331 /* We want to extend the request deadline by at_extra seconds,
1332 * so we set our service estimate to reflect how much time has
1333 * passed since this request arrived plus an additional
1334 * at_extra seconds. The client will calculate the new deadline
1335 * based on this service estimate (plus some additional time to
1336 * account for network latency). See ptlrpc_at_recv_early_reply
1338 at_measured(&svcpt->scp_at_estimate, at_extra +
1339 cfs_time_current_sec() -
1340 req->rq_arrival_time.tv_sec);
1342 /* Check to see if we've actually increased the deadline -
1343 * we may be past adaptive_max */
1344 if (req->rq_deadline >= req->rq_arrival_time.tv_sec +
1345 at_get(&svcpt->scp_at_estimate)) {
1346 DEBUG_REQ(D_WARNING, req, "Couldn't add any time "
1347 "(%ld/%ld), not sending early reply\n",
1348 olddl, req->rq_arrival_time.tv_sec +
1349 at_get(&svcpt->scp_at_estimate) -
1350 cfs_time_current_sec());
1355 reqcopy = ptlrpc_request_cache_alloc(GFP_NOFS);
1356 if (reqcopy == NULL)
1358 OBD_ALLOC_LARGE(reqmsg, req->rq_reqlen);
1360 GOTO(out_free, rc = -ENOMEM);
1363 reqcopy->rq_reply_state = NULL;
1364 reqcopy->rq_rep_swab_mask = 0;
1365 reqcopy->rq_pack_bulk = 0;
1366 reqcopy->rq_pack_udesc = 0;
1367 reqcopy->rq_packed_final = 0;
1368 sptlrpc_svc_ctx_addref(reqcopy);
1369 /* We only need the reqmsg for the magic */
1370 reqcopy->rq_reqmsg = reqmsg;
1371 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1373 LASSERT(atomic_read(&req->rq_refcount));
1374 /** if it is last refcount then early reply isn't needed */
1375 if (atomic_read(&req->rq_refcount) == 1) {
1376 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
1377 "abort sending early reply\n");
1378 GOTO(out, rc = -EINVAL);
1381 /* Connection ref */
1382 reqcopy->rq_export = class_conn2export(
1383 lustre_msg_get_handle(reqcopy->rq_reqmsg));
1384 if (reqcopy->rq_export == NULL)
1385 GOTO(out, rc = -ENODEV);
1388 class_export_rpc_inc(reqcopy->rq_export);
1389 if (reqcopy->rq_export->exp_obd &&
1390 reqcopy->rq_export->exp_obd->obd_fail)
1391 GOTO(out_put, rc = -ENODEV);
1393 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1397 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1400 /* Adjust our own deadline to what we told the client */
1401 req->rq_deadline = req->rq_arrival_time.tv_sec +
1402 at_get(&svcpt->scp_at_estimate);
1403 req->rq_early_count++; /* number sent, server side */
1405 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1408 /* Free the (early) reply state from lustre_pack_reply.
1409 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1410 ptlrpc_req_drop_rs(reqcopy);
1413 class_export_rpc_dec(reqcopy->rq_export);
1414 class_export_put(reqcopy->rq_export);
1416 sptlrpc_svc_ctx_decref(reqcopy);
1417 OBD_FREE_LARGE(reqmsg, req->rq_reqlen);
1419 ptlrpc_request_cache_free(reqcopy);
1423 /* Send early replies to everybody expiring within at_early_margin
1424 asking for at_extra time */
1425 static int ptlrpc_at_check_timed(struct ptlrpc_service_part *svcpt)
1427 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1428 struct ptlrpc_request *rq, *n;
1429 struct list_head work_list;
1432 time_t now = cfs_time_current_sec();
1433 cfs_duration_t delay;
1434 int first, counter = 0;
1437 spin_lock(&svcpt->scp_at_lock);
1438 if (svcpt->scp_at_check == 0) {
1439 spin_unlock(&svcpt->scp_at_lock);
1442 delay = cfs_time_sub(cfs_time_current(), svcpt->scp_at_checktime);
1443 svcpt->scp_at_check = 0;
1445 if (array->paa_count == 0) {
1446 spin_unlock(&svcpt->scp_at_lock);
1450 /* The timer went off, but maybe the nearest rpc already completed. */
1451 first = array->paa_deadline - now;
1452 if (first > at_early_margin) {
1453 /* We've still got plenty of time. Reset the timer. */
1454 ptlrpc_at_set_timer(svcpt);
1455 spin_unlock(&svcpt->scp_at_lock);
1459 /* We're close to a timeout, and we don't know how much longer the
1460 server will take. Send early replies to everyone expiring soon. */
1461 INIT_LIST_HEAD(&work_list);
1463 index = (unsigned long)array->paa_deadline % array->paa_size;
1464 count = array->paa_count;
1466 count -= array->paa_reqs_count[index];
1467 list_for_each_entry_safe(rq, n,
1468 &array->paa_reqs_array[index],
1470 if (rq->rq_deadline > now + at_early_margin) {
1471 /* update the earliest deadline */
1472 if (deadline == -1 ||
1473 rq->rq_deadline < deadline)
1474 deadline = rq->rq_deadline;
1478 ptlrpc_at_remove_timed(rq);
1480 * ptlrpc_server_drop_request() may drop
1481 * refcount to 0 already. Let's check this and
1482 * don't add entry to work_list
1484 if (likely(atomic_inc_not_zero(&rq->rq_refcount)))
1485 list_add(&rq->rq_timed_list, &work_list);
1489 if (++index >= array->paa_size)
1492 array->paa_deadline = deadline;
1493 /* we have a new earliest deadline, restart the timer */
1494 ptlrpc_at_set_timer(svcpt);
1496 spin_unlock(&svcpt->scp_at_lock);
1498 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1499 "replies\n", first, at_extra, counter);
1501 /* We're already past request deadlines before we even get a
1502 chance to send early replies */
1503 LCONSOLE_WARN("%s: This server is not able to keep up with "
1504 "request traffic (cpu-bound).\n",
1505 svcpt->scp_service->srv_name);
1506 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1507 "delay="CFS_DURATION_T"(jiff)\n",
1508 counter, svcpt->scp_nreqs_incoming,
1509 svcpt->scp_nreqs_active,
1510 at_get(&svcpt->scp_at_estimate), delay);
1513 /* we took additional refcount so entries can't be deleted from list, no
1514 * locking is needed */
1515 while (!list_empty(&work_list)) {
1516 rq = list_entry(work_list.next, struct ptlrpc_request,
1518 list_del_init(&rq->rq_timed_list);
1520 if (ptlrpc_at_send_early_reply(rq) == 0)
1521 ptlrpc_at_add_timed(rq);
1523 ptlrpc_server_drop_request(rq);
1526 RETURN(1); /* return "did_something" for liblustre */
1529 /* Check if we are already handling earlier incarnation of this request.
1530 * Called under &req->rq_export->exp_rpc_lock locked */
1531 static int ptlrpc_server_check_resend_in_progress(struct ptlrpc_request *req)
1533 struct ptlrpc_request *tmp = NULL;
1535 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) ||
1536 (atomic_read(&req->rq_export->exp_rpc_count) == 0))
1539 /* bulk request are aborted upon reconnect, don't try to
1541 if (req->rq_bulk_write || req->rq_bulk_read)
1544 /* This list should not be longer than max_requests in
1545 * flights on the client, so it is not all that long.
1546 * Also we only hit this codepath in case of a resent
1547 * request which makes it even more rarely hit */
1548 list_for_each_entry(tmp, &req->rq_export->exp_reg_rpcs,
1550 /* Found duplicate one */
1551 if (tmp->rq_xid == req->rq_xid)
1554 list_for_each_entry(tmp, &req->rq_export->exp_hp_rpcs,
1556 /* Found duplicate one */
1557 if (tmp->rq_xid == req->rq_xid)
1563 DEBUG_REQ(D_HA, req, "Found duplicate req in processing");
1564 DEBUG_REQ(D_HA, tmp, "Request being processed");
1569 * Put the request to the export list if the request may become
1570 * a high priority one.
1572 static int ptlrpc_server_hpreq_init(struct ptlrpc_service_part *svcpt,
1573 struct ptlrpc_request *req)
1575 struct list_head *list;
1580 if (svcpt->scp_service->srv_ops.so_hpreq_handler) {
1581 rc = svcpt->scp_service->srv_ops.so_hpreq_handler(req);
1586 if (req->rq_export) {
1588 /* Perform request specific check. We should do this
1589 * check before the request is added into exp_hp_rpcs
1590 * list otherwise it may hit swab race at LU-1044. */
1591 if (req->rq_ops->hpreq_check) {
1592 rc = req->rq_ops->hpreq_check(req);
1594 * XXX: Out of all current
1595 * ptlrpc_hpreq_ops::hpreq_check(), only
1596 * ldlm_cancel_hpreq_check() can return an
1597 * error code; other functions assert in
1598 * similar places, which seems odd.
1599 * What also does not seem right is that
1600 * handlers for those RPCs do not assert
1601 * on the same checks, but rather handle the
1602 * error cases. e.g. see ost_rw_hpreq_check(),
1603 * and ost_brw_read(), ost_brw_write().
1607 LASSERT(rc == 0 || rc == 1);
1610 list = &req->rq_export->exp_hp_rpcs;
1612 list = &req->rq_export->exp_reg_rpcs;
1615 /* do search for duplicated xid and the adding to the list
1617 spin_lock_bh(&req->rq_export->exp_rpc_lock);
1618 rc = ptlrpc_server_check_resend_in_progress(req);
1620 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1623 list_add(&req->rq_exp_list, list);
1624 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1627 ptlrpc_nrs_req_initialize(svcpt, req, !!hp);
1632 /** Remove the request from the export list. */
1633 static void ptlrpc_server_hpreq_fini(struct ptlrpc_request *req)
1636 if (req->rq_export) {
1637 /* refresh lock timeout again so that client has more
1638 * room to send lock cancel RPC. */
1639 if (req->rq_ops && req->rq_ops->hpreq_fini)
1640 req->rq_ops->hpreq_fini(req);
1642 spin_lock_bh(&req->rq_export->exp_rpc_lock);
1643 list_del_init(&req->rq_exp_list);
1644 spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1649 static int ptlrpc_hpreq_check(struct ptlrpc_request *req)
1654 static struct ptlrpc_hpreq_ops ptlrpc_hpreq_common = {
1655 .hpreq_check = ptlrpc_hpreq_check,
1658 /* Hi-Priority RPC check by RPC operation code. */
1659 int ptlrpc_hpreq_handler(struct ptlrpc_request *req)
1661 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1663 /* Check for export to let only reconnects for not yet evicted
1664 * export to become a HP rpc. */
1665 if ((req->rq_export != NULL) &&
1666 (opc == OBD_PING || opc == MDS_CONNECT || opc == OST_CONNECT))
1667 req->rq_ops = &ptlrpc_hpreq_common;
1671 EXPORT_SYMBOL(ptlrpc_hpreq_handler);
1673 static int ptlrpc_server_request_add(struct ptlrpc_service_part *svcpt,
1674 struct ptlrpc_request *req)
1679 rc = ptlrpc_server_hpreq_init(svcpt, req);
1683 /* the current thread is not the processing thread for this request
1684 * since that, but request is in exp_hp_list and can be find there.
1685 * Remove all relations between request and old thread. */
1686 req->rq_svc_thread->t_env->le_ses = NULL;
1687 req->rq_svc_thread = NULL;
1688 req->rq_session.lc_thread = NULL;
1690 ptlrpc_nrs_req_add(svcpt, req, !!rc);
1696 * Allow to handle high priority request
1697 * User can call it w/o any lock but need to hold
1698 * ptlrpc_service_part::scp_req_lock to get reliable result
1700 static bool ptlrpc_server_allow_high(struct ptlrpc_service_part *svcpt,
1703 int running = svcpt->scp_nthrs_running;
1705 if (!nrs_svcpt_has_hp(svcpt))
1711 if (ptlrpc_nrs_req_throttling_nolock(svcpt, true))
1714 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1715 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1716 /* leave just 1 thread for normal RPCs */
1717 running = PTLRPC_NTHRS_INIT;
1718 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1722 if (svcpt->scp_nreqs_active >= running - 1)
1725 if (svcpt->scp_nhreqs_active == 0)
1728 return !ptlrpc_nrs_req_pending_nolock(svcpt, false) ||
1729 svcpt->scp_hreq_count < svcpt->scp_service->srv_hpreq_ratio;
1732 static bool ptlrpc_server_high_pending(struct ptlrpc_service_part *svcpt,
1735 return ptlrpc_server_allow_high(svcpt, force) &&
1736 ptlrpc_nrs_req_pending_nolock(svcpt, true);
1740 * Only allow normal priority requests on a service that has a high-priority
1741 * queue if forced (i.e. cleanup), if there are other high priority requests
1742 * already being processed (i.e. those threads can service more high-priority
1743 * requests), or if there are enough idle threads that a later thread can do
1744 * a high priority request.
1745 * User can call it w/o any lock but need to hold
1746 * ptlrpc_service_part::scp_req_lock to get reliable result
1748 static bool ptlrpc_server_allow_normal(struct ptlrpc_service_part *svcpt,
1751 int running = svcpt->scp_nthrs_running;
1753 if (1) /* always allow to handle normal request for liblustre */
1756 if (unlikely(svcpt->scp_service->srv_req_portal == MDS_REQUEST_PORTAL &&
1757 CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))) {
1758 /* leave just 1 thread for normal RPCs */
1759 running = PTLRPC_NTHRS_INIT;
1760 if (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL)
1767 if (ptlrpc_nrs_req_throttling_nolock(svcpt, false))
1770 if (svcpt->scp_nreqs_active < running - 2)
1773 if (svcpt->scp_nreqs_active >= running - 1)
1776 return svcpt->scp_nhreqs_active > 0 || !nrs_svcpt_has_hp(svcpt);
1779 static bool ptlrpc_server_normal_pending(struct ptlrpc_service_part *svcpt,
1782 return ptlrpc_server_allow_normal(svcpt, force) &&
1783 ptlrpc_nrs_req_pending_nolock(svcpt, false);
1787 * Returns true if there are requests available in incoming
1788 * request queue for processing and it is allowed to fetch them.
1789 * User can call it w/o any lock but need to hold ptlrpc_service::scp_req_lock
1790 * to get reliable result
1791 * \see ptlrpc_server_allow_normal
1792 * \see ptlrpc_server_allow high
1795 ptlrpc_server_request_pending(struct ptlrpc_service_part *svcpt, bool force)
1797 return ptlrpc_server_high_pending(svcpt, force) ||
1798 ptlrpc_server_normal_pending(svcpt, force);
1802 * Fetch a request for processing from queue of unprocessed requests.
1803 * Favors high-priority requests.
1804 * Returns a pointer to fetched request.
1806 static struct ptlrpc_request *
1807 ptlrpc_server_request_get(struct ptlrpc_service_part *svcpt, bool force)
1809 struct ptlrpc_request *req = NULL;
1812 spin_lock(&svcpt->scp_req_lock);
1814 /* !@%$# liblustre only has 1 thread */
1815 if (atomic_read(&svcpt->scp_nreps_difficult) != 0) {
1816 spin_unlock(&svcpt->scp_req_lock);
1821 if (ptlrpc_server_high_pending(svcpt, force)) {
1822 req = ptlrpc_nrs_req_get_nolock(svcpt, true, force);
1824 svcpt->scp_hreq_count++;
1829 if (ptlrpc_server_normal_pending(svcpt, force)) {
1830 req = ptlrpc_nrs_req_get_nolock(svcpt, false, force);
1832 svcpt->scp_hreq_count = 0;
1837 spin_unlock(&svcpt->scp_req_lock);
1841 svcpt->scp_nreqs_active++;
1843 svcpt->scp_nhreqs_active++;
1845 spin_unlock(&svcpt->scp_req_lock);
1847 if (likely(req->rq_export))
1848 class_export_rpc_inc(req->rq_export);
1854 * Handle freshly incoming reqs, add to timed early reply list,
1855 * pass on to regular request queue.
1856 * All incoming requests pass through here before getting into
1857 * ptlrpc_server_handle_req later on.
1860 ptlrpc_server_handle_req_in(struct ptlrpc_service_part *svcpt,
1861 struct ptlrpc_thread *thread)
1863 struct ptlrpc_service *svc = svcpt->scp_service;
1864 struct ptlrpc_request *req;
1869 spin_lock(&svcpt->scp_lock);
1870 if (list_empty(&svcpt->scp_req_incoming)) {
1871 spin_unlock(&svcpt->scp_lock);
1875 req = list_entry(svcpt->scp_req_incoming.next,
1876 struct ptlrpc_request, rq_list);
1877 list_del_init(&req->rq_list);
1878 svcpt->scp_nreqs_incoming--;
1879 /* Consider this still a "queued" request as far as stats are
1881 spin_unlock(&svcpt->scp_lock);
1883 /* go through security check/transform */
1884 rc = sptlrpc_svc_unwrap_request(req);
1888 case SECSVC_COMPLETE:
1889 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1898 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
1899 * redo it wouldn't be harmful.
1901 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1902 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1904 CERROR("error unpacking request: ptl %d from %s "
1905 "x"LPU64"\n", svc->srv_req_portal,
1906 libcfs_id2str(req->rq_peer), req->rq_xid);
1911 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1913 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1914 LPU64"\n", svc->srv_req_portal,
1915 libcfs_id2str(req->rq_peer), req->rq_xid);
1919 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1920 lustre_msg_get_opc(req->rq_reqmsg) == cfs_fail_val) {
1921 CERROR("drop incoming rpc opc %u, x"LPU64"\n",
1922 cfs_fail_val, req->rq_xid);
1927 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1928 CERROR("wrong packet type received (type=%u) from %s\n",
1929 lustre_msg_get_type(req->rq_reqmsg),
1930 libcfs_id2str(req->rq_peer));
1934 switch(lustre_msg_get_opc(req->rq_reqmsg)) {
1937 req->rq_bulk_write = 1;
1941 case MGS_CONFIG_READ:
1942 req->rq_bulk_read = 1;
1946 CDEBUG(D_RPCTRACE, "got req x"LPU64"\n", req->rq_xid);
1948 req->rq_export = class_conn2export(
1949 lustre_msg_get_handle(req->rq_reqmsg));
1950 if (req->rq_export) {
1951 rc = ptlrpc_check_req(req);
1953 rc = sptlrpc_target_export_check(req->rq_export, req);
1955 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1956 "illegal security flavor,");
1961 ptlrpc_update_export_timer(req->rq_export, 0);
1964 /* req_in handling should/must be fast */
1965 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1966 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1967 cfs_time_sub(cfs_time_current_sec(),
1968 req->rq_arrival_time.tv_sec));
1970 /* Set rpc server deadline and add it to the timed list */
1971 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1972 MSGHDR_AT_SUPPORT) ?
1973 /* The max time the client expects us to take */
1974 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1975 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1976 if (unlikely(deadline == 0)) {
1977 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1981 /* Skip early reply */
1982 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_RESEND))
1983 req->rq_deadline += obd_timeout;
1985 req->rq_svc_thread = thread;
1986 if (thread != NULL) {
1987 /* initialize request session, it is needed for request
1988 * processing by target */
1989 rc = lu_context_init(&req->rq_session, LCT_SERVER_SESSION |
1992 CERROR("%s: failure to initialize session: rc = %d\n",
1993 thread->t_name, rc);
1996 req->rq_session.lc_thread = thread;
1997 lu_context_enter(&req->rq_session);
1998 thread->t_env->le_ses = &req->rq_session;
2001 ptlrpc_at_add_timed(req);
2003 /* Move it over to the request processing queue */
2004 rc = ptlrpc_server_request_add(svcpt, req);
2008 wake_up(&svcpt->scp_waitq);
2012 ptlrpc_server_finish_request(svcpt, req);
2018 * Main incoming request handling logic.
2019 * Calls handler function from service to do actual processing.
2022 ptlrpc_server_handle_request(struct ptlrpc_service_part *svcpt,
2023 struct ptlrpc_thread *thread)
2025 struct ptlrpc_service *svc = svcpt->scp_service;
2026 struct ptlrpc_request *request;
2027 struct timeval work_start;
2028 struct timeval work_end;
2034 request = ptlrpc_server_request_get(svcpt, false);
2035 if (request == NULL)
2038 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
2039 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
2040 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
2041 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
2043 if (unlikely(fail_opc)) {
2044 if (request->rq_export && request->rq_ops)
2045 OBD_FAIL_TIMEOUT(fail_opc, 4);
2048 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
2050 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
2051 libcfs_debug_dumplog();
2053 do_gettimeofday(&work_start);
2054 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
2055 if (likely(svc->srv_stats != NULL)) {
2056 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
2058 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
2059 svcpt->scp_nreqs_incoming);
2060 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
2061 svcpt->scp_nreqs_active);
2062 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
2063 at_get(&svcpt->scp_at_estimate));
2066 if (likely(request->rq_export)) {
2067 if (unlikely(ptlrpc_check_req(request)))
2069 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
2072 /* Discard requests queued for longer than the deadline.
2073 The deadline is increased if we send an early reply. */
2074 if (cfs_time_current_sec() > request->rq_deadline) {
2075 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
2076 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
2077 libcfs_id2str(request->rq_peer),
2078 cfs_time_sub(request->rq_deadline,
2079 request->rq_arrival_time.tv_sec),
2080 cfs_time_sub(cfs_time_current_sec(),
2081 request->rq_deadline));
2085 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
2086 "%s:%s+%d:%d:x"LPU64":%s:%d\n", current_comm(),
2087 (request->rq_export ?
2088 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
2089 (request->rq_export ?
2090 atomic_read(&request->rq_export->exp_refcount) : -99),
2091 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
2092 libcfs_id2str(request->rq_peer),
2093 lustre_msg_get_opc(request->rq_reqmsg));
2095 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
2096 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, cfs_fail_val);
2098 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
2100 /* re-assign request and sesson thread to the current one */
2101 request->rq_svc_thread = thread;
2102 if (thread != NULL) {
2103 LASSERT(request->rq_session.lc_thread == NULL);
2104 request->rq_session.lc_thread = thread;
2105 thread->t_env->le_ses = &request->rq_session;
2107 svc->srv_ops.so_req_handler(request);
2109 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
2112 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
2113 DEBUG_REQ(D_WARNING, request, "Request took longer "
2114 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
2115 " client may timeout.",
2116 cfs_time_sub(request->rq_deadline,
2117 request->rq_arrival_time.tv_sec),
2118 cfs_time_sub(cfs_time_current_sec(),
2119 request->rq_deadline));
2122 do_gettimeofday(&work_end);
2123 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
2124 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
2125 "%s:%s+%d:%d:x"LPU64":%s:%d Request procesed in "
2126 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
2128 (request->rq_export ?
2129 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
2130 (request->rq_export ?
2131 atomic_read(&request->rq_export->exp_refcount) : -99),
2132 lustre_msg_get_status(request->rq_reqmsg),
2134 libcfs_id2str(request->rq_peer),
2135 lustre_msg_get_opc(request->rq_reqmsg),
2137 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
2138 (request->rq_repmsg ?
2139 lustre_msg_get_transno(request->rq_repmsg) :
2140 request->rq_transno),
2142 (request->rq_repmsg ?
2143 lustre_msg_get_status(request->rq_repmsg) : -999));
2144 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
2145 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
2146 int opc = opcode_offset(op);
2147 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
2148 LASSERT(opc < LUSTRE_MAX_OPCODES);
2149 lprocfs_counter_add(svc->srv_stats,
2150 opc + EXTRA_MAX_OPCODES,
2154 if (unlikely(request->rq_early_count)) {
2155 DEBUG_REQ(D_ADAPTTO, request,
2156 "sent %d early replies before finishing in "
2158 request->rq_early_count,
2159 cfs_time_sub(work_end.tv_sec,
2160 request->rq_arrival_time.tv_sec));
2163 ptlrpc_server_finish_active_request(svcpt, request);
2169 * An internal function to process a single reply state object.
2172 ptlrpc_handle_rs(struct ptlrpc_reply_state *rs)
2174 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
2175 struct ptlrpc_service *svc = svcpt->scp_service;
2176 struct obd_export *exp;
2181 exp = rs->rs_export;
2183 LASSERT(rs->rs_difficult);
2184 LASSERT(rs->rs_scheduled);
2185 LASSERT(list_empty(&rs->rs_list));
2187 spin_lock(&exp->exp_lock);
2188 /* Noop if removed already */
2189 list_del_init(&rs->rs_exp_list);
2190 spin_unlock(&exp->exp_lock);
2192 /* The disk commit callback holds exp_uncommitted_replies_lock while it
2193 * iterates over newly committed replies, removing them from
2194 * exp_uncommitted_replies. It then drops this lock and schedules the
2195 * replies it found for handling here.
2197 * We can avoid contention for exp_uncommitted_replies_lock between the
2198 * HRT threads and further commit callbacks by checking rs_committed
2199 * which is set in the commit callback while it holds both
2200 * rs_lock and exp_uncommitted_reples.
2202 * If we see rs_committed clear, the commit callback _may_ not have
2203 * handled this reply yet and we race with it to grab
2204 * exp_uncommitted_replies_lock before removing the reply from
2205 * exp_uncommitted_replies. Note that if we lose the race and the
2206 * reply has already been removed, list_del_init() is a noop.
2208 * If we see rs_committed set, we know the commit callback is handling,
2209 * or has handled this reply since store reordering might allow us to
2210 * see rs_committed set out of sequence. But since this is done
2211 * holding rs_lock, we can be sure it has all completed once we hold
2212 * rs_lock, which we do right next.
2214 if (!rs->rs_committed) {
2215 spin_lock(&exp->exp_uncommitted_replies_lock);
2216 list_del_init(&rs->rs_obd_list);
2217 spin_unlock(&exp->exp_uncommitted_replies_lock);
2220 spin_lock(&rs->rs_lock);
2222 been_handled = rs->rs_handled;
2225 nlocks = rs->rs_nlocks; /* atomic "steal", but */
2226 rs->rs_nlocks = 0; /* locks still on rs_locks! */
2228 if (nlocks == 0 && !been_handled) {
2229 /* If we see this, we should already have seen the warning
2230 * in mds_steal_ack_locks() */
2231 CDEBUG(D_HA, "All locks stolen from rs %p x"LPD64".t"LPD64
2234 rs->rs_xid, rs->rs_transno, rs->rs_opc,
2235 libcfs_nid2str(exp->exp_connection->c_peer.nid));
2238 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
2239 spin_unlock(&rs->rs_lock);
2241 if (!been_handled && rs->rs_on_net) {
2242 LNetMDUnlink(rs->rs_md_h);
2243 /* Ignore return code; we're racing with completion */
2246 while (nlocks-- > 0)
2247 ldlm_lock_decref(&rs->rs_locks[nlocks],
2248 rs->rs_modes[nlocks]);
2250 spin_lock(&rs->rs_lock);
2253 rs->rs_scheduled = 0;
2255 if (!rs->rs_on_net) {
2257 spin_unlock(&rs->rs_lock);
2259 class_export_put (exp);
2260 rs->rs_export = NULL;
2261 ptlrpc_rs_decref(rs);
2262 if (atomic_dec_and_test(&svcpt->scp_nreps_difficult) &&
2263 svc->srv_is_stopping)
2264 wake_up_all(&svcpt->scp_waitq);
2268 /* still on the net; callback will schedule */
2269 spin_unlock(&rs->rs_lock);
2276 * Check whether given service has a reply available for processing
2279 * \param svc a ptlrpc service
2280 * \retval 0 no replies processed
2281 * \retval 1 one reply processed
2284 ptlrpc_server_handle_reply(struct ptlrpc_service_part *svcpt)
2286 struct ptlrpc_reply_state *rs = NULL;
2289 spin_lock(&svcpt->scp_rep_lock);
2290 if (!list_empty(&svcpt->scp_rep_queue)) {
2291 rs = list_entry(svcpt->scp_rep_queue.prev,
2292 struct ptlrpc_reply_state,
2294 list_del_init(&rs->rs_list);
2296 spin_unlock(&svcpt->scp_rep_lock);
2298 ptlrpc_handle_rs(rs);
2302 /* FIXME make use of timeout later */
2304 liblustre_check_services (void *arg)
2306 int did_something = 0;
2308 struct list_head *tmp, *nxt;
2311 /* I'm relying on being single threaded, not to have to lock
2312 * ptlrpc_all_services etc */
2313 list_for_each_safe(tmp, nxt, &ptlrpc_all_services) {
2314 struct ptlrpc_service *svc =
2315 list_entry(tmp, struct ptlrpc_service, srv_list);
2316 struct ptlrpc_service_part *svcpt;
2318 LASSERT(svc->srv_ncpts == 1);
2319 svcpt = svc->srv_parts[0];
2321 if (svcpt->scp_nthrs_running != 0) /* I've recursed */
2324 /* service threads can block for bulk, so this limits us
2325 * (arbitrarily) to recursing 1 stack frame per service.
2326 * Note that the problem with recursion is that we have to
2327 * unwind completely before our caller can resume. */
2329 svcpt->scp_nthrs_running++;
2332 rc = ptlrpc_server_handle_req_in(svcpt, NULL);
2333 rc |= ptlrpc_server_handle_reply(svcpt);
2334 rc |= ptlrpc_at_check_timed(svcpt);
2335 rc |= ptlrpc_server_handle_request(svcpt, NULL);
2336 rc |= (ptlrpc_server_post_idle_rqbds(svcpt) > 0);
2337 did_something |= rc;
2340 svcpt->scp_nthrs_running--;
2343 RETURN(did_something);
2345 #define ptlrpc_stop_all_threads(s) do {} while (0)
2347 #else /* __KERNEL__ */
2350 ptlrpc_check_rqbd_pool(struct ptlrpc_service_part *svcpt)
2352 int avail = svcpt->scp_nrqbds_posted;
2353 int low_water = test_req_buffer_pressure ? 0 :
2354 svcpt->scp_service->srv_nbuf_per_group / 2;
2356 /* NB I'm not locking; just looking. */
2358 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
2359 * allowed the request history to grow out of control. We could put a
2360 * sanity check on that here and cull some history if we need the
2363 if (avail <= low_water)
2364 ptlrpc_grow_req_bufs(svcpt, 1);
2366 if (svcpt->scp_service->srv_stats) {
2367 lprocfs_counter_add(svcpt->scp_service->srv_stats,
2368 PTLRPC_REQBUF_AVAIL_CNTR, avail);
2373 ptlrpc_retry_rqbds(void *arg)
2375 struct ptlrpc_service_part *svcpt = (struct ptlrpc_service_part *)arg;
2377 svcpt->scp_rqbd_timeout = 0;
2382 ptlrpc_threads_enough(struct ptlrpc_service_part *svcpt)
2384 return svcpt->scp_nreqs_active <
2385 svcpt->scp_nthrs_running - 1 -
2386 (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL);
2390 * allowed to create more threads
2391 * user can call it w/o any lock but need to hold
2392 * ptlrpc_service_part::scp_lock to get reliable result
2395 ptlrpc_threads_increasable(struct ptlrpc_service_part *svcpt)
2397 return svcpt->scp_nthrs_running +
2398 svcpt->scp_nthrs_starting <
2399 svcpt->scp_service->srv_nthrs_cpt_limit;
2403 * too many requests and allowed to create more threads
2406 ptlrpc_threads_need_create(struct ptlrpc_service_part *svcpt)
2408 return !ptlrpc_threads_enough(svcpt) &&
2409 ptlrpc_threads_increasable(svcpt);
2413 ptlrpc_thread_stopping(struct ptlrpc_thread *thread)
2415 return thread_is_stopping(thread) ||
2416 thread->t_svcpt->scp_service->srv_is_stopping;
2420 ptlrpc_rqbd_pending(struct ptlrpc_service_part *svcpt)
2422 return !list_empty(&svcpt->scp_rqbd_idle) &&
2423 svcpt->scp_rqbd_timeout == 0;
2427 ptlrpc_at_check(struct ptlrpc_service_part *svcpt)
2429 return svcpt->scp_at_check;
2433 * requests wait on preprocessing
2434 * user can call it w/o any lock but need to hold
2435 * ptlrpc_service_part::scp_lock to get reliable result
2438 ptlrpc_server_request_incoming(struct ptlrpc_service_part *svcpt)
2440 return !list_empty(&svcpt->scp_req_incoming);
2443 static __attribute__((__noinline__)) int
2444 ptlrpc_wait_event(struct ptlrpc_service_part *svcpt,
2445 struct ptlrpc_thread *thread)
2447 /* Don't exit while there are replies to be handled */
2448 struct l_wait_info lwi = LWI_TIMEOUT(svcpt->scp_rqbd_timeout,
2449 ptlrpc_retry_rqbds, svcpt);
2451 lc_watchdog_disable(thread->t_watchdog);
2455 l_wait_event_exclusive_head(svcpt->scp_waitq,
2456 ptlrpc_thread_stopping(thread) ||
2457 ptlrpc_server_request_incoming(svcpt) ||
2458 ptlrpc_server_request_pending(svcpt, false) ||
2459 ptlrpc_rqbd_pending(svcpt) ||
2460 ptlrpc_at_check(svcpt), &lwi);
2462 if (ptlrpc_thread_stopping(thread))
2465 lc_watchdog_touch(thread->t_watchdog,
2466 ptlrpc_server_get_timeout(svcpt));
2471 * Main thread body for service threads.
2472 * Waits in a loop waiting for new requests to process to appear.
2473 * Every time an incoming requests is added to its queue, a waitq
2474 * is woken up and one of the threads will handle it.
2476 static int ptlrpc_main(void *arg)
2478 struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
2479 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2480 struct ptlrpc_service *svc = svcpt->scp_service;
2481 struct ptlrpc_reply_state *rs;
2482 #ifdef WITH_GROUP_INFO
2483 struct group_info *ginfo = NULL;
2486 int counter = 0, rc = 0;
2489 thread->t_pid = current_pid();
2490 unshare_fs_struct();
2492 /* NB: we will call cfs_cpt_bind() for all threads, because we
2493 * might want to run lustre server only on a subset of system CPUs,
2494 * in that case ->scp_cpt is CFS_CPT_ANY */
2495 rc = cfs_cpt_bind(svc->srv_cptable, svcpt->scp_cpt);
2497 CWARN("%s: failed to bind %s on CPT %d\n",
2498 svc->srv_name, thread->t_name, svcpt->scp_cpt);
2501 #ifdef WITH_GROUP_INFO
2502 ginfo = groups_alloc(0);
2508 set_current_groups(ginfo);
2509 put_group_info(ginfo);
2512 if (svc->srv_ops.so_thr_init != NULL) {
2513 rc = svc->srv_ops.so_thr_init(thread);
2524 rc = lu_context_init(&env->le_ctx,
2525 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
2529 thread->t_env = env;
2530 env->le_ctx.lc_thread = thread;
2531 env->le_ctx.lc_cookie = 0x6;
2533 while (!list_empty(&svcpt->scp_rqbd_idle)) {
2534 rc = ptlrpc_server_post_idle_rqbds(svcpt);
2538 CERROR("Failed to post rqbd for %s on CPT %d: %d\n",
2539 svc->srv_name, svcpt->scp_cpt, rc);
2543 /* Alloc reply state structure for this one */
2544 OBD_ALLOC_LARGE(rs, svc->srv_max_reply_size);
2550 spin_lock(&svcpt->scp_lock);
2552 LASSERT(thread_is_starting(thread));
2553 thread_clear_flags(thread, SVC_STARTING);
2555 LASSERT(svcpt->scp_nthrs_starting == 1);
2556 svcpt->scp_nthrs_starting--;
2558 /* SVC_STOPPING may already be set here if someone else is trying
2559 * to stop the service while this new thread has been dynamically
2560 * forked. We still set SVC_RUNNING to let our creator know that
2561 * we are now running, however we will exit as soon as possible */
2562 thread_add_flags(thread, SVC_RUNNING);
2563 svcpt->scp_nthrs_running++;
2564 spin_unlock(&svcpt->scp_lock);
2566 /* wake up our creator in case he's still waiting. */
2567 wake_up(&thread->t_ctl_waitq);
2569 thread->t_watchdog = lc_watchdog_add(ptlrpc_server_get_timeout(svcpt),
2572 spin_lock(&svcpt->scp_rep_lock);
2573 list_add(&rs->rs_list, &svcpt->scp_rep_idle);
2574 wake_up(&svcpt->scp_rep_waitq);
2575 spin_unlock(&svcpt->scp_rep_lock);
2577 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
2578 svcpt->scp_nthrs_running);
2580 /* XXX maintain a list of all managed devices: insert here */
2581 while (!ptlrpc_thread_stopping(thread)) {
2582 if (ptlrpc_wait_event(svcpt, thread))
2585 ptlrpc_check_rqbd_pool(svcpt);
2587 if (ptlrpc_threads_need_create(svcpt)) {
2588 /* Ignore return code - we tried... */
2589 ptlrpc_start_thread(svcpt, 0);
2592 /* reset le_ses to initial state */
2594 /* Process all incoming reqs before handling any */
2595 if (ptlrpc_server_request_incoming(svcpt)) {
2596 lu_context_enter(&env->le_ctx);
2597 ptlrpc_server_handle_req_in(svcpt, thread);
2598 lu_context_exit(&env->le_ctx);
2600 /* but limit ourselves in case of flood */
2601 if (counter++ < 100)
2606 if (ptlrpc_at_check(svcpt))
2607 ptlrpc_at_check_timed(svcpt);
2609 if (ptlrpc_server_request_pending(svcpt, false)) {
2610 lu_context_enter(&env->le_ctx);
2611 ptlrpc_server_handle_request(svcpt, thread);
2612 lu_context_exit(&env->le_ctx);
2615 if (ptlrpc_rqbd_pending(svcpt) &&
2616 ptlrpc_server_post_idle_rqbds(svcpt) < 0) {
2617 /* I just failed to repost request buffers.
2618 * Wait for a timeout (unless something else
2619 * happens) before I try again */
2620 svcpt->scp_rqbd_timeout = cfs_time_seconds(1) / 10;
2621 CDEBUG(D_RPCTRACE, "Posted buffers: %d\n",
2622 svcpt->scp_nrqbds_posted);
2626 lc_watchdog_delete(thread->t_watchdog);
2627 thread->t_watchdog = NULL;
2631 * deconstruct service specific state created by ptlrpc_start_thread()
2633 if (svc->srv_ops.so_thr_done != NULL)
2634 svc->srv_ops.so_thr_done(thread);
2637 lu_context_fini(&env->le_ctx);
2641 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2642 thread, thread->t_pid, thread->t_id, rc);
2644 spin_lock(&svcpt->scp_lock);
2645 if (thread_test_and_clear_flags(thread, SVC_STARTING))
2646 svcpt->scp_nthrs_starting--;
2648 if (thread_test_and_clear_flags(thread, SVC_RUNNING)) {
2649 /* must know immediately */
2650 svcpt->scp_nthrs_running--;
2654 thread_add_flags(thread, SVC_STOPPED);
2656 wake_up(&thread->t_ctl_waitq);
2657 spin_unlock(&svcpt->scp_lock);
2662 static int hrt_dont_sleep(struct ptlrpc_hr_thread *hrt,
2663 struct list_head *replies)
2667 spin_lock(&hrt->hrt_lock);
2669 list_splice_init(&hrt->hrt_queue, replies);
2670 result = ptlrpc_hr.hr_stopping || !list_empty(replies);
2672 spin_unlock(&hrt->hrt_lock);
2677 * Main body of "handle reply" function.
2678 * It processes acked reply states
2680 static int ptlrpc_hr_main(void *arg)
2682 struct ptlrpc_hr_thread *hrt = (struct ptlrpc_hr_thread *)arg;
2683 struct ptlrpc_hr_partition *hrp = hrt->hrt_partition;
2684 struct list_head replies;
2685 char threadname[20];
2688 INIT_LIST_HEAD(&replies);
2689 snprintf(threadname, sizeof(threadname), "ptlrpc_hr%02d_%03d",
2690 hrp->hrp_cpt, hrt->hrt_id);
2691 unshare_fs_struct();
2693 rc = cfs_cpt_bind(ptlrpc_hr.hr_cpt_table, hrp->hrp_cpt);
2695 CWARN("Failed to bind %s on CPT %d of CPT table %p: rc = %d\n",
2696 threadname, hrp->hrp_cpt, ptlrpc_hr.hr_cpt_table, rc);
2699 atomic_inc(&hrp->hrp_nstarted);
2700 wake_up(&ptlrpc_hr.hr_waitq);
2702 while (!ptlrpc_hr.hr_stopping) {
2703 l_wait_condition(hrt->hrt_waitq, hrt_dont_sleep(hrt, &replies));
2705 while (!list_empty(&replies)) {
2706 struct ptlrpc_reply_state *rs;
2708 rs = list_entry(replies.prev,
2709 struct ptlrpc_reply_state,
2711 list_del_init(&rs->rs_list);
2712 ptlrpc_handle_rs(rs);
2716 atomic_inc(&hrp->hrp_nstopped);
2717 wake_up(&ptlrpc_hr.hr_waitq);
2722 static void ptlrpc_stop_hr_threads(void)
2724 struct ptlrpc_hr_partition *hrp;
2728 ptlrpc_hr.hr_stopping = 1;
2730 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2731 if (hrp->hrp_thrs == NULL)
2732 continue; /* uninitialized */
2733 for (j = 0; j < hrp->hrp_nthrs; j++)
2734 wake_up_all(&hrp->hrp_thrs[j].hrt_waitq);
2737 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2738 if (hrp->hrp_thrs == NULL)
2739 continue; /* uninitialized */
2740 wait_event(ptlrpc_hr.hr_waitq,
2741 atomic_read(&hrp->hrp_nstopped) ==
2742 atomic_read(&hrp->hrp_nstarted));
2746 static int ptlrpc_start_hr_threads(void)
2748 struct ptlrpc_hr_partition *hrp;
2753 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
2756 for (j = 0; j < hrp->hrp_nthrs; j++) {
2757 struct ptlrpc_hr_thread *hrt = &hrp->hrp_thrs[j];
2758 struct task_struct *task;
2760 task = kthread_run(ptlrpc_hr_main,
2762 "ptlrpc_hr%02d_%03d",
2771 wait_event(ptlrpc_hr.hr_waitq,
2772 atomic_read(&hrp->hrp_nstarted) == j);
2775 CERROR("cannot start reply handler thread %d:%d: "
2776 "rc = %d\n", i, j, rc);
2777 ptlrpc_stop_hr_threads();
2785 static void ptlrpc_svcpt_stop_threads(struct ptlrpc_service_part *svcpt)
2787 struct l_wait_info lwi = { 0 };
2788 struct ptlrpc_thread *thread;
2789 struct list_head zombie;
2793 CDEBUG(D_INFO, "Stopping threads for service %s\n",
2794 svcpt->scp_service->srv_name);
2796 INIT_LIST_HEAD(&zombie);
2797 spin_lock(&svcpt->scp_lock);
2798 /* let the thread know that we would like it to stop asap */
2799 list_for_each_entry(thread, &svcpt->scp_threads, t_link) {
2800 CDEBUG(D_INFO, "Stopping thread %s #%u\n",
2801 svcpt->scp_service->srv_thread_name, thread->t_id);
2802 thread_add_flags(thread, SVC_STOPPING);
2805 wake_up_all(&svcpt->scp_waitq);
2807 while (!list_empty(&svcpt->scp_threads)) {
2808 thread = list_entry(svcpt->scp_threads.next,
2809 struct ptlrpc_thread, t_link);
2810 if (thread_is_stopped(thread)) {
2811 list_del(&thread->t_link);
2812 list_add(&thread->t_link, &zombie);
2815 spin_unlock(&svcpt->scp_lock);
2817 CDEBUG(D_INFO, "waiting for stopping-thread %s #%u\n",
2818 svcpt->scp_service->srv_thread_name, thread->t_id);
2819 l_wait_event(thread->t_ctl_waitq,
2820 thread_is_stopped(thread), &lwi);
2822 spin_lock(&svcpt->scp_lock);
2825 spin_unlock(&svcpt->scp_lock);
2827 while (!list_empty(&zombie)) {
2828 thread = list_entry(zombie.next,
2829 struct ptlrpc_thread, t_link);
2830 list_del(&thread->t_link);
2831 OBD_FREE_PTR(thread);
2837 * Stops all threads of a particular service \a svc
2839 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2841 struct ptlrpc_service_part *svcpt;
2845 ptlrpc_service_for_each_part(svcpt, i, svc) {
2846 if (svcpt->scp_service != NULL)
2847 ptlrpc_svcpt_stop_threads(svcpt);
2852 EXPORT_SYMBOL(ptlrpc_stop_all_threads);
2854 int ptlrpc_start_threads(struct ptlrpc_service *svc)
2861 /* We require 2 threads min, see note in ptlrpc_server_handle_request */
2862 LASSERT(svc->srv_nthrs_cpt_init >= PTLRPC_NTHRS_INIT);
2864 for (i = 0; i < svc->srv_ncpts; i++) {
2865 for (j = 0; j < svc->srv_nthrs_cpt_init; j++) {
2866 rc = ptlrpc_start_thread(svc->srv_parts[i], 1);
2872 /* We have enough threads, don't start more. b=15759 */
2879 CERROR("cannot start %s thread #%d_%d: rc %d\n",
2880 svc->srv_thread_name, i, j, rc);
2881 ptlrpc_stop_all_threads(svc);
2884 EXPORT_SYMBOL(ptlrpc_start_threads);
2886 int ptlrpc_start_thread(struct ptlrpc_service_part *svcpt, int wait)
2888 struct l_wait_info lwi = { 0 };
2889 struct ptlrpc_thread *thread;
2890 struct ptlrpc_service *svc;
2891 struct task_struct *task;
2895 LASSERT(svcpt != NULL);
2897 svc = svcpt->scp_service;
2899 CDEBUG(D_RPCTRACE, "%s[%d] started %d min %d max %d\n",
2900 svc->srv_name, svcpt->scp_cpt, svcpt->scp_nthrs_running,
2901 svc->srv_nthrs_cpt_init, svc->srv_nthrs_cpt_limit);
2904 if (unlikely(svc->srv_is_stopping))
2907 if (!ptlrpc_threads_increasable(svcpt) ||
2908 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2909 svcpt->scp_nthrs_running == svc->srv_nthrs_cpt_init - 1))
2912 OBD_CPT_ALLOC_PTR(thread, svc->srv_cptable, svcpt->scp_cpt);
2915 init_waitqueue_head(&thread->t_ctl_waitq);
2917 spin_lock(&svcpt->scp_lock);
2918 if (!ptlrpc_threads_increasable(svcpt)) {
2919 spin_unlock(&svcpt->scp_lock);
2920 OBD_FREE_PTR(thread);
2924 if (svcpt->scp_nthrs_starting != 0) {
2925 /* serialize starting because some modules (obdfilter)
2926 * might require unique and contiguous t_id */
2927 LASSERT(svcpt->scp_nthrs_starting == 1);
2928 spin_unlock(&svcpt->scp_lock);
2929 OBD_FREE_PTR(thread);
2931 CDEBUG(D_INFO, "Waiting for creating thread %s #%d\n",
2932 svc->srv_thread_name, svcpt->scp_thr_nextid);
2937 CDEBUG(D_INFO, "Creating thread %s #%d race, retry later\n",
2938 svc->srv_thread_name, svcpt->scp_thr_nextid);
2942 svcpt->scp_nthrs_starting++;
2943 thread->t_id = svcpt->scp_thr_nextid++;
2944 thread_add_flags(thread, SVC_STARTING);
2945 thread->t_svcpt = svcpt;
2947 list_add(&thread->t_link, &svcpt->scp_threads);
2948 spin_unlock(&svcpt->scp_lock);
2950 if (svcpt->scp_cpt >= 0) {
2951 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s%02d_%03d",
2952 svc->srv_thread_name, svcpt->scp_cpt, thread->t_id);
2954 snprintf(thread->t_name, PTLRPC_THR_NAME_LEN, "%s_%04d",
2955 svc->srv_thread_name, thread->t_id);
2958 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", thread->t_name);
2959 task = kthread_run(ptlrpc_main, thread, "%s", thread->t_name);
2962 CERROR("cannot start thread '%s': rc = %d\n",
2963 thread->t_name, rc);
2964 spin_lock(&svcpt->scp_lock);
2965 --svcpt->scp_nthrs_starting;
2966 if (thread_is_stopping(thread)) {
2967 /* this ptlrpc_thread is being hanled
2968 * by ptlrpc_svcpt_stop_threads now
2970 thread_add_flags(thread, SVC_STOPPED);
2971 wake_up(&thread->t_ctl_waitq);
2972 spin_unlock(&svcpt->scp_lock);
2974 list_del(&thread->t_link);
2975 spin_unlock(&svcpt->scp_lock);
2976 OBD_FREE_PTR(thread);
2984 l_wait_event(thread->t_ctl_waitq,
2985 thread_is_running(thread) || thread_is_stopped(thread),
2988 rc = thread_is_stopped(thread) ? thread->t_id : 0;
2992 int ptlrpc_hr_init(void)
2994 struct ptlrpc_hr_partition *hrp;
2995 struct ptlrpc_hr_thread *hrt;
3001 memset(&ptlrpc_hr, 0, sizeof(ptlrpc_hr));
3002 ptlrpc_hr.hr_cpt_table = cfs_cpt_table;
3004 ptlrpc_hr.hr_partitions = cfs_percpt_alloc(ptlrpc_hr.hr_cpt_table,
3006 if (ptlrpc_hr.hr_partitions == NULL)
3009 init_waitqueue_head(&ptlrpc_hr.hr_waitq);
3011 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
3014 atomic_set(&hrp->hrp_nstarted, 0);
3015 atomic_set(&hrp->hrp_nstopped, 0);
3017 hrp->hrp_nthrs = cfs_cpt_weight(ptlrpc_hr.hr_cpt_table, i);
3018 hrp->hrp_nthrs /= cfs_cpu_ht_nsiblings(0);
3020 LASSERT(hrp->hrp_nthrs > 0);
3021 OBD_CPT_ALLOC(hrp->hrp_thrs, ptlrpc_hr.hr_cpt_table, i,
3022 hrp->hrp_nthrs * sizeof(*hrt));
3023 if (hrp->hrp_thrs == NULL)
3024 GOTO(out, rc = -ENOMEM);
3026 for (j = 0; j < hrp->hrp_nthrs; j++) {
3027 hrt = &hrp->hrp_thrs[j];
3030 hrt->hrt_partition = hrp;
3031 init_waitqueue_head(&hrt->hrt_waitq);
3032 spin_lock_init(&hrt->hrt_lock);
3033 INIT_LIST_HEAD(&hrt->hrt_queue);
3037 rc = ptlrpc_start_hr_threads();
3044 void ptlrpc_hr_fini(void)
3046 struct ptlrpc_hr_partition *hrp;
3049 if (ptlrpc_hr.hr_partitions == NULL)
3052 ptlrpc_stop_hr_threads();
3054 cfs_percpt_for_each(hrp, i, ptlrpc_hr.hr_partitions) {
3055 if (hrp->hrp_thrs != NULL) {
3056 OBD_FREE(hrp->hrp_thrs,
3057 hrp->hrp_nthrs * sizeof(hrp->hrp_thrs[0]));
3061 cfs_percpt_free(ptlrpc_hr.hr_partitions);
3062 ptlrpc_hr.hr_partitions = NULL;
3065 #endif /* __KERNEL__ */
3068 * Wait until all already scheduled replies are processed.
3070 static void ptlrpc_wait_replies(struct ptlrpc_service_part *svcpt)
3074 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
3077 rc = l_wait_event(svcpt->scp_waitq,
3078 atomic_read(&svcpt->scp_nreps_difficult) == 0, &lwi);
3081 CWARN("Unexpectedly long timeout %s %p\n",
3082 svcpt->scp_service->srv_name, svcpt->scp_service);
3087 ptlrpc_service_del_atimer(struct ptlrpc_service *svc)
3089 struct ptlrpc_service_part *svcpt;
3092 /* early disarm AT timer... */
3093 ptlrpc_service_for_each_part(svcpt, i, svc) {
3094 if (svcpt->scp_service != NULL)
3095 cfs_timer_disarm(&svcpt->scp_at_timer);
3100 ptlrpc_service_unlink_rqbd(struct ptlrpc_service *svc)
3102 struct ptlrpc_service_part *svcpt;
3103 struct ptlrpc_request_buffer_desc *rqbd;
3104 struct l_wait_info lwi;
3108 /* All history will be culled when the next request buffer is
3109 * freed in ptlrpc_service_purge_all() */
3110 svc->srv_hist_nrqbds_cpt_max = 0;
3112 rc = LNetClearLazyPortal(svc->srv_req_portal);
3115 ptlrpc_service_for_each_part(svcpt, i, svc) {
3116 if (svcpt->scp_service == NULL)
3119 /* Unlink all the request buffers. This forces a 'final'
3120 * event with its 'unlink' flag set for each posted rqbd */
3121 list_for_each_entry(rqbd, &svcpt->scp_rqbd_posted,
3123 rc = LNetMDUnlink(rqbd->rqbd_md_h);
3124 LASSERT(rc == 0 || rc == -ENOENT);
3128 ptlrpc_service_for_each_part(svcpt, i, svc) {
3129 if (svcpt->scp_service == NULL)
3132 /* Wait for the network to release any buffers
3133 * it's currently filling */
3134 spin_lock(&svcpt->scp_lock);
3135 while (svcpt->scp_nrqbds_posted != 0) {
3136 spin_unlock(&svcpt->scp_lock);
3137 /* Network access will complete in finite time but
3138 * the HUGE timeout lets us CWARN for visibility
3139 * of sluggish NALs */
3140 lwi = LWI_TIMEOUT_INTERVAL(
3141 cfs_time_seconds(LONG_UNLINK),
3142 cfs_time_seconds(1), NULL, NULL);
3143 rc = l_wait_event(svcpt->scp_waitq,
3144 svcpt->scp_nrqbds_posted == 0, &lwi);
3145 if (rc == -ETIMEDOUT) {
3146 CWARN("Service %s waiting for "
3147 "request buffers\n",
3148 svcpt->scp_service->srv_name);
3150 spin_lock(&svcpt->scp_lock);
3152 spin_unlock(&svcpt->scp_lock);
3157 ptlrpc_service_purge_all(struct ptlrpc_service *svc)
3159 struct ptlrpc_service_part *svcpt;
3160 struct ptlrpc_request_buffer_desc *rqbd;
3161 struct ptlrpc_request *req;
3162 struct ptlrpc_reply_state *rs;
3165 ptlrpc_service_for_each_part(svcpt, i, svc) {
3166 if (svcpt->scp_service == NULL)
3169 spin_lock(&svcpt->scp_rep_lock);
3170 while (!list_empty(&svcpt->scp_rep_active)) {
3171 rs = list_entry(svcpt->scp_rep_active.next,
3172 struct ptlrpc_reply_state, rs_list);
3173 spin_lock(&rs->rs_lock);
3174 ptlrpc_schedule_difficult_reply(rs);
3175 spin_unlock(&rs->rs_lock);
3177 spin_unlock(&svcpt->scp_rep_lock);
3179 /* purge the request queue. NB No new replies (rqbds
3180 * all unlinked) and no service threads, so I'm the only
3181 * thread noodling the request queue now */
3182 while (!list_empty(&svcpt->scp_req_incoming)) {
3183 req = list_entry(svcpt->scp_req_incoming.next,
3184 struct ptlrpc_request, rq_list);
3186 list_del(&req->rq_list);
3187 svcpt->scp_nreqs_incoming--;
3188 ptlrpc_server_finish_request(svcpt, req);
3191 while (ptlrpc_server_request_pending(svcpt, true)) {
3192 req = ptlrpc_server_request_get(svcpt, true);
3193 ptlrpc_server_finish_active_request(svcpt, req);
3196 LASSERT(list_empty(&svcpt->scp_rqbd_posted));
3197 LASSERT(svcpt->scp_nreqs_incoming == 0);
3198 LASSERT(svcpt->scp_nreqs_active == 0);
3199 /* history should have been culled by
3200 * ptlrpc_server_finish_request */
3201 LASSERT(svcpt->scp_hist_nrqbds == 0);
3203 /* Now free all the request buffers since nothing
3204 * references them any more... */
3206 while (!list_empty(&svcpt->scp_rqbd_idle)) {
3207 rqbd = list_entry(svcpt->scp_rqbd_idle.next,
3208 struct ptlrpc_request_buffer_desc,
3210 ptlrpc_free_rqbd(rqbd);
3212 ptlrpc_wait_replies(svcpt);
3214 while (!list_empty(&svcpt->scp_rep_idle)) {
3215 rs = list_entry(svcpt->scp_rep_idle.next,
3216 struct ptlrpc_reply_state,
3218 list_del(&rs->rs_list);
3219 OBD_FREE_LARGE(rs, svc->srv_max_reply_size);
3225 ptlrpc_service_free(struct ptlrpc_service *svc)
3227 struct ptlrpc_service_part *svcpt;
3228 struct ptlrpc_at_array *array;
3231 ptlrpc_service_for_each_part(svcpt, i, svc) {
3232 if (svcpt->scp_service == NULL)
3235 /* In case somebody rearmed this in the meantime */
3236 cfs_timer_disarm(&svcpt->scp_at_timer);
3237 array = &svcpt->scp_at_array;
3239 if (array->paa_reqs_array != NULL) {
3240 OBD_FREE(array->paa_reqs_array,
3241 sizeof(struct list_head) * array->paa_size);
3242 array->paa_reqs_array = NULL;
3245 if (array->paa_reqs_count != NULL) {
3246 OBD_FREE(array->paa_reqs_count,
3247 sizeof(__u32) * array->paa_size);
3248 array->paa_reqs_count = NULL;
3252 ptlrpc_service_for_each_part(svcpt, i, svc)
3253 OBD_FREE_PTR(svcpt);
3255 if (svc->srv_cpts != NULL)
3256 cfs_expr_list_values_free(svc->srv_cpts, svc->srv_ncpts);
3258 OBD_FREE(svc, offsetof(struct ptlrpc_service,
3259 srv_parts[svc->srv_ncpts]));
3262 int ptlrpc_unregister_service(struct ptlrpc_service *service)
3266 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
3268 service->srv_is_stopping = 1;
3270 mutex_lock(&ptlrpc_all_services_mutex);
3271 list_del_init(&service->srv_list);
3272 mutex_unlock(&ptlrpc_all_services_mutex);
3274 ptlrpc_service_del_atimer(service);
3275 ptlrpc_stop_all_threads(service);
3277 ptlrpc_service_unlink_rqbd(service);
3278 ptlrpc_service_purge_all(service);
3279 ptlrpc_service_nrs_cleanup(service);
3281 ptlrpc_lprocfs_unregister_service(service);
3283 ptlrpc_service_free(service);
3287 EXPORT_SYMBOL(ptlrpc_unregister_service);
3290 * Returns 0 if the service is healthy.
3292 * Right now, it just checks to make sure that requests aren't languishing
3293 * in the queue. We'll use this health check to govern whether a node needs
3294 * to be shot, so it's intentionally non-aggressive. */
3295 int ptlrpc_svcpt_health_check(struct ptlrpc_service_part *svcpt)
3297 struct ptlrpc_request *request = NULL;
3298 struct timeval right_now;
3301 do_gettimeofday(&right_now);
3303 spin_lock(&svcpt->scp_req_lock);
3304 /* How long has the next entry been waiting? */
3305 if (ptlrpc_server_high_pending(svcpt, true))
3306 request = ptlrpc_nrs_req_peek_nolock(svcpt, true);
3307 else if (ptlrpc_server_normal_pending(svcpt, true))
3308 request = ptlrpc_nrs_req_peek_nolock(svcpt, false);
3310 if (request == NULL) {
3311 spin_unlock(&svcpt->scp_req_lock);
3315 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
3316 spin_unlock(&svcpt->scp_req_lock);
3318 if ((timediff / ONE_MILLION) >
3319 (AT_OFF ? obd_timeout * 3 / 2 : at_max)) {
3320 CERROR("%s: unhealthy - request has been waiting %lds\n",
3321 svcpt->scp_service->srv_name, timediff / ONE_MILLION);
3329 ptlrpc_service_health_check(struct ptlrpc_service *svc)
3331 struct ptlrpc_service_part *svcpt;
3337 ptlrpc_service_for_each_part(svcpt, i, svc) {
3338 int rc = ptlrpc_svcpt_health_check(svcpt);
3345 EXPORT_SYMBOL(ptlrpc_service_health_check);