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 0FAB3C432C0 for ; Tue, 3 Dec 2019 01:13:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D95C220684 for ; Tue, 3 Dec 2019 01:13:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575335609; bh=pwx2zpy5m+hWbDJf2hRVzLvYGrk9YhD0g6iBdIxmiC8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:List-ID:From; b=AAIgpv768t2c/lqRyXuswzW8nXejyjCEfsSnNAEvdp51rBP92eXwT7/XEGVArQXzX VHnON+fTTizj3urHfjYUTE6Um/n+kv5cA05nhKefIEwnPlCDZ7lxPQQAZ/vyCq0+ze 1cE8SwYz9qUglKIXVdQUCtV56wR74z7EDqgXFL1Y= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726024AbfLCBN3 (ORCPT ); Mon, 2 Dec 2019 20:13:29 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:43721 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725853AbfLCBN2 (ORCPT ); Mon, 2 Dec 2019 20:13:28 -0500 Received: by mail-lj1-f196.google.com with SMTP id a13so1706153ljm.10 for ; Mon, 02 Dec 2019 17:13:27 -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=Rvg4ciNAKrjoxh06hRfTULNQ9m1KCrbtwrRbD5pB4Z8sX8SbeLGkN1zKz/YpTSuVKW jmM3KafJzEt71s5BWRLx6/kBlYIlbq5gcc41dxIG/6+7Dc7ZIj2kRx3iwjB4D3Ay/tRH XLiL0bNVIHHyKmKeMbYHLXUtgL/yucVddZH9Il8iC4Rw8GOkJ9VqR1OeexVkw5if+v4e jIx7lu1kzIe6iLp4zNHpc6icP5Rhzp+6BcvzTvoL9w8AZX7H+r8berj2PXu90mtsSdk8 0c/i8Od8sdRlzwAtpEu6lqGfzDGHMn8a3KDTzJOwGPHmI96kpjerTJ5Vs7TxWKXt6bde SEYQ== X-Gm-Message-State: APjAAAXHm3RE+Sn1sOIEtDpwYC5QnJjxYxfwDkLZ67no6vu8MFK36zbs vZM7EQDY73z5cbOo6J1oUv1ndmCn2eg= X-Google-Smtp-Source: APXvYqy1WrT5gtwhYSd9U4kYGCswiIS2BM8RqJwz1GKcH+IFx+md6Ck61tU76F2VKRHBKAIAkceMHA== X-Received: by 2002:a2e:b537:: with SMTP id z23mr932458ljm.129.1575335606540; Mon, 02 Dec 2019 17:13:26 -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 v5sm466692ljk.67.2019.12.02.17.13.26 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Dec 2019 17:13:26 -0800 (PST) Received: by mail-lj1-f172.google.com with SMTP id e28so1715011ljo.9 for ; Mon, 02 Dec 2019 17:13:26 -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-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@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