From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5184CE6F093 for ; Fri, 1 Nov 2024 23:35:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C7F36B009B; Fri, 1 Nov 2024 19:35:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 677556B009E; Fri, 1 Nov 2024 19:35:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53E726B009F; Fri, 1 Nov 2024 19:35:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3631D6B009B for ; Fri, 1 Nov 2024 19:35:33 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9168C1610DC for ; Fri, 1 Nov 2024 23:35:32 +0000 (UTC) X-FDA: 82739134464.24.FFFC1B3 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf09.hostedemail.com (Postfix) with ESMTP id C171A140006 for ; Fri, 1 Nov 2024 23:35:10 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UixwKEY5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of "SRS0=Xeo+=R4=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org" designates 147.75.193.91 as permitted sender) smtp.mailfrom="SRS0=Xeo+=R4=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730504011; a=rsa-sha256; cv=none; b=l8EPJg9IiYzCX3ejINn+29MK029MKiiPDeX8agpbTeGET9a9tf+oG7syRzSVYi2C8Er3wQ bQ+PhAIOP4KSIVMNMoiI9r0S/bgKXJNxAbZaS/qp63lT8dxME4L3wRQI1FXGTEr/X98wKN dUuQwkiTrTeC7oynIcOY7peq06FgqXc= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UixwKEY5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of "SRS0=Xeo+=R4=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org" designates 147.75.193.91 as permitted sender) smtp.mailfrom="SRS0=Xeo+=R4=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730504010; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SCbdCxoBNvMNL36FTFydtOQ3LLCEfsyrMJMfCrZBvyc=; b=Slb/whepE1qro91W/YZTo80jBpURVOyaZfMHHfZOytiAmMff2fctQry9778WXbPFALk8rb giGv3EfwI7n25TcqbCycNMONh5jmAswENERyV01A7Y2TPKjbvVnzsZnTVgisIehrvmoBjv gNpQXJpKHPeh45x2zZlD8UkFaMKdQa0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 81093A446E3; Fri, 1 Nov 2024 23:33:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EB10C4CECD; Fri, 1 Nov 2024 23:35:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730504129; bh=38aB2N81jTBUvMMnlfX7wcBFPqsI4F0XA1yHucd9Isg=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=UixwKEY5hw0KOSn5J88cB4VClNVP6fHOD0IasQLd+vEQA9gkIXIC7cvMPOzsEAZfc jHxaLyAKK5QXhhwu3hPtCbDWAkUOn+MazX6x9k52hYmccICUpva8X5xoHS97OBORsI CD0GL7yfCca4lCalCoNczwL+NmNhTMHTggBgif8NN+MafPCZEmxW2Q5W+Vv0NKai0D 6U6w9nzXvb+PuZOG5437qJseL56jJ/O7THo38VvfH5+WZBujFwgNoQbEi9kvpEtaxg 5HrJVQNltF8bGySJa0/y6qLMt/ozaWB5nkqFUfU6wsBpw4tCY3VAXM/pQ71B0Jv8ga +CjY5ToKkgvwA== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id D3782CE0F74; Fri, 1 Nov 2024 16:35:28 -0700 (PDT) Date: Fri, 1 Nov 2024 16:35:28 -0700 From: "Paul E. McKenney" To: Boqun Feng Cc: Sebastian Andrzej Siewior , Vlastimil Babka , Marco Elver , linux-next@vger.kernel.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, sfr@canb.auug.org.au, longman@redhat.com, cl@linux.com, penberg@kernel.org, rientjes@google.com, iamjoonsoo.kim@lge.com, akpm@linux-foundation.org, Thomas Gleixner , Peter Zijlstra Subject: Re: [PATCH] scftorture: Use workqueue to free scf_check Message-ID: <37c2ad76-37d1-44da-9532-65d67e849bba@paulmck-laptop> Reply-To: paulmck@kernel.org References: <20241101195438.1658633-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241101195438.1658633-1-boqun.feng@gmail.com> X-Rspamd-Queue-Id: C171A140006 X-Stat-Signature: ojdr6tz6wdchzsau9bn7f8puubephgx1 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1730504110-12717 X-HE-Meta: U2FsdGVkX18HfvxxIKTI9vB0rfNnXAyQ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FRcSDr5LjqprKo1FRslR3yfH5lnbKTyE49xJS3Du6NpY5bCo061T9wfHmZDD3FoBt4KZy9Xgx9I6NO5LR5PfgNb7rqRLKH1YMW8rinAQKjcApPn/a4N7eD9q527qE7IlcHJ2bAVBRYgBukurzwqE8agdemncC3FmSI4PeKBNSICqy9yIVtWBDx+XNfiyuYaxUjHB7DITltk/vN5GaJkyZByLrRZiyI0i7QvIpC1y+FtscYn5cYTzmez/cCaRfEpL0ArnLSxYFMFzPPF1oX+teVn+sIO9Pyqv4ubqWS6p+OjZMO0dLZGpAj22+p78V8HQ9XFhijDUXkXbQmgSWpe4DLZGRznKhmfWm0XFeSGhyAQmePSAC1u/EVGnfA0jGFu4KjwNM/YS+k66zgFBSp3Gfpga0oN6ybMns36GUzyN7wwMl8Prijd1H9U0OP644lyLmkdKIgPhq6olT+9CKL1KDHQJYRQ2YFFSSWYAkOT7BUj+cD/FbBxSzbtXJGeoif0cpQGMIqXd5CwF+G3yEf0fe2bAOT8g02Xa4BhI4r8d80hs3JzQFvEJfqXYrErnZCdUQUEUiSJbdKbD77OEHWkWcsvUI062Mqhy6e4Rd1S31rwzgs0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Fri, Nov 01, 2024 at 12:54:38PM -0700, Boqun Feng wrote: > Paul reported an invalid wait context issue in scftorture catched by > lockdep, and the cause of the issue is because scf_handler() may call > kfree() to free the struct scf_check: > > static void scf_handler(void *scfc_in) > { > [...] > } else { > kfree(scfcp); > } > } > > (call chain anlysis from Marco Elver) > > This is problematic because smp_call_function() uses non-threaded > interrupt and kfree() may acquire a local_lock which is a sleepable lock > on RT. > > The general rule is: do not alloc or free memory in non-threaded > interrupt conntexts. > > A quick fix is to use workqueue to defer the kfree(). However, this is > OK only because scftorture is test code. In general the users of > interrupts should avoid giving interrupt handlers the ownership of > objects, that is, users should handle the lifetime of objects outside > and interrupt handlers should only hold references to objects. > > Reported-by: "Paul E. McKenney" > Link: https://lore.kernel.org/lkml/41619255-cdc2-4573-a360-7794fc3614f7@paulmck-laptop/ > Signed-off-by: Boqun Feng Thank you! I was worried that putting each kfree() into a separate workqueue handler would result in freeing not keeping up with allocation for asynchronous testing (for example, scftorture.weight_single=1), but it seems to be doing fine in early testing. So I have queued this in my -rcu tree for review and further testing. Thanx, Paul > --- > kernel/scftorture.c | 14 +++++++++++++- > 1 file changed, 13 insertions(+), 1 deletion(-) > > diff --git a/kernel/scftorture.c b/kernel/scftorture.c > index 44e83a646264..ab6dcc7c0116 100644 > --- a/kernel/scftorture.c > +++ b/kernel/scftorture.c > @@ -127,6 +127,7 @@ static unsigned long scf_sel_totweight; > > // Communicate between caller and handler. > struct scf_check { > + struct work_struct work; > bool scfc_in; > bool scfc_out; > int scfc_cpu; // -1 for not _single(). > @@ -252,6 +253,13 @@ static struct scf_selector *scf_sel_rand(struct torture_random_state *trsp) > return &scf_sel_array[0]; > } > > +static void kfree_scf_check_work(struct work_struct *w) > +{ > + struct scf_check *scfcp = container_of(w, struct scf_check, work); > + > + kfree(scfcp); > +} > + > // Update statistics and occasionally burn up mass quantities of CPU time, > // if told to do so via scftorture.longwait. Otherwise, occasionally burn > // a little bit. > @@ -296,7 +304,10 @@ static void scf_handler(void *scfc_in) > if (scfcp->scfc_rpc) > complete(&scfcp->scfc_completion); > } else { > - kfree(scfcp); > + // Cannot call kfree() directly, pass it to workqueue. It's OK > + // only because this is test code, avoid this in real world > + // usage. > + queue_work(system_wq, &scfcp->work); > } > } > > @@ -335,6 +346,7 @@ static void scftorture_invoke_one(struct scf_statistics *scfp, struct torture_ra > scfcp->scfc_wait = scfsp->scfs_wait; > scfcp->scfc_out = false; > scfcp->scfc_rpc = false; > + INIT_WORK(&scfcp->work, kfree_scf_check_work); > } > } > switch (scfsp->scfs_prim) { > -- > 2.45.2 >