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 0DF4DC25B50 for ; Mon, 23 Jan 2023 18:47:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F8D36B0072; Mon, 23 Jan 2023 13:47:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 784B76B0073; Mon, 23 Jan 2023 13:47:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FBE06B007B; Mon, 23 Jan 2023 13:47:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4AEA86B0072 for ; Mon, 23 Jan 2023 13:47:17 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 107CFAC8D3 for ; Mon, 23 Jan 2023 18:47:17 +0000 (UTC) X-FDA: 80386946514.28.698E162 Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) by imf15.hostedemail.com (Postfix) with ESMTP id 425F7A0025 for ; Mon, 23 Jan 2023 18:47:15 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XUYoLqkW; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of surenb@google.com designates 209.85.128.176 as permitted sender) smtp.mailfrom=surenb@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1674499635; 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=EEZWpRCqDnFRlmuW2ysCODSPytTJZRpx7aAYLqFc7io=; b=LqGpO9lMPMwcJUUF2dCNMgt5VB97/qnxzLXH7Q8fKySW1JArOTKhmzeisu9fBrUSJljhJg hqloAholaDsgrSZFimVQR0Ua5eMWy+zxO9S3yQQqnhiLb/BBZCCpQ2bGWzhP+KztzYTe6W AfAzKNh9jK6JkHUS3mjyAe3tzxlpjTk= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XUYoLqkW; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of surenb@google.com designates 209.85.128.176 as permitted sender) smtp.mailfrom=surenb@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1674499635; a=rsa-sha256; cv=none; b=kuAjrrOiiH0LRLANbwLjT5Rmp6qOJbY03lTZczwWZiXlH01WSGeyBR/KP8WYKufNz2wc9C OcyDJXMm2CLBaGcdujkmhnYSz33vvw4diXq5iapa7K0QQMLtkm4m8OskJlc3M+89v7Qs65 +1JBqwRTa5ndzmeqHCpPRq4UmUHyRXc= Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-4c131bede4bso185189777b3.5 for ; Mon, 23 Jan 2023 10:47:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=EEZWpRCqDnFRlmuW2ysCODSPytTJZRpx7aAYLqFc7io=; b=XUYoLqkWsCaBJ04HPvri04PJii+UnFNZfkttGO5DhZ8O2cDkfdoED9gTGKUpCW9h1a JBcTELRdCohSIRirMa+mImJm53I336wxROakhil5KRb9AkilXbhbOf8RDwpNO5TUNRN6 5v1+iN4dIzrHw8ZxFeWt3klOFn3U+XFIWQpG90bfiWFj3EU8yyCx+D4tyHkyiB5MXlEH hHlT66itrO9FizYgORvZRn5EsQbfXCI4h1yRuSQMhdrtoQQExigoat0ejKguLIelNJmb HKo6I8eKidhP2zQ97/alMJnO+/yx1DXIiS5235nyc/LfXHwRMP9rsCWu2SLVdJz56lXN bCFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EEZWpRCqDnFRlmuW2ysCODSPytTJZRpx7aAYLqFc7io=; b=WOqyRzWsprK0kxFYtGlJr4nGY8WL7z185rlfFHtsdM4y0OwdMW6n2cpgJAtwE8mKw5 6hnq74KeQ44nlmF+Qj4GKncC/cWQ/S2pDKUai4ivuEgLnxBJigbUhsO9//QRKBvfwE5K +Fe9qc8r8oxho7Bo7nC5anXgkruX7htVgg728KXCb/750HJ1zE+rvx1dA6Y2eyWiFCJq J3LLU3f3wA7Q8B8olij8Nr1zNaYndoyAB07A3ki+oeQYbHFoB9B6T95u1A/fJkVwQiIP rllFCfisDyv1NUtutwhjK6jLHglf2h+n1YjqiClO/TnJVVrUrBcDqtucUuWi0KT2mvuQ X7/A== X-Gm-Message-State: AFqh2ko/GyWVHHjXO58c32G6ZiJuIIvx4iQBiZO4u0SUmnUou04R4g37 xz0jwDKVykzZB/eZAwAI9237DRGZqgTmV7DjbE76/w== X-Google-Smtp-Source: AMrXdXulFktynQi9i95G9lw3C1GDLWNlJVNyrZN3qPgr/W6spDmnmjN0faCW1cv674kzUn8t1U2hk+KJr7613dnjJc4= X-Received: by 2002:a81:708c:0:b0:479:b6a1:d9a4 with SMTP id l134-20020a81708c000000b00479b6a1d9a4mr2744223ywc.263.1674499634132; Mon, 23 Jan 2023 10:47:14 -0800 (PST) MIME-Version: 1.0 References: <20230120170815.yuylbs27r6xcjpq5@revolver> In-Reply-To: From: Suren Baghdasaryan Date: Mon, 23 Jan 2023 10:47:02 -0800 Message-ID: Subject: Re: [PATCH 39/41] kernel/fork: throttle call_rcu() calls in vm_area_free To: Matthew Wilcox 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 Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 425F7A0025 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: jj9dn4aqd19538pioyhfuwtz3dyd1qef X-HE-Tag: 1674499635-376138 X-HE-Meta: U2FsdGVkX1+5e50BfWp3G9o6mblwoBpyVH76YeDm0dtzhhasb1fqw4bFvk2mByLoXQqRXE+ZHP5aX2oxgpt3QoSFVvxjxuFjSLBd4s992cuZYmSBCOXlhMmkHdg1vdgU8pJ7aYrOWbCuWlmHzpkEE3YTi/dTaPnVujwUmyo3kTXYTIdrzGH6+BdQlabFePvSpKoAa/za4cMjWFCx1iV1nONO+BdpOaOoRRLxFoNWO7/xxY0AWnUM+hLI1Mg8ex5PXz1hAd5YhEiZFsycI3HBfkVE26FyP8ITusIw18iL+rKUk0n5REerO9AMYjYvhyYiYm3RJ3RRq0569Kd1zxQClUNQIvcx0AsSvCI1rwHksl1OGRO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-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Jan 23, 2023 at 10:23 AM Matthew Wilcox wrote: > > 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); More precisely, it's: mmget(mm); vma = lock_vma_under_rcu(mm, addr); -> calls vma_read_trylock(vma) mmput(mm); vma->a = b; vma_read_unlock(vma); To be fair, vma_read_unlock() does not take mm as a parameter, so one could have an impression that mm doesn't need to be pinned at the time of its call. > > 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. >