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 BFB1BC4345F for ; Thu, 2 May 2024 04:11:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A762B6B007B; Thu, 2 May 2024 00:11:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A24F46B0082; Thu, 2 May 2024 00:11:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 93B9F6B0083; Thu, 2 May 2024 00:11:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 706116B007B for ; Thu, 2 May 2024 00:11:56 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E4A728087D for ; Thu, 2 May 2024 04:11:55 +0000 (UTC) X-FDA: 82072132590.01.C57C636 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by imf03.hostedemail.com (Postfix) with ESMTP id 4DF8420004 for ; Thu, 2 May 2024 04:11:54 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZlM5QdLZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of ritesh.list@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=ritesh.list@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1714623114; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to:references:dkim-signature; bh=d/pO3KnwV5/hmCMrLfPzaasia4xkM57wUS++D9NkkGM=; b=p0nBBYg68harESwJ0Muhiu5umlz2i2Ki4lJNoLZ/DCDA4ID6cgdCedhtBqyB3pLYJbR00y L/7lhCFs2zqXjDTQWQ0nF9bbWt3KarhcJTXgBxHqICNhnCanCVwkHWLIhu+laFTkgbnjaZ a4qnhcB2kaLBIznKZj0R8/Cd6Xq1puk= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=ZlM5QdLZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of ritesh.list@gmail.com designates 209.85.210.169 as permitted sender) smtp.mailfrom=ritesh.list@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1714623114; a=rsa-sha256; cv=none; b=FcExA3p1CZFONGdCiLR4EbUe6gi8UElN+1uJFMEfoBbKs4f9AdHYEUe6FPGYE1N/mq4y9d R4OuK+zvI4UgO20d+7FhUBgrMV7xLjh9dmv8IapUntzHan3OPN6Lnymc+7OGiqhCdBZvgy MyOEJgXFLPT3fVTL835czzG98Ig7plQ= Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6f26588dd5eso6344302b3a.0 for ; Wed, 01 May 2024 21:11:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714623113; x=1715227913; darn=kvack.org; h=in-reply-to:subject:cc:to:from:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=d/pO3KnwV5/hmCMrLfPzaasia4xkM57wUS++D9NkkGM=; b=ZlM5QdLZcOyQkPaeqY8QBaudS0ul092HM3xJytKYDFMb52JaY6RTjEaobuu37Ix8lg iOQnTtSttMV2Q6WdMDZdenn9e/EDkI5OGXsCUom+JjuRaoKHC8av79yvRcgI9VQLrejT 7NLsyOEry0Ijg7GD/hLbF3/WnEwmPMb47mTFniiSE9CfrrXemB2qZ5DaoP1XxsQoqNPS veBj9jQ1ETvRRnutHHIc+1EipUA68kBaY4PhoBByRkm/9KcAdBhbEhCZlUC/XniOX2JY mt9E80V8wnBX0huTdq9XrQQ87sJTE9RCT64uQheq0thJe/IZwSVCLNNlf3RP2YbwqEqj EZAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714623113; x=1715227913; h=in-reply-to:subject:cc:to:from:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=d/pO3KnwV5/hmCMrLfPzaasia4xkM57wUS++D9NkkGM=; b=b+yzmn3ZM9QB527kicNVjUcFwh9So5JKZb46AytDhCqNdbtSfmwhQsLOv4Xng7m+/V 5OAbBt0P7HTEELoVi0N3+ro/HoxBny9H88uq5vtUTtBLY3EqMqWzjE9jOQV8QJBJihcq USiTdhutnEoQUFrikpq1UGKxRhDsras63Feh1j7yfHlTXgDY2wPsKuRZQmOSlTPlzbGf 5KNFzIoa7Hjv9KqrRxgz+fmpjlBf24IxiQcxk15SAcGE1V7pPW521CzpgwEe0IVVT5bt wIUUa/EsjGgJYWz0GPGhklKMUDbJB/wCo8Fvn/MHQC9rPv8gCtJ3NJjLItXdoCIFe8LC Y16w== X-Forwarded-Encrypted: i=1; AJvYcCWcToUyoyJF6lb2wyjV85kZ/McxMTn8eOyo1j5snDOEki25nRZiPolodGvEBZfDeIsz6w8V/MAQnaYpNiDXgmGeZJM= X-Gm-Message-State: AOJu0Yy6O5943qwiQHTIxljvc3hwBmCfTDjYjearqIsRP4/KUO2d9VRO bQyX6ZqbhGmeuUxgUB9fQmMEPt3uGNykTxMAhDWeFezQCJhSjEf2 X-Google-Smtp-Source: AGHT+IHBezvM4qNTsf04QOHARohIj7h1mUhy1D38HBCSojZX6TJxw+sBESjB8870/wRg2sNyBm0Xjw== X-Received: by 2002:a05:6a21:8189:b0:1af:63f2:bc62 with SMTP id pd9-20020a056a21818900b001af63f2bc62mr4674374pzb.15.1714623112936; Wed, 01 May 2024 21:11:52 -0700 (PDT) Received: from dw-tp ([129.41.58.7]) by smtp.gmail.com with ESMTPSA id b11-20020a170902d50b00b001eb2f4648d3sm169228plg.228.2024.05.01.21.11.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 21:11:52 -0700 (PDT) Date: Thu, 02 May 2024 09:41:39 +0530 Message-Id: <87a5l8am4k.fsf@gmail.com> From: Ritesh Harjani (IBM) To: Dave Chinner Cc: Zhang Yi , linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.cz, hch@infradead.org, djwong@kernel.org, willy@infradead.org, zokeefe@google.com, yi.zhang@huawei.com, chengzhihao1@huawei.com, yukuai3@huawei.com, wangkefeng.wang@huawei.com Subject: Re: [PATCH v4 02/34] ext4: check the extent status again before inserting delalloc block In-Reply-To: X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4DF8420004 X-Stat-Signature: sotef8fcsj7b4frsfd5p9ioa8zcykdts X-Rspam-User: X-HE-Tag: 1714623114-294141 X-HE-Meta: U2FsdGVkX1+wL03IO9xmabgTZD+yj6HN16Lo9TMib8p5Q4SOyEM9iGfHlJyPP4of5nqOsR4v7QniiE2QTLGZE1TyfoYGd0+4vH1O7ytTVtfTcBCogzUbSEBzWMRMDPcuXsMwd2+gAWle+tdboL3MT+kOiaRB00x5DbPJMctFXtQ6r2alTN2KsIAX4e9o4T2j/vuEmb4ZtcjWuf/Fy37gKjceannYUR1FkUdvKww75HfbQkWzQgpxMbG94K/urBc1eDqcyfaXcncxnl8NL+BDI+UEgUHoLePSKk7JUKPVxdfF/bPRnjjaBQVtjNxBgceYq3hZjazuLZPsyaVTZh54SHUdwOl+1btd0vjGjX3t0kCzTkQkJbIyq+1GjNAMuAR2IQLKuWLZWMBcGRsBGLFmnQ9gOYBN9I/QVLsoVYi1dS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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: Dave Chinner writes: > On Wed, May 01, 2024 at 05:49:50PM +0530, Ritesh Harjani wrote: >> Dave Chinner writes: >> >> > On Wed, Apr 10, 2024 at 10:29:16PM +0800, Zhang Yi wrote: >> >> From: Zhang Yi >> >> >> >> Now we lookup extent status entry without holding the i_data_sem before >> >> inserting delalloc block, it works fine in buffered write path and >> >> because it holds i_rwsem and folio lock, and the mmap path holds folio >> >> lock, so the found extent locklessly couldn't be modified concurrently. >> >> But it could be raced by fallocate since it allocate block whitout >> >> holding i_rwsem and folio lock. >> >> >> >> ext4_page_mkwrite() ext4_fallocate() >> >> block_page_mkwrite() >> >> ext4_da_map_blocks() >> >> //find hole in extent status tree >> >> ext4_alloc_file_blocks() >> >> ext4_map_blocks() >> >> //allocate block and unwritten extent >> >> ext4_insert_delayed_block() >> >> ext4_da_reserve_space() >> >> //reserve one more block >> >> ext4_es_insert_delayed_block() >> >> //drop unwritten extent and add delayed extent by mistake >> > >> > Shouldn't this be serialised by the file invalidation lock? Hole >> > punching via fallocate must do this to avoid data use-after-free >> > bugs w.r.t racing page faults and all the other fallocate ops need >> > to serialise page faults to avoid page cache level data corruption. >> > Yet here we see a problem resulting from a fallocate operation >> > racing with a page fault.... >> >> IIUC, fallocate operations which invalidates the page cache contents needs >> to take th invalidate_lock in exclusive mode to prevent page fault >> operations from loading pages for stale mappings (blocks which were >> marked free might get reused). This can cause stale data exposure. >> >> Here the fallocate operation require allocation of unwritten extents and >> does not require truncate of pagecache range. So I guess, it is not >> strictly necessary to hold the invalidate lock here. > > True, but you can make exactly the same argument for write() vs > fallocate(). Yet this path in ext4_fallocate() locks out > concurrent write()s and waits for DIOs in flight to drain. What > makes buffered writes triggered by page faults special? > > i.e. if you are going to say "we don't need serialisation between > writes and fallocate() allocating unwritten extents", then why is it > still explicitly serialising against both buffered and direct IO and > not just truncate and other fallocate() operations? > >> But I see XFS does take IOLOCK_EXCL AND MMAPLOCK_EXCL even for this operation. > > Yes, that's the behaviour preallocation has had in XFS since we > introduced the MMAPLOCK almost a decade ago. This was long before > the file_invalidation_lock() was even a glimmer in Jan's eye. > > btrfs does the same thing, for the same reasons. COW support makes > extent tree manipulations excitingly complex at times... > >> I guess we could use the invalidate lock for fallocate operation in ext4 >> too. However, I think we still require the current patch. The reason is >> ext4_da_map_blocks() call here first tries to lookup the extent status >> cache w/o any i_data_sem lock in the fastpath. If it finds a hole, it >> takes the i_data_sem in write mode and just inserts an entry into extent >> status cache w/o re-checking for the same under the exclusive lock. >> ...So I believe we still should have this patch which re-verify under >> the write lock if whether any other operation has inserted any entry >> already or not. > > Yup, I never said the code in the patch is wrong or unnecessary; I'm > commenting on the high level race condition that lead to the bug > beting triggered. i.e. that racing data modification operations with > low level extent manipulations is often dangerous and a potential > source of very subtle, hard to trigger, reproduce and debug issues > like the one reported... > Yes, thanks for explaining and commenting on the high level design. It was indeed helpful. And I agree with your comment on, we can refactor out the common operations from fallocate path and use invalidate lock to protect against data modification (page fault) and extent manipulation path (fallocate operations). -ritesh