Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1300737rwi; Fri, 14 Oct 2022 16:34:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4GkoHGJvGoBJOgHndlO0LaLm83N7Z/n0AICskeaVqtG5APtgPns2jqeFHtEglyP9VqaaFD X-Received: by 2002:a17:90a:7849:b0:20d:2d54:65d with SMTP id y9-20020a17090a784900b0020d2d54065dmr19921616pjl.219.1665790488388; Fri, 14 Oct 2022 16:34:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665790488; cv=none; d=google.com; s=arc-20160816; b=IfKafjt8W15VG7gE13q/fam7cywsRZbxbQCWVLyeX2LBnuK+dS2aG/IJVX2YeqqY2b QFbg9Hs7LmT5vY62v4sW0PZ0GzaJKf0dzW2z6QROfhdzIGA44GmHpxq7mytNg1su3v66 GWsYhg05Dhc4ELX/leOb5QiqsZOCUHet4B9E2L4JixlsrBBBm7ESWN7rINByoBXrhqhW LsWvskE/S9ZyjS1OaYIqRLn2RBUWRR1bLtOMGqpnkG9FmBtpHMt7zCdVu8JiyK2EZmpl q4KMdds/tj/RLVcuzhOkAyhDS1b23RCJZKj50LQ94Tqrv2cSCVqMZT2EwkyXOW5CFuUs w6HA== 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=MQmRtQd4Y17eTEgOLxItVutgn0mL2LxkpMiy4WqkuT4=; b=lfynx0tazLfOLmV3XHMpfsH3KJbkGNnDWb/+H0V5lQ+LVeCAzhZwSu2xcjdAdIBR2F rO0p0eMSDLHV87ZWh1UoUELz8zyv70FdwyaOB511r2edqY62XBJU1KZm32q6MpktEycP gZnJXaSMwfiiaN5/p8xs7Y9taVOqMR9oMbVl1bASZlJg1RkeaBOo9p+VaXoKmLYdQv7j d7M6qV2huAJwnFIpyx9LEawym6SE1XjgIKOUT4Faj9EMyutl58L7OI+cYiraWq/lR3rs /49zufjVHWlXnLZ4QEssSlMV1+1J3RDXdJ8nVAOjijg5ywyk/5gct10YCIDZGY8OkgcW mMvA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Dox+MWLE; 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 pj11-20020a17090b4f4b00b00209b614417esi9790509pjb.88.2022.10.14.16.34.36; Fri, 14 Oct 2022 16:34:48 -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=Dox+MWLE; 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 S229703AbiJNWaj (ORCPT + 99 others); Fri, 14 Oct 2022 18:30:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229540AbiJNWaf (ORCPT ); Fri, 14 Oct 2022 18:30:35 -0400 Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 502361408D for ; Fri, 14 Oct 2022 15:30:34 -0700 (PDT) Received: by mail-io1-xd2a.google.com with SMTP id h203so5033276iof.1 for ; Fri, 14 Oct 2022 15:30:34 -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=MQmRtQd4Y17eTEgOLxItVutgn0mL2LxkpMiy4WqkuT4=; b=Dox+MWLElhSlYcloiGdbaN0HaBpQV27gSS2NUSu6jtmMVfYGrj2fyDbGgg17sPi4rH lsUqZDhkqwwuZLVZlvOFNKoteJQFuuJjVWlFVCYfILVAUPEibUKcIumNIBsiga+wo/i2 8W64ktmmI1DN+ZsfCezcNv4AjGqIJVt/0rphSKdE9PyBxv8w9WgW40+311sazjyRZDqT gE4g70Fjd1IKTAwFtIaOSfgaOJPg/k8awcCTcO6Yv7FRym6RvE8s5PNt3Dc+qnOAmpSj u7R/9j1cQwgPwtG2y3ncvu0AUgx8bZhqzenjQbJ3M6kOEAu8rjaDcTYkf2l8/vOHT5PR Ej5g== 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=MQmRtQd4Y17eTEgOLxItVutgn0mL2LxkpMiy4WqkuT4=; b=ThdFllYfnNAHQ+pBsFRThImnt4V1LNfMbn+W2kxTiIi0eA9/hnZFywpvot+n20MTts npVeCWp9rDQc3D78+fanplY9r9/yn4GUlUFcOVtp3nJi6EOuveu0bbB+A/g5cehr891i DM0Q8TUbE6Gb5sR4I3mCiKIO0IVAbOM7lDBxQKP2/P4rSQpBDA0Cus2rMDCdoC2JlzKc u6h+o1jDblhkjMGmtyonlvei0c+qceNJ75zOgc/+M7fUc/KUmYWSDphwAt9d1T0dkllt QMZsCTqt6z4v1SF9z/PFp19ezBUu20iJ/kbZP4XwbKNpi3dn2znnzpaTZXHXVVsDoyyP EeVA== X-Gm-Message-State: ACrzQf01/EM3gQw82XLVvBYRrtNtc4j3mJERjKNVIudNRY6w5zVxy5Re bouA9aJ4MBz9pfO+8hrS4CAl8cwPScbXkhKznoRKk6p0hpE= X-Received: by 2002:a05:6638:3a15:b0:363:ecaf:2a53 with SMTP id cn21-20020a0566383a1500b00363ecaf2a53mr44236jab.66.1665786633385; Fri, 14 Oct 2022 15:30:33 -0700 (PDT) MIME-Version: 1.0 References: <20221014222346.n337tvkbyr33dsdx@box.shutemov.name> In-Reply-To: <20221014222346.n337tvkbyr33dsdx@box.shutemov.name> From: Jann Horn Date: Sat, 15 Oct 2022 00:29:57 +0200 Message-ID: Subject: Re: [BUG?] X86 arch_tlbbatch_flush() seems to be lacking mm_tlb_flush_nested() integration To: "Kirill A. Shutemov" Cc: Andy Lutomirski , Linux-MM , Mel Gorman , Rik van Riel , kernel list , Kees Cook , Ingo Molnar , Sasha Levin , Andrew Morton , Will Deacon , Peter Zijlstra , Linus Torvalds 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 Sat, Oct 15, 2022 at 12:23 AM Kirill A. Shutemov wrote: > On Fri, Oct 14, 2022 at 08:19:42PM +0200, Jann Horn wrote: > > Hi! > > > > I haven't actually managed to reproduce this behavior, so maybe I'm > > just misunderstanding how this works; but I think the > > arch_tlbbatch_flush() path for batched TLB flushing in vmscan ought to > > have some kind of integration with mm_tlb_flush_nested(). > > > > I think that currently, the following race could happen: > > > > [initial situation: page P is mapped into a page table of task B, but > > the page is not referenced, the PTE's A/D bits are clear] > > A: vmscan begins > > A: vmscan looks at P and P's PTEs, and concludes that P is not currently in use > > B: reads from P through the PTE, setting the Accessed bit and creating > > a TLB entry > > A: vmscan enters try_to_unmap_one() > > A: try_to_unmap_one() calls should_defer_flush(), which returns true > > A: try_to_unmap_one() removes the PTE and queues a TLB flush > > (arch_tlbbatch_add_mm()) > > A: try_to_unmap_one() returns, try_to_unmap() returns to shrink_folio_list() > > B: calls munmap() on the VMA that mapped P > > B: no PTEs are removed, so no TLB flush happens > > B: munmap() returns > > I think here we will serialize against anon_vma/i_mmap lock in > __do_munmap() -> unmap_region() -> free_pgtables() that A also holds. > > So I believe munmap() is safe, but MADV_DONTNEED (and its flavours) is not. shrink_folio_list() is not in a context that is operating on a specific MM; it is operating on a list of pages that might be mapped into different processes all over the system. So A has temporarily held those locks somewhere inside try_to_unmap_one(), but it will drop them before it reaches the point where it issues the batched TLB flush. And this batched TLB flush potentially covers multiple MMs at once; it is not targeted towards a specific MM, but towards all of the CPUs on which any of the touched MMs might be active.