From patchwork Mon Jan 27 07:55:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 13951050 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 D04E3C0218F for ; Mon, 27 Jan 2025 07:55:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 224D9280110; Mon, 27 Jan 2025 02:55:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D31B2800E8; Mon, 27 Jan 2025 02:55:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07381280110; Mon, 27 Jan 2025 02:55:56 -0500 (EST) 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 C66702800E8 for ; Mon, 27 Jan 2025 02:55:56 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 49FEEC2002 for ; Mon, 27 Jan 2025 07:55:56 +0000 (UTC) X-FDA: 83052473112.28.0B8D5A6 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) by imf10.hostedemail.com (Postfix) with ESMTP id 7E041C0004 for ; Mon, 27 Jan 2025 07:55:54 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DeEfX2PF; spf=pass (imf10.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=1737964554; 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=ADd5QPXGRb0ZUjBZ78cLndx1ZBrU1KrHUZpnVuQhnKU=; b=OS2mtyonIjrQNxyX8iaNkysOUd02DLX4rpCYIn1SAsmRhhJ/TrKEyr+FJZuQ+cQ65KK6HY hP51n7aDOawHQ4h+EoUpeLiIgfEnXJ0p9OXu3JNbkXU6UGu/nR9x9Nnq5fyfy8dllZFKCC P6FGLMdc3xZDNPp5DgyQKCLEt2SWwfQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=DeEfX2PF; spf=pass (imf10.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=1737964554; a=rsa-sha256; cv=none; b=IMXvGASssbbl8HwnmEKga32jRJV4d7joPvlsXSLdcvSyynANaKMhdHI2+3dSJjTQQ8BxWq 0KmI5xf9nuD9x8hRloFHuSBhBrqCCC4bxmoKHkbLz3Cy6SuDcZyfa8rMyTDmhCy+dTp6v2 IXZdjCaQlluh1UChmRPLcWL2QWTGXiA= Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-aab925654d9so797481166b.2 for ; Sun, 26 Jan 2025 23:55:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737964553; x=1738569353; 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=ADd5QPXGRb0ZUjBZ78cLndx1ZBrU1KrHUZpnVuQhnKU=; b=DeEfX2PF4rtoybTRuR43y7e0cXs7LeAasFPXSHZgv80zxevJqY4ReZeF6rPIbbz7Ea 0zTs+3EG7OoqxafuyAzFuAI8f67fHV/ZIlTcNmRQPLtlh4sxMeZjyS4vOfyDa7ty5kY0 L0S9rjvNY2rBVdSfALUU5xpvDJow/NNObjoAIadTGLTjKfEs3PoikzRr9suKEiN4nwBg RVjKkyGX7TUZBvuhb53WN20hKdqa+2ZR8ZytsP0JxEQvprJ8dV5U30/cdwz9nFOD/CXe hLCH7avnk6py8JJben75aHp8UbHG2cCM66s1gOs16N/kxHK/MUpO3LjCNr3BfZVTSeIk l4Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737964553; x=1738569353; 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=ADd5QPXGRb0ZUjBZ78cLndx1ZBrU1KrHUZpnVuQhnKU=; b=cBW1SLDMAxYFsZOGFt2Lrqr95qom/n0IDtn68sLZfoOQ2NzGLUXF7N3WQO1dIBnM5R CqWi2afMhJwJfciRucn1Sf8UE85D57ujyzPDY/8YLotSCEG6rYEgDFRN4g8pdGB2IogW 8xWsWIUqyF1/6+cQXG3nX6E9WRjg0P6fCWuxEO2VhsZNlXPJ5o2NUtm7Y/15RzTZ7Ga6 ckopaZD6BgiSBD6LjptmnkCuyEsib/REH3uiONX5MSqCOAkeioveOetYE7ITyreBaOna nky+dGtk9IIDq2I+VoV0gKdW3wC+Kv2fyj1hLiO8e5dFU26j+jrUzEOGrWQ+zuL/jhD4 fAYQ== X-Gm-Message-State: AOJu0YzH12/8pPF+keo/Z4jQbK8zGocCint0OK4HRsep1ajMvkt8rUlF wQm3Ttl/F1Fi3kwoBWcA4n6W8jccsr30D0FTzdE2oGoW80+8aImxoIsGBI9Q X-Gm-Gg: ASbGncs28NbZkJhN9+oHcR1x6+g0u1ETvJSfWkrqVMpBCyFFXhIwHVi9hjGqTOlyktl Tj+xLvS753QnsnR4MbcPSEL7P1W9+J7sg/QnHXxCg08iY6Lr/etsFMwdn5wcVQ/7+cw3vWaylbp tZZss9rzpHVTeGz2QrBBOsexAkER40F7WYAsM8iBctgA1nr9uPa6PZRnyDasvq5fMKzLfTNiyCv XSD4SztP6Wd1/4VhTr0RYN3Da9hE2UDT12XdEexBX7DW+NwCtQN58U3P3geB537kQUXUHTxexUQ ZWd9 X-Google-Smtp-Source: AGHT+IHlscf9pv6r0jiLUseqKU8ejlUSjBA5cQSqFQEHoIoA5Djzp67orLWMyS93LOOrQKsWadji5A== X-Received: by 2002:a17:907:940e:b0:aae:bd36:b198 with SMTP id a640c23a62f3a-ab38b4c6a1emr3326207366b.47.1737964552729; Sun, 26 Jan 2025 23:55:52 -0800 (PST) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab675e11ec2sm542120066b.35.2025.01.26.23.55.51 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 26 Jan 2025 23:55:51 -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" , Rick Edgecombe , stable@vger.kernel.org Subject: [PATCH 1/2] mm/vma: fix gap check for unmapped_area with VM_GROWSDOWN Date: Mon, 27 Jan 2025 07:55:26 +0000 Message-Id: <20250127075527.16614-2-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-Server: rspam07 X-Rspamd-Queue-Id: 7E041C0004 X-Stat-Signature: mmhnhpkx1ippsoebkyoaaw1pjdwzjwob X-Rspam-User: X-HE-Tag: 1737964554-684643 X-HE-Meta: U2FsdGVkX19WfWqrbt5fx+4KS2Rv7KGVGs46seze142Cw6X2UonpETfT4vcUQE4qCEJBBLijMd1Buhr1WKmTqsSnyiGlKzlyThZH5IhRe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JJvlmCmbVwDyw5YMk8oLEtUuxXdCsstRxtFrM/AE4dJsbQdZLSycL52SMZfUwUZ321ANleh73HXzhIhSV7CWk7RO1qCyOTkJqbyiKiJtSB8MCHEd5XtFXYZKS02uX0abWMTg+C7oWS+TFH3CJ+HmmBWoLqmfWqW1olOOf0LoDP2TIfbm490wcXfW9T7NLLYaEKCRuQLEgOCywMBa/ceT+rDpDtsUW+Hjg1STBpBpN6I3hPxBF+T1qihLbdvLLTuqbOZv9cakk70hrjAAS/tzCobGGzXT0fgSQpqfRyjMEYVwn+Jde1LwOtPgE9DQEzrjjsn2n8VInExxxxvCZb4Mns3eeXCUnGrQr5pRrAkFEX+BedxvE/diMJ6GgftJIPDPUs6svsEhWIK3M+wYE+eG57ZkmoAZj8VE0M2a0ng1i41p1ZBgG31w9Uve3g== 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: Current unmapped_area() may fail to get the available area. For example, we have a vma range like below: 0123456789abcdef m m A m m Let's assume start_gap is 0x2000 and stack_guard_gap is 0x1000. And now we are looking for free area with size 0x1000 within [0x2000, 0xd000]. The unmapped_area_topdown() could find address at 0x8000, while unmapped_area() fails. In original code before commit 3499a13168da ("mm/mmap: use maple tree for unmapped_area{_topdown}"), the logic is: * find a gap with total length, including alignment * adjust start address with alignment What we do now is: * find a gap with total length, including alignment * adjust start address with alignment * then compare the left range with total length This is not necessary to compare with total length again after start address adjusted. Also, it is not correct to minus 1 here. This may not trigger an issue in real world, since address are usually aligned with page size. Fixes: 58c5d0d6d522 ("mm/mmap: regression fix for unmapped_area{_topdown}") Signed-off-by: Wei Yang CC: Liam R. Howlett CC: Lorenzo Stoakes CC: Vlastimil Babka CC: Jann Horn CC: Rick Edgecombe Cc: --- mm/vma.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/vma.c b/mm/vma.c index 3f45a245e31b..d82fdbc710b0 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -2668,7 +2668,7 @@ unsigned long unmapped_area(struct vm_unmapped_area_info *info) gap += (info->align_offset - gap) & info->align_mask; tmp = vma_next(&vmi); if (tmp && (tmp->vm_flags & VM_STARTGAP_FLAGS)) { /* Avoid prev check if possible */ - if (vm_start_gap(tmp) < gap + length - 1) { + if (vm_start_gap(tmp) < gap + info->length) { low_limit = tmp->vm_end; vma_iter_reset(&vmi); goto retry; 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.we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cC1FZD42BrzFkQVQ0zcL9TU1RuGX8mjn s1A6zd3Y YAxSMl40MQhavCdA0a+PjZd/BZvFoM2jzwQJC0z8yb1eKcrrAwbsCtIJXQyN6dfO4cJt8tOU8my6NUWwh0f2g3NOGE11iir4bzvKkiAalq0JahZ7rGJCcR/LAeKrNAFx/+cryAx5B+oeHYQdWbigYermfE/6AYvj9FcyBe4NMy4SiQqwN0AiIy5n878HED2yqFP1f0Ui8RZ6i1BTdZQcudv1jdqmk5SupZ3+hIOVi3EjPj9N75DHhaLkpIc9NtdfTcqKOcaSXgkNF4xQaJL9NbVvXRvP2dghykxYFhZkWb4k1Pgy50l1OybWazLYm2GrAW0iese3QsYZom0nqsVIfRtYple2fN5WccpUqceqcHyWFDjJAhJKNXutgxFJ7FDFk0/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)