From patchwork Mon Jan 8 05:35:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10149057 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 73C0E601BE for ; Mon, 8 Jan 2018 05:40:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6789C287A6 for ; Mon, 8 Jan 2018 05:40:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5C62B287BE; Mon, 8 Jan 2018 05:40:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 16E38287A6 for ; Mon, 8 Jan 2018 05:40:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755490AbeAHFj7 (ORCPT ); Mon, 8 Jan 2018 00:39:59 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:40541 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755527AbeAHFi4 (ORCPT ); Mon, 8 Jan 2018 00:38:56 -0500 Received: by mail-pg0-f65.google.com with SMTP id q12so4880582pgt.7; Sun, 07 Jan 2018 21:38:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PxOy7qU4nHVI/Rgh/NWmZAbamnyK/oMuXrxHQttmEIs=; b=YL5XLDxeT0xD7pGMkebwlkmZQfUtHtnQu9W6ZMLyDGtYJ8ze3X1HU0NsFW7Y2nBP9k jF8swtvz5PjBcjWgEQ4hpc7Rq+5cmG5855i1/12nGLl8JskII/FcEDFvMy7GeheGPLvz vtLoHWFFoBM+Cwntky1cA3CDt9qrBxH8QFcr8wlYcvzna/rvTJAOGGLT9kXvkCYEaBwg 00l91p7NfeX8vu2nB1DJ7nwTY2Adduo8V44OJ+HQTRAdhtqmHgZeMUnpf0QC6BK7mHAZ bwzVQrX/Z45JuCE6ehEu8BF491q1OfghYdQrDcHrrEnXPzpvI8f70iNyDdgfV4z8c/jn LLDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PxOy7qU4nHVI/Rgh/NWmZAbamnyK/oMuXrxHQttmEIs=; b=cOuxYucJLE0K+mZH4pXkl4OTwtPwjEA1twKD9c9X+Ghc+PGRO4trIA6D6JoQK5xfI9 ATJZNfE5IB6frgwknjUULWwF7zc1a3YK2aQHI/bQlS8XgyRKLYZHMGuS4mT+GtnI4Vk2 WVpw7Hut0WZMDlKTjZrL4GHbVGX7zE9AhqxDKIRsYCYxKySvV6Df9H9sMiUH/NwQLEiK 7lv1CA6IjWgDxSVJugAzL4lSXNlNyHtj4Uo418zeflrUv45HEzV88UuvByU+9h4TiuLK lxbZrTmUTvTZRPB9cQRMGeFZym6Oj2GD+AES1R/uj8dBvSGivwrjSAPNTZwhsZ6weSC2 wDww== X-Gm-Message-State: AKGB3mLbaono2NUHnT0H+kZB+rI2qbzATFQPIabthP1Dq8dbuSo8zpMl XCN+LiZbit83wDV5MV5pAK4TkRWU X-Google-Smtp-Source: ACJfBovaCiC0QXhDK2ppq9W+f258IqaszsgGLANzjNb1Pd5O9p8WKG3Fr9lpAbnFCwJExVTMTMgnUg== X-Received: by 10.99.125.70 with SMTP id m6mr8559179pgn.415.1515389935550; Sun, 07 Jan 2018 21:38:55 -0800 (PST) Received: from zzz.localdomain (c-67-185-97-198.hsd1.wa.comcast.net. [67.185.97.198]) by smtp.gmail.com with ESMTPSA id w84sm26611743pfk.16.2018.01.07.21.38.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 07 Jan 2018 21:38:54 -0800 (PST) From: Eric Biggers To: linux-fsdevel@vger.kernel.org Cc: Alexander Viro , Joe Lawrence , Michael Kerrisk , Willy Tarreau , Mikulas Patocka , "Luis R . Rodriguez" , Kees Cook , linux-kernel@vger.kernel.org, Eric Biggers Subject: [PATCH 6/7] pipe: simplify round_pipe_size() Date: Sun, 7 Jan 2018 21:35:41 -0800 Message-Id: <20180108053542.6472-7-ebiggers3@gmail.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180108053542.6472-1-ebiggers3@gmail.com> References: <20180108053542.6472-1-ebiggers3@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers round_pipe_size() calculates the number of pages the requested size corresponds to, then rounds the page count up to the next power of 2. However, it also rounds everything < PAGE_SIZE up to PAGE_SIZE. Therefore, there's no need to actually translate the size into a page count; we just need to round the size up to the next power of 2. We do need to verify that bit 31 isn't set, since on 32-bit systems roundup_pow_of_two() would be undefined in that case. But that can just be combined with the UINT_MAX check which we need anyway now. Finally, also remove the check for '!nr_pages' in pipe_set_size(), since round_pipe_size() always returns either 0 or a multiple of PAGE_SIZE. Signed-off-by: Eric Biggers --- fs/pipe.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/fs/pipe.c b/fs/pipe.c index f1ee1e599495..774cafd947dc 100644 --- a/fs/pipe.c +++ b/fs/pipe.c @@ -1022,20 +1022,14 @@ const struct file_operations pipefifo_fops = { */ unsigned int round_pipe_size(unsigned long size) { - unsigned long nr_pages; - - if (size > UINT_MAX) + if (size > (1U << 31)) return 0; /* Minimum pipe size, as required by POSIX */ if (size < PAGE_SIZE) - size = PAGE_SIZE; - - nr_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; - if (nr_pages == 0) - return 0; + return PAGE_SIZE; - return roundup_pow_of_two(nr_pages) << PAGE_SHIFT; + return roundup_pow_of_two(size); } /* @@ -1054,9 +1048,6 @@ static long pipe_set_size(struct pipe_inode_info *pipe, unsigned long arg) return -EINVAL; nr_pages = size >> PAGE_SHIFT; - if (!nr_pages) - return -EINVAL; - /* * If trying to increase the pipe capacity, check that an * unprivileged user is not trying to exceed various limits