From patchwork Fri Aug 31 07:13:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiao Yang X-Patchwork-Id: 963775 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cn.fujitsu.com Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 421F8q0HPBz9ryn for ; Thu, 30 Aug 2018 17:52:58 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 8AA453E748C for ; Thu, 30 Aug 2018 09:52:56 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [217.194.8.5]) by picard.linux.it (Postfix) with ESMTP id 695C03E7455 for ; Thu, 30 Aug 2018 09:52:52 +0200 (CEST) Received: from heian.cn.fujitsu.com (mail.cn.fujitsu.com [183.91.158.132]) by in-5.smtp.seeweb.it (Postfix) with ESMTP id 49B92600976 for ; Thu, 30 Aug 2018 09:52:50 +0200 (CEST) X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44179990" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 30 Aug 2018 15:52:46 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id D662E4B6EC64; Thu, 30 Aug 2018 15:52:43 +0800 (CST) Received: from RHEL7U5Alpha_SERVER.g08.fujitsu.local (10.167.220.156) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.408.0; Thu, 30 Aug 2018 15:52:42 +0800 From: Xiao Yang To: Date: Fri, 31 Aug 2018 15:13:21 +0800 Message-ID: <1535699602-28185-1-git-send-email-yangx.jy@cn.fujitsu.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <864746561.43466655.1535545169373.JavaMail.zimbra@redhat.com> References: <864746561.43466655.1535545169373.JavaMail.zimbra@redhat.com> MIME-Version: 1.0 X-Originating-IP: [10.167.220.156] X-yoursite-MailScanner-ID: D662E4B6EC64.AC61B X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: yangx.jy@cn.fujitsu.com X-Spam-Status: No, score=2.5 required=7.0 tests=DATE_IN_FUTURE_12_24 autolearn=disabled version=3.4.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Cc: ltp@lists.linux.it Subject: [LTP] [PATCH v2 3/4] syscalls/mlock201: Add new testcase X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.18 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" 1) Add SAFE_MINCORE() macro 2) Check the basic functionality of mlock2(2). Note: 1) We use tst_syscall() to check if mlock2() is supported. 2) since kernel v2.6.9, the limits and permissions of mlock2() changed, so we just check mlock2() since the version. Signed-off-by: Xiao Yang --- include/lapi/mlock2.h | 16 ++++ include/tst_safe_macros.h | 5 + lib/safe_macros.c | 14 +++ runtest/ltplite | 2 + runtest/stress.part3 | 2 + runtest/syscalls | 2 + testcases/kernel/syscalls/mlock2/.gitignore | 1 + testcases/kernel/syscalls/mlock2/Makefile | 8 ++ testcases/kernel/syscalls/mlock2/mlock201.c | 144 ++++++++++++++++++++++++++++ 9 files changed, 194 insertions(+) create mode 100644 include/lapi/mlock2.h create mode 100644 testcases/kernel/syscalls/mlock2/.gitignore create mode 100644 testcases/kernel/syscalls/mlock2/Makefile create mode 100644 testcases/kernel/syscalls/mlock2/mlock201.c diff --git a/include/lapi/mlock2.h b/include/lapi/mlock2.h new file mode 100644 index 0000000..fa2b2de --- /dev/null +++ b/include/lapi/mlock2.h @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang + */ + +#ifndef LAPI_MLOCK2_H__ +# define LAPI_MLOCK2_H__ + +#include + +#ifndef MLOCK_ONFAULT +# define MLOCK_ONFAULT 0x01 +#endif + +#endif /* LAPI_MLOCK2_H__ */ diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h index dc6c1f7..03657a4 100644 --- a/include/tst_safe_macros.h +++ b/include/tst_safe_macros.h @@ -490,6 +490,11 @@ int safe_munlock(const char *file, const int lineno, const char *addr, size_t len); #define SAFE_MUNLOCK(addr, len) safe_munlock(__FILE__, __LINE__, (addr), (len)) +int safe_mincore(const char *file, const int lineno, void *start, + size_t length, unsigned char *vec); +#define SAFE_MINCORE(start, length, vec) \ + safe_mincore(__FILE__, __LINE__, (start), (length), (vec)) + int safe_fanotify_init(const char *file, const int lineno, unsigned int flags, unsigned int event_f_flags); #define SAFE_FANOTIFY_INIT(fan, mode) \ diff --git a/lib/safe_macros.c b/lib/safe_macros.c index fa92a6f..5cc80d0 100644 --- a/lib/safe_macros.c +++ b/lib/safe_macros.c @@ -1034,3 +1034,17 @@ int safe_munlock(const char *file, const int lineno, const void *addr, return rval; } + +int safe_mincore(const char *file, const int lineno, void *start, + size_t length, unsigned char *vec) +{ + int rval; + + rval = mincore(start, length, vec); + if (rval == -1) { + tst_brkm(TBROK | TERRNO, NULL, + "%s:%d: mincore() failed", file, lineno); + } + + return rval; +} diff --git a/runtest/ltplite b/runtest/ltplite index 9ca6c42..270c649 100644 --- a/runtest/ltplite +++ b/runtest/ltplite @@ -443,6 +443,8 @@ mknod09 mknod09 mlock01 mlock01 mlock02 mlock02 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/runtest/stress.part3 b/runtest/stress.part3 index ec18dcf..6850572 100644 --- a/runtest/stress.part3 +++ b/runtest/stress.part3 @@ -369,6 +369,8 @@ mknod09 mknod09 mlock01 mlock01 mlock02 mlock02 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/runtest/syscalls b/runtest/syscalls index 3161d91..5d84d48 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -633,6 +633,8 @@ mlock02 mlock02 mlock03 mlock03 -i 20 mlock04 mlock04 +mlock201 mlock201 + qmm01 mmap001 -m 1 mmap01 mmap01 mmap02 mmap02 diff --git a/testcases/kernel/syscalls/mlock2/.gitignore b/testcases/kernel/syscalls/mlock2/.gitignore new file mode 100644 index 0000000..431eff8 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/.gitignore @@ -0,0 +1 @@ +/mlock201 diff --git a/testcases/kernel/syscalls/mlock2/Makefile b/testcases/kernel/syscalls/mlock2/Makefile new file mode 100644 index 0000000..427df06 --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. +# Author: Xiao Yang + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/syscalls/mlock2/mlock201.c b/testcases/kernel/syscalls/mlock2/mlock201.c new file mode 100644 index 0000000..d2090bc --- /dev/null +++ b/testcases/kernel/syscalls/mlock2/mlock201.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved. + * Author: Xiao Yang + */ +/* + * Description: + * Check the basic functionality of the mlock2(2) since kernel v2.6.9: + * 1) When we use mlock2() without MLOCK_ONFAULT to lock memory in the + * specified range that is multiples of page size or not, we can + * show correct size of locked memory by VmLck from /proc/PID/status + * and lock all pages including non-present. + * 2) When we use mlock2() with MLOCK_ONFAULT to lock memory in the + * specified range that is multiples of page size or not, we can + * show correct size of locked memory by VmLck from /proc/PID/status + * and just lock present pages. + */ +#include +#include +#include +#include +#include +#include + +#include "tst_test.h" +#include "lapi/syscalls.h" +#include "lapi/mlock2.h" + +#define PAGES 8 +#define HPAGES (PAGES / 2) + +static size_t pgsz; +static unsigned char *vec; + +static struct tcase { + size_t pop_pgs; + size_t lock_pgs; + size_t offset; + size_t exp_vmlcks; + size_t exp_pgs; + int flag; +} tcases[] = { + {0, 1, 0, 1, 1, 0}, + {0, PAGES, 0, PAGES, PAGES, 0}, + /* mlock2() locks 3 pages if the specified + * range is little more than 2 pages. + */ + {0, 2, 1, 3, 3, 0}, + /* mlock2() locks 2 pages if the specified + * range is little less than 2 pages. + */ + {0, 2, -1, 2, 2, 0}, + /* mlock2() with MLOCK_ONFAULT just lock present + * pages populated by data. + */ + {0, 1, 0, 1, 0, MLOCK_ONFAULT}, + {HPAGES, PAGES, 0, PAGES, HPAGES, MLOCK_ONFAULT}, + {1, HPAGES, 1, HPAGES + 1, 1, MLOCK_ONFAULT}, + {HPAGES, HPAGES, -1, HPAGES, HPAGES, MLOCK_ONFAULT}, +}; + +static size_t check_locked_pages(char *addr, size_t len, size_t num_pgs) +{ + size_t n; + size_t act_pages = 0; + + SAFE_MINCORE(addr, len, vec); + + for (n = 0; n < num_pgs; n++) { + if (vec[n] & 1) + act_pages++; + } + + return act_pages; +} + +static void verify_mlock2(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + size_t bsize, asize, act_vmlcks, act_pgs; + char *addr; + + addr = SAFE_MMAP(NULL, PAGES * pgsz, PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, 0, 0); + + if (tc->pop_pgs) + memset(addr, 0, tc->pop_pgs * pgsz); + + SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &bsize); + + TEST(tst_syscall(__NR_mlock2, addr, tc->lock_pgs * pgsz + tc->offset, + tc->flag)); + if (TST_RET != 0) { + if (tc->flag && TST_ERR == EINVAL) + tst_res(TCONF, "mlock2() didn't support MLOCK_ONFAULT"); + else + tst_res(TFAIL | TTERRNO, "mlock2() failed"); + goto end2; + } + + SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &asize); + + act_vmlcks = (asize - bsize) * 1024 / pgsz; + if (tc->exp_vmlcks != act_vmlcks) { + tst_res(TFAIL, "VmLck showed wrong %ld pages, expected %ld", + act_vmlcks, tc->exp_vmlcks); + goto end1; + } + + act_pgs = check_locked_pages(addr, PAGES * pgsz, PAGES); + if (act_pgs != tc->exp_pgs) { + tst_res(TFAIL, "mlock2(%d) locked %ld pages, expected %ld", + tc->flag, act_pgs, tc->exp_pgs); + } else { + tst_res(TPASS, "mlock2(%d) succeeded in locking %ld pages", + tc->flag, tc->exp_pgs); + } + +end1: + SAFE_MUNLOCK(addr, tc->lock_pgs * pgsz + tc->offset); +end2: + SAFE_MUNMAP(addr, PAGES * pgsz); +} + +static void setup(void) +{ + pgsz = getpagesize(); + vec = SAFE_MALLOC((PAGES * pgsz + pgsz - 1) / pgsz); +} + +static void cleanup(void) +{ + if (vec) + free(vec); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_mlock2, + .setup = setup, + .cleanup = cleanup, + .needs_root = 1, + .min_kver = "2.6.9", +};