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 72D18C25B76 for ; Wed, 5 Jun 2024 16:24:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 08A0D6B0099; Wed, 5 Jun 2024 12:24:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 038EB6B009A; Wed, 5 Jun 2024 12:24:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E43156B009B; Wed, 5 Jun 2024 12:24:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id C63DF6B0099 for ; Wed, 5 Jun 2024 12:24:22 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7E80A40D5B for ; Wed, 5 Jun 2024 16:24:22 +0000 (UTC) X-FDA: 82197357564.19.5C21844 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) by imf17.hostedemail.com (Postfix) with ESMTP id A280F4000A for ; Wed, 5 Jun 2024 16:24:20 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=COSs6LJd; spf=pass (imf17.hostedemail.com: domain of andrii.nakryiko@gmail.com designates 209.85.216.49 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=1717604660; 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=flQITc7xQJ7hzuC/l9q3Vyu4uqvXTdu5y2cqIKMXoP8=; b=MRzVanPANCWOWFSmcFphSS83D/LtbCopR6LcWkQui4H3vgpmkNeZt/XiC2MUdCmcdMa7sQ id1vOSQLdX6Ihar/c3GsCPL7tmNQa/hfX7ch/J7gua/1IX0qkr5EN31QY6Und7rAYUskkl TrNO1G8hcf7QwyoOgjrjP1dv5PvSwYw= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=COSs6LJd; spf=pass (imf17.hostedemail.com: domain of andrii.nakryiko@gmail.com designates 209.85.216.49 as permitted sender) smtp.mailfrom=andrii.nakryiko@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717604660; a=rsa-sha256; cv=none; b=ca6eUa6A5jPyYwjnzwvRmLUrRmUEapq3ZSwePuj4D+jgL/udYIkYZL91OQzjJrZ9uYZ23z y5ztjfslJEgsW88QCTJg6bfZ+KK4FeJrCh76pbUOwMMBg5x7Fn5jlWqwerYx2EvmJqN4y7 7PBmmHiZRQbDIAnuffncszT7n1SQBUw= Received: by mail-pj1-f49.google.com with SMTP id 98e67ed59e1d1-2c24115469bso851422a91.0 for ; Wed, 05 Jun 2024 09:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717604659; x=1718209459; 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=flQITc7xQJ7hzuC/l9q3Vyu4uqvXTdu5y2cqIKMXoP8=; b=COSs6LJdO5Nk0305k6N0uBK+h0sraSdtr3Nh7oPCx6eG+MzBiUS/WXSAvfnpA8ezG1 +GjFvb/cJk/PgGqnlmp3hYgRuDjpZHUXIdZ+3JofWUqiuZI0qXJx6uVAqZ7W2lh+GPKo rATpmlWVas/t/Zkp9+1YkOlKyGbjz56C4tJDz0xKmfuA79rOQ2j/lU+vSgrHFeUh7jR5 HtK5m/qnF6Qv7YC66PbLskxxGgrcl3LqjQRoli2htDuthGMeQ48NwdC6VKd2EF114hYI MKxs9t72WcP5UggqcozK5V4p3VyE6rYGbu5D24bLbgcKDkznEvOx5BLhE3d2sihhtJfb u96w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717604659; x=1718209459; 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=flQITc7xQJ7hzuC/l9q3Vyu4uqvXTdu5y2cqIKMXoP8=; b=QK4n4h4mnI7AUHaVgSm7DsPV8TKwkm1genbJ7t79l+7OSHyLCVXc3f+NMFe2kyz8NO jGJuf8s/g/C1n9Sfu053abUl1zrNu02vE9241fCE9c30kXyFko3KBUwz8dQbEo0HhVUI VK8Em8a/as/hkYRYJm3dXbGe0AM4jcoOrS4TCkFUOli0hbn2COZyrTVKbrodF1sltEKo PZyo1EZ+vLyaTzz8nxuiUSl70f0C5qKd/woH+a2lyX4MczNXkROrCkWhP2MOLljdKOvN Fz8owKJXomtgx+jwq5s6Dcf9KTKEYUlFIrsgwA1kHKqrFZp8Gwvv+aFagzPuHRdBfVHf negA== X-Forwarded-Encrypted: i=1; AJvYcCWzux4T27aIWXSpphXBhrIVcHMTU7MUekf90VD9/fap26qCiaWFDWCzG47UFfuOIiz3fdtOKQRRmx3VrWE6hz6kVhY= X-Gm-Message-State: AOJu0YwWsApMfPLDDmDfZW2JU6d0kdXvhSAYzrtpZHooR9D8di6gKDMl Q8AA7OrDmhjLaPSF5KzZ3eprg91+m38Y4/yAN0xnP1ogHnTONMWmold8dU9dehbUYbCqMF03Kze VqNpe+7w1qasJ6SIlZfmRqLbpvI8= X-Google-Smtp-Source: AGHT+IFl4BDR1W/qUUzA2PO/5TqjnOMuZDrbSdWXqHm0/vvIQclz1kYhZyeF3J6773NUVsVX4sJpCnsoN9a3gAPR1gc= X-Received: by 2002:a17:90a:6b43:b0:2c1:dc60:d933 with SMTP id 98e67ed59e1d1-2c299a23347mr185983a91.19.1717604659345; Wed, 05 Jun 2024 09:24:19 -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:24:07 -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-Stat-Signature: 67wh8dp6kgkmqnyw9a3py8h55obut11g X-Rspamd-Queue-Id: A280F4000A X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1717604660-326466 X-HE-Meta: U2FsdGVkX1/UZmIy/c2IJM7z1zVJ7Mw56snk3wDZeuZYvK+IHx3kkQhtyWNeo5LCzr+E5iLzWScChX/iS15GaWwyPLZ+AFEkNCl0Iex02bZnsj/5inii5AC3xGc+sWgsAoE4XcKjCM9aAto2YzRReJ2Zeh8HLmo9duxV8h2sVp7H/xZJMURXgw9JJBeKTdu0bUhxUUOIFluEglG5GLcucJwXyCSLvgK9xA+F1E4uC9qZmt9hO3F3RrdTPaG7FkmiRRovDqvqbS+Jj6pZbSGkKL6194HsLr9vGMUiF1jt/ZGNEiZ3hYnHYeat3GR8pFef2S7oqsYJ+wi56/XjHb3CSABEKP9Ij3ROzJpFFpK7K6sLBSLrIuZJeKYrlBgHY27QI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+TrciijaQsI5P6pN8IjtlC4LZUk/sWAmyxosqMq2M87jAE9TM4A/Uo5p71hee+en5+/7+1ZOV 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: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 add= ress, or the > > > > + * next VMA. Search is done under RCU protection, without taking o= r assuming > > > > + * mmap_lock. Returned VMA is guaranteed to be stable and not isol= ated. > > > > > > 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 ret= urned. > > > > + */ > > > > +struct vm_area_struct *find_and_lock_vma_rcu(struct mm_struct *mm, > > > > + 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 the = VMA. > > > > + * Note, unlike lock_vma_under_rcu() we are searching for VMA c= overing > > > > + * address or the next one, so we only make sure VMA wasn't upd= ated 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 probably > > 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? > > > > > > > > > > > > + goto retry; > > > > + } > > >