Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2051154pxu; Tue, 24 Nov 2020 15:56:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJx2Y7JiWODnHGgXUStRBX9W+EYbK6/Ji/GfdXJmcSXIf/TwG4/UI2nlEiQ9l0azxjTFORa9 X-Received: by 2002:a17:906:f8cd:: with SMTP id lh13mr773097ejb.551.1606262175076; Tue, 24 Nov 2020 15:56:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606262175; cv=none; d=google.com; s=arc-20160816; b=mulXGBFVpCRs+zh5PTTVv8khW3s0M10B399gcWk/ekBV9OgrB9TnUuvKJr//Dfmawh We7ro2R4nd4nLuNyPRaKSTSWncHHWaJ4FWUPzjOYeZiVWTxDm2tw9X4vVkbwAXcH6CjO 5Zl1WZKYH6FZ121GHxp6jlEEz2oYS9/6n897nABn4wATVD3UlK9QZomtsrvJ513v0AZr eLhSZtg5/O47kqqvsAhZLlbN7C+5J+APevIzJ60TDiSEqm/IXYvnJQcC1jHUnJv1l2Q4 CSziUhzCcwtvNkV9pMczjpg4/Tnr37+TnBkDiaHR3Egq9tsNK5IHuC7Gwwz943RCFqmt FWQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=BJdQ1AkYS9c6sycb/KEWwFyD23B6TTw5nJN9Smm4bmE=; b=amcazR8ka66yRdrxTmYpUFC5ucrdGVBZC8IdVX7FJEfdYns1c6PrMLtvZpVcPFb9Ne p1kEeEpiYBL7apPyhQYGd1LWqt/c1Aq22sUBH7AJ92mH74pGMNR2wLzYajwpbMrdntO4 yrFKJ0AlvpmvDFKXQMxOJPnpjwXMiQh8dkil8rgZgNr15thdwepcTHmsIxLsVWMaRO/I 0Z9tfMTQN/r+SzPWErPSkgRvLY0ddT1oZhsB+HLrSHXrKGbIs5/qpkEDPuJ4t/M49wtq KiIWVgiQcmwQfDbBoyp3bwG62yP5VwWUSnHCjgTDtyoUr/PsQjEaU7ZbrYqvIlFdVgQW q20Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f15si137946edj.239.2020.11.24.15.55.52; Tue, 24 Nov 2020 15:56:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728304AbgKXTtm (ORCPT + 99 others); Tue, 24 Nov 2020 14:49:42 -0500 Received: from shelob.surriel.com ([96.67.55.147]:36726 "EHLO shelob.surriel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727849AbgKXTtk (ORCPT ); Tue, 24 Nov 2020 14:49:40 -0500 Received: from imladris.surriel.com ([96.67.55.152]) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94) (envelope-from ) id 1kheJc-0006wM-4h; Tue, 24 Nov 2020 14:49:28 -0500 From: Rik van Riel To: hughd@google.com Cc: xuyu@linux.alibaba.com, akpm@linux-foundation.org, mgorman@suse.de, aarcange@redhat.com, willy@infradead.org, linux-kernel@vger.kernel.org, kernel-team@fb.com, linux-mm@kvack.org, vbabka@suse.cz, mhocko@suse.com, Rik van Riel Subject: [PATCH 3/3] mm,thp,shmem: make khugepaged obey tmpfs mount flags Date: Tue, 24 Nov 2020 14:49:25 -0500 Message-Id: <20201124194925.623931-4-riel@surriel.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20201124194925.623931-1-riel@surriel.com> References: <20201124194925.623931-1-riel@surriel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: riel@shelob.surriel.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently if thp enabled=[madvise], mounting a tmpfs filesystem with huge=always and mmapping files from that tmpfs does not result in khugepaged collapsing those mappings, despite the mount flag indicating that it should. Fix that by breaking up the blocks of tests in hugepage_vma_check a little bit, and testing things in the correct order. Signed-off-by: Rik van Riel Fixes: c2231020ea7b ("mm: thp: register mm for khugepaged when merging vma for shmem") --- include/linux/khugepaged.h | 2 ++ mm/khugepaged.c | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/linux/khugepaged.h b/include/linux/khugepaged.h index c941b7377321..2fcc01891b47 100644 --- a/include/linux/khugepaged.h +++ b/include/linux/khugepaged.h @@ -3,6 +3,7 @@ #define _LINUX_KHUGEPAGED_H #include /* MMF_VM_HUGEPAGE */ +#include #ifdef CONFIG_TRANSPARENT_HUGEPAGE @@ -57,6 +58,7 @@ static inline int khugepaged_enter(struct vm_area_struct *vma, { if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags)) if ((khugepaged_always() || + (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) || (khugepaged_req_madv() && (vm_flags & VM_HUGEPAGE))) && !(vm_flags & VM_NOHUGEPAGE) && !test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 4e3dff13eb70..abab394c4206 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -440,18 +440,28 @@ static inline int khugepaged_test_exit(struct mm_struct *mm) static bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags) { - if ((!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) || - (vm_flags & VM_NOHUGEPAGE) || + /* Explicitly disabled through madvise. */ + if ((vm_flags & VM_NOHUGEPAGE) || test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) return false; - if (shmem_file(vma->vm_file) || - (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && - vma->vm_file && - (vm_flags & VM_DENYWRITE))) { + /* Enabled via shmem mount options or sysfs settings. */ + if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) { return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff, HPAGE_PMD_NR); } + + /* THP settings require madvise. */ + if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) + return false; + + /* Read-only file mappings need to be aligned for THP to work. */ + if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file && + (vm_flags & VM_DENYWRITE)) { + return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff, + HPAGE_PMD_NR); + } + if (!vma->anon_vma || vma->vm_ops) return false; if (vma_is_temporary_stack(vma)) -- 2.25.4