Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp2889177ybh; Mon, 16 Mar 2020 11:38:25 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvINCujiH8/1tLHuP2UCV7rZ9NY4cAyKi9ktigOe0S0DN+e7IS5YkussH0nWidyfg8pdgPk X-Received: by 2002:a9d:64b:: with SMTP id 69mr458808otn.237.1584383904965; Mon, 16 Mar 2020 11:38:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584383904; cv=none; d=google.com; s=arc-20160816; b=s99n5u7+nCpreUC8fFOPePXKr948iR58bDtRpqhIqgBzqdtQ8Fyq5e8EqR7c3RFyo1 M7ydb0lFiIbJJNvMQGRaRmNTb0TbiB1jAa6eWPXG+kKbq7fGGaj4IlcorkRH9yIj8FT4 LgIclXVoVlISgfvulEqbfDCFlA/PWk//a3YtJUkZ5Bpijrj7GwXI4wPo1K1RjAwmF6hS 5ApdcdZKIMLBzkZO9jxT3Mf7qqq9u14/mH4T849GNDHVQmvjLgoMP/QzfI8Cfz59NBFB c85OCahRZuNIBc7kJkGv3e/fiKmQX3OCP/Cjxaus22VCcFEdfyCH/OLXXpg1ttS77wu6 33Cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=SDZkdFn866FZyb8wKcZ5NVbcYj5DmQdl6rHHmLmswtw=; b=mNJHkf4Xlpn1YVt5jmZhbpjkJBaI4mJTlcZ2CaME1P2H1oXQlZ9eYrYGyD5wGET/nb ZUmHMzDPlyGScNJTdM8wthwY00iIYnQ2UVd385F5fbNeyV1OOEm3ti7TKj6p1HwneiYZ OQvghBNWPef8jx+97m8xVqVDIIKYx6524nJxzO2gYM1txbIp5CDoynlj2RhFXK2xlbNY CxCNIchsioW4Qnmw02VUaMnU6R6w1pBJcNLNtU7h0uoddq6TbpkQBvp4d60/VQLjeXJF 6IhVznwkIA37oC9/wuLXEoYlmXxsnqVtY/ETl/Q5yQXF2uDui2DjoIxiuhcqxgdz0oHn D8zQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g35si350030otg.32.2020.03.16.11.38.12; Mon, 16 Mar 2020 11:38:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732347AbgCPShf (ORCPT + 99 others); Mon, 16 Mar 2020 14:37:35 -0400 Received: from zeniv.linux.org.uk ([195.92.253.2]:41614 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731967AbgCPShf (ORCPT ); Mon, 16 Mar 2020 14:37:35 -0400 Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92.3 #3 (Red Hat Linux)) id 1jDua6-00CwUo-Nf; Mon, 16 Mar 2020 18:35:18 +0000 Date: Mon, 16 Mar 2020 18:35:18 +0000 From: Al Viro To: Peter Zijlstra Cc: Ling Ma , linux-kernel@vger.kernel.org, Ingo Molnar , "ling.ma" Subject: Re: [RFC PATCH] locks:Remove spinlock in unshare_files Message-ID: <20200316183518.GZ23230@ZenIV.linux.org.uk> References: <20200313031017.71090-1-ling.ma.program@gmail.com> <20200316133916.GD12561@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200316133916.GD12561@hirez.programming.kicks-ass.net> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Mar 16, 2020 at 02:39:16PM +0100, Peter Zijlstra wrote: > > > diff --git a/kernel/fork.c b/kernel/fork.c > > > index 60a1295..fe54600 100644 > > > --- a/kernel/fork.c > > > +++ b/kernel/fork.c > > > @@ -3041,9 +3041,7 @@ int unshare_files(struct files_struct **displaced) > > > return error; > > > } > > > *displaced = task->files; > > > - task_lock(task); > > > - task->files = copy; > > > - task_unlock(task); > > > + WRITE_ONCE(task->files, copy); > > > return 0; > > > } > > AFAICT this is completely and utterly buggered. > > IFF task->files was lockless, like say RCU, then you'd still need > smp_store_release(). But if we look at fs/file.c then everything uses > task_lock() and removing it like the above is actively broken. The problem is not fs/file.c; it's the code that does (read-only) access to *other* threads' ->files. procfs, SAK, some cgroup shite (pardon the redundancy)... All of those rely upon task_lock. FWIW, having just grepped around, I'm worried about the crap io_uring is pulling off - interplay with unshare(2) could be unpleasant. In any case - task_lock in the code that assigns to ->files (and it's not just unshare_files()) serves to protect the 3rd-party readers (including get_files_struct()) from having the fucker taken apart under them. It's not just freeing the thing - it's the entire close_files(). And no, we do *NOT* want to convert everything to get_files_struct() + being clever in it. I would rather have get_files_struct() taken out and shot, TBH - the only real reason it hadn't been killed years ago is the loop in proc_readfd_common()... I'd prefer to have 3rd-party readers indicate their interest in a way that would be distinguishable from normal references, with close_files() waiting until all of those are gone. One way to do that would be * secondary counter in files_struct * rcu-delayed freeing of actual structure (not a problem) * rcu_read_lock in 3rd-party readers (among other things it means that proc_readfd_common() would need to be rearchitected a bit) * close_files() starting with subtraction of large constant from the secondary counter and then spinning until it gets to - * 3rd-party readers (under rcu_read_lock()) fetching task->files, bumping the secondary counter unless it's negative, doing their thing, then decrementing the counter.