linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hao Xu <hao.xu@linux.dev>
To: io-uring@vger.kernel.org
Cc: Jens Axboe <axboe@kernel.dk>,
	Pavel Begunkov <asml.silence@gmail.com>,
	linux-fsdevel@vger.kernel.org,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Andrew Morton <akpm@linux-foundation.org>,
	Luis Chamberlain <mcgrof@kernel.org>,
	Kuniyuki Iwashima <kuniyu@amazon.co.jp>
Subject: [PATCH 2/5] pipe: add trylock helpers for pipe lock
Date: Tue,  7 Jun 2022 16:06:16 +0800	[thread overview]
Message-ID: <20220607080619.513187-3-hao.xu@linux.dev> (raw)
In-Reply-To: <20220607080619.513187-1-hao.xu@linux.dev>

From: Hao Xu <howeyxu@tencent.com>

Add two helpers pipe_trylock() and pipe_double_trylock(), they are used
in nonblock splice(pipe to pipe) scenario in next patches.

Signed-off-by: Hao Xu <howeyxu@tencent.com>
---
 fs/pipe.c                 | 29 +++++++++++++++++++++++++++++
 include/linux/pipe_fs_i.h |  2 ++
 2 files changed, 31 insertions(+)

diff --git a/fs/pipe.c b/fs/pipe.c
index 74ae9fafd25a..033736eb61fb 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -98,6 +98,11 @@ void pipe_unlock(struct pipe_inode_info *pipe)
 }
 EXPORT_SYMBOL(pipe_unlock);
 
+int pipe_trylock(struct pipe_inode_info *pipe)
+{
+	return mutex_trylock(&pipe->mutex);
+}
+
 static inline void __pipe_lock(struct pipe_inode_info *pipe)
 {
 	mutex_lock_nested(&pipe->mutex, I_MUTEX_PARENT);
@@ -122,6 +127,30 @@ void pipe_double_lock(struct pipe_inode_info *pipe1,
 	}
 }
 
+int pipe_double_trylock(struct pipe_inode_info *pipe1,
+			 struct pipe_inode_info *pipe2)
+{
+	BUG_ON(pipe1 == pipe2);
+
+	if (pipe1 < pipe2) {
+		if (!pipe_trylock(pipe1))
+			return 0;
+		if (!pipe_trylock(pipe2)) {
+			pipe_unlock(pipe1);
+			return 0;
+		}
+	} else {
+		if (!pipe_trylock(pipe2))
+			return 0;
+		if (!pipe_trylock(pipe1)) {
+			pipe_unlock(pipe2);
+			return 0;
+		}
+	}
+
+	return 1;
+}
+
 static void anon_pipe_buf_release(struct pipe_inode_info *pipe,
 				  struct pipe_buffer *buf)
 {
diff --git a/include/linux/pipe_fs_i.h b/include/linux/pipe_fs_i.h
index cb0fd633a610..78dc2c7c999f 100644
--- a/include/linux/pipe_fs_i.h
+++ b/include/linux/pipe_fs_i.h
@@ -235,8 +235,10 @@ static inline bool pipe_buf_try_steal(struct pipe_inode_info *pipe,
 
 /* Pipe lock and unlock operations */
 void pipe_lock(struct pipe_inode_info *);
+int pipe_trylock(struct pipe_inode_info *);
 void pipe_unlock(struct pipe_inode_info *);
 void pipe_double_lock(struct pipe_inode_info *, struct pipe_inode_info *);
+int pipe_double_trylock(struct pipe_inode_info *, struct pipe_inode_info *);
 
 /* Wait for a pipe to be readable/writable while dropping the pipe lock */
 void pipe_wait_readable(struct pipe_inode_info *);
-- 
2.25.1


  parent reply	other threads:[~2022-06-07  8:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-07  8:06 [RFC 0/5] support nonblock submission for splice pipe to pipe Hao Xu
2022-06-07  8:06 ` [PATCH 1/5] io_uring: move sp->len check up for splice and tee Hao Xu
2022-06-07  8:06 ` Hao Xu [this message]
2022-06-07  8:06 ` [PATCH 3/5] splice: support nonblock for splice from pipe to pipe Hao Xu
2022-06-07  8:06 ` [PATCH 4/5] io_uring: support nonblock try for splicing " Hao Xu
2022-06-07 21:38   ` kernel test robot
2022-06-08 14:18   ` kernel test robot
2022-06-09  2:24   ` kernel test robot
2022-06-07  8:06 ` [PATCH 5/5] io_uring: add file_in in io_splice{} to avoid duplicate calculation Hao Xu
2022-06-07  8:13 ` [RFC 0/5] support nonblock submission for splice pipe to pipe Hao Xu
2022-06-07  9:27 ` Pavel Begunkov
2022-06-08  4:19   ` Hao Xu
2022-06-08  4:31     ` Jens Axboe
2022-06-08 11:33     ` Pavel Begunkov
2022-06-14 13:23 ` Hao Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220607080619.513187-3-hao.xu@linux.dev \
    --to=hao.xu@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=asml.silence@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=kuniyu@amazon.co.jp \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).