Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp240106pxf; Wed, 10 Mar 2021 05:26:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJzGiOIATr+jp/j3qCTvB07ORnOUP7SFmzljwJKKWUGBDRY4H1HYLRkf7eCMT5CMdajpf+Hw X-Received: by 2002:a17:907:75c7:: with SMTP id jl7mr3759191ejc.191.1615382761892; Wed, 10 Mar 2021 05:26:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615382761; cv=none; d=google.com; s=arc-20160816; b=GU/0AIWbiq4zQyvSRBMXWJMZ1qaY5/oTl0kqJ+0tXSMyxPeh1ZtWsj3FGssGnZJdS4 2Im0ZMPaJ0YJ3Wx2ypGL+AgBlECoUd0VOhk4Au4LJXeWQDXEpBBlsuMgJoZH4UnnnMqN XDEok1FanAa2B18jTRwGFFDlDNS30O17PV4x91U/ROTv2dp6OoE4l/8njan1xZ2K0LJ0 Ur4WCIaXmxkpurYfPxVu0CSE2d9t0pOV/V/Xlv4nnDFPXjbmx791YJHSy42EJoZFpzV4 2dFPTvKl889noUeNSQv9x+EpHLFsZWv/eZZmRqfLelAHTI9KYpCwbw4rTc8CPf566wBo Z7sg== 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=MJA2hFpvOMHt2w9lxCQHcqMFVIgEEGG68GT0MJIflGU=; b=gPTtUV7ejJo2q95PxD9aJdcttsJ0ty1T9ljjbudm4WmG0l1omNO3rq5zW9fQ9am70B XuVS/F/ffCfivRqTD3C6RNKvvwnI/GZHuWS2/u0vyceNYECv5U7XXEyO5dfiXtf+Uj1C H+ewBBxsrVREox5Er0qZJZuW2rXWjtxShrri3SvCzfYGg5FSQPzkzJ1e63QVO9YA3QHZ u8cpPIEN8euEn0jdqvZxP+qTGE9htiaiPp0olcgCgBBHII6bAvhn2gcGYR3ax5qVBLQ+ e63MnwQ7Fv+qVkWNDFG2ayrCc75c3Unu2KRSvkDH5GtZR1dwHgB2+0UxXggPXosNGaCj cg3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XTlBgEqQ; 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 f11si11867666ejw.277.2021.03.10.05.25.35; Wed, 10 Mar 2021 05:26:01 -0800 (PST) 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=XTlBgEqQ; 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 S232947AbhCJNYn (ORCPT + 99 others); Wed, 10 Mar 2021 08:24:43 -0500 Received: from mail.kernel.org ([198.145.29.99]:45344 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232565AbhCJNYG (ORCPT ); Wed, 10 Mar 2021 08:24:06 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C90F864FD8; Wed, 10 Mar 2021 13:24:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615382645; bh=jlDMQqt2Fi37WONNSWPUAljt6UEZEUdSaECPGdDiatc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XTlBgEqQX4JqajaZisFVPBMf5RGVo4YIafZhaI1SZatxxcGcp2MwNjUGS9t+BjeW7 4bcUzLfNWymvi8AyoRK/Q8ICykylQ/Ux46h1xCGOKxEgc8An+/NvCqNos+LVFwNUqu JKO1EvFQgLqYliduJ2Qsnh0fODYb/lQVhpbuXbKI= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Jens Axboe , Pavel Begunkov Subject: [PATCH 5.11 06/36] fs: provide locked helper variant of close_fd_get_file() Date: Wed, 10 Mar 2021 14:23:19 +0100 Message-Id: <20210310132320.723697369@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210310132320.510840709@linuxfoundation.org> References: <20210310132320.510840709@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: Greg Kroah-Hartman From: Jens Axboe commit 53dec2ea74f2ef360e8455439be96a780baa6097 upstream Assumes current->files->file_lock is already held on invocation. Helps the caller check the file before removing the fd, if it needs to. Signed-off-by: Jens Axboe Signed-off-by: Pavel Begunkov Signed-off-by: Greg Kroah-Hartman --- fs/file.c | 36 +++++++++++++++++++++++++----------- fs/internal.h | 1 + 2 files changed, 26 insertions(+), 11 deletions(-) --- a/fs/file.c +++ b/fs/file.c @@ -22,6 +22,8 @@ #include #include +#include "internal.h" + unsigned int sysctl_nr_open __read_mostly = 1024*1024; unsigned int sysctl_nr_open_min = BITS_PER_LONG; /* our min() is unusable in constant expressions ;-/ */ @@ -732,36 +734,48 @@ int __close_range(unsigned fd, unsigned } /* - * variant of close_fd that gets a ref on the file for later fput. - * The caller must ensure that filp_close() called on the file, and then - * an fput(). + * See close_fd_get_file() below, this variant assumes current->files->file_lock + * is held. */ -int close_fd_get_file(unsigned int fd, struct file **res) +int __close_fd_get_file(unsigned int fd, struct file **res) { struct files_struct *files = current->files; struct file *file; struct fdtable *fdt; - spin_lock(&files->file_lock); fdt = files_fdtable(files); if (fd >= fdt->max_fds) - goto out_unlock; + goto out_err; file = fdt->fd[fd]; if (!file) - goto out_unlock; + goto out_err; rcu_assign_pointer(fdt->fd[fd], NULL); __put_unused_fd(files, fd); - spin_unlock(&files->file_lock); get_file(file); *res = file; return 0; - -out_unlock: - spin_unlock(&files->file_lock); +out_err: *res = NULL; return -ENOENT; } +/* + * variant of close_fd that gets a ref on the file for later fput. + * The caller must ensure that filp_close() called on the file, and then + * an fput(). + */ +int close_fd_get_file(unsigned int fd, struct file **res) +{ + struct files_struct *files = current->files; + int ret; + + spin_lock(&files->file_lock); + ret = __close_fd_get_file(fd, res); + spin_unlock(&files->file_lock); + + return ret; +} + void do_close_on_exec(struct files_struct *files) { unsigned i; --- a/fs/internal.h +++ b/fs/internal.h @@ -132,6 +132,7 @@ extern struct file *do_file_open_root(st const char *, const struct open_flags *); extern struct open_how build_open_how(int flags, umode_t mode); extern int build_open_flags(const struct open_how *how, struct open_flags *op); +extern int __close_fd_get_file(unsigned int fd, struct file **res); long do_sys_ftruncate(unsigned int fd, loff_t length, int small); int chmod_common(const struct path *path, umode_t mode);