Received: by 2002:ab2:7041:0:b0:1f4:bcc8:f211 with SMTP id x1csp118758lql; Fri, 12 Apr 2024 05:47:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVvjud+G+M5hs+t83h8XBdGBCpvY1JBzCDNtpDLM1XtykIt2lLBkCbrJ84oD9LIEGmP28WXrVwkdEe8uUzp2wIKTYaSnccTtnQto+3Mvw== X-Google-Smtp-Source: AGHT+IHvUlVU4TYXK++44BvnC/+RwFsRk4/HvybMDVxUBNWs8yoGtEUM+gsbvCm16XVmj/t/Hiss X-Received: by 2002:a17:906:2bce:b0:a52:3ca9:9bda with SMTP id n14-20020a1709062bce00b00a523ca99bdamr846449ejg.22.1712926036314; Fri, 12 Apr 2024 05:47:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712926036; cv=pass; d=google.com; s=arc-20160816; b=JwRl7/qgE82pSfpbzrnRAP/uloFPXFL4g591uNKzmXP9PcR3wGlWyqaN1D6K4swXUD rEim+WzVmQhvCD5NbBfKKhsILejuclApjqxgGiNi4SeGLtQdcoz+yAN3MOoilisIbSgE CCl7gO81i6MeWRWughpPpQFe0nEpKhLmYKMQj0d7fJwC6VDXAch3m0eab+YxuX3GllR4 H3KIrwGHNvXMpP4tQoT2yS4M9C3Km9i+RZSqFt4G+3JqqxQGc5SP5ILl1LS2IZq9+ldq i2iwrTAv5lOG2URemMQPwxRzk/QfO0e76+z5TLD2z1pjc05g+lJZs59iw8kJR/GZ+5of 1p0A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=z2Fr7UK4mXSasVYjxfIK4yXcjvIBvCQKhIDx0tN0PsE=; fh=ttzz9RQDhFlalRUONGgdllNN1MA0GcJ6mhD2jvBl9m8=; b=wFaXkbELRrCD8d37gYxof7VJ1jpa0UAZQnGDqp7MkjeovTomm9jaQaRJchWWDcfHCx 5QGAahw2MIb5OWZHqIPW/zTI9t3tGUA/ufKsSD4mqavg3+VjD3HQ9xp3W3sLcaKK/Q68 QKrGDVvmvdMLJsSGaSXDufNxiBPUo87EKrcysfwupvze5q8WS1jghnSZ8M+RPFCaqaFp HQXE8d1DdsQbsYVn6Q9Vu3WGNSg/c6WqqGsTXiRNEiQSkX/WiLo+qzoclX1+BYevh7LW /RdiDH9gMpXqaSOEF1pm3XfEeOXy8bBfHYNEF3BcJuuvvYhEtkyEo2OYipZKddEW7K3p FZEA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZfqyyvYJ; 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-142665-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142665-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. [147.75.80.249]) by mx.google.com with ESMTPS id cs17-20020a170906dc9100b00a51c7ecfa3bsi1777881ejc.489.2024.04.12.05.47.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 05:47:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-142665-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ZfqyyvYJ; 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-142665-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-142665-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 D4C3A1F2170B for ; Fri, 12 Apr 2024 12:47:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D8BFD59154; Fri, 12 Apr 2024 12:47:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZfqyyvYJ" Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) (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 8E7E73BBC3 for ; Fri, 12 Apr 2024 12:47:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712926028; cv=none; b=JKDgGaxSNH4vdPN//obViJFe2YuYRooMT/kMlxfYUr2SV6PICeRdsKWGXlXfSlG2BAnwseJYUlt+DX5yv9l748Ohb/bQn850dok0NuPZ/G8kZsFwRup32EpnreU6OxYFetHMO2Nyw79Q/P+i0A5Dw/kGeZNLhlkJOXajpvnuU8M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712926028; c=relaxed/simple; bh=YMEv9nfCTRdb23MZLqU4t4+ChnuYRbUYDBnLYpTmJdA=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=AMMXgA5mmk3avB5AykCqc/e7eTRypaKhOy8qVS1Zlso6QtP7A4y0KHVcGESfiQQMFflYcV8gEKpMk+wgKD9G0mowRXVN2NtXiq5cRRvgIYbZrFL/6p++6Z47B25httudqMbLic58FASlCcM4ZB7dy/feRUQ9tGcUh5j7VdCHMkw= 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=ZfqyyvYJ; arc=none smtp.client-ip=209.85.219.173 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-f173.google.com with SMTP id 3f1490d57ef6-ddda842c399so905200276.3 for ; Fri, 12 Apr 2024 05:47:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712926025; x=1713530825; darn=vger.kernel.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=z2Fr7UK4mXSasVYjxfIK4yXcjvIBvCQKhIDx0tN0PsE=; b=ZfqyyvYJiENd7CkCUH+wzpZKo5CRl//WaQKOMGTwQEJS+O9wYD1RNylt5ZvQLfN7/H psmzCKxZAgWpObMbdCuksy6O9wS2uLjKBrpgp0yDi1+JDuQDRuGRNtBaE6k3d+LZrJEH 08ZM1xt0J/uI7hH7t4YJonA/7sbxD8yTN4Drv/kxvAiGc4uYiZWCjW/9oszIGAcbOO+a 8a+r4bYYGWzH3tk4fRIJp0cPZGloFcNA6eEo2kbRDu3VJwaCxuaQgMvkcgP8Tp4cKR0I +Xd+9win5uxYz/i4pbVzsYn2rSIPeafPIFCCuGzqB8BVOZhw0TcmGGL0JvqkW6Jr8Dcg Fqqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712926025; x=1713530825; 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=z2Fr7UK4mXSasVYjxfIK4yXcjvIBvCQKhIDx0tN0PsE=; b=tF//3H1BXU64buqQ1ixePJ7UNfNOs+K0J/u31dROnyelEVy+lEYISNWimMdRqRTTDu IjsdPxX8CM4vR2cKx2jX8VTNPEZFSbXUaj3BNaLbwWQK75nv1WklodG4wqgBZwaEJjJ9 Fy/AqXHp5L7kuOuMF5pdRhim1tbuzIbij2dBtyQ5r6LCy6LMOugxrdfmPzw1aOqt5lCS Yo6oEApUSpBV4ExAhEIAdua4D0s/hP6tHx+0UddCbgoJsoxLgyiWC+N/erDLR+taz6fC IKZE+szMSBRHAsPkR0JdS2k4/0cTCBITEltn0VJ28GHyR2EBoThJJYfFBkukZrJv1+g4 phkw== X-Forwarded-Encrypted: i=1; AJvYcCU7CCSNSV4uRmDyVcAW1wpUr95s5EpH1WO6EDmH3vyCser8YAzon1n75pVRAYWG+5bA3Dzqcm2mdG/TpzUze013gysJD5u6ZTv7rGF5 X-Gm-Message-State: AOJu0YxUKm68iYZhP5xqTiFp6cL35RtbRsKe/njiLZVDbMSHVcl7N565 IA9gPAwebWORjLdhJ7oEF7kwtcQGEJpJqy1WQsVmv40+csjXkACYhziopWQdzPY3AA4MsGB/lBv J3EUIzfVI7E2vJ26bDfX8Wu1u/+afVvD7tisN X-Received: by 2002:a25:a128:0:b0:dcc:f5d4:8b43 with SMTP id z37-20020a25a128000000b00dccf5d48b43mr2409323ybh.9.1712926025188; Fri, 12 Apr 2024 05:47:05 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240410170621.2011171-1-peterx@redhat.com> <20240411171319.almhz23xulg4f7op@revolver> In-Reply-To: From: Suren Baghdasaryan Date: Fri, 12 Apr 2024 05:46:52 -0700 Message-ID: Subject: Re: [PATCH] mm: Always sanity check anon_vma first for per-vma locks To: Matthew Wilcox Cc: Peter Xu , "Liam R. Howlett" , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Lokesh Gidra , Alistair Popple Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 11, 2024 at 8:14=E2=80=AFPM Matthew Wilcox wrote: > > On Thu, Apr 11, 2024 at 11:02:32PM +0100, Matthew Wilcox wrote: > > > How many instructions it takes for a late RETRY for WRITEs to private= file > > > mappings, fallback to mmap_sem? > > > > Doesn't matter. That happens _once_ per VMA, and it's dwarfed by the > > cost of allocating and initialising the COWed page. You're adding > > instructions to every single page fault. I'm not happy that we had to > > add extra instructions to the fault path for single-threaded programs, > > but we at least had the justification that we were improving scalabilit= y > > on large systems. Your excuse is "it makes the code cleaner". And > > honestly, I don't think it even does that. > > Suren, what would you think to this? > > diff --git a/mm/memory.c b/mm/memory.c > index 6e2fe960473d..e495adcbe968 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -5821,15 +5821,6 @@ struct vm_area_struct *lock_vma_under_rcu(struct m= m_struct *mm, > if (!vma_start_read(vma)) > goto inval; > > - /* > - * find_mergeable_anon_vma uses adjacent vmas which are not locke= d. > - * This check must happen after vma_start_read(); otherwise, a > - * concurrent mremap() with MREMAP_DONTUNMAP could dissociate the= VMA > - * from its anon_vma. > - */ > - if (unlikely(vma_is_anonymous(vma) && !vma->anon_vma)) > - goto inval_end_read; > - > /* Check since vm_start/vm_end might change before we lock the VM= A */ > if (unlikely(address < vma->vm_start || address >=3D vma->vm_end)= ) > goto inval_end_read; > > That takes a few insns out of the page fault path (good!) at the cost > of one extra trip around the fault handler for the first fault on an > anon vma. It makes the file & anon paths more similar to each other > (good!) I see what you mean. The impact would depend on the workload but in my earlier tests when developing per-VMA locks there were on average less than 1% faults which were for anonymous pages and had vma->anon_vma=3D=3DNULL. I recorded that after using my desktop for a day or so and running a series of benchmark tests. Again, that number might be drastically different on some other workloads. About the code, I'll take a closer look once I'm back from vacation this weekend but I think you will also have to modify do_anonymous_page() to use vmf_anon_prepare() instead of anon_vma_prepare(). > > We'd need some data to be sure it's really a win, but less code is > always good. > > We could even eagerly initialise vma->anon_vma for anon vmas. I don't > know why we don't do that. You found the answer to that question a long time ago and IIRC it was because in many cases we end up not needing to set vma->anon_vma at all. So, this is an optimization to try avoiding extra operations whenever we can. I'll try to find your comment on this.