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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E059C67871 for ; Mon, 24 Oct 2022 19:58:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACC63940008; Mon, 24 Oct 2022 15:58:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7CE4940007; Mon, 24 Oct 2022 15:58:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94441940008; Mon, 24 Oct 2022 15:58:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 80CD5940007 for ; Mon, 24 Oct 2022 15:58:45 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 46940160A03 for ; Mon, 24 Oct 2022 19:58:45 +0000 (UTC) X-FDA: 80056905810.19.6CDFAA9 Received: from mail-io1-f50.google.com (mail-io1-f50.google.com [209.85.166.50]) by imf28.hostedemail.com (Postfix) with ESMTP id D473AC000B for ; Mon, 24 Oct 2022 19:58:44 +0000 (UTC) Received: by mail-io1-f50.google.com with SMTP id i65so8698932ioa.0 for ; Mon, 24 Oct 2022 12:58:44 -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=qP/thjgyuAieWIIuLJnTzw7Z4QodSVaj7b3Ew9ay+yaSWqlnE0xFDLoorYX6tO5ZTM 2QbO/nvM/TBGOi+qdCp4+GwFKwy4vOHMkrNnHzoY8XywKd9PyzGhPXG1sS2QwprfQJv6 8bmsFldLxcwcGpCx4rA6LEuULuwLVH2aOlXmERD7Weoy0AFzfozbGuqFarTS56o/CfrU agdRs0+FG9k00iNAvZgT+5wCUF5tsIAkcIMp+TnpOeM1glnni1ySSLHkKauZlMVI9qGC CvdPK1bCrHqtdgAifUTkt55kIxcdJcJHqvYCkkmSVMx8pI9jJPyLPwKAQgzKDVLiEHmf VK9Q== X-Gm-Message-State: ACrzQf1gQY+kF1W/l/Ru6fUetNRlyiC8PNiOW6ohDboZHfMz+jFhNi9d Oxt51UMFZT/Ku6kL2t90IukUcel/alyPZQmAUYjbzQ== X-Google-Smtp-Source: AMsMyM6AlYIzYIEv7dB8WzNL/xThVqRV40naQrbQOOYHQpXH7IRYVOsjebSlUxSlYOzR4jNfcdlXvdStJ+odCEbY0gY= 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" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666641524; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hWSp/6UcndKaZ8NRQJXiNT1wjP9FmhU1KAH3NkWN5I4=; b=TYeFfhVFzYeSaOAvTddtZCVI89UreHkWJh+pg9mZlAhYfZQiU0zeCvc/KDQcmiXnXb2amj UwOoRNkFWCY4mJ3kEufLS6+T4jTL9pRWfPzr3RmYxWogkLiTUxn1d43Kq0VRDJiE3mDsqG Q/rATC+FLYnGOkN3PkwAA7YeRc/Vcl0= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=UzP0aPIk; spf=pass (imf28.hostedemail.com: domain of jannh@google.com designates 209.85.166.50 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666641524; a=rsa-sha256; cv=none; b=gy1X42CzmTmGWalWqQ160bSO0PVQOrWvCrPS/W2hkgGy3gDo3bL3W3sIM2DeJxgkXPOrL7 iY/KbhtzJBRTBy/OxgrevKvK4sj7dyMzWB4wa7GAA/ZAW9L6mZwqeyJqjvVYlC1kpBdFBQ HBXdXdEa2l4IRxI1+KYtkLdm5zlqs4M= X-Rspamd-Queue-Id: D473AC000B Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=UzP0aPIk; spf=pass (imf28.hostedemail.com: domain of jannh@google.com designates 209.85.166.50 as permitted sender) smtp.mailfrom=jannh@google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Stat-Signature: dhjwaioayne9fk7bibjcsrz3jam79zyt X-HE-Tag: 1666641524-558199 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: 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.