Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp7512412rwi; Mon, 24 Oct 2022 16:03:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7aYmpldHMl4yWGCfiP/Et23SAcYNx7uso/FQ6FtwkguZylf9M6g6S/SMpG8t9xWj3tLaBq X-Received: by 2002:a17:907:7e87:b0:78e:2dc3:945 with SMTP id qb7-20020a1709077e8700b0078e2dc30945mr29950778ejc.326.1666652604887; Mon, 24 Oct 2022 16:03:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666652604; cv=none; d=google.com; s=arc-20160816; b=GHxk0AnMPzeBAYDXPcPvTTuFl0QpE52R23inFukvyWvT2Lf3hKb/+7UFVy98V8bLpJ mW5yZggcUF9NQ1nO3lmlwKROyiT75AotUfFs4w1X0dkY9Il74YOEdeLeFyTiuP1zVkvl daDibXss3p+e7+3y38Vz9dL3jrXDnKnUYtMSE4TeHxEAVXG0R1qNXnsXHFOdx4IjyM0a Rpd3PwamNxWkkfZZgl1/uV/WzYEDrm0Dpzzh+n85PLozEJ0lmWxG80BUzr8wzlY39h4y zHmyi717mMLR150wVPDDnS3FvJEUKCBHMJ0nB24Fut2heOpzzo8B9c5XZgIm4OROhiQe zN+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=hWSp/6UcndKaZ8NRQJXiNT1wjP9FmhU1KAH3NkWN5I4=; b=gnrda7pUMZjJxWYX//Q/gCxvuqsJ+8Nk18xV3y961iIZBaJBoDoSTNcm4h41+RgPJg bgsVSzg6ti8wOolBmFPlJfvVtdjl9838+A2cFIZ/z3r5X6szNxlXShdJ05bO0wUogEOl toFHn0jfrUu4CXmk2wMKiCItbIOe1Wj11rJWuqeZFziznZ79DSbpqVCRfmU4emEPcWk1 yJGE8hDp6h7sFj9nfULdbXtpAlb1hehIWhbjyObWdCywYGAR3PS4nHvS2Nl6ZKOpYCfG 8JqqO2PDpLWuHeuv5G6M3LeEnzWz028elyHXEz2a64CBl+vCIMyH8ypJ4TwGFeF7izjA IbLg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UzP0aPIk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y15-20020a170906470f00b0078b85fdf4e4si925632ejq.532.2022.10.24.16.02.59; Mon, 24 Oct 2022 16:03:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=UzP0aPIk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230398AbiJXVsK (ORCPT + 99 others); Mon, 24 Oct 2022 17:48:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230394AbiJXVrv (ORCPT ); Mon, 24 Oct 2022 17:47:51 -0400 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C53DFD2E for ; Mon, 24 Oct 2022 12:59:54 -0700 (PDT) Received: by mail-io1-xd2c.google.com with SMTP id z68so1302642iof.10 for ; Mon, 24 Oct 2022 12:59:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=hWSp/6UcndKaZ8NRQJXiNT1wjP9FmhU1KAH3NkWN5I4=; b=UzP0aPIkaGLmtOilY1VnI/pgy1ujFkv6mRlP6oURmh/OKv7pGfue41WG+pA0w9G2S1 vzrLcH0eaCqnYLMUk48QwLCP+G/x8RtSePFd96m8uhamOFQSghYcbTghAbe9lUWwEZAq idYVWY/Uu9vy2ap9WK9wEaCnuIBlmchwho/1/PU0Gqf5Kcb4Yaggpm8pCvEceDUPivTj 0V9HqvSCcTbX9F4BXlWiv4An24Nw32XxlxlpW3NxQf82usQ6jXe9PsS1BsxPcemb7gcw ThB/O4qzGUQfC5UkrNiKU7MusnMm5GD4rM+0/0sxz8lVqkkz1ZvUSQ3Xcr9c3G8cU9VQ YDLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=hWSp/6UcndKaZ8NRQJXiNT1wjP9FmhU1KAH3NkWN5I4=; b=KDpBNHaoNKfEgXZ7sXUxpcQ2+gE0gWrCq3qD6evPhIgWm6B59098QZSWn8KYLux0xW hxyLVvBfZ8xf7rQs7oY2gevRYzLXnsWD5FZe1Zm9PVEhLbt628W3j7sUu69ZUE1I5nVc QWLo2emzqd5mPZUrDxMzcP/WfvHTJR2KdZDIuVycnnFzfkczOz2tnCzqH1qnWC9ZAYYn 5Vdo7LhP3OwgNoB1tPN9H8VCpU5673p1eO5k+HIJVDbrPv06eJlxly92HqWNHdYS5vk+ 1q9l+Lv7YFTNidvJhbSwR6OxPGshVSqLPZmxSgSQA8mbisEcbwoWzcDEks7DAK0uPBBZ 7riA== X-Gm-Message-State: ACrzQf2bsrGYFuWEXhRmuyprETJPSB3GnMDVKaOfmu5hr+DyBg7bXbF5 MLpUmS33Vzuv0Jq794opZ4MnmfeCjJO4gLJ+RyApPeq47V0= X-Received: by 2002:a6b:5d08:0:b0:6bb:f915:4a33 with SMTP id r8-20020a6b5d08000000b006bbf9154a33mr18924194iob.2.1666641524042; Mon, 24 Oct 2022 12:58:44 -0700 (PDT) MIME-Version: 1.0 References: <20221022111403.531902164@infradead.org> <20221022114424.515572025@infradead.org> <2c800ed1-d17a-def4-39e1-09281ee78d05@nvidia.com> In-Reply-To: From: Jann Horn Date: Mon, 24 Oct 2022 21:58:07 +0200 Message-ID: Subject: Re: [PATCH 01/13] mm: Update ptep_get_lockless()s comment To: Peter Zijlstra Cc: John Hubbard , x86@kernel.org, willy@infradead.org, torvalds@linux-foundation.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, aarcange@redhat.com, kirill.shutemov@linux.intel.com, jroedel@suse.de, ubizjak@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 24, 2022 at 10:01 AM Peter Zijlstra wrote: > > On Sun, Oct 23, 2022 at 10:42:49PM -0700, John Hubbard wrote: > > On 10/22/22 04:14, Peter Zijlstra wrote: > > > --- a/include/linux/pgtable.h > > > +++ b/include/linux/pgtable.h > > > @@ -260,15 +260,12 @@ static inline pte_t ptep_get(pte_t *ptep > > > > > > #ifdef CONFIG_GUP_GET_PTE_LOW_HIGH > > > /* > > > - * WARNING: only to be used in the get_user_pages_fast() implementation. > > > - * > > > - * With get_user_pages_fast(), we walk down the pagetables without taking any > > > - * locks. For this we would like to load the pointers atomically, but sometimes > > > - * that is not possible (e.g. without expensive cmpxchg8b on x86_32 PAE). What > > > - * we do have is the guarantee that a PTE will only either go from not present > > > - * to present, or present to not present or both -- it will not switch to a > > > - * completely different present page without a TLB flush in between; something > > > - * that we are blocking by holding interrupts off. > > > + * For walking the pagetables without holding any locks. Some architectures > > > + * (eg x86-32 PAE) cannot load the entries atomically without using expensive > > > + * instructions. We are guaranteed that a PTE will only either go from not > > > + * present to present, or present to not present -- it will not switch to a > > > + * completely different present page without a TLB flush inbetween; which we > > > + * are blocking by holding interrupts off. > > > > > > This is getting interesting. My latest understanding of this story is > > that both the "before" and "after" versions of that comment are > > incorrect! Because, as Jann Horn noticed recently [1], there might not > > be any IPIs involved in a TLB flush, if x86 is running under a > > hypervisor, and that breaks the chain of reasoning here. > > That mail doesn't really include enough detail. The way x86 HV TLB > flushing is supposed to work is by making use of > MMU_GATHER_RCU_TABLE_FREE. Specifically, something like: > > > vCPU0 vCPU1 > > tlb_gather_mmut(&tlb, mm); > > .... > > local_irq_disable(); > ... starts page-table walk ... > > > > tlb_finish_mmu(&tlb) > ... > kvm_flush_tlb_multi() > if (state & KVM_VCPU_PREEMPTED) > if (try_cmpxchg(,&state, state | KVM_VCPU_FLUSH_TLB)) > __cpumask_clear_cpu(cpu, flushmask); > > > tlb_remove_table_sync_one() / call_rcu() > > > > > ... continues page-table walk ... > local_irq_enable(); > > If mmu gather is forced into tlb_remove_talbe_sync_one() (by memory > pressure), then you've got your IPI back, otherwise it does call_rcu() > and RCU itself will need vCPU0 to enable IRQs in order to make progress. > > Either way around, the actual freeing of the pages is delayed until the > page-table walk is finished. > > What am I missing? Unless I'm completely misunderstanding what's going on here, the whole "remove_table" thing only happens when you "remove a table", meaning you free an entire *pagetable*. Just zapping PTEs doesn't trigger that logic.