Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp542250lqt; Thu, 6 Jun 2024 10:37:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVcwPB2gMhcpDL3eX8Puc5hdFV1H2qi4fZmb5VBv70OcTIK6UzfjIKu5uzIiWrSZshWdco5Q+fDUZ/WPQ1uzek2A0JSE5qNb47WIv65Qw== X-Google-Smtp-Source: AGHT+IFCCi89bFTikz1gHF2aQnV0vDcJQtLlVrJTrgRK42XOCfrrcA60ryEE+YUo8jisKKNBlH6/ X-Received: by 2002:a05:6e02:198b:b0:374:a477:5343 with SMTP id e9e14a558f8ab-3758030836amr4193595ab.9.1717695439103; Thu, 06 Jun 2024 10:37:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717695439; cv=pass; d=google.com; s=arc-20160816; b=iTpQ0OxFvE8hB/hdisYhtM+xwTSsX5UgrCe2p440p7AV93okVdYz8k8bZEvb9t3rp3 6RbvJ+UqDwrCrx6xt0eSAiBwEwIwvBJMo0VWk/4UCkFBAgauV1EznEdL2Y4N8/Ud4TDc 5KGi/RXlI4aIf9zPH3zPjjFUHZ6LQj1/6U6MkzoW29H+hq77MfNNCg1loV8SuWO84hG6 BzvzQDjfg8GfVxYAPqjLUsmSy3eKLZRZEebDjXsmcWwI1KQnEaEv2oGmxTGBjjEr//FE j7hOLCJff9malsVsk4yJ6bbRdVApmhCrV1plwlL58WiMJz+0KA7fWtoCjrT1ZszBKnSN GwUA== 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=mPjsJlKDBXzcFKJiIo/4f6GGzkeINELRnLiFwHcGpOo=; fh=KJT2EOZJQPmuet4BgPKPFc+oJCBXQdMFuQyoRnZt0Wc=; b=0L74+I9G8lbMuQrvHV09+osSBi7QYwpnoAkaHvzdM8MIt3GY5/fvYPf/3F4rWduJdU zXtVnbJfP0HROHCwTY7uBPy1bQgCziqwRdkgzPze4fvJo7b9QfCSChpXYSpqNuCay6yH L6JJO+2xz1A4DM18V2MB9nDmoDusPpC8e/eUFY+KCSCYKx+IuthnOIT4L8qA7buKrXUD JCidMl2StVCGLI2GJ3FnCpHKWffUqu1VEg2f1/9o1bVIkzjTiIHeyI2mVEuw+rv7Gcmz Xz5wuX8t2VVmNbA7RbK1ItjKYCEQC21GhnwctfX74zBQNMIvpt2/BSdGwUZbOGbXpn4O qL6A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mDTXyTi0; 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-204814-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204814-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de275ff394si1449543a12.455.2024.06.06.10.37.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 10:37:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-204814-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mDTXyTi0; 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-204814-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204814-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id E574D28C364 for ; Thu, 6 Jun 2024 17:33:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6738C197A6A; Thu, 6 Jun 2024 17:33:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mDTXyTi0" Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 6CF651991D5 for ; Thu, 6 Jun 2024 17:33:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717695218; cv=none; b=U/Glv4ttZypGPfXIJFqsUg5cd3ksHBaslfk5hQoUpCzlnJWZwsH5zUEAKF4YrshhkmTwRBMMg181WKjmjhE2JWL2vh/futhPKqtpwKDDYe7Boe0NYreKo4QwoK9gyhiYw9INOwZ87pyJuJBTtK1QrHvKHchu6ngsZAynQFctyoc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717695218; c=relaxed/simple; bh=rYVMkwI8qFoB/ptB7kU79gZFDx+FsNx/WF4p8yfanOI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Content-Type; b=ON7ElGIfxm7Rc7eEztKTwrKqmj064VMjAfnVBPM0DtLKuFNz8v3sXZ8e90enJkZg/6916z5A2tZCqVDw9NWYx+Oo5xzOu/y6MGdmO4WZQAX4CKCK4A4aPK/I3+zDjmmGKm82dSgTqH62MRbZ4ZNWkt3h9/wscauRAam9DBXaot4= 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=mDTXyTi0; arc=none smtp.client-ip=209.85.128.51 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-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4210aa012e5so13741165e9.0 for ; Thu, 06 Jun 2024 10:33:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717695215; x=1718300015; 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=mPjsJlKDBXzcFKJiIo/4f6GGzkeINELRnLiFwHcGpOo=; b=mDTXyTi0rNw369u0JcGh6PhF8bbU1DdWb+GmNbbS/K5/j1zjdJa6PDMz2m++vK1gwa M0qxjq4TcBc8R8CeRthxIZQfF5Y/RuM6f/Sme+sY6OvrFkZyWMw5QZvRDZlL3PbgoFIK vZT1mxIRTSQioeBa//dcS7595/B+C46yb79cZ5cCBKkyLcLWC7VaVQhzgc1s5wq52nGJ AZu3JET/rp8SQwrPDW0qXqJ7NJKJHRSIF3PB9tfKwPIlB4ZBJ8hfjzRWMNCraIIJ+26G Yg1jT2s3OZfaq9250Ovcm6F5rElsrxJ3yLWk5oMcwzGt0wBMZsJB0Y6Age3GbIzX5QOL m8Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717695215; x=1718300015; 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=mPjsJlKDBXzcFKJiIo/4f6GGzkeINELRnLiFwHcGpOo=; b=EOrJC16dZYZ5dHirQrE8vzj1MRImB6CWsD3F59JDNP+44tnD0a/de+kZ+YMrh9ffG8 XCEEHmiV/QE+Afg+PL/UKmKth8Ey9TSWkDUtAW1oI4MNg1DYL+jS5NErE1TsFMWMDBB0 krz9yB22ui85hpX9BKAEnHOcx8JqAF2YpQKtW+qeZMtq3TrjtL9BZWu3ZSTiXlcw1vGj t6oSjb5ri3Wr5OaxtVFGdAn2pTDr/0K7vi388mH1OmhlaOvYSByUsSSIgGLzqem3Q5BK Ii8utOcs+AVI0tK8lr0IAg8gsbEh2KX+2caNVH1su83UqbqCXMXaJUaaODBVKHFr9pJG sJ6A== X-Forwarded-Encrypted: i=1; AJvYcCUch4PHAqj3uTsPbYTtTGGqxClLXuhHuFZIkts4NxyxkGerOVVrZO7mkhe0YKOpNR1HZzm5fpSTnMlcU9JH6EHO8pu/bX9jqHXC0lWp X-Gm-Message-State: AOJu0YwmkQEeH8hgs7dkpAwzK4BcesrkqDMvG5BCRcqqhQv7RkCaiXEp R0rwgucaVV0Um5667G0JcP5l6ULyUvHV8B+cJZPN/Z0ocSN5Cl61fLsU2HNmDXJso6VHszP3WHg MTbPeUofdI+cb8tBWZmJlfR2RIT//vRwEVN3k X-Received: by 2002:a05:600c:54ca:b0:420:309a:fe63 with SMTP id 5b1f17b1804b1-42164a030a5mr3279955e9.22.1717695214069; Thu, 06 Jun 2024 10:33:34 -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-5-andrii@kernel.org> In-Reply-To: From: Suren Baghdasaryan Date: Thu, 6 Jun 2024 10:33:19 -0700 Message-ID: Subject: Re: [PATCH v3 4/9] fs/procfs: use per-VMA RCU-protected locking in PROCMAP_QUERY API To: "Liam R. Howlett" , Andrii Nakryiko , Suren Baghdasaryan , 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, rppt@kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jun 6, 2024 at 10:15=E2=80=AFAM Liam R. Howlett wrote: > > * Andrii Nakryiko [240606 12:52]: > > On Wed, Jun 5, 2024 at 4:16=E2=80=AFPM Suren Baghdasaryan wrote: > > > > > > On Tue, Jun 4, 2024 at 5:25=E2=80=AFPM Andrii Nakryiko wrote: > > > > > > > > Attempt to use RCU-protected per-VMA lock when looking up requested= VMA > > > > as much as possible, only falling back to mmap_lock if per-VMA lock > > > > failed. This is done so that querying of VMAs doesn't interfere wit= h > > > > other critical tasks, like page fault handling. > > > > > > > > This has been suggested by mm folks, and we make use of a newly add= ed > > > > internal API that works like find_vma(), but tries to use per-VMA l= ock. > > > > > > > > We have two sets of setup/query/teardown helper functions with diff= erent > > > > implementations depending on availability of per-VMA lock (conditio= ned > > > > on CONFIG_PER_VMA_LOCK) to abstract per-VMA lock subtleties. > > > > > > > > When per-VMA lock is available, lookup is done under RCU, attemptin= g to > > > > take a per-VMA lock. If that fails, we fallback to mmap_lock, but t= hen > > > > proceed to unconditionally grab per-VMA lock again, dropping mmap_l= ock > > > > immediately. In this configuration mmap_lock is never helf for long= , > > > > minimizing disruptions while querying. > > > > > > > > When per-VMA lock is compiled out, we take mmap_lock once, query VM= As > > > > using find_vma() API, and then unlock mmap_lock at the very end onc= e as > > > > well. In this setup we avoid locking/unlocking mmap_lock on every l= ooked > > > > up VMA (depending on query parameters we might need to iterate a fe= w of > > > > them). > > > > > > > > Signed-off-by: Andrii Nakryiko > > > > --- > > > > fs/proc/task_mmu.c | 46 ++++++++++++++++++++++++++++++++++++++++++= ++++ > > > > 1 file changed, 46 insertions(+) > > > > > > > > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c > > > > index 614fbe5d0667..140032ffc551 100644 > > > > --- a/fs/proc/task_mmu.c > > > > +++ b/fs/proc/task_mmu.c > > > > @@ -388,6 +388,49 @@ static int pid_maps_open(struct inode *inode, = struct file *file) > > > > PROCMAP_QUERY_VMA_FLAGS \ > > > > ) > > > > > > > > +#ifdef CONFIG_PER_VMA_LOCK > > > > +static int query_vma_setup(struct mm_struct *mm) > > > > +{ > > > > + /* in the presence of per-VMA lock we don't need any setup/= teardown */ > > > > + return 0; > > > > +} > > > > + > > > > +static void query_vma_teardown(struct mm_struct *mm, struct vm_are= a_struct *vma) > > > > +{ > > > > + /* in the presence of per-VMA lock we need to unlock vma, i= f present */ > > > > + if (vma) > > > > + vma_end_read(vma); > > > > +} > > > > + > > > > +static struct vm_area_struct *query_vma_find_by_addr(struct mm_str= uct *mm, unsigned long addr) > > > > +{ > > > > + struct vm_area_struct *vma; > > > > + > > > > + /* try to use less disruptive per-VMA lock */ > > > > + vma =3D find_and_lock_vma_rcu(mm, addr); > > > > + if (IS_ERR(vma)) { > > > > + /* failed to take per-VMA lock, fallback to mmap_lo= ck */ > > > > + if (mmap_read_lock_killable(mm)) > > > > + return ERR_PTR(-EINTR); > > > > + > > > > + vma =3D find_vma(mm, addr); > > > > + if (vma) { > > > > + /* > > > > + * We cannot use vma_start_read() as it may= fail due to > > > > + * false locked (see comment in vma_start_r= ead()). We > > > > + * can avoid that by directly locking vm_lo= ck under > > > > + * mmap_lock, which guarantees that nobody = can lock the > > > > + * vma for write (vma_start_write()) under = us. > > > > + */ > > > > + down_read(&vma->vm_lock->lock); > > > > > > Hi Andrii, > > > The above pattern of locking VMA under mmap_lock and then dropping > > > mmap_lock is becoming more common. Matthew had an RFC proposal for an > > > API to do this here: > > > https://lore.kernel.org/all/ZivhG0yrbpFqORDw@casper.infradead.org/. I= t > > > might be worth reviving that discussion. > > > > Sure, it would be nice to have generic and blessed primitives to use > > here. But the good news is that once this is all figured out by you mm > > folks, it should be easy to make use of those primitives here, right? > > > > > > > > > + } > > > > + > > > > + mmap_read_unlock(mm); > > > > > > Later on in your code you are calling get_vma_name() which might call > > > anon_vma_name() to retrieve user-defined VMA name. After this patch > > > this operation will be done without holding mmap_lock, however per > > > https://elixir.bootlin.com/linux/latest/source/include/linux/mm_types= .h#L582 > > > this function has to be called with mmap_lock held for read. Indeed > > > with debug flags enabled you should hit this assertion: > > > https://elixir.bootlin.com/linux/latest/source/mm/madvise.c#L96. > > The documentation on the first link says to hold the lock or take a > reference, but then we assert the lock. If you take a reference to the > anon vma name, then we will trigger the assert. Either the > documentation needs changing or the assert is incorrect - or I'm missing > something? I think the documentation is correct. It says that at the time of calling anon_vma_name() the mmap_lock should be locked (hence the assertion). Then the user can raise anon_vma_name refcount, drop mmap_lock and safely continue using anon_vma_name object. IOW this is fine: mmap_read_lock(vma->mm); anon_name =3D anon_vma_name(vma); anon_vma_name_get(anon_name); mmap_read_unlock(vma->mm); // keep using anon_name anon_vma_name_put(anon_name); > > > > > Sigh... Ok, what's the suggestion then? Should it be some variant of > > mmap_assert_locked() || vma_assert_locked() logic, or it's not so > > simple? > > > > Maybe I should just drop the CONFIG_PER_VMA_LOCK changes for now until > > all these gotchas are figured out for /proc//maps anyway, and > > then we can adapt both text-based and ioctl-based /proc//maps > > APIs on top of whatever the final approach will end up being the right > > one? > > > > Liam, any objections to this? The whole point of this patch set is to > > add a new API, not all the CONFIG_PER_VMA_LOCK gotchas. My > > implementation is structured in a way that should be easily amenable > > to CONFIG_PER_VMA_LOCK changes, but if there are a few more subtle > > things that need to be figured for existing text-based > > /proc//maps anyways, I think it would be best to use mmap_lock > > for now for this new API, and then adopt the same final > > CONFIG_PER_VMA_LOCK-aware solution. > > The reason I was hoping to have the new interface use the per-vma > locking from the start is to ensure the guarantees that we provide to > the users would not change. We'd also avoid shifting to yet another > mmap_lock users. > > I also didn't think it would complicate your series too much, so I > understand why you want to revert to the old locking semantics. I'm > fine with you continuing with the series on the old lock. Thanks for > trying to make this work. > > Regards, > Liam