From patchwork Sat Oct 9 09:26:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rongwei Wang X-Patchwork-Id: 12547389 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 C06E2C433F5 for ; Sat, 9 Oct 2021 09:27:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 61CA660EE4 for ; Sat, 9 Oct 2021 09:27:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 61CA660EE4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 5C790940007; Sat, 9 Oct 2021 05:27:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5773D6B0073; Sat, 9 Oct 2021 05:27:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F092940007; Sat, 9 Oct 2021 05:27:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id 26DDF6B0072 for ; Sat, 9 Oct 2021 05:27:09 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id BCC358249980 for ; Sat, 9 Oct 2021 09:27:08 +0000 (UTC) X-FDA: 78676370136.22.AB13D02 Received: from out30-45.freemail.mail.aliyun.com (out30-45.freemail.mail.aliyun.com [115.124.30.45]) by imf20.hostedemail.com (Postfix) with ESMTP id D2F27D004BC7 for ; Sat, 9 Oct 2021 09:27:07 +0000 (UTC) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04400;MF=rongwei.wang@linux.alibaba.com;NM=1;PH=DS;RN=12;SR=0;TI=SMTPD_---0Ur5qOcb_1633771618; Received: from localhost.localdomain(mailfrom:rongwei.wang@linux.alibaba.com fp:SMTPD_---0Ur5qOcb_1633771618) by smtp.aliyun-inc.com(127.0.0.1); Sat, 09 Oct 2021 17:26:59 +0800 From: Rongwei Wang To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: akpm@linux-foundation.org, willy@infradead.org, viro@zeniv.linux.org.uk, song@kernel.org, william.kucharski@oracle.com, hughd@google.com, shy828301@gmail.com, linmiaohe@huawei.com, peterx@redhat.com Subject: [PATCH 0/3] mm, thp: introduce a new sysfs interface to facilitate file THP for .text Date: Sat, 9 Oct 2021 17:26:55 +0800 Message-Id: <20211009092658.59665-1-rongwei.wang@linux.alibaba.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=alibaba.com; spf=pass (imf20.hostedemail.com: domain of rongwei.wang@linux.alibaba.com designates 115.124.30.45 as permitted sender) smtp.mailfrom=rongwei.wang@linux.alibaba.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D2F27D004BC7 X-Stat-Signature: m44gcm9nskqqaz7yuw1qpx3j5ug5wjcs X-HE-Tag: 1633771627-906631 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: Hi, all Recently, our team focus on huge pages of executable binary files and share libraries, refer to these huge pages as 'hugetext' in the next description. The hugetext indeed to improve the performance of application, e.g. mysql. It has been shown in [1][2]. And with the increase of the text section, the improvement will be more obvious. Base on [1][2], we make some improvement to make file-backed THP more usability and easy for applications. In current kernel, ref[1] introduced READ_ONLY_THP_FOR_FS, and ref[2] add the support for share libraries based on the previous one. However, Until now, hugetext is not convenient to use at present. For example, we need to explicitly madvise MADV_HUGEPAGE for .text and set "transparent_hugepage/enabled" to always or madvise . On the other hand, hugetext requires 2M alignment of vma->vm_start and vma->vm_pgoff, which is not guaranteed by kernel or loader. Our design: To solve the drawback mentioned above of file THP in using, we have mainly improved two points that shows below. (1) introduce a new sysfs interface "transparent_hugepage/hugetext_enabled" in order to automatically (i.e., transparently) enable file THP for suitable .text vmas. The usage belows: to disable hugetext: $ echo 0 > /sys/kernel/mm/transparent_hugepage/hugetext_enabled to enable hugetext: $ echo 1 > /sys/kernel/mm/transparent_hugepage/hugetext_enabled to enable or disable in boot options: hugetext=1 or hugetext=0 Q: Why not add a new option, e.g., "text_always", in addition to "always", "madvise", and "never" to "transparent_hugepage/enabled" ? A: A new option to "transparent_hugepage/enabled" cannot handle such scenario, where THP always for .text, and madivse/never for others (e.g., anon vma). The .text is usually small in size. In our production environment, at most 10G out of 500G total memory is used as .text. The .text is also performance critical. More important, We don't want to change the user's default behavior too much. So we think that a new independent sysfs interface for file THP is worthy. (2) make vm_start of .text 2M align with vm_pgoff, especially for PIE/PIC binaries and shared libraries. For binaries that are compiled with '--pie -fPIC' and with LOAD alignment smaller than 2M (typically 4K, 64K), change maximum_alignment to 2M. For shared libraries, ld.so seems not to consider p_align well, as shown below. $ readelf -l /usr/lib64/libc-2.17.so LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 0x00000000001c2fe8 0x00000000001c2fe8 R E 200000 $ cat /proc/1/smaps 7fecc4072000-7fecc4235000 r-xp 00000000 08:03 655802 /usr/lib64/libc-2.17.so Finally, why this feasure is implemented in kernel, not in userspace, or ld.so? Userspace methods like libhugetlbfs have various disadvantages: * require recompiling applications; * the anonymous mapping cannot be shared; * debugging is not convenient. To madvise MADV_HUGEPAGE for .text in ld.so has been suggested in the glibc mailing list[3], but there was no response. Finally, considering that this feature requires very little code and is not too difficult to implement based on the existing file-backed THP support, it was finally chosen to be implemented in the kernel. Thanks! Reference: [1] https://patchwork.kernel.org/project/linux-mm/cover/20190801184244.3169074-1-songliubraving@fb.com/ [2] https://patchwork.kernel.org/project/linux-fsdevel/patch/20210406000930.3455850-1-cfijalkovich@google.com/ [3] https://sourceware.org/pipermail/libc-alpha/2021-February/122334.html Rongwei Wang (3): mm, thp: support binaries transparent use of file THP mm, thp: make mapping address of libraries THP align mm, thp: make mapping address of PIC binaries THP align fs/binfmt_elf.c | 5 +++ include/linux/huge_mm.h | 36 +++++++++++++++++++ include/linux/khugepaged.h | 9 +++++ mm/Kconfig | 11 ++++++ mm/huge_memory.c | 72 ++++++++++++++++++++++++++++++++++++++ mm/khugepaged.c | 4 +++ mm/memory.c | 12 +++++++ mm/mmap.c | 18 ++++++++++ 8 files changed, 167 insertions(+)