From patchwork Mon Jan 27 07:55:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 13951051 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 D1ECBC0218C for ; Mon, 27 Jan 2025 07:55:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 44855280112; Mon, 27 Jan 2025 02:55:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A7CA2800E8; Mon, 27 Jan 2025 02:55:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1FA06280112; Mon, 27 Jan 2025 02:55:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EDAD62800E8 for ; Mon, 27 Jan 2025 02:55:57 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 906C3162100 for ; Mon, 27 Jan 2025 07:55:57 +0000 (UTC) X-FDA: 83052473154.26.D611C93 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) by imf20.hostedemail.com (Postfix) with ESMTP id ACFE11C000E for ; Mon, 27 Jan 2025 07:55:55 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lROj6rnD; spf=pass (imf20.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.44 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737964555; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=3DwVXD9T9GG/At13eQyoSQoYWHfxq+Au8guXIQCaFOs=; b=dqR92UAzjS8vVDcO16rDTRLf1ijtHfx5LcwIcA81bY/B7+22TcGxcPC4brjzIURpPIHR+5 jn6yofP1z/xak/+WLB9/tjUklSu6wIJZ9VIfFEEzVS/8BY4ef+zJigkUjyH5TWtYqZvH9E HDcD+vzaH3F/TzGBad1SBJivByHGZwc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lROj6rnD; spf=pass (imf20.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.44 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737964555; a=rsa-sha256; cv=none; b=sK5LUZORcllkhFm6tSgT8aA3lGLn07jU3U9l04jgVurFnxcFXymEz9J8e9Dj2B3iWVeHB5 tRp8CX86ju1H3oNFaODyI9ZPV6t79G/tWnglHPzB4TttXOwNUuI/4yX53UCTcI3IvnyBbp sj10SN7UYw1qBFtaqPmxD8MdzTjetak= Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-aaf57c2e0beso842583766b.3 for ; Sun, 26 Jan 2025 23:55:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737964554; x=1738569354; darn=kvack.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=3DwVXD9T9GG/At13eQyoSQoYWHfxq+Au8guXIQCaFOs=; b=lROj6rnDvI8fWWfkwzLOzXS7mWEAawkp2VoN1z+/PdFl2BMZhXNxjJ4hkmEZqsLoAB 0By9FcNwE6S2Y7z4BzP9Yt4eS2Z9eNyNl5dDKcdYgCDWaeS+OHd9ciAcZnVSNzVY80Gw v2dYyxyjtJb+MRGMnKVO/2FFYMmtxtDxVT5T2uFV7xLabqfAGk0SF2SEKUU/G7deZ+h5 h7Hb7/hrlwrtFLJIIxOnxRjES4GV4HBMXRAfDPCj5jm5r1hVq54YP/jcCbMSbqtlW9sh JtilVOGfPNtW5gwirdjKhuj4UEBNutzQe+P8l+AFEc/aQ3CE5vD1nSvRJz00Zu7ir5O/ RE5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737964554; x=1738569354; h=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=3DwVXD9T9GG/At13eQyoSQoYWHfxq+Au8guXIQCaFOs=; b=BpQIF7xuk+mP9e3ezIvjRjkUTFb5pj9nQc4pk6GdBPhN6BQot8H0mfIawxQ4UqHdQn Mzb+UQDj5lZ8BgxdgBlwVwJnAQPo5JQSujYRXzlId5LfSrG6fT/Vw3xLALHWdvijw8i2 qHQ8ToL/YSLe7QDedLtGjD2FbJK/203dgTKKvaTuedb6Swq5fXfBnT0mLXoHxOBfNiJq FegvGAatt8DszdolgNQLaiAXgNLzYFo97ACNcmk3w4KBAo8jj2QEBu5maXbkeDQChXbp MbvyKC7/WljDYWjxDedzp9wKNWCQuGoi9TJLHC+YsIT7k5P3I86MS0ZFCi900GBfgeW6 dveg== X-Gm-Message-State: AOJu0YxOqVfMfjSsRmVPfuG0ttzHZmW97ur7ZelPQO90vdHkzCEqVRTV x4EO9mYquSVrbo0fFegmKpvwcBYcBQU2KxnHcbf70ZSS1jIMrgZc0rE3LyTe X-Gm-Gg: ASbGnctkuiXCBfuJyFlNRDrtuKDGkmaMwgEQwWjW+PzrYSMzbfSWXG9Pkktdmsls+SL /QCYXsPA5hIDnrCTjltIp8iMdGB1GjqNuHNSWrwA4fHBG6v54oBNs5hweRrntHnU5fZPcyFuMtJ hcc8fKVDPXVN4cBdCrxq4JJ2bRP3S8tqfnhX1HAfYjKkWT1oRsxxxtjmD2thvfhDyo0WnveJLuX XsfCv+8s5RGzHDoYTgF/SUhuABIUpDbMQn2fTjHuZGAuRQxY2JN4iakgiIdn1RJ2GCodWPaxnb5 wGIp X-Google-Smtp-Source: AGHT+IE8j94drsAFvUrc3YoC9VsMP/mAczxEyQjoFYUxzLDCAJjDTIUb+nMbVW/Mq4mc6FdxPHmq3g== X-Received: by 2002:a17:907:1b03:b0:aaf:1183:e9be with SMTP id a640c23a62f3a-ab38b1e8f64mr4144616166b.2.1737964554111; Sun, 26 Jan 2025 23:55:54 -0800 (PST) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab675e13694sm535860866b.24.2025.01.26.23.55.53 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 Jan 2025 23:55:53 -0800 (PST) From: Wei Yang To: akpm@linux-foundation.org, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, jannh@google.com Cc: linux-mm@kvack.org, Wei Yang , "Liam R . Howlett" Subject: [PATCH 2/2] tools: testing: add unmapped_area() tests Date: Mon, 27 Jan 2025 07:55:27 +0000 Message-Id: <20250127075527.16614-3-richard.weiyang@gmail.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20250127075527.16614-1-richard.weiyang@gmail.com> References: <20250127075527.16614-1-richard.weiyang@gmail.com> X-Rspamd-Queue-Id: ACFE11C000E X-Stat-Signature: o1acmpxddhr1yemr848uinuw1xim97ce X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737964555-274643 X-HE-Meta: U2FsdGVkX19vEu7yyLnnTm67WexB9hhWzE5FDg6j+ordRIzLkR8PGUzvTsvzHD1HAnYB3CAyzPb9cj1relrgT7N4ydX0P7jWtpxqBgi5D3LtlWmsqHCVfCc+ZROScWB1n/oe41kW6G8E2ZYyzX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IOVi3EjPj9N75DHhaLkpIc9NtdfTcqKOcaSXgkNF4xQaJL9NbVvXRvP2dghykxYFhZkWb4k1Pgy50l1OybWazLYm2GrAW0iese3QsYZom0nqsVIfRtYple2fN5WccpUqceqcHyWFDjJAhJKNXutgxFJ7FDFk0/Wfrqq2ts4nmcWYpe6R/imtIvBV/kbvH2UAap98yOX89PNqi8YnHSrIFncvmPJzGPYaqaP/T6jElSCf7rTg5mJG+gE7jykYt80kAoKhiSR8tVbyxr90hjEizLoNBEMfoep1wLcOUimBu4eUXfROKDlfZf1Y8rbXDQpQhL7U6sG5IQX7R2lFoUfJSGXJsOUWhVoVccBivVjM7/8= 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: List-Subscribe: List-Unsubscribe: Leverage the userland vma tests to verify unmapped_area{_topdown} behavior. Signed-off-by: Wei Yang CC: Liam R. Howlett CC: Lorenzo Stoakes CC: Vlastimil Babka CC: Jann Horn --- tools/testing/vma/vma.c | 177 +++++++++++++++++++++++++++++++ tools/testing/vma/vma_internal.h | 2 +- 2 files changed, 178 insertions(+), 1 deletion(-) diff --git a/tools/testing/vma/vma.c b/tools/testing/vma/vma.c index 6c31118d5fe5..3c1817b01ac8 100644 --- a/tools/testing/vma/vma.c +++ b/tools/testing/vma/vma.c @@ -1735,6 +1735,182 @@ static bool test_mmap_region_basic(void) return true; } +static bool test_unmapped_area(void) +{ + struct mm_struct mm = {}; + struct vm_unmapped_area_info info = {}; + unsigned long addr, addr1, addr2, addr3, addr4; + unsigned long low = mmap_min_addr + 0x2000, size = 0x1000, gap = 0x1000; + unsigned long stack_guard_gap_old = stack_guard_gap; + + VMA_ITERATOR(vmi, &mm, 0); + + current->mm = &mm; + + /* adjust guard gap for test */ + stack_guard_gap = gap; + + /* + * Prepare a range like this: + * + * 0123456789abcdef + * m m m m + * ma m m am start_gap = 0 + * m am m am start_gap = 0x1000 + * m m aam m start_gap = 0x2000 + */ + addr1 = low; + __mmap_region(NULL, addr1, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE, + addr1 >> PAGE_SHIFT, NULL); + addr2 = addr1 + size + (gap * 2); + __mmap_region(NULL, addr2, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE, + addr2 >> PAGE_SHIFT, NULL); + addr3 = addr2 + size + (gap * 4); + __mmap_region(NULL, addr3, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE, + addr3 >> PAGE_SHIFT, NULL); + addr4 = addr3 + size + (gap * 2); + __mmap_region(NULL, addr4, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE, + addr4 >> PAGE_SHIFT, NULL); + + /* start_gap == 0 */ + info.length = size; + info.low_limit = low; + info.high_limit = addr4 + size; + info.start_gap = 0; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr1 + size); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr4 - size); + + /* start_gap == 0x1000 */ + info.start_gap = gap; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr1 + size + info.start_gap); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr4 - size); + + /* start_gap == 0x2000 */ + info.start_gap = gap * 2; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr2 + size + info.start_gap); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr3 - size); + + cleanup_mm(&mm, &vmi); + + /* + * Prepare a range like this with VM_GROWSDOWN set: + * + * 0123456789abcdef + * m m m m + * ma m ma m start_gap = 0 + * m m aa m m start_gap = 0x1000 + * m m a m m start_gap = 0x2000 + */ + addr1 = low; + __mmap_region(NULL, addr1, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSDOWN, + addr1 >> PAGE_SHIFT, NULL); + addr2 = addr1 + size + (gap * 2); + __mmap_region(NULL, addr2, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSDOWN, + addr2 >> PAGE_SHIFT, NULL); + addr3 = addr2 + size + (gap * 4); + __mmap_region(NULL, addr3, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSDOWN, + addr3 >> PAGE_SHIFT, NULL); + addr4 = addr3 + size + (gap * 2); + __mmap_region(NULL, addr4, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSDOWN, + addr4 >> PAGE_SHIFT, NULL); + + /* start_gap == 0 */ + info.length = size; + info.low_limit = low; + info.high_limit = addr4 + size; + info.start_gap = 0; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr1 + size); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr4 - stack_guard_gap - size); + + /* start_gap == 0x1000 */ + info.start_gap = gap; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr2 + size + info.start_gap); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr3 - stack_guard_gap - size); + + /* start_gap == 0x2000 */ + info.start_gap = gap * 2; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr2 + size + info.start_gap); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr3 - stack_guard_gap - size); + + cleanup_mm(&mm, &vmi); + + /* + * Prepare a range like this with VM_GROWSUP set: + * + * 0123456789abcdef + * m m m m + * m am m am start_gap = 0 + * m am m am start_gap = 0x1000 + * m m aam m start_gap = 0x2000 + */ + addr1 = low; + __mmap_region(NULL, addr1, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSUP, + addr1 >> PAGE_SHIFT, NULL); + addr2 = addr1 + size + (gap * 2); + __mmap_region(NULL, addr2, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSUP, + addr2 >> PAGE_SHIFT, NULL); + addr3 = addr2 + size + (gap * 4); + __mmap_region(NULL, addr3, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSUP, + addr3 >> PAGE_SHIFT, NULL); + addr4 = addr3 + size + (gap * 2); + __mmap_region(NULL, addr4, size, + VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_GROWSUP, + addr4 >> PAGE_SHIFT, NULL); + + /* start_gap == 0 */ + info.length = size; + info.low_limit = low; + info.high_limit = addr4 + size; + info.start_gap = 0; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr1 + size + stack_guard_gap); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr4 - size); + + /* start_gap == 0x1000 */ + info.start_gap = gap; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr1 + size + stack_guard_gap); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr4 - size); + + /* start_gap == 0x2000 */ + info.start_gap = gap * 2; + addr = unmapped_area(&info); + ASSERT_EQ(addr, addr2 + size + info.start_gap); + addr = unmapped_area_topdown(&info); + ASSERT_EQ(addr, addr3 - size); + + cleanup_mm(&mm, &vmi); + + /* restore stack_guard_gap */ + stack_guard_gap = stack_guard_gap_old; + return true; +} + int main(void) { int num_tests = 0, num_fail = 0; @@ -1769,6 +1945,7 @@ int main(void) TEST(expand_only_mode); TEST(mmap_region_basic); + TEST(unmapped_area); #undef TEST diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h index 1eae23039854..37b47fb85a3b 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -65,7 +65,7 @@ extern unsigned long dac_mmap_min_addr; #define VM_SHADOW_STACK VM_NONE #define VM_SOFTDIRTY 0 #define VM_ARCH_1 0x01000000 /* Architecture-specific flag */ -#define VM_GROWSUP VM_NONE +#define VM_GROWSUP VM_ARCH_1 #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC) #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP)