From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80DBC3655E9 for ; Tue, 12 May 2026 15:45:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778600725; cv=none; b=q4OhMb6jFWzFtFtUSYmqceQInwW4PDUTepmt+rgisyw0KOwblVy6xCVJVbZxr4y/DU/56CjM8ju0iuMLyY+6ZHaXXvxFl4mx/7gBiXaY16eEOQqHJeQ85JXE77jX9RUkaE+RVcUm785jonBpVAZouu3FHNheupnDtUDBlVj/lkM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778600725; c=relaxed/simple; bh=0VqlMifA9WfgtGvIY7oa1hJjCcmJdHVbFplw7GNQXjg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cIC9A/POtQomN59Y4JCkEqjcitsGvcEcjJ7Q8LibdSBgNEcnPQ22fVRj/3Ujlrbab5XVDPTYMb28FIIq4Nb4CPjv3IFKGnCPBItOIFOMWVlKKHF/aedeqjOfB+1F6ggcfDw3Une1qz86xFPf7gieSoyPRFFTQCTIlKIJLxKShEo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=m9F+0o/K; arc=none smtp.client-ip=209.85.210.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="m9F+0o/K" Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-7dcdd1b492eso5089510a34.1 for ; Tue, 12 May 2026 08:45:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778600723; x=1779205523; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=OodPr8B22yRreAu513frxaApOkBgnO2v0gw1uJXccro=; b=m9F+0o/KTa7NMhxe6rnZewq5xrhtn6vdNtHzPkv16Y1unf+y9Xzxhb6NicxBgWeXds qv37erLRy7x7KGHfDIZVBTEUniC8mD9tamFLyhEJlzDrwBnu4LwgMSRrBv2bYTgbGc/r 7HWjRQ0CHoMr/EKuNtt4H/P0+lgx7qzk/AdC42UNxe/+caOk/wesfCFtr6tfxKNMKNHw abBE58n2rp6KWAUVKtv2GaksktEoE9jWGbUjRdYVYylB+2KFog/DldEw8cvti8EpuKxI EEADAOQ1q7sNC+7e2EanWcLSIwMZXlXvVfCapFhH68e/48p2Si/YnjGl6hwHUBjWDmT3 tnQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778600723; x=1779205523; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OodPr8B22yRreAu513frxaApOkBgnO2v0gw1uJXccro=; b=IuCvXrihxYYoGD8nX1D475CxmpVcKj/5EKxCg1s9Xni7UXDHUzYM9vzOqUUJ4Hun/x tw6QEhzX7jvnxBsQxY+5nCd6ZB81QeitZ9cr25yqaKkiEi3OUVjo/aX+nUneLulF2Xey BBlIijYmKWglyZTWJCDZPJgQN+XEm2bSMra/DsUf7Hs28BfTQhbYlqrGO9KfrlLfn4VC 6Q4AORQE6UrtCbQUd90Ssk4iDI8AWOmkaI6CLg+JRgthBz8bQGHhFDLTqzLNwePCizPZ 3/CNK/8c7h0xDSNUL3kCMv6DgBxFUhwSuMKJICFkXmeXMca9ByoSPsVn1gDhyLg1AptF JBwQ== X-Forwarded-Encrypted: i=1; AFNElJ+vbgurhXmLQ5YQzU95YlHzj9c8yyjMsJabfqlZXwS+LD3oyzvKe3XwqmfYQwHL2WyBGekQWs+nhLFWBjU=@vger.kernel.org X-Gm-Message-State: AOJu0Yys4kEXY+MZt6Ky1gmKR8yv3jlAlxXQJ1g96RkzTvNyuP5c5Ged HdSNFrzUQpg3uzmtTYOh0/Nm0yWOURRXWwqeaspq7/kcZG9IDQq7JjWD X-Gm-Gg: Acq92OEOBQRVgYjYoq9jjiZQKcAmyDgd/i23z67lqfbPCSzMkEDELE6k5hnkYI5ZTNn JoPqK1Yhlyl9TUnlq5M72TZ5uI9cCqovhDgAtaIOzNdleafWM//Z91/N7uPYCWSRLOm1JxfwpX1 w3VGSSo6/cpwoxDBCQWi2lKvX6t6h8aa5QPTuxJ1LaH7xEguTyhLGknliVbtrkYH3dYSYNz1KEG iKPLwESd70UMFlVFH/peWk3en1Tt/170Ax2g1z0UC/WdqKvo4TRyMBkCovTyXL5EYiI6wo1r+50 x+YNX44vAG1ZNAuebZIv7NeTKxWlWvOSjMa99WWB3YYRHvaKhkIa0wfh8QqAmq39VSZC7z+exBv jZoYBo/nPNdrVm6zsMhk3X3tNSormu+u0YIE4hqFJViw7WGq9c6ZqiGi4mR0N5Dhs58KFfofEn7 jhKJRNM7xkU7zB70qoWer35fmw/KAIPuIKYn7dlBpwolnzR9Nv9JqxPRQAK8blyKuVojrNGV/8Z UQteoqff9cyFihtOi8q X-Received: by 2002:a9d:799a:0:b0:7dc:e2ae:c15c with SMTP id 46e09a7af769-7e3bd35333fmr1627765a34.16.1778600723396; Tue, 12 May 2026 08:45:23 -0700 (PDT) Received: from fedora ([172.245.82.59]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7e367d9d788sm8910606a34.24.2026.05.12.08.45.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 May 2026 08:45:22 -0700 (PDT) Date: Tue, 12 May 2026 23:45:14 +0800 From: Ming Lei To: Peter Zijlstra Cc: Jens Axboe , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Ingo Molnar , Juri Lelli , Vincent Guittot , Michael Wu , Xiaosen He , Tejun Heo , Thomas Gleixner Subject: Re: [PATCH] sched: flush plug in schedule_preempt_disabled() to prevent deadlock Message-ID: References: <20260512085939.1107372-1-tom.leiming@gmail.com> <20260512120431.GC1889694@noisy.programming.kicks-ass.net> <20260512124021.GA2214256@noisy.programming.kicks-ass.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260512124021.GA2214256@noisy.programming.kicks-ass.net> On Tue, May 12, 2026 at 02:40:21PM +0200, Peter Zijlstra wrote: > On Tue, May 12, 2026 at 02:04:32PM +0200, Peter Zijlstra wrote: > > On Tue, May 12, 2026 at 04:59:39PM +0800, Ming Lei wrote: > > > On preemptible kernels, a deadlock can occur when a task with plugged IO > > > calls schedule_preempt_disabled(): > > > > > > schedule_preempt_disabled() > > > sched_preempt_enable_no_resched() // preemption now enabled > > > schedule() // <-- preemption can happen here > > > sched_submit_work() > > > blk_flush_plug() > > > > > > After sched_preempt_enable_no_resched() re-enables preemption, the task > > > can be preempted (e.g., by a higher-priority RT task) before reaching > > > blk_flush_plug() in sched_submit_work(). Since the task's state is > > > already TASK_UNINTERRUPTIBLE (set by the mutex/rwsem slowpath caller), > > > requests in current->plug remain unflushed for an unbounded time. > > > > > > If another task depends on those plugged requests to make progress (e.g., > > > to release a lock the sleeping task needs), a deadlock results: > > > > > > - Task A (writeback worker): holds plugged IO, preempted before > > > flushing, stuck on run queue behind higher-priority work > > > - Task B: waiting for IO completion from Task A's plug, holds a lock > > > that Task A needs to be woken up > > > > > > Both reported deadlocks involve mutex/rwsem slowpaths, which are the > > > primary callers of schedule_preempt_disabled() with non-running task > > > state. > > > > > > Fix by flushing the plug in schedule_preempt_disabled() while > > > preemption is still disabled. This ensures the plug is empty before the > > > preemption window opens. > > > > How is this different from any path calling schedule()? That would be > > subject to exactly the same issue. > > > > The patch cannot be correct. > > Also, is there a reason io_schedule_prepare() has a blk_flush_plug() > call? It is added in Tejun's "[PATCHSET RFC] sched, jbd2: mark sleeps on journal->j_checkpoint_mutex as iowait": https://lore.kernel.org/all/1477673892-28940-1-git-send-email-tj@kernel.org/#t which fixes iowait accounting for ext4, meantime adds the model "io_schedule_prepare() + schedule() + io_schedule_finish()", which actually can avoid this kind issue easily because io_schedule_prepare() is called in task running state. For this f2fs issue, maybe it can be addressed by adding rwsem io variant just like mutex_lock_io(), meantime iowait accounting is covered too. > io_schedule() > token = io_schedule_prepare() > blk_flush_plug(current->plug, true); > schedule() > if (!task_is_running(tsk)) > sched_submit_work() > blk_flush_plug(tsk->plug, true); > > Why isn't the one in sched_submit_work() sufficient? This thing either > needs a comment justifying its existence, or get removed. This plug is originally added in 73c101011926 "block: initial patch for on-stack per-task plugging") and commit a237c1c5bc5d "block: let io_schedule() flush the plug inline" by Jens, when there isn't such preempt issue. But it is moved out to sched_submit_work() latter in commit 9c40cef2b799 "sched: Move blk_schedule_flush_plug() out of __schedule()") by Thomas Gleixner, when this issue starts to become likely. If io_schedule_prepare() can be called in every iowait context, looks blk_flush_plug() from sched_submit_work() may be removed. Thanks, Ming