Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp35608361rwd; Mon, 10 Jul 2023 09:46:54 -0700 (PDT) X-Google-Smtp-Source: APBJJlGZ3fLTD2OOqs7DvZGOJrUExQVKxeR8bsYMA3Vpn1M3CdZWKPiYNopTdpTav3xpsTgF0vWp X-Received: by 2002:ac2:5b9b:0:b0:4fb:7be5:46b3 with SMTP id o27-20020ac25b9b000000b004fb7be546b3mr9574888lfn.55.1689007614222; Mon, 10 Jul 2023 09:46:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689007614; cv=none; d=google.com; s=arc-20160816; b=r37WUAo+M7UiytUIR86Y7IPlYtNFS8m20rDAEG6KX6E1S3mQHViQySMLVRxycb4dcF 0V4thK36X7ktBYBjy3XSOE6UPz6h2RBd4xZ5y4MSIKFH8Xkmr7OXQTgu1V68y4hN11Dg 78IvtYItrrr/0kLLegf7BFN9Dtn6U0pMgaF9yDgUtzVk4V7yiVaxnfP5/2bIbT20hjTY SFI56O2rVjXPyeRn8NvWyoSTx/oP4nMLpxrblOXdh4vejwdpnD3hJKXukGGM9W1juTfe y/mD1bm1ushfyJ7jO1xr+6begTgngnO9HP/6dBfLXRAfgHDm/6Sg9iHYtFPO6jfihi2K yX4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:dkim-signature; bh=PcpIPeHhWc2m2lhZkVjMnOKb7FhhmNhwHFxKRRno+6Y=; fh=1mpseLkGFXp1jGJr9QrS6J8xZJaGrNTaerrRbpi6Fog=; b=Wz/QgIKp2c3Fylodc49on+gUfJBoJSBX6cYwwNbxA+lPSfQCs+knvQRyD/Y39oESfA QKfnp2ijd4IKC2En2H/OXhbQRJW1bJH1hOS7mHqmNp90Wr+pYSB9lIkzHfCTN/9DGwEq o3IaCORkJcrniYCUvXHI8IPeo4CvbZnZur4quQgKKaoXh8UVQxn6eNrjFqJ/TvR3flkj jJ/Pfv3mLQQqWw3HG27AVxMtQJqjKdelXe3rdmGUqbN5muRqO8SlW+Icp/qqo4eE91R5 GHZjXjD/maoU+EnASPPWxtBTDZkyq6nFcIay6vXbzA4RFawsTODhSJ8m1yGiGTISPKNz sD3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nHNXX72O; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a1709063a8c00b00992ee6da875si7021803ejd.218.2023.07.10.09.46.29; Mon, 10 Jul 2023 09:46:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nHNXX72O; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230227AbjGJQmE (ORCPT + 99 others); Mon, 10 Jul 2023 12:42:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230016AbjGJQmD (ORCPT ); Mon, 10 Jul 2023 12:42:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D449AC0 for ; Mon, 10 Jul 2023 09:42:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 689D161124 for ; Mon, 10 Jul 2023 16:42:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C52BC433C8; Mon, 10 Jul 2023 16:42:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689007321; bh=V7jVm1GC/C3FYiTl8Eo30S8B6hhZ+AOBMyp56VVnInc=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=nHNXX72OJ8iuOXwpdj/B6hEFgOf57RAPCptxKVU+6h0oX3OtV468gEnpartQhkXAa 7bYqdm9FMVg+g19zn2dfaBRCNMeJ2XKSaY8vMvSZxXw7U+UWL6AUUHTSTyNfpfSf3c G/IQF6mrr3xD/COZIgg9m8+aa2WB+wzL4l/q1ih+BQJ3/ttgcmi5/Mx146RP+4sBiT 4oJxzJh66HuDToaUdu7kp0o/UnY1ySgRbWzY2lSs3/p3WzYehdsfYI3sTUF5iiepb5 nxDW6r/UJIpww/5LKUUze2sqd5qhE95Sit1DPd/tJ32iX2Qg0RW6ahTxXHDfvv1iFw 7EMq+11kZmBtw== Subject: [PATCH v3 1/9] SUNRPC: Deduplicate thread wake-up code From: Chuck Lever To: linux-nfs@vger.kernel.org Cc: Chuck Lever , lorenzo@kernel.org, neilb@suse.de, jlayton@redhat.com, david@fromorbit.com Date: Mon, 10 Jul 2023 12:42:00 -0400 Message-ID: <168900732034.7514.4396138579922803024.stgit@manet.1015granger.net> In-Reply-To: <168900729243.7514.15141312295052254929.stgit@manet.1015granger.net> References: <168900729243.7514.15141312295052254929.stgit@manet.1015granger.net> User-Agent: StGit/1.5 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org From: Chuck Lever Refactor: Extract the loop that finds an idle service thread from svc_xprt_enqueue() and svc_wake_up(). Both functions do just about the same thing. Signed-off-by: Chuck Lever --- include/linux/sunrpc/svc.h | 1 + net/sunrpc/svc.c | 28 ++++++++++++++++++++++++++ net/sunrpc/svc_xprt.c | 48 +++++++++++++++----------------------------- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/include/linux/sunrpc/svc.h b/include/linux/sunrpc/svc.h index f8751118c122..dc2d90a655e2 100644 --- a/include/linux/sunrpc/svc.h +++ b/include/linux/sunrpc/svc.h @@ -427,6 +427,7 @@ int svc_register(const struct svc_serv *, struct net *, const int, void svc_wake_up(struct svc_serv *); void svc_reserve(struct svc_rqst *rqstp, int space); +struct svc_rqst *svc_pool_wake_idle_thread(struct svc_pool *pool); struct svc_pool *svc_pool_for_cpu(struct svc_serv *serv); char * svc_print_addr(struct svc_rqst *, char *, size_t); const char * svc_proc_name(const struct svc_rqst *rqstp); diff --git a/net/sunrpc/svc.c b/net/sunrpc/svc.c index 587811a002c9..05ee92b5fa1e 100644 --- a/net/sunrpc/svc.c +++ b/net/sunrpc/svc.c @@ -689,6 +689,34 @@ svc_prepare_thread(struct svc_serv *serv, struct svc_pool *pool, int node) return rqstp; } +/** + * svc_pool_wake_idle_thread - Awaken an idle thread in @pool + * @pool: service thread pool + * + * Returns an idle service thread (now marked BUSY), or NULL + * if no service threads are available. Finding an idle service + * thread and marking it BUSY is atomic with respect to other + * calls to svc_pool_wake_idle_thread(). + */ +struct svc_rqst *svc_pool_wake_idle_thread(struct svc_pool *pool) +{ + struct svc_rqst *rqstp; + + rcu_read_lock(); + list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { + if (test_and_set_bit(RQ_BUSY, &rqstp->rq_flags)) + continue; + + rcu_read_unlock(); + WRITE_ONCE(rqstp->rq_qtime, ktime_get()); + wake_up_process(rqstp->rq_task); + percpu_counter_inc(&pool->sp_threads_woken); + return rqstp; + } + rcu_read_unlock(); + return NULL; +} + /* * Choose a pool in which to create a new thread, for svc_set_num_threads */ diff --git a/net/sunrpc/svc_xprt.c b/net/sunrpc/svc_xprt.c index 62c7919ea610..89302bf09b77 100644 --- a/net/sunrpc/svc_xprt.c +++ b/net/sunrpc/svc_xprt.c @@ -455,8 +455,8 @@ static bool svc_xprt_ready(struct svc_xprt *xprt) */ void svc_xprt_enqueue(struct svc_xprt *xprt) { + struct svc_rqst *rqstp; struct svc_pool *pool; - struct svc_rqst *rqstp = NULL; if (!svc_xprt_ready(xprt)) return; @@ -476,20 +476,10 @@ void svc_xprt_enqueue(struct svc_xprt *xprt) list_add_tail(&xprt->xpt_ready, &pool->sp_sockets); spin_unlock_bh(&pool->sp_lock); - /* find a thread for this xprt */ - rcu_read_lock(); - list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { - if (test_and_set_bit(RQ_BUSY, &rqstp->rq_flags)) - continue; - percpu_counter_inc(&pool->sp_threads_woken); - rqstp->rq_qtime = ktime_get(); - wake_up_process(rqstp->rq_task); - goto out_unlock; - } - set_bit(SP_CONGESTED, &pool->sp_flags); - rqstp = NULL; -out_unlock: - rcu_read_unlock(); + rqstp = svc_pool_wake_idle_thread(pool); + if (!rqstp) + set_bit(SP_CONGESTED, &pool->sp_flags); + trace_svc_xprt_enqueue(xprt, rqstp); } EXPORT_SYMBOL_GPL(svc_xprt_enqueue); @@ -581,7 +571,10 @@ static void svc_xprt_release(struct svc_rqst *rqstp) svc_xprt_put(xprt); } -/* +/** + * svc_wake_up - Wake up a service thread for non-transport work + * @serv: RPC service + * * Some svc_serv's will have occasional work to do, even when a xprt is not * waiting to be serviced. This function is there to "kick" a task in one of * those services so that it can wake up and do that work. Note that we only @@ -590,27 +583,18 @@ static void svc_xprt_release(struct svc_rqst *rqstp) */ void svc_wake_up(struct svc_serv *serv) { + struct svc_pool *pool = &serv->sv_pools[0]; struct svc_rqst *rqstp; - struct svc_pool *pool; - pool = &serv->sv_pools[0]; - - rcu_read_lock(); - list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) { - /* skip any that aren't queued */ - if (test_bit(RQ_BUSY, &rqstp->rq_flags)) - continue; - rcu_read_unlock(); - wake_up_process(rqstp->rq_task); - trace_svc_wake_up(rqstp->rq_task->pid); + rqstp = svc_pool_wake_idle_thread(pool); + if (!rqstp) { + set_bit(SP_TASK_PENDING, &pool->sp_flags); + smp_wmb(); + trace_svc_wake_up(0); return; } - rcu_read_unlock(); - /* No free entries available */ - set_bit(SP_TASK_PENDING, &pool->sp_flags); - smp_wmb(); - trace_svc_wake_up(0); + trace_svc_wake_up(rqstp->rq_task->pid); } EXPORT_SYMBOL_GPL(svc_wake_up);