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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5073C3DA7D for ; Wed, 4 Jan 2023 00:02:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233130AbjADACi (ORCPT ); Tue, 3 Jan 2023 19:02:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231240AbjADACh (ORCPT ); Tue, 3 Jan 2023 19:02:37 -0500 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9343113CC5 for ; Tue, 3 Jan 2023 16:02:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=TNNR/zoPc6I7wufEnKICiO2nqhp7wSpFpVyOPdHlC60=; b=GO9vTGdj53ux8UhfpGo2TVVO4Y IHid7GyLJsEYUMu5xS9U/AvcoBatEv1OvMZ8nHmbEPxWnA2EUxKABw2AepW9L+V+zAIcpoodbfiHz elr2eEB7agAloChwbJV0wkHkSWgR8c+A+ysbclz7n4zDXjfe3/vuGyvITljbHa7b/MwXuGY2dm4vd cu3NUEBWaBRN1+NCnWWOxWYsI1ozlIiyPnMKIF6wbtZ6YC84BAhlu82Wmf9uzgvwJpG9JWOcvcwGJ j/dX46GHZ+9Z1lNtGk7Bz4cSm4C/xYGN5Ek1fibbP2KsnNI8iKVpKuPKdy60mE3Qx4yjEuPdFeNcd ZSKBdbTw==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1pCrEm-00H9TX-08; Wed, 04 Jan 2023 00:02:32 +0000 Date: Wed, 4 Jan 2023 00:02:31 +0000 From: Al Viro To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, Matthew Wilcox , Christoph Hellwig , Andrew Morton Subject: Re: [PATCH] fs: don't allocate blocks beyond EOF from __mpage_writepage Message-ID: References: <20230103104430.27749-1-jack@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230103104430.27749-1-jack@suse.cz> Sender: Al Viro Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org On Tue, Jan 03, 2023 at 11:44:30AM +0100, Jan Kara wrote: > When __mpage_writepage() is called for a page beyond EOF, it will go and > allocate all blocks underlying the page. This is not only unnecessary > but this way blocks can get leaked (e.g. if a page beyond EOF is marked > dirty but in the end write fails and i_size is not extended). > > Signed-off-by: Jan Kara > --- > fs/mpage.c | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/fs/mpage.c b/fs/mpage.c > index 0f8ae954a579..9f040c1d5912 100644 > --- a/fs/mpage.c > +++ b/fs/mpage.c > @@ -524,6 +524,12 @@ static int __mpage_writepage(struct page *page, struct writeback_control *wbc, > */ > BUG_ON(!PageUptodate(page)); > block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits); > + /* > + * Whole page beyond EOF? Skip allocating blocks to avoid leaking > + * space. > + */ > + if (block_in_file >= (i_size + (1 << blkbits) - 1) >> blkbits) > + goto page_is_mapped; > last_block = (i_size - 1) >> blkbits; Why not simply if (block_in_file > last_block) goto page_is_mapped; after last_block has been calculated?