Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3760746pxt; Tue, 10 Aug 2021 10:41:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXCLMbmWzjpbK4qJ5KKVdN1Bda8/8rQKkoPQUxJMRtWufy14NDd+Ga7N2oyritU4qCBQpC X-Received: by 2002:a92:cd49:: with SMTP id v9mr240700ilq.142.1628617287738; Tue, 10 Aug 2021 10:41:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628617287; cv=none; d=google.com; s=arc-20160816; b=QhKCHuV0vjcowr6IvA5kQORZhByb3YzvQqa/5BbYmfoOQOIMV87fhEXjyUrz0UBOnA cYkig/tDPp/NmcvvK+Vz53qyuq4BEv5V/zhIL1ksZ20uxLBVhETPwtui62YJGz6QD8bR 3Chs1LvjCYqLxgnVXL4Gha9ZlnU9PfCVoC8hDpMI6IY1PdzTZY8GZpv0TLGvgSXZ5QjW P8PnyEs0Xz4RyESOsVnJ8cPS7v8YnTmUnmXFuy+CfLhUdyLYoeuDVqh/LnGk/DuQNwE6 DhHbCmDA7WhmQ3xkwYtENEkK1a7/fZRaOdPp/VMQ46+3VdXtviWEwwB3flSp5yd9snZh aFmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kZVIvZ9AwzbIzsvQ5qmPsJasiVBWPjfBaS6w9ixcSsw=; b=fBinZjvKgYtTu+Teawepw7Vxb/PSXehn8CYeTpyjivQOTA6fElVJdC0w5VY5rSygut WpXHBrVLHsm1/wIy3guZy0xZcX1ymrYHfEdcqUecl8g94/jJNOev2QlOVqPNZfnu+c/4 eMo60oHXeJRQKjBxfIs/yF1trOkSNoY0U2/c8cAGmBL5Gh7dtQqcJ3f60zWQxHoY2M5e PISjxQvWhPt/oy6JelrxVG6+MJze5JoanHx44mWX7WQi1iDhlbvGRdJU3h3qBvFQ6FII zpHNcndIjr/JqGyp+xqz9zJgEd1SydeelF2hBayZEwEiDKdhIryd9Bj2gLOdZvNOfPmw au9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="ItW/PH5G"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r8si21814658jar.122.2021.08.10.10.41.16; Tue, 10 Aug 2021 10:41:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="ItW/PH5G"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234747AbhHJRkx (ORCPT + 99 others); Tue, 10 Aug 2021 13:40:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:43106 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235081AbhHJRjT (ORCPT ); Tue, 10 Aug 2021 13:39:19 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 32946610A0; Tue, 10 Aug 2021 17:36:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628617013; bh=RABCGUc/EXegW3+JRJjZfkZkcxe79Tvl1euXjL0Bkp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ItW/PH5GXADg6/fBnOC/gdHxbysdKQiNRz8KsTBLJsi23jW5M+cvvrr2fX85OPQIz X2EVpnU97WKqgxeax3Ju7so0wby2C0Uryks0Zs8hEpD7oO0lXrZxxcW9bo37Vutzel eI5zyy6G/IzDVkM9lW+Tp/Nnu7Mbl0lLCuZr+euw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Alex Xu (Hello71)" , Linus Torvalds Subject: [PATCH 5.4 59/85] pipe: increase minimum default pipe size to 2 pages Date: Tue, 10 Aug 2021 19:30:32 +0200 Message-Id: <20210810172950.227398327@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810172948.192298392@linuxfoundation.org> References: <20210810172948.192298392@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alex Xu (Hello71) commit 46c4c9d1beb7f5b4cec4dd90e7728720583ee348 upstream. This program always prints 4096 and hangs before the patch, and always prints 8192 and exits successfully after: int main() { int pipefd[2]; for (int i = 0; i < 1025; i++) if (pipe(pipefd) == -1) return 1; size_t bufsz = fcntl(pipefd[1], F_GETPIPE_SZ); printf("%zd\n", bufsz); char *buf = calloc(bufsz, 1); write(pipefd[1], buf, bufsz); read(pipefd[0], buf, bufsz-1); write(pipefd[1], buf, 1); } Note that you may need to increase your RLIMIT_NOFILE before running the program. Fixes: 759c01142a ("pipe: limit the per-user amount of pages allocated in pipes") Cc: Link: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/ Link: https://lore.kernel.org/lkml/1628127094.lxxn016tj7.none@localhost/ Signed-off-by: Alex Xu (Hello71) Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/pipe.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) --- a/fs/pipe.c +++ b/fs/pipe.c @@ -31,6 +31,21 @@ #include "internal.h" /* + * New pipe buffers will be restricted to this size while the user is exceeding + * their pipe buffer quota. The general pipe use case needs at least two + * buffers: one for data yet to be read, and one for new data. If this is less + * than two, then a write to a non-empty pipe may block even if the pipe is not + * full. This can occur with GNU make jobserver or similar uses of pipes as + * semaphores: multiple processes may be waiting to write tokens back to the + * pipe before reading tokens: https://lore.kernel.org/lkml/1628086770.5rn8p04n6j.none@localhost/. + * + * Users can reduce their pipe buffers with F_SETPIPE_SZ below this at their + * own risk, namely: pipe writes to non-full pipes may block until the pipe is + * emptied. + */ +#define PIPE_MIN_DEF_BUFFERS 2 + +/* * The max size that a non-root user is allowed to grow the pipe. Can * be set by root in /proc/sys/fs/pipe-max-size */ @@ -666,8 +681,8 @@ struct pipe_inode_info *alloc_pipe_info( user_bufs = account_pipe_buffers(user, 0, pipe_bufs); if (too_many_pipe_buffers_soft(user_bufs) && is_unprivileged_user()) { - user_bufs = account_pipe_buffers(user, pipe_bufs, 1); - pipe_bufs = 1; + user_bufs = account_pipe_buffers(user, pipe_bufs, PIPE_MIN_DEF_BUFFERS); + pipe_bufs = PIPE_MIN_DEF_BUFFERS; } if (too_many_pipe_buffers_hard(user_bufs) && is_unprivileged_user())