From mboxrd@z Thu Jan 1 00:00:00 1970 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=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 9F5CAC433EF for ; Fri, 24 Sep 2021 05:11:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EDE3C6105A for ; Fri, 24 Sep 2021 05:11:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org EDE3C6105A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id D5D9B6B006C; Fri, 24 Sep 2021 01:11:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D0C7C6B0071; Fri, 24 Sep 2021 01:11:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFBCC900002; Fri, 24 Sep 2021 01:11:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0227.hostedemail.com [216.40.44.227]) by kanga.kvack.org (Postfix) with ESMTP id B05386B006C for ; Fri, 24 Sep 2021 01:11:06 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5F0453207B for ; Fri, 24 Sep 2021 05:11:06 +0000 (UTC) X-FDA: 78621292932.01.2D540B0 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf30.hostedemail.com (Postfix) with ESMTP id 4E971E001982 for ; Fri, 24 Sep 2021 05:11:04 +0000 (UTC) Received: by mail-pg1-f178.google.com with SMTP id x191so1844829pgd.9 for ; Thu, 23 Sep 2021 22:11:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vzqjuklikXd1yyaVBqex7jIXr6EPFUdZGObWj4aWYco=; b=L2Ksg0xpwm7l/+7qy95yUW5KQa6egIJ835QqbCQwPK44M2VneUTbSROw2muZDAUFOE 2pe3J5MHRCwW+5mNJ1Tp8f3gbivSj+QbsDecWg/nNNL2dju1+nmgs4g3FCAjBDmyu/bM oyR7rgqoYrOhq6m3ZmblUnutCnKWzQXfvK596tkZkwXacJnL+k6y5/5wMFoO4pN/iSHT 382yS1h2BLTtjah+AH7hWyL+Rz2DJmsTfuirtyD2l4m0sN0TmRqwWRJcVEbof8dcSuEr IWLCW3hN5dv/9TgOJWtJU9LS7jpdmPwgv3XAnCF4rzhGDp56c87qyclv9XZ9+kc6UWPJ tQ8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vzqjuklikXd1yyaVBqex7jIXr6EPFUdZGObWj4aWYco=; b=4U297tRkA68LzvoJ37qhxVWji//ypnTctIToNiNpAPAgl658XNJBDPoxYMQVZhRlAp cY5id2hjMtDTTgLPQt2k5WzqhMT+yeLk5Os1bzaLbiUOYjy0HAh9m6jS2U3uz0OvM+to kG5nIUhAOxh+PG8mnu3ZbPb9sjybiKL+ezOOWacZ5ASZrim0+WXh4zp//seJcwsAiKIG E5wl1+ePFReXc7HjPgQISiGBjmvNKZbryPET0w13eGD1A+4/rCCK2aRjcLFPM85ILRkt R26xJTHh9yakP3Ckq0jomieF+PyjX0oTYzyX7LrFMEn1koZGp4K7y1LXVcB2Gx968vCk oWoQ== X-Gm-Message-State: AOAM5310IVH1OP77fGNWf1j48Jc2XfCN9fyUGpWNKRcemmZ8ro/JOQWt Qiq7WOF+Nj+3Lcx68uwjJyiBliU/K43jVgmh95h7IA== X-Google-Smtp-Source: ABdhPJzkE+tv3MCnLS/iXDLKnueQlx+9lpsTOIPRPuRi3tYaTN1v5cB+yOMHogKiycnI+FMp7WpvlQFLSUjypaWP65Y= X-Received: by 2002:a63:1a64:: with SMTP id a36mr2164845pgm.225.1632460263042; Thu, 23 Sep 2021 22:11:03 -0700 (PDT) MIME-Version: 1.0 References: <20210922102411.34494-1-songmuchun@bytedance.com> <20210922102411.34494-5-songmuchun@bytedance.com> In-Reply-To: From: Muchun Song Date: Fri, 24 Sep 2021 13:10:24 +0800 Message-ID: Subject: Re: [PATCH v3 4/4] selftests: vm: add a hugetlb test case To: Barry Song <21cnbao@gmail.com> Cc: Mike Kravetz , Andrew Morton , Oscar Salvador , Michal Hocko , Barry Song , David Hildenbrand , Chen Huang , "Bodeddula, Balasubramaniam" , Jonathan Corbet , Matthew Wilcox , Xiongchun duan , fam.zheng@bytedance.com, Muchun Song , Qi Zheng , linux-doc@vger.kernel.org, LKML , Linux-MM Content-Type: text/plain; charset="UTF-8" Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=L2Ksg0xp; spf=pass (imf30.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4E971E001982 X-Stat-Signature: mfk4r9emfb5cf7zqrucaj1tkjoe4wi69 X-HE-Tag: 1632460264-682831 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: On Fri, Sep 24, 2021 at 6:29 AM Barry Song <21cnbao@gmail.com> wrote: > > On Wed, Sep 22, 2021 at 10:27 PM Muchun Song wrote: > > > > Since the head vmemmap page frame associated with each HugeTLB page is > > reused, we should hide the PG_head flag of tail struct page from the > > user. Add a tese case to check whether it is work properly. The test > > steps are as follows. > > > > 1) alloc 2MB hugeTLB > > 2) get each page frame > > 3) apply those APIs in each page frame > > 4) Those APIs work completely the same as before. > > > > Reading the flags of a page by /proc/kpageflags is done in > > stable_page_flags(), which has invoked PageHead(), PageTail(), > > PageCompound() and compound_head(). If those APIs work properly, the > > head page must have 15 and 17 bits set. And tail pages must have 16 > > and 17 bits set but 15 bit unset. Those flags are checked in > > check_page_flags(). > > > > Signed-off-by: Muchun Song > > --- > > tools/testing/selftests/vm/vmemmap_hugetlb.c | 144 +++++++++++++++++++++++++++ > > 1 file changed, 144 insertions(+) > > create mode 100644 tools/testing/selftests/vm/vmemmap_hugetlb.c > > > > diff --git a/tools/testing/selftests/vm/vmemmap_hugetlb.c b/tools/testing/selftests/vm/vmemmap_hugetlb.c > > new file mode 100644 > > index 000000000000..4cc74dd4c333 > > --- /dev/null > > +++ b/tools/testing/selftests/vm/vmemmap_hugetlb.c > > @@ -0,0 +1,144 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * A test case of using hugepage memory in a user application using the > > + * mmap system call with MAP_HUGETLB flag. Before running this program > > + * make sure the administrator has allocated enough default sized huge > > + * pages to cover the 2 MB allocation. > > + * > > + * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages. > > + * That means the addresses starting with 0x800000... will need to be > > + * specified. Specifying a fixed address is not required on ppc64, i386 > > + * or x86_64. > > + */ > > +#include > > +#include > > +#include > > +#include > > +#include > > + > > +#define MAP_LENGTH (2UL * 1024 * 1024) > > + > > +#ifndef MAP_HUGETLB > > +#define MAP_HUGETLB 0x40000 /* arch specific */ > > +#endif > > + > > +#define PAGE_SIZE 4096 > > + > > +#define PAGE_COMPOUND_HEAD (1UL << 15) > > +#define PAGE_COMPOUND_TAIL (1UL << 16) > > +#define PAGE_HUGE (1UL << 17) > > + > > +#define HEAD_PAGE_FLAGS (PAGE_COMPOUND_HEAD | PAGE_HUGE) > > +#define TAIL_PAGE_FLAGS (PAGE_COMPOUND_TAIL | PAGE_HUGE) > > + > > +#define PM_PFRAME_BITS 55 > > +#define PM_PFRAME_MASK ~((1UL << PM_PFRAME_BITS) - 1) > > + > > better to move the comment here: > > + * For ia64 architecture, Linux kernel reserves Region number 4 for hugepages. > + * That means the addresses starting with 0x800000... will need to be > + * specified. Specifying a fixed address is not required on ppc64, i386 > + * or x86_64. > > With the change, > > Reviewed-by: Barry Song Thanks. Will do. > > > +/* Only ia64 requires this */ > > +#ifdef __ia64__ > > +#define MAP_ADDR (void *)(0x8000000000000000UL) > > +#define MAP_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_FIXED) > > +#else > > +#define MAP_ADDR NULL > > +#define MAP_FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB) > > +#endif > > + > > +static void write_bytes(char *addr, size_t length) > > +{ > > + unsigned long i; > > + > > + for (i = 0; i < length; i++) > > + *(addr + i) = (char)i; > > +} > > + > > +static unsigned long virt_to_pfn(void *addr) > > +{ > > + int fd; > > + unsigned long pagemap; > > + > > + fd = open("/proc/self/pagemap", O_RDONLY); > > + if (fd < 0) > > + return -1UL; > > + > > + lseek(fd, (unsigned long)addr / PAGE_SIZE * sizeof(pagemap), SEEK_SET); > > + read(fd, &pagemap, sizeof(pagemap)); > > + close(fd); > > + > > + return pagemap & ~PM_PFRAME_MASK; > > +} > > + > > +static int check_page_flags(unsigned long pfn) > > +{ > > + int fd, i; > > + unsigned long pageflags; > > + > > + fd = open("/proc/kpageflags", O_RDONLY); > > + if (fd < 0) > > + return -1; > > + > > + lseek(fd, pfn * sizeof(pageflags), SEEK_SET); > > + > > + read(fd, &pageflags, sizeof(pageflags)); > > + if ((pageflags & HEAD_PAGE_FLAGS) != HEAD_PAGE_FLAGS) { > > + close(fd); > > + printf("Head page flags (%lx) is invalid\n", pageflags); > > + return -1; > > + } > > + > > + /* > > + * pages other than the first page must be tail and shouldn't be head; > > + * this also verifies kernel has correctly set the fake page_head to tail > > + * while hugetlb_free_vmemmap is enabled. > > + */ > > + for (i = 1; i < MAP_LENGTH / PAGE_SIZE; i++) { > > + read(fd, &pageflags, sizeof(pageflags)); > > + if ((pageflags & TAIL_PAGE_FLAGS) != TAIL_PAGE_FLAGS || > > + (pageflags & HEAD_PAGE_FLAGS) == HEAD_PAGE_FLAGS) { > > + close(fd); > > + printf("Tail page flags (%lx) is invalid\n", pageflags); > > + return -1; > > + } > > + } > > + > > + close(fd); > > + > > + return 0; > > +} > > + > > +int main(int argc, char **argv) > > +{ > > + void *addr; > > + unsigned long pfn; > > + > > + addr = mmap(MAP_ADDR, MAP_LENGTH, PROT_READ | PROT_WRITE, MAP_FLAGS, -1, 0); > > + if (addr == MAP_FAILED) { > > + perror("mmap"); > > + exit(1); > > + } > > + > > + /* Trigger allocation of HugeTLB page. */ > > + write_bytes(addr, MAP_LENGTH); > > + > > + pfn = virt_to_pfn(addr); > > + if (pfn == -1UL) { > > + munmap(addr, MAP_LENGTH); > > + perror("virt_to_pfn"); > > + exit(1); > > + } > > + > > + printf("Returned address is %p whose pfn is %lx\n", addr, pfn); > > + > > + if (check_page_flags(pfn) < 0) { > > + munmap(addr, MAP_LENGTH); > > + perror("check_page_flags"); > > + exit(1); > > + } > > + > > + /* munmap() length of MAP_HUGETLB memory must be hugepage aligned */ > > + if (munmap(addr, MAP_LENGTH)) { > > + perror("munmap"); > > + exit(1); > > + } > > + > > + return 0; > > +} > > -- > > 2.11.0 > > > > Thanks > barry