Received: by 2002:a05:7412:f589:b0:e2:908c:2ebd with SMTP id eh9csp152403rdb; Tue, 31 Oct 2023 03:52:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGkUe1dpB5YqhfkY5tAijSF9n/kgt3Lqh+Rp/0lJyCeMaxeR42WoXRLoeJLk8Ipg3CfJe4F X-Received: by 2002:a05:6358:3406:b0:168:d2b5:db9 with SMTP id h6-20020a056358340600b00168d2b50db9mr13785921rwd.6.1698749575131; Tue, 31 Oct 2023 03:52:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698749575; cv=none; d=google.com; s=arc-20160816; b=rJf2O3+DUy7bDo3zZpCuQG1yQ7trhpbgEKJEnOcyK8/2NbD/wBelK8U9Ed7aP5BI50 h/Lu6YvZTwBKcwnFfTD08O4J4ljV2UBlTBfVZ3E5q0l1gxYlVJDzoVgDnSS3HlH7LRou NQOGtAY6M8YFxcqEluizfeFWtnAn0NGd9i0f1DD9HBUzk+6PdNf1UIIFH8rG560B1dpX whVH8QIlVp3vJPa37/iGut1OUEWGSKLl8ne5/qmGQ2RNHIvE2W/3YOMcy8mJHOAPZ9Ui ZyLFZvtLkvieCuMysD2Z86PBpKsDY3OVGgdDeidH/MFd/iXwdmoOvJZyMVqzqJax4g0j UBkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=fyNByB//Dsp4QlM2SSXrSjx7vSiI2IeXzVAzjc4Jq5Q=; fh=vnyIE8rBwgiYZ0bD5oFUcpjL3n+Yr5BSXHkf8StvbTI=; b=DcZEB3dQy1sWmUxxqI02A7IxaWrLfB0iHHk8dYf2lKD7y13/oXkME85HGGvGu/XdoS 4XaaYTBGENwiitHHgl0VTiQe1t2pFUyg724ycNMlmCz905Z19jEnZrtBI5yaD16mCoU+ a8z26b7nlCDaLwzEGRIlXGY7fslF/jE4cIO3FDwi0spuOHm7KX5EBQzZHc5DAzaq7uIA ke2y1SzFTuWoDfwhwEAD499EzdwyMDViJLIXCk6xsjYyHbUfWko3LPqWfEF+S0tCMw8D a7Yqmymqfq33ORLpGWgJUpXX5Bn0urZ5miHNEDhd5vQ70JOoA7/vI0n8V2uvTtYtUoYO bMYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jAjmyDoc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id bg11-20020a056a02010b00b005b9305bfbd6si895824pgb.302.2023.10.31.03.52.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 03:52:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jAjmyDoc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A6E448031AD8; Tue, 31 Oct 2023 03:52:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343984AbjJaKwp (ORCPT + 99 others); Tue, 31 Oct 2023 06:52:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230370AbjJaKwo (ORCPT ); Tue, 31 Oct 2023 06:52:44 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36BEDA1 for ; Tue, 31 Oct 2023 03:52:42 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1293DC433C7; Tue, 31 Oct 2023 10:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698749561; bh=QxPlull2BB81b83iUvmVPFPs6Ac/bnfOsodU2R8tfYQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jAjmyDoctca301AH94jcqEQNYV94SCsDaMdhrat+N8Zp4heS2yTzo7ygvXSV5DzeD WRHnI7D/HVb00nv37CLilasjkWpEdsd4ROilAvuxmd328fkHD9I7oTEZe/0/bviMOc IComnX5UbWyJuk1KioYViUwelvoJfmxiSviNfWQRNoJSiVWYqHLlD7AkBUnpwEo19i /5LfbK4B6Pf2H28dd/JcX3YTl/4/5jY5WrNbpDk6/6ItIi/eY5Hy4rrwFz7uCzyZE2 Ag5X95On54Bd1HwSglV38gSqrMjPaps2M6IqeJHRRS3fr7GHwQueJXZoIAdVE9+hlW fmG44iAoysiTw== Date: Tue, 31 Oct 2023 11:52:38 +0100 From: Frederic Weisbecker To: Peter Zijlstra Cc: Waiman Long , Ingo Molnar , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , linux-kernel@vger.kernel.org, Phil Auld , kernel test robot , aubrey.li@linux.intel.com, yu.c.chen@intel.com, paulmck@kernel.org, quic_neeraju@quicinc.com, joel@joelfernandes.org, josh@joshtriplett.org, boqun.feng@gmail.com, mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com, qiang.zhang1211@gmail.com Subject: Re: [PATCH] sched: Don't call any kfree*() API in do_set_cpus_allowed() Message-ID: References: <20231031001418.274187-1-longman@redhat.com> <20231031085308.GB35651@noisy.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231031085308.GB35651@noisy.programming.kicks-ass.net> X-Spam-Status: No, score=-1.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 31 Oct 2023 03:52:52 -0700 (PDT) On Tue, Oct 31, 2023 at 09:53:08AM +0100, Peter Zijlstra wrote: > On Mon, Oct 30, 2023 at 08:14:18PM -0400, Waiman Long wrote: > > Commit 851a723e45d1 ("sched: Always clear user_cpus_ptr in > > do_set_cpus_allowed()") added a kfree() call to free any user > > provided affinity mask, if present. It was changed later to use > > kfree_rcu() in commit 9a5418bc48ba ("sched/core: Use kfree_rcu() > > in do_set_cpus_allowed()") to avoid a circular locking dependency > > problem. > > > > It turns out that even kfree_rcu() isn't safe for avoiding > > circular locking problem. As reported by kernel test robot, > > the following circular locking dependency still exists: > > > > &rdp->nocb_lock --> rcu_node_0 --> &rq->__lock > > > > So no kfree*() API can be used in do_set_cpus_allowed(). To prevent > > memory leakage, the unused user provided affinity mask is now saved in a > > lockless list to be reused later by subsequent sched_setaffinity() calls. > > > > Without kfree_rcu(), the internal cpumask_rcuhead union can be removed > > too as a lockless list entry only holds a single pointer. > > > > Fixes: 851a723e45d1 ("sched: Always clear user_cpus_ptr in do_set_cpus_allowed()") > > Bah, or we fix RCU... Paul, how insane is the below? Makes sense. We can't remove &rdp->nocb_lock --> rcu_node_0 but we can (and should) indeed remove rcu_node_0 --> &rq->__lock Just a detail below: > @@ -2284,10 +2289,13 @@ static void force_qs_rnp(int (*f)(struct rcu_data *rdp)) > } > for_each_leaf_node_cpu_mask(rnp, cpu, rnp->qsmask) { > rdp = per_cpu_ptr(&rcu_data, cpu); > - if (f(rdp)) { > + ret = f(rdp); > + if (ret > 0) { > mask |= rdp->grpmask; > rcu_disable_urgency_upon_qs(rdp); > } > + if (ret < 0) > + rsmask |= 1UL << (cpu - rnp->grplo); I guess this can be simplified with rsmask |= rdp->grpmask; Thanks. > } > if (mask != 0) { > /* Idle/offline CPUs, report (releases rnp->lock). */ > @@ -2296,6 +2304,9 @@ static void force_qs_rnp(int (*f)(struct rcu_data *rdp)) > /* Nothing to do here, so just drop the lock. */ > raw_spin_unlock_irqrestore_rcu_node(rnp, flags); > } > + > + for_each_leaf_node_cpu_mask(rnp, cpu, rsmask) > + resched_cpu(cpu); > } > } >