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 X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, HTML_MESSAGE,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2481C4338F for ; Mon, 2 Aug 2021 00:43:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3901F610A0 for ; Mon, 2 Aug 2021 00:43:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3901F610A0 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id A855B6B0033; Sun, 1 Aug 2021 20:43:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A363F6B0036; Sun, 1 Aug 2021 20:43:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8FD248D0001; Sun, 1 Aug 2021 20:43:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0119.hostedemail.com [216.40.44.119]) by kanga.kvack.org (Postfix) with ESMTP id 721636B0033 for ; Sun, 1 Aug 2021 20:43:08 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 142CC8249980 for ; Mon, 2 Aug 2021 00:43:08 +0000 (UTC) X-FDA: 78428291256.30.8CB23B2 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) by imf07.hostedemail.com (Postfix) with ESMTP id BF7A210004FB for ; Mon, 2 Aug 2021 00:43:07 +0000 (UTC) Received: by mail-ed1-f47.google.com with SMTP id n2so22117012eda.10 for ; Sun, 01 Aug 2021 17:43:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=E4XWhAk2IJWrjDcl0kLvTKDrtF+KXhOl+vYmaul9OiY=; b=A0DpbwggrfpIRknY0RGsTucGRXJLX6umiS40U9L5FfBWCvKRUFz4s3EUQ7cLiRamgq dTpjZT7avpgGPRVe3gz7PX2xqrn1jlWAWxwBnd4kacc9Ge38UYf1u1nli4RqNJLltI6D 6IDwDPAO26JFWEWCU9LQk0cWvAZ/+Gv9xm24Bs+rYUGQ/E56FirPMchVK8YauYuQbGB2 NGW+Q9novVXWhzjN887vNwMtqE1gZ8uNxQcJCV81TteQH/PGzAoIbUIEhLogsi36YT18 f6F4DjHHAHI6pFaXCK8u4iMQsS90cr8e9I4lqIdLATHXL7tl4pUWRiKH98EQSAFs1izJ muYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=E4XWhAk2IJWrjDcl0kLvTKDrtF+KXhOl+vYmaul9OiY=; b=ni5bepgpzsWQJuAWDvIzn6wt7eP7W7+/es0TdQLKZ1Atv6RAoaMYmRmjpeosMV0eIY 6ccse9eWILyB7VQcHAk/Lg/x+3nsKHl1ykAZr8CCO/ogq1J4fNO69gSR9syZNILI3CNV 8Q1yMblVtCPWzFeLLO/6h0xfsGtrPJKOEAr254aLzaaqMsc2u5fRvnFBWZdKGtYjHVuk NJlbYzxsoGxtmlPkv6kF0etm4CJHx/OeV0U1P1vZTBJWo1ATAw+tX2CG+hxGJ40Kbbwn fJhxGFCEMrcSefa6yhBS06zlC1NzkzC+fB7Xm6cXfbfJrtbhUmB9JRk8zNLAA7K6Lo7o RFKA== X-Gm-Message-State: AOAM531BOHQrOe9sbr3hUo3MnNiQIQu9GK3Z8IerazpdxO//HfF/3IUc zF5cVgnmkzJYdivaHFoOwsVOGHaN4bSooMz+LqpjkCKfLE+soQ== X-Google-Smtp-Source: ABdhPJz31fntFKMtR5EvGO0yWY9JBVGBx45h4oeIHYD8aOygfZWGGKMluK+NG36u3RR/Mkt78OZ84nR5qrjC2GSUvhk= X-Received: by 2002:a05:6402:b83:: with SMTP id cf3mr16175753edb.12.1627863080177; Sun, 01 Aug 2021 17:11:20 -0700 (PDT) MIME-Version: 1.0 References: <20210731175341.3458608-1-lrizzo@google.com> <20210801123335.6a7f8e1ee1e52ea64db80323@linux-foundation.org> In-Reply-To: <20210801123335.6a7f8e1ee1e52ea64db80323@linux-foundation.org> From: Luigi Rizzo Date: Mon, 2 Aug 2021 02:11:09 +0200 Message-ID: Subject: Re: [PATCH] Add mmap_assert_locked() annotations to find_vma*() To: Andrew Morton Cc: linux-kernel , David Rientjes , linux-mm@kvack.org Content-Type: multipart/alternative; boundary="000000000000a3589905c888688d" Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=A0Dpbwgg; spf=pass (imf07.hostedemail.com: domain of lrizzo@google.com designates 209.85.208.47 as permitted sender) smtp.mailfrom=lrizzo@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: BF7A210004FB X-Stat-Signature: qw4wx6f6ebzeknqu53uin9akfiiiudwr X-HE-Tag: 1627864987-965298 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: --000000000000a3589905c888688d Content-Type: text/plain; charset="UTF-8" On Sun, Aug 1, 2021 at 9:33 PM Andrew Morton wrote: > On Sat, 31 Jul 2021 10:53:41 -0700 Luigi Rizzo wrote: > > > find_vma() and variants need protection when used. > > This patch adds mmap_assert_lock() calls in the functions. > > > > To make sure the invariant is satisfied, we also need to add a > > mmap_read_loc() around the get_user_pages_remote() call in > > get_arg_page(). The lock is not strictly necessary because the mm > > has been newly created, but the extra cost is limited because > > the same mutex was also acquired shortly before in __bprm_mm_init(), > > so it is hot and uncontended. > > > > Well, it isn't cost-free. find_vma() is called a lot and a surprising > number of systems apparently run with CONFIG_DEBUG_VM. Why do you > think this cost is justified? > I assume you are concerned with the cost of mmap_assert_locked() ? I'd say the justification is the same as for all asserts: at some point some code change may miss the required lock, and the asserts are there to catch elusive race conditions, There are in fact already instances of mmap_locked_assert() right before find_vma() in walk_page_range(), and a couple before calls to __get_user_pages(). As for the cost, I'd think that if CONFIG_DEBUG_VM is set, one does it on purpose to catch errors and is prepared to pay the cost (in this case the atomic_read(counter) in rwsem_is_locked(), the counter should be hot). FWIW I have instrumented find_vma() on a fast machine using kstats https://github.com/luigirizzo/lr-cstats (load the module then enable the trace with echo "trace pcpu:find_vma bits 3" > /sys/kernel/debug/kstats/_control and monitor the time with watch "grep CPUS /sys/kernel/debug/kstats/find_vma" I didn't run anything especially intensive except some network benchmarks, but I have collected ~2M samples with the following distribution of find_vma() time in nanoseconds in 3 configs: CONFIGURATION p10 p50 p90 p95 p98 no-debug 89 109 214 332 605 debug 331 369 603 862 1338 debug+this patch 337 369 603 863 1339 As you can see, just compiling a debug kernel, even without this patch, makes the function 3x more expensive. The effect of this patch is not measurable (the differences are below measurement error). cheers luigi --000000000000a3589905c888688d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Sun, Aug 1, 2021 at 9:33 PM Andr= ew Morton <akpm@linux-foundation.org> wrote:
On Sat, 31 Jul 2021 10:53:41 -0700 Luigi Rizzo &= lt;lrizzo@google.com= > wrote:

