From patchwork Fri Dec 10 20:41:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wander Lairson Costa X-Patchwork-Id: 522975 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADB4DC433F5 for ; Fri, 10 Dec 2021 20:42:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344228AbhLJUqb (ORCPT ); Fri, 10 Dec 2021 15:46:31 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35160 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344263AbhLJUqa (ORCPT ); Fri, 10 Dec 2021 15:46:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639168974; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lQVfgXvUEecz1ew4Duj5H2gRLrd/qk8IbwHLmWuD9+M=; b=J7glEDyDsbCF6hysyaLlZtpwT7/I335IMSZvWl8kEqnqVdNRZZV22DHLnPk3DaP/xGXWbx j5Cxkg8Mjpykfv3VC0+WJ3nVyZQy8PmFBidRkdPTGAvWVjJsHY7dPaU8x/tFPR+Fp0NkaA eEG4QU3Fq9pldXtefbXBqvREOPuofPM= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-108-eZtuOUN6MsON-0NKnFnQWQ-1; Fri, 10 Dec 2021 15:42:51 -0500 X-MC-Unique: eZtuOUN6MsON-0NKnFnQWQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9F2D01023F4E; Fri, 10 Dec 2021 20:42:49 +0000 (UTC) Received: from wcosta.com (ovpn-116-199.gru2.redhat.com [10.97.116.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id 58E6C781EF; Fri, 10 Dec 2021 20:42:45 +0000 (UTC) From: Wander Lairson Costa To: linux-kernel@vger.kernel.org Cc: Steven Rostedt , Thomas Gleixner , Sebastian Andrzej Siewior , linux-rt-users@vger.kernel.org, Wander Lairson Costa Subject: [PREEMPT_RT PATCH 1/2] block: Avoid sleeping function called from invalid context bug Date: Fri, 10 Dec 2021 17:41:55 -0300 Message-Id: <20211210204156.96336-2-wander@redhat.com> In-Reply-To: <20211210204156.96336-1-wander@redhat.com> References: <20211210204156.96336-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-rt-users@vger.kernel.org This was caught during QA test: BUG: sleeping function called from invalid context at kernel/locking/rtmutex.c:942 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 243401, name: sed INFO: lockdep is turned off. Preemption disabled at: [] blk_cgroup_bio_start+0x28/0xd0 CPU: 2 PID: 243401 Comm: sed Kdump: loaded Not tainted 4.18.0-353.rt7.138.el8.x86_64+debug #1 Hardware name: HP ProLiant DL380 Gen9, BIOS P89 05/06/2015 Call Trace: dump_stack+0x5c/0x80 ___might_sleep.cold.89+0xf5/0x109 rt_spin_lock+0x3e/0xd0 ? __blk_add_trace+0x428/0x4b0 __blk_add_trace+0x428/0x4b0 blk_add_trace_bio+0x16e/0x1c0 generic_make_request_checks+0x7e8/0x8c0 generic_make_request+0x3c/0x420 ? membarrier_private_expedited+0xd0/0x2b0 ? lock_release+0x1ca/0x450 ? submit_bio+0x3c/0x160 ? _raw_spin_unlock_irqrestore+0x3c/0x80 submit_bio+0x3c/0x160 ? rt_mutex_futex_unlock+0x66/0xa0 iomap_submit_ioend.isra.36+0x4a/0x70 xfs_vm_writepages+0x65/0x90 [xfs] do_writepages+0x41/0xe0 ? rt_mutex_futex_unlock+0x66/0xa0 __filemap_fdatawrite_range+0xce/0x110 xfs_release+0x11c/0x160 [xfs] __fput+0xd5/0x270 task_work_run+0xa1/0xd0 exit_to_usermode_loop+0x14d/0x160 do_syscall_64+0x23b/0x240 entry_SYSCALL_64_after_hwframe+0x6a/0xdf We replace the get/put_cpu() call by get/put_cpu_light to avoid this bug. Signed-off-by: Wander Lairson Costa Reported-by: kernel test robot --- block/blk-cgroup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 663aabfeba18..0a532bb3003c 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1911,7 +1911,7 @@ void blk_cgroup_bio_start(struct bio *bio) struct blkg_iostat_set *bis; unsigned long flags; - cpu = get_cpu(); + cpu = get_cpu_light(); bis = per_cpu_ptr(bio->bi_blkg->iostat_cpu, cpu); flags = u64_stats_update_begin_irqsave(&bis->sync); @@ -1928,7 +1928,7 @@ void blk_cgroup_bio_start(struct bio *bio) u64_stats_update_end_irqrestore(&bis->sync, flags); if (cgroup_subsys_on_dfl(io_cgrp_subsys)) cgroup_rstat_updated(bio->bi_blkg->blkcg->css.cgroup, cpu); - put_cpu(); + put_cpu_light(); } static int __init blkcg_init(void) From patchwork Fri Dec 10 20:41:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wander Lairson Costa X-Patchwork-Id: 523314 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D37DC433FE for ; Fri, 10 Dec 2021 20:43:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344267AbhLJUqe (ORCPT ); Fri, 10 Dec 2021 15:46:34 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35460 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242454AbhLJUqd (ORCPT ); Fri, 10 Dec 2021 15:46:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639168978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kfeMOa6Z5Vowu/EKkb75Mnq7CRyTMFcviOigZZP4WFY=; b=IKp5OrjmjVTzHkUq1uVbWwySZywFd93IXSVp7N/S4BYKTHYDJQVf0cFKGOnNASI3jOHwgf T1xFuLyl+27QHNAhJITXGraP47NMefEJUH7CY2sQIGSXW4k4/62RtVh5s+C0ecd5YKrQMK 8BINwNlsmZ4deZH9fB/gJaJtjdwqW5Y= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-510-QibK6UsnNwWQC8WgNgax8w-1; Fri, 10 Dec 2021 15:42:55 -0500 X-MC-Unique: QibK6UsnNwWQC8WgNgax8w-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D129E192FDA0; Fri, 10 Dec 2021 20:42:53 +0000 (UTC) Received: from wcosta.com (ovpn-116-199.gru2.redhat.com [10.97.116.199]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6FF5B781EF; Fri, 10 Dec 2021 20:42:50 +0000 (UTC) From: Wander Lairson Costa To: linux-kernel@vger.kernel.org Cc: Steven Rostedt , Thomas Gleixner , Sebastian Andrzej Siewior , linux-rt-users@vger.kernel.org, Wander Lairson Costa Subject: [PREEMPT_RT PATCH 2/2] blktrace: switch trace spinlock to a raw spinlock Date: Fri, 10 Dec 2021 17:41:56 -0300 Message-Id: <20211210204156.96336-3-wander@redhat.com> In-Reply-To: <20211210204156.96336-1-wander@redhat.com> References: <20211210204156.96336-1-wander@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Precedence: bulk List-ID: X-Mailing-List: linux-rt-users@vger.kernel.org TRACE_EVENT disables preemption before calling the callback. Because of that blktrace triggers the following bug under PREEMPT_RT: BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:35 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 119, name: kworker/u2:2 5 locks held by kworker/u2:2/119: #0: ffff8c2e4a88f538 ((wq_completion)xfs-cil/dm-0){+.+.}-{0:0}, at: process_one_work+0x200/0x450 #1: ffffab3840ac7e68 ((work_completion)(&cil->xc_push_work)){+.+.}-{0:0}, at: process_one_work+0x200/0x450 #2: ffff8c2e4a887128 (&cil->xc_ctx_lock){++++}-{3:3}, at: xlog_cil_push_work+0xb7/0x670 [xfs] #3: ffffffffa6a63780 (rcu_read_lock){....}-{1:2}, at: blk_add_trace_bio+0x0/0x1f0 #4: ffffffffa6610620 (running_trace_lock){+.+.}-{2:2}, at: __blk_add_trace+0x3ef/0x480 Preemption disabled at: [] migrate_enable+0x45/0x140 CPU: 0 PID: 119 Comm: kworker/u2:2 Kdump: loaded Not tainted 5.14.0-25.rt21.25.light.el9.x86_64+debug #1 Hardware name: Red Hat KVM, BIOS 0.5.1 01/01/2011 Workqueue: xfs-cil/dm-0 xlog_cil_push_work [xfs] Call Trace: ? migrate_enable+0x45/0x140 dump_stack_lvl+0x57/0x7d ___might_sleep.cold+0xe3/0xf7 rt_spin_lock+0x3a/0xd0 ? __blk_add_trace+0x3ef/0x480 __blk_add_trace+0x3ef/0x480 blk_add_trace_bio+0x18d/0x1f0 trace_block_bio_queue+0xb5/0x150 submit_bio_checks+0x1f0/0x520 ? sched_clock_cpu+0xb/0x100 submit_bio_noacct+0x30/0x1d0 ? bio_associate_blkg+0x66/0x190 xlog_cil_push_work+0x1b6/0x670 [xfs] ? register_lock_class+0x43/0x4f0 ? xfs_swap_extents+0x5f0/0x5f0 [xfs] process_one_work+0x275/0x450 ? process_one_work+0x200/0x450 worker_thread+0x55/0x3c0 ? process_one_work+0x450/0x450 kthread+0x188/0x1a0 ? set_kthread_struct+0x40/0x40 ret_from_fork+0x22/0x30 To avoid this bug, we switch the trace lock to a raw spinlock. Signed-off-by: Wander Lairson Costa Reported-by: kernel test robot Reported-by: kernel test robot --- kernel/trace/blktrace.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c index 1183c88634aa..e900dd3efcbe 100644 --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -34,7 +34,7 @@ static struct trace_array *blk_tr; static bool blk_tracer_enabled __read_mostly; static LIST_HEAD(running_trace_list); -static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock); +static __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(running_trace_lock); /* Select an alternative, minimalistic output than the original one */ #define TRACE_BLK_OPT_CLASSIC 0x1 @@ -121,12 +121,12 @@ static void trace_note_tsk(struct task_struct *tsk) struct blk_trace *bt; tsk->btrace_seq = blktrace_seq; - spin_lock_irqsave(&running_trace_lock, flags); + raw_spin_lock_irqsave(&running_trace_lock, flags); list_for_each_entry(bt, &running_trace_list, running_list) { trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm), 0); } - spin_unlock_irqrestore(&running_trace_lock, flags); + raw_spin_unlock_irqrestore(&running_trace_lock, flags); } static void trace_note_time(struct blk_trace *bt) @@ -666,9 +666,9 @@ static int __blk_trace_startstop(struct request_queue *q, int start) blktrace_seq++; smp_mb(); bt->trace_state = Blktrace_running; - spin_lock_irq(&running_trace_lock); + raw_spin_lock_irq(&running_trace_lock); list_add(&bt->running_list, &running_trace_list); - spin_unlock_irq(&running_trace_lock); + raw_spin_unlock_irq(&running_trace_lock); trace_note_time(bt); ret = 0; @@ -676,9 +676,9 @@ static int __blk_trace_startstop(struct request_queue *q, int start) } else { if (bt->trace_state == Blktrace_running) { bt->trace_state = Blktrace_stopped; - spin_lock_irq(&running_trace_lock); + raw_spin_lock_irq(&running_trace_lock); list_del_init(&bt->running_list); - spin_unlock_irq(&running_trace_lock); + raw_spin_unlock_irq(&running_trace_lock); relay_flush(bt->rchan); ret = 0; }