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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=ham 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 CFFD3C4338F for ; Mon, 2 Aug 2021 00:16:28 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5BED36054F for ; Mon, 2 Aug 2021 00:16:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5BED36054F 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 E74548D0001; Sun, 1 Aug 2021 20:16:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFCBD6B0036; Sun, 1 Aug 2021 20:16:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9D2E8D0001; Sun, 1 Aug 2021 20:16:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0070.hostedemail.com [216.40.44.70]) by kanga.kvack.org (Postfix) with ESMTP id AA3EB6B0033 for ; Sun, 1 Aug 2021 20:16:27 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 55AC5180AD81A for ; Mon, 2 Aug 2021 00:16:27 +0000 (UTC) X-FDA: 78428224014.04.710138D Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by imf09.hostedemail.com (Postfix) with ESMTP id 08A133000139 for ; Mon, 2 Aug 2021 00:16:26 +0000 (UTC) Received: by mail-ej1-f41.google.com with SMTP id hw6so14099515ejc.10 for ; Sun, 01 Aug 2021 17:16:26 -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=QgfmF/O4y4TDgdvbuE5rlFQmQjvGwXeEHXj3QAJZlao=; b=UEx+7MrYdZY/nTwdYYbmSF0UVARmJ5TubstgWxNYq1ZgwWD5WIeOt4rmcpyo7brdVk zLDe0WiBglVlFknD0R3Tk8ih2MbzLv4PctRbao0Dfltk46z0H2VSC8hueiCALDBfxz64 ycaqaE1vx0Tn8khGFh9U8klBN0yS6o7odYb7cA1A3ApriKhxZt35FG56ODtzC2LtWpVZ HO5TCIuqZNM1Q4ws+ccbjXeq75u1SPSMu2q6QwQs3/430ezcCPofdgwz8VLnXB+QqMpV 7mRjC+p6XzKiw7CcV+noRPHdM/ZSigG0i1ZtFYJn1PRpgBUCqC4NUyfYQuLEZUOJlJvb xPag== 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=QgfmF/O4y4TDgdvbuE5rlFQmQjvGwXeEHXj3QAJZlao=; b=NID2+ymnuEG+TBC87cEIZz6dI4I3SjbkyM2/0X6F4VzULfemcchWgDpes5hixU09Mt Ojf8Taa6z/P6eK3ZR9sFO6EIgKc6H+ATi37m7GN03C0uU6GAuxgg0sOMlAaXECyW9wi3 hifT1PVYSA5a653DKAMP0lB9PBGm9v4WcnP0zPfihu9I1MSCWhXZ5iEJFvQAcTbK7Lr3 bvBM/VzQKBRdT7h1SKdKGmT92tJoyZd61w56VJyLqyV59wYUJj+2JR+esl+e6bp5clhP WPSz8FPxI03DD9vzzVP0YXBvhY9J9EmiOvtHpmDozqOkZaYcHHouLglj1M27jOQcYjrS Axiw== X-Gm-Message-State: AOAM533huWAG33U+tT8tCx5GI2OBCYy9FuQhrnb2MkoGmKC0+af7fl80 RItLQ/Mg2f742bGLsOE7sd12QHXXA+ZquF1tNPSt3yxIBhtygw== X-Google-Smtp-Source: ABdhPJxFCfAsK6OeHM0o/Arn3r6OXz1iESjcrPoZuvXbibu/NUPHEIu8g17vbhtLGtoFgTAaDGpSlQgafE3vUTCK2oo= X-Received: by 2002:a17:906:5e07:: with SMTP id n7mr13318802eju.552.1627863385573; Sun, 01 Aug 2021 17:16:25 -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:16:14 +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: text/plain; charset="UTF-8" Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20161025 header.b=UEx+7MrY; spf=pass (imf09.hostedemail.com: domain of lrizzo@google.com designates 209.85.218.41 as permitted sender) smtp.mailfrom=lrizzo@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 08A133000139 X-Stat-Signature: scqmq67kexbd5z6f1kdugt33dxny6yj8 X-HE-Tag: 1627863386-972421 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: (repost in plain text) 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