From: Boqun Feng <boqun.feng@gmail.com>
To: paulmck@kernel.org
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Vlastimil Babka <vbabka@suse.cz>, Marco Elver <elver@google.com>,
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 <tglx@linutronix.de>,
Peter Zijlstra <peterz@infradead.org>,
Boqun Feng <boqun.feng@gmail.com>
Subject: [PATCH] scftorture: Use workqueue to free scf_check
Date: Fri, 1 Nov 2024 12:54:38 -0700 [thread overview]
Message-ID: <20241101195438.1658633-1-boqun.feng@gmail.com> (raw)
In-Reply-To: <ZyUxBr5Umbc9odcH@boqun-archlinux>
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" <paulmck@kernel.org>
Link: https://lore.kernel.org/lkml/41619255-cdc2-4573-a360-7794fc3614f7@paulmck-laptop/
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
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
next prev parent reply other threads:[~2024-11-01 19:56 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-30 21:05 [BUG] -next lockdep invalid wait context Paul E. McKenney
2024-10-30 21:48 ` Vlastimil Babka
2024-10-30 22:34 ` Marco Elver
2024-10-30 23:04 ` Boqun Feng
2024-10-30 23:10 ` Paul E. McKenney
2024-10-31 7:21 ` Sebastian Andrzej Siewior
2024-10-31 7:35 ` Vlastimil Babka
2024-10-31 7:55 ` Sebastian Andrzej Siewior
2024-10-31 8:18 ` Vlastimil Babka
2024-11-01 17:14 ` Paul E. McKenney
2024-10-31 17:50 ` Paul E. McKenney
2024-11-01 19:50 ` Boqun Feng
2024-11-01 19:54 ` Boqun Feng [this message]
2024-11-01 23:35 ` [PATCH] scftorture: Use workqueue to free scf_check Paul E. McKenney
2024-11-03 3:35 ` Boqun Feng
2024-11-03 15:03 ` Paul E. McKenney
2024-11-04 10:50 ` [PATCH 1/2] scftorture: Move memory allocation outside of preempt_disable region Sebastian Andrzej Siewior
2024-11-04 10:50 ` [PATCH 2/2] scftorture: Use a lock-less list to free memory Sebastian Andrzej Siewior
2024-11-05 1:00 ` Boqun Feng
2024-11-07 11:21 ` Sebastian Andrzej Siewior
2024-11-07 14:08 ` Paul E. McKenney
2024-11-07 14:43 ` Sebastian Andrzej Siewior
2024-11-07 14:59 ` Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241101195438.1658633-1-boqun.feng@gmail.com \
--to=boqun.feng@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=bigeasy@linutronix.de \
--cc=cl@linux.com \
--cc=elver@google.com \
--cc=iamjoonsoo.kim@lge.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-next@vger.kernel.org \
--cc=longman@redhat.com \
--cc=paulmck@kernel.org \
--cc=penberg@kernel.org \
--cc=peterz@infradead.org \
--cc=rientjes@google.com \
--cc=sfr@canb.auug.org.au \
--cc=tglx@linutronix.de \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox