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 X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2CDF4C432C0 for ; Tue, 3 Dec 2019 01:11:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EE77720684 for ; Tue, 3 Dec 2019 01:11:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575335506; bh=pwx2zpy5m+hWbDJf2hRVzLvYGrk9YhD0g6iBdIxmiC8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:List-ID:From; b=Dv9KXDMnV6lpjg/M3lF7tLWbsww0AvXhM3hYUMhdV/65lQLcc7qzhfdGAlikAuJZ/ NjnMKYMY6WGJgmB2pTc/T+XMLB6wOqWsM6mqyfrTTdnTJDPtydgFFQTpoJ2Pg6Ty4X 3kkxwiLpcU1mQBH7lArY2MXrkh+yHRznzAVf29Ww= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725997AbfLCBLl (ORCPT ); Mon, 2 Dec 2019 20:11:41 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:42553 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725853AbfLCBLl (ORCPT ); Mon, 2 Dec 2019 20:11:41 -0500 Received: by mail-lj1-f196.google.com with SMTP id e28so1711063ljo.9 for ; Mon, 02 Dec 2019 17:11:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=f16wPMX3Hv5+5LpFCWWiK7eqWkk4a1TMh+b2JYuh2L4=; b=KquqAkqUVTyG83FELzXx6nZWqrQom0qPebBeM3l4nUnY0IjRxfw5jl9f4rhB4P0Ys1 yMMjUqHh9Atftq9/aMH7vPLqvTYv5hjkMQeepjJof4arWmddxk+x0kYqi0oEsm1gm01r c0+/cFEMdOFDQe0735Al7ThhTziCMU1XmN/7g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=f16wPMX3Hv5+5LpFCWWiK7eqWkk4a1TMh+b2JYuh2L4=; b=YaMARcJWlRiE6p+BcJZ8oITCZUvnBCbMnz1YV/uR+RH0SA/kpO1W+xknLiRNiL2emP nqD/+LsOZn0LKyFgfbX/mRZYOCX7t9AhWhQ400UIFqAcKc4BiKKXHVljXmBZHTZ9w9NC ZgxKogT60jxpYsKGNsAzcDYQDp4Rc8jk3QxRxw1QVh6yqlCQWdy953eqAjxPD+mCwXo8 c7LTD/3aRBDmOaExiS02DqaFTqUJA6wmze+3zyzaSRue/bG/EDHVNPT5N5l2y5vmltec ZmkB3TS9Y8o4qlobYL6ucGean/qh0AYG8kEnBgA4QsO0Zg9CFlFM6QZV3rOi7HRuh6yL mBTQ== X-Gm-Message-State: APjAAAWj8pXSzBLiTZ+IrGRFXXfpdgQQlJKe7WNxz5DHRfDUFzXxnfyK LZ7ozvpxpkUzy9cSP1NRcAqM06R4GWM= X-Google-Smtp-Source: APXvYqzmjZJ3R5yN5n/77Bb/cBV/+vBUs8+czQKfiyeE3AycvcnnJYyT3Jz0v6WRrDGVpEEWAoTsFw== X-Received: by 2002:a2e:980b:: with SMTP id a11mr924072ljj.189.1575335498600; Mon, 02 Dec 2019 17:11:38 -0800 (PST) Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com. [209.85.208.172]) by smtp.gmail.com with ESMTPSA id y7sm475845ljn.31.2019.12.02.17.11.38 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Dec 2019 17:11:38 -0800 (PST) Received: by mail-lj1-f172.google.com with SMTP id s22so1737360ljs.7 for ; Mon, 02 Dec 2019 17:11:38 -0800 (PST) X-Received: by 2002:a2e:86c4:: with SMTP id n4mr889870ljj.97.1575335193641; Mon, 02 Dec 2019 17:06:33 -0800 (PST) MIME-Version: 1.0 References: <20191129142045.7215-1-agruenba@redhat.com> In-Reply-To: <20191129142045.7215-1-agruenba@redhat.com> From: Linus Torvalds Date: Mon, 2 Dec 2019 17:06:17 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v2] fs: Fix page_mkwrite off-by-one errors To: Andreas Gruenbacher Cc: Christoph Hellwig , "Darrick J. Wong" , Linux Kernel Mailing List , Alexander Viro , Jeff Layton , Sage Weil , Ilya Dryomov , "Theodore Ts'o" , Andreas Dilger , Jaegeuk Kim , Chao Yu , linux-xfs , linux-fsdevel , Richard Weinberger , Artem Bityutskiy , Adrian Hunter , ceph-devel@vger.kernel.org, Ext4 Developers List , linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On Fri, Nov 29, 2019 at 6:21 AM Andreas Gruenbacher wrote: > > +/** > + * page_mkwrite_check_truncate - check if page was truncated > + * @page: the page to check > + * @inode: the inode to check the page against > + * > + * Returns the number of bytes in the page up to EOF, > + * or -EFAULT if the page was truncated. > + */ > +static inline int page_mkwrite_check_truncate(struct page *page, > + struct inode *inode) > +{ > + loff_t size = i_size_read(inode); > + pgoff_t end_index = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; This special end_index calculation seems to be redundant. You later want "size >> PAGE_SHIFT" for another test, and that's actually the important part. The "+ PAGE_SIZE - 1" case is purely to handle the "AT the page boundary is special" case, but since you have to calculate "offset_in_page(size)" anyway, that's entirely redundant - the answer is part of that. So I think it would be better to write the logic as loff_t size = i_size_read(inode); pgoff_t index = size >> PAGE_SHIFT; int offset = offset_in_page(size); if (page->mapping != inode->i_mapping) return -EFAULT; /* Page is wholly past the EOF page */ if (page->index > index) return -EFAULT; /* page is wholly inside EOF */ if (page->index < index) return PAGE_SIZE; /* bytes in a page? If 0, it's past EOF */ return offset ? offset : -PAGE_SIZE; instead. That avoids the unnecessary "round up" part, and simply uses the same EOF index for everything. Linus