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 93798D41D44 for ; Tue, 12 Nov 2024 02:31:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F0076B00C1; Mon, 11 Nov 2024 21:31:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 676EF6B00D5; Mon, 11 Nov 2024 21:31:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F2E66B00E9; Mon, 11 Nov 2024 21:31:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 2D5C36B00C1 for ; Mon, 11 Nov 2024 21:31:55 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C896C161BAE for ; Tue, 12 Nov 2024 02:31:54 +0000 (UTC) X-FDA: 82775866572.24.7E8E25A Received: from out30-133.freemail.mail.aliyun.com (out30-133.freemail.mail.aliyun.com [115.124.30.133]) by imf04.hostedemail.com (Postfix) with ESMTP id 14EEA40008 for ; Tue, 12 Nov 2024 02:30:57 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=aLFQWcDK; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf04.hostedemail.com: domain of jefflexu@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=jefflexu@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731378651; a=rsa-sha256; cv=none; b=10ofDNVYa8CkG6DYGyOPHFWEPcqiCTX75racOrsHcEXEkfMi+uBIvj1gmMkH/d4AqJcT1N PY0dHnxZqw7yYzIs8fR/zxDz7Lewf59QKIt6MWOTXzOffNvOTkiQ7AoC0nLZvCDLwphaPL x/WXGcQAAtT/lZvvSw2zMAkv5CmN+UM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=aLFQWcDK; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf04.hostedemail.com: domain of jefflexu@linux.alibaba.com designates 115.124.30.133 as permitted sender) smtp.mailfrom=jefflexu@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731378651; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=5E/yqWznFqIBCSOfrFucrEVO6/G4cCny4JPSermzaNk=; b=7KfVuwnEdQupIroJZz+5MDGx0DGRHJ2Y/RLsVL/RlDaVLMOCov8fBlDHrZ76+asFYIS/QY 2WmdlOu2FWLKaUTrqhkULTxBn1bGg12h3FtXicYOx7821x9X/J1VpzsZULFknUEuEQ+sJ4 EZaTE7DvYq1ggy+CJPfsnGveEV7+g00= DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1731378707; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=5E/yqWznFqIBCSOfrFucrEVO6/G4cCny4JPSermzaNk=; b=aLFQWcDKrXID3iZ9EFim1uJaFkGd31AQEAq9EEA4+4D9QdW4FKvFEEMp71NcTiVjqFecwatPEdgt4MrDvo8f7AUsnAyvr8AzfQecjPomC/HU5jDsBh92oS6OykxPikwa+/Sd6xp34Qs2IVKX0JCHGotsiZtrJ4Yg9giLWKrmbTk= Received: from 30.221.148.118(mailfrom:jefflexu@linux.alibaba.com fp:SMTPD_---0WJFJuD2_1731378705 cluster:ay36) by smtp.aliyun-inc.com; Tue, 12 Nov 2024 10:31:46 +0800 Message-ID: <0f585a7c-678b-492a-9492-358f21e57291@linux.alibaba.com> Date: Tue, 12 Nov 2024 10:31:43 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 6/6] fuse: remove tmp folio for writebacks and internal rb tree To: Joanne Koong Cc: miklos@szeredi.hu, linux-fsdevel@vger.kernel.org, shakeel.butt@linux.dev, josef@toxicpanda.com, linux-mm@kvack.org, bernd.schubert@fastmail.fm, kernel-team@meta.com References: <20241107235614.3637221-1-joannelkoong@gmail.com> <20241107235614.3637221-7-joannelkoong@gmail.com> <9c0dbdac-0aed-467c-86c7-5b9a9f96d89d@linux.alibaba.com> Content-Language: en-US From: Jingbo Xu In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 14EEA40008 X-Rspamd-Server: rspam01 X-Stat-Signature: ahtrag83i96kkawkxw3nq67i7w4peszq X-HE-Tag: 1731378657-512411 X-HE-Meta: U2FsdGVkX18mZVJ1QTBVV4JJwr2Ymu70hG2dgx0SFS2ICJEFV7y00ToM2JMj1RbrV9NparsEegJFAzCShkboSAkol7PXWY3FzG7OV86UkZWUyanRnVPDtnFpdaiZ1s0eB32O3PgEl7q2Z7CyHIaEltxWG7s+H6bZ+W763tvtKKDOwSovFvl9ldqACfrxNV33B+lBuUsHrI57Y/rWwhaUHiHkLp6VZuLy4QIHXFr0n2WtYS3K86/NwlCYGh1DRWqBgvFbsdWlyzDXAbXJ7UmRoLdoGvRU56qy6vJ2FrKGyZgBwFucwmi+TmeW+X/geKNePGP1c8pF/emSK5qqAvUyNsHe4ja7WnauCV4/+b4X7I6Lqr/5rnQ3tvqJ9IT/+S3T4vD9WiUewbnu1FDa0xi8WJw0l7/kvovbJB+zHRm/gtSssPqozqvtuMW0BAOYWfeSly+Y5QDI4d4NpmLpjlSQp+uc4ssfiDsG8lqKQRAv3zO4J0zj29A17LPMaBeQLh13cBmO21JI3mjJcgTy2db2uZ59HDxpaGj8GBXVH5kwDwRxF8zvsMs7uEh62Ti+D5ZK1W/kuxZdrkNus3pD4P8tYigSxrZzJg76zMsxmK001RjoFca2NBJ1K2R9gpBAg4fBxGgrl8EOn34b6+bl3JtBtbKVyonZmV4J7paW27/CX0eX8hvKT7Vg8B4B/d3+zfnyLa1YFGoo9jwYSimS5xYhf6p8eBLY2x9t3ZO7B6Cl2x09rNVMfhY3E/Z0XGOWTEzRmndX6cZbVrULa/dBRMhpFIYQKJ5zG4n643xqt8f9v7V8APsU+UCe9n+7G2J+/jfJXJa09d86evqt4OSNTc+bxT0Q4xlUa9JUTfWFsisFlyHfT4fdSRfDVO6ZNtP+lV7pt2pfT8Ylm5fzVQBMPDXkb7GyDb431sKROYUkOPNTJonFz5PLTiBChTgfD+ye9QqrqWQTDhCdCcInl0X7CA7 X3kCtuTc JDvzDz+c7kz5NeOLfBueF71h+wUZB+okW2vOSTJbxQR8/RazdvtnQjQ35v+myLTbAzTDLqdT+JpQnIv0sfah6LKPAOY6By63RWQiDIbD3XO8HyozcdXYYdexeOa2RVrOwhYV149IBrzCVGgFxRSIolumeUWzmK+x6y6VRCcIuookZXH9gOVnoxLiOBc2qpKrzvtWXs0b8pcgfBl6Amrc3Xp4qIYFMpdIBBVaP4SrUGWnlsoNmf2MlcM+xJ9ncYDm8FdDXyMiZm1qEEOXZlpsAf6eStCib3npE7IVH8PgJb8dU8faakrbQtKPS1NBhY7BNH2dr 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: On 11/12/24 5:30 AM, Joanne Koong wrote: > On Mon, Nov 11, 2024 at 12:32 AM Jingbo Xu wrote: >> >> Hi, Joanne and Miklos, >> >> On 11/8/24 7:56 AM, Joanne Koong wrote: >>> Currently, we allocate and copy data to a temporary folio when >>> handling writeback in order to mitigate the following deadlock scenario >>> that may arise if reclaim waits on writeback to complete: >>> * single-threaded FUSE server is in the middle of handling a request >>> that needs a memory allocation >>> * memory allocation triggers direct reclaim >>> * direct reclaim waits on a folio under writeback >>> * the FUSE server can't write back the folio since it's stuck in >>> direct reclaim >>> >>> To work around this, we allocate a temporary folio and copy over the >>> original folio to the temporary folio so that writeback can be >>> immediately cleared on the original folio. This additionally requires us >>> to maintain an internal rb tree to keep track of writeback state on the >>> temporary folios. >>> >>> A recent change prevents reclaim logic from waiting on writeback for >>> folios whose mappings have the AS_WRITEBACK_MAY_BLOCK flag set in it. >>> This commit sets AS_WRITEBACK_MAY_BLOCK on FUSE inode mappings (which >>> will prevent FUSE folios from running into the reclaim deadlock described >>> above) and removes the temporary folio + extra copying and the internal >>> rb tree. >>> >>> fio benchmarks -- >>> (using averages observed from 10 runs, throwing away outliers) >>> >>> Setup: >>> sudo mount -t tmpfs -o size=30G tmpfs ~/tmp_mount >>> ./libfuse/build/example/passthrough_ll -o writeback -o max_threads=4 -o source=~/tmp_mount ~/fuse_mount >>> >>> fio --name=writeback --ioengine=sync --rw=write --bs={1k,4k,1M} --size=2G >>> --numjobs=2 --ramp_time=30 --group_reporting=1 --directory=/root/fuse_mount >>> >>> bs = 1k 4k 1M >>> Before 351 MiB/s 1818 MiB/s 1851 MiB/s >>> After 341 MiB/s 2246 MiB/s 2685 MiB/s >>> % diff -3% 23% 45% >>> >>> Signed-off-by: Joanne Koong >> >> >> IIUC this patch seems to break commit >> 8b284dc47291daf72fe300e1138a2e7ed56f38ab ("fuse: writepages: handle same >> page rewrites"). >> > > Interesting! My understanding was that we only needed that commit > because we were clearing writeback on the original folio before > writeback had actually finished. > > Now that folio writeback state is accounted for normally (eg through > writeback being set/cleared on the original folio), does the > folio_wait_writeback() call we do in fuse_page_mkwrite() not mitigate > this? Yes, after inspecting the writeback logic more, it seems that the second writeback won't be initiated if the first one has not completed yet, see ``` a_ops->writepages write_cache_pages writeback_iter writeback_get_folio folio_prepare_writeback if folio_test_writeback(folio): folio_wait_writeback(folio) ``` and thus it won't be an issue to remove the auxiliary list ;) > >>> - /* >>> - * Being under writeback is unlikely but possible. For example direct >>> - * read to an mmaped fuse file will set the page dirty twice; once when >>> - * the pages are faulted with get_user_pages(), and then after the read >>> - * completed. >>> - */ >> >> In short, the target scenario is like: >> >> ``` >> # open a fuse file and mmap >> fd1 = open("fuse-file-path", ...) >> uaddr = mmap(fd1, ...) >> >> # DIRECT read to the mmaped fuse file >> fd2 = open("ext4-file-path", O_DIRECT, ...) >> read(fd2, uaddr, ...) >> # get_user_pages() of uaddr, and triggers faultin >> # a_ops->dirty_folio() <--- mark PG_dirty >> >> # when DIRECT IO completed: >> # a_ops->dirty_folio() <--- mark PG_dirty > > If you have the direct io function call stack at hand, could you point > me to the function where the direct io completion marks this folio as > dirty? FYI The full call stack is like: ``` # DIRECT read(2) to the mmaped fuse file read(fd2, uaddr1, ...) f_ops->read_iter() (iomap-based ) iomap_dio_rw # for READ && user_backed_iter(iter): dio->flags |= IOMAP_DIO_DIRTY iomap_dio_iter iomap_dio_bio_iter # add user or kernel pages to a bio bio_iov_iter_get_pages ... pin_user_pages_fast(..., FOLL_WRITE, ...) # find corresponding vma of dest buffer (fuse page cache) # search page table (pet) to find corresponding page # if not fault yet, trigger explicit faultin: faultin_page(..., FOLL_WRITE, ...) handle_mm_fault(..., FAULT_FLAG_WRITE) handle_pte_fault do_wp_page (vma->vm_flags & VM_SHARED) wp_page_shared ... fault_dirty_shared_page folio_mark_dirty a_ops->dirty_folio(), i.e., filemap_dirty_folio() # set PG_dirty folio_test_set_dirty(folio) # set PAGECACHE_TAG_DIRTY __folio_mark_dirty # if dio->flags & IOMAP_DIO_DIRTY: bio_set_pages_dirty (for each dest page) folio_mark_dirty a_ops->dirty_folio(), i.e., filemap_dirty_folio() # set PG_dirty folio_test_set_dirty(folio) # set PAGECACHE_TAG_DIRTY __folio_mark_dirty ``` > >> ``` >> >> The auxiliary write request list was introduced to fix this. >> >> I'm not sure if there's an alternative other than the auxiliary list to >> fix it, e.g. calling folio_wait_writeback() in a_ops->dirty_folio() so >> that the same folio won't get dirtied when the writeback has not >> completed yet? >> > > I'm curious how other filesystems solve for this - this seems like a > generic situation other filesystems would run into as well. > As mentioned above, the writeback path will prevent the duplicate writeback request on the same page when the first writeback IO has not completed yet. Sorry for the noise... -- Thanks, Jingbo