Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2254033imm; Thu, 7 Jun 2018 07:45:55 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLeCwINuwIDknX9d7sK0KW9EgHV9PDiMjAeyo5a6Sq80wKW+upfbZtFguwxLsWhgafpiAKG X-Received: by 2002:a17:902:6102:: with SMTP id t2-v6mr2416807plj.392.1528382755793; Thu, 07 Jun 2018 07:45:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528382755; cv=none; d=google.com; s=arc-20160816; b=WLjoNHix6yzen8jaerxfLXTQ+irYZXEhJV0MwGIR3GYFUc07kvzFfKM5TUM2ZHZZsr YAy8ztFsZOZ7rSQ6O53Bi5Dc1gspCe9yux0D5Wh5W/WNVMmhHpm8FZfMMmH9k6ICzYJC rFKHcqxAV4u8fwTqesUTSriaz9qQjryJdxIjogkAVvbnFy11x/Cgc7uZLgFKGoBpBSSU 8ajeHONrOrOR3A2ZGP3WC3jwa6GMgX++dys8BLHFCDjyXZ7/3sbq7zmzlHu0S3ZJ781C 13HoqKZwkOBRfTdyQU6kecD2Us6jmTGTwTu0nOwmC+UmvvBstgWRlnBL4rwv+vMG92Jz 5MYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=Ax9uCo6RiLv2TSkBpYkWyzoqvOlsYe7v43K0aVeoAkc=; b=N3ST5afKdfkJNDNQeavY9tIQFGusLYmo+27yskpNWrLqPwkTVCo+BYvRXMUjEY65i8 Ei0i0Hx1HM0vs/Iu5YMdTSdwzTJwRPx1nic8aMXQZoQ1+4/Wfcg2ZmYfjsbGF6/GaNJM 8GmswY931E4CKdK2YnYWW4o0RTnVNzTVjPLTa90LyWtnlCyokucDylwlMqh69yFPuid9 Ak6vDYXddNZ7gnizjWwEvXQwLa/Yr5p8hAWKKj0GWom1MrccrWC8raWd3Q0jBChDL4YW r86LkvBPmahm1C87PbnXurr/jFAR3YnXy+ONPWc6OFoq3zZ+jl9d9SA9LhKpAV0ygW2g 74IA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c2-v6si30709005pgp.147.2018.06.07.07.45.41; Thu, 07 Jun 2018 07:45:55 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934881AbeFGOnz (ORCPT + 99 others); Thu, 7 Jun 2018 10:43:55 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:40696 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934145AbeFGOnx (ORCPT ); Thu, 7 Jun 2018 10:43:53 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbg-0005Zk-Ex; Thu, 07 Jun 2018 15:09:40 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvb6-0002zs-50; Thu, 07 Jun 2018 15:09:04 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "" , "Al Viro" , "Willy Tarreau" , "Jens Axboe" , "Vegard Nossum" , "Tetsuo Handa" , "Michael Kerrisk (man-pages)" , "Linus Torvalds" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 205/410] pipe: make account_pipe_buffers() return a value, and use it In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: "Michael Kerrisk (man-pages)" commit 9c87bcf0a31b338dc8a69a5d251a037565a94e13 upstream. This is an optional patch, to provide a small performance improvement. Alter account_pipe_buffers() so that it returns the new value in user->pipe_bufs. This means that we can refactor too_many_pipe_buffers_soft() and too_many_pipe_buffers_hard() to avoid the costs of repeated use of atomic_long_read() to get the value user->pipe_bufs. Link: http://lkml.kernel.org/r/93e5f193-1e5e-3e1f-3a20-eae79b7e1310@gmail.com Signed-off-by: Michael Kerrisk Reviewed-by: Vegard Nossum Cc: Willy Tarreau Cc: Cc: Tetsuo Handa Cc: Jens Axboe Cc: Al Viro Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Ben Hutchings --- fs/pipe.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) --- a/fs/pipe.c +++ b/fs/pipe.c @@ -590,22 +590,20 @@ pipe_fasync(int fd, struct file *filp, i return retval; } -static void account_pipe_buffers(struct user_struct *user, +static unsigned long account_pipe_buffers(struct user_struct *user, unsigned long old, unsigned long new) { - atomic_long_add(new - old, &user->pipe_bufs); + return atomic_long_add_return(new - old, &user->pipe_bufs); } -static bool too_many_pipe_buffers_soft(struct user_struct *user) +static bool too_many_pipe_buffers_soft(unsigned long user_bufs) { - return pipe_user_pages_soft && - atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_soft; + return pipe_user_pages_soft && user_bufs >= pipe_user_pages_soft; } -static bool too_many_pipe_buffers_hard(struct user_struct *user) +static bool too_many_pipe_buffers_hard(unsigned long user_bufs) { - return pipe_user_pages_hard && - atomic_long_read(&user->pipe_bufs) >= pipe_user_pages_hard; + return pipe_user_pages_hard && user_bufs >= pipe_user_pages_hard; } struct pipe_inode_info *alloc_pipe_info(void) @@ -613,19 +611,20 @@ struct pipe_inode_info *alloc_pipe_info( struct pipe_inode_info *pipe; unsigned long pipe_bufs = PIPE_DEF_BUFFERS; struct user_struct *user = get_current_user(); + unsigned long user_bufs; pipe = kzalloc(sizeof(struct pipe_inode_info), GFP_KERNEL); if (pipe == NULL) goto out_free_uid; - account_pipe_buffers(user, 0, pipe_bufs); + user_bufs = account_pipe_buffers(user, 0, pipe_bufs); - if (too_many_pipe_buffers_soft(user)) { - account_pipe_buffers(user, pipe_bufs, 1); + if (too_many_pipe_buffers_soft(user_bufs)) { + user_bufs = account_pipe_buffers(user, pipe_bufs, 1); pipe_bufs = 1; } - if (too_many_pipe_buffers_hard(user)) + if (too_many_pipe_buffers_hard(user_bufs)) goto out_revert_acct; pipe->bufs = kcalloc(pipe_bufs, sizeof(struct pipe_buffer), @@ -641,7 +640,7 @@ struct pipe_inode_info *alloc_pipe_info( } out_revert_acct: - account_pipe_buffers(user, pipe_bufs, 0); + (void) account_pipe_buffers(user, pipe_bufs, 0); kfree(pipe); out_free_uid: free_uid(user); @@ -652,7 +651,7 @@ void free_pipe_info(struct pipe_inode_in { int i; - account_pipe_buffers(pipe->user, pipe->buffers, 0); + (void) account_pipe_buffers(pipe->user, pipe->buffers, 0); free_uid(pipe->user); for (i = 0; i < pipe->buffers; i++) { struct pipe_buffer *buf = pipe->bufs + i; @@ -1022,6 +1021,7 @@ static long pipe_set_size(struct pipe_in { struct pipe_buffer *bufs; unsigned int size, nr_pages; + unsigned long user_bufs; long ret = 0; size = round_pipe_size(arg); @@ -1041,11 +1041,11 @@ static long pipe_set_size(struct pipe_in size > pipe_max_size && !capable(CAP_SYS_RESOURCE)) return -EPERM; - account_pipe_buffers(pipe->user, pipe->buffers, nr_pages); + user_bufs = account_pipe_buffers(pipe->user, pipe->buffers, nr_pages); if (nr_pages > pipe->buffers && - (too_many_pipe_buffers_hard(pipe->user) || - too_many_pipe_buffers_soft(pipe->user)) && + (too_many_pipe_buffers_hard(user_bufs) || + too_many_pipe_buffers_soft(user_bufs)) && !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { ret = -EPERM; goto out_revert_acct; @@ -1096,7 +1096,7 @@ static long pipe_set_size(struct pipe_in return nr_pages * PAGE_SIZE; out_revert_acct: - account_pipe_buffers(pipe->user, nr_pages, pipe->buffers); + (void) account_pipe_buffers(pipe->user, nr_pages, pipe->buffers); return ret; }