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 27EF2C25B76 for ; Wed, 5 Jun 2024 16:27:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B12946B009B; Wed, 5 Jun 2024 12:27:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC2836B009C; Wed, 5 Jun 2024 12:27:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 989CA6B009D; Wed, 5 Jun 2024 12:27:30 -0400 (EDT) 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 7BC9E6B009B for ; Wed, 5 Jun 2024 12:27:30 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D823A81381 for ; Wed, 5 Jun 2024 16:27:29 +0000 (UTC) X-FDA: 82197365418.28.7D19252 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf09.hostedemail.com (Postfix) with ESMTP id 1D92A140019 for ; Wed, 5 Jun 2024 16:27:27 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=B2X1LVYy; spf=pass (imf09.hostedemail.com: domain of andrii.nakryiko@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=andrii.nakryiko@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1717604848; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to: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=dlqYX7CZRXcik8grmUTE6vS1byeP3eeXqYE/oo9qiFo=; b=1GEYshEcu0V8C2mkzpEyACdb6jwuJODjPCSH99LtwgHUaJ6BhRW2vxg9/dICR/7oBJlk5w /BhLA+gCXRK6POmvMlIO/PZSTbbk/NURT9nq9K+qBSkzaJ3VYyXXDeaSOjRCTL0L1/LbJf VtU5bvIsOKX1be/MCwcvLDZ23mwPf4o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717604848; a=rsa-sha256; cv=none; b=eMXlrh2s3HDfkqvJ2+8oT6vOUhOVZLeU5hUD8QraZi8+c42d4UUoqKBtWIF+f3WxSj2KKq 9BzaOHp/Hbp7i8OzfG6zVLHGXfR0RVZz+04k7Zvvx9vv+1jzJ1jx5Hw38iKLKyrAQ5dhvo PGwfpItGffHwZFEQH8MxERbgba99vu8= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=B2X1LVYy; spf=pass (imf09.hostedemail.com: domain of andrii.nakryiko@gmail.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=andrii.nakryiko@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1f480624d0dso405935ad.1 for ; Wed, 05 Jun 2024 09:27:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717604847; x=1718209647; darn=kvack.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=dlqYX7CZRXcik8grmUTE6vS1byeP3eeXqYE/oo9qiFo=; b=B2X1LVYynoaN76cWA7lV7IB5m7AwWYo9aWDK8k6K3ALBQzLw/U2TYg+yaYfIuBHYIw UjhisnvjQI2/tJYva5tH5QUZtcEgtP+2k56qLmVrBFtZu4n0Rbdyti2ebMRqB1MKRX1B G3YNPLJeY51kzEgc78XOjeEbQAPUP88unKG1EhwVmxE/mWzNHzpf906ASbzlMgnp9mRq FVvzjCxkP+7I9lXhR/vn2i1i8CymHQtKcd3aUPkB+E+CRfkf4B06+Er6X9VliKRocDgo bVOdq8j+ZUrHEgY+40oy6fJVWjdNzJipbCWET55dV580NssZnOpD/vPi97tP2VLa0v1M ReeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717604847; x=1718209647; h=content-transfer-encoding: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=dlqYX7CZRXcik8grmUTE6vS1byeP3eeXqYE/oo9qiFo=; b=LqB28uxV92AbPaK4NDM9Fu+0EKe4klarERuTvKf3buRCCl271NvZYzhBtKzZS1cJh+ jdyAQCtRSLxoXhh+LdD80NnxaPPl78z15A2eJiTm5xgmOLmZm6wFTEvCp56u+Vn2oq2t 9wcHcOUG78jsbQFnbEpcS9R7T/6ZSrK02pbBkShiocZ/QZXmESO+5I3d0mBgVFVPb9rN V+zLDhRilomGy9GwjuqO5j/6EjBbcQG94/pD//4mei2yVkpxELU0PAp+wRO4JN1ddiyI iTJw0C4/agxZjXmrwww/I4SWYMQApEmVxpiWzkzTPu4rIR5N+odKMnYRPCmt6/KLFun5 Drmg== X-Forwarded-Encrypted: i=1; AJvYcCVYCBLULWXmYP21H1Xlket6Jk3hFrvDW3KON6o4hT6b82dHjm4MbuyTpMAxxUqhm/uimscE/Ky6bqI3+1En1ATKTBo= X-Gm-Message-State: AOJu0YxKxibPw+vIGIT1ww9/240FH8y8fHMLk5qRUKU/SDvwCur8GV48 AK7Dtk8JytYR5JLM43jlA3hVICgklbVs/4jrw06BLgUg8v+jJ6snMvbRKzIHEbeEm4RjlpXOIrZ IYYfbCrt4+SsfJQ0FSS0okn3Lbfs= X-Google-Smtp-Source: AGHT+IFm0S5xOg68BNRVacZTRtX3OMveoYFGLEWlIQvH25K+sH/ZnawuSmmeabnbH099N3GydWsGDSWHpu0ibWhLLZA= X-Received: by 2002:a17:90a:f6c8:b0:2c1:a9a2:256e with SMTP id 98e67ed59e1d1-2c27db4faddmr3612067a91.31.1717604846834; Wed, 05 Jun 2024 09:27:26 -0700 (PDT) MIME-Version: 1.0 References: <20240605002459.4091285-1-andrii@kernel.org> <20240605002459.4091285-2-andrii@kernel.org> In-Reply-To: From: Andrii Nakryiko Date: Wed, 5 Jun 2024 09:27:14 -0700 Message-ID: Subject: Re: [PATCH v3 1/9] mm: add find_vma()-like API but RCU protected and taking VMA lock To: "Liam R. Howlett" , Matthew Wilcox , Andrii Nakryiko , linux-fsdevel@vger.kernel.org, brauner@kernel.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, gregkh@linuxfoundation.org, linux-mm@kvack.org, surenb@google.com, rppt@kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1D92A140019 X-Rspam-User: X-Stat-Signature: jzki4eid3ekwu688r145qm38e1e15me1 X-HE-Tag: 1717604847-615982 X-HE-Meta: U2FsdGVkX1+thNPaanDl3wc2ZGkVxOj52ehc9QSnby6nGxQDE/W3Z67I3nyZG7i9T2sGQyqy5N7QppMlXZQwTgev8wFyYh3RnSkVYe9f3/37c+F2ZOigfDGrTOhFkVmR4y8aX4ymEW3Z6MTzdHrI2kJ/nF8mxlrbKNPzGWrmhUPuAgHJAiS7riDP7zvFnCr/i51FzSo7DX7/lw2FZsbAErWgLR+GoPJOSEUEjC/6/Hscuz+9W0Ji0O3yqkfO4HTNDiL4R41SBW+dhVC8SFBbMA9woFY9qdtSd8tT6W44EaCw96QS9gnSIpBxLiq+4Xi591Iv3q6jf6rxxqN6/aAR2951uMKH9C5bQD/tl1ArEG35IS9Zn3Oqy1vzUS3Ei131PvRbRNJDno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UuWr495xluKz3k4a9+emvVwPUv7xo0jfYYR+rvLSPzSG6NTBA6MI6anhkS30zlimRuJ39aLe+q0fZxG 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: List-Subscribe: List-Unsubscribe: On Wed, Jun 5, 2024 at 9:24=E2=80=AFAM Andrii Nakryiko wrote: > > On Wed, Jun 5, 2024 at 9:13=E2=80=AFAM Andrii Nakryiko > wrote: > > > > On Wed, Jun 5, 2024 at 6:33=E2=80=AFAM Liam R. Howlett wrote: > > > > > > * Matthew Wilcox [240604 20:57]: > > > > On Tue, Jun 04, 2024 at 05:24:46PM -0700, Andrii Nakryiko wrote: > > > > > +/* > > > > > + * find_and_lock_vma_rcu() - Find and lock the VMA for a given a= ddress, or the > > > > > + * next VMA. Search is done under RCU protection, without taking= or assuming > > > > > + * mmap_lock. Returned VMA is guaranteed to be stable and not is= olated. > > > > > > > > You know this is supposed to be the _short_ description, right? > > > > Three lines is way too long. The full description goes between the > > > > arguments and the Return: line. > > > > Sure, I'll adjust. > > > > > > > > > > > + * @mm: The mm_struct to check > > > > > + * @addr: The address > > > > > + * > > > > > + * Returns: The VMA associated with addr, or the next VMA. > > > > > + * May return %NULL in the case of no VMA at addr or above. > > > > > + * If the VMA is being modified and can't be locked, -EBUSY is r= eturned. > > > > > + */ > > > > > +struct vm_area_struct *find_and_lock_vma_rcu(struct mm_struct *m= m, > > > > > + unsigned long address) > > > > > +{ > > > > > + MA_STATE(mas, &mm->mm_mt, address, address); > > > > > + struct vm_area_struct *vma; > > > > > + int err; > > > > > + > > > > > + rcu_read_lock(); > > > > > +retry: > > > > > + vma =3D mas_find(&mas, ULONG_MAX); > > > > > + if (!vma) { > > > > > + err =3D 0; /* no VMA, return NULL */ > > > > > + goto inval; > > > > > + } > > > > > + > > > > > + if (!vma_start_read(vma)) { > > > > > + err =3D -EBUSY; > > > > > + goto inval; > > > > > + } > > > > > + > > > > > + /* > > > > > + * Check since vm_start/vm_end might change before we lock th= e VMA. > > > > > + * Note, unlike lock_vma_under_rcu() we are searching for VMA= covering > > > > > + * address or the next one, so we only make sure VMA wasn't u= pdated to > > > > > + * end before the address. > > > > > + */ > > > > > + if (unlikely(vma->vm_end <=3D address)) { > > > > > + err =3D -EBUSY; > > > > > + goto inval_end_read; > > > > > + } > > > > > + > > > > > + /* Check if the VMA got isolated after we found it */ > > > > > + if (vma->detached) { > > > > > + vma_end_read(vma); > > > > > + count_vm_vma_lock_event(VMA_LOCK_MISS); > > > > > + /* The area was replaced with another one */ > > > > > > > > Surely you need to mas_reset() before you goto retry? > > > > > > Probably more than that. We've found and may have adjusted the > > > index/last; we should reconfigure the maple state. You should probab= ly > > > use mas_set(), which will reset the maple state and set the index and > > > long to address. > > > > Yep, makes sense, thanks. As for the `unlikely(vma->vm_end <=3D > > address)` case, I presume we want to do the same, right? Basically, on > > each retry start from the `address` unconditionally, no matter what's > > the reason for retry. > > ah, never mind, we don't retry in that situation, I'll just put > `mas_set(&mas, address);` right before `goto retry;`. Unless we should > actually retry in the case when VMA got moved before the requested > address, not sure, let me know what you think. Presumably retrying > will allow us to get the correct VMA without the need to fall back to > mmap_lock? sorry, one more question as I look some more around this (unfamiliar to me) piece of code. I see that lock_vma_under_rcu counts VMA_LOCK_MISS on retry, but I see that there is actually a VMA_LOCK_RETRY stat as well. Any reason it's a MISS instead of RETRY? Should I use MISS as well, or actually count a RETRY? > > > > > > > > > > > > > > > > > > + goto retry; > > > > > + } > > > >