From patchwork Wed Sep 22 17:51:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12511167 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E37F2C433EF for ; Wed, 22 Sep 2021 17:52:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7D0E760527 for ; Wed, 22 Sep 2021 17:52:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7D0E760527 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 030DC940009; Wed, 22 Sep 2021 13:52:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F23326B008A; Wed, 22 Sep 2021 13:52:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9BAA940009; Wed, 22 Sep 2021 13:52:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0161.hostedemail.com [216.40.44.161]) by kanga.kvack.org (Postfix) with ESMTP id C6FD96B0089 for ; Wed, 22 Sep 2021 13:52:03 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 703A38249980 for ; Wed, 22 Sep 2021 17:52:03 +0000 (UTC) X-FDA: 78615952926.12.2204325 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 225249000258 for ; Wed, 22 Sep 2021 17:52:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632333122; h=from:from: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:content-transfer-encoding; bh=vKKXIJLwy/fLR00ynbhI+6m3Ml8OSdkI8ATKMZTi12I=; b=dgDkRMwaYFHBMKbciSrUmj2pBSWEqMsV9Fbkqeo4IA35yDR63uZ87a2FYWDd4TMlixRLpf f1uMaJiE9gbdty0HsfLpUbBdkx2Z/gkS3okx+5WEbBa8oElAT3DrNoDwDcd06nTq+DiaY4 iQ7i8h6bple084LyFr4wGbCsWCNW5pU= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-503-6cXCM1bCOKGpojAGp6KpoQ-1; Wed, 22 Sep 2021 13:52:01 -0400 X-MC-Unique: 6cXCM1bCOKGpojAGp6KpoQ-1 Received: by mail-qv1-f69.google.com with SMTP id h16-20020a05621402f000b0037cc26a5659so14364232qvu.1 for ; Wed, 22 Sep 2021 10:52:01 -0700 (PDT) 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=vKKXIJLwy/fLR00ynbhI+6m3Ml8OSdkI8ATKMZTi12I=; b=HUPCGXqai+7mu/B2FeFffTeSc7s9nq+voQUR+EWacHyn0rRqnozr4yJRK9bj6amo2H zApDtZO5esUC9b+wPaCq3nHaQu02fT50Wnz8wS7Div3J3I1FkTePPMBjYvAoYV4w9j2T kScgNMnIzj5I0zJMzQrI3TVh3+JF98dhjBN9R1OX0DFL4hedU8SvHFwHfwim+p2KgRVf R+Of6B9lfMRtF7BdyHYE3F9LQx98mX9wlTdpHNeV7mEkkH5IbwZJYDK8mp5A0gBYKF7H Ktllarw6/7z+C2G86Crr/509bGO9LsgYClg0N9MIcHGkVGRuqAasm6xHYXSP2V40y7zx 5yBA== X-Gm-Message-State: AOAM533yyAvy1yAyK7eC+FlYLqjWaEtZs5+CzywEtoa/bVsiyxJxU6F/ eWtCDrGrkOGy/9/Rcp+3iDRGA7eWv3S0CLcyQ2gVXbcFZ/UD16emnZEQpj2TX0LoWlJbAb+bztU xtOjpUeg3clM= X-Received: by 2002:aed:2794:: with SMTP id a20mr490808qtd.243.1632333120140; Wed, 22 Sep 2021 10:52:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdxDXZlgkBgU1qcW7DZdL7V4D2gO7rktK27Bs2jpfo95oSFVNULk2DyaNxUBOonyDhSFOKlQ== X-Received: by 2002:aed:2794:: with SMTP id a20mr490781qtd.243.1632333119878; Wed, 22 Sep 2021 10:51:59 -0700 (PDT) Received: from t490s.redhat.com ([2607:fea8:56a2:9100::d3ec]) by smtp.gmail.com with ESMTPSA id r17sm1854075qtx.17.2021.09.22.10.51.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Sep 2021 10:51:59 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Andrew Morton , peterx@redhat.com, Andrea Arcangeli , Axel Rasmussen , Hugh Dickins , Nadav Amit Subject: [PATCH] mm/khugepaged: Detecting uffd-wp vma more efficiently Date: Wed, 22 Sep 2021 13:51:56 -0400 Message-Id: <20210922175156.130228-1-peterx@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 225249000258 X-Stat-Signature: wobfwwnsoy8uos8tig5zayn3sh8dszo8 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=dgDkRMwa; spf=none (imf29.hostedemail.com: domain of peterx@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1632333122-843624 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: We forbid merging thps for uffd-wp enabled regions, by breaking the khugepaged scanning right after we detected a uffd-wp armed pte (either present, or swap). It works, but it's less efficient, because those ptes only exist for VM_UFFD_WP enabled VMAs. Checking against the vma flag would be more efficient, and good enough. To be explicit, we could still be able to merge some thps for VM_UFFD_WP regions before this patch as long as they have zero uffd-wp armed ptes, however that's not a major target for thp collapse anyways. This mostly reverts commit e1e267c7928fe387e5e1cffeafb0de2d0473663a, but instead we do the same check at vma level, so it's not a bugfix. This also paves the way for file-backed uffd-wp support, as the VM_UFFD_WP flag will work for file-backed too. After this patch, the error for khugepaged for these regions will switch from SCAN_PTE_UFFD_WP to SCAN_VMA_CHECK. Since uffd minor mode should not allow thp as well, do the same thing for minor mode to stop early on trying to collapse pages in khugepaged. Cc: Andrea Arcangeli Cc: Axel Rasmussen Cc: Hugh Dickins Cc: Nadav Amit Signed-off-by: Peter Xu --- Axel: as I asked in the other thread, please help check whether minor mode will work properly with shmem thp enabled. If not, I feel like this patch could be part of that effort at last, but it's also possible that I missed something. Signed-off-by: Peter Xu --- include/trace/events/huge_memory.h | 1 - mm/khugepaged.c | 26 +++----------------------- 2 files changed, 3 insertions(+), 24 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h index 4fdb14a81108..53532f5925c3 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -15,7 +15,6 @@ EM( SCAN_EXCEED_SWAP_PTE, "exceed_swap_pte") \ EM( SCAN_EXCEED_SHARED_PTE, "exceed_shared_pte") \ EM( SCAN_PTE_NON_PRESENT, "pte_non_present") \ - EM( SCAN_PTE_UFFD_WP, "pte_uffd_wp") \ EM( SCAN_PAGE_RO, "no_writable_page") \ EM( SCAN_LACK_REFERENCED_PAGE, "lack_referenced_page") \ EM( SCAN_PAGE_NULL, "page_null") \ diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 045cc579f724..3afe66d48db0 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -31,7 +31,6 @@ enum scan_result { SCAN_EXCEED_SWAP_PTE, SCAN_EXCEED_SHARED_PTE, SCAN_PTE_NON_PRESENT, - SCAN_PTE_UFFD_WP, SCAN_PAGE_RO, SCAN_LACK_REFERENCED_PAGE, SCAN_PAGE_NULL, @@ -467,6 +466,9 @@ static bool hugepage_vma_check(struct vm_area_struct *vma, return false; if (vma_is_temporary_stack(vma)) return false; + /* Don't allow thp merging for wp/minor enabled uffd regions */ + if (userfaultfd_wp(vma) || userfaultfd_minor(vma)) + return false; return !(vm_flags & VM_NO_KHUGEPAGED); } @@ -1246,15 +1248,6 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, pte_t pteval = *_pte; if (is_swap_pte(pteval)) { if (++unmapped <= khugepaged_max_ptes_swap) { - /* - * Always be strict with uffd-wp - * enabled swap entries. Please see - * comment below for pte_uffd_wp(). - */ - if (pte_swp_uffd_wp(pteval)) { - result = SCAN_PTE_UFFD_WP; - goto out_unmap; - } continue; } else { result = SCAN_EXCEED_SWAP_PTE; @@ -1270,19 +1263,6 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, goto out_unmap; } } - if (pte_uffd_wp(pteval)) { - /* - * Don't collapse the page if any of the small - * PTEs are armed with uffd write protection. - * Here we can also mark the new huge pmd as - * write protected if any of the small ones is - * marked but that could bring unknown - * userfault messages that falls outside of - * the registered range. So, just be simple. - */ - result = SCAN_PTE_UFFD_WP; - goto out_unmap; - } if (pte_write(pteval)) writable = true;