From patchwork Thu Aug 29 21:43:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Xu X-Patchwork-Id: 13783935 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 39460CA0EC4 for ; Thu, 29 Aug 2024 21:44:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BAA96B0088; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 641B16B0089; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46AD76B008A; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 259D56B0088 for ; Thu, 29 Aug 2024 17:44:10 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AA3D61C3329 for ; Thu, 29 Aug 2024 21:44:09 +0000 (UTC) X-FDA: 82506611418.29.C615450 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf26.hostedemail.com (Postfix) with ESMTP id BA9A7140007 for ; Thu, 29 Aug 2024 21:44:07 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=lQiW5eRP; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.179 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724967758; 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:in-reply-to:references:references:dkim-signature; bh=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=7MHZW3hD89HefGL6soLlFj6dlai7DFkdJ921RxhFOOZVsGwITMZx8q0ki9hiDVOsMHU7gT NaTrxun9/vXIUe6GFezk6NUnnk/uLzm7VNVoyVBLwnRpOdgL3rQfloB5Vb9U54K5X9HLDu A6hLDEoicyrkUGF2h4n+XqafzziWyGs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724967758; a=rsa-sha256; cv=none; b=ze2KRcKp+fdTQoXKSw1QCFxHfcEI/r8xDh4ZBpRJ/s5QaNYh1T8gnY3HEMYWBYCAn9lsU9 pCa8MAq5VKDJ0W+gjDNCe4gzQw9EV9HMQrZ09iq9KBgw3iybk54VvAsFy5ChWw3afCIkDT IQW+3JAf88Ude0dO3CVmyWDfmLIYqz8= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=lQiW5eRP; spf=pass (imf26.hostedemail.com: domain of jeffxu@chromium.org designates 209.85.210.179 as permitted sender) smtp.mailfrom=jeffxu@chromium.org; dmarc=pass (policy=none) header.from=chromium.org Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71420e66ecfso96279b3a.0 for ; Thu, 29 Aug 2024 14:44:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1724967846; x=1725572646; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=lQiW5eRPU3Ovp/c531xW0lBLcTPv9DHCA+Z1uSEErkjH10bRJnLKW9t2JBkYRu4lQL aanyYatoo124YWwb/8o4orFehjAdQHn32hzAiAiCOgSDUI/EPWD5n/tTLy6ytbvNSL24 9wL+/W8IDfBYjUFB3NcrSNAvMMZkl1YyU3zOg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724967846; x=1725572646; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qngXRj3l1V5w2+Tk+0qChrUc0VPNj8YHKUhOOfdL/8E=; b=Oj9qSuDWFzrR9i5DetDGemUg+Z19w11hqe+LI07KXM1wp4dzFMZUYmDrJvgEUwOnOT 8bILVKMzsndeEF4ywdyJ9Nu6yoeJE4/wL4B9yPko8QlGLbwe0P0m6whIljtbi7+FWhH/ yL8DX045mngTJhSR3O/H2sZmpBmrShPX4HfgaoWtGj7EoXW4Q7ap/DkiABFaaupN8Ws7 q9dZApLSmxVuICN+vYhKLCymljHcNY66UqFl7211ps2bERgf0+HoSHdT9INukdG7ostk pCZpa66Pj0/2Br787J3hdDInhpQmKnn7gLmI5YodsF6QyYdzG17h+J+7RqZ7kbYcaBdU gNmQ== X-Forwarded-Encrypted: i=1; AJvYcCUqoez9gI9JW9fE4edUC72NCR+h9iAQmLFK/YgQ8CR/jn0UHgUzSVR0+31NbnbJFHtkwc0QnqTbzg==@kvack.org X-Gm-Message-State: AOJu0Ywq3iQyUnNfcllUh6pNUaLqCtPDbwwObWrKt3WZ0NtIJFEvKJWJ xcn9a2nZRstMMJOI+8OMLjQv7HUxAIl2njwSsAgXIgRjSBbD+QoxEQ2VqlmdFA== X-Google-Smtp-Source: AGHT+IGw6LhTiqcHA4wOD2hbInscY7fZlLpfvydbHUPkI3AUpW/gqVA9D8kpvdBYJgGdQzekGp3weg== X-Received: by 2002:a17:903:2350:b0:1fa:a9f5:64b1 with SMTP id d9443c01a7336-2052761fcd8mr1316265ad.3.1724967846304; Thu, 29 Aug 2024 14:44:06 -0700 (PDT) Received: from localhost (150.12.83.34.bc.googleusercontent.com. [34.83.12.150]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-205152b26b2sm15882985ad.6.2024.08.29.14.44.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 29 Aug 2024 14:44:05 -0700 (PDT) From: jeffxu@chromium.org To: akpm@linux-foundation.org Cc: linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, pedro.falcato@gmail.com, willy@infradead.org, lorenzo.stoakes@oracle.com, broonie@kernel.org, vbabka@suse.cz, Liam.Howlett@oracle.com, rientjes@google.com, keescook@chromium.org, Jeff Xu Subject: [PATCH v2 2/4] selftests/mm: mseal_test add sealed madvise type Date: Thu, 29 Aug 2024 21:43:50 +0000 Message-ID: <20240829214352.963001-3-jeffxu@chromium.org> X-Mailer: git-send-email 2.46.0.469.g59c65b2a67-goog In-Reply-To: <20240829214352.963001-1-jeffxu@chromium.org> References: <20240829214352.963001-1-jeffxu@chromium.org> MIME-Version: 1.0 X-Stat-Signature: jb487b3fzepys58998egg6tz86k8hpfb X-Rspamd-Queue-Id: BA9A7140007 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1724967847-853846 X-HE-Meta: U2FsdGVkX1/oRgmXIVr+zFFFIvj3sgl9QSlo7CmUm/977ZKDuUDa1DgbqQAOVj7gu3UqsUjyeUsMHpaUEEmTIPs7uuO/QjnkEqPLm3Z/omkjqH6R+zbE3/gMWurYvg1bQXcMlswBCvq7/NpEjA6MFyp8iHfCnZzILME8hHNIB4mtt2ZWYzBggVIIXy/tTsHEGoH4/hHzDOqjMSUZKnVj0vTYgwohb/3tD7tMw0wTZ2nGZO77I5c5ywYxdbPnXkt0qV4QRO71tLZJ9q1Cci91niYvz3UpsieCFBvkHXg2iAr3EVnz0s7ncUVWroc8yXWbwmAGVjbkimR5lgRJFqn1X6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-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Jeff Xu Add a testcase to cover all sealed madvise type. Signed-off-by: Jeff Xu --- tools/testing/selftests/mm/mseal_test.c | 108 +++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/mseal_test.c b/tools/testing/selftests/mm/mseal_test.c index adc646cf576c..ae06c354220d 100644 --- a/tools/testing/selftests/mm/mseal_test.c +++ b/tools/testing/selftests/mm/mseal_test.c @@ -2121,6 +2121,107 @@ static void test_seal_madvise_nodiscard(bool seal) REPORT_TEST_PASS(); } +static void test_seal_discard_madvise_advice(void) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 4 * page_size; + int ret; + int sealed_advice[] = {MADV_FREE, MADV_DONTNEED, + MADV_DONTNEED_LOCKED, MADV_REMOVE, + MADV_DONTFORK, MADV_WIPEONFORK}; + int size_sealed_advice = sizeof(sealed_advice) / sizeof(int); + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + ret = seal_single_address(ptr, size); + FAIL_TEST_IF_FALSE(!ret); + + for (int i = 0; i < size_sealed_advice; i++) { + ret = sys_madvise(ptr, size, sealed_advice[i]); + FAIL_TEST_IF_FALSE(ret < 0); + FAIL_TEST_IF_FALSE(errno == EPERM); + } + + REPORT_TEST_PASS(); +} + +static void test_munmap_free_multiple_ranges(bool seal) +{ + void *ptr; + unsigned long page_size = getpagesize(); + unsigned long size = 8 * page_size; + int ret; + int prot; + + setup_single_address(size, &ptr); + FAIL_TEST_IF_FALSE(ptr != (void *)-1); + + /* unmap one page from beginning. */ + ret = sys_munmap(ptr, page_size); + FAIL_TEST_IF_FALSE(!ret); + + /* unmap one page from middle. */ + ret = sys_munmap(ptr + 4 * page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr + page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + + /* seal the last page */ + if (seal) { + ret = sys_mseal(ptr + 7 * page_size, page_size); + FAIL_TEST_IF_FALSE(!ret); + + size = get_vma_size(ptr + 1 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 7 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + } + + /* munmap all 8 pages from beginning */ + ret = sys_munmap(ptr, 8 * page_size); + if (seal) { + FAIL_TEST_IF_FALSE(ret); + + /* verify mapping are not changed */ + size = get_vma_size(ptr + 1 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 3 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 5 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 2 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + + size = get_vma_size(ptr + 7 * page_size, &prot); + FAIL_TEST_IF_FALSE(size == 1 * page_size); + FAIL_TEST_IF_FALSE(prot == 4); + } else { + FAIL_TEST_IF_FALSE(!ret); + + for (int i = 0; i < 8; i++) { + size = get_vma_size(ptr, &prot); + FAIL_TEST_IF_FALSE(size == 0); + } + } + + REPORT_TEST_PASS(); +} + int main(int argc, char **argv) { bool test_seal = seal_support(); @@ -2142,7 +2243,7 @@ int main(int argc, char **argv) if (!get_vma_size_supported()) ksft_exit_skip("get_vma_size not supported\n"); - ksft_set_plan(88); + ksft_set_plan(91); test_seal_addseal(); test_seal_unmapped_start(); @@ -2251,5 +2352,10 @@ int main(int argc, char **argv) test_seal_discard_ro_anon_on_pkey(false); test_seal_discard_ro_anon_on_pkey(true); + test_seal_discard_madvise_advice(); + + test_munmap_free_multiple_ranges(false); + test_munmap_free_multiple_ranges(true); + ksft_finished(); }