All of lore.kernel.org
 help / color / mirror / Atom feed
* Question on KASAN calltrace record in RT
@ 2021-04-06  8:26 Zhang, Qiang
  2021-04-13 15:29 ` Dmitry Vyukov
  0 siblings, 1 reply; 13+ messages in thread
From: Zhang, Qiang @ 2021-04-06  8:26 UTC (permalink / raw)
  To: Andrew Halaney, andreyknvl, ryabinin.a.a, dvyukov, andreyknvl, akpm
  Cc: linux-kernel, kasan-dev

Hello everyone

In RT system,   after  Andrew test,   found the following calltrace ,
in KASAN, we record callstack through stack_depot_save(), in this function, may be call alloc_pages,  but in RT, the spin_lock replace with 
rt_mutex in alloc_pages(), if before call this function, the irq is disabled,
will trigger following calltrace.

maybe  add array[KASAN_STACK_DEPTH] in struct kasan_track to record callstack  in RT system.

Is there a better solution ?
Thanks
Qiang

BUG: sleeping function called from invalid context at kernel/locking/rtmutex.c:951
[   14.522262] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 640, name: mount
[   14.522304] Call Trace:
[   14.522306]  dump_stack+0x92/0xc1
[   14.522313]  ___might_sleep.cold.99+0x1b0/0x1ef
[   14.522319]  rt_spin_lock+0x3e/0xc0
[   14.522329]  local_lock_acquire+0x52/0x3c0
[   14.522332]  get_page_from_freelist+0x176c/0x3fd0
[   14.522543]  __alloc_pages_nodemask+0x28f/0x7f0
[   14.522559]  stack_depot_save+0x3a1/0x470
[   14.522564]  kasan_save_stack+0x2f/0x40
[   14.523575]  kasan_record_aux_stack+0xa3/0xb0
[   14.523580]  insert_work+0x48/0x340
[   14.523589]  __queue_work+0x430/0x1280
[   14.523595]  mod_delayed_work_on+0x98/0xf0
[   14.523607]  kblockd_mod_delayed_work_on+0x17/0x20
[   14.523611]  blk_mq_run_hw_queue+0x151/0x2b0
[   14.523620]  blk_mq_sched_insert_request+0x2ad/0x470
[   14.523633]  blk_mq_submit_bio+0xd2a/0x2330
[   14.523675]  submit_bio_noacct+0x8aa/0xfe0
[   14.523693]  submit_bio+0xf0/0x550
[   14.523714]  submit_bio_wait+0xfe/0x200
[   14.523724]  xfs_rw_bdev+0x370/0x480 [xfs]
[   14.523831]  xlog_do_io+0x155/0x320 [xfs]
[   14.524032]  xlog_bread+0x23/0xb0 [xfs]
[   14.524133]  xlog_find_head+0x131/0x8b0 [xfs]
[   14.524375]  xlog_find_tail+0xc8/0x7b0 [xfs]
[   14.524828]  xfs_log_mount+0x379/0x660 [xfs]
[   14.524927]  xfs_mountfs+0xc93/0x1af0 [xfs]
[   14.525424]  xfs_fs_fill_super+0x923/0x17f0 [xfs]
[   14.525522]  get_tree_bdev+0x404/0x680
[   14.525622]  vfs_get_tree+0x89/0x2d0
[   14.525628]  path_mount+0xeb2/0x19d0
[   14.525648]  do_mount+0xcb/0xf0
[   14.525665]  __x64_sys_mount+0x162/0x1b0
[   14.525670]  do_syscall_64+0x33/0x40
[   14.525674]  entry_SYSCALL_64_after_hwframe+0x44/0xae
[   14.525677] RIP: 0033:0x7fd6c15eaade

^ permalink raw reply	[flat|nested] 13+ messages in thread
* [patch] kasan: Make it RT aware
@ 2021-08-21  6:48 Mike Galbraith
  0 siblings, 0 replies; 13+ messages in thread
From: Mike Galbraith @ 2021-08-21  6:48 UTC (permalink / raw)
  To: linux-rt-users; +Cc: Sebastian Andrzej Siewior, Thomas Gleixner


Skip preallocation when not possible for RT, and move cache removal
from IPI, where freeing is not possible for RT, to synchronous work.

Signed-off-by: Mike Galbraith <efault@gmx.de>
---
 lib/stackdepot.c      |    4 ++--
 mm/kasan/quarantine.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

--- a/lib/stackdepot.c
+++ b/lib/stackdepot.c
@@ -265,7 +265,7 @@ depot_stack_handle_t stack_depot_save(un
 	struct page *page = NULL;
 	void *prealloc = NULL;
 	unsigned long flags;
-	u32 hash;
+	u32 hash, may_prealloc = !IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible();

 	if (unlikely(nr_entries == 0) || stack_depot_disable)
 		goto fast_exit;
@@ -291,7 +291,7 @@ depot_stack_handle_t stack_depot_save(un
 	 * The smp_load_acquire() here pairs with smp_store_release() to
 	 * |next_slab_inited| in depot_alloc_stack() and init_stack_slab().
 	 */
-	if (unlikely(!smp_load_acquire(&next_slab_inited))) {
+	if (unlikely(!smp_load_acquire(&next_slab_inited) && may_prealloc)) {
 		/*
 		 * Zero out zone modifiers, as we don't have specific zone
 		 * requirements. Keep the flags related to allocation in atomic
--- a/mm/kasan/quarantine.c
+++ b/mm/kasan/quarantine.c
@@ -19,6 +19,9 @@
 #include <linux/srcu.h>
 #include <linux/string.h>
 #include <linux/types.h>
+#include <linux/cpu.h>
+#include <linux/mutex.h>
+#include <linux/workqueue.h>
 #include <linux/cpuhotplug.h>

 #include "../slab.h"
@@ -308,6 +311,48 @@ static void per_cpu_remove_cache(void *a
 	qlist_free_all(&to_free, cache);
 }

+#ifdef CONFIG_PREEMPT_RT
+struct remove_cache_work {
+	struct work_struct work;
+	struct kmem_cache *cache;
+};
+
+static DEFINE_MUTEX(remove_caches_lock);
+static DEFINE_PER_CPU(struct remove_cache_work, remove_cache_work);
+
+static void per_cpu_remove_cache_work(struct work_struct *w)
+{
+	struct remove_cache_work *rcw;
+
+	rcw = container_of(w, struct remove_cache_work, work);
+	per_cpu_remove_cache(rcw->cache);
+}
+
+static void per_cpu_remove_caches_sync(struct kmem_cache *cache)
+{
+	struct remove_cache_work *rcw;
+	unsigned int cpu;
+
+	cpus_read_lock();
+	mutex_lock(&remove_caches_lock);
+
+	for_each_online_cpu(cpu) {
+		rcw = &per_cpu(remove_cache_work, cpu);
+		INIT_WORK(&rcw->work, per_cpu_remove_cache_work);
+		rcw->cache = cache;
+		schedule_work_on(cpu, &rcw->work);
+	}
+
+	for_each_online_cpu(cpu) {
+		rcw = &per_cpu(remove_cache_work, cpu);
+		flush_work(&rcw->work);
+	}
+
+	mutex_unlock(&remove_caches_lock);
+	cpus_read_unlock();
+}
+#endif
+
 /* Free all quarantined objects belonging to cache. */
 void kasan_quarantine_remove_cache(struct kmem_cache *cache)
 {
@@ -321,7 +366,11 @@ void kasan_quarantine_remove_cache(struc
 	 * achieves the first goal, while synchronize_srcu() achieves the
 	 * second.
 	 */
+#ifndef CONFIG_PREEMPT_RT
 	on_each_cpu(per_cpu_remove_cache, cache, 1);
+#else
+	per_cpu_remove_caches_sync(cache);
+#endif

 	raw_spin_lock_irqsave(&quarantine_lock, flags);
 	for (i = 0; i < QUARANTINE_BATCHES; i++) {



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2021-08-21  6:49 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-06  8:26 Question on KASAN calltrace record in RT Zhang, Qiang
2021-04-13 15:29 ` Dmitry Vyukov
2021-04-14  4:00   ` Mike Galbraith
2021-04-14  5:26     ` Dmitry Vyukov
2021-04-14  6:15       ` Mike Galbraith
2021-04-14 15:04         ` [patch] kasan: make it RT aware Mike Galbraith
2021-04-15 18:13       ` Question on KASAN calltrace record in RT Mike Galbraith
2021-04-14  7:29     ` 回复: " Zhang, Qiang
2021-04-14  7:56       ` Mike Galbraith
2021-04-14  8:09         ` 回复: " Zhang, Qiang
2021-04-14  6:58   ` Zhang, Qiang
2021-04-14  7:07     ` Dmitry Vyukov
2021-08-21  6:48 [patch] kasan: Make it RT aware Mike Galbraith

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.