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 68BF6C282C5 for ; Mon, 3 Mar 2025 04:52:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BBDC6B0095; Sun, 2 Mar 2025 23:52:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 96B1B6B0096; Sun, 2 Mar 2025 23:52:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80B8E6B0098; Sun, 2 Mar 2025 23:52:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5FE4F6B0095 for ; Sun, 2 Mar 2025 23:52:22 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 17D3DA3704 for ; Mon, 3 Mar 2025 04:52:22 +0000 (UTC) X-FDA: 83179018524.04.87F6189 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf11.hostedemail.com (Postfix) with ESMTP id 2ADB34000B for ; Mon, 3 Mar 2025 04:52:18 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=wSWLuwDL; dmarc=none; spf=none (imf11.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1740977539; a=rsa-sha256; cv=none; b=fQ0uwSGGONbFZ+FQS88VRJtiOjBA7+tLVyrfz6w9vEbbl1Xb2YDqIliiMGErVJiC40Eagq 7CgGn4XZRNnEndj7gUqei6Fh7SkStwzU6JCqbmnN7YCQummWq0l0+T15Zu4/XtMzN/lCcl cQx7UsjuBh4FCXMEn3Tl9/3MuoxEyu0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=wSWLuwDL; dmarc=none; spf=none (imf11.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1740977539; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gQw5ITGZgmCnNe/hk5hAVGzpx+dtW1jxF5kvbKk1odc=; b=DqcOiyBVAMOBM+3Wyo6diQmsiTnuMZ/uTUHeZJ/yHNJKNTgZYrzyCPW5KAXdgHlzS9jVjn cxKCaLL16CxVmfLFiDnQhdjieHd2M8AH2F70+oWRqtcGCHXURJehTn5XKtLiZOG2lVWI48 6MrwBXe2GtI7XV62FQNhcDgHv9oZm7c= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=gQw5ITGZgmCnNe/hk5hAVGzpx+dtW1jxF5kvbKk1odc=; b=wSWLuwDLOx/IJZoMUiZ0yLVGD2 SXj8j0XbeQNESMdMfbNuJ/QVGxN4Nv7yHkdEn7N+3JpX/8ziNQncpR0Ynhc9ODh2iuuCPpC3kv61t 8rjAQdpbQbAWzTCoxuOwLB1/FUVQD5AGsxQsggfavZPHcTE5edeBdsE6rYCysG0CDwwi72ymLzyfH dzQuCzdW+sHYBawGMWiblgM+wgqcSyNDHs3069Cip0lO0QYSQUK+Ik36Kus3jjXRGyn0SuEkN/V6K oT2FaHsFenmKx8eADvCWxRiDakYpt37qIq4cAHLLtK2eargfP9WChI4J143zjK5At4HwKEibYNLvq yRBRqg6Q==; Received: from willy by casper.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1toxmp-0000000Aadi-29RH; Mon, 03 Mar 2025 04:52:15 +0000 Date: Mon, 3 Mar 2025 04:52:15 +0000 From: Matthew Wilcox To: Qu Wenruo Cc: Linux Memory Management List , linux-f2fs-devel@lists.sourceforge.net, Jens Axboe , linux-fsdevel@vger.kernel.org Subject: Re: Calling folio_end_writeback() inside a spinlock under task context? Message-ID: References: <14bd34c8-8fe0-4440-8dfd-e71223303edc@gmx.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <14bd34c8-8fe0-4440-8dfd-e71223303edc@gmx.com> X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 2ADB34000B X-Stat-Signature: q6bea8nqinaqio3way7bdyhsaqb8gdsi X-HE-Tag: 1740977538-483030 X-HE-Meta: U2FsdGVkX1/R805wQE8jLbzKdCKwqqrRmOxq/7B2Vvoq+WlaU0eOBWmQZYiUFqet1uPRUxkikyyj4LtPuzieKMB5guu4xRZZEydhTDgJVsjnW0WSRjFkL9auTZiGgLoOymk6/9KU5VNYTB11FCIjtadO6rXgY/oFtvB+vUpf228t2Y34p1Cch3MEb9Tph1PGcvqwgRZ0Qjcuxrslt1nvMvE2G2l0a5Dtl5dtfGzhUFptLajfTTej6/HjDz6ukr1y7OD50m7gQH9Ddxsu/PGCv06pI7Z6Gznod/xIQOvxwvz8T4fttqNGxfDPNeq4sHcZvuJmsMrT6b0agF8Q9rCI+uUVbhEWWMvxDoYJPq0ZNP2nJHjIJYIJPY3zWJ8B+x9D0ALDYI4zkRFwIZuGDINPAQEjwHKvZobTcJag+zZieEV50e/+67Ko5tC5pWAoTo1OdJUgDYFYKGtTPwNrO269NtWiqT6R4zPfyofvtqkhWaGahLjgLlID14hAee+6tcQJBWOlydE5tt9T887m3ag7uZvUuc9mhfPl6pmPPaJDrJCG1seWWzrB1jubbO8pPRKyhJVpdHCpjNSW+X9O4f4xLZ0XkD/Wd3ird81AZVs1QjhxDjZFGwfkb1jeJqCPecIInvSbuH3kGA4MFDkLO0ndWpw+vF4Ix6/nPtHIjGzgFw6/CEMPqZ8+BBQqeEVPhBz+tx8vtOD2pNHLSIhzEcr7O9OHYxY17RtQcuP1qZZXgLbnKu1JlOx1VNzEYc2KB++9ofXvcKmOxTtat1TUh6xvAcYvDO4ePh6v7i89YTYoZbA7l3sVvcMT+oFYGFvfAfI3aUeDWoAT5w7qO3mnD6bdbOAvQY6g8OtD9s1AcTsSVcdFw4gtLD66YcAHAx/+ALIIHuQYwqMik1lkabgS8FiITRE8EiiGvO8UInzSMWUvTsWiOqDo3UToFdXH8Mcraf646e9cxu2cL6Vh6fw26de kraqCnSW BM+Zeg92dpWdJe1OqPv4d6RU28kHqBhHncvPStjJuG2F+0Wp+pE+HbuRN4ScujGTViTPdLNWc+84UhwE88b8N/R/dagKba7nV1/Bwc5ZIeRPy4hI3ro8U0MjRO9pWcT1JGFYh+j1QCjUHdlCWe7SacCmV8gDAm6GwjFPYy0waEr430jv/MlE6SwOYJBE+/WMLs5vDxtG4UGNlANvR/yn+AQbRkk6wl3hQ9a9eTy1o3IO8sw/Tqa7qZlKaOGf/24NPeToHXVN3xi9NvO62EfUKlf4P7pUHIRymasCvD5rk3vaj0Ch2Im4vjdzNiSdpWq+CZIHS 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: List-Subscribe: List-Unsubscribe: Adding Jens to the cc. As you well know, he added this code, so I'm mystified why you didn't cc him. Also adding linux-fsdevel (I presume this was a mistake and you inadvertently cc'd f2fs-devel instead.) On Mon, Mar 03, 2025 at 10:34:26AM +1030, Qu Wenruo wrote: > [SPINLOCK AND END WRITEBACK] > > Although folio_end_writeback() can be called in an interruption context > (by the in_task() check), surprisingly it may not be suitable to be > called inside a spinlock (in task context): It's poor practice to do that; you're introducing a dependency between your fs lock and the i_mapping lock, which is already pretty intertwined with ... every other lock in the system. > For example the following call chain can lead to sleep: > > spin_lock() > folio_end_writeback() > |- folio_end_dropbehind_write() > |- folio_launder() > Which can sleep. > > My question is, can and should we allow folio_end_writeback() inside a > spinlock? > > [BTRFS' ASYNC EXTENT PROBLEM] > > This is again a btrfs specific behavior, that we have to call > folio_end_writeback() inside a spinlock to make sure really only one > thread can clear the writeback flag of a folio. > > I know iomap is doing a pretty good job preventing early finished > writeback to clear the folio writeback flag, meanwhile we're still > submitting other blocks inside the folio. > > Iomap goes holding one extra writeback count before starting marking > blocks writeback and submitting them. > And after all blocks are submitted, reduce the writeback count (and call > folio_end_writeback() if it's the last one holding the writeback flag). > > But the iomap solution requires that, all blocks inside a folio to be > submitted at the same time. I aactually don't like the iomap solution as it's currently written; it just hasn't risen high enough up my todo list to fix it. What we should do is initialise folio->ifs->write_bytes_pending to bitmap_weight(ifs->state, blocks_per_folio) * block_size in iomap_writepage_map() [this is oversimplified; we'd need to handle eof and so on too] That would address your problem as well as do fewer atomic operations. > This is not true in btrfs, due to the design of btrfs' async extent, > which can mark the blocks for writeback really at any timing, as we keep > the lock of folios and queue them into a workqueue to do compression, > then only after the compression is done, we submit and mark them > writeback and unlock. > > If we do not hold a spinlock calling folio_end_writeback(), but only > checks if we're the last holder and call folio_end_writeback() out of > the spinlock, we can hit the following race where folio_end_writeback() > can be called on the same folio: > > 0 32K 64K > |<- AE 1 ->|<- AE 2 ->| > > Thread A (AE 1) | Thread B (AE 2) > --------------------------------------+------------------------------ > submit_one_async_extent() | > |- process_one_folio() | > |- subpage_set_writeback() | > | > /* IO finished */ | > end_compressed_writeback() | > |- btrfs_folio_clear_writeback() | > |- spin_lock() | > | /* this is the last writeback | > | holder, should end the | > | folio writeback flag */ | > |- last = true | > |- spin_unlock() | > | | submit_one_async_extent() > | | |- process_one_folio() > | | |- subpage_set_writeback() This seems weird. Why are you setting the "subpage" writeback bit while the folio writeback bit is still set? That smells like a bug-in-waiting if not an actual bug to me. Surely it should wait on the folio writeback bit to clear? > | | /* IO finished */ > | | end_compressed_writeback() > | | |btrfs_folio_clear_writeback() > | | | /* Again the last holder */ > | | |- spin_lock() > | | |- last = true > | | |- spin_unlock() > |- folio_end_writeback() | |- folio_end_writeback() > > I know the most proper solution would to get rid of the delayed unlock > and submission, but mark blocks for writeback without the async extent > mechanism completely, then follow the iomap's solution. > > But that will be a huge change (although we will go that path > eventually), meanwhile the folio_end_writeback() inside spinlock needs > to be fixed asap. I'd suggest the asap solution is for btrfs to mark itself as not supporting dropbehind. > So my question is, can we allow folio_end_writeback() to be called > inside a spinlock, at the cost of screwing up the folio reclaim for > DONTCACHE? > > Thanks, > Qu >