Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2675651pxa; Mon, 17 Aug 2020 16:17:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJHB1bPgchp64Ju4F7XFdeUBv0ioxV9dlauAWSt/ZMITqPNKaACEdqvBZCnnV1EhuXmwPE X-Received: by 2002:a17:906:43c9:: with SMTP id j9mr17282283ejn.542.1597706265718; Mon, 17 Aug 2020 16:17:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597706265; cv=none; d=google.com; s=arc-20160816; b=fk/Bz5ay3LitDf6xQ/ZHypluCtxMGN6/Knv8o34eYK3a8prsFDDKUExnJ8hJmmDst0 6PG3cbzr0krzqXttzKmei3Oc7lJlp/6BFhB2jW/XkjxYSgmni6IhGYNLcS/YtUUg712g SP/AvBI3TtY7A2fEghjow/NDRlzrhvo5in+Gj+6eiejOdn3V2tObjdPAStP+RmAu37ii wtunY0OWVocUAsV4jOK45oEvqArWcmRaWmEbDNdaf+RrA4uCTZBd9uTzkoI9LTRBtTXa m9/D+F3M8y/0q0dtL9/ALi3J6STq5qWo+iw1mbsKxTIhGItInzeogjLjLQjl3/z2FQbf snFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:subject:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:cc:to:from; bh=A9iEdLEC0P6Coo+/bbUAF3BA9UlqLHQy/pn4xiG4jGQ=; b=MwTpHyFfvnt8Yr0VVN0IrByY+oRermuuwNrpvx5Fn3n4jc1Nkdot6NDQnCBP1TXr4Z WJWPo3G6fwyMlZDPQ13Xq8uL4cPqKiRKQYsuSkZLegrrFeIpAD4r6jpZour7TxACVnvU X7spawyRjxYNH++1e+a0o7bTqIiUsrWdfGP3/Ks+M0CG347YlFVAvtv8kSqdthfyG9Rk YtHo7svgAESUXUmqZjbwYYQYrRpeQNNjSh2C3nRZWVYmktNzmdXm8uskXAr5N/Y5ajF/ Jyhm7i3RfdKq+BbxipGPehwT92LI/tz3IqyRugQdTh5PblB+4CGt1TlvtI5o03xx/Bza Ua7Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id fx20si11789046ejb.581.2020.08.17.16.17.22; Mon, 17 Aug 2020 16:17:45 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=xmission.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387430AbgHQWK3 (ORCPT + 99 others); Mon, 17 Aug 2020 18:10:29 -0400 Received: from out01.mta.xmission.com ([166.70.13.231]:46586 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730177AbgHQWKN (ORCPT ); Mon, 17 Aug 2020 18:10:13 -0400 Received: from in02.mta.xmission.com ([166.70.13.52]) by out01.mta.xmission.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1k7nKW-004vtM-PI; Mon, 17 Aug 2020 16:10:12 -0600 Received: from ip68-227-160-95.om.om.cox.net ([68.227.160.95] helo=x220.int.ebiederm.org) by in02.mta.xmission.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.87) (envelope-from ) id 1k7nKV-0004PB-W4; Mon, 17 Aug 2020 16:10:12 -0600 From: "Eric W. Biederman" To: linux-kernel@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, criu@openvz.org, bpf@vger.kernel.org, Linus Torvalds , Alexander Viro , Christian Brauner , Oleg Nesterov , Cyrill Gorcunov , Jann Horn , Kees Cook , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Jeff Layton , Miklos Szeredi , Matthew Wilcox , "J. Bruce Fields" , Matthew Wilcox , Trond Myklebust , Chris Wright , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , John Fastabend , KP Singh , "Eric W. Biederman" Date: Mon, 17 Aug 2020 17:04:18 -0500 Message-Id: <20200817220425.9389-10-ebiederm@xmission.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <87ft8l6ic3.fsf@x220.int.ebiederm.org> References: <87ft8l6ic3.fsf@x220.int.ebiederm.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-XM-SPF: eid=1k7nKV-0004PB-W4;;;mid=<20200817220425.9389-10-ebiederm@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1/rGzp3jrWwVWIWQnolVzaJMEaOuCOGy6w= X-SA-Exim-Connect-IP: 68.227.160.95 X-SA-Exim-Mail-From: ebiederm@xmission.com X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on sa07.xmission.com X-Spam-Level: * X-Spam-Status: No, score=1.3 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,T_TooManySym_01,XMNoVowels autolearn=disabled version=3.4.2 X-Spam-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.8 BAYES_50 BODY: Bayes spam probability is 40 to 60% * [score: 0.5000] * 1.5 XMNoVowels Alpha-numberic number with no vowels * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa07 0; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: ; sa07 0; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: *;linux-kernel@vger.kernel.org X-Spam-Relay-Country: X-Spam-Timing: total 405 ms - load_scoreonly_sql: 0.08 (0.0%), signal_user_changed: 11 (2.8%), b_tie_ro: 10 (2.4%), parse: 1.50 (0.4%), extract_message_metadata: 14 (3.3%), get_uri_detail_list: 1.99 (0.5%), tests_pri_-1000: 16 (3.9%), tests_pri_-950: 1.36 (0.3%), tests_pri_-900: 1.21 (0.3%), tests_pri_-90: 76 (18.9%), check_bayes: 75 (18.5%), b_tokenize: 10 (2.5%), b_tok_get_all: 9 (2.3%), b_comp_prob: 3.7 (0.9%), b_tok_touch_all: 48 (11.9%), b_finish: 0.90 (0.2%), tests_pri_0: 260 (64.3%), check_dkim_signature: 1.04 (0.3%), check_dkim_adsp: 2.4 (0.6%), poll_dns_idle: 0.65 (0.2%), tests_pri_10: 3.3 (0.8%), tests_pri_500: 16 (3.9%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH 10/17] proc/fd: In proc_readfd_common use fnext_task X-Spam-Flag: No X-SA-Exim-Version: 4.2.1 (built Thu, 05 May 2016 13:38:54 -0600) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When discussing[1] exec and posix file locks it was realized that none of the callers of get_files_struct fundamentally needed to call get_files_struct, and that by switching them to helper functions instead it will both simplify their code and remove unnecessary increments of files_struct.count. Those unnecessary increments can result in exec unnecessarily unsharing files_struct which breaking posix locks, and it can result in fget_light having to fallback to fget reducing system performance. Using fnext_task simplifies proc_readfd_common, by moving the checking for the maximum file descritor into the generic code, and by remvoing the need for capturing and releasing a reference on files_struct. [1] https://lkml.kernel.org/r/20180915160423.GA31461@redhat.com Suggested-by: Oleg Nesterov Signed-off-by: Eric W. Biederman --- fs/proc/fd.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/fs/proc/fd.c b/fs/proc/fd.c index abfdcb21cc79..d9fee5390fd7 100644 --- a/fs/proc/fd.c +++ b/fs/proc/fd.c @@ -218,7 +218,6 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx, instantiate_t instantiate) { struct task_struct *p = get_proc_task(file_inode(file)); - struct files_struct *files; unsigned int fd; if (!p) @@ -226,22 +225,18 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx, if (!dir_emit_dots(file, ctx)) goto out; - files = get_files_struct(p); - if (!files) - goto out; rcu_read_lock(); - for (fd = ctx->pos - 2; - fd < files_fdtable(files)->max_fds; - fd++, ctx->pos++) { + for (fd = ctx->pos - 2;; fd++, ctx->pos++) { struct file *f; struct fd_data data; char name[10 + 1]; unsigned int len; - f = fcheck_files(files, fd); + f = fnext_task(p, &fd); + ctx->pos = fd; if (!f) - continue; + break; data.mode = f->f_mode; rcu_read_unlock(); data.fd = fd; @@ -250,13 +245,11 @@ static int proc_readfd_common(struct file *file, struct dir_context *ctx, if (!proc_fill_cache(file, ctx, name, len, instantiate, p, &data)) - goto out_fd_loop; + goto out; cond_resched(); rcu_read_lock(); } rcu_read_unlock(); -out_fd_loop: - put_files_struct(files); out: put_task_struct(p); return 0; -- 2.25.0