Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1003418yba; Thu, 4 Apr 2019 02:11:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqyYgHm8++nhWJ3M5kr4ls8ZPkKxHr1/i4k7Gpy01LPk6pZeNDFSMCEL41Tot9GcHNUr151F X-Received: by 2002:a63:3281:: with SMTP id y123mr4645885pgy.272.1554369069777; Thu, 04 Apr 2019 02:11:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554369069; cv=none; d=google.com; s=arc-20160816; b=popthe8yEEARqDYnEBdh+LZqS5K8I+XEYhA6JDal8/R7G3t4WbLzxSRXmelkbH0RmQ ST5nnnMR+hqnLl0MqEOicMZMDlsuTDepdyTk4nl2xtkZ4xTxrFu72MPxBh4FiE6R13Iv 9P0LACYuPJ/4OdZS3EdYiL+OwnulQi/52GPeRS+7pDOhbUd6aFQXCUPVKaZ0V7nqSNvX kf9l8v4Uav26kjJcNdz3lpq8K2KShK8BnstdTVzGFRhREHVyN7bW3R17ao/89IbM7qYd bampvpXfw3ogNcUx6hWoMTXUv7P6ssd/9qXApNQR8gnu58EIY16GI2nr8pkf3MI5Cx0S GyLw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=6/DG5KFhcs6s4PSo+TLSsAd17OG/ACLB3ocefjR7G0Q=; b=pf/bZzlx5QJFytFtxz2z+RTL3vsL4IcmAVGBAWLb2kV+pBU6+Q0kj6kJbIAnRi5Mqn nQNSgMSpLAifJbI2/k3HMB9kUKGAtiITNry977VnfYC2Q/uIMKS9Q+K4SreWzz6D4r8b PRC6UXRdwpxsgKjub+wHw1Hc0RSleZhxmd2EowPxQUNe/6TN1D/C8i5IecLzX5Bbm7/I eFt7+QL4+ixL/oNTXvTFB7bHpCeDhu4UCPVW5ZdIlZ+3i0vNX30ZPGRhxOG+Jrz/Svk2 m0UqZN9IR6lV/EB/aGOC3KUbp6vkQqtlj+mal0jd4u4LownBKEp/l1+7wsGCXCYwn/zp 8MXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=p5gLfrjw; 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 z73si15986298pgd.345.2019.04.04.02.10.54; Thu, 04 Apr 2019 02:11:09 -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=p5gLfrjw; 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 S1732969AbfDDJKM (ORCPT + 99 others); Thu, 4 Apr 2019 05:10:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:49698 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732961AbfDDJKK (ORCPT ); Thu, 4 Apr 2019 05:10:10 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4282E20693; Thu, 4 Apr 2019 09:10:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1554369009; bh=uFlpdJl0cQzqWgIMyjGzl2Ckkbs5ykVYUAJLFddGLBU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p5gLfrjwDyuRqs9pZd8M0e5y1avBkLOJHdFNY+jvfgmViQNdgZl6s0NO3yp55qSud R/ZaXnB2NHFN8aEBjdV1daMuD8ILElhalENz7I1BPimZcIYBlmuOs9f0Q1X0uyu8o+ AVXmY2BJKLSCORv1CtuhsqaPdEPhs+HeMJXqGe4E= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Slavomir Kaslev , "Steven Rostedt (VMware)" , Linus Torvalds , Sasha Levin Subject: [PATCH 5.0 047/246] fs: Make splice() and tee() take into account O_NONBLOCK flag on pipes Date: Thu, 4 Apr 2019 10:45:47 +0200 Message-Id: <20190404084620.671108728@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404084619.236418459@linuxfoundation.org> References: <20190404084619.236418459@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 5.0-stable review patch. If anyone has any objections, please let me know. ------------------ [ 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 90c29675d573..f568273ac336 100644 --- a/fs/splice.c +++ b/fs/splice.c @@ -1123,6 +1123,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); } @@ -1148,6 +1151,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); @@ -1172,6 +1178,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) @@ -1721,6 +1730,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