Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp37582lqt; Wed, 5 Jun 2024 16:24:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXN+vxx3rgtSnfS7mz68EmM1Wgl7WkYhhxk1MWM7pnGoS18AsRWxg49x/YFokxgohkUq3VjY6OCihrfAhy1aOW622J7YJmAog6anmGdOg== X-Google-Smtp-Source: AGHT+IEtIZMglahPHQbSvCtlicdUUdutq8Ft9nF4yUqzk4KPJRPrpj8GMYt+7c0rjM1XQpwjWfY7 X-Received: by 2002:a17:906:4ad2:b0:a68:8987:891d with SMTP id a640c23a62f3a-a6c7650adc9mr77953366b.30.1717629879887; Wed, 05 Jun 2024 16:24:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717629879; cv=pass; d=google.com; s=arc-20160816; b=OqV/9p28RMumgzuqYquzK3YZ3fo3SiMfC7EFZo8m+ey38RSjwsavNUPrmvRh7x39T/ sblyfOa8inPlJ1uzvdnS0vL/M3qI0X1TNRUbw2LkSgQ5oZtEp88+gvQ7NwhvqUw80kxD 5enOal396yEbCEf4kXLbE8EVcY+9HBOxd0rWUY7QAB+qd1C2G1/1UnC6TIKBmx3eGqAj +dYe/drXtDYDQXw3YehNp3Xww1p7mlaZ9c+t2kp08TfRKAUONUkcecl6MrYadSUqhvRA oi8+pqNYUqsAlnjoewEvYDb429tIvD9kFFXeeL0cOptGvmZSvoutIqgXRV1xm/pFHrmR RUHQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=Cw47IidnP2/eaju7UvZBzqOetg82MP8FWmgrm1mdRb4=; fh=/tXxe9qmLtDon/AKdfzDF5vSN6UfuOq0je80leEJLfc=; b=ip4c6rTtgV2bEqY0NIplRYLKTRPb7MGS0Mdnebw6R08I3w2YFSLHVg3gsbtNF1adCJ 8VdIWoxw+1+XdYvLBnNed/xbles5LJSqJYPFxVepN2FjwQbJq13GhKIph2p3Rbw7NU5N z5BLSQHilDk7scOUcej3vtMznVaiXAzvNXzbc/dZQpaDxZKVUdrfIxDdQsTsEFLFQYx8 Nt7MqJXfvVvKYTAWTd2AgN5ScEXCbAfYoiJqE+q4Q7aQ/KW0bJMQ6bXU9J6VTAeIcGbq lnv088GZ2AsZuxWrCjTI2mlsqoS9Nn6g/PGKLwVZloJbiR65UrNXblI7ZnUgtrC2y6+j AC9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=H1c7TElA; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-203405-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-203405-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a6c806dac81si3816066b.391.2024.06.05.16.24.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 16:24:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-203405-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=H1c7TElA; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-203405-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-203405-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 662811F2573E for ; Wed, 5 Jun 2024 23:24:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F1AD168C09; Wed, 5 Jun 2024 23:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="H1c7TElA" Received: from mail-yb1-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5BCD15FD08 for ; Wed, 5 Jun 2024 23:22:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717629761; cv=none; b=iSYAS11opS9mrw9wZ0LQrpXKUr9GuGW1mO2XwYOXwpmuMxwOFp9Iys8v/eJAx4dpE+ZJyitzu64/hpmXi1kQ89H8bZFcPx0BNuLbSWCbk+kjEvZ1pMZ8gM+KPZCuFLYlQHud/vpa3ou2srw2TYm+SNakjxmwG5yLdUnpDFN8ggU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717629761; c=relaxed/simple; bh=VjQzXI18+qQG7Z7bgYqIEkcpDbiT6kmJveAjRT/fKpo=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Content-Type; b=M3G8dReTxk6OnpUOE28ss0WdHitG4XAu4jdgZzEWYez+5xdHl3GsMzr0Vy4uUPdlWhM4oxH41h5bs84AJZbZIzcy+UGrLDQgVeRJqUOHaw+AXrUudVIBjqs8XL78N3boSiWyRv4/WbB4N0zYpQLPlUvVI+eciJK0Ze5d2EbFYww= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=H1c7TElA; arc=none smtp.client-ip=209.85.219.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-yb1-f181.google.com with SMTP id 3f1490d57ef6-df481bf6680so414881276.3 for ; Wed, 05 Jun 2024 16:22:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717629759; x=1718234559; darn=vger.kernel.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=Cw47IidnP2/eaju7UvZBzqOetg82MP8FWmgrm1mdRb4=; b=H1c7TElAkkGFLa3whvtNbDeJeqiBUThK8nY5g0i8VtX0dWJGjjcCuQGDBA7xtaK9iq Nse+ktQtaBMw9Y+5F47l1v3+Hvjp+PS9DS81QT8/kZ9DwHZo+RTwu7/dI2fB8UnapyLl qT+JwDSyc1k7DTRwN/Yd4BqRu0keo2cj4Mx2OEiE6leeXrv9s560m8eQIo3B691qgJeP ezoLKGXBcqMl6NfW/IWJ8mvMux0xvszx2BYG1g0DmV1tnij321Ua/b+tDbzkRE17eIo/ YwBlWyh+pSjF6NAJvjjvPtTYsz8s6B0xKW6/JTND7iSnZ7i9fuIwKmVrhT/fSXHHipsx uR1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717629759; x=1718234559; 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=Cw47IidnP2/eaju7UvZBzqOetg82MP8FWmgrm1mdRb4=; b=HoGJtso2xlUUSfdo3/rC+osTRN37UETkUiByozBxsE0Efir2RvIZu6UpWYfdCMrpf1 Vc/M9I++p3OREnlK4vwwdQh30uVnYqVvPaTb5jmrjUiaoydr3jCOslAfJW7SpN5duYUY nlL0wRyBKZ2nThubLYa8Yh/FBdulkQ7XxxAUPJ29dnOU+DLT2ZfXyTHLkR4eBAvH64io hxETbskIbmcTIETwGcLwsY4xMubDPpIpeDgfBt6viI9QM/O4qc3ULX/G6gavy+0wwrcF 3FKjVLtULulC6Av/96A7daUHa1FJpZJUh/gnuMJtry4Jc/287ie/yV0u3vOO12/2WhFG pF0A== X-Forwarded-Encrypted: i=1; AJvYcCUAyk7DC7ftelRzpi4dXYVTtVA5Ipwc3JjT+KlcDHjO95qwycQS6c76FFzIiPPbrJuqEz3ZOKpt+Wg7Fp62n2/+IBvvwAoz5ja8xHzJ X-Gm-Message-State: AOJu0YwcvfwIXyLnLjqa7tJ4aM5alu891xnWCZvJZJ/okd6PBX+ZwXNO SiavhcvJ4NBCQNZf0Rv3MuMfuhiSEeZgW0+W7h2Zr+03+pQmK1o7HdIOOgVLpl+ev28j4dQv6KL OFqCOZ5pe4xhswQg++Dz/fn8f43zhl6FAdZVk X-Received: by 2002:a05:6902:1793:b0:df7:923f:f2a4 with SMTP id 3f1490d57ef6-dfacab2c208mr4276890276.7.1717629758331; Wed, 05 Jun 2024 16:22:38 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240605002459.4091285-1-andrii@kernel.org> <20240605002459.4091285-2-andrii@kernel.org> <5fmylram4hhrrdl7vf6odyvuxcrvhipsx2ij5z4dsfciuzf4on@qwk7qzze6gbt> In-Reply-To: <5fmylram4hhrrdl7vf6odyvuxcrvhipsx2ij5z4dsfciuzf4on@qwk7qzze6gbt> From: Suren Baghdasaryan Date: Wed, 5 Jun 2024 16:22:27 -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" , Andrii Nakryiko , 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 On Wed, Jun 5, 2024 at 10:03=E2=80=AFAM Liam R. Howlett wrote: > > * Andrii Nakryiko [240605 12:27]: > > 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 giv= en address, or the > > > > > > > + * next VMA. Search is done under RCU protection, without ta= king or assuming > > > > > > > + * mmap_lock. Returned VMA is guaranteed to be stable and no= t isolated. > > > > > > > > > > > > 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 returned. > > > > > > > + */ > > > > > > > +struct vm_area_struct *find_and_lock_vma_rcu(struct mm_struc= t *mm, > > > > > > > + unsigned long addres= s) > > > > > > > +{ > > > > > > > + 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 loc= k the 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 updated 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 pr= obably > > > > > 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 shoul= d > > > 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? > > > > VMA_LOCK_MISS is used here because we missed the VMA due to a write > happening to move the vma (rather rare). The VMA_LOCK missed the vma. > > VMA_LOCK_RETRY is used to indicate we need to retry under the mmap lock. > A retry is needed after the VMA_LOCK did not work under rcu locking. Originally lock_vma_under_rcu() was used only inside page fault path, so these counters helped us quantify how effective VMA locking is when handling page faults. With more users of that function these counters will be affected by other paths as well. I'm not sure but I think it makes sense to use them only inside page fault path, IOW we should probably move count_vm_vma_lock_event() calls outside of lock_vma_under_rcu() and add them only when handling page faults. > > Thanks, > Liam