Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5615232img; Wed, 27 Mar 2019 11:45:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqx+5um/f/gZbK6eGjEKc/UoN1CiQ2pzDJFgZzNWp9ZrYbJMctzGs40zT+tHA13x/08Xrz2v X-Received: by 2002:a17:902:a7:: with SMTP id a36mr38853917pla.267.1553712353876; Wed, 27 Mar 2019 11:45:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553712353; cv=none; d=google.com; s=arc-20160816; b=CVzaHUpZec5x8jVwdt3eP3GSjS+PzzD18RD1DBzNgdDEyESX3XbyWPOOEWr5cUV79B oMDF41lvBVOPGH+E5SbiTf7e8HJC0u6qWwhh/F+dvtmqgNJKdYiMI0dYSQaHWNXOPna7 j0uEH8u9hcuUdKX5ela60rh6lEh1eZYLa9vFDOTHYmkBTYDK0ipZOMvTegEz6Ft1Qtdi 1CxRZEr/JN39eraLvvkGdCm4ueN7AOn38NfpXVYdmiuGcBp0P+KmBv5mCBwCnfFTenL8 YgdmnobuhvPeM1HyeOseD7qbfZRu8lew9DfMpL90bbp+qqp2o0hVK1B+DJf8It4IBPuC 1hcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=D9QeXI2FAzhtvZT7xvHhreWaqBNzRZwHLPQmPL19C88=; b=KA54suSXxMK9zg7EHXeju1qMi+RV8AYVDX0Os/o5wbhNcMnjf4V34eXQxFhSpFk6DI +0Y1M0TZLLtr/IRVxpwy50dB8hGk8oBJVDMcsb2lMgyiww68budMqehC11poWw23G9OJ rZFa7B2EjUwDKZ9htCQDguVGUtEn1DL3tRxf2LAus3K86MlmcWXdekcxiIOV5AvSFMOZ 8eVzGXebPzApE1+GS5ACogTz81lMuC8sB1Bnhqt4R5YoNQ+aolQuxmKFV1Z1DFTXAJH1 rGMpz9REj6oOk5IksNE30vGag7RbGIPN63VxGM/KvGJwkqFGeeo+FDNOoDEcGC/rLHNI 35og== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=y3f0QN7z; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t9si11370691plo.98.2019.03.27.11.45.38; Wed, 27 Mar 2019 11:45:53 -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; dkim=pass header.i=@kernel.org header.s=default header.b=y3f0QN7z; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391122AbfC0SVS (ORCPT + 99 others); Wed, 27 Mar 2019 14:21:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:39090 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390815AbfC0SVR (ORCPT ); Wed, 27 Mar 2019 14:21:17 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D2B592063F; Wed, 27 Mar 2019 18:21:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710875; bh=yAD3eSUVcOOYvdL71e0GH7lPTLlbQQUtUa7H+DHR3GM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y3f0QN7zhP4eqSKgKYJXtuW6KjoA2nEOnSd+adTd1Xu4gJ6vWR7Lw2jiRJqtWIh0m ZEBmgiIUoiIwaPT196Vf4H1lfJcKXaq0jlNeZmc+nKzY6RMOYh86xjB/Z+RdVchqsc LJSU/tlyU17BRhw9QZ29+eT2+bgv1Fwa/hjgmr8o= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Slavomir Kaslev , Linus Torvalds , Sasha Levin , linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 4.9 20/87] fs: Make splice() and tee() take into account O_NONBLOCK flag on pipes Date: Wed, 27 Mar 2019 14:19:33 -0400 Message-Id: <20190327182040.17444-20-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327182040.17444-1-sashal@kernel.org> References: <20190327182040.17444-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Slavomir Kaslev [ Upstream commit ee5e001196d1345b8fee25925ff5f1d67936081e ] The current implementation of splice() and tee() ignores O_NONBLOCK set on pipe file descriptors and checks only the SPLICE_F_NONBLOCK flag for blocking on pipe arguments. This is inconsistent since splice()-ing from/to non-pipe file descriptors does take O_NONBLOCK into consideration. Fix this by promoting O_NONBLOCK, when set on a pipe, to SPLICE_F_NONBLOCK. Some context for how the current implementation of splice() leads to inconsistent behavior. In the ongoing work[1] to add VM tracing capability to trace-cmd we stream tracing data over named FIFOs or vsockets from guests back to the host. When we receive SIGINT from user to stop tracing, we set O_NONBLOCK on the input file descriptor and set SPLICE_F_NONBLOCK for the next call to splice(). If splice() was blocked waiting on data from the input FIFO, after SIGINT splice() restarts with the same arguments (no SPLICE_F_NONBLOCK) and blocks again instead of returning -EAGAIN when no data is available. This differs from the splice() behavior when reading from a vsocket or when we're doing a traditional read()/write() loop (trace-cmd's --nosplice argument). With this patch applied we get the same behavior in all situations after setting O_NONBLOCK which also matches the behavior of doing a read()/write() loop instead of splice(). This change does have potential of breaking users who don't expect EAGAIN from splice() when SPLICE_F_NONBLOCK is not set. OTOH programs that set O_NONBLOCK and don't anticipate EAGAIN are arguably buggy[2]. [1] https://github.com/skaslev/trace-cmd/tree/vsock [2] https://github.com/torvalds/linux/blob/d47e3da1759230e394096fd742aad423c291ba48/fs/read_write.c#L1425 Signed-off-by: Slavomir Kaslev Reviewed-by: Steven Rostedt (VMware) Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- fs/splice.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fs/splice.c b/fs/splice.c index 01983bea760c..3f9e7a724d85 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -1137,6 +1137,9 @@ static long do_splice(struct file *in, loff_t __user *off_in, if (ipipe == opipe) return -EINVAL; + if ((in->f_flags | out->f_flags) & O_NONBLOCK) + flags |= SPLICE_F_NONBLOCK; + return splice_pipe_to_pipe(ipipe, opipe, len, flags); } @@ -1162,6 +1165,9 @@ static long do_splice(struct file *in, loff_t __user *off_in, if (unlikely(ret < 0)) return ret; + if (in->f_flags & O_NONBLOCK) + flags |= SPLICE_F_NONBLOCK; + file_start_write(out); ret = do_splice_from(ipipe, out, &offset, len, flags); file_end_write(out); @@ -1186,6 +1192,9 @@ static long do_splice(struct file *in, loff_t __user *off_in, offset = in->f_pos; } + if (out->f_flags & O_NONBLOCK) + flags |= SPLICE_F_NONBLOCK; + pipe_lock(opipe); ret = wait_for_space(opipe, flags); if (!ret) @@ -1718,6 +1727,9 @@ static long do_tee(struct file *in, struct file *out, size_t len, * copying the data. */ if (ipipe && opipe && ipipe != opipe) { + if ((in->f_flags | out->f_flags) & O_NONBLOCK) + flags |= SPLICE_F_NONBLOCK; + /* * Keep going, unless we encounter an error. The ipipe/opipe * ordering doesn't really matter. -- 2.19.1