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 C84B9C432C0 for ; Tue, 3 Dec 2019 01:13:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9AC0820684 for ; Tue, 3 Dec 2019 01:13:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575335607; bh=pwx2zpy5m+hWbDJf2hRVzLvYGrk9YhD0g6iBdIxmiC8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:List-ID:From; b=k3IcirdzgPqxmgN2JPd2kwrSoxYYj2JNeToqYZtXEc6KdGjhhiwKTuB/QfUYPrkUV gNwmwsKSI86EOtBoUhTsu+f7/bgaz5aScHi4/f5Wc9CSB2bfkxeEKE98zoyQeuPhnr r3mYThKXQX4apshPMA3yVrixLnwankAKu+u6swEU= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725954AbfLCBN1 (ORCPT ); Mon, 2 Dec 2019 20:13:27 -0500 Received: from mail-lf1-f68.google.com ([209.85.167.68]:37777 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725853AbfLCBN0 (ORCPT ); Mon, 2 Dec 2019 20:13:26 -0500 Received: by mail-lf1-f68.google.com with SMTP id b15so1463343lfc.4 for ; Mon, 02 Dec 2019 17:13:25 -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=F4bQ9dyHtN5RH3qkBqbTkJGJWxwNE+6vyCcVHWqRrPbq+nvz6ttCHWrfGjB1iUNzUA J9a5f0KI8nl6mEGJCyEvSgYhh6GMVzEpdS/HwkoZD4hv2F4/WBhVCTvnzmd6I8eTPp1l fN7tRr8E7scTBHm0uWbxwK86wignOQeifaqayNSV22YFussPasgcdSR1KvUlUAS1aid+ ME+JhuE3JILoIGHbo8zyZJ7Wk6sMgWe8phjskT7Yp6cJ++mIZhe+9KZIfY7mKBiLyhBR YPDANO5uxS4bhsnhKzngICwyY/znxbAQvWYGuCA/awWyDNzTPIf8RXxWvx/uH+9KQqwY rw1A== X-Gm-Message-State: APjAAAVwSbJobjHRLJS1BuuSaKVxTL04oz7IIeD785CcTZ9pvGctJu// 0A+yrx+kvixkk9lUiR+n7y5SREnapd8= X-Google-Smtp-Source: APXvYqx/q3iKnvCLCv9kJEK6XrBBX6FGlJIrNcevWjCoAORTkDnhYmvwLDhjOe+rxnFz05hITkukUg== X-Received: by 2002:ac2:53a8:: with SMTP id j8mr1048960lfh.163.1575335604696; Mon, 02 Dec 2019 17:13:24 -0800 (PST) Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com. [209.85.208.171]) by smtp.gmail.com with ESMTPSA id i19sm486173ljj.24.2019.12.02.17.13.24 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Dec 2019 17:13:24 -0800 (PST) Received: by mail-lj1-f171.google.com with SMTP id e28so1714948ljo.9 for ; Mon, 02 Dec 2019 17:13:24 -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-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@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