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.7 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,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 88D87C4361B for ; Thu, 17 Dec 2020 07:27:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2CE0823715 for ; Thu, 17 Dec 2020 07:27:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726985AbgLQH1e (ORCPT ); Thu, 17 Dec 2020 02:27:34 -0500 Received: from out30-130.freemail.mail.aliyun.com ([115.124.30.130]:36724 "EHLO out30-130.freemail.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726416AbgLQH10 (ORCPT ); Thu, 17 Dec 2020 02:27:26 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R131e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04420;MF=teawaterz@linux.alibaba.com;NM=1;PH=DS;RN=4;SR=0;TI=SMTPD_---0UItlrWK_1608189998; Received: from localhost(mailfrom:teawaterz@linux.alibaba.com fp:SMTPD_---0UItlrWK_1608189998) by smtp.aliyun-inc.com(127.0.0.1); Thu, 17 Dec 2020 15:26:42 +0800 From: Hui Zhu To: wufengguang@huawei.com, linux-kernel@vger.kernel.org Cc: Hui Zhu , Hui Zhu Subject: [PATCH] usemem: Add option touch-alloc Date: Thu, 17 Dec 2020 15:26:31 +0800 Message-Id: <20201217072631.6440-1-teawater@gmail.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some environment will not fault in memory even if MAP_POPULATE is set. This commit add option touch-alloc to read memory after allocate it to make sure the pages is fault in. Signed-off-by: Hui Zhu --- usemem.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/usemem.c b/usemem.c index 6d1d575..d93691b 100644 --- a/usemem.c +++ b/usemem.c @@ -97,6 +97,7 @@ unsigned long opt_delay = 0; int opt_read_again = 0; int opt_punch_holes = 0; int opt_init_time = 0; +int opt_touch_alloc = 0; int nr_task; int nr_thread; int nr_cpu; @@ -157,6 +158,7 @@ void usage(int ok) " -Z|--read-again read memory again after access the memory\n" " --punch-holes free every other page after allocation\n" " --init-time remove the initialization time from the run time and show the initialization time\n" + " --touch-alloc read memory after allocate it\n" " -h|--help show this message\n" , ourname); @@ -197,6 +199,7 @@ static const struct option opts[] = { { "read-again" , 0, NULL, 'Z' }, { "punch-holes" , 0, NULL, 0 }, { "init-time" , 0, NULL, 0 }, + { "touch-alloc" , 0, NULL, 0 }, { "help" , 0, NULL, 'h' }, { NULL , 0, NULL, 0 } }; @@ -326,6 +329,18 @@ void detach(void) } } +unsigned long do_access(unsigned long *p, unsigned long idx, int read) +{ + volatile unsigned long *vp = p; + + if (read) + return vp[idx]; /* read data */ + else { + vp[idx] = idx; /* write data */ + return 0; + } +} + unsigned long * allocate(unsigned long bytes) { unsigned long *p; @@ -352,6 +367,14 @@ unsigned long * allocate(unsigned long bytes) p = (unsigned long *)ALIGN((unsigned long)p, pagesize - 1); } + if (opt_touch_alloc) { + unsigned long i; + unsigned long m = bytes / sizeof(*p); + + for (i = 0; i < m; i += 1) + do_access(p, i, 1); + } + return p; } @@ -433,18 +456,6 @@ void shm_unlock(int seg_id) shmctl(seg_id, SHM_UNLOCK, NULL); } -unsigned long do_access(unsigned long *p, unsigned long idx, int read) -{ - volatile unsigned long *vp = p; - - if (read) - return vp[idx]; /* read data */ - else { - vp[idx] = idx; /* write data */ - return 0; - } -} - #define NSEC_PER_SEC (1UL * 1000 * 1000 * 1000) long nsec_sub(long nsec1, long nsec2) @@ -950,6 +961,8 @@ int main(int argc, char *argv[]) opt_punch_holes = 1; } else if (strcmp(opts[opt_index].name, "init-time") == 0) { opt_init_time = 1; + } else if (strcmp(opts[opt_index].name, "touch-alloc") == 0) { + opt_touch_alloc = 1; } else usage(1); break; -- 2.17.1