Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp520781lqi; Thu, 7 Mar 2024 04:28:52 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV8qxWPaNVO2espnUkx6Ku2i6VJMnHhMdi94swISillMBFTMkaDosNDIA3Sgw48V5N27pzD/DSuQlZvszhFC/I1/sJpGnWif8yT5mmZjw== X-Google-Smtp-Source: AGHT+IFtF7SWrUktRV9QffFR+wyvItMZJKqSbb4x7JPGknZJn3R3uGvChcyOMgtLisJfuQ+n0NRa X-Received: by 2002:a05:6a20:e68c:b0:1a0:e189:8568 with SMTP id mz12-20020a056a20e68c00b001a0e1898568mr1959814pzb.15.1709814532333; Thu, 07 Mar 2024 04:28:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709814532; cv=pass; d=google.com; s=arc-20160816; b=kL2zacEWbpA3XytQozHppiruZTW1t48x68hpWtYU8TFBBlXBEjKITJs1xN0+9m7+Z7 8cqYzqXE2PIPsWh9NSVwZXUI+7nhFU7OKPtM12lLuhfsj60h2Ip1uOHf+h4DmP7E8LPO PRKGVuKrgC4qhFX9zfZdz0A8/fwp2pNEQx7zIr0jnARCmn9wTHR97bAgNqcDHxtVrzMM 7GamNh32vC7YO0AHbElVSjhxAfL/Ln+Nw5WCP9Ii80j/ZoYOid5TNF0KmjaGp5RdY17p PRmN2yp2CbRVbV4mO3mMQO6+LON2Y02iF6as/h5vZuV5pWKCbMDUcVMIh0on8SehuqCr jWMA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:date:from:dkim-signature; bh=gz8Cbh194aVw03XqXK43A9BunRKEWBlJAiUGr2SJ1Hg=; fh=5zQlmZbF34kqIFPrmJyGj8RE7nMYljPs0Cx2IsIzJ6Y=; b=q79wkfycrQd+GdCoRUjNRz14paUxgeL0mk6dtChc1C+iJ1H7AhUV9OVamwaSsOGLdz 15YUEKBG83iyEZGqYGwdZl4Iz6ndU7ZSz+4vnlGn6FBXV1Jj+8V89QXhf8Ayj0KO9lDd NP58ZiYaXd5r2SjtPQIs45MFypP/z4Q9Jzi2wSd5c0j2I1y3UJHEeK9umyRWC1sYGg0r O5PKWS3pRfH3HaUwXJ+pC7dk2tIDpqTtmVIfKqDIV73C39BWbEFdMlNQJ19L4ENVHtRA SusLsYbqi5MisURz1BE+WbZJgzWxw2ez71V60nXPBzoq1/0gYeXBBi8LZ1o8tuiBivsV k4Xw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="TCE/VXET"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-95532-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95532-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id z62-20020a636541000000b005e49bf549b2si14008141pgb.523.2024.03.07.04.28.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 04:28:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-95532-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="TCE/VXET"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-95532-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-95532-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5FCA52854E8 for ; Thu, 7 Mar 2024 12:28:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9CA9912AAD8; Thu, 7 Mar 2024 12:28:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TCE/VXET" Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF1868563D; Thu, 7 Mar 2024 12:28:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709814488; cv=none; b=mBXN/ijKE3q4rMM7Rc98NC4XzvuVNgzMLPbVkLuXfo249IaoQ31NLR+3pCTCkvsWjy5mZyAe0nrQH0Be97/++CMbELBIyJaSBEhdNc7hjYpStLzD4VXQLdaezLaHB8gKqCM2zJioshDgg1uHSDMcC8mVecAn7kaNNgdbksZiSn0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709814488; c=relaxed/simple; bh=EqkDsKVlKPs/822PR4HaJMgwoDO9BoVj0fvPRRDp83U=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=nrR+PZPGCUnbiDLIs8qwdxBP7ggFP3cop2K7ax7Vw9iSGdtjV5U1I8e/mUrtRjkJPMQl/gTsMk+RGmFEstk3UEQPSeGxQDZxZ0N8gczOWM5R1Q/7l4WPK7ObkuHl++IXBLqp48a7dYuLuvwu2EuSnpdfsbbqogLCrQS7PikmWCQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TCE/VXET; arc=none smtp.client-ip=209.85.167.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-513181719easo631869e87.3; Thu, 07 Mar 2024 04:28:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709814485; x=1710419285; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=gz8Cbh194aVw03XqXK43A9BunRKEWBlJAiUGr2SJ1Hg=; b=TCE/VXETLQ3mteN8pNmfVsr7TyG3Khl09uU6xh1Mpl5R0uNampI8iSlZBcxpuzCbVu TKyHTlgsJwqjp/GsKz/pSmyMw12/xHqX5l1Q/RAFk28ZrCfT0/xVf6Oi/LAo/JCytJ3m GtcTNI3aDHUTblOqJ64egc1po0jiLc5yOQwS71RNdEin9zxaFZMa2ffpDRoNp6kbOdmY tYKFLTQC8UrMY6JkGfRVPK8dLcMgcT1rN1FgNMwPR54lQKGNU5k3yz9DH+3zKHO6Up7L XPfy3QW97k3+ofrkvBiVsOtJVlPDo4QdfA0t5UtHU6QmKKitXVtl9BrLTqaQRV9QDKcn dQCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709814485; x=1710419285; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gz8Cbh194aVw03XqXK43A9BunRKEWBlJAiUGr2SJ1Hg=; b=krX4ReVllHh4luLKO7dL6YHOROAT64ujEprVKfKkKNAIcWLO0SrGTSUGCmN65OVWnV fEex23J+Lee6y6BdtfkbDNAFgP9vt5OCT7jAVIYhg9CWwG86MHttZLkVpBgaFBwqHZG1 JlnGdrQKGy+5SHMf6W++ryNa7chI6dTtsgbzxsAKVZiMPcoYn+nFZibSHqrXmFaaToga IJZyotuTsErFkq5JKtI2mF1gYiGgagI7BDZkRl+VAWP8HNBP/5RB6Dm/HwyVcojqjFyu NgjSWb0IesOk1h7G19NpK2ps/XiT4+0/TPqOypdNOK1gZiXb0pFWW1ik1SuK3mPws6eG f3bg== X-Forwarded-Encrypted: i=1; AJvYcCUjWGdzY0CgrHVvRp5YJQzFGNGQaqanbLXK1zd64rZqH0YZD4uEOBFstHtF8xcmWiSI2IqKDSxOBaJ73OqWhfWADkpi3GNNJMAMzJWXWe3y/q+twOa8DaRllbPZ6mk+tmF3 X-Gm-Message-State: AOJu0YwGqFSlnpA8bynLG5MwqNRxjrfT9qRtl3fSxYcwBhK9VhFbghp3 07Lwe6zujzJ2dmXlJx638A7+4ToQmvbs9M41HupqGb22uH9gFLxA X-Received: by 2002:ac2:5f08:0:b0:513:1e47:bc5b with SMTP id 8-20020ac25f08000000b005131e47bc5bmr1311362lfq.25.1709814484392; Thu, 07 Mar 2024 04:28:04 -0800 (PST) Received: from pc636 (host-90-235-19-15.mobileonline.telia.com. [90.235.19.15]) by smtp.gmail.com with ESMTPSA id a22-20020a19f816000000b00512fb973e21sm3027187lff.294.2024.03.07.04.28.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 04:28:04 -0800 (PST) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Thu, 7 Mar 2024 13:28:01 +0100 To: Joel Fernandes Cc: "Uladzislau Rezki (Sony)" , "Paul E . McKenney" , RCU , Neeraj upadhyay , Boqun Feng , Hillf Danton , LKML , Oleksiy Avramchenko , Frederic Weisbecker Subject: Re: [PATCH 1/2] rcu: Do not release a wait-head from a GP kthread Message-ID: References: <20240305195720.42687-1-urezki@gmail.com> <14ab5bd4-d7b8-4233-9389-f21884986671@joelfernandes.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <14ab5bd4-d7b8-4233-9389-f21884986671@joelfernandes.org> On Thu, Mar 07, 2024 at 01:21:50AM -0500, Joel Fernandes wrote: > > > On 3/6/2024 5:31 PM, Joel Fernandes wrote: > > > > > > On 3/5/2024 2:57 PM, Uladzislau Rezki (Sony) wrote: > >> Fix a below race by not releasing a wait-head from the > >> GP-kthread as it can lead for reusing it whereas a worker > >> can still access it thus execute newly added callbacks too > >> early. > >> > >> CPU 0 CPU 1 > >> ----- ----- > >> > >> // wait_tail == HEAD1 > >> rcu_sr_normal_gp_cleanup() { > >> // has passed SR_MAX_USERS_WAKE_FROM_GP > >> wait_tail->next = next; > >> // done_tail = HEAD1 > >> smp_store_release(&rcu_state.srs_done_tail, wait_tail); > >> queue_work() { > >> test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work) > >> __queue_work() > >> } > >> } > >> > >> set_work_pool_and_clear_pending() > >> rcu_sr_normal_gp_cleanup_work() { > >> // new GP, wait_tail == HEAD2 > >> rcu_sr_normal_gp_cleanup() { > >> // executes all completion, but stop at HEAD1 > >> wait_tail->next = HEAD1; > >> // done_tail = HEAD2 > >> smp_store_release(&rcu_state.srs_done_tail, wait_tail); > >> queue_work() { > >> test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work) > >> __queue_work() > >> } > >> } > >> // done = HEAD2 > >> done = smp_load_acquire(&rcu_state.srs_done_tail); > >> // head = HEAD1 > >> head = done->next; > >> done->next = NULL; > >> llist_for_each_safe() { > >> // completes all callbacks, release HEAD1 > >> } > >> } > >> // Process second queue > >> set_work_pool_and_clear_pending() > >> rcu_sr_normal_gp_cleanup_work() { > >> // done = HEAD2 > >> done = smp_load_acquire(&rcu_state.srs_done_tail); > >> > >> // new GP, wait_tail == HEAD3 > >> rcu_sr_normal_gp_cleanup() { > >> // Finds HEAD2 with ->next == NULL at the end > >> rcu_sr_put_wait_head(HEAD2) > >> ... > >> > >> // A few more GPs later > >> rcu_sr_normal_gp_init() { > >> HEAD2 = rcu_sr_get_wait_head(); > >> llist_add(HEAD2, &rcu_state.srs_next); > >> // head == rcu_state.srs_next > >> head = done->next; > >> done->next = NULL; > >> llist_for_each_safe() { > >> // EXECUTE CALLBACKS TOO EARLY!!! > >> } > >> } > >> > >> Reported-by: Frederic Weisbecker > >> Fixes: 05a10b921000 ("rcu: Support direct wake-up of synchronize_rcu() users") > >> Signed-off-by: Uladzislau Rezki (Sony) > >> --- > >> kernel/rcu/tree.c | 22 ++++++++-------------- > >> 1 file changed, 8 insertions(+), 14 deletions(-) > >> > >> diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c > >> index 31f3a61f9c38..475647620b12 100644 > >> --- a/kernel/rcu/tree.c > >> +++ b/kernel/rcu/tree.c > >> @@ -1656,21 +1656,11 @@ static void rcu_sr_normal_gp_cleanup(void) > >> WARN_ON_ONCE(!rcu_sr_is_wait_head(wait_tail)); > >> > >> /* > >> - * Process (a) and (d) cases. See an illustration. Apart of > >> - * that it handles the scenario when all clients are done, > >> - * wait-head is released if last. The worker is not kicked. > >> + * Process (a) and (d) cases. See an illustration. > >> */ > >> llist_for_each_safe(rcu, next, wait_tail->next) { > >> - if (rcu_sr_is_wait_head(rcu)) { > >> - if (!rcu->next) { > >> - rcu_sr_put_wait_head(rcu); > >> - wait_tail->next = NULL; > >> - } else { > >> - wait_tail->next = rcu; > >> - } > >> - > >> + if (rcu_sr_is_wait_head(rcu)) > >> break; > >> - } > >> > >> rcu_sr_normal_complete(rcu); > >> // It can be last, update a next on this step. > >> @@ -1684,8 +1674,12 @@ static void rcu_sr_normal_gp_cleanup(void) > >> smp_store_release(&rcu_state.srs_done_tail, wait_tail); > >> ASSERT_EXCLUSIVE_WRITER(rcu_state.srs_done_tail); > >> > >> - if (wait_tail->next) > >> - queue_work(system_highpri_wq, &rcu_state.srs_cleanup_work); > >> + /* > >> + * We schedule a work in order to perform a final processing > >> + * of outstanding users(if still left) and releasing wait-heads > >> + * added by rcu_sr_normal_gp_init() call. > >> + */ > >> + queue_work(system_highpri_wq, &rcu_state.srs_cleanup_work); > >> } > > One question, why do you need to queue_work() if wait_tail->next == NULL? > > AFAICS, at this stage if wait_tail->next == NULL, you are in CASE f. so the last > remaining HEAD stays? (And llist_for_each_safe() in > rcu_sr_normal_gp_cleanup_work becomes a NOOP). > > Could be something like this, but maybe I missed something: > > @@ -1672,7 +1674,7 @@ static void rcu_sr_normal_gp_cleanup_work(struct > work_struct *work) > */ > static void rcu_sr_normal_gp_cleanup(void) > { > - struct llist_node *wait_tail, *next, *rcu; > + struct llist_node *wait_tail, *next = NULL, *rcu = NULL; > int done = 0; > > wait_tail = rcu_state.srs_wait_tail; > @@ -1707,7 +1709,8 @@ static void rcu_sr_normal_gp_cleanup(void) > * of outstanding users(if still left) and releasing wait-heads > * added by rcu_sr_normal_gp_init() call. > */ > - queue_work(system_highpri_wq, &rcu_state.srs_cleanup_work); > + if (rcu) > + queue_work(system_highpri_wq, &rcu_state.srs_cleanup_work); > } > > /* > Unneeded queueing happens only first time after boot. After one cycle we always need to queue the work to do a previous cleanups. if (wait_head->next) queue_the_work(); wait_head->next is always not NULL except first cycle after boot. -- Uladzislau Rezki