Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2676153pxa; Mon, 17 Aug 2020 16:18:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3I5Dr6bjUh5j0rz/x+i3gF5AU1rL16UMF9eGSS7e2jKydBgL6V7/KZB/CB9mOUBquVKMl X-Received: by 2002:a17:906:4a4e:: with SMTP id a14mr17769396ejv.450.1597706320549; Mon, 17 Aug 2020 16:18:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597706320; cv=none; d=google.com; s=arc-20160816; b=G5ZkZ5ZUTeetYmSmJwSgA4zPHZTCOKUJe3cgYH9wO/bog0N8+lIqfuFzFIrgVo9kyN NAkZJJtA620x0XRz7W1zLpZZgN5k6mUYlBtmV2vKXM/wJ2XzgxDta7MBASSmKeyWTUuc DYGRwlc8WohNg7vhoJTLnq/KQTwYWUtczV6+6osndN3HIi8PsSYqKhDyAIdxEyCczDlt 4wBYGQ+oUAg0rBRJ+ZwTBc2V7ZYiKIZ12VM0y3vIkSk4TY/aZOm1bhZG6M9Xjds1iU29 vkJ9vNSyYQMwSyphZKWy7DyfEQ0jXC4bcPOQ3NvmojJlgZrgNfAqRE+wXLv+4uAy5LGh ii/w== 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=AAcVEu4OtKrVk9nrJb7xDkn0TDGRRiPNYojCt8+z3c4=; b=j4t2Q/KNmvYMNZGS+qn4TlTrZuDc9IvbWR7P+dCER8slgwNFeiM6h5dCHpgiJeZS1L kufycVYxRS/ngu8zvjVs2NwIIkV7rAgTxFW47WuLVK/2LxNXPDWb3qwyBoraMDxjo4o4 AhbG2PZJx2pgGTikZ006rEyaYFukmZIStAds+UelEKeFFQb694vA8W4udpV4YGJL2ngP gNfb8fIoEOfL7M0dSPrKAvXdKr/XR54okC7sCzuh2oG70VCgw5HUOFJcg3FaHb/WVhtL fM6sBI/+hnXdmMIq8mYfMVHGOQfSSCKal6Z0YUYusu5x9Nqyh3M2ZpfSVxcIMUgaCrn0 GTBg== 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 x30si11382565edc.80.2020.08.17.16.18.17; Mon, 17 Aug 2020 16:18:40 -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 S1730351AbgHQWMU (ORCPT + 99 others); Mon, 17 Aug 2020 18:12:20 -0400 Received: from out01.mta.xmission.com ([166.70.13.231]:47250 "EHLO out01.mta.xmission.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730271AbgHQWLv (ORCPT ); Mon, 17 Aug 2020 18:11:51 -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 1k7nM6-004wAs-AJ; Mon, 17 Aug 2020 16:11:50 -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 1k7nKe-0004PB-Bh; Mon, 17 Aug 2020 16:10:21 -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:20 -0500 Message-Id: <20200817220425.9389-12-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=1k7nKe-0004PB-Bh;;;mid=<20200817220425.9389-12-ebiederm@xmission.com>;;;hst=in02.mta.xmission.com;;;ip=68.227.160.95;;;frm=ebiederm@xmission.com;;;spf=neutral X-XM-AID: U2FsdGVkX1+qmfRJ3lIgWJPZ2rZtUxGKy6lZEDFtyz4= 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 sa06.xmission.com X-Spam-Level: ** X-Spam-Status: No, score=2.0 required=8.0 tests=ALL_TRUSTED,BAYES_50, DCC_CHECK_NEGATIVE,T_TooManySym_01,T_TooManySym_02,XMNoVowels, XMSubLong 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.7 XMSubLong Long Subject * -0.0 DCC_CHECK_NEGATIVE Not listed in DCC * [sa06 0; Body=1 Fuz1=1 Fuz2=1] * 0.0 T_TooManySym_02 5+ unique symbols in subject * 0.0 T_TooManySym_01 4+ unique symbols in subject X-Spam-DCC: ; sa06 0; Body=1 Fuz1=1 Fuz2=1 X-Spam-Combo: **;linux-kernel@vger.kernel.org X-Spam-Relay-Country: X-Spam-Timing: total 459 ms - load_scoreonly_sql: 0.15 (0.0%), signal_user_changed: 12 (2.5%), b_tie_ro: 10 (2.2%), parse: 1.82 (0.4%), extract_message_metadata: 18 (3.9%), get_uri_detail_list: 2.6 (0.6%), tests_pri_-1000: 20 (4.3%), tests_pri_-950: 1.42 (0.3%), tests_pri_-900: 1.15 (0.3%), tests_pri_-90: 154 (33.6%), check_bayes: 152 (33.1%), b_tokenize: 10 (2.2%), b_tok_get_all: 12 (2.7%), b_comp_prob: 5 (1.1%), b_tok_touch_all: 114 (24.9%), b_finish: 1.23 (0.3%), tests_pri_0: 238 (51.7%), check_dkim_signature: 0.57 (0.1%), check_dkim_adsp: 2.2 (0.5%), poll_dns_idle: 0.56 (0.1%), tests_pri_10: 2.1 (0.5%), tests_pri_500: 8 (1.7%), rewrite_mail: 0.00 (0.0%) Subject: [PATCH 12/17] proc/fd: In fdinfo seq_show don't use get_files_struct 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. Instead hold task_lock for the duration that task->files needs to be stable in seq_show. The task_lock was already taken in get_files_struct, and so skipping get_files_struct performs less work overall, and avoids the problems with the files_struct reference count. [1] https://lkml.kernel.org/r/20180915160423.GA31461@redhat.com Suggested-by: Oleg Nesterov Signed-off-by: "Eric W. Biederman" --- fs/proc/fd.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/proc/fd.c b/fs/proc/fd.c index d9fee5390fd7..0b46eea154b7 100644 --- a/fs/proc/fd.c +++ b/fs/proc/fd.c @@ -28,9 +28,8 @@ static int seq_show(struct seq_file *m, void *v) if (!task) return -ENOENT; - files = get_files_struct(task); - put_task_struct(task); - + task_lock(task); + files = task->files; if (files) { unsigned int fd = proc_fd(m->private); @@ -47,8 +46,9 @@ static int seq_show(struct seq_file *m, void *v) ret = 0; } spin_unlock(&files->file_lock); - put_files_struct(files); } + task_unlock(task); + put_task_struct(task); if (ret) return ret; @@ -57,6 +57,7 @@ static int seq_show(struct seq_file *m, void *v) (long long)file->f_pos, f_flags, real_mount(file->f_path.mnt)->mnt_id); + /* show_fd_locks() never deferences files so a stale value is safe */ show_fd_locks(m, file, files); if (seq_has_overflowed(m)) goto out; -- 2.25.0