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 A23B2C433EF for ; Fri, 15 Apr 2022 18:35:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BEF2A6B0072; Fri, 15 Apr 2022 14:35:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B77AB6B0073; Fri, 15 Apr 2022 14:35:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F1F36B0074; Fri, 15 Apr 2022 14:35:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id 8F2C86B0072 for ; Fri, 15 Apr 2022 14:35:02 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 618D860D85 for ; Fri, 15 Apr 2022 18:35:02 +0000 (UTC) X-FDA: 79359965244.22.5727E27 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf23.hostedemail.com (Postfix) with ESMTP id 9762314000C for ; Fri, 15 Apr 2022 18:35:01 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id AD02EB82E24; Fri, 15 Apr 2022 18:34:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24888C385A5; Fri, 15 Apr 2022 18:34:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1650047698; bh=CHJOi0QikcLS+zbx9WMqbgiw520KeIq70nFT8T9pZvE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=mTIddQFCF2meMRyoQtshjWg/omYfycmOtZ+d1O4m0LVYZT8iK3E6TV9LZHq73j+DZ tW9FiaW1sIg7hKrR+kQ4i1erxbPToQx6eEIxcmrx6jJbCToDVgFtZjIJcgRg5+WHbR dKIsU5lv6SgRqo5LgW7W5BIci6aNhf0SzY/8MEuM= Date: Fri, 15 Apr 2022 11:34:56 -0700 From: Andrew Morton To: Zqiang Cc: ryabinin.a.a@gmail.com, glider@google.com, andreyknvl@gmail.com, dvyukov@google.com, bigeasy@linutronix.de, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v2] kasan: Fix sleeping function called from invalid context on RT kernel Message-Id: <20220415113456.2f24660a610e4f35fd72970a@linux-foundation.org> In-Reply-To: <20220401134649.2222485-1-qiang1.zhang@intel.com> References: <20220401134649.2222485-1-qiang1.zhang@intel.com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=mTIddQFC; dmarc=none; spf=pass (imf23.hostedemail.com: domain of akpm@linux-foundation.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9762314000C X-Stat-Signature: g77kwaqqqtc1rhkr414q3h88drxziess X-HE-Tag: 1650047701-635792 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: On Fri, 1 Apr 2022 21:46:49 +0800 Zqiang wrote: > When the kmem_cache_shrink() be called, the IPI was triggered, the > ___cache_free() is called in IPI interrupt context, the local-lock > or spin-lock will be acquired. on PREEMPT_RT kernel, these lock is > replaced with sleepbale rt-spinlock, so the above problem is triggered. > fix it by move the qlist_free_allfrom() the IPI interrupt context > to the task context when PREEMPT_RT is enabled. This patch is rather ifdeffy so I propose the below cleanup. Please review and test? Note that it incorporates the changes from your https://lkml.kernel.org/r/20220414025925.2423818-1-qiang1.zhang@intel.com btw, how are we supposed to test PREEMPT_RT builds? I had to patch arch/Kconfig. --- a/mm/kasan/quarantine.c~kasan-fix-sleeping-function-called-from-invalid-context-on-rt-kernel-fix +++ a/mm/kasan/quarantine.c @@ -319,28 +319,37 @@ static void qlist_move_cache(struct qlis } } -static void per_cpu_remove_cache(void *arg) +#ifndef CONFIG_PREEMPT_RT +static void __per_cpu_remove_cache(struct qlist_head *q, void *arg) { struct kmem_cache *cache = arg; - struct qlist_head *q; -#ifndef CONFIG_PREEMPT_RT struct qlist_head to_free = QLIST_INIT; -#else - unsigned long flags; - struct cpu_shrink_qlist *sq; -#endif - q = this_cpu_ptr(&cpu_quarantine); -#ifndef CONFIG_PREEMPT_RT - if (READ_ONCE(q->offline)) - return; + qlist_move_cache(q, &to_free, cache); qlist_free_all(&to_free, cache); +} #else +static void __per_cpu_remove_cache(struct qlist_head *q, void *arg) +{ + struct kmem_cache *cache = arg; + unsigned long flags; + struct cpu_shrink_qlist *sq; + sq = this_cpu_ptr(&shrink_qlist); raw_spin_lock_irqsave(&sq->lock, flags); qlist_move_cache(q, &sq->qlist, cache); raw_spin_unlock_irqrestore(&sq->lock, flags); +} #endif + +static void per_cpu_remove_cache(void *arg) +{ + struct qlist_head *q; + + q = this_cpu_ptr(&cpu_quarantine); + if (READ_ONCE(q->offline)) + return; + __per_cpu_remove_cache(q, arg); } /* Free all quarantined objects belonging to cache. */ @@ -348,10 +357,6 @@ void kasan_quarantine_remove_cache(struc { unsigned long flags, i; struct qlist_head to_free = QLIST_INIT; -#ifdef CONFIG_PREEMPT_RT - int cpu; - struct cpu_shrink_qlist *sq; -#endif /* * Must be careful to not miss any objects that are being moved from @@ -363,13 +368,18 @@ void kasan_quarantine_remove_cache(struc on_each_cpu(per_cpu_remove_cache, cache, 1); #ifdef CONFIG_PREEMPT_RT - for_each_online_cpu(cpu) { - sq = per_cpu_ptr(&shrink_qlist, cpu); - raw_spin_lock_irqsave(&sq->lock, flags); - qlist_move_cache(&sq->qlist, &to_free, cache); - raw_spin_unlock_irqrestore(&sq->lock, flags); + { + int cpu; + struct cpu_shrink_qlist *sq; + + for_each_online_cpu(cpu) { + sq = per_cpu_ptr(&shrink_qlist, cpu); + raw_spin_lock_irqsave(&sq->lock, flags); + qlist_move_cache(&sq->qlist, &to_free, cache); + raw_spin_unlock_irqrestore(&sq->lock, flags); + } + qlist_free_all(&to_free, cache); } - qlist_free_all(&to_free, cache); #endif raw_spin_lock_irqsave(&quarantine_lock, flags); _