From patchwork Mon Nov 13 09:05:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xu Yu X-Patchwork-Id: 13453716 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1C48C4332F for ; Mon, 13 Nov 2023 09:06:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7425C6B01F3; Mon, 13 Nov 2023 04:06:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6CBC26B01F7; Mon, 13 Nov 2023 04:06:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56C2C6B01F8; Mon, 13 Nov 2023 04:06:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 46ED76B01F3 for ; Mon, 13 Nov 2023 04:06:13 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0F54AC072E for ; Mon, 13 Nov 2023 09:06:13 +0000 (UTC) X-FDA: 81452349426.03.2E5B114 Received: from out30-101.freemail.mail.aliyun.com (out30-101.freemail.mail.aliyun.com [115.124.30.101]) by imf14.hostedemail.com (Postfix) with ESMTP id 98E0E10000E for ; Mon, 13 Nov 2023 09:06:09 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of xuyu@linux.alibaba.com designates 115.124.30.101 as permitted sender) smtp.mailfrom=xuyu@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699866371; a=rsa-sha256; cv=none; b=n90t4a2BvfoJhuCpvl8hNW1VgueMD0JXxiKVPQLoHc6a3dL93aj6YutDK4G1R69vhzqBFY WqRl4s3GGWfP17/imhUFEvSU2wzuS6g+rK8mGiDJm9znMg01GKm10FlFGHiR+Hu58AEReF AJb6sjIcvI9SoEO+2iEoc9f0ED5Mu2g= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of xuyu@linux.alibaba.com designates 115.124.30.101 as permitted sender) smtp.mailfrom=xuyu@linux.alibaba.com; dmarc=pass (policy=none) header.from=alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699866371; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=GimYDYrS6WrMn1KxSw2ExukLGCXN3arJJsf6TCNUNmY=; b=fum3zzu5IfGvuUMZhT21t3KqfbsgI6iHhxf9U7BTKyNG1B1WOuClYWY+HY85j+NqmJALiy CtGM9+hpdjvL6Ok1PmNCAohyVWy8bJh/UprFOO0PTZR8y2bDUs8lUtdHuAAEvv36I5ox4y hZHGltglyMLKReGVdyXIJTv/REGp+Uw= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R281e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046060;MF=xuyu@linux.alibaba.com;NM=1;PH=DS;RN=5;SR=0;TI=SMTPD_---0VwGg171_1699866363; Received: from localhost(mailfrom:xuyu@linux.alibaba.com fp:SMTPD_---0VwGg171_1699866363) by smtp.aliyun-inc.com; Mon, 13 Nov 2023 17:06:04 +0800 From: Xu Yu To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, zokeefe@google.com, song@kernel.org, shy828301@gmail.com Subject: [PATCH 0/1] mm/khugepaged: map anonymous pte-mapped THPs by pmds Date: Mon, 13 Nov 2023 17:05:57 +0800 Message-Id: X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 98E0E10000E X-Stat-Signature: fc6qmb8cfn3ijxu96kwub4oxmiw9xua4 X-HE-Tag: 1699866369-618512 X-HE-Meta: U2FsdGVkX1+bIn6o7bJDoTaD5+SXRpfne+MRRuRVGgH9ksAITVLeWUxq01W8ZN2dQYyMVfDAnCeTHu9CY6fFCSshqhRi6TnbyQ2RzU1H2a1bGhugO2SGzQ26FvzwGJb79v4I5dunxVIsdLcxSd4A2atVtn/H8JzAg3tOBUT+Z3X1i/fD3j1cFZrcl+Cy2Lr8rX5Fh4xEc/iGAJSEzVBMMPNyfLvy3zK8zzKNQ2DlWOVlCxIwmd0T8o7ccOWULdXLjgn2YhVf9JwuR0xU28XpcBri+qzZV9/jmuTHv6bGm6zwYi5OtgunxAZ7ZHc403L94MsGN28TGjFZb1tSix+p0Dpo8UFaEr63QQxCCjLceVugrh6p3Y9AdtMQ1mFUL3WXHknXP2nyfnjKC1iqWCGgWDhjKglntK9iKzTEOCYMjgBZBhHkOXcYTEvAGhJExIyiMVyermVipv+iUidjqtQdM5jqnCZbNnMTmU66622ESWbActSFC3xjs9ZBzP7mFeQPOV8HG/7rmS8/BG8lKh2GKt/0HweTEjDp/XvQVLEI0E1o5SGjwalFFYAIT1qrNH0DIaY1BIfkgpNJ4ycIrC5qTBdU1ulUAQSUPXoeYBOfn9jdMOx4BS0x++WHa1jW485ke6W5jWI+nP22k+ooTD13OpYSl5hv00IZOhWwVK6RJ2UCjgOpu9+wEy3XFds+R7sSLIbWv+Nui9WifucqkdUedD+CStntEJZgCLfpfSnrEPsin/wT9c94IoIFe2h3Hf96QMMXFIVzDmwYz5Wv12By3nxpNKXN+PUUfUhBtdiIuV5ye5NEjSwVXAg2Y9bf13InjYoYNrhlvCCoR+Ei7HsJv5Dt8zbR47JKkXcXkBpXzyej49t0+PQ8TX4ZtIO1HXNunI9v9OIOoaMICuoqKzlD4MFXZd0YGd7Ai6VlViptlexkkagfmHYbY2d6qx8bSGbbn6rDrK1g24SGY6Wa4cM 8Z4PyCMH B0bdn2kByv9QhVwdaaEvtBteVvK0DJaVCIPKfzPPfX8pV6upYWeO7+Dom8Q== 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: List-Subscribe: List-Unsubscribe: I write a testcase[1] to demonstrate this scenario. vanilla kernel (v6.6): # ./test [mmap rw ] vaddr: 0x0x7f2000000000, pfn: 0x110000, is_thp: 1, is_thp_mapped: 1 [mprotect ro] vaddr: 0x0x7f2000000000, pfn: 0x110000, is_thp: 1, is_thp_mapped: 0 ^^ __split_huge_pmd() is called in change_protection() [mprotect rw] vaddr: 0x0x7f2000000000, pfn: 0x110000, is_thp: 1, is_thp_mapped: 0 ^^ vma_merge() [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x110000, is_thp: 1, is_thp_mapped: 0 [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x110000, is_thp: 1, is_thp_mapped: 0 [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x117a00, is_thp: 1, is_thp_mapped: 1 ^^ hpage_collapse_scan_pmd ^^^^^^^^ new hugepage is allocated [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x117a00, is_thp: 1, is_thp_mapped: 1 patched kernel: # ./test [mmap rw ] vaddr: 0x0x7f2000000000, pfn: 0x128400, is_thp: 1, is_thp_mapped: 1 [mprotect ro] vaddr: 0x0x7f2000000000, pfn: 0x128400, is_thp: 1, is_thp_mapped: 0 [mprotect rw] vaddr: 0x0x7f2000000000, pfn: 0x128400, is_thp: 1, is_thp_mapped: 0 [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x128400, is_thp: 1, is_thp_mapped: 0 [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x128400, is_thp: 1, is_thp_mapped: 0 [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x128400, is_thp: 1, is_thp_mapped: 1 ^^ hpage_collapse_scan_pmd ^^^^^^^^ old hugepage [khugepaged ] vaddr: 0x0x7f2000000000, pfn: 0x128400, is_thp: 1, is_thp_mapped: 1 [1] testcase source code #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #define PAGE_SHIFT 12 #define SIZE_2M (2<<20) #define ADDRESS 0x7f2000000000 #define PAGEMAP_PRESENT 63 #define PAGEMAP_PFN_MASK 0x007FFFFFFFFFFFFFUL /* bits 54:0 */ #define KPF_SIZE 8 #define KPF_THP 22 static bool is_thp(unsigned long pfn) { int kpageflags_fd; unsigned long flag; int ret; kpageflags_fd = open("/proc/kpageflags", O_RDONLY); ret = pread(kpageflags_fd, &flag, KPF_SIZE, KPF_SIZE * pfn); close(kpageflags_fd); if (ret != KPF_SIZE) return false; return flag & (1 << KPF_THP); } static bool is_thp_mapped(void *addr) { char path[64], line[128], pattern[64]; int pid; FILE *fp; int anon_huge_pages = 0; snprintf(pattern, 64, "%lx", (unsigned long)addr); pid = getpid(); snprintf(path, 64, "/proc/%u/smaps", pid); fp = fopen(path, "r"); while (fgets(line, 64, fp) != NULL) { if (strstr(line, pattern)) break; } while (fgets(line, 64, fp) != NULL) { if (1 == sscanf(line, "AnonHugePages: %d kB", &anon_huge_pages)) break; } fclose(fp); return anon_huge_pages != 0; } static unsigned long get_pfn(unsigned long vaddr) { char path[64]; int pid, pagemap_fd; off64_t offset; unsigned long pfn = 0; int ret; offset = (vaddr >> PAGE_SHIFT) * sizeof(unsigned long); pid = getpid(); snprintf(path, 64, "/proc/%u/pagemap", pid); pagemap_fd = open(path, O_RDONLY); lseek64(pagemap_fd, offset, SEEK_SET); ret = read(pagemap_fd, &pfn, sizeof(pfn)); close(pagemap_fd); if (ret < 0) return -1; if (!(pfn & (1UL << PAGEMAP_PRESENT))) return 0; return (pfn & PAGEMAP_PFN_MASK); } static void show_addr_info(void *addr, char *msg) { unsigned long pfn; pfn = get_pfn((unsigned long)addr); printf("[%s] vaddr: 0x%p, pfn: 0x%lx, is_thp: %d, is_thp_mapped: %d\n", msg, addr, pfn, is_thp(pfn), is_thp_mapped(addr)); } int main(int argc, char *argv[]) { void *addr; addr = mmap((void *)ADDRESS, SIZE_2M, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, -1, 0); if (addr == MAP_FAILED) { perror("mmap"); return 1; } show_addr_info(addr, "mmap rw "); mprotect(addr, SIZE_2M / 2, PROT_READ); show_addr_info(addr, "mprotect ro"); mprotect(addr, SIZE_2M / 2, PROT_READ | PROT_WRITE); show_addr_info(addr, "mprotect rw"); while (1) { show_addr_info(addr, "khugepaged "); sleep(5); } return 0; } Xu Yu (1): mm/khugepaged: map anonymous pte-mapped THPs by pmds mm/khugepaged.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 180 insertions(+), 7 deletions(-)