From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8FB77C54EAA for ; Mon, 23 Jan 2023 18:24:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 154276B0074; Mon, 23 Jan 2023 13:24:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 103F36B0075; Mon, 23 Jan 2023 13:24:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F0DEE6B0078; Mon, 23 Jan 2023 13:24:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E2E336B0074 for ; Mon, 23 Jan 2023 13:24:04 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7F6DF141AAC for ; Mon, 23 Jan 2023 18:24:04 +0000 (UTC) X-FDA: 80386888008.11.AA0D5C6 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf19.hostedemail.com (Postfix) with ESMTP id B1DF31A0011 for ; Mon, 23 Jan 2023 18:24:01 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=GTYwYH7n; spf=none (imf19.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674498242; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=B+yzq/Z0e8jumLGKSlhRVExf8hQcAkabvnjcTYI9KiY=; b=wPYkLC4MWEdd89kTsGnv6EOb/18HXATslhiNwqqWiv2Pxoc/XGsRbI0S98aP0keVXc7Mas NWJA0I7xuVInHYf5xvIMvFsE31nLC4aXvnxYQgPZvXYywa4Rb2HieQKJbwY6b5KC2SfDBZ WfrgeoXHkfIqWNr2VCRMovEpTw3aLkM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=GTYwYH7n; spf=none (imf19.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674498242; a=rsa-sha256; cv=none; b=dsAbidxLQo2DwMvNaBMidQvrryvbNDPorK2+RqSKwLuewpIHOg7k5BiihCwIwHHRxoXFk/ GITyTWGjjNS1NIDfI6E7rd9QjKO9HfYVvEFmfdarWgQVbmuB4WiqK5cr4Eft3ufuOdFper KvPNdefk2hkRheRv4aJloxDK1BaiAFI= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=B+yzq/Z0e8jumLGKSlhRVExf8hQcAkabvnjcTYI9KiY=; b=GTYwYH7nNq+4sMY3Vk3QgXskdG 2L0ANeqflfK/jWWxB3yA/UDdJse/cePbqp6JZ8LYI+ze4uQ2DiYVBbpxRZqbnNpgx7+PiwTdZAWQA SPQ8VEgtJD+rLax4CdxZofnTxPHlBnCYqKh2woEwBT70dhcwKjgs/lXEeBXNZqJJqrbREEMkZCIOC eUu/2OzjQqN0hU4Otc/g+SXPiM42qqmVRB3hjb21IaWZjIZNTf9WW/47lCPpArGf0Iosgqs1mCKFf 0wABKYADlN5xm08SG1OokKkWTXE5xfSYdcp7g2ktegETsutXNqTGIlbKeQljQR19PgiMGAq4KA1NB eZTb8cWA==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pK1TI-004R6r-TB; Mon, 23 Jan 2023 18:23:09 +0000 Date: Mon, 23 Jan 2023 18:23:08 +0000 From: Matthew Wilcox To: Suren Baghdasaryan Cc: Michal Hocko , "Liam R. Howlett" , akpm@linux-foundation.org, michel@lespinasse.org, jglisse@google.com, vbabka@suse.cz, hannes@cmpxchg.org, mgorman@techsingularity.net, dave@stgolabs.net, peterz@infradead.org, ldufour@linux.ibm.com, laurent.dufour@fr.ibm.com, paulmck@kernel.org, luto@kernel.org, songliubraving@fb.com, peterx@redhat.com, david@redhat.com, dhowells@redhat.com, hughd@google.com, bigeasy@linutronix.de, kent.overstreet@linux.dev, punit.agrawal@bytedance.com, lstoakes@gmail.com, peterjung1337@gmail.com, rientjes@google.com, axelrasmussen@google.com, joelaf@google.com, minchan@google.com, jannh@google.com, shakeelb@google.com, tatashin@google.com, edumazet@google.com, gthelen@google.com, gurua@google.com, arjunroy@google.com, soheil@google.com, hughlynch@google.com, leewalsh@google.com, posk@google.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, x86@kernel.org, linux-kernel@vger.kernel.org, kernel-team@android.com Subject: Re: [PATCH 39/41] kernel/fork: throttle call_rcu() calls in vm_area_free Message-ID: References: <20230120170815.yuylbs27r6xcjpq5@revolver> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: B1DF31A0011 X-Stat-Signature: wb9atd41ghg5mdbs96zw8hnq49gmcgtx X-Rspam-User: X-HE-Tag: 1674498241-136489 X-HE-Meta: U2FsdGVkX18/Nhhr7p/nAtBXIefe/CFOTALz0pPgtf7g8e7kGysQffWJ8o2o+utmLWGfNGLZQZw25bleInA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iIZ8XsnXzI8KjYUm5P7iE2WPG3wV3EXH37GAPf+nYelw1ah/9lhy/vSSJbepqwBho6plejdLBxgkT9LYYpKK/qm0tzxt9f+Jh9z34TfOUxGtXtonarQtRiAQQlGu+Frh80TGmDwoG8G5UcbxUx7JxL4fZk7Ef9uuepEnzSSiU X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Jan 23, 2023 at 09:46:20AM -0800, Suren Baghdasaryan wrote: > On Mon, Jan 23, 2023 at 9:16 AM Michal Hocko wrote: > > > > On Mon 23-01-23 09:07:34, Suren Baghdasaryan wrote: > > > On Mon, Jan 23, 2023 at 8:55 AM Michal Hocko wrote: > > > > > > > > On Mon 23-01-23 08:22:53, Suren Baghdasaryan wrote: > > > > > On Mon, Jan 23, 2023 at 1:56 AM Michal Hocko wrote: > > > > > > > > > > > > On Fri 20-01-23 09:50:01, Suren Baghdasaryan wrote: > > > > > > > On Fri, Jan 20, 2023 at 9:32 AM Matthew Wilcox wrote: > > > > > > [...] > > > > > > > > The page fault handler (or whatever other reader -- ptrace, proc, etc) > > > > > > > > should have a refcount on the mm_struct, so we can't be in this path > > > > > > > > trying to free VMAs. Right? > > > > > > > > > > > > > > Hmm. That sounds right. I checked process_mrelease() as well, which > > > > > > > operated on mm with only mmgrab()+mmap_read_lock() but it only unmaps > > > > > > > VMAs without freeing them, so we are still good. Michal, do you agree > > > > > > > this is ok? > > > > > > > > > > > > Don't we need RCU procetions for the vma life time assurance? Jann has > > > > > > already shown how rwsem is not safe wrt to unlock and free without RCU. > > > > > > > > > > Jann's case requires a thread freeing the VMA to be blocked on vma > > > > > write lock waiting for the vma real lock to be released by a page > > > > > fault handler. However exit_mmap() means mm->mm_users==0, which in > > > > > turn suggests that there are no racing page fault handlers and no new > > > > > page fault handlers will appear. Is that a correct assumption? If so, > > > > > then races with page fault handlers can't happen while in exit_mmap(). > > > > > Any other path (other than page fault handlers), accesses vma->lock > > > > > under protection of mmap_lock (for read or write, does not matter). > > > > > One exception is when we operate on an isolated VMA, then we don't > > > > > need mmap_lock protection, but exit_mmap() does not deal with isolated > > > > > VMAs, so out of scope here. exit_mmap() frees vm_area_structs under > > > > > protection of mmap_lock in write mode, so races with anything other > > > > > than page fault handler should be safe as they are today. > > > > > > > > I do not see you talking about #PF (RCU + vma read lock protected) with > > > > munmap. It is my understanding that the latter will synchronize over per > > > > vma lock (along with mmap_lock exclusive locking). But then we are back > > > > to the lifetime guarantees, or do I miss anything. > > > > > > munmap() or any VMA-freeing operation other than exit_mmap() will free > > > using call_rcu(), as implemented today. The suggestion is to free VMAs > > > directly, without RCU grace period only when done from exit_mmap(). > > > > OK, I have clearly missed that. This makes more sense but it also adds > > some more complexity and assumptions - a harder to maintain code in the > > end. Whoever wants to touch this scheme in future would have to > > re-evaluate all of them. So, I would just avoid that special casing if > > that is feasible. > > Ok, I understand your point. > > > > > Dealing with the flood of call_rcu during exit_mmap is a trivial thing > > to deal with as proposed elsewhere (just batch all of them in a single > > run). This will surely add some more code but at least the locking would > > consistent. > > Yes, batching the vmas into a list and draining it in remove_mt() and > exit_mmap() as you suggested makes sense to me and is quite simple. > Let's do that if nobody has objections. I object. We *know* nobody has a reference to any of the VMAs because you have to have a refcount on the mm before you can get a reference to a VMA. If Michal is saying that somebody could do: mmget(mm); vma = find_vma(mm); lock_vma(vma); mmput(mm); vma->a = b; unlock_vma(mm, vma); then that's something we'd catch in review -- you obviously can't use the mm after you've dropped your reference to it. Having all this extra code to solve two problems badly is a very poor choice. We have two distinct problems, each of which has a simple, efficient solution.