> find_vma() and variants need protection when used.
> This patch adds mmap_assert_lock() calls in the functions.
>
> To make sure the invariant is satisfied, we also need to add a
> mmap_read_loc() around the get_user_pages_remote() call in
> get_arg_page(). The lock is not strictly necessary because the mm
> has been newly created, but the extra cost is limited because
> the same mutex was also acquired shortly before in __bprm_mm_init(), > so it is hot and uncontended.
>

Well, it isn't cost-free.=C2=A0 find_vma() is called a lot and a surpri= sing
number of systems apparently run with CONFIG_DEBUG_VM.=C2=A0 Why do you
think this cost is justified?

I assume you are c= oncerned with the cost of mmap_assert_locked() ?

I'd say the jus= tification is the same as for all asserts:
at some point some code change = may miss the required lock, and=C2=A0the
asser= ts are there to catch elusive race conditions,

There are in fact already instances of mm= ap_locked_assert()
= right before find_vma() in walk_page_range(), and a couple before
<= span style=3D"font-family:monospace,monospace">calls to __get_user_pages().=

<= div class=3D"gmail_default">
As for the cost, I'd think that if=C2=A0CONFIG_DEBUG_VM is set,
one=C2=A0does it on pur= pose to catch errors=C2=A0and is prepared to pay
=
the cost (in this case the=C2=A0atomic_read(cou= nter) in=C2=A0rwsem_is_locked(),
th= e counter should be hot).

FWIW I have instrumented find_vma() on a fast machine using kstats<= /span>


(load the module th= en enable the trace with
=C2=A0 echo "trace pcpu:find_vma = bits 3" > /sys/kernel/debug/kstats/_control
and monitor= the time with
=C2=A0 watch "grep CPUS /sys/kernel/debug/k= stats/find_vma"

I didn't run anything= especially intensive except some network
benchmarks, but I hav= e collected ~2M samples with the following=C2=A0
distribution o= f find_vma() time in nanoseconds in 3 configs:

CONFIGURATION=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0p10=C2=A0 =C2=A0p50=C2=A0 = =C2=A0p90=C2=A0 =C2=A0p95=C2=A0 =C2=A0p98

no-= debug=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A089=C2=A0 =C2=A0= 109=C2=A0 =C2=A0214=C2=A0 =C2=A0332=C2=A0 =C2=A0 605
debug=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0331=C2=A0 =C2=A0= 369=C2=A0 =C2=A0603=C2=A0 =C2=A0862=C2=A0 =C2=A01338
debug+this= patch=C2=A0 =C2=A0 =C2=A0 337=C2=A0 =C2=A0369=C2=A0 =C2=A0603=C2=A0 =C2=A0= 863=C2=A0 =C2=A01339

As you can see, just comp= iling a debug kernel, even without this patch,
makes the=C2=A0<= span style=3D"font-family:monospace,monospace">function 3x more expensive. = The effect of this patch is
not measurable (the differe= nces are below measurement error).

cheers
luigi
--000000000000a3589905c888688d--