From patchwork Fri Aug 25 13:54:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hao Xu X-Patchwork-Id: 13365738 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 228AFC71133 for ; Fri, 25 Aug 2023 13:56:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B499C2800B6; Fri, 25 Aug 2023 09:56:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AD2EE8E0015; Fri, 25 Aug 2023 09:56:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 973D82800B6; Fri, 25 Aug 2023 09:56:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 867788E0015 for ; Fri, 25 Aug 2023 09:56:50 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 60552A06C3 for ; Fri, 25 Aug 2023 13:56:50 +0000 (UTC) X-FDA: 81162777780.04.59295E0 Received: from out-244.mta1.migadu.com (out-244.mta1.migadu.com [95.215.58.244]) by imf06.hostedemail.com (Postfix) with ESMTP id AFBD718001B for ; Fri, 25 Aug 2023 13:56:47 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=GYR7q+ed; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.244 as permitted sender) smtp.mailfrom=hao.xu@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692971807; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W04QOWrrBuMFm18D/+g+MI2uZNnKBIsbwxq2wO1dKKw=; b=mjeOUXUsIUCJuWdA0OadErTaf4MzBF1VpQXUt+a0PHOCJIcjfds3GCOu1sBzvVGIs00jTm 1qs66sLkOkCgOrtOfSqJHGZ4J/gkoxnpJ1bjskfG/4KsEhOYWciHSGmops6o5+T+2vs9qf Kyt6rQJUTTVGNFt9myct9Dm75HC8sQg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=GYR7q+ed; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of hao.xu@linux.dev designates 95.215.58.244 as permitted sender) smtp.mailfrom=hao.xu@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692971807; a=rsa-sha256; cv=none; b=ac52tH9D1RWWMuktc/+DQl3PRzVooH9getHE4xikRsB95SN4soDXfgzmR5aUp6togngtfu RQ1xNrxUWHAE4jhc8UWSMntxhXBHqOxc/H6cHbL6EmTPKi+3ZLY3ZtRtxIFgrMeGWB4WkR RFicCk59Qr6q4gKfbe9KpB/E9j6yM9o= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1692971805; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W04QOWrrBuMFm18D/+g+MI2uZNnKBIsbwxq2wO1dKKw=; b=GYR7q+ed6aYuRljJYmvSX9IH2GLL+DVsCTCg/+y32Q33mqJCInZ91y+uQeCstRhUPnoP3N ePQXgcigakNgh8HTbQeNs8AWnI9Lei+c8/zQ92SOW8UgAHTnrdNmJY44TCN57rW11W5x8C QP3i32OeE0C7qn2HcY/8yIvkb/iSTUs= From: Hao Xu To: io-uring@vger.kernel.org, Jens Axboe Cc: Dominique Martinet , Pavel Begunkov , Christian Brauner , Alexander Viro , Stefan Roesch , Clay Harris , Dave Chinner , "Darrick J . Wong" , linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-ext4@vger.kernel.org, linux-cachefs@redhat.com, ecryptfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-unionfs@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-btrfs@vger.kernel.org, codalist@coda.cs.cmu.edu, linux-f2fs-devel@lists.sourceforge.net, cluster-devel@redhat.com, linux-mm@kvack.org, linux-nilfs@vger.kernel.org, devel@lists.orangefs.org, linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-mtd@lists.infradead.org, Wanpeng Li Subject: [PATCH 05/29] vfs: add a vfs helper for io_uring file pos lock Date: Fri, 25 Aug 2023 21:54:07 +0800 Message-Id: <20230825135431.1317785-6-hao.xu@linux.dev> In-Reply-To: <20230825135431.1317785-1-hao.xu@linux.dev> References: <20230825135431.1317785-1-hao.xu@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: p1rssrq91g81o8japexaczpojdqshfar X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: AFBD718001B X-HE-Tag: 1692971807-720306 X-HE-Meta: U2FsdGVkX19EbEAJy8edeNBNY5pXTw7V6bdrej6SWh3ddJ/y+GZWoIebWqJIIhKuhWvfiXsVN+eB6k8poa3XY8bhk4GwlUI+ap4MGMVS4iuRvawjZqSjZA9NgL763NHKtXKYZ5KsMKoAd43zu1fNqodfMKiANxI0ZuY8/ngg8w899AFDMgid/BnmT53fGAFUyv8AKEuB5+gaqjXX/hazWhsgwC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: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Hao Xu Add a vfs helper file_pos_lock_nowait() for io_uring usage. The function have conditional nowait logic, i.e. if nowait is needed, return -EAGAIN when trylock fails. Signed-off-by: Hao Xu --- fs/file.c | 13 +++++++++++++ include/linux/file.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/fs/file.c b/fs/file.c index 35c62b54c9d6..8e5c38f5db52 100644 --- a/fs/file.c +++ b/fs/file.c @@ -1053,6 +1053,19 @@ void __f_unlock_pos(struct file *f) mutex_unlock(&f->f_pos_lock); } +int file_pos_lock_nowait(struct file *file, bool nowait) +{ + if (!(file->f_mode & FMODE_ATOMIC_POS)) + return 0; + + if (!nowait) + mutex_lock(&file->f_pos_lock); + else if (!mutex_trylock(&file->f_pos_lock)) + return -EAGAIN; + + return 1; +} + /* * We only lock f_pos if we have threads or if the file might be * shared with another process. In both cases we'll have an elevated diff --git a/include/linux/file.h b/include/linux/file.h index 6e9099d29343..bcc6ba0aec50 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -81,6 +81,8 @@ static inline void fdput_pos(struct fd f) fdput(f); } +extern int file_pos_lock_nowait(struct file *file, bool nowait); + DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd) extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);