From patchwork Thu Oct 21 12:21:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Amit X-Patchwork-Id: 12576359 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 088EEC433EF for ; Thu, 21 Oct 2021 19:52:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8BB5D61371 for ; Thu, 21 Oct 2021 19:52:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 8BB5D61371 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id BC087900002; Thu, 21 Oct 2021 15:52:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6F7C6B0071; Thu, 21 Oct 2021 15:52:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5EEA900002; Thu, 21 Oct 2021 15:52:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0126.hostedemail.com [216.40.44.126]) by kanga.kvack.org (Postfix) with ESMTP id 943BD6B006C for ; Thu, 21 Oct 2021 15:52:48 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 459BE183380BF for ; Thu, 21 Oct 2021 19:52:48 +0000 (UTC) X-FDA: 78721492416.11.0B9D2E6 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) by imf12.hostedemail.com (Postfix) with ESMTP id EF21A10000BB for ; Thu, 21 Oct 2021 19:52:47 +0000 (UTC) Received: by mail-pj1-f51.google.com with SMTP id ls18so1288900pjb.3 for ; Thu, 21 Oct 2021 12:52:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=TR1T6zOrAlzQbQbzWioCnGERHhFMAtdNyRA0+55dPpg=; b=Lv4rh9Fwt7a1dDy1pd5bz6+KAW/2dFR/tGUjA0sYoP5Tiy1n1iFPnjWYJzxYej3LkV 8jjrE6Q0v/9QlaaW+I8nn9KEaZwHnF+SmrC+MLEkXXRBwwbcBNrqNlRZZIsLq2gHXSIl n1Ud8+FsW4wfFg5HQRjwwP7cwQfPhUJuEIgG9XbbQotEhZQIV+05Zrz9TEek3nZfQNZC iIghwaEkThKQlPgPZd8oALawLB6BH62vDdawAQewuSwEg+1DJfHnC6Vlq0ch+btat6T/ ooxffoJdWMLe3q277QEeQhAe0GN6HjirBtTUtbcDA6ZLMHd15iz4wCJGi/pFUpPGMMPa VNuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=TR1T6zOrAlzQbQbzWioCnGERHhFMAtdNyRA0+55dPpg=; b=0XtCuR3/pGLdwPjnhzafoOXGozMBAQvy2QMykm6r1DASxA0zvZEBKzlfcqsqiqZAm6 6njmtWXqCgflYCcV5kqwelX/Op9jqbKz/9NgGPp9CjcizE+Tbh+rIWMNA7qbNXHbx5cL U09uDmjk58wsyEd5rUhpOOfsH8sjl98vA0g/q4VzOqsKnjgo5WTNDUNccKWHMT51AF6p 5O4enyVsIH8xizqFGZuVuzBAli6i/E0IIsZWKsBXgFtw36f6xU1tZ3i6HvsLZ3HB8ZIm 9sIONVKA2MgxWioCqBoBV0jJjSPaWvO9EvPIrYudvgIIyDTc6XQqh2yG22tsfKLz+zPy Q07Q== X-Gm-Message-State: AOAM5326Kpvs7hCeCNYHlGM3C6zvQgUt6uK3F7B3FdB0r4h0QlZ7oi0T 7oFq69KeFYJsGwZldG2Z6i5aPyb5QJw= X-Google-Smtp-Source: ABdhPJx7eKqqB+m2EpgtEJGAakQ0CuFFpBpcqmZQWX3IiNBBr1ZBR2aHZiUgLlWDTG0/gYBxUJwFhw== X-Received: by 2002:a17:90b:1242:: with SMTP id gx2mr9168584pjb.152.1634845966177; Thu, 21 Oct 2021 12:52:46 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id n202sm7098078pfd.160.2021.10.21.12.52.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 12:52:45 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Nadav Amit , Andi Kleen , Andrea Arcangeli , Andrew Cooper , Andrew Morton , Andy Lutomirski , Dave Hansen , Peter Xu , Peter Zijlstra , Thomas Gleixner , Will Deacon , Yu Zhao , Nick Piggin , x86@kernel.org Subject: [PATCH v2 0/5] mm/mprotect: avoid unnecessary TLB flushes Date: Thu, 21 Oct 2021 05:21:07 -0700 Message-Id: <20211021122112.592634-1-namit@vmware.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Stat-Signature: 7eca5a4w3c4ojnhiaa9app4atd5mhhgb X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: EF21A10000BB Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=Lv4rh9Fw; dmarc=pass (policy=none) header.from=gmail.com; spf=none (imf12.hostedemail.com: domain of mail-pj1-f51.google.com has no SPF policy when checking 209.85.216.51) smtp.helo=mail-pj1-f51.google.com X-HE-Tag: 1634845967-814281 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: From: Nadav Amit This patch-set is intended to remove unnecessary TLB flushes. It is based on feedback from v1 and several bugs I found in v1 myself. Basically, there are 3 optimizations in this patch-set: 1. Avoiding TLB flushes on change_huge_pmd() that are only needed to prevent the A/D bits from changing. 2. Use TLB batching infrastructure to batch flushes across VMAs and do better/fewer flushes. 3. Avoid TLB flushes on permission demotion. Andrea asked for the aforementioned (2) to come after (3), but this is not simple (specifically since change_prot_numa() needs the number of pages affected). There are many changes from v1 to v2 so consider the change log as partial. v1->v2: * Wrong detection of permission demotion [Andrea] * Better comments [Andrea] * Handle THP [Andrea] * Batching across VMAs [Peter Xu] * Avoid open-coding PTE analysis * Fix wrong use of the mmu_gather() Cc: Andi Kleen Cc: Andrea Arcangeli Cc: Andrew Cooper Cc: Andrew Morton Cc: Andy Lutomirski Cc: Dave Hansen Cc: Peter Xu Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: Will Deacon Cc: Yu Zhao Cc: Nick Piggin Cc: x86@kernel.org Nadav Amit (5): x86: Detection of Knights Landing A/D leak mm: avoid unnecessary flush on change_huge_pmd() x86/mm: check exec permissions on fault mm/mprotect: use mmu_gather mm/mprotect: do not flush on permission promotion arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/pgtable.h | 8 +++ arch/x86/include/asm/pgtable_types.h | 2 + arch/x86/include/asm/tlbflush.h | 80 +++++++++++++++++++++++ arch/x86/kernel/cpu/intel.c | 5 ++ arch/x86/mm/fault.c | 11 +++- fs/exec.c | 6 +- include/asm-generic/tlb.h | 14 +++++ include/linux/huge_mm.h | 5 +- include/linux/mm.h | 5 +- include/linux/pgtable.h | 5 ++ mm/huge_memory.c | 22 ++++--- mm/mprotect.c | 94 +++++++++++++++------------- mm/pgtable-generic.c | 8 +++ mm/userfaultfd.c | 6 +- 15 files changed, 215 insertions(+), 57 deletions(-)