From patchwork Wed Sep 28 08:14:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 12991950 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 lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8A38AC04A95 for ; Wed, 28 Sep 2022 09:29:07 +0000 (UTC) Received: from localhost ([::1]:37590 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1odTNK-0001nM-J3 for qemu-devel@archiver.kernel.org; Wed, 28 Sep 2022 05:29:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34280) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1odSEC-0008E2-1y for qemu-devel@nongnu.org; Wed, 28 Sep 2022 04:15:36 -0400 Received: from mout.kundenserver.de ([212.227.17.24]:48769) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1odSE9-0008C0-GL for qemu-devel@nongnu.org; Wed, 28 Sep 2022 04:15:35 -0400 Received: from quad ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue108 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MJmCV-1oxbfi0dFB-00KBOg; Wed, 28 Sep 2022 10:15:27 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Ilya Leoshkevich , Richard Henderson , Laurent Vivier Subject: [PULL 17/38] tests/tcg/linux-test: Add linux-madvise test Date: Wed, 28 Sep 2022 10:14:56 +0200 Message-Id: <20220928081517.734954-18-laurent@vivier.eu> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220928081517.734954-1-laurent@vivier.eu> References: <20220928081517.734954-1-laurent@vivier.eu> MIME-Version: 1.0 X-Provags-ID: V03:K1:d2Jg5mLWB36vBtez6hhiQ29KTnw5vHmZPZPTV4IiQpDPNFKSmve 11w5PtzLwx0zOkoiq7OPaRC9AYwlcbfYmMr5Mrnf/sLwG/JUyYp5auyKIacrodR0rZFPpmo KqWV9l21/g7GJsVL2IgW/cz9sbsigJp87uHqOSFIg1zecd/OIidpK3CwCEg3RUVkrYmWYyM FxQrs/G4qT9uczZMAukqA== X-UI-Out-Filterresults: notjunk:1;V03:K0:6XwwXc7CQI0=:RBgrlJ1tv8VE+7hC/HLjd4 5ZeFkOPs8tUxe2ne7fXblQSxc6hYLAcoZ+qq3Bzr7NrfPVr+jy+H8DM9wnHtIiOfvBLuoQF1b TVq2lFJfN+T+sczO0ZT9/nDNt8jo7TKai9fdSzQ0sf0IXGyaI4afMXNeKR+1p61SuoOV4hI+0 qW24UXs8mHKkkMaCPdj997efEonmbLT/qc0E0elQTUc74nsujXHk0ViEFLv6+bOPA9xkKFteC uEcPkKFoqVXTuPzS3WJ4MrxzOAT3jl0fTIsVTbxdNeqRHueJOmVJl1Kbzeg/UzsxrKS9LvZ/G gtNjlzc4fOhcVqLHGHgdz+qt2ADo273k0mKSCD6lha1LHovvRasmZ/PLQKluOg4kafaeM4gfs O9RMSI4HV35N7zfCpxrBebABt98/edR6fA2okh9kye0V7p3dwMc/As16o2SAM/dS8n4uFSpBq 8qflKol24hRq4CzdpNyV133bSvcr3lvzV5cwcctysdeZAatmB9aJDic03Lv4blIiwA+taQ/Y3 792UEXt8TJskM1Djz8K3U5errVpTQMuge8eSHVvIwtQwZIJOqASm0U4s75KY1mvUvpCAD/nbS /paxcCFTUIZyq+y0UwCMpMsj1Vv/uCCM0dOuNpO30DAZZimHOaOANJTKS4gfZxLlf3xDP6O10 Go1DWL2oC6O2exsP2IkAXFlmrC038KxDXyHZsuzWdf35f7jCZJr678CZjMMdOwIkw2HhLd9I+ Fznt7K7r+83FO2gWUqgYYcPrEoSyj8hn+gKrucYKH8l/teGqHw6cuBBkGCWbwDU6AbnM852yY fSzyIS8 Received-SPF: none client-ip=212.227.17.24; envelope-from=laurent@vivier.eu; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Ilya Leoshkevich Add a test that checks madvise(MADV_DONTNEED) behavior with anonymous and file mappings in order to prevent regressions. Signed-off-by: Ilya Leoshkevich Reviewed-by: Richard Henderson Message-Id: <20220906000839.1672934-6-iii@linux.ibm.com> Signed-off-by: Laurent Vivier --- tests/tcg/multiarch/linux/linux-madvise.c | 70 +++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/tcg/multiarch/linux/linux-madvise.c diff --git a/tests/tcg/multiarch/linux/linux-madvise.c b/tests/tcg/multiarch/linux/linux-madvise.c new file mode 100644 index 000000000000..29d0997e687c --- /dev/null +++ b/tests/tcg/multiarch/linux/linux-madvise.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +static void test_anonymous(void) +{ + int pagesize = getpagesize(); + char *page; + int ret; + + page = mmap(NULL, pagesize, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + assert(page != MAP_FAILED); + + /* Check that mprotect() does not interfere with MADV_DONTNEED. */ + ret = mprotect(page, pagesize, PROT_READ | PROT_WRITE); + assert(ret == 0); + + /* Check that MADV_DONTNEED clears the page. */ + *page = 42; + ret = madvise(page, pagesize, MADV_DONTNEED); + assert(ret == 0); + assert(*page == 0); + + ret = munmap(page, pagesize); + assert(ret == 0); +} + +static void test_file(void) +{ + char tempname[] = "/tmp/.cmadviseXXXXXX"; + int pagesize = getpagesize(); + ssize_t written; + char c = 42; + char *page; + int ret; + int fd; + + fd = mkstemp(tempname); + assert(fd != -1); + ret = unlink(tempname); + assert(ret == 0); + written = write(fd, &c, sizeof(c)); + assert(written == sizeof(c)); + page = mmap(NULL, pagesize, PROT_READ, MAP_PRIVATE, fd, 0); + assert(page != MAP_FAILED); + + /* Check that mprotect() does not interfere with MADV_DONTNEED. */ + ret = mprotect(page, pagesize, PROT_READ | PROT_WRITE); + assert(ret == 0); + + /* Check that MADV_DONTNEED resets the page. */ + *page = 0; + ret = madvise(page, pagesize, MADV_DONTNEED); + assert(ret == 0); + assert(*page == c); + + ret = munmap(page, pagesize); + assert(ret == 0); + ret = close(fd); + assert(ret == 0); +} + +int main(void) +{ + test_anonymous(); + test_file(); + + return EXIT_SUCCESS; +}