Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1018229rwi; Fri, 14 Oct 2022 11:35:58 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5AenNwdno6t+rAa4jTPcFvspizhffk7U69+RFQz0N4uyc4qBLxP6F0elayhCfzHpnghOtH X-Received: by 2002:a65:49c9:0:b0:462:9ce1:3f58 with SMTP id t9-20020a6549c9000000b004629ce13f58mr5709319pgs.200.1665772558221; Fri, 14 Oct 2022 11:35:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665772558; cv=none; d=google.com; s=arc-20160816; b=oy/MRSmzcM2n694wGA8xpIrfp/4lvB/olowA7q7bJsVgudzpy4AMiat1esLMx4ZPLP qX7WJUB6x9aYxlX+kAKDU9KlRvBtMBeMRaU5/h2VHnN9IbNcIZEqduA+tXY3rJeTJdSG Eha+5lmC0CcnewFkYq/LgHZ/+z4ecXGMyGX/e+e0+ctn/CzKrSr2/WhdfhNQGcqWDy2i ZOsNVo/NiZTIIMJC1Dl0WGuysBR5Z+dbDfhKfCmL5HO08rR48gg7NmZ+1w6wczIY7sy/ iJlLz0aXzMsgh8BbK2gqEEgh5k5P6pW5qPVh+fPFsT9YCKAx1v9R6TXTOTwDeUSq1NrE sAGw== 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:mime-version :dkim-signature; bh=hRrLIOmcMN6dxNUCyEtTWc83pVv8BngJLKL099x7jQ0=; b=Tg6MaQszfDtlcaw/fTZaa+UxpfTY1QsPdZTFkbSr5XBPEySn0QemDkKRXt2Wjfn4YL 7d4tZ3LMQhz9CmdZU4sMgZxKzIaHaee3/RYsHkFD4JMh8jglTnElo1HnhwuDzvApI9CD Fow0oJDtu3kTqttNAzxCj0rtdyQz7HJwLGfyDETYCGFENra3nsSDweOvWsFrU6JbDyki G3XUEoFmu0kBzu6hUbCSWfji6YAJbfaR+EnubD3o/zZcXwxrdBLhzg85dhKL3V9hYVtS ft2gDisBjyvA7K+5qG0WI9ToidSu/SeZvTZdkPtrNHWFW63QzI4JKy+tbQhxKYb4UQzI aGZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=CM1KudZ8; 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 i11-20020a17090332cb00b0017a0c17e47bsi4067249plr.78.2022.10.14.11.35.44; Fri, 14 Oct 2022 11:35:58 -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=CM1KudZ8; 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 S230036AbiJNSUZ (ORCPT + 99 others); Fri, 14 Oct 2022 14:20:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229747AbiJNSUX (ORCPT ); Fri, 14 Oct 2022 14:20:23 -0400 Received: from mail-il1-x136.google.com (mail-il1-x136.google.com [IPv6:2607:f8b0:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 765F232D93 for ; Fri, 14 Oct 2022 11:20:19 -0700 (PDT) Received: by mail-il1-x136.google.com with SMTP id d14so2948983ilf.2 for ; Fri, 14 Oct 2022 11:20:19 -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:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=hRrLIOmcMN6dxNUCyEtTWc83pVv8BngJLKL099x7jQ0=; b=CM1KudZ8ldL0cjM/0tzdsATxKwcOAqtoIJ+e5963ilVoRUTzCZe3X4olG0lO5gt6fn wLVdC2HMaXGL3DTX0vvPCWlzi5wRQqssXGSZ735mX/d9WkUxIJjoqoSZ9fF6vmRdBDGU JqUhIeNLn8+P7GOhD7AB5cu+CaThVVIQjV0J22mgJ3p4S75R0J+L8KRquTaxT9YeML21 mi8KgTuAEJmIMb/3rr/Ms1+FQq+4/c3r+2L5aVLp4GRqDrOvC0/n4qgd6RMacBmFh9sO KDq+GwXx4VsH6kgyUMklo6VsVcP+HVATpazb9pkE2RTXvhY5yq7BnvUd9mcUU6f0YhSY pM+g== 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:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=hRrLIOmcMN6dxNUCyEtTWc83pVv8BngJLKL099x7jQ0=; b=yaEP8TvXnsTMCRs2eBY6si58R7/47h1t9rJqbvG9j8TXUtt1VWXORnRbP0TNOOb/de QGTcNkmKr3eMJkILtdWksAMmh6kw0EqYDfPl9bzLA5XWZ0s/z7qvJPrZ8DDj/HOE/0Lj 9NRkCWxS1Xc24FOmlev0ac/ZsZRkcqaSFwwrSt5KaUmnpk4BZm7bjo65i/hIoRVe9csv aRaQhgBq4JOKe/6VKVlCaWzBN4l8n+ovxUL9Uu0/algInSBFNSP6DxkIWVZokicKEHUi TaGOEerTt+y1wJM5tcOnpaFvj5cJ070au1HaYofkmF1C0940LaEWJkOMziExtXRGMBUC j23w== X-Gm-Message-State: ACrzQf2SlDcPf56mkpyBK3OTxbyqeGQrkCDAlGYlrrNrmg9Zhdefr7jk qtuNy+MXX4Bj17BxjC2PqdOl4KD4tfphWuDawluyNw== X-Received: by 2002:a05:6e02:930:b0:2f9:9d1b:2525 with SMTP id o16-20020a056e02093000b002f99d1b2525mr3000381ilt.173.1665771618748; Fri, 14 Oct 2022 11:20:18 -0700 (PDT) MIME-Version: 1.0 From: Jann Horn Date: Fri, 14 Oct 2022 20:19:42 +0200 Message-ID: Subject: [BUG?] X86 arch_tlbbatch_flush() seems to be lacking mm_tlb_flush_nested() integration To: Andy Lutomirski , Linux-MM , Mel Gorman , Rik van Riel Cc: 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 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 [at this point, the TLB entry still exists] B: calls mmap(), which reuses the same area that was just unmapped B: tries to access the newly created VMA, but instead the access goes through the stale TLB entry A: shrink_folio_list() calls try_to_unmap_flush(), which removes the stale TLB entry The effect would be that after process B removes a mapping with munmap() and creates a new mapping in its place, it would still see data from the old mapping when trying to access the new mapping. Am I missing something that protects against this scenario? munmap() uses the mmu_gather infrastructure, which tries to protect against this kind of correctness bug with multiple racing TLB invalidations in tlb_finish_mmu() by blowing away the whole TLB whenever one TLB invalidation ends while another is still in progress (tested with mm_tlb_flush_nested(tlb->mm)). But mmu_gather doesn't seem to be aware of TLB flushes that are batched up in the arch_tlbbatch_flush() infrastructure, so that doesn't help here. I think it might be necessary to add a new global counter of pending arch_tlbbatch_flush() flushes, and query that in mm_tlb_flush_nested(), or something like that.