Received: by 2002:a05:6500:1b41:b0:1fb:d597:ff75 with SMTP id cz1csp406723lqb; Tue, 4 Jun 2024 15:24:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWCD11H/4dczaod5646RabM97iH9p0KjAagNZ8Wk/h8hLGe7myhC6PB9q/UjYxplTl36+xYcOEYlfMbf2DxeoWv7Ba/i6qOV4sh1kvjQg== X-Google-Smtp-Source: AGHT+IFLZhYArePJqoNoN8Mq6fM/2t3VnF5C0mrReOgwvVWyPR7bwN7+IYOsYE2OZs5KXrp2X9VQ X-Received: by 2002:a05:6358:719:b0:19c:5c1c:11a6 with SMTP id e5c5f4694b2df-19c6c8fb406mr88024655d.26.1717539859618; Tue, 04 Jun 2024 15:24:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717539859; cv=pass; d=google.com; s=arc-20160816; b=rOjXB428py2BRnZDtbJA8On0aiKQYdeXd17iexHoTbQjqE0ZoqkaL4hAf0wvDycgpD 3q46v53IVsgIkJ5kmhG8xtRXBjyjxE1YSXZtwUrSxfFUSa0nRTCneZTtmd7fiAxa+11F x5aHgzFh03F5hvszrFFx7t3YwUOWY5zuh/dLN/njWz9pwZobuGc7uI2fTOe0rH5l5mxZ k2z74SP8Dh3l5O7ggSrMhFP3vUjtLD6eGkk+GkxCknXFsJb1Z6QjjSjWiSuFcPM17P7u ynur48MFkHu/J6Yk7eOynEZyVBrBq41aYRRA7qeZRd4gXYYTdnMS1T91sKItuM1bo25Z dgAw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dkQdNEajsVNuqDqxMExOAyvwZSeXo3aFCgs0LJI6Hd0=; fh=AOuB0KM7btdIiaaeZ3AU7v2rkRKW2Y3Ihg2LYZqjmYM=; b=a0M1w3JFAm7TfzojimaAfzqm8KShf7wocp5gfCUcQmuqOIQepFaR3zthOHCLXuSluY eMBlwB1JBiJfIPq0DdoUAnDOwmcfcWjChTLU2YCz613PuThx1m5Sm0HSgThzyczU6pOQ N6HpVgAJVHqparWKwPIOYU/XS28vnQxq76ScVqmgLtdPwKKbqebEgfxVRjTM3O285REe fXP+H7kiUmqCs4XpFR+rO2FZXKX6HRoNKtIsnadBtJHVRbVTX9OYwar6xPCK8rhxLo/n tYEXSjbFiiiWoVe7F5PTIWS/lYMOkBw5/+qtaIGmk2UmrT0C9bttu+4rq5kEVrg/wFM2 UpEQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Kt3FpNKb; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201461-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201461-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6ae4a732e42si121925676d6.49.2024.06.04.15.24.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 15:24:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-201461-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Kt3FpNKb; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-201461-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201461-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 568801C21E6B for ; Tue, 4 Jun 2024 22:24:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B82DE14D2B2; Tue, 4 Jun 2024 22:23:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Kt3FpNKb" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8844A5F; Tue, 4 Jun 2024 22:23:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717539836; cv=none; b=rdxNTIPh4Bi1c0CFYcmj3phHAk6yyr2+a7FB0DjHPZEjefLqh+wghUW99PT0ld3m+C/98crF3ivpTrgpoTESaK4DPA+K81t20SJNLgkE0V0Q0esECF3rpeCT+l3Z3wjQLhCGci3t+maOq0XGZBHDPYkOuPNSMtMIvGavWWM2aB4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717539836; c=relaxed/simple; bh=RUq/x40RoZ1+Q28PHuHwZWpj7Bz1teiZmeOv5T+R3o0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KDSUad7wwcIyF26/bHgLMEGqPMRXDZ8g4HZxFVyaQjRdPbp5JlQsvaf3o2Be9pp2PcdjhPYBNR4q49Of3xJJq8GT9yQkpXmxH2tZQSulUWHz7MCqwWBxlxZKaJx//S06WT6ky84j0M3wT3WTnQBtSpr9jGMDvAhHylUAhh+JUdo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Kt3FpNKb; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3980C3277B; Tue, 4 Jun 2024 22:23:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717539836; bh=RUq/x40RoZ1+Q28PHuHwZWpj7Bz1teiZmeOv5T+R3o0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Kt3FpNKbTghXYwbkm+LzOtM+NWHFQdZhQcDh65ABKodYDBCJ9tOwN2z7AHbgYUgYi WZv5/SAjdvqw1xhhnzcVMLiFhj203zOi71+yPHpjJakaYR8mHfYFpqGE+QTZ7B8YkA 0Dx467tCwu8w1vNNY+sK7o6J+aA38ub5gT7xW9rLBK5WVXWYOBEi1SD8iNyURotfsA T+mX0rlyQb3Fk4MwuwrkrIlAO/+51MbITrNmrU8INEVOke6npkJWY0hqTZzZBWW5VQ dJPx4MMSvGRNIbkzsdozlziGbRLkQGbZtIpWO7SdfUgMTFyq2jXC94waG7/sEcx/2O vmNS3HSV9A35A== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 5CADECE3F0F; Tue, 4 Jun 2024 15:23:56 -0700 (PDT) From: "Paul E. McKenney" To: rcu@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com, rostedt@goodmis.org, Neeraj Upadhyay , "Paul E . McKenney" Subject: [PATCH rcu 2/9] rcu: Reduce synchronize_rcu() delays when all wait heads are in use Date: Tue, 4 Jun 2024 15:23:48 -0700 Message-Id: <20240604222355.2370768-2-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <657595c8-e86c-4594-a5b1-3c64a8275607@paulmck-laptop> References: <657595c8-e86c-4594-a5b1-3c64a8275607@paulmck-laptop> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Neeraj Upadhyay When all wait heads are in use, which can happen when rcu_sr_normal_gp_cleanup_work()'s callback processing is slow, any new synchronize_rcu() user's rcu_synchronize node's processing is deferred to future GP periods. This can result in long list of synchronize_rcu() invocations waiting for full grace period processing, which can delay freeing of memory. Mitigate this problem by using first node in the list as wait tail when all wait heads are in use. While methods to speed up callback processing would be needed to recover from this situation, allowing new nodes to complete their grace period can help prevent delays due to a fixed number of wait head nodes. Signed-off-by: Neeraj Upadhyay Signed-off-by: Paul E. McKenney --- kernel/rcu/tree.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 28c7031711a3f..6ba36d9c09bde 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -1463,14 +1463,11 @@ static void rcu_poll_gp_seq_end_unlocked(unsigned long *snap) * for this new grace period. Given that there are a fixed * number of wait nodes, if all wait nodes are in use * (which can happen when kworker callback processing - * is delayed) and additional grace period is requested. - * This means, a system is slow in processing callbacks. - * - * TODO: If a slow processing is detected, a first node - * in the llist should be used as a wait-tail for this - * grace period, therefore users which should wait due - * to a slow process are handled by _this_ grace period - * and not next. + * is delayed), first node in the llist is used as wait + * tail for this grace period. This means, the first node + * has to go through additional grace periods before it is + * part of the wait callbacks. This should be ok, as + * the system is slow in processing callbacks anyway. * * Below is an illustration of how the done and wait * tail pointers move from one set of rcu_synchronize nodes @@ -1639,7 +1636,6 @@ static void rcu_sr_normal_gp_cleanup_work(struct work_struct *work) if (!done) return; - WARN_ON_ONCE(!rcu_sr_is_wait_head(done)); head = done->next; done->next = NULL; @@ -1676,13 +1672,21 @@ static void rcu_sr_normal_gp_cleanup(void) rcu_state.srs_wait_tail = NULL; ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_wait_tail); - WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail)); /* * Process (a) and (d) cases. See an illustration. */ llist_for_each_safe(rcu, next, wait_tail->next) { - if (rcu_sr_is_wait_head(rcu)) + /* + * The done tail may reference a rcu_synchronize node. + * Stop at done tail, as using rcu_sr_normal_complete() + * from this path can result in use-after-free. This + * may occur if, following the wake-up of the synchronize_rcu() + * wait contexts and freeing up of node memory, + * rcu_sr_normal_gp_cleanup_work() accesses the done tail and + * its subsequent nodes. + */ + if (wait_tail->next == rcu_state.srs_done_tail) break; rcu_sr_normal_complete(rcu); @@ -1719,15 +1723,17 @@ static bool rcu_sr_normal_gp_init(void) return start_new_poll; wait_head = rcu_sr_get_wait_head(); - if (!wait_head) { - // Kick another GP to retry. + if (wait_head) { + /* Inject a wait-dummy-node. */ + llist_add(wait_head, &rcu_state.srs_next); + } else { + // Kick another GP for first node. start_new_poll = true; - return start_new_poll; + if (first == rcu_state.srs_done_tail) + return start_new_poll; + wait_head = first; } - /* Inject a wait-dummy-node. */ - llist_add(wait_head, &rcu_state.srs_next); - /* * A waiting list of rcu_synchronize nodes should be empty on * this step, since a GP-kthread, rcu_gp_init() -> gp_cleanup(), -- 2.40.1