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 11B8CE77187 for ; Wed, 18 Dec 2024 17:58:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 519F46B0083; Wed, 18 Dec 2024 12:58:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CA4F6B0085; Wed, 18 Dec 2024 12:58:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36AF36B0088; Wed, 18 Dec 2024 12:58:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 192236B0083 for ; Wed, 18 Dec 2024 12:58:27 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 84D5A160FF2 for ; Wed, 18 Dec 2024 17:58:26 +0000 (UTC) X-FDA: 82908838404.30.909C505 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by imf24.hostedemail.com (Postfix) with ESMTP id 8CB52180015 for ; Wed, 18 Dec 2024 17:58:20 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ETX+kYKx; spf=pass (imf24.hostedemail.com: domain of surenb@google.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734544669; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nHJNHRO2k8QSCZzU9lDdX3fOBlskNizCNqWgtHFVHAI=; b=2C/NON98XCpcp75qgWzB/bMbItZmEj7LpcP6LUjce30f9Kz9DTKgFVzmQK3W0gpupwS3hc OdwwVA2NOdCS+I1ZLQEahRt5p4hxGLGhhUrpkmGRu45nqd3nCe3k5i3oIFNH85QTz8bKcu xpP/ltWIT0lMUfW2AycO8jwpjeXyDUs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734544669; a=rsa-sha256; cv=none; b=kpXhTq+8Shf6nwh0jtiyLl1Sg0Rd4nYDqAWU/3N4KIP/ZReD+1l9E4JXwDDzlF5hIvd265 f7+D/psfHrFLxH6UtgLN52j2nTwQyOSkozsZUs3GsiqOWooMwFZWnlxRI0O4LKxMMRSPA2 51Fc+fyG7WfwoKEuLThcJcNeThpPpdY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ETX+kYKx; spf=pass (imf24.hostedemail.com: domain of surenb@google.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-467896541e1so4381cf.0 for ; Wed, 18 Dec 2024 09:58:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734544704; x=1735149504; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nHJNHRO2k8QSCZzU9lDdX3fOBlskNizCNqWgtHFVHAI=; b=ETX+kYKxIrdbFDmZy7GjkyTyhue2PdfyaD9T7eWwiyvKzV1kc68uAeNo5S5O3VROU5 mFe8pgWMq4SKg20w+nOzhA4AyNeIJ+/83uKwpgP4cgW+uTqRl+n9LS6tp2CiZ5wZ1aWY vj0+3Gt6eUogvBDfAPy4j7XxfHH9b1p6+G9d5S9+c4kOKrDeDGAY7gd/GgD0eN+KIA2e E3jFhUEfQ6KUNPO7OnxrlRbHnKjD/ISsnSW5pQWug8p3lcj+Zx6BGlMaQTSS1D7h1TIs qOrgEDXfMdvLaPCxQWuhr98Zg8j8rj6FFaZXXL2zav4XDa6DkfmCcl6+u+vZ8zfJr2G4 T6+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734544704; x=1735149504; h=content-transfer-encoding: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=nHJNHRO2k8QSCZzU9lDdX3fOBlskNizCNqWgtHFVHAI=; b=SiTCo4a5bAa/vj22mgstS9AFPqZR1W0zCLQe+Kpap7cyYLeUS5zYfJb7II0Sbt1cMV nPRcRT9NzA7cMiorMEXBspD5O/np7Jas6rkrk3wV3J498uDe4UfdCe52HhZkSRkg1Fd8 IIzRvt1sgHgbccRwHbnXmZ4CoHG03MruJaXUQw+e69Whu/zsUx6RlujxW82kPS5ZEQVU A9bOXJt739D/IsRWhWPQwnLZNqpcX/V4J1d8leMYphUsjtYqt3pgDplyILqCeuFiovFf Sy1lubZIkOkXvQ3YlZQ8Sbzy2ANho2PJ3cAh4BPeZOrkarY56PazKsGyiBbI+P1nNw4l cKRw== X-Forwarded-Encrypted: i=1; AJvYcCWJSybATEOZN5/C+MBgPdq3ieaaPicpNqJAuvYiYfn+44TgFyj9wtWwAmx/V+Xdut8x6a4te9RKFw==@kvack.org X-Gm-Message-State: AOJu0YxlGWPyXok7XMxNmurbWITIbzjsnbeJSp2DCQLSKUY6SIL916xI bjuJn95zSGBq9UowfSdcS1sj2WzMbI8LLHGND1o/FVIk8aNfFZKFAOK9du5GiaMGnIl/vECyhqu b0GkjTVDivCMXFgAfkSE3HLazlGu+oUDkpcOD X-Gm-Gg: ASbGncvRk/Y381cUaRakIizoIDucRlM/1vv8Xpg+Bm0HTd/agjlo8VJBeZ+PRCvjZM3 4OiC4E+n9e6tLw1v4QBKsIFcdkPQd87AAwqMEWXxPpV/TlLi7YjiGBWsKQu+KjGYLefO6 X-Google-Smtp-Source: AGHT+IEssDY0SR+bYX95z/ZDlSkj8wQHIJjGSznYGJGFMR+0yJkHJYirEpSKl22PGI8XS5CJz8s3aT1ZECCIERJ75Go= X-Received: by 2002:a05:622a:13c7:b0:465:c590:ed18 with SMTP id d75a77b69052e-46935520985mr3369851cf.9.1734544703526; Wed, 18 Dec 2024 09:58:23 -0800 (PST) MIME-Version: 1.0 References: <20241216213753.GD9803@noisy.programming.kicks-ass.net> <20241217103035.GD11133@noisy.programming.kicks-ass.net> <20241218094104.GC2354@noisy.programming.kicks-ass.net> <20241218100601.GI12500@noisy.programming.kicks-ass.net> <20241218161850.GG2354@noisy.programming.kicks-ass.net> <20241218174428.GQ2354@noisy.programming.kicks-ass.net> In-Reply-To: <20241218174428.GQ2354@noisy.programming.kicks-ass.net> From: Suren Baghdasaryan Date: Wed, 18 Dec 2024 09:58:12 -0800 Message-ID: Subject: Re: [PATCH v6 10/16] mm: replace vm_lock and detached flag with a reference count To: Peter Zijlstra Cc: "Liam R. Howlett" , akpm@linux-foundation.org, willy@infradead.org, lorenzo.stoakes@oracle.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mjguzik@gmail.com, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, paulmck@kernel.org, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 8C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CVNN5 kAEB5eAK J1LhusHAFkEVJVd6ZccAMPhhkyGpnN4lS6sCfZCg+8GH59ObpNSXL7ZQOWe+kmysRNxD4ghfQj9RS9y8K5BQcbOozMVE753EmzaEP+QZrNO/IYpGI3n/v+ArASU1TYG64Q0ZPHVDZso8Yz8uvrUf4QN5XbwyW5w2t/bpIA97f7et3btK3J9dLIRSDAZCFuTE8kKD1hjbwXr1qEqoNaTYzAsI3hbKsf3mnqMVLZjWsI7VNH7ntIgTbo9CnCOOOttAZ4aTMxZIDHF6LOav9OCqZwpr8WyUv2su6jbW3mts6q5/xpfQ= X-Bogosity: Ham, tests=bogofilter, spamicity=0.002796, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Dec 18, 2024 at 9:44=E2=80=AFAM Peter Zijlstra wrote: > > On Wed, Dec 18, 2024 at 09:36:42AM -0800, Suren Baghdasaryan wrote: > > > > You will not. vms_complete_munmap_vmas() will call remove_vma() to > > > remove PTEs IIRC, and if you do start_write() and detach() before > > > dropping mmap_lock_write, you should be good. > > > > Ok, I think we will have to move mmap_write_downgrade() inside > > vms_complete_munmap_vmas() to be called after remove_vma(). > > vms_clear_ptes() is using vmas, so we can't move remove_vma() before > > mmap_write_downgrade(). > > Why ?! > > vms_clear_ptes() and remove_vma() are fine where they are -- there is no > concurrency left at this point. > > Note that by doing vma_start_write() inside vms_complete_munmap_vmas(), > which is *after* the vmas have been unhooked from the mm, you wait for > any concurrent user to go away. > > And since they're unhooked, there can't be any new users. > > So you're the one and only user left, and code is fine the way it is. Ok, let me make sure I understand this part of your proposal. From your earlier email: @@ -1173,6 +1173,11 @@ static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms, struct vm_area_struct *vma; struct mm_struct *mm; + mas_for_each(mas_detach, vma, ULONG_MAX) { + vma_start_write(next); + vma_mark_detached(next, true); + } + mm =3D current->mm; mm->map_count -=3D vms->vma_count; mm->locked_vm -=3D vms->locked_vm; This would mean: vms_complete_munmap_vmas vma_start_write vma_mark_detached mmap_write_downgrade vms_clear_ptes remove_vma And remove_vma will be just freeing the vmas. Is that correct? I'm a bit confused because the original thinking was that vma_mark_detached() would drop the last refcnt and if it's 0 we would free the vma right there. If that's still what we want to do then I think the above sequence should look like this: vms_complete_munmap_vmas vms_clear_ptes remove_vma vma_start_write vma_mark_detached mmap_write_downgrade because vma_start_write+vma_mark_detached should be done under mmap_write_= lock. Please let me know which way you want to move forward. > >