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) 2011, 2012, Whamcloud, Inc.
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_hpreq_fini(struct ptlrpc_request *req);
69 static CFS_LIST_HEAD(ptlrpc_all_services);
70 cfs_spinlock_t ptlrpc_all_services_lock;
72 struct ptlrpc_request_buffer_desc *
73 ptlrpc_alloc_rqbd(struct ptlrpc_service_part *svcpt)
75 struct ptlrpc_service *svc = svcpt->scp_service;
76 struct ptlrpc_request_buffer_desc *rqbd;
82 rqbd->rqbd_svcpt = svcpt;
83 rqbd->rqbd_refcount = 0;
84 rqbd->rqbd_cbid.cbid_fn = request_in_callback;
85 rqbd->rqbd_cbid.cbid_arg = rqbd;
86 CFS_INIT_LIST_HEAD(&rqbd->rqbd_reqs);
87 OBD_ALLOC_LARGE(rqbd->rqbd_buffer, svc->srv_buf_size);
89 if (rqbd->rqbd_buffer == NULL) {
94 cfs_spin_lock(&svcpt->scp_lock);
95 cfs_list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
96 svcpt->scp_nrqbds_total++;
97 cfs_spin_unlock(&svcpt->scp_lock);
103 ptlrpc_free_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
105 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
107 LASSERT(rqbd->rqbd_refcount == 0);
108 LASSERT(cfs_list_empty(&rqbd->rqbd_reqs));
110 cfs_spin_lock(&svcpt->scp_lock);
111 cfs_list_del(&rqbd->rqbd_list);
112 svcpt->scp_nrqbds_total--;
113 cfs_spin_unlock(&svcpt->scp_lock);
115 OBD_FREE_LARGE(rqbd->rqbd_buffer, svcpt->scp_service->srv_buf_size);
120 ptlrpc_grow_req_bufs(struct ptlrpc_service_part *svcpt)
122 struct ptlrpc_service *svc = svcpt->scp_service;
123 struct ptlrpc_request_buffer_desc *rqbd;
127 for (i = 0; i < svc->srv_nbuf_per_group; i++) {
128 /* NB: another thread might be doing this as well, we need to
129 * make sure that it wouldn't over-allocate, see LU-1212. */
130 if (svcpt->scp_nrqbds_posted >= svc->srv_nbuf_per_group)
133 rqbd = ptlrpc_alloc_rqbd(svcpt);
136 CERROR("%s: Can't allocate request buffer\n",
142 if (ptlrpc_server_post_idle_rqbds(svcpt) < 0) {
149 "%s: allocate %d new %d-byte reqbufs (%d/%d left), rc = %d\n",
150 svc->srv_name, i, svc->srv_buf_size,
151 svcpt->scp_nrqbds_posted, svcpt->scp_nrqbds_total, rc);
157 * Part of Rep-Ack logic.
158 * Puts a lock and its mode into reply state assotiated to request reply.
161 ptlrpc_save_lock(struct ptlrpc_request *req,
162 struct lustre_handle *lock, int mode, int no_ack)
164 struct ptlrpc_reply_state *rs = req->rq_reply_state;
168 LASSERT(rs->rs_nlocks < RS_MAX_LOCKS);
170 if (req->rq_export->exp_disconnected) {
171 ldlm_lock_decref(lock, mode);
173 idx = rs->rs_nlocks++;
174 rs->rs_locks[idx] = *lock;
175 rs->rs_modes[idx] = mode;
176 rs->rs_difficult = 1;
177 rs->rs_no_ack = !!no_ack;
183 #define HRT_RUNNING 0
184 #define HRT_STOPPING 1
186 struct ptlrpc_hr_thread {
187 cfs_spinlock_t hrt_lock;
188 unsigned long hrt_flags;
189 cfs_waitq_t hrt_wait;
190 cfs_list_t hrt_queue;
191 cfs_completion_t hrt_completion;
194 struct ptlrpc_hr_service {
198 struct ptlrpc_hr_thread hr_threads[0];
202 cfs_list_t rsb_replies;
203 unsigned int rsb_n_replies;
204 struct ptlrpc_service_part *rsb_svcpt;
208 * A pointer to per-node reply handling service.
210 static struct ptlrpc_hr_service *ptlrpc_hr = NULL;
213 * maximum mumber of replies scheduled in one batch
215 #define MAX_SCHEDULED 256
218 * Initialize a reply batch.
222 static void rs_batch_init(struct rs_batch *b)
224 memset(b, 0, sizeof *b);
225 CFS_INIT_LIST_HEAD(&b->rsb_replies);
229 * Choose an hr thread to dispatch requests to.
231 static unsigned int get_hr_thread_index(struct ptlrpc_hr_service *hr)
235 /* Concurrent modification of hr_index w/o any spinlock
236 protection is harmless as long as the result fits
237 [0..(hr_n_threads-1)] range and each thread gets near equal
240 hr->hr_index = (idx >= hr->hr_n_threads - 1) ? 0 : idx + 1;
245 * Dispatch all replies accumulated in the batch to one from
246 * dedicated reply handling threads.
250 static void rs_batch_dispatch(struct rs_batch *b)
252 if (b->rsb_n_replies != 0) {
253 struct ptlrpc_hr_service *hr = ptlrpc_hr;
256 idx = get_hr_thread_index(hr);
258 cfs_spin_lock(&hr->hr_threads[idx].hrt_lock);
259 cfs_list_splice_init(&b->rsb_replies,
260 &hr->hr_threads[idx].hrt_queue);
261 cfs_spin_unlock(&hr->hr_threads[idx].hrt_lock);
262 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
263 b->rsb_n_replies = 0;
268 * Add a reply to a batch.
269 * Add one reply object to a batch, schedule batched replies if overload.
274 static void rs_batch_add(struct rs_batch *b, struct ptlrpc_reply_state *rs)
276 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
278 if (svcpt != b->rsb_svcpt || b->rsb_n_replies >= MAX_SCHEDULED) {
279 if (b->rsb_svcpt != NULL) {
280 rs_batch_dispatch(b);
281 cfs_spin_unlock(&b->rsb_svcpt->scp_rep_lock);
283 cfs_spin_lock(&svcpt->scp_rep_lock);
284 b->rsb_svcpt = svcpt;
286 cfs_spin_lock(&rs->rs_lock);
287 rs->rs_scheduled_ever = 1;
288 if (rs->rs_scheduled == 0) {
289 cfs_list_move(&rs->rs_list, &b->rsb_replies);
290 rs->rs_scheduled = 1;
293 rs->rs_committed = 1;
294 cfs_spin_unlock(&rs->rs_lock);
298 * Reply batch finalization.
299 * Dispatch remaining replies from the batch
300 * and release remaining spinlock.
304 static void rs_batch_fini(struct rs_batch *b)
306 if (b->rsb_svcpt != NULL) {
307 rs_batch_dispatch(b);
308 cfs_spin_unlock(&b->rsb_svcpt->scp_rep_lock);
312 #define DECLARE_RS_BATCH(b) struct rs_batch b
314 #else /* __KERNEL__ */
316 #define rs_batch_init(b) do{}while(0)
317 #define rs_batch_fini(b) do{}while(0)
318 #define rs_batch_add(b, r) ptlrpc_schedule_difficult_reply(r)
319 #define DECLARE_RS_BATCH(b)
321 #endif /* __KERNEL__ */
324 * Put reply state into a queue for processing because we received
325 * ACK from the client
327 void ptlrpc_dispatch_difficult_reply(struct ptlrpc_reply_state *rs)
330 struct ptlrpc_hr_service *hr = ptlrpc_hr;
334 LASSERT(cfs_list_empty(&rs->rs_list));
336 idx = get_hr_thread_index(hr);
337 cfs_spin_lock(&hr->hr_threads[idx].hrt_lock);
338 cfs_list_add_tail(&rs->rs_list, &hr->hr_threads[idx].hrt_queue);
339 cfs_spin_unlock(&hr->hr_threads[idx].hrt_lock);
340 cfs_waitq_signal(&hr->hr_threads[idx].hrt_wait);
343 cfs_list_add_tail(&rs->rs_list, &rs->rs_svcpt->scp_rep_queue);
348 ptlrpc_schedule_difficult_reply(struct ptlrpc_reply_state *rs)
352 LASSERT_SPIN_LOCKED(&rs->rs_svcpt->scp_rep_lock);
353 LASSERT_SPIN_LOCKED(&rs->rs_lock);
354 LASSERT (rs->rs_difficult);
355 rs->rs_scheduled_ever = 1; /* flag any notification attempt */
357 if (rs->rs_scheduled) { /* being set up or already notified */
362 rs->rs_scheduled = 1;
363 cfs_list_del_init(&rs->rs_list);
364 ptlrpc_dispatch_difficult_reply(rs);
368 void ptlrpc_commit_replies(struct obd_export *exp)
370 struct ptlrpc_reply_state *rs, *nxt;
371 DECLARE_RS_BATCH(batch);
374 rs_batch_init(&batch);
375 /* Find any replies that have been committed and get their service
376 * to attend to complete them. */
378 /* CAVEAT EMPTOR: spinlock ordering!!! */
379 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
380 cfs_list_for_each_entry_safe(rs, nxt, &exp->exp_uncommitted_replies,
382 LASSERT (rs->rs_difficult);
383 /* VBR: per-export last_committed */
384 LASSERT(rs->rs_export);
385 if (rs->rs_transno <= exp->exp_last_committed) {
386 cfs_list_del_init(&rs->rs_obd_list);
387 rs_batch_add(&batch, rs);
390 cfs_spin_unlock(&exp->exp_uncommitted_replies_lock);
391 rs_batch_fini(&batch);
396 ptlrpc_server_post_idle_rqbds(struct ptlrpc_service_part *svcpt)
398 struct ptlrpc_request_buffer_desc *rqbd;
403 cfs_spin_lock(&svcpt->scp_lock);
405 if (cfs_list_empty(&svcpt->scp_rqbd_idle)) {
406 cfs_spin_unlock(&svcpt->scp_lock);
410 rqbd = cfs_list_entry(svcpt->scp_rqbd_idle.next,
411 struct ptlrpc_request_buffer_desc,
413 cfs_list_del(&rqbd->rqbd_list);
415 /* assume we will post successfully */
416 svcpt->scp_nrqbds_posted++;
417 cfs_list_add(&rqbd->rqbd_list, &svcpt->scp_rqbd_posted);
419 cfs_spin_unlock(&svcpt->scp_lock);
421 rc = ptlrpc_register_rqbd(rqbd);
428 cfs_spin_lock(&svcpt->scp_lock);
430 svcpt->scp_nrqbds_posted--;
431 cfs_list_del(&rqbd->rqbd_list);
432 cfs_list_add_tail(&rqbd->rqbd_list, &svcpt->scp_rqbd_idle);
434 /* Don't complain if no request buffers are posted right now; LNET
435 * won't drop requests because we set the portal lazy! */
437 cfs_spin_unlock(&svcpt->scp_lock);
442 static void ptlrpc_at_timer(unsigned long castmeharder)
444 struct ptlrpc_service_part *svcpt;
446 svcpt = (struct ptlrpc_service_part *)castmeharder;
448 svcpt->scp_at_check = 1;
449 svcpt->scp_at_checktime = cfs_time_current();
450 cfs_waitq_signal(&svcpt->scp_waitq);
454 ptlrpc_server_nthreads_check(struct ptlrpc_service_conf *conf,
455 int *min_p, int *max_p)
458 struct ptlrpc_service_thr_conf *tc = &conf->psc_thr;
462 nthrs_min = PTLRPC_NTHRS_MIN + (conf->psc_ops.so_hpreq_handler != NULL);
463 nthrs_min = max_t(int, nthrs_min, tc->tc_nthrs_min);
465 nthrs = tc->tc_nthrs_user;
466 if (nthrs != 0) { /* validate it */
467 nthrs = min_t(int, nthrs, tc->tc_nthrs_max);
468 nthrs = max_t(int, nthrs, nthrs_min);
469 *min_p = *max_p = nthrs;
474 * NB: we will add some common at here for estimating, for example:
475 * add a new member ptlrpc_service_thr_conf::tc_factor, and estimate
476 * threads number based on:
477 * (online_cpus * conf::tc_factor) + conf::tc_nthrs_base.
479 * So we can remove code block like estimation in ost_setup, also,
480 * we might estimate MDS threads number as well instead of using
481 * absolute number, and have more threads on fat servers to improve
482 * availability of service.
484 * Also, we will need to validate threads number at here for
485 * CPT affinity service (CPU ParTiion) in the future.
486 * A service can have percpt thread-pool instead of a global thread
487 * pool for each service, which means user might not always get the
488 * threads number they want even they set it in conf::tc_nthrs_user,
489 * because we need to adjust threads number for each CPT, instead of
490 * just use (conf::tc_nthrs_user / NCPTS), to make sure each pool
493 *max_p = tc->tc_nthrs_max;
495 #else /* __KERNEL__ */
496 *max_p = *min_p = 1; /* whatever */
501 * Initialize percpt data for a service
504 ptlrpc_service_part_init(struct ptlrpc_service *svc,
505 struct ptlrpc_service_part *svcpt)
507 struct ptlrpc_at_array *array;
512 CFS_INIT_LIST_HEAD(&svcpt->scp_threads);
514 /* rqbd and incoming request queue */
515 cfs_spin_lock_init(&svcpt->scp_lock);
516 CFS_INIT_LIST_HEAD(&svcpt->scp_rqbd_idle);
517 CFS_INIT_LIST_HEAD(&svcpt->scp_rqbd_posted);
518 CFS_INIT_LIST_HEAD(&svcpt->scp_req_incoming);
519 cfs_waitq_init(&svcpt->scp_waitq);
520 /* history request & rqbd list */
521 CFS_INIT_LIST_HEAD(&svcpt->scp_hist_reqs);
522 CFS_INIT_LIST_HEAD(&svcpt->scp_hist_rqbds);
524 /* acitve requests and hp requests */
525 cfs_spin_lock_init(&svcpt->scp_req_lock);
526 CFS_INIT_LIST_HEAD(&svcpt->scp_req_pending);
527 CFS_INIT_LIST_HEAD(&svcpt->scp_hreq_pending);
530 cfs_spin_lock_init(&svcpt->scp_rep_lock);
531 CFS_INIT_LIST_HEAD(&svcpt->scp_rep_active);
533 CFS_INIT_LIST_HEAD(&svcpt->scp_rep_queue);
535 CFS_INIT_LIST_HEAD(&svcpt->scp_rep_idle);
536 cfs_waitq_init(&svcpt->scp_rep_waitq);
537 cfs_atomic_set(&svcpt->scp_nreps_difficult, 0);
539 /* adaptive timeout */
540 cfs_spin_lock_init(&svcpt->scp_at_lock);
541 array = &svcpt->scp_at_array;
543 size = at_est2timeout(at_max);
544 array->paa_size = size;
545 array->paa_count = 0;
546 array->paa_deadline = -1;
548 /* allocate memory for scp_at_array (ptlrpc_at_array) */
549 OBD_ALLOC(array->paa_reqs_array, sizeof(cfs_list_t) * size);
550 if (array->paa_reqs_array == NULL)
553 for (index = 0; index < size; index++)
554 CFS_INIT_LIST_HEAD(&array->paa_reqs_array[index]);
556 OBD_ALLOC(array->paa_reqs_count, sizeof(__u32) * size);
557 if (array->paa_reqs_count == NULL)
560 cfs_timer_init(&svcpt->scp_at_timer, ptlrpc_at_timer, svcpt);
561 /* At SOW, service time should be quick; 10s seems generous. If client
562 * timeout is less than this, we'll be sending an early reply. */
563 at_init(&svcpt->scp_at_estimate, 10, 0);
565 /* assign this before call ptlrpc_grow_req_bufs */
566 svcpt->scp_service = svc;
567 /* Now allocate the request buffers, but don't post them now */
568 rc = ptlrpc_grow_req_bufs(svcpt);
569 /* We shouldn't be under memory pressure at startup, so
570 * fail if we can't allocate all our buffers at this time. */
577 if (array->paa_reqs_count != NULL) {
578 OBD_FREE(array->paa_reqs_count, sizeof(__u32) * size);
579 array->paa_reqs_count = NULL;
582 if (array->paa_reqs_array != NULL) {
583 OBD_FREE(array->paa_reqs_array,
584 sizeof(cfs_list_t) * array->paa_size);
585 array->paa_reqs_array = NULL;
592 * Initialize service on a given portal.
593 * This includes starting serving threads , allocating and posting rqbds and
596 struct ptlrpc_service *
597 ptlrpc_register_service(struct ptlrpc_service_conf *conf,
598 cfs_proc_dir_entry_t *proc_entry)
600 struct ptlrpc_service *service;
604 LASSERT(conf->psc_buf.bc_nbufs > 0);
605 LASSERT(conf->psc_buf.bc_buf_size >=
606 conf->psc_buf.bc_req_max_size + SPTLRPC_MAX_PAYLOAD);
607 LASSERT(conf->psc_thr.tc_ctx_tags != 0);
609 OBD_ALLOC_PTR(service);
611 RETURN(ERR_PTR(-ENOMEM));
614 cfs_spin_lock_init(&service->srv_lock);
615 service->srv_name = conf->psc_name;
616 service->srv_watchdog_factor = conf->psc_watchdog_factor;
617 CFS_INIT_LIST_HEAD(&service->srv_list); /* for safty of cleanup */
619 /* buffer configuration */
620 service->srv_nbuf_per_group = test_req_buffer_pressure ?
621 1 : conf->psc_buf.bc_nbufs;
622 service->srv_max_req_size = conf->psc_buf.bc_req_max_size +
624 service->srv_buf_size = conf->psc_buf.bc_buf_size;
625 service->srv_rep_portal = conf->psc_buf.bc_rep_portal;
626 service->srv_req_portal = conf->psc_buf.bc_req_portal;
628 /* Increase max reply size to next power of two */
629 service->srv_max_reply_size = 1;
630 while (service->srv_max_reply_size <
631 conf->psc_buf.bc_rep_max_size + SPTLRPC_MAX_PAYLOAD)
632 service->srv_max_reply_size <<= 1;
634 ptlrpc_server_nthreads_check(conf, &service->srv_threads_min,
635 &service->srv_threads_max);
637 service->srv_thread_name = conf->psc_thr.tc_thr_name;
638 service->srv_ctx_tags = conf->psc_thr.tc_ctx_tags;
639 service->srv_cpu_affinity = !!conf->psc_thr.tc_cpu_affinity;
640 service->srv_hpreq_ratio = PTLRPC_SVC_HP_RATIO;
641 service->srv_ops = conf->psc_ops;
643 OBD_ALLOC_PTR(service->srv_part);
644 if (service->srv_part == NULL)
645 GOTO(failed, rc = -ENOMEM);
647 rc = ptlrpc_service_part_init(service, service->srv_part);
651 rc = LNetSetLazyPortal(service->srv_req_portal);
654 cfs_spin_lock (&ptlrpc_all_services_lock);
655 cfs_list_add (&service->srv_list, &ptlrpc_all_services);
656 cfs_spin_unlock (&ptlrpc_all_services_lock);
658 if (proc_entry != NULL)
659 ptlrpc_lprocfs_register_service(proc_entry, service);
661 CDEBUG(D_NET, "%s: Started, listening on portal %d\n",
662 service->srv_name, service->srv_req_portal);
665 rc = ptlrpc_start_threads(service);
667 CERROR("Failed to start threads for service %s: %d\n",
668 service->srv_name, rc);
675 ptlrpc_unregister_service(service);
680 * to actually free the request, must be called without holding svc_lock.
681 * note it's caller's responsibility to unlink req->rq_list.
683 static void ptlrpc_server_free_request(struct ptlrpc_request *req)
685 LASSERT(cfs_atomic_read(&req->rq_refcount) == 0);
686 LASSERT(cfs_list_empty(&req->rq_timed_list));
688 /* DEBUG_REQ() assumes the reply state of a request with a valid
689 * ref will not be destroyed until that reference is dropped. */
690 ptlrpc_req_drop_rs(req);
692 sptlrpc_svc_ctx_decref(req);
694 if (req != &req->rq_rqbd->rqbd_req) {
695 /* NB request buffers use an embedded
696 * req if the incoming req unlinked the
697 * MD; this isn't one of them! */
698 OBD_FREE(req, sizeof(*req));
703 * drop a reference count of the request. if it reaches 0, we either
704 * put it into history list, or free it immediately.
706 void ptlrpc_server_drop_request(struct ptlrpc_request *req)
708 struct ptlrpc_request_buffer_desc *rqbd = req->rq_rqbd;
709 struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
710 struct ptlrpc_service *svc = svcpt->scp_service;
715 if (!cfs_atomic_dec_and_test(&req->rq_refcount))
718 cfs_spin_lock(&svcpt->scp_at_lock);
719 if (req->rq_at_linked) {
720 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
721 __u32 index = req->rq_at_index;
723 LASSERT(!cfs_list_empty(&req->rq_timed_list));
724 cfs_list_del_init(&req->rq_timed_list);
725 cfs_spin_lock(&req->rq_lock);
726 req->rq_at_linked = 0;
727 cfs_spin_unlock(&req->rq_lock);
728 array->paa_reqs_count[index]--;
731 LASSERT(cfs_list_empty(&req->rq_timed_list));
733 cfs_spin_unlock(&svcpt->scp_at_lock);
735 /* finalize request */
736 if (req->rq_export) {
737 class_export_put(req->rq_export);
738 req->rq_export = NULL;
741 cfs_spin_lock(&svcpt->scp_lock);
743 cfs_list_add(&req->rq_list, &rqbd->rqbd_reqs);
745 refcount = --(rqbd->rqbd_refcount);
747 /* request buffer is now idle: add to history */
748 cfs_list_del(&rqbd->rqbd_list);
750 cfs_list_add_tail(&rqbd->rqbd_list, &svcpt->scp_hist_rqbds);
751 svcpt->scp_hist_nrqbds++;
753 /* cull some history?
754 * I expect only about 1 or 2 rqbds need to be recycled here */
755 while (svcpt->scp_hist_nrqbds > svc->srv_max_history_rqbds) {
756 rqbd = cfs_list_entry(svcpt->scp_hist_rqbds.next,
757 struct ptlrpc_request_buffer_desc,
760 cfs_list_del(&rqbd->rqbd_list);
761 svcpt->scp_hist_nrqbds--;
763 /* remove rqbd's reqs from svc's req history while
764 * I've got the service lock */
765 cfs_list_for_each(tmp, &rqbd->rqbd_reqs) {
766 req = cfs_list_entry(tmp, struct ptlrpc_request,
768 /* Track the highest culled req seq */
769 if (req->rq_history_seq >
770 svcpt->scp_hist_seq_culled) {
771 svcpt->scp_hist_seq_culled =
774 cfs_list_del(&req->rq_history_list);
777 cfs_spin_unlock(&svcpt->scp_lock);
779 cfs_list_for_each_safe(tmp, nxt, &rqbd->rqbd_reqs) {
780 req = cfs_list_entry(rqbd->rqbd_reqs.next,
781 struct ptlrpc_request,
783 cfs_list_del(&req->rq_list);
784 ptlrpc_server_free_request(req);
787 cfs_spin_lock(&svcpt->scp_lock);
789 * now all reqs including the embedded req has been
790 * disposed, schedule request buffer for re-use.
792 LASSERT(cfs_atomic_read(&rqbd->rqbd_req.rq_refcount) ==
794 cfs_list_add_tail(&rqbd->rqbd_list,
795 &svcpt->scp_rqbd_idle);
798 cfs_spin_unlock(&svcpt->scp_lock);
799 } else if (req->rq_reply_state && req->rq_reply_state->rs_prealloc) {
800 /* If we are low on memory, we are not interested in history */
801 cfs_list_del(&req->rq_list);
802 cfs_list_del_init(&req->rq_history_list);
804 cfs_spin_unlock(&svcpt->scp_lock);
806 ptlrpc_server_free_request(req);
808 cfs_spin_unlock(&svcpt->scp_lock);
813 * to finish a request: stop sending more early replies, and release
814 * the request. should be called after we finished handling the request.
816 static void ptlrpc_server_finish_request(struct ptlrpc_service_part *svcpt,
817 struct ptlrpc_request *req)
819 ptlrpc_hpreq_fini(req);
821 cfs_spin_lock(&svcpt->scp_req_lock);
822 svcpt->scp_nreqs_active--;
824 svcpt->scp_nhreqs_active--;
825 cfs_spin_unlock(&svcpt->scp_req_lock);
827 ptlrpc_server_drop_request(req);
831 * This function makes sure dead exports are evicted in a timely manner.
832 * This function is only called when some export receives a message (i.e.,
833 * the network is up.)
835 static void ptlrpc_update_export_timer(struct obd_export *exp, long extra_delay)
837 struct obd_export *oldest_exp;
838 time_t oldest_time, new_time;
844 /* Compensate for slow machines, etc, by faking our request time
845 into the future. Although this can break the strict time-ordering
846 of the list, we can be really lazy here - we don't have to evict
847 at the exact right moment. Eventually, all silent exports
848 will make it to the top of the list. */
850 /* Do not pay attention on 1sec or smaller renewals. */
851 new_time = cfs_time_current_sec() + extra_delay;
852 if (exp->exp_last_request_time + 1 /*second */ >= new_time)
855 exp->exp_last_request_time = new_time;
856 CDEBUG(D_HA, "updating export %s at "CFS_TIME_T" exp %p\n",
857 exp->exp_client_uuid.uuid,
858 exp->exp_last_request_time, exp);
860 /* exports may get disconnected from the chain even though the
861 export has references, so we must keep the spin lock while
862 manipulating the lists */
863 cfs_spin_lock(&exp->exp_obd->obd_dev_lock);
865 if (cfs_list_empty(&exp->exp_obd_chain_timed)) {
866 /* this one is not timed */
867 cfs_spin_unlock(&exp->exp_obd->obd_dev_lock);
871 cfs_list_move_tail(&exp->exp_obd_chain_timed,
872 &exp->exp_obd->obd_exports_timed);
874 oldest_exp = cfs_list_entry(exp->exp_obd->obd_exports_timed.next,
875 struct obd_export, exp_obd_chain_timed);
876 oldest_time = oldest_exp->exp_last_request_time;
877 cfs_spin_unlock(&exp->exp_obd->obd_dev_lock);
879 if (exp->exp_obd->obd_recovering) {
880 /* be nice to everyone during recovery */
885 /* Note - racing to start/reset the obd_eviction timer is safe */
886 if (exp->exp_obd->obd_eviction_timer == 0) {
887 /* Check if the oldest entry is expired. */
888 if (cfs_time_current_sec() > (oldest_time + PING_EVICT_TIMEOUT +
890 /* We need a second timer, in case the net was down and
891 * it just came back. Since the pinger may skip every
892 * other PING_INTERVAL (see note in ptlrpc_pinger_main),
893 * we better wait for 3. */
894 exp->exp_obd->obd_eviction_timer =
895 cfs_time_current_sec() + 3 * PING_INTERVAL;
896 CDEBUG(D_HA, "%s: Think about evicting %s from "CFS_TIME_T"\n",
897 exp->exp_obd->obd_name,
898 obd_export_nid2str(oldest_exp), oldest_time);
901 if (cfs_time_current_sec() >
902 (exp->exp_obd->obd_eviction_timer + extra_delay)) {
903 /* The evictor won't evict anyone who we've heard from
904 * recently, so we don't have to check before we start
906 if (!ping_evictor_wake(exp))
907 exp->exp_obd->obd_eviction_timer = 0;
915 * Sanity check request \a req.
916 * Return 0 if all is ok, error code otherwise.
918 static int ptlrpc_check_req(struct ptlrpc_request *req)
922 if (unlikely(lustre_msg_get_conn_cnt(req->rq_reqmsg) <
923 req->rq_export->exp_conn_cnt)) {
924 DEBUG_REQ(D_ERROR, req,
925 "DROPPING req from old connection %d < %d",
926 lustre_msg_get_conn_cnt(req->rq_reqmsg),
927 req->rq_export->exp_conn_cnt);
930 if (unlikely(req->rq_export->exp_obd &&
931 req->rq_export->exp_obd->obd_fail)) {
932 /* Failing over, don't handle any more reqs, send
933 error response instead. */
934 CDEBUG(D_RPCTRACE, "Dropping req %p for failed obd %s\n",
935 req, req->rq_export->exp_obd->obd_name);
937 } else if (lustre_msg_get_flags(req->rq_reqmsg) &
938 (MSG_REPLAY | MSG_REQ_REPLAY_DONE) &&
939 !(req->rq_export->exp_obd->obd_recovering)) {
940 DEBUG_REQ(D_ERROR, req,
941 "Invalid replay without recovery");
942 class_fail_export(req->rq_export);
944 } else if (lustre_msg_get_transno(req->rq_reqmsg) != 0 &&
945 !(req->rq_export->exp_obd->obd_recovering)) {
946 DEBUG_REQ(D_ERROR, req, "Invalid req with transno "
947 LPU64" without recovery",
948 lustre_msg_get_transno(req->rq_reqmsg));
949 class_fail_export(req->rq_export);
953 if (unlikely(rc < 0)) {
960 static void ptlrpc_at_set_timer(struct ptlrpc_service_part *svcpt)
962 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
965 cfs_spin_lock(&svcpt->scp_at_lock);
966 if (array->paa_count == 0) {
967 cfs_timer_disarm(&svcpt->scp_at_timer);
968 cfs_spin_unlock(&svcpt->scp_at_lock);
972 /* Set timer for closest deadline */
973 next = (__s32)(array->paa_deadline - cfs_time_current_sec() -
976 ptlrpc_at_timer((unsigned long)svcpt);
978 cfs_timer_arm(&svcpt->scp_at_timer, cfs_time_shift(next));
979 CDEBUG(D_INFO, "armed %s at %+ds\n",
980 svcpt->scp_service->srv_name, next);
982 cfs_spin_unlock(&svcpt->scp_at_lock);
985 /* Add rpc to early reply check list */
986 static int ptlrpc_at_add_timed(struct ptlrpc_request *req)
988 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
989 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
990 struct ptlrpc_request *rq = NULL;
997 if (req->rq_no_reply)
1000 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0)
1003 cfs_spin_lock(&svcpt->scp_at_lock);
1004 LASSERT(cfs_list_empty(&req->rq_timed_list));
1006 index = (unsigned long)req->rq_deadline % array->paa_size;
1007 if (array->paa_reqs_count[index] > 0) {
1008 /* latest rpcs will have the latest deadlines in the list,
1009 * so search backward. */
1010 cfs_list_for_each_entry_reverse(rq,
1011 &array->paa_reqs_array[index],
1013 if (req->rq_deadline >= rq->rq_deadline) {
1014 cfs_list_add(&req->rq_timed_list,
1015 &rq->rq_timed_list);
1021 /* Add the request at the head of the list */
1022 if (cfs_list_empty(&req->rq_timed_list))
1023 cfs_list_add(&req->rq_timed_list,
1024 &array->paa_reqs_array[index]);
1026 cfs_spin_lock(&req->rq_lock);
1027 req->rq_at_linked = 1;
1028 cfs_spin_unlock(&req->rq_lock);
1029 req->rq_at_index = index;
1030 array->paa_reqs_count[index]++;
1032 if (array->paa_count == 1 || array->paa_deadline > req->rq_deadline) {
1033 array->paa_deadline = req->rq_deadline;
1036 cfs_spin_unlock(&svcpt->scp_at_lock);
1039 ptlrpc_at_set_timer(svcpt);
1044 static int ptlrpc_at_send_early_reply(struct ptlrpc_request *req)
1046 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1047 struct ptlrpc_request *reqcopy;
1048 struct lustre_msg *reqmsg;
1049 cfs_duration_t olddl = req->rq_deadline - cfs_time_current_sec();
1054 /* deadline is when the client expects us to reply, margin is the
1055 difference between clients' and servers' expectations */
1056 DEBUG_REQ(D_ADAPTTO, req,
1057 "%ssending early reply (deadline %+lds, margin %+lds) for "
1058 "%d+%d", AT_OFF ? "AT off - not " : "",
1059 olddl, olddl - at_get(&svcpt->scp_at_estimate),
1060 at_get(&svcpt->scp_at_estimate), at_extra);
1066 DEBUG_REQ(D_WARNING, req, "Already past deadline (%+lds), "
1067 "not sending early reply. Consider increasing "
1068 "at_early_margin (%d)?", olddl, at_early_margin);
1070 /* Return an error so we're not re-added to the timed list. */
1074 if ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) == 0){
1075 DEBUG_REQ(D_INFO, req, "Wanted to ask client for more time, "
1076 "but no AT support");
1080 if (req->rq_export &&
1081 lustre_msg_get_flags(req->rq_reqmsg) &
1082 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
1083 /* During recovery, we don't want to send too many early
1084 * replies, but on the other hand we want to make sure the
1085 * client has enough time to resend if the rpc is lost. So
1086 * during the recovery period send at least 4 early replies,
1087 * spacing them every at_extra if we can. at_estimate should
1088 * always equal this fixed value during recovery. */
1089 at_measured(&svcpt->scp_at_estimate, min(at_extra,
1090 req->rq_export->exp_obd->obd_recovery_timeout / 4));
1092 /* Fake our processing time into the future to ask the clients
1093 * for some extra amount of time */
1094 at_measured(&svcpt->scp_at_estimate, at_extra +
1095 cfs_time_current_sec() -
1096 req->rq_arrival_time.tv_sec);
1098 /* Check to see if we've actually increased the deadline -
1099 * we may be past adaptive_max */
1100 if (req->rq_deadline >= req->rq_arrival_time.tv_sec +
1101 at_get(&svcpt->scp_at_estimate)) {
1102 DEBUG_REQ(D_WARNING, req, "Couldn't add any time "
1103 "(%ld/%ld), not sending early reply\n",
1104 olddl, req->rq_arrival_time.tv_sec +
1105 at_get(&svcpt->scp_at_estimate) -
1106 cfs_time_current_sec());
1110 newdl = cfs_time_current_sec() + at_get(&svcpt->scp_at_estimate);
1112 OBD_ALLOC(reqcopy, sizeof *reqcopy);
1113 if (reqcopy == NULL)
1115 OBD_ALLOC_LARGE(reqmsg, req->rq_reqlen);
1117 OBD_FREE(reqcopy, sizeof *reqcopy);
1122 reqcopy->rq_reply_state = NULL;
1123 reqcopy->rq_rep_swab_mask = 0;
1124 reqcopy->rq_pack_bulk = 0;
1125 reqcopy->rq_pack_udesc = 0;
1126 reqcopy->rq_packed_final = 0;
1127 sptlrpc_svc_ctx_addref(reqcopy);
1128 /* We only need the reqmsg for the magic */
1129 reqcopy->rq_reqmsg = reqmsg;
1130 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1132 LASSERT(cfs_atomic_read(&req->rq_refcount));
1133 /** if it is last refcount then early reply isn't needed */
1134 if (cfs_atomic_read(&req->rq_refcount) == 1) {
1135 DEBUG_REQ(D_ADAPTTO, reqcopy, "Normal reply already sent out, "
1136 "abort sending early reply\n");
1137 GOTO(out, rc = -EINVAL);
1140 /* Connection ref */
1141 reqcopy->rq_export = class_conn2export(
1142 lustre_msg_get_handle(reqcopy->rq_reqmsg));
1143 if (reqcopy->rq_export == NULL)
1144 GOTO(out, rc = -ENODEV);
1147 class_export_rpc_get(reqcopy->rq_export);
1148 if (reqcopy->rq_export->exp_obd &&
1149 reqcopy->rq_export->exp_obd->obd_fail)
1150 GOTO(out_put, rc = -ENODEV);
1152 rc = lustre_pack_reply_flags(reqcopy, 1, NULL, NULL, LPRFL_EARLY_REPLY);
1156 rc = ptlrpc_send_reply(reqcopy, PTLRPC_REPLY_EARLY);
1159 /* Adjust our own deadline to what we told the client */
1160 req->rq_deadline = newdl;
1161 req->rq_early_count++; /* number sent, server side */
1163 DEBUG_REQ(D_ERROR, req, "Early reply send failed %d", rc);
1166 /* Free the (early) reply state from lustre_pack_reply.
1167 (ptlrpc_send_reply takes it's own rs ref, so this is safe here) */
1168 ptlrpc_req_drop_rs(reqcopy);
1171 class_export_rpc_put(reqcopy->rq_export);
1172 class_export_put(reqcopy->rq_export);
1174 sptlrpc_svc_ctx_decref(reqcopy);
1175 OBD_FREE_LARGE(reqmsg, req->rq_reqlen);
1176 OBD_FREE(reqcopy, sizeof *reqcopy);
1180 /* Send early replies to everybody expiring within at_early_margin
1181 asking for at_extra time */
1182 static int ptlrpc_at_check_timed(struct ptlrpc_service_part *svcpt)
1184 struct ptlrpc_at_array *array = &svcpt->scp_at_array;
1185 struct ptlrpc_request *rq, *n;
1186 cfs_list_t work_list;
1189 time_t now = cfs_time_current_sec();
1190 cfs_duration_t delay;
1191 int first, counter = 0;
1194 cfs_spin_lock(&svcpt->scp_at_lock);
1195 if (svcpt->scp_at_check == 0) {
1196 cfs_spin_unlock(&svcpt->scp_at_lock);
1199 delay = cfs_time_sub(cfs_time_current(), svcpt->scp_at_checktime);
1200 svcpt->scp_at_check = 0;
1202 if (array->paa_count == 0) {
1203 cfs_spin_unlock(&svcpt->scp_at_lock);
1207 /* The timer went off, but maybe the nearest rpc already completed. */
1208 first = array->paa_deadline - now;
1209 if (first > at_early_margin) {
1210 /* We've still got plenty of time. Reset the timer. */
1211 cfs_spin_unlock(&svcpt->scp_at_lock);
1212 ptlrpc_at_set_timer(svcpt);
1216 /* We're close to a timeout, and we don't know how much longer the
1217 server will take. Send early replies to everyone expiring soon. */
1218 CFS_INIT_LIST_HEAD(&work_list);
1220 index = (unsigned long)array->paa_deadline % array->paa_size;
1221 count = array->paa_count;
1223 count -= array->paa_reqs_count[index];
1224 cfs_list_for_each_entry_safe(rq, n,
1225 &array->paa_reqs_array[index],
1227 if (rq->rq_deadline <= now + at_early_margin) {
1228 cfs_list_del_init(&rq->rq_timed_list);
1230 * ptlrpc_server_drop_request() may drop
1231 * refcount to 0 already. Let's check this and
1232 * don't add entry to work_list
1234 if (likely(cfs_atomic_inc_not_zero(&rq->rq_refcount)))
1235 cfs_list_add(&rq->rq_timed_list, &work_list);
1237 array->paa_reqs_count[index]--;
1239 cfs_spin_lock(&rq->rq_lock);
1240 rq->rq_at_linked = 0;
1241 cfs_spin_unlock(&rq->rq_lock);
1245 /* update the earliest deadline */
1246 if (deadline == -1 || rq->rq_deadline < deadline)
1247 deadline = rq->rq_deadline;
1252 if (++index >= array->paa_size)
1255 array->paa_deadline = deadline;
1256 cfs_spin_unlock(&svcpt->scp_at_lock);
1258 /* we have a new earliest deadline, restart the timer */
1259 ptlrpc_at_set_timer(svcpt);
1261 CDEBUG(D_ADAPTTO, "timeout in %+ds, asking for %d secs on %d early "
1262 "replies\n", first, at_extra, counter);
1264 /* We're already past request deadlines before we even get a
1265 chance to send early replies */
1266 LCONSOLE_WARN("%s: This server is not able to keep up with "
1267 "request traffic (cpu-bound).\n",
1268 svcpt->scp_service->srv_name);
1269 CWARN("earlyQ=%d reqQ=%d recA=%d, svcEst=%d, "
1270 "delay="CFS_DURATION_T"(jiff)\n",
1271 counter, svcpt->scp_nreqs_incoming,
1272 svcpt->scp_nreqs_active,
1273 at_get(&svcpt->scp_at_estimate), delay);
1276 /* we took additional refcount so entries can't be deleted from list, no
1277 * locking is needed */
1278 while (!cfs_list_empty(&work_list)) {
1279 rq = cfs_list_entry(work_list.next, struct ptlrpc_request,
1281 cfs_list_del_init(&rq->rq_timed_list);
1283 if (ptlrpc_at_send_early_reply(rq) == 0)
1284 ptlrpc_at_add_timed(rq);
1286 ptlrpc_server_drop_request(rq);
1289 RETURN(1); /* return "did_something" for liblustre */
1293 * Put the request to the export list if the request may become
1294 * a high priority one.
1296 static int ptlrpc_hpreq_init(struct ptlrpc_service *svc,
1297 struct ptlrpc_request *req)
1302 if (svc->srv_ops.so_hpreq_handler) {
1303 rc = svc->srv_ops.so_hpreq_handler(req);
1307 if (req->rq_export && req->rq_ops) {
1308 /* Perform request specific check. We should do this check
1309 * before the request is added into exp_hp_rpcs list otherwise
1310 * it may hit swab race at LU-1044. */
1311 if (req->rq_ops->hpreq_check)
1312 rc = req->rq_ops->hpreq_check(req);
1314 cfs_spin_lock_bh(&req->rq_export->exp_rpc_lock);
1315 cfs_list_add(&req->rq_exp_list,
1316 &req->rq_export->exp_hp_rpcs);
1317 cfs_spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1323 /** Remove the request from the export list. */
1324 static void ptlrpc_hpreq_fini(struct ptlrpc_request *req)
1327 if (req->rq_export && req->rq_ops) {
1328 /* refresh lock timeout again so that client has more
1329 * room to send lock cancel RPC. */
1330 if (req->rq_ops->hpreq_fini)
1331 req->rq_ops->hpreq_fini(req);
1333 cfs_spin_lock_bh(&req->rq_export->exp_rpc_lock);
1334 cfs_list_del_init(&req->rq_exp_list);
1335 cfs_spin_unlock_bh(&req->rq_export->exp_rpc_lock);
1341 * Make the request a high priority one.
1343 * All the high priority requests are queued in a separate FIFO
1344 * ptlrpc_service_part::scp_hpreq_pending list which is parallel to
1345 * ptlrpc_service_part::scp_req_pending list but has a higher priority
1348 * \see ptlrpc_server_handle_request().
1350 static void ptlrpc_hpreq_reorder_nolock(struct ptlrpc_service_part *svcpt,
1351 struct ptlrpc_request *req)
1355 cfs_spin_lock(&req->rq_lock);
1356 if (req->rq_hp == 0) {
1357 int opc = lustre_msg_get_opc(req->rq_reqmsg);
1359 /* Add to the high priority queue. */
1360 cfs_list_move_tail(&req->rq_list, &svcpt->scp_hreq_pending);
1362 if (opc != OBD_PING)
1363 DEBUG_REQ(D_RPCTRACE, req, "high priority req");
1365 cfs_spin_unlock(&req->rq_lock);
1370 * \see ptlrpc_hpreq_reorder_nolock
1372 void ptlrpc_hpreq_reorder(struct ptlrpc_request *req)
1374 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1377 cfs_spin_lock(&svcpt->scp_req_lock);
1378 /* It may happen that the request is already taken for the processing
1379 * but still in the export list, or the request is not in the request
1380 * queue but in the export list already, do not add it into the
1382 if (!cfs_list_empty(&req->rq_list))
1383 ptlrpc_hpreq_reorder_nolock(svcpt, req);
1384 cfs_spin_unlock(&svcpt->scp_req_lock);
1388 /** Check if the request is a high priority one. */
1389 static int ptlrpc_server_hpreq_check(struct ptlrpc_service *svc,
1390 struct ptlrpc_request *req)
1394 /* Check by request opc. */
1395 if (OBD_PING == lustre_msg_get_opc(req->rq_reqmsg))
1398 RETURN(ptlrpc_hpreq_init(svc, req));
1401 /** Check if a request is a high priority one. */
1402 static int ptlrpc_server_request_add(struct ptlrpc_service_part *svcpt,
1403 struct ptlrpc_request *req)
1408 rc = ptlrpc_server_hpreq_check(svcpt->scp_service, req);
1412 cfs_spin_lock(&svcpt->scp_req_lock);
1415 ptlrpc_hpreq_reorder_nolock(svcpt, req);
1417 cfs_list_add_tail(&req->rq_list, &svcpt->scp_req_pending);
1419 cfs_spin_unlock(&svcpt->scp_req_lock);
1425 * Allow to handle high priority request
1426 * User can call it w/o any lock but need to hold
1427 * ptlrpc_service_part::scp_req_lock to get reliable result
1429 static int ptlrpc_server_allow_high(struct ptlrpc_service_part *svcpt,
1435 if (svcpt->scp_nreqs_active >= svcpt->scp_nthrs_running - 1)
1438 return cfs_list_empty(&svcpt->scp_req_pending) ||
1439 svcpt->scp_hreq_count < svcpt->scp_service->srv_hpreq_ratio;
1442 static int ptlrpc_server_high_pending(struct ptlrpc_service_part *svcpt,
1445 return ptlrpc_server_allow_high(svcpt, force) &&
1446 !cfs_list_empty(&svcpt->scp_hreq_pending);
1450 * Only allow normal priority requests on a service that has a high-priority
1451 * queue if forced (i.e. cleanup), if there are other high priority requests
1452 * already being processed (i.e. those threads can service more high-priority
1453 * requests), or if there are enough idle threads that a later thread can do
1454 * a high priority request.
1455 * User can call it w/o any lock but need to hold
1456 * ptlrpc_service_part::scp_req_lock to get reliable result
1458 static int ptlrpc_server_allow_normal(struct ptlrpc_service_part *svcpt,
1462 if (1) /* always allow to handle normal request for liblustre */
1466 svcpt->scp_nreqs_active < svcpt->scp_nthrs_running - 2)
1469 if (svcpt->scp_nreqs_active >= svcpt->scp_nthrs_running - 1)
1472 return svcpt->scp_nhreqs_active > 0 ||
1473 svcpt->scp_service->srv_ops.so_hpreq_handler == NULL;
1476 static int ptlrpc_server_normal_pending(struct ptlrpc_service_part *svcpt,
1479 return ptlrpc_server_allow_normal(svcpt, force) &&
1480 !cfs_list_empty(&svcpt->scp_req_pending);
1484 * Returns true if there are requests available in incoming
1485 * request queue for processing and it is allowed to fetch them.
1486 * User can call it w/o any lock but need to hold ptlrpc_service::scp_req_lock
1487 * to get reliable result
1488 * \see ptlrpc_server_allow_normal
1489 * \see ptlrpc_server_allow high
1492 ptlrpc_server_request_pending(struct ptlrpc_service_part *svcpt, int force)
1494 return ptlrpc_server_high_pending(svcpt, force) ||
1495 ptlrpc_server_normal_pending(svcpt, force);
1499 * Fetch a request for processing from queue of unprocessed requests.
1500 * Favors high-priority requests.
1501 * Returns a pointer to fetched request.
1503 static struct ptlrpc_request *
1504 ptlrpc_server_request_get(struct ptlrpc_service_part *svcpt, int force)
1506 struct ptlrpc_request *req;
1509 if (ptlrpc_server_high_pending(svcpt, force)) {
1510 req = cfs_list_entry(svcpt->scp_hreq_pending.next,
1511 struct ptlrpc_request, rq_list);
1512 svcpt->scp_hreq_count++;
1516 if (ptlrpc_server_normal_pending(svcpt, force)) {
1517 req = cfs_list_entry(svcpt->scp_req_pending.next,
1518 struct ptlrpc_request, rq_list);
1519 svcpt->scp_hreq_count = 0;
1526 * Handle freshly incoming reqs, add to timed early reply list,
1527 * pass on to regular request queue.
1528 * All incoming requests pass through here before getting into
1529 * ptlrpc_server_handle_req later on.
1532 ptlrpc_server_handle_req_in(struct ptlrpc_service_part *svcpt)
1534 struct ptlrpc_service *svc = svcpt->scp_service;
1535 struct ptlrpc_request *req;
1540 cfs_spin_lock(&svcpt->scp_lock);
1541 if (cfs_list_empty(&svcpt->scp_req_incoming)) {
1542 cfs_spin_unlock(&svcpt->scp_lock);
1546 req = cfs_list_entry(svcpt->scp_req_incoming.next,
1547 struct ptlrpc_request, rq_list);
1548 cfs_list_del_init(&req->rq_list);
1549 svcpt->scp_nreqs_incoming--;
1550 /* Consider this still a "queued" request as far as stats are
1552 cfs_spin_unlock(&svcpt->scp_lock);
1554 /* go through security check/transform */
1555 rc = sptlrpc_svc_unwrap_request(req);
1559 case SECSVC_COMPLETE:
1560 target_send_reply(req, 0, OBD_FAIL_MDS_ALL_REPLY_NET);
1569 * for null-flavored rpc, msg has been unpacked by sptlrpc, although
1570 * redo it wouldn't be harmful.
1572 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
1573 rc = ptlrpc_unpack_req_msg(req, req->rq_reqlen);
1575 CERROR("error unpacking request: ptl %d from %s "
1576 "x"LPU64"\n", svc->srv_req_portal,
1577 libcfs_id2str(req->rq_peer), req->rq_xid);
1582 rc = lustre_unpack_req_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
1584 CERROR ("error unpacking ptlrpc body: ptl %d from %s x"
1585 LPU64"\n", svc->srv_req_portal,
1586 libcfs_id2str(req->rq_peer), req->rq_xid);
1590 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_REQ_OPC) &&
1591 lustre_msg_get_opc(req->rq_reqmsg) == cfs_fail_val) {
1592 CERROR("drop incoming rpc opc %u, x"LPU64"\n",
1593 cfs_fail_val, req->rq_xid);
1598 if (lustre_msg_get_type(req->rq_reqmsg) != PTL_RPC_MSG_REQUEST) {
1599 CERROR("wrong packet type received (type=%u) from %s\n",
1600 lustre_msg_get_type(req->rq_reqmsg),
1601 libcfs_id2str(req->rq_peer));
1605 switch(lustre_msg_get_opc(req->rq_reqmsg)) {
1608 req->rq_bulk_write = 1;
1612 case MGS_CONFIG_READ:
1613 req->rq_bulk_read = 1;
1617 CDEBUG(D_RPCTRACE, "got req x"LPU64"\n", req->rq_xid);
1619 req->rq_export = class_conn2export(
1620 lustre_msg_get_handle(req->rq_reqmsg));
1621 if (req->rq_export) {
1622 rc = ptlrpc_check_req(req);
1624 rc = sptlrpc_target_export_check(req->rq_export, req);
1626 DEBUG_REQ(D_ERROR, req, "DROPPING req with "
1627 "illegal security flavor,");
1632 ptlrpc_update_export_timer(req->rq_export, 0);
1635 /* req_in handling should/must be fast */
1636 if (cfs_time_current_sec() - req->rq_arrival_time.tv_sec > 5)
1637 DEBUG_REQ(D_WARNING, req, "Slow req_in handling "CFS_DURATION_T"s",
1638 cfs_time_sub(cfs_time_current_sec(),
1639 req->rq_arrival_time.tv_sec));
1641 /* Set rpc server deadline and add it to the timed list */
1642 deadline = (lustre_msghdr_get_flags(req->rq_reqmsg) &
1643 MSGHDR_AT_SUPPORT) ?
1644 /* The max time the client expects us to take */
1645 lustre_msg_get_timeout(req->rq_reqmsg) : obd_timeout;
1646 req->rq_deadline = req->rq_arrival_time.tv_sec + deadline;
1647 if (unlikely(deadline == 0)) {
1648 DEBUG_REQ(D_ERROR, req, "Dropping request with 0 timeout");
1652 ptlrpc_at_add_timed(req);
1654 /* Move it over to the request processing queue */
1655 rc = ptlrpc_server_request_add(svcpt, req);
1657 ptlrpc_hpreq_fini(req);
1660 cfs_waitq_signal(&svcpt->scp_waitq);
1664 cfs_spin_lock(&svcpt->scp_req_lock);
1665 svcpt->scp_nreqs_active++;
1666 cfs_spin_unlock(&svcpt->scp_req_lock);
1667 ptlrpc_server_finish_request(svcpt, req);
1673 * Main incoming request handling logic.
1674 * Calls handler function from service to do actual processing.
1677 ptlrpc_server_handle_request(struct ptlrpc_service_part *svcpt,
1678 struct ptlrpc_thread *thread)
1680 struct ptlrpc_service *svc = svcpt->scp_service;
1681 struct obd_export *export = NULL;
1682 struct ptlrpc_request *request;
1683 struct timeval work_start;
1684 struct timeval work_end;
1690 cfs_spin_lock(&svcpt->scp_req_lock);
1692 /* !@%$# liblustre only has 1 thread */
1693 if (cfs_atomic_read(&svcpt->scp_nreps_difficult) != 0) {
1694 cfs_spin_unlock(&svcpt->scp_req_lock);
1698 request = ptlrpc_server_request_get(svcpt, 0);
1699 if (request == NULL) {
1700 cfs_spin_unlock(&svcpt->scp_req_lock);
1704 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT))
1705 fail_opc = OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT;
1706 else if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
1707 fail_opc = OBD_FAIL_PTLRPC_HPREQ_TIMEOUT;
1709 if (unlikely(fail_opc)) {
1710 if (request->rq_export && request->rq_ops) {
1711 cfs_spin_unlock(&svcpt->scp_req_lock);
1713 OBD_FAIL_TIMEOUT(fail_opc, 4);
1715 cfs_spin_lock(&svcpt->scp_req_lock);
1716 request = ptlrpc_server_request_get(svcpt, 0);
1717 if (request == NULL) {
1718 cfs_spin_unlock(&svcpt->scp_req_lock);
1724 cfs_list_del_init(&request->rq_list);
1725 svcpt->scp_nreqs_active++;
1727 svcpt->scp_nhreqs_active++;
1729 cfs_spin_unlock(&svcpt->scp_req_lock);
1731 ptlrpc_rqphase_move(request, RQ_PHASE_INTERPRET);
1733 if(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DUMP_LOG))
1734 libcfs_debug_dumplog();
1736 cfs_gettimeofday(&work_start);
1737 timediff = cfs_timeval_sub(&work_start, &request->rq_arrival_time,NULL);
1738 if (likely(svc->srv_stats != NULL)) {
1739 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
1741 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQQDEPTH_CNTR,
1742 svcpt->scp_nreqs_incoming);
1743 lprocfs_counter_add(svc->srv_stats, PTLRPC_REQACTIVE_CNTR,
1744 svcpt->scp_nreqs_active);
1745 lprocfs_counter_add(svc->srv_stats, PTLRPC_TIMEOUT,
1746 at_get(&svcpt->scp_at_estimate));
1749 rc = lu_context_init(&request->rq_session,
1750 LCT_SESSION|LCT_REMEMBER|LCT_NOREF);
1752 CERROR("Failure to initialize session: %d\n", rc);
1755 request->rq_session.lc_thread = thread;
1756 request->rq_session.lc_cookie = 0x5;
1757 lu_context_enter(&request->rq_session);
1759 CDEBUG(D_NET, "got req "LPU64"\n", request->rq_xid);
1761 request->rq_svc_thread = thread;
1763 request->rq_svc_thread->t_env->le_ses = &request->rq_session;
1765 if (likely(request->rq_export)) {
1766 if (unlikely(ptlrpc_check_req(request)))
1768 ptlrpc_update_export_timer(request->rq_export, timediff >> 19);
1769 export = class_export_rpc_get(request->rq_export);
1772 /* Discard requests queued for longer than the deadline.
1773 The deadline is increased if we send an early reply. */
1774 if (cfs_time_current_sec() > request->rq_deadline) {
1775 DEBUG_REQ(D_ERROR, request, "Dropping timed-out request from %s"
1776 ": deadline "CFS_DURATION_T":"CFS_DURATION_T"s ago\n",
1777 libcfs_id2str(request->rq_peer),
1778 cfs_time_sub(request->rq_deadline,
1779 request->rq_arrival_time.tv_sec),
1780 cfs_time_sub(cfs_time_current_sec(),
1781 request->rq_deadline));
1782 goto put_rpc_export;
1785 CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:nid:opc "
1786 "%s:%s+%d:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1787 (request->rq_export ?
1788 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1789 (request->rq_export ?
1790 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
1791 lustre_msg_get_status(request->rq_reqmsg), request->rq_xid,
1792 libcfs_id2str(request->rq_peer),
1793 lustre_msg_get_opc(request->rq_reqmsg));
1795 if (lustre_msg_get_opc(request->rq_reqmsg) != OBD_PING)
1796 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_PTLRPC_PAUSE_REQ, cfs_fail_val);
1798 rc = svc->srv_ops.so_req_handler(request);
1800 ptlrpc_rqphase_move(request, RQ_PHASE_COMPLETE);
1804 class_export_rpc_put(export);
1806 lu_context_exit(&request->rq_session);
1807 lu_context_fini(&request->rq_session);
1809 if (unlikely(cfs_time_current_sec() > request->rq_deadline)) {
1810 DEBUG_REQ(D_WARNING, request, "Request x"LPU64" took longer "
1811 "than estimated ("CFS_DURATION_T":"CFS_DURATION_T"s);"
1812 " client may timeout.",
1813 request->rq_xid, cfs_time_sub(request->rq_deadline,
1814 request->rq_arrival_time.tv_sec),
1815 cfs_time_sub(cfs_time_current_sec(),
1816 request->rq_deadline));
1819 cfs_gettimeofday(&work_end);
1820 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1821 CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:nid:opc "
1822 "%s:%s+%d:%d:x"LPU64":%s:%d Request procesed in "
1823 "%ldus (%ldus total) trans "LPU64" rc %d/%d\n",
1825 (request->rq_export ?
1826 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
1827 (request->rq_export ?
1828 cfs_atomic_read(&request->rq_export->exp_refcount) : -99),
1829 lustre_msg_get_status(request->rq_reqmsg),
1831 libcfs_id2str(request->rq_peer),
1832 lustre_msg_get_opc(request->rq_reqmsg),
1834 cfs_timeval_sub(&work_end, &request->rq_arrival_time, NULL),
1835 (request->rq_repmsg ?
1836 lustre_msg_get_transno(request->rq_repmsg) :
1837 request->rq_transno),
1839 (request->rq_repmsg ?
1840 lustre_msg_get_status(request->rq_repmsg) : -999));
1841 if (likely(svc->srv_stats != NULL && request->rq_reqmsg != NULL)) {
1842 __u32 op = lustre_msg_get_opc(request->rq_reqmsg);
1843 int opc = opcode_offset(op);
1844 if (opc > 0 && !(op == LDLM_ENQUEUE || op == MDS_REINT)) {
1845 LASSERT(opc < LUSTRE_MAX_OPCODES);
1846 lprocfs_counter_add(svc->srv_stats,
1847 opc + EXTRA_MAX_OPCODES,
1851 if (unlikely(request->rq_early_count)) {
1852 DEBUG_REQ(D_ADAPTTO, request,
1853 "sent %d early replies before finishing in "
1855 request->rq_early_count,
1856 cfs_time_sub(work_end.tv_sec,
1857 request->rq_arrival_time.tv_sec));
1861 ptlrpc_server_finish_request(svcpt, request);
1867 * An internal function to process a single reply state object.
1870 ptlrpc_handle_rs(struct ptlrpc_reply_state *rs)
1872 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
1873 struct ptlrpc_service *svc = svcpt->scp_service;
1874 struct obd_export *exp;
1879 exp = rs->rs_export;
1881 LASSERT (rs->rs_difficult);
1882 LASSERT (rs->rs_scheduled);
1883 LASSERT (cfs_list_empty(&rs->rs_list));
1885 cfs_spin_lock (&exp->exp_lock);
1886 /* Noop if removed already */
1887 cfs_list_del_init (&rs->rs_exp_list);
1888 cfs_spin_unlock (&exp->exp_lock);
1890 /* The disk commit callback holds exp_uncommitted_replies_lock while it
1891 * iterates over newly committed replies, removing them from
1892 * exp_uncommitted_replies. It then drops this lock and schedules the
1893 * replies it found for handling here.
1895 * We can avoid contention for exp_uncommitted_replies_lock between the
1896 * HRT threads and further commit callbacks by checking rs_committed
1897 * which is set in the commit callback while it holds both
1898 * rs_lock and exp_uncommitted_reples.
1900 * If we see rs_committed clear, the commit callback _may_ not have
1901 * handled this reply yet and we race with it to grab
1902 * exp_uncommitted_replies_lock before removing the reply from
1903 * exp_uncommitted_replies. Note that if we lose the race and the
1904 * reply has already been removed, list_del_init() is a noop.
1906 * If we see rs_committed set, we know the commit callback is handling,
1907 * or has handled this reply since store reordering might allow us to
1908 * see rs_committed set out of sequence. But since this is done
1909 * holding rs_lock, we can be sure it has all completed once we hold
1910 * rs_lock, which we do right next.
1912 if (!rs->rs_committed) {
1913 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
1914 cfs_list_del_init(&rs->rs_obd_list);
1915 cfs_spin_unlock(&exp->exp_uncommitted_replies_lock);
1918 cfs_spin_lock(&rs->rs_lock);
1920 been_handled = rs->rs_handled;
1923 nlocks = rs->rs_nlocks; /* atomic "steal", but */
1924 rs->rs_nlocks = 0; /* locks still on rs_locks! */
1926 if (nlocks == 0 && !been_handled) {
1927 /* If we see this, we should already have seen the warning
1928 * in mds_steal_ack_locks() */
1929 CWARN("All locks stolen from rs %p x"LPD64".t"LPD64
1932 rs->rs_xid, rs->rs_transno, rs->rs_opc,
1933 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1936 if ((!been_handled && rs->rs_on_net) || nlocks > 0) {
1937 cfs_spin_unlock(&rs->rs_lock);
1939 if (!been_handled && rs->rs_on_net) {
1940 LNetMDUnlink(rs->rs_md_h);
1941 /* Ignore return code; we're racing with
1945 while (nlocks-- > 0)
1946 ldlm_lock_decref(&rs->rs_locks[nlocks],
1947 rs->rs_modes[nlocks]);
1949 cfs_spin_lock(&rs->rs_lock);
1952 rs->rs_scheduled = 0;
1954 if (!rs->rs_on_net) {
1956 cfs_spin_unlock(&rs->rs_lock);
1958 class_export_put (exp);
1959 rs->rs_export = NULL;
1960 ptlrpc_rs_decref (rs);
1961 if (cfs_atomic_dec_and_test(&svcpt->scp_nreps_difficult) &&
1962 svc->srv_is_stopping)
1963 cfs_waitq_broadcast(&svcpt->scp_waitq);
1967 /* still on the net; callback will schedule */
1968 cfs_spin_unlock(&rs->rs_lock);
1975 * Check whether given service has a reply available for processing
1978 * \param svc a ptlrpc service
1979 * \retval 0 no replies processed
1980 * \retval 1 one reply processed
1983 ptlrpc_server_handle_reply(struct ptlrpc_service_part *svcpt)
1985 struct ptlrpc_reply_state *rs = NULL;
1988 cfs_spin_lock(&svcpt->scp_rep_lock);
1989 if (!cfs_list_empty(&svcpt->scp_rep_queue)) {
1990 rs = cfs_list_entry(svcpt->scp_rep_queue.prev,
1991 struct ptlrpc_reply_state,
1993 cfs_list_del_init(&rs->rs_list);
1995 cfs_spin_unlock(&svcpt->scp_rep_lock);
1997 ptlrpc_handle_rs(rs);
2001 /* FIXME make use of timeout later */
2003 liblustre_check_services (void *arg)
2005 int did_something = 0;
2007 cfs_list_t *tmp, *nxt;
2010 /* I'm relying on being single threaded, not to have to lock
2011 * ptlrpc_all_services etc */
2012 cfs_list_for_each_safe (tmp, nxt, &ptlrpc_all_services) {
2013 struct ptlrpc_service *svc =
2014 cfs_list_entry (tmp, struct ptlrpc_service, srv_list);
2015 struct ptlrpc_service_part *svcpt;
2017 svcpt = svc->srv_part;
2019 if (svcpt->scp_nthrs_running != 0) /* I've recursed */
2022 /* service threads can block for bulk, so this limits us
2023 * (arbitrarily) to recursing 1 stack frame per service.
2024 * Note that the problem with recursion is that we have to
2025 * unwind completely before our caller can resume. */
2027 svcpt->scp_nthrs_running++;
2030 rc = ptlrpc_server_handle_req_in(svcpt);
2031 rc |= ptlrpc_server_handle_reply(svcpt);
2032 rc |= ptlrpc_at_check_timed(svcpt);
2033 rc |= ptlrpc_server_handle_request(svcpt, NULL);
2034 rc |= (ptlrpc_server_post_idle_rqbds(svcpt) > 0);
2035 did_something |= rc;
2038 svcpt->scp_nthrs_running--;
2041 RETURN(did_something);
2043 #define ptlrpc_stop_all_threads(s) do {} while (0)
2045 #else /* __KERNEL__ */
2048 ptlrpc_check_rqbd_pool(struct ptlrpc_service_part *svcpt)
2050 int avail = svcpt->scp_nrqbds_posted;
2051 int low_water = test_req_buffer_pressure ? 0 :
2052 svcpt->scp_service->srv_nbuf_per_group / 2;
2054 /* NB I'm not locking; just looking. */
2056 /* CAVEAT EMPTOR: We might be allocating buffers here because we've
2057 * allowed the request history to grow out of control. We could put a
2058 * sanity check on that here and cull some history if we need the
2061 if (avail <= low_water)
2062 ptlrpc_grow_req_bufs(svcpt);
2064 if (svcpt->scp_service->srv_stats) {
2065 lprocfs_counter_add(svcpt->scp_service->srv_stats,
2066 PTLRPC_REQBUF_AVAIL_CNTR, avail);
2071 ptlrpc_retry_rqbds(void *arg)
2073 struct ptlrpc_service_part *svcpt = (struct ptlrpc_service_part *)arg;
2075 svcpt->scp_rqbd_timeout = 0;
2080 ptlrpc_threads_enough(struct ptlrpc_service_part *svcpt)
2082 return svcpt->scp_nreqs_active <
2083 svcpt->scp_nthrs_running - 1 -
2084 (svcpt->scp_service->srv_ops.so_hpreq_handler != NULL);
2088 * allowed to create more threads
2089 * user can call it w/o any lock but need to hold
2090 * ptlrpc_service_part::scp_lock to get reliable result
2093 ptlrpc_threads_increasable(struct ptlrpc_service_part *svcpt)
2095 return svcpt->scp_nthrs_running +
2096 svcpt->scp_nthrs_starting < svcpt->scp_service->srv_threads_max;
2100 * too many requests and allowed to create more threads
2103 ptlrpc_threads_need_create(struct ptlrpc_service_part *svcpt)
2105 return !ptlrpc_threads_enough(svcpt) &&
2106 ptlrpc_threads_increasable(svcpt);
2110 ptlrpc_thread_stopping(struct ptlrpc_thread *thread)
2112 return thread_is_stopping(thread) ||
2113 thread->t_svcpt->scp_service->srv_is_stopping;
2117 ptlrpc_rqbd_pending(struct ptlrpc_service_part *svcpt)
2119 return !cfs_list_empty(&svcpt->scp_rqbd_idle) &&
2120 svcpt->scp_rqbd_timeout == 0;
2124 ptlrpc_at_check(struct ptlrpc_service_part *svcpt)
2126 return svcpt->scp_at_check;
2130 * requests wait on preprocessing
2131 * user can call it w/o any lock but need to hold
2132 * ptlrpc_service_part::scp_lock to get reliable result
2135 ptlrpc_server_request_incoming(struct ptlrpc_service_part *svcpt)
2137 return !cfs_list_empty(&svcpt->scp_req_incoming);
2140 static __attribute__((__noinline__)) int
2141 ptlrpc_wait_event(struct ptlrpc_service_part *svcpt,
2142 struct ptlrpc_thread *thread)
2144 /* Don't exit while there are replies to be handled */
2145 struct l_wait_info lwi = LWI_TIMEOUT(svcpt->scp_rqbd_timeout,
2146 ptlrpc_retry_rqbds, svcpt);
2148 lc_watchdog_disable(thread->t_watchdog);
2152 l_wait_event_exclusive_head(svcpt->scp_waitq,
2153 ptlrpc_thread_stopping(thread) ||
2154 ptlrpc_server_request_incoming(svcpt) ||
2155 ptlrpc_server_request_pending(svcpt, 0) ||
2156 ptlrpc_rqbd_pending(svcpt) ||
2157 ptlrpc_at_check(svcpt), &lwi);
2159 if (ptlrpc_thread_stopping(thread))
2162 lc_watchdog_touch(thread->t_watchdog,
2163 ptlrpc_server_get_timeout(svcpt));
2168 * Main thread body for service threads.
2169 * Waits in a loop waiting for new requests to process to appear.
2170 * Every time an incoming requests is added to its queue, a waitq
2171 * is woken up and one of the threads will handle it.
2173 static int ptlrpc_main(void *arg)
2175 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
2176 struct ptlrpc_thread *thread = data->thread;
2177 struct ptlrpc_service_part *svcpt = thread->t_svcpt;
2178 struct ptlrpc_service *svc = svcpt->scp_service;
2179 struct ptlrpc_reply_state *rs;
2180 #ifdef WITH_GROUP_INFO
2181 cfs_group_info_t *ginfo = NULL;
2184 int counter = 0, rc = 0;
2187 thread->t_pid = cfs_curproc_pid();
2188 cfs_daemonize_ctxt(data->name);
2190 #if defined(HAVE_NODE_TO_CPUMASK) && defined(CONFIG_NUMA)
2191 /* we need to do this before any per-thread allocation is done so that
2192 * we get the per-thread allocations on local node. bug 7342 */
2193 if (svc->srv_cpu_affinity) {
2196 for (cpu = 0, num_cpu = 0; cpu < cfs_num_possible_cpus();
2198 if (!cpu_online(cpu))
2200 if (num_cpu == thread->t_id % cfs_num_online_cpus())
2204 cfs_set_cpus_allowed(cfs_current(),
2205 node_to_cpumask(cpu_to_node(cpu)));
2209 #ifdef WITH_GROUP_INFO
2210 ginfo = cfs_groups_alloc(0);
2216 cfs_set_current_groups(ginfo);
2217 cfs_put_group_info(ginfo);
2220 if (svc->srv_ops.so_thr_init != NULL) {
2221 rc = svc->srv_ops.so_thr_init(thread);
2232 rc = lu_context_init(&env->le_ctx,
2233 svc->srv_ctx_tags|LCT_REMEMBER|LCT_NOREF);
2237 thread->t_env = env;
2238 env->le_ctx.lc_thread = thread;
2239 env->le_ctx.lc_cookie = 0x6;
2241 /* Alloc reply state structure for this one */
2242 OBD_ALLOC_LARGE(rs, svc->srv_max_reply_size);
2248 cfs_spin_lock(&svcpt->scp_lock);
2250 LASSERT(thread_is_starting(thread));
2251 thread_clear_flags(thread, SVC_STARTING);
2253 svcpt->scp_nthrs_starting--;
2255 /* SVC_STOPPING may already be set here if someone else is trying
2256 * to stop the service while this new thread has been dynamically
2257 * forked. We still set SVC_RUNNING to let our creator know that
2258 * we are now running, however we will exit as soon as possible */
2259 thread_add_flags(thread, SVC_RUNNING);
2260 svcpt->scp_nthrs_running++;
2261 cfs_spin_unlock(&svcpt->scp_lock);
2264 * wake up our creator. Note: @data is invalid after this point,
2265 * because it's allocated on ptlrpc_start_thread() stack.
2267 cfs_waitq_signal(&thread->t_ctl_waitq);
2269 thread->t_watchdog = lc_watchdog_add(ptlrpc_server_get_timeout(svcpt),
2272 cfs_spin_lock(&svcpt->scp_rep_lock);
2273 cfs_list_add(&rs->rs_list, &svcpt->scp_rep_idle);
2274 cfs_waitq_signal(&svcpt->scp_rep_waitq);
2275 cfs_spin_unlock(&svcpt->scp_rep_lock);
2277 CDEBUG(D_NET, "service thread %d (#%d) started\n", thread->t_id,
2278 svcpt->scp_nthrs_running);
2280 /* XXX maintain a list of all managed devices: insert here */
2281 while (!ptlrpc_thread_stopping(thread)) {
2282 if (ptlrpc_wait_event(svcpt, thread))
2285 ptlrpc_check_rqbd_pool(svcpt);
2287 if (ptlrpc_threads_need_create(svcpt)) {
2288 /* Ignore return code - we tried... */
2289 ptlrpc_start_thread(svcpt);
2292 /* Process all incoming reqs before handling any */
2293 if (ptlrpc_server_request_incoming(svcpt)) {
2294 ptlrpc_server_handle_req_in(svcpt);
2295 /* but limit ourselves in case of flood */
2296 if (counter++ < 100)
2301 if (ptlrpc_at_check(svcpt))
2302 ptlrpc_at_check_timed(svcpt);
2304 if (ptlrpc_server_request_pending(svcpt, 0)) {
2305 lu_context_enter(&env->le_ctx);
2306 ptlrpc_server_handle_request(svcpt, thread);
2307 lu_context_exit(&env->le_ctx);
2310 if (ptlrpc_rqbd_pending(svcpt) &&
2311 ptlrpc_server_post_idle_rqbds(svcpt) < 0) {
2312 /* I just failed to repost request buffers.
2313 * Wait for a timeout (unless something else
2314 * happens) before I try again */
2315 svcpt->scp_rqbd_timeout = cfs_time_seconds(1) / 10;
2316 CDEBUG(D_RPCTRACE, "Posted buffers: %d\n",
2317 svcpt->scp_nrqbds_posted);
2321 lc_watchdog_delete(thread->t_watchdog);
2322 thread->t_watchdog = NULL;
2326 * deconstruct service specific state created by ptlrpc_start_thread()
2328 if (svc->srv_ops.so_thr_done != NULL)
2329 svc->srv_ops.so_thr_done(thread);
2332 lu_context_fini(&env->le_ctx);
2336 CDEBUG(D_RPCTRACE, "service thread [ %p : %u ] %d exiting: rc %d\n",
2337 thread, thread->t_pid, thread->t_id, rc);
2339 cfs_spin_lock(&svcpt->scp_lock);
2340 if (thread_test_and_clear_flags(thread, SVC_STARTING))
2341 svcpt->scp_nthrs_starting--;
2343 if (thread_test_and_clear_flags(thread, SVC_RUNNING)) {
2344 /* must know immediately */
2345 svcpt->scp_nthrs_running--;
2349 thread_add_flags(thread, SVC_STOPPED);
2351 cfs_waitq_signal(&thread->t_ctl_waitq);
2352 cfs_spin_unlock(&svcpt->scp_lock);
2357 struct ptlrpc_hr_args {
2360 struct ptlrpc_hr_service *hrs;
2363 static int hrt_dont_sleep(struct ptlrpc_hr_thread *t,
2364 cfs_list_t *replies)
2368 cfs_spin_lock(&t->hrt_lock);
2369 cfs_list_splice_init(&t->hrt_queue, replies);
2370 result = cfs_test_bit(HRT_STOPPING, &t->hrt_flags) ||
2371 !cfs_list_empty(replies);
2372 cfs_spin_unlock(&t->hrt_lock);
2377 * Main body of "handle reply" function.
2378 * It processes acked reply states
2380 static int ptlrpc_hr_main(void *arg)
2382 struct ptlrpc_hr_args * hr_args = arg;
2383 struct ptlrpc_hr_service *hr = hr_args->hrs;
2384 struct ptlrpc_hr_thread *t = &hr->hr_threads[hr_args->thread_index];
2385 char threadname[20];
2386 CFS_LIST_HEAD(replies);
2388 snprintf(threadname, sizeof(threadname),
2389 "ptlrpc_hr_%d", hr_args->thread_index);
2391 cfs_daemonize_ctxt(threadname);
2392 #if defined(CONFIG_NUMA) && defined(HAVE_NODE_TO_CPUMASK)
2393 cfs_set_cpus_allowed(cfs_current(),
2394 node_to_cpumask(cpu_to_node(hr_args->cpu_index)));
2396 cfs_set_bit(HRT_RUNNING, &t->hrt_flags);
2397 cfs_waitq_signal(&t->hrt_wait);
2399 while (!cfs_test_bit(HRT_STOPPING, &t->hrt_flags)) {
2401 l_wait_condition(t->hrt_wait, hrt_dont_sleep(t, &replies));
2402 while (!cfs_list_empty(&replies)) {
2403 struct ptlrpc_reply_state *rs;
2405 rs = cfs_list_entry(replies.prev,
2406 struct ptlrpc_reply_state,
2408 cfs_list_del_init(&rs->rs_list);
2409 ptlrpc_handle_rs(rs);
2413 cfs_clear_bit(HRT_RUNNING, &t->hrt_flags);
2414 cfs_complete(&t->hrt_completion);
2419 static int ptlrpc_start_hr_thread(struct ptlrpc_hr_service *hr, int n, int cpu)
2421 struct ptlrpc_hr_thread *t = &hr->hr_threads[n];
2422 struct ptlrpc_hr_args args;
2426 args.thread_index = n;
2427 args.cpu_index = cpu;
2430 rc = cfs_create_thread(ptlrpc_hr_main, (void*)&args, CFS_DAEMON_FLAGS);
2432 cfs_complete(&t->hrt_completion);
2435 l_wait_condition(t->hrt_wait, cfs_test_bit(HRT_RUNNING, &t->hrt_flags));
2441 static void ptlrpc_stop_hr_thread(struct ptlrpc_hr_thread *t)
2445 cfs_set_bit(HRT_STOPPING, &t->hrt_flags);
2446 cfs_waitq_signal(&t->hrt_wait);
2447 cfs_wait_for_completion(&t->hrt_completion);
2452 static void ptlrpc_stop_hr_threads(struct ptlrpc_hr_service *hrs)
2457 for (n = 0; n < hrs->hr_n_threads; n++)
2458 ptlrpc_stop_hr_thread(&hrs->hr_threads[n]);
2463 static int ptlrpc_start_hr_threads(struct ptlrpc_hr_service *hr)
2466 int n, cpu, threads_started = 0;
2469 LASSERT(hr != NULL);
2470 LASSERT(hr->hr_n_threads > 0);
2472 for (n = 0, cpu = 0; n < hr->hr_n_threads; n++) {
2473 #if defined(CONFIG_SMP) && defined(HAVE_NODE_TO_CPUMASK)
2474 while (!cpu_online(cpu)) {
2476 if (cpu >= cfs_num_possible_cpus())
2480 rc = ptlrpc_start_hr_thread(hr, n, cpu);
2486 if (threads_started == 0) {
2487 CERROR("No reply handling threads started\n");
2490 if (threads_started < hr->hr_n_threads) {
2491 CWARN("Started only %d reply handling threads from %d\n",
2492 threads_started, hr->hr_n_threads);
2493 hr->hr_n_threads = threads_started;
2498 static void ptlrpc_stop_thread(struct ptlrpc_service_part *svcpt,
2499 struct ptlrpc_thread *thread)
2501 struct l_wait_info lwi = { 0 };
2504 CDEBUG(D_RPCTRACE, "Stopping thread [ %p : %u ]\n",
2505 thread, thread->t_pid);
2507 cfs_spin_lock(&svcpt->scp_lock);
2508 /* let the thread know that we would like it to stop asap */
2509 thread_add_flags(thread, SVC_STOPPING);
2510 cfs_spin_unlock(&svcpt->scp_lock);
2512 cfs_waitq_broadcast(&svcpt->scp_waitq);
2513 l_wait_event(thread->t_ctl_waitq,
2514 thread_is_stopped(thread), &lwi);
2516 cfs_spin_lock(&svcpt->scp_lock);
2517 cfs_list_del(&thread->t_link);
2518 cfs_spin_unlock(&svcpt->scp_lock);
2520 OBD_FREE_PTR(thread);
2525 * Stops all threads of a particular service \a svc
2527 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
2529 struct ptlrpc_service_part *svcpt = svc->srv_part;
2530 struct ptlrpc_thread *thread;
2533 LASSERT(svcpt != NULL);
2535 cfs_spin_lock(&svcpt->scp_lock);
2536 while (!cfs_list_empty(&svcpt->scp_threads)) {
2537 thread = cfs_list_entry(svcpt->scp_threads.next,
2538 struct ptlrpc_thread, t_link);
2540 cfs_spin_unlock(&svcpt->scp_lock);
2541 ptlrpc_stop_thread(svcpt, thread);
2542 cfs_spin_lock(&svcpt->scp_lock);
2545 cfs_spin_unlock(&svcpt->scp_lock);
2549 int ptlrpc_start_threads(struct ptlrpc_service *svc)
2554 /* We require 2 threads min - see note in
2555 ptlrpc_server_handle_request */
2556 LASSERT(svc->srv_threads_min >= 2);
2557 for (i = 0; i < svc->srv_threads_min; i++) {
2558 rc = ptlrpc_start_thread(svc->srv_part);
2559 /* We have enough threads, don't start more. b=15759 */
2560 if (rc == -EMFILE) {
2565 CERROR("cannot start %s thread #%d: rc %d\n",
2566 svc->srv_thread_name, i, rc);
2567 ptlrpc_stop_all_threads(svc);
2574 int ptlrpc_start_thread(struct ptlrpc_service_part *svcpt)
2576 struct l_wait_info lwi = { 0 };
2577 struct ptlrpc_svc_data d;
2578 struct ptlrpc_thread *thread;
2579 struct ptlrpc_service *svc = svcpt->scp_service;
2584 LASSERT(svcpt != NULL);
2586 CDEBUG(D_RPCTRACE, "%s started %d min %d max %d\n",
2587 svc->srv_name, svcpt->scp_nthrs_running,
2588 svc->srv_threads_min, svc->srv_threads_max);
2590 if (unlikely(svc->srv_is_stopping))
2593 if (!ptlrpc_threads_increasable(svcpt) ||
2594 (OBD_FAIL_CHECK(OBD_FAIL_TGT_TOOMANY_THREADS) &&
2595 svcpt->scp_nthrs_running == svc->srv_threads_min - 1))
2598 OBD_ALLOC_PTR(thread);
2601 cfs_waitq_init(&thread->t_ctl_waitq);
2603 cfs_spin_lock(&svcpt->scp_lock);
2604 if (!ptlrpc_threads_increasable(svcpt)) {
2605 cfs_spin_unlock(&svcpt->scp_lock);
2606 OBD_FREE_PTR(thread);
2610 svcpt->scp_nthrs_starting++;
2611 thread->t_id = svcpt->scp_thr_nextid++;
2612 thread_add_flags(thread, SVC_STARTING);
2613 thread->t_svcpt = svcpt;
2615 cfs_list_add(&thread->t_link, &svcpt->scp_threads);
2616 cfs_spin_unlock(&svcpt->scp_lock);
2618 sprintf(name, "%s_%02d", svc->srv_thread_name, thread->t_id);
2623 CDEBUG(D_RPCTRACE, "starting thread '%s'\n", name);
2625 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
2626 * just drop the VM and FILES in cfs_daemonize_ctxt() right away.
2628 rc = cfs_create_thread(ptlrpc_main, &d, CFS_DAEMON_FLAGS);
2630 CERROR("cannot start thread '%s': rc %d\n", name, rc);
2632 cfs_spin_lock(&svcpt->scp_lock);
2633 cfs_list_del(&thread->t_link);
2634 --svcpt->scp_nthrs_starting;
2635 cfs_spin_unlock(&svcpt->scp_lock);
2637 OBD_FREE(thread, sizeof(*thread));
2640 l_wait_event(thread->t_ctl_waitq,
2641 thread_is_running(thread) || thread_is_stopped(thread),
2644 rc = thread_is_stopped(thread) ? thread->t_id : 0;
2648 int ptlrpc_hr_init(void)
2651 int n_cpus = cfs_num_online_cpus();
2652 struct ptlrpc_hr_service *hr;
2657 LASSERT(ptlrpc_hr == NULL);
2659 size = offsetof(struct ptlrpc_hr_service, hr_threads[n_cpus]);
2660 OBD_ALLOC(hr, size);
2663 for (i = 0; i < n_cpus; i++) {
2664 struct ptlrpc_hr_thread *t = &hr->hr_threads[i];
2666 cfs_spin_lock_init(&t->hrt_lock);
2667 cfs_waitq_init(&t->hrt_wait);
2668 CFS_INIT_LIST_HEAD(&t->hrt_queue);
2669 cfs_init_completion(&t->hrt_completion);
2671 hr->hr_n_threads = n_cpus;
2675 rc = ptlrpc_start_hr_threads(hr);
2677 OBD_FREE(hr, hr->hr_size);
2683 void ptlrpc_hr_fini(void)
2685 if (ptlrpc_hr != NULL) {
2686 ptlrpc_stop_hr_threads(ptlrpc_hr);
2687 OBD_FREE(ptlrpc_hr, ptlrpc_hr->hr_size);
2692 #endif /* __KERNEL__ */
2695 * Wait until all already scheduled replies are processed.
2697 static void ptlrpc_wait_replies(struct ptlrpc_service_part *svcpt)
2701 struct l_wait_info lwi = LWI_TIMEOUT(cfs_time_seconds(10),
2704 rc = l_wait_event(svcpt->scp_waitq,
2705 cfs_atomic_read(&svcpt->scp_nreps_difficult) == 0, &lwi);
2708 CWARN("Unexpectedly long timeout %s %p\n",
2709 svcpt->scp_service->srv_name, svcpt->scp_service);
2713 int ptlrpc_unregister_service(struct ptlrpc_service *service)
2715 struct l_wait_info lwi;
2716 struct ptlrpc_service_part *svcpt;
2717 struct ptlrpc_reply_state *rs;
2718 struct ptlrpc_reply_state *t;
2719 struct ptlrpc_at_array *array;
2724 service->srv_is_stopping = 1;
2725 svcpt = service->srv_part;
2727 if (svcpt == NULL || /* no instance of ptlrpc_service_part */
2728 svcpt->scp_service == NULL) /* it's not fully initailzed */
2731 cfs_timer_disarm(&svcpt->scp_at_timer);
2733 ptlrpc_stop_all_threads(service);
2735 cfs_spin_lock (&ptlrpc_all_services_lock);
2736 cfs_list_del_init (&service->srv_list);
2737 cfs_spin_unlock (&ptlrpc_all_services_lock);
2739 ptlrpc_lprocfs_unregister_service(service);
2741 /* All history will be culled when the next request buffer is
2743 service->srv_max_history_rqbds = 0;
2745 CDEBUG(D_NET, "%s: tearing down\n", service->srv_name);
2747 rc = LNetClearLazyPortal(service->srv_req_portal);
2750 /* Unlink all the request buffers. This forces a 'final' event with
2751 * its 'unlink' flag set for each posted rqbd */
2752 cfs_list_for_each(tmp, &svcpt->scp_rqbd_posted) {
2753 struct ptlrpc_request_buffer_desc *rqbd =
2754 cfs_list_entry(tmp, struct ptlrpc_request_buffer_desc,
2757 rc = LNetMDUnlink(rqbd->rqbd_md_h);
2758 LASSERT (rc == 0 || rc == -ENOENT);
2761 /* Wait for the network to release any buffers it's currently
2764 cfs_spin_lock(&svcpt->scp_lock);
2765 rc = svcpt->scp_nrqbds_posted;
2766 cfs_spin_unlock(&svcpt->scp_lock);
2771 /* Network access will complete in finite time but the HUGE
2772 * timeout lets us CWARN for visibility of sluggish NALs */
2773 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2774 cfs_time_seconds(1), NULL, NULL);
2775 rc = l_wait_event(svcpt->scp_waitq,
2776 svcpt->scp_nrqbds_posted == 0, &lwi);
2777 if (rc == -ETIMEDOUT)
2778 CWARN("Service %s waiting for request buffers\n",
2782 /* schedule all outstanding replies to terminate them */
2783 cfs_spin_lock(&svcpt->scp_rep_lock);
2784 while (!cfs_list_empty(&svcpt->scp_rep_active)) {
2785 struct ptlrpc_reply_state *rs =
2786 cfs_list_entry(svcpt->scp_rep_active.next,
2787 struct ptlrpc_reply_state, rs_list);
2788 cfs_spin_lock(&rs->rs_lock);
2789 ptlrpc_schedule_difficult_reply(rs);
2790 cfs_spin_unlock(&rs->rs_lock);
2792 cfs_spin_unlock(&svcpt->scp_rep_lock);
2794 /* purge the request queue. NB No new replies (rqbds all unlinked)
2795 * and no service threads, so I'm the only thread noodling the
2796 * request queue now */
2797 while (!cfs_list_empty(&svcpt->scp_req_incoming)) {
2798 struct ptlrpc_request *req =
2799 cfs_list_entry(svcpt->scp_req_incoming.next,
2800 struct ptlrpc_request,
2803 cfs_list_del(&req->rq_list);
2804 svcpt->scp_nreqs_incoming--;
2805 svcpt->scp_nreqs_active++;
2806 ptlrpc_server_finish_request(svcpt, req);
2808 while (ptlrpc_server_request_pending(svcpt, 1)) {
2809 struct ptlrpc_request *req;
2811 req = ptlrpc_server_request_get(svcpt, 1);
2812 cfs_list_del(&req->rq_list);
2813 svcpt->scp_nreqs_active++;
2814 ptlrpc_server_finish_request(svcpt, req);
2816 LASSERT(svcpt->scp_nreqs_incoming == 0);
2817 LASSERT(svcpt->scp_nreqs_active == 0);
2818 LASSERT(svcpt->scp_hist_nrqbds == 0);
2819 LASSERT(cfs_list_empty(&svcpt->scp_rqbd_posted));
2821 /* Now free all the request buffers since nothing references them
2823 while (!cfs_list_empty(&svcpt->scp_rqbd_idle)) {
2824 struct ptlrpc_request_buffer_desc *rqbd =
2825 cfs_list_entry(svcpt->scp_rqbd_idle.next,
2826 struct ptlrpc_request_buffer_desc,
2829 ptlrpc_free_rqbd(rqbd);
2832 ptlrpc_wait_replies(svcpt);
2834 cfs_list_for_each_entry_safe(rs, t, &svcpt->scp_rep_idle, rs_list) {
2835 cfs_list_del(&rs->rs_list);
2836 OBD_FREE_LARGE(rs, service->srv_max_reply_size);
2839 /* In case somebody rearmed this in the meantime */
2840 cfs_timer_disarm(&svcpt->scp_at_timer);
2842 array = &svcpt->scp_at_array;
2843 if (array->paa_reqs_array != NULL) {
2844 OBD_FREE(array->paa_reqs_array,
2845 sizeof(cfs_list_t) * array->paa_size);
2846 array->paa_reqs_array = NULL;
2849 if (array->paa_reqs_count != NULL) {
2850 OBD_FREE(array->paa_reqs_count,
2851 sizeof(__u32) * array->paa_size);
2852 array->paa_reqs_count= NULL;
2855 OBD_FREE_PTR(svcpt);
2857 OBD_FREE_PTR(service);
2862 * Returns 0 if the service is healthy.
2864 * Right now, it just checks to make sure that requests aren't languishing
2865 * in the queue. We'll use this health check to govern whether a node needs
2866 * to be shot, so it's intentionally non-aggressive. */
2867 int ptlrpc_service_health_check(struct ptlrpc_service *svc)
2869 struct ptlrpc_service_part *svcpt;
2870 struct ptlrpc_request *request;
2871 struct timeval right_now;
2874 if (svc == NULL || svc->srv_part == NULL)
2877 cfs_gettimeofday(&right_now);
2879 svcpt = svc->srv_part;
2880 cfs_spin_lock(&svcpt->scp_req_lock);
2881 if (!ptlrpc_server_request_pending(svcpt, 1)) {
2882 cfs_spin_unlock(&svcpt->scp_req_lock);
2886 /* How long has the next entry been waiting? */
2887 if (cfs_list_empty(&svcpt->scp_req_pending)) {
2888 request = cfs_list_entry(svcpt->scp_hreq_pending.next,
2889 struct ptlrpc_request, rq_list);
2891 request = cfs_list_entry(svcpt->scp_req_pending.next,
2892 struct ptlrpc_request, rq_list);
2895 timediff = cfs_timeval_sub(&right_now, &request->rq_arrival_time, NULL);
2896 cfs_spin_unlock(&svcpt->scp_req_lock);
2898 if ((timediff / ONE_MILLION) >
2899 (AT_OFF ? obd_timeout * 3 / 2 : at_max)) {
2900 CERROR("%s: unhealthy - request has been waiting %lds\n",
2901 svcpt->scp_service->srv_name, timediff / ONE_MILLION);