From patchwork Mon Apr 20 07:15:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nate Karstens X-Patchwork-Id: 11498241 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C289214DD for ; Mon, 20 Apr 2020 07:16:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9BFF12078A for ; Mon, 20 Apr 2020 07:16:27 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=garmin.com header.i=@garmin.com header.b="jHgtKMai" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726316AbgDTHQ0 (ORCPT ); Mon, 20 Apr 2020 03:16:26 -0400 Received: from mail-eopbgr680105.outbound.protection.outlook.com ([40.107.68.105]:20708 "EHLO NAM04-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726151AbgDTHQY (ORCPT ); Mon, 20 Apr 2020 03:16:24 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UaBuw2ak3rNv7v9bAUrwvrLhsz0014+m99ijR/1xTVtTEqfYmG0XqAyva+hSWxcp66RZI2gDGBH2KG0sTiRJwTxObeRothHyg85izPxh49qAlYOHpvYx6kVYIZPI9VUZs7/lR3DCF5FCliM08EgZAvMI8NbpdNSvgicEPLVM/lVezlDTIixUG1BPb8F41QDahTkg+jgHW9mx1bD7yuv31ZYussC4djuQmvvL1SbUKjYzrJUNHPl8pelF4Ge2edXh0eQCeYHyZPrGUbFCZ3lq3BHMz2A3NkfJ9ZdhMhhHReydxJ6spuwaFcKi4UgqLpT+5szQZkWJeaFgFhdOM8uW5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6dAWobLvVWrE8yxfuLtMfva9SmJ+ZqWnqRTNKx+Qvng=; b=FPJR4MQTh4yu+n24iGOoJOwamZnCFxTL96+yQgaYnBHigSy+ZdyLNx1O44UAV0yHe0kpPHTdvAopszfnzv20QEQnd1WGCoMPlFpwFhmc9Sw9eViTb25KheSum1rPWFN2y7Kd4ia+BPKwxC08WFf9juhC2lTD1Cj+pA4MOs5Wt1ytdXr+Pps3M1jIUViA4O78V35kwR3hpcH4990//m8UwTQ/jqd0PPAIWXHeNcAUiaxM+Qa2+6mM+mgobHTZSX4kCdJYk4TYTzm3Zt19xKemcY92HcABJAe33aMhhmLJsd1Y51rVoxG6Z6kYfLiuwLMjaYfPYnmPk/ntRbr+VNEXgg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 204.77.163.244) smtp.rcpttodomain=zeniv.linux.org.uk smtp.mailfrom=garmin.com; dmarc=pass (p=quarantine sp=quarantine pct=20) action=none header.from=garmin.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garmin.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6dAWobLvVWrE8yxfuLtMfva9SmJ+ZqWnqRTNKx+Qvng=; b=jHgtKMair2wI2r01/1ks3F4CAd1GOOAoZVOgmiRWczyN/0EKuQmLE2DX0hmY3wfqKGLzb5qQaf28yhmgyOVxy3M9qudrpDwTdlznRq5TcmRPUGVsJXeGERzREdO2xHDUxMwgWtaUvBT0rzEayjJ07NurmSouSuyRI7v7PxHV+tXsYY57P9EPvZUzABkXEaW5JgVs7ckRtdedkypOUMbKRr4tCZvjs5G2WVNAFSFzni2zMkB6K6KvhyTzmS6+n38CjzIjWk6n+nBGAxTGisS+VqJkVSvW7sqEPNPySCxjnBvMUL0W9NhG6/KulzGaS8hiXh4o7XrEHXUjJBd8eKS4DA== Received: from BN6PR06CA0018.namprd06.prod.outlook.com (2603:10b6:404:10b::28) by MWHPR04MB1102.namprd04.prod.outlook.com (2603:10b6:301:44::37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.29; Mon, 20 Apr 2020 07:16:18 +0000 Received: from BN7NAM10FT049.eop-nam10.prod.protection.outlook.com (2603:10b6:404:10b:cafe::5) by BN6PR06CA0018.outlook.office365.com (2603:10b6:404:10b::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25 via Frontend Transport; Mon, 20 Apr 2020 07:16:17 +0000 Authentication-Results: spf=pass (sender IP is 204.77.163.244) smtp.mailfrom=garmin.com; zeniv.linux.org.uk; dkim=none (message not signed) header.d=none;zeniv.linux.org.uk; dmarc=pass action=none header.from=garmin.com; Received-SPF: Pass (protection.outlook.com: domain of garmin.com designates 204.77.163.244 as permitted sender) receiver=protection.outlook.com; client-ip=204.77.163.244; helo=edgetransport.garmin.com; Received: from edgetransport.garmin.com (204.77.163.244) by BN7NAM10FT049.mail.protection.outlook.com (10.13.157.3) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25 via Frontend Transport; Mon, 20 Apr 2020 07:16:17 +0000 Received: from OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) by olawpa-edge4.garmin.com (10.60.4.228) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.1466.3; Mon, 20 Apr 2020 02:16:15 -0500 Received: from ola-d01c000-vm.ad.garmin.com (10.5.84.15) by OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Mon, 20 Apr 2020 02:16:15 -0500 From: Nate Karstens To: Alexander Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , Richard Henderson , Ivan Kokshaysky , Matt Turner , "James E.J. Bottomley" , Helge Deller , "David S. Miller" , Jakub Kicinski , , , , , , , CC: Changli Gao , Nate Karstens Subject: [PATCH 1/4] fs: Implement close-on-fork Date: Mon, 20 Apr 2020 02:15:45 -0500 Message-ID: <20200420071548.62112-2-nate.karstens@garmin.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200420071548.62112-1-nate.karstens@garmin.com> References: <20200420071548.62112-1-nate.karstens@garmin.com> MIME-Version: 1.0 X-ClientProxiedBy: OLAWPA-EXMB3.ad.garmin.com (10.5.144.15) To OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) X-TM-AS-Product-Ver: SMEX-12.5.0.1300-8.5.1020-25366.005 X-TM-AS-Result: No-8.450200-8.000000-10 X-TMASE-MatchedRID: BPEQoO09kToY5XJhZJs6SDCMW7zNwFaIGiQ8GIEGP38y2ckJNvUX+HzK 3Q9zSFL7ATYOMR69+dqRXQsE2URVLPpC5SMG+P7XB8FxO/BQHsIacZilk37ECL95OdmJ178B3A8 rIQHSOzD6Pp/0inv5dXDlPghqPnfyYlldA0POS1IaPMGCcVm9DgKflB9+9kWVlaO5BvPDtzcxAn K2eeQXhhfb/g6/AVAtMRc8tpsXpWvHLcRe7wyoEIlSWYvdSPSY4F58RPNYsrEHAe47mcoCCclQV vH1JO6TNcFOGcoM9ErGBkuRwWcFOGi8YU2giSiPutvHF25zoU8r9gVlOIN/6mcBMv0Fvtzl0dsY UxvcQZcuI35yUPSOFqkquW+P/OwXo8WMkQWv6iXBcIE78YqRWvcUt5lc1lLgoGRyAacnhaaAxBo 6LHbCgebkD1AsfOCTGhoVV6XcNT7hO5URK+drpH7cGd19dSFd X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--8.450200-8.000000 X-TMASE-Version: SMEX-12.5.0.1300-8.5.1020-25366.005 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:204.77.163.244;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:edgetransport.garmin.com;PTR:extedge.garmin.com;CAT:NONE;SFTY:;SFS:(10019020)(396003)(39860400002)(346002)(136003)(376002)(46966005)(110136005)(246002)(8676002)(7696005)(186003)(54906003)(70586007)(44832011)(36756003)(2616005)(70206006)(8936002)(5660300002)(107886003)(26005)(2906002)(86362001)(356005)(7416002)(6666004)(47076004)(82740400003)(7636003)(336012)(426003)(1076003)(316002)(4326008)(478600001)(921003);DIR:OUT;SFP:1102; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e6563c99-e5b1-49fc-6a0b-08d7e4fab7cc X-MS-TrafficTypeDiagnostic: MWHPR04MB1102: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3383; X-Forefront-PRVS: 03793408BA X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a9cmivBsdETlNUdnyu40HEPW/ndezmSP0HeTdSRoCicfy4opYVORQq3X2z1Y97aD64c/lCl/sFQDIqXUfWmUSUAZbMSSrIbGa1kbgZcMZg+sNM3ghYkSJv9mZHZFZJ52D1NugxvyPQ8TocABi6leyQHhrCRBV2qhzMVXLL/R/PPZMMargyGafiRu4iyNPdnkM6K+MWW+sPzs8vEpeWfvFQYt+mFPP6R6aQtALgjPIyZPZyeduHBIBCoRv0s2UHR4toXpNjx2qPXP79H/IQ492kmIsQblhHC9O+luS+zHIMPSDHwWRTIu/vgCUajsrXZbjWFDCABxHzwT+RS2zORkt3WWXbenUEkU1mc44z4i5wORwKpkomJIPy9GexahHfqQUOhT/ro9YHg7/XOD2Q/P00nMRha2Oy1jOTTCECuXXC5kAgHkqtd3pOHkt9YfYFJ2N2hnQpNOSR+i1zIDTUxnG5DPhQyiA/jen/uGnCWSumwSMY65ujShgmLe8zqI0sX0kb/cT8QUbfHX8WtK6Q427YCxCL08paxh7qOP92cG8sc= X-OriginatorOrg: garmin.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2020 07:16:17.1749 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e6563c99-e5b1-49fc-6a0b-08d7e4fab7cc X-MS-Exchange-CrossTenant-Id: 38d0d425-ba52-4c0a-a03e-2a65c8e82e2d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=38d0d425-ba52-4c0a-a03e-2a65c8e82e2d;Ip=[204.77.163.244];Helo=[edgetransport.garmin.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR04MB1102 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org The close-on-fork flag causes the file descriptor to be closed atomically in the child process before the child process returns from fork(). Implement this feature and provide a method to get/set the close-on-fork flag using fcntl(2). This functionality was approved by the Austin Common Standards Revision Group for inclusion in the next revision of the POSIX standard (see issue 1318 in the Austin Group Defect Tracker). Co-developed-by: Changli Gao Signed-off-by: Changli Gao Signed-off-by: Nate Karstens Signed-off-by: Changli Gao Signed-off-by: Nate Karstens --- fs/fcntl.c | 2 ++ fs/file.c | 50 +++++++++++++++++++++++++- include/linux/fdtable.h | 7 ++++ include/linux/file.h | 2 ++ include/uapi/asm-generic/fcntl.h | 5 +-- tools/include/uapi/asm-generic/fcntl.h | 5 +-- 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/fs/fcntl.c b/fs/fcntl.c index 2e4c0fa2074b..23964abf4a1a 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -335,10 +335,12 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, break; case F_GETFD: err = get_close_on_exec(fd) ? FD_CLOEXEC : 0; + err |= get_close_on_fork(fd) ? FD_CLOFORK : 0; break; case F_SETFD: err = 0; set_close_on_exec(fd, arg & FD_CLOEXEC); + set_close_on_fork(fd, arg & FD_CLOFORK); break; case F_GETFL: err = filp->f_flags; diff --git a/fs/file.c b/fs/file.c index c8a4e4c86e55..de7260ba718d 100644 --- a/fs/file.c +++ b/fs/file.c @@ -57,6 +57,8 @@ static void copy_fd_bitmaps(struct fdtable *nfdt, struct fdtable *ofdt, memset((char *)nfdt->open_fds + cpy, 0, set); memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy); memset((char *)nfdt->close_on_exec + cpy, 0, set); + memcpy(nfdt->close_on_fork, ofdt->close_on_fork, cpy); + memset((char *)nfdt->close_on_fork + cpy, 0, set); cpy = BITBIT_SIZE(count); set = BITBIT_SIZE(nfdt->max_fds) - cpy; @@ -118,7 +120,7 @@ static struct fdtable * alloc_fdtable(unsigned int nr) fdt->fd = data; data = kvmalloc(max_t(size_t, - 2 * nr / BITS_PER_BYTE + BITBIT_SIZE(nr), L1_CACHE_BYTES), + 3 * nr / BITS_PER_BYTE + BITBIT_SIZE(nr), L1_CACHE_BYTES), GFP_KERNEL_ACCOUNT); if (!data) goto out_arr; @@ -126,6 +128,8 @@ static struct fdtable * alloc_fdtable(unsigned int nr) data += nr / BITS_PER_BYTE; fdt->close_on_exec = data; data += nr / BITS_PER_BYTE; + fdt->close_on_fork = data; + data += nr / BITS_PER_BYTE; fdt->full_fds_bits = data; return fdt; @@ -236,6 +240,17 @@ static inline void __clear_close_on_exec(unsigned int fd, struct fdtable *fdt) __clear_bit(fd, fdt->close_on_exec); } +static inline void __set_close_on_fork(unsigned int fd, struct fdtable *fdt) +{ + __set_bit(fd, fdt->close_on_fork); +} + +static inline void __clear_close_on_fork(unsigned int fd, struct fdtable *fdt) +{ + if (test_bit(fd, fdt->close_on_fork)) + __clear_bit(fd, fdt->close_on_fork); +} + static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt) { __set_bit(fd, fdt->open_fds); @@ -290,6 +305,7 @@ struct files_struct *dup_fd(struct files_struct *oldf, int *errorp) new_fdt = &newf->fdtab; new_fdt->max_fds = NR_OPEN_DEFAULT; new_fdt->close_on_exec = newf->close_on_exec_init; + new_fdt->close_on_fork = newf->close_on_fork_init; new_fdt->open_fds = newf->open_fds_init; new_fdt->full_fds_bits = newf->full_fds_bits_init; new_fdt->fd = &newf->fd_array[0]; @@ -337,6 +353,12 @@ struct files_struct *dup_fd(struct files_struct *oldf, int *errorp) for (i = open_files; i != 0; i--) { struct file *f = *old_fds++; + + if (test_bit(open_files - i, new_fdt->close_on_fork)) { + __clear_bit(open_files - i, new_fdt->open_fds); + f = NULL; + } + if (f) { get_file(f); } else { @@ -453,6 +475,7 @@ struct files_struct init_files = { .max_fds = NR_OPEN_DEFAULT, .fd = &init_files.fd_array[0], .close_on_exec = init_files.close_on_exec_init, + .close_on_fork = init_files.close_on_fork_init, .open_fds = init_files.open_fds_init, .full_fds_bits = init_files.full_fds_bits_init, }, @@ -865,6 +888,31 @@ bool get_close_on_exec(unsigned int fd) return res; } +void set_close_on_fork(unsigned int fd, int flag) +{ + struct files_struct *files = current->files; + struct fdtable *fdt; + spin_lock(&files->file_lock); + fdt = files_fdtable(files); + if (flag) + __set_close_on_fork(fd, fdt); + else + __clear_close_on_fork(fd, fdt); + spin_unlock(&files->file_lock); +} + +bool get_close_on_fork(unsigned int fd) +{ + struct files_struct *files = current->files; + struct fdtable *fdt; + bool res; + rcu_read_lock(); + fdt = files_fdtable(files); + res = close_on_fork(fd, fdt); + rcu_read_unlock(); + return res; +} + static int do_dup2(struct files_struct *files, struct file *file, unsigned fd, unsigned flags) __releases(&files->file_lock) diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h index f07c55ea0c22..61c551947fa3 100644 --- a/include/linux/fdtable.h +++ b/include/linux/fdtable.h @@ -27,6 +27,7 @@ struct fdtable { unsigned int max_fds; struct file __rcu **fd; /* current fd array */ unsigned long *close_on_exec; + unsigned long *close_on_fork; unsigned long *open_fds; unsigned long *full_fds_bits; struct rcu_head rcu; @@ -37,6 +38,11 @@ static inline bool close_on_exec(unsigned int fd, const struct fdtable *fdt) return test_bit(fd, fdt->close_on_exec); } +static inline bool close_on_fork(unsigned int fd, const struct fdtable *fdt) +{ + return test_bit(fd, fdt->close_on_fork); +} + static inline bool fd_is_open(unsigned int fd, const struct fdtable *fdt) { return test_bit(fd, fdt->open_fds); @@ -61,6 +67,7 @@ struct files_struct { spinlock_t file_lock ____cacheline_aligned_in_smp; unsigned int next_fd; unsigned long close_on_exec_init[1]; + unsigned long close_on_fork_init[1]; unsigned long open_fds_init[1]; unsigned long full_fds_bits_init[1]; struct file __rcu * fd_array[NR_OPEN_DEFAULT]; diff --git a/include/linux/file.h b/include/linux/file.h index 142d102f285e..86fbb36b438b 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -85,6 +85,8 @@ extern int f_dupfd(unsigned int from, struct file *file, unsigned flags); extern int replace_fd(unsigned fd, struct file *file, unsigned flags); extern void set_close_on_exec(unsigned int fd, int flag); extern bool get_close_on_exec(unsigned int fd); +extern void set_close_on_fork(unsigned int fd, int flag); +extern bool get_close_on_fork(unsigned int fd); extern int __get_unused_fd_flags(unsigned flags, unsigned long nofile); extern int get_unused_fd_flags(unsigned flags); extern void put_unused_fd(unsigned int fd); diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h index 9dc0bf0c5a6e..0cb7199a7743 100644 --- a/include/uapi/asm-generic/fcntl.h +++ b/include/uapi/asm-generic/fcntl.h @@ -98,8 +98,8 @@ #endif #define F_DUPFD 0 /* dup */ -#define F_GETFD 1 /* get close_on_exec */ -#define F_SETFD 2 /* set/clear close_on_exec */ +#define F_GETFD 1 /* get close_on_exec & close_on_fork */ +#define F_SETFD 2 /* set/clear close_on_exec & close_on_fork */ #define F_GETFL 3 /* get file->f_flags */ #define F_SETFL 4 /* set file->f_flags */ #ifndef F_GETLK @@ -160,6 +160,7 @@ struct f_owner_ex { /* for F_[GET|SET]FL */ #define FD_CLOEXEC 1 /* actually anything with low bit set goes */ +#define FD_CLOFORK 2 /* for posix fcntl() and lockf() */ #ifndef F_RDLCK diff --git a/tools/include/uapi/asm-generic/fcntl.h b/tools/include/uapi/asm-generic/fcntl.h index ac190958c981..e04a00fecb4a 100644 --- a/tools/include/uapi/asm-generic/fcntl.h +++ b/tools/include/uapi/asm-generic/fcntl.h @@ -97,8 +97,8 @@ #endif #define F_DUPFD 0 /* dup */ -#define F_GETFD 1 /* get close_on_exec */ -#define F_SETFD 2 /* set/clear close_on_exec */ +#define F_GETFD 1 /* get close_on_exec & close_on_fork */ +#define F_SETFD 2 /* set/clear close_on_exec & close_on_fork */ #define F_GETFL 3 /* get file->f_flags */ #define F_SETFL 4 /* set file->f_flags */ #ifndef F_GETLK @@ -159,6 +159,7 @@ struct f_owner_ex { /* for F_[GET|SET]FL */ #define FD_CLOEXEC 1 /* actually anything with low bit set goes */ +#define FD_CLOFORK 2 /* for posix fcntl() and lockf() */ #ifndef F_RDLCK From patchwork Mon Apr 20 07:15:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nate Karstens X-Patchwork-Id: 11498245 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1CEF492C for ; Mon, 20 Apr 2020 07:16:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EB3642078A for ; Mon, 20 Apr 2020 07:16:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=garmin.com header.i=@garmin.com header.b="k9EENX3d" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726426AbgDTHQ2 (ORCPT ); Mon, 20 Apr 2020 03:16:28 -0400 Received: from mail-co1nam11on2115.outbound.protection.outlook.com ([40.107.220.115]:9664 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726147AbgDTHQX (ORCPT ); Mon, 20 Apr 2020 03:16:23 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oYRtlngSVn8ezUrM5WryFSIT6hZhPdfuuiX/ktGIGMx7XpmXczJKfli6ORG7i4x6yuLtekpW7MGXa8P1qF+DtqhfGm6qks5k+ugeFrRZZ2S24yd19+uNFRayok+NybW+nLCxM2nn3Vi0m54kFpNDx7ab1w+4NLfXoZg/CRrWFdmEHyIjbYYUsKS6yD1XwTaateGi1f2iyhLX6BL/PHN6EDQAjl6wzTrHo0e0uEjvq7HJJ3gaqnc35IQXnKCxppxWYGZ6xm/ENrCrhAM0SMH7pMRkwd+951J1ooJ6ej7PnAP4EVxVW7vRFHovW3N9TGvm/ZVkqxOL4uD4cC8Rh9eINw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KxGBEzQn5cuKkX7mLr+s7bTq6EpNG8Hh4TggrLFv1+M=; b=ITod/aAs8rFqpygPAAt9e9jX6rmKrGPNVLBWJp9gfvDBPlzBqU7oAOxcnJCQqcP51X9GcEQ6o80LGuMovOlYrlj/sPJi91Y/iY6w3I6YcgZCwFd7Oks5dXPesk++NEgcGsStsBioUCDnnqn1DG7+68CRgi9w8MiMJmVIJK4pmsNgVfkuVjN/seRnYW0CWZeE9WVktKDB9/Ia/5Z3VlJWUkWhHzkWlkG/V+8EXnc/pyyBf0GWD4RfT4Ey9YBJ7lC8qCytatKAoS1QzVbsEF/gPKQwmFSwuSOP0lTEwHjzpq8beJTmSDGaSr5gOxb8Nf7e5+HZpOX0a7phjjYM5Wg8Dw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 204.77.163.244) smtp.rcpttodomain=zeniv.linux.org.uk smtp.mailfrom=garmin.com; dmarc=pass (p=quarantine sp=quarantine pct=20) action=none header.from=garmin.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garmin.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KxGBEzQn5cuKkX7mLr+s7bTq6EpNG8Hh4TggrLFv1+M=; b=k9EENX3dnEjzvCQU8w4pSxPqAnh9dg+h4xshaAGwTaL/iWcbiLyRlRo+pkVA7zbL2rX0mSiyFCEe4qiuTVomoc5VOO8km8+tLm8+3igRz6uU1gW/eb8UuHYsnhZf6gxYvHJ9jAGR+L1cHBnVmU+auSJwC1SN1D9oLIcAZB7W3o2a5eR8Do39A8XChO6kb+8AHZjksGailV8OhAJsQEWQjLtLMIEa3UOZvL/Di7H5I6u5Ecnhy5qkg2EAjWOzxcEL1CqI1ldVKrSQmXb7nLB5+HlYpfHBRMbV6KgZAZwxgHU2ZMulaZRut8/+4xJYLirPUlp1BmhKMRJeP3EbfB6M4g== Received: from MWHPR10CA0052.namprd10.prod.outlook.com (2603:10b6:300:2c::14) by BN3PR04MB2242.namprd04.prod.outlook.com (2a01:111:e400:7bb8::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.29; Mon, 20 Apr 2020 07:16:18 +0000 Received: from MW2NAM10FT022.eop-nam10.prod.protection.outlook.com (2603:10b6:300:2c:cafe::d2) by MWHPR10CA0052.outlook.office365.com (2603:10b6:300:2c::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25 via Frontend Transport; Mon, 20 Apr 2020 07:16:18 +0000 Authentication-Results: spf=pass (sender IP is 204.77.163.244) smtp.mailfrom=garmin.com; zeniv.linux.org.uk; dkim=none (message not signed) header.d=none;zeniv.linux.org.uk; dmarc=pass action=none header.from=garmin.com; Received-SPF: Pass (protection.outlook.com: domain of garmin.com designates 204.77.163.244 as permitted sender) receiver=protection.outlook.com; client-ip=204.77.163.244; helo=edgetransport.garmin.com; Received: from edgetransport.garmin.com (204.77.163.244) by MW2NAM10FT022.mail.protection.outlook.com (10.13.155.101) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25 via Frontend Transport; Mon, 20 Apr 2020 07:16:17 +0000 Received: from OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) by olawpa-edge3.garmin.com (10.60.4.226) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.1466.3; Mon, 20 Apr 2020 02:16:16 -0500 Received: from ola-d01c000-vm.ad.garmin.com (10.5.84.15) by OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Mon, 20 Apr 2020 02:16:16 -0500 From: Nate Karstens To: Alexander Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , Richard Henderson , Ivan Kokshaysky , Matt Turner , "James E.J. Bottomley" , Helge Deller , "David S. Miller" , Jakub Kicinski , , , , , , , CC: Changli Gao , Nate Karstens Subject: [PATCH 2/4] fs: Add O_CLOFORK flag for open(2) and dup3(2) Date: Mon, 20 Apr 2020 02:15:46 -0500 Message-ID: <20200420071548.62112-3-nate.karstens@garmin.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200420071548.62112-1-nate.karstens@garmin.com> References: <20200420071548.62112-1-nate.karstens@garmin.com> MIME-Version: 1.0 X-ClientProxiedBy: OLAWPA-EXMB3.ad.garmin.com (10.5.144.15) To OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) X-TM-AS-Product-Ver: SMEX-12.5.0.1300-8.5.1020-25366.005 X-TM-AS-Result: No-9.399500-8.000000-10 X-TMASE-MatchedRID: 9FKQq2Ubc8b1HKFnt7/e8tKhw1CGAxrIlHLUcNM85doAZTQQTIkkc93N NJQDizxeGDXccV5w1UP1q35E6lDQr2+4O3SBmLlW9u1rQ4BgXPIBmf/gD11vZAaYevV4zG3ZQBz oPKhLashZ3hT8koiLOx4fZrHGL7yoEVfyWRZcVuVZMZ6MZ0H1UhACJh4BWAPePTublrdV/SPAgD 8QYQcBsjk4XrS69X2i0/NbBMSkGddoWWTS0CIqzlgUnyU84hcfquFC73XPA2ffc2Xd6VJ+ym9Ih irTclUbTnj5SlaoRXUaH2v8HV+LCsBvT9NZRcVl4vM1YF6AJbbCCfuIMF6xLcK21zBg2KlfFAQv QYa7pIOjr4CAOuztuw1Dkx0IaMzoPwR95aMFg0Nbic5W0OLHgw== X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--9.399500-8.000000 X-TMASE-Version: SMEX-12.5.0.1300-8.5.1020-25366.005 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:204.77.163.244;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:edgetransport.garmin.com;PTR:extedge.garmin.com;CAT:NONE;SFTY:;SFS:(10019020)(346002)(396003)(39860400002)(136003)(376002)(46966005)(2906002)(86362001)(5660300002)(4326008)(82740400003)(44832011)(1076003)(478600001)(186003)(36756003)(26005)(356005)(7636003)(426003)(2616005)(336012)(54906003)(70206006)(70586007)(6666004)(47076004)(7696005)(110136005)(8676002)(7416002)(246002)(8936002)(107886003)(316002)(921003);DIR:OUT;SFP:1102; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2505f673-7eb2-4b32-c190-08d7e4fab80a X-MS-TrafficTypeDiagnostic: BN3PR04MB2242: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-Forefront-PRVS: 03793408BA X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6Znyy5NvGD3VCN/zcDlmuOhWEz7295J1Hw0NUT2P895wP7PAC7bn//oggDs3kB6EhF4+qSUr8Fzy65VyDmgNKUye7cBwdSw9CG97N5CcUMTiUqQFM1anq6SMNhcNM6+rQCgsF1YNcXJDKgNXpPfwHAShZwguNIp0bIfqdhRWER1ZQwpwMLzcsqeaOH88axEJ6L2LwN05E0RZaCWsa1WmMPRT0AZSzJfwaKsRHDpvX0fA9qCeu5UPMUWrGpGHy3pf/l7ntGTt7EHTjYBiKpUI32rw1v2Uj/351M9agkiRwasVbMoh81wX3RKl99QXLgsKLUVW3ZFnonykTjzosBkF13qSqjJ2yJfmOCYc8mbhWv8aAKXhmmTwqy6KNyh3DtRs56Gaj92+MsBvYsDpS5SJcz4zt0Y/WwG86R6sEzi0fFbEriSQFV9IHAvH4SdlR9lmQ0ieBewan2mQidqglo7SOaeKSQkOOcIAazw3TCWOrUHxG+Rwk+XhqRQYEPl2U2JSE51VYLyO0ARotpwYryzlfw== X-OriginatorOrg: garmin.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2020 07:16:17.5554 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2505f673-7eb2-4b32-c190-08d7e4fab80a X-MS-Exchange-CrossTenant-Id: 38d0d425-ba52-4c0a-a03e-2a65c8e82e2d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=38d0d425-ba52-4c0a-a03e-2a65c8e82e2d;Ip=[204.77.163.244];Helo=[edgetransport.garmin.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3PR04MB2242 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Add the O_CLOFORK flag to open(2) and dup3(2) to automatically set the close-on-fork flag in the new file descriptor, saving a separate call to fcntl(2). Co-developed-by: Changli Gao Signed-off-by: Changli Gao Signed-off-by: Nate Karstens --- arch/alpha/include/uapi/asm/fcntl.h | 2 ++ arch/parisc/include/uapi/asm/fcntl.h | 39 +++++++++++++------------- arch/sparc/include/uapi/asm/fcntl.h | 1 + fs/fcntl.c | 2 +- fs/file.c | 10 ++++++- include/linux/fcntl.h | 2 +- include/uapi/asm-generic/fcntl.h | 4 +++ tools/include/uapi/asm-generic/fcntl.h | 4 +++ 8 files changed, 42 insertions(+), 22 deletions(-) diff --git a/arch/alpha/include/uapi/asm/fcntl.h b/arch/alpha/include/uapi/asm/fcntl.h index 50bdc8e8a271..fbab69b15f7f 100644 --- a/arch/alpha/include/uapi/asm/fcntl.h +++ b/arch/alpha/include/uapi/asm/fcntl.h @@ -35,6 +35,8 @@ #define O_PATH 040000000 #define __O_TMPFILE 0100000000 +#define O_CLOFORK 0200000000 /* set close_on_fork */ + #define F_GETLK 7 #define F_SETLK 8 #define F_SETLKW 9 diff --git a/arch/parisc/include/uapi/asm/fcntl.h b/arch/parisc/include/uapi/asm/fcntl.h index 03ce20e5ad7d..8f5989e75b05 100644 --- a/arch/parisc/include/uapi/asm/fcntl.h +++ b/arch/parisc/include/uapi/asm/fcntl.h @@ -2,26 +2,27 @@ #ifndef _PARISC_FCNTL_H #define _PARISC_FCNTL_H -#define O_APPEND 000000010 -#define O_BLKSEEK 000000100 /* HPUX only */ -#define O_CREAT 000000400 /* not fcntl */ -#define O_EXCL 000002000 /* not fcntl */ -#define O_LARGEFILE 000004000 -#define __O_SYNC 000100000 +#define O_APPEND 0000000010 +#define O_BLKSEEK 0000000100 /* HPUX only */ +#define O_CREAT 0000000400 /* not fcntl */ +#define O_EXCL 0000002000 /* not fcntl */ +#define O_LARGEFILE 0000004000 +#define __O_SYNC 0000100000 #define O_SYNC (__O_SYNC|O_DSYNC) -#define O_NONBLOCK 000200004 /* HPUX has separate NDELAY & NONBLOCK */ -#define O_NOCTTY 000400000 /* not fcntl */ -#define O_DSYNC 001000000 /* HPUX only */ -#define O_RSYNC 002000000 /* HPUX only */ -#define O_NOATIME 004000000 -#define O_CLOEXEC 010000000 /* set close_on_exec */ - -#define O_DIRECTORY 000010000 /* must be a directory */ -#define O_NOFOLLOW 000000200 /* don't follow links */ -#define O_INVISIBLE 004000000 /* invisible I/O, for DMAPI/XDSM */ - -#define O_PATH 020000000 -#define __O_TMPFILE 040000000 +#define O_NONBLOCK 0000200004 /* HPUX has separate NDELAY & NONBLOCK */ +#define O_NOCTTY 0000400000 /* not fcntl */ +#define O_DSYNC 0001000000 /* HPUX only */ +#define O_RSYNC 0002000000 /* HPUX only */ +#define O_NOATIME 0004000000 +#define O_CLOEXEC 0010000000 /* set close_on_exec */ + +#define O_DIRECTORY 0000010000 /* must be a directory */ +#define O_NOFOLLOW 0000000200 /* don't follow links */ +#define O_INVISIBLE 0004000000 /* invisible I/O, for DMAPI/XDSM */ + +#define O_PATH 0020000000 +#define __O_TMPFILE 0040000000 +#define O_CLOFORK 0100000000 #define F_GETLK64 8 #define F_SETLK64 9 diff --git a/arch/sparc/include/uapi/asm/fcntl.h b/arch/sparc/include/uapi/asm/fcntl.h index 67dae75e5274..d631ea13bac3 100644 --- a/arch/sparc/include/uapi/asm/fcntl.h +++ b/arch/sparc/include/uapi/asm/fcntl.h @@ -37,6 +37,7 @@ #define O_PATH 0x1000000 #define __O_TMPFILE 0x2000000 +#define O_CLOFORK 0x4000000 #define F_GETOWN 5 /* for sockets. */ #define F_SETOWN 6 /* for sockets. */ diff --git a/fs/fcntl.c b/fs/fcntl.c index 23964abf4a1a..b59b27c3a338 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -1035,7 +1035,7 @@ static int __init fcntl_init(void) * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ - BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ != + BUILD_BUG_ON(22 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32( (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) | __FMODE_EXEC | __FMODE_NONOTIFY)); diff --git a/fs/file.c b/fs/file.c index de7260ba718d..95774b7962d1 100644 --- a/fs/file.c +++ b/fs/file.c @@ -544,6 +544,10 @@ int __alloc_fd(struct files_struct *files, __set_close_on_exec(fd, fdt); else __clear_close_on_exec(fd, fdt); + if (flags & O_CLOFORK) + __set_close_on_fork(fd, fdt); + else + __clear_close_on_fork(fd, fdt); error = fd; #if 1 /* Sanity check */ @@ -945,6 +949,10 @@ __releases(&files->file_lock) __set_close_on_exec(fd, fdt); else __clear_close_on_exec(fd, fdt); + if (flags & O_CLOFORK) + __set_close_on_fork(fd, fdt); + else + __clear_close_on_fork(fd, fdt); spin_unlock(&files->file_lock); if (tofree) @@ -985,7 +993,7 @@ static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) struct file *file; struct files_struct *files = current->files; - if ((flags & ~O_CLOEXEC) != 0) + if ((flags & ~(O_CLOEXEC | O_CLOFORK)) != 0) return -EINVAL; if (unlikely(oldfd == newfd)) diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h index 7bcdcf4f6ab2..cd4c625647db 100644 --- a/include/linux/fcntl.h +++ b/include/linux/fcntl.h @@ -10,7 +10,7 @@ (O_RDONLY | O_WRONLY | O_RDWR | O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC | \ O_APPEND | O_NDELAY | O_NONBLOCK | O_NDELAY | __O_SYNC | O_DSYNC | \ FASYNC | O_DIRECT | O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW | \ - O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE) + O_NOATIME | O_CLOEXEC | O_PATH | __O_TMPFILE | O_CLOFORK) /* List of all valid flags for the how->upgrade_mask argument: */ #define VALID_UPGRADE_FLAGS \ diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h index 0cb7199a7743..165a0736a3aa 100644 --- a/include/uapi/asm-generic/fcntl.h +++ b/include/uapi/asm-generic/fcntl.h @@ -89,6 +89,10 @@ #define __O_TMPFILE 020000000 #endif +#ifndef O_CLOFORK +#define O_CLOFORK 040000000 /* set close_on_fork */ +#endif + /* a horrid kludge trying to make sure that this will fail on old kernels */ #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY) #define O_TMPFILE_MASK (__O_TMPFILE | O_DIRECTORY | O_CREAT) diff --git a/tools/include/uapi/asm-generic/fcntl.h b/tools/include/uapi/asm-generic/fcntl.h index e04a00fecb4a..69d8a000ec65 100644 --- a/tools/include/uapi/asm-generic/fcntl.h +++ b/tools/include/uapi/asm-generic/fcntl.h @@ -88,6 +88,10 @@ #define __O_TMPFILE 020000000 #endif +#ifndef O_CLOFORK +#define O_CLOFORK 040000000 /* set close_on_fork */ +#endif + /* a horrid kludge trying to make sure that this will fail on old kernels */ #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY) #define O_TMPFILE_MASK (__O_TMPFILE | O_DIRECTORY | O_CREAT) From patchwork Mon Apr 20 07:15:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nate Karstens X-Patchwork-Id: 11498253 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 11F0092C for ; Mon, 20 Apr 2020 07:16:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E7FA62078A for ; Mon, 20 Apr 2020 07:16:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=garmin.com header.i=@garmin.com header.b="GD2rUKFQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726200AbgDTHQX (ORCPT ); Mon, 20 Apr 2020 03:16:23 -0400 Received: from mail-bn7nam10on2139.outbound.protection.outlook.com ([40.107.92.139]:45312 "EHLO NAM10-BN7-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725773AbgDTHQW (ORCPT ); Mon, 20 Apr 2020 03:16:22 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mMZxtmgcFEwXlOQN3NZJeESwjTXvcSk21FrnNGhcOGc0NXebgv4RWmCkM5mLCFzTZVKo7ZZuXWVs54ctysP+ut+LeEHnDmRu6qILSPRi4g2Y6g+t5FAor/l4BoMal22ZxuXZ0o8lcHQ09S8TdINTN7glhWJoZfC5wLaxl7LrGry7HbTIdWsPS6WH5qOY6IW/9HI29J0ti3+GYmGjwUE8abuFzEwR7yE6/A8BOc+mszyNxBkwgFAxBMRSsvydfRLs71QogTGjNvYpi/Adhp/KkfZA/eZlVUmeKk8y/gXSizIVvdROasCWjhSlgk0ubM1aelL4S2ItxqLNz28mtOQXXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZOYvnp7bJqq5anIhuffI1wsXH6vHP2jlj1SDx9yGSv8=; b=LZuEPUzNXJ4d6Dn29tTBbittAhHKB3bmEc4JDeiQThUd8ZloiuIqiP67h3xhl+ckigFxWfm89MxiGMYydLE/nk5JPTjyRwIgeyUrrYAxJ8DB8aMH2G+KsaIUYU6gAAgxb2rXgQHQmB0fB/PegCis6MViS2HASLBEc9OK6BbJlvabZzadeA2tsFjQX44u6297TnRGjK+QTK0aQWN0FHqwxOu/zLN56ADiItLK9p9wTYcGsQmyZiORRzmvcV0C2h2wj3FgkB7o0JwW3IKLBjG1JUmSyAm4ddibKihD3y81Rkr1ACMnuj6oOvl0LAt7Ka/U2ePpw2RCtJeDFUsXiGzQ8w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 204.77.163.244) smtp.rcpttodomain=zeniv.linux.org.uk smtp.mailfrom=garmin.com; dmarc=pass (p=quarantine sp=quarantine pct=20) action=none header.from=garmin.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garmin.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZOYvnp7bJqq5anIhuffI1wsXH6vHP2jlj1SDx9yGSv8=; b=GD2rUKFQHpPI28/vU5DE0lbVWY/TIq+0dJEorFtSomb/XynkllZxnl7ZWTTS+j4xyGLZJGtvaMCiGgPJYsCgS8vluppKNGTPjwGC11P7PzDoyYRx2bvQQIQERPl34wklBe2w/QDB++AaLOaNM+JtwggExobZ/zsD4xuLd9LH5xz5FgnclvH1cSpx1QXGHXGd6TSWaWkmxJlbdujLLMNldZg7V0to2fk7e6kkRyOp26ajHt57zPh6zhEWdp2X41bwN896tsiKDaDBcDsce4pgdWvyBAy/gDAwbLrLwRz+Cl+6JuVjdXW1Hnfo8UHb7jC0wZBnMQwa9MXgO0tqM0jCDA== Received: from CO2PR05CA0089.namprd05.prod.outlook.com (2603:10b6:104:1::15) by MN2PR04MB5630.namprd04.prod.outlook.com (2603:10b6:208:ff::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.26; Mon, 20 Apr 2020 07:16:18 +0000 Received: from MW2NAM10FT016.eop-nam10.prod.protection.outlook.com (2603:10b6:104:1:cafe::fd) by CO2PR05CA0089.outlook.office365.com (2603:10b6:104:1::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2937.6 via Frontend Transport; Mon, 20 Apr 2020 07:16:18 +0000 Authentication-Results: spf=pass (sender IP is 204.77.163.244) smtp.mailfrom=garmin.com; zeniv.linux.org.uk; dkim=none (message not signed) header.d=none;zeniv.linux.org.uk; dmarc=pass action=none header.from=garmin.com; Received-SPF: Pass (protection.outlook.com: domain of garmin.com designates 204.77.163.244 as permitted sender) receiver=protection.outlook.com; client-ip=204.77.163.244; helo=edgetransport.garmin.com; Received: from edgetransport.garmin.com (204.77.163.244) by MW2NAM10FT016.mail.protection.outlook.com (10.13.155.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25 via Frontend Transport; Mon, 20 Apr 2020 07:16:17 +0000 Received: from OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) by olawpa-edge2.garmin.com (10.60.4.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.1466.3; Mon, 20 Apr 2020 02:16:16 -0500 Received: from ola-d01c000-vm.ad.garmin.com (10.5.84.15) by OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Mon, 20 Apr 2020 02:16:16 -0500 From: Nate Karstens To: Alexander Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , Richard Henderson , Ivan Kokshaysky , Matt Turner , "James E.J. Bottomley" , Helge Deller , "David S. Miller" , Jakub Kicinski , , , , , , , CC: Changli Gao , Nate Karstens Subject: [PATCH 3/4] fs: Add F_DUPFD_CLOFORK to fcntl(2) Date: Mon, 20 Apr 2020 02:15:47 -0500 Message-ID: <20200420071548.62112-4-nate.karstens@garmin.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200420071548.62112-1-nate.karstens@garmin.com> References: <20200420071548.62112-1-nate.karstens@garmin.com> MIME-Version: 1.0 X-ClientProxiedBy: OLAWPA-EXMB3.ad.garmin.com (10.5.144.15) To OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) X-TM-AS-Product-Ver: SMEX-12.5.0.1300-8.5.1020-25366.005 X-TM-AS-Result: No-5.713000-8.000000-10 X-TMASE-MatchedRID: eeq60RTJBf0Y5XJhZJs6SDCMW7zNwFaIvj5VWb6CNmV+SLLtNOiBhgml i9Nhz+dFfGzuoVn0Vs6PQi9XuOWoOHI/MxNRI7UkzNIobH2DzGExgXFacxiSBt9zZd3pUn7Kg7l N9LOvFDucmKqWNzxVSaWKPIetqooLqNz1cokKoAVCOHkvZleP7OuLFZZYlisfHxPMjOKY7A9qHX ONfTwSQsRB0bsfrpPIXzYxeQR1Dvs+lQOfpVOOU1nQzOr+jM9o0Y74GdR0vcESeUlcAF2N8EW/6 xYikolL X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--5.713000-8.000000 X-TMASE-Version: SMEX-12.5.0.1300-8.5.1020-25366.005 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:204.77.163.244;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:edgetransport.garmin.com;PTR:extedge.garmin.com;CAT:NONE;SFTY:;SFS:(10019020)(376002)(39860400002)(346002)(136003)(396003)(46966005)(110136005)(54906003)(86362001)(2906002)(8676002)(107886003)(36756003)(47076004)(70586007)(70206006)(1076003)(316002)(5660300002)(2616005)(246002)(7636003)(7696005)(8936002)(356005)(6666004)(4326008)(44832011)(186003)(26005)(478600001)(82740400003)(426003)(336012)(7416002)(921003);DIR:OUT;SFP:1102; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 890b902e-1863-4a1f-367e-08d7e4fab838 X-MS-TrafficTypeDiagnostic: MN2PR04MB5630: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-Forefront-PRVS: 03793408BA X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 63nbtYsEnWbgMp1mOgFKnp2HufTuWZry0p8SAGgeBNVv6Chc4EGItGv2W+jfFI8nNl0Fb0yvc//pYPuK6ZfN7gOSNzqc2FqW9fzK+KOZzA4K0T0PZZzO+9PH3I0NLkPifP8RkcdjSJgGLEnKuWm253bJJyeU99b2S8v4b8oTuaUMhoNB+CjXiHiYo+vuvxebtfwuKPfS/S/6AYaWU/RCO2wV9HnP4OFJepRfniCxL2D5jlPMZP/bif0nNJdgB3jXGkDf/GR2GZn6XRDvxIeeUu+aUDbW4LViaECJtHj5YvwEJuNHomD12vCsyBAwPe2OUrGsTK+OHTzcICPWUWMF3X9gMsBIRlIqI/l0MayalY2CR4u3/ulfVuvQ66lkZHzGWBhsa95eejQ4BgWmtI1WoRiJM+/vN5O+3Vk2e8kNV365PBdFwRzVuaeJumljpjEiFXRPCWOoCqUBUEkIaNK1clTYDJaCxn3c4Gx71Z3sAmofIsTUaRBjw+aTHs/EwtZqxooIohIItFBvv9440dPEog== X-OriginatorOrg: garmin.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2020 07:16:17.8533 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 890b902e-1863-4a1f-367e-08d7e4fab838 X-MS-Exchange-CrossTenant-Id: 38d0d425-ba52-4c0a-a03e-2a65c8e82e2d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=38d0d425-ba52-4c0a-a03e-2a65c8e82e2d;Ip=[204.77.163.244];Helo=[edgetransport.garmin.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5630 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Implement functionality for duplicating a file descriptor and having the close-on-fork flag automatically set in the new file descriptor. Signed-off-by: Nate Karstens --- fs/fcntl.c | 4 ++++ include/uapi/linux/fcntl.h | 3 +++ tools/include/uapi/linux/fcntl.h | 3 +++ 3 files changed, 10 insertions(+) diff --git a/fs/fcntl.c b/fs/fcntl.c index b59b27c3a338..43ca3e3dacc5 100644 --- a/fs/fcntl.c +++ b/fs/fcntl.c @@ -333,6 +333,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg, case F_DUPFD_CLOEXEC: err = f_dupfd(arg, filp, O_CLOEXEC); break; + case F_DUPFD_CLOFORK: + err = f_dupfd(arg, filp, O_CLOFORK); + break; case F_GETFD: err = get_close_on_exec(fd) ? FD_CLOEXEC : 0; err |= get_close_on_fork(fd) ? FD_CLOFORK : 0; @@ -439,6 +442,7 @@ static int check_fcntl_cmd(unsigned cmd) switch (cmd) { case F_DUPFD: case F_DUPFD_CLOEXEC: + case F_DUPFD_CLOFORK: case F_GETFD: case F_SETFD: case F_GETFL: diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h index ca88b7bce553..9e1069ff3a22 100644 --- a/include/uapi/linux/fcntl.h +++ b/include/uapi/linux/fcntl.h @@ -55,6 +55,9 @@ #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13) #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* Create a file descriptor with FD_CLOFORK set. */ +#define F_DUPFD_CLOFORK (F_LINUX_SPECIFIC_BASE + 15) + /* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set. diff --git a/tools/include/uapi/linux/fcntl.h b/tools/include/uapi/linux/fcntl.h index ca88b7bce553..9e1069ff3a22 100644 --- a/tools/include/uapi/linux/fcntl.h +++ b/tools/include/uapi/linux/fcntl.h @@ -55,6 +55,9 @@ #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13) #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) +/* Create a file descriptor with FD_CLOFORK set. */ +#define F_DUPFD_CLOFORK (F_LINUX_SPECIFIC_BASE + 15) + /* * Valid hint values for F_{GET,SET}_RW_HINT. 0 is "not set", or can be * used to clear any hints previously set. From patchwork Mon Apr 20 07:15:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nate Karstens X-Patchwork-Id: 11498249 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E5A7814DD for ; Mon, 20 Apr 2020 07:16:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C1B5F218AC for ; Mon, 20 Apr 2020 07:16:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=garmin.com header.i=@garmin.com header.b="M2rMcKlo" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726414AbgDTHQ1 (ORCPT ); Mon, 20 Apr 2020 03:16:27 -0400 Received: from mail-co1nam11on2138.outbound.protection.outlook.com ([40.107.220.138]:53754 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726161AbgDTHQZ (ORCPT ); Mon, 20 Apr 2020 03:16:25 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UBAnFg/mCOz+ErpHv2NSOBQWOptEUcUQGn7EOcENIW+lCIarPUF5LrxXfQKEAVD2V9m3Yx8gw+isTHG8tL1BCU1iXXgcXHdBC4X4yp8UzRgbJ6kaJzeTcjnS7YfOs4Tl86XHtxpGJgmIO+GBl85p2wBPg5RBTB6PKNaUb/OlNX1j75JG2fnGIWhRdPcjFrqud7S/TFhj7I+iOBfrgZG4qpuBeY0G+k1EpJF6NWvjHR5FbF0AHB9bsy+JQGyZyFKMBUTavIXq7Emu2srr8avo1wHx1BmcnGQLzX3851kONjaG21RlKhxhvnhm/wdvntVMq6IDrWcvTisu4vafNNBdwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4yYEsRUYimhigxmNAl7hxApckYLbS26D6tFLpPrG/lo=; b=Zon1sozQeDM3YBIMPfViOe3LN10WhMF3SakUUohxP5ezfqhUL9s3qwkiqqWDJUqmXqk+hEP7hLXqf867h7zEwfruRdaPPlYs7bq01XrpRctEoaXc/G/p31UmGsf3QJg47WDw9adaneWqUi/qA9JsVKAp3cBBglm8EKfogp7Greff4VrCXFS5d+IGhoVXm6qFfrQvRzIZgwff0+gxIIh5smezPhi0t9i9aad1AfMtuGAU0Uc3/4xPEqCV8Zec70X+HhCRT9O+MePimuWd8JBxmdZjX146GyfFBhCbV5bLX47Rmy0uAxZY313a/4L/Lijuom7MFZzX6y97rmAKfntHrw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 204.77.163.244) smtp.rcpttodomain=zeniv.linux.org.uk smtp.mailfrom=garmin.com; dmarc=pass (p=quarantine sp=quarantine pct=20) action=none header.from=garmin.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garmin.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4yYEsRUYimhigxmNAl7hxApckYLbS26D6tFLpPrG/lo=; b=M2rMcKlo4LyEmZNQpHoBi/g0BzY22mToxCtY0yoGAc5rF1HaOWRD1NK9R5pZPSSwHUushgRVWnn5EBHAtEu71forlt5lzGveAPDUoClFPD7FpBGoJiS30/iTJ3OclenMMfwqM5mZ+YIspiso+7Uo09TsWJU4uZ5mcNdwuZJ9xpvt9xeUstKdB7AmF/Wb2MNtnTwKGoyJl/67inseffQoO5E6FGf95vfih38vuqFRB10V3OCS279+X3vpnFMSLsB5xEh0Ai0ddOP8mh6HdliBqLG/7SLtyYZdGF3igJvvwJ0p1K+tBFteujhgE6eR5Jppb58hjdjWxT3Zn5Gx0iPbfA== Received: from DM6PR03CA0044.namprd03.prod.outlook.com (2603:10b6:5:100::21) by BN6PR04MB1202.namprd04.prod.outlook.com (2603:10b6:404:92::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.29; Mon, 20 Apr 2020 07:16:18 +0000 Received: from DM6NAM10FT049.eop-nam10.prod.protection.outlook.com (2603:10b6:5:100:cafe::d8) by DM6PR03CA0044.outlook.office365.com (2603:10b6:5:100::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25 via Frontend Transport; Mon, 20 Apr 2020 07:16:18 +0000 Authentication-Results: spf=pass (sender IP is 204.77.163.244) smtp.mailfrom=garmin.com; zeniv.linux.org.uk; dkim=none (message not signed) header.d=none;zeniv.linux.org.uk; dmarc=pass action=none header.from=garmin.com; Received-SPF: Pass (protection.outlook.com: domain of garmin.com designates 204.77.163.244 as permitted sender) receiver=protection.outlook.com; client-ip=204.77.163.244; helo=edgetransport.garmin.com; Received: from edgetransport.garmin.com (204.77.163.244) by DM6NAM10FT049.mail.protection.outlook.com (10.13.153.121) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.25 via Frontend Transport; Mon, 20 Apr 2020 07:16:17 +0000 Received: from OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) by olawpa-edge1.garmin.com (10.60.4.227) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.1466.3; Mon, 20 Apr 2020 02:16:17 -0500 Received: from ola-d01c000-vm.ad.garmin.com (10.5.84.15) by OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Mon, 20 Apr 2020 02:16:16 -0500 From: Nate Karstens To: Alexander Viro , Jeff Layton , "J. Bruce Fields" , Arnd Bergmann , Richard Henderson , Ivan Kokshaysky , Matt Turner , "James E.J. Bottomley" , Helge Deller , "David S. Miller" , Jakub Kicinski , , , , , , , CC: Changli Gao , Nate Karstens Subject: [PATCH 4/4] net: Add SOCK_CLOFORK Date: Mon, 20 Apr 2020 02:15:48 -0500 Message-ID: <20200420071548.62112-5-nate.karstens@garmin.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: <20200420071548.62112-1-nate.karstens@garmin.com> References: <20200420071548.62112-1-nate.karstens@garmin.com> MIME-Version: 1.0 X-ClientProxiedBy: OLAWPA-EXMB3.ad.garmin.com (10.5.144.15) To OLAWPA-EXMB7.ad.garmin.com (10.5.144.21) X-TM-AS-Product-Ver: SMEX-12.5.0.1300-8.5.1020-25366.005 X-TM-AS-Result: No-5.988100-8.000000-10 X-TMASE-MatchedRID: 3IdSvgGCM2OSsyjfsjrH/tKhw1CGAxrILoFHmcx3krwAIXlMppp3Xw5a yixA3COc1+Otxunw83huL3ESIrARlyHhSBQfglfsA9lly13c/gHaKQ0GLhRPDxh58BVvx3LmF5J Ui8H1I3XP8poBdrWc73VybJRFpSevgRZdz333xpBJUdgxNDUXWmf6wD367VgtDs0BGU1luwj6p1 jlhLAJAsAhMlHsyVwnkA7KM/+6n4wylv9EjaWo1Q97mDMXdNW364sVlliWKx8fE8yM4pjsDwtuK BGekqUpOlxBO2IcOBbnd1hvM4M+M/Oj3yVarJf2NRETSGY0whj9UjqKN8S2I8jQVQJrd8Qi X-TM-AS-User-Approved-Sender: No X-TM-AS-User-Blocked-Sender: No X-TMASE-Result: 10--5.988100-8.000000 X-TMASE-Version: SMEX-12.5.0.1300-8.5.1020-25366.005 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:204.77.163.244;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:edgetransport.garmin.com;PTR:extedge.garmin.com;CAT:NONE;SFTY:;SFS:(10019020)(396003)(376002)(136003)(346002)(39860400002)(46966005)(478600001)(8936002)(70206006)(8676002)(70586007)(7636003)(107886003)(82740400003)(7416002)(4326008)(6666004)(316002)(7696005)(86362001)(26005)(110136005)(2906002)(44832011)(356005)(336012)(47076004)(5660300002)(1076003)(426003)(246002)(186003)(54906003)(36756003)(2616005)(921003);DIR:OUT;SFP:1102; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9ab779eb-5e35-43ed-9d80-08d7e4fab828 X-MS-TrafficTypeDiagnostic: BN6PR04MB1202: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-Forefront-PRVS: 03793408BA X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0CcKh8Qf3FddEbi5nhFMe1VjNNcgSCdxvxgYKQ0r8QLp6udo80abRk9RAM73VPsVotqQ5hMJQ+Y9jQnYZv93duqDQj4t4HAhGnhwdZKZbrwulBDWqLHm4oFlodAX1DxEYVDFsfmRkT7zTVdzv//mDk7HYKvDA2zc5YtyrVAcxYtmkCj4V83xUnmzcp8k3Zou/Chtri2EbJ37H+3Xy3xR3Ow1l25NxHf5HLZtHFySl51u6naoWhPFm0NmTIdDlJHThvHNdqP1zhWTOaQFv4WuxRTyuaeGZrdqK0V0o3Bp2UqxKH2Yj01ylctsK+nXszGvzi8ftQnEuHrbpqgtr5ady6lYsiKe+yXOGk37Rj8xRqD3njbWF8YlfoEiupOIcNr//YR8TJhfwX8diaZ1Zrz8DFgGmvIgrX9Et/Lz/Jq1QdlIMWGUYDasnW4I4rjLSCY10yLXXL6qhHrB4BMMoUUIY1JWu/Gq46QK2Zj+aOE75r8rseRvCy4qc2sZ5z+T6duX872YkGq8ceuSUXC7KeM9HQ== X-OriginatorOrg: garmin.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2020 07:16:17.7851 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9ab779eb-5e35-43ed-9d80-08d7e4fab828 X-MS-Exchange-CrossTenant-Id: 38d0d425-ba52-4c0a-a03e-2a65c8e82e2d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=38d0d425-ba52-4c0a-a03e-2a65c8e82e2d;Ip=[204.77.163.244];Helo=[edgetransport.garmin.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR04MB1202 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Implements a new socket flag that automatically sets the close-on-fork flag for sockets created using socket(2), socketpair(2), and accept4(2). Signed-off-by: Nate Karstens --- include/linux/net.h | 3 ++- net/socket.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/linux/net.h b/include/linux/net.h index 6451425e828f..57663c9dc8c4 100644 --- a/include/linux/net.h +++ b/include/linux/net.h @@ -17,7 +17,7 @@ #include #include #include -#include /* For O_CLOEXEC and O_NONBLOCK */ +#include /* For O_CLOEXEC, O_CLOFORK, and O_NONBLOCK */ #include #include #include @@ -73,6 +73,7 @@ enum sock_type { /* Flags for socket, socketpair, accept4 */ #define SOCK_CLOEXEC O_CLOEXEC +#define SOCK_CLOFORK O_CLOFORK #ifndef SOCK_NONBLOCK #define SOCK_NONBLOCK O_NONBLOCK #endif diff --git a/net/socket.c b/net/socket.c index 2eecf1517f76..ba6e971c7e78 100644 --- a/net/socket.c +++ b/net/socket.c @@ -1511,12 +1511,14 @@ int __sys_socket(int family, int type, int protocol) /* Check the SOCK_* constants for consistency. */ BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC); + BUILD_BUG_ON(SOCK_CLOFORK != O_CLOFORK); BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK); BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK); + BUILD_BUG_ON(SOCK_CLOFORK & SOCK_TYPE_MASK); BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK); flags = type & ~SOCK_TYPE_MASK; - if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) + if (flags & ~(SOCK_CLOEXEC | SOCK_CLOFORK | SOCK_NONBLOCK)) return -EINVAL; type &= SOCK_TYPE_MASK; @@ -1527,7 +1529,7 @@ int __sys_socket(int family, int type, int protocol) if (retval < 0) return retval; - return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK)); + return sock_map_fd(sock, flags & (O_CLOEXEC | O_CLOFORK | O_NONBLOCK)); } SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol) @@ -1547,7 +1549,7 @@ int __sys_socketpair(int family, int type, int protocol, int __user *usockvec) int flags; flags = type & ~SOCK_TYPE_MASK; - if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) + if (flags & ~(SOCK_CLOEXEC | SOCK_CLOFORK | SOCK_NONBLOCK)) return -EINVAL; type &= SOCK_TYPE_MASK; @@ -1715,7 +1717,7 @@ int __sys_accept4_file(struct file *file, unsigned file_flags, int err, len, newfd; struct sockaddr_storage address; - if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) + if (flags & ~(SOCK_CLOEXEC | SOCK_CLOFORK | SOCK_NONBLOCK)) return -EINVAL; if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK)) @@ -3628,8 +3630,8 @@ EXPORT_SYMBOL(kernel_listen); * @newsock: new connected socket * @flags: flags * - * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0. - * If it fails, @newsock is guaranteed to be %NULL. + * @flags must be SOCK_CLOEXEC, SOCK_CLOFORK, SOCK_NONBLOCK, + * or 0. If it fails, @newsock is guaranteed to be %NULL. * Returns 0 or an error. */