From patchwork Fri Sep 6 03:07:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13793167 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 EEBCACE7AA5 for ; Fri, 6 Sep 2024 03:09:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 636206B008C; Thu, 5 Sep 2024 23:09:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BCEE6B008A; Thu, 5 Sep 2024 23:09:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 442CE6B008C; Thu, 5 Sep 2024 23:09:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2546E6B0088 for ; Thu, 5 Sep 2024 23:09:25 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C2EA240D65 for ; Fri, 6 Sep 2024 03:09:24 +0000 (UTC) X-FDA: 82532832648.30.FB35692 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by imf26.hostedemail.com (Postfix) with ESMTP id ACBBF140008 for ; Fri, 6 Sep 2024 03:09:21 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UCUMWvQA; spf=pass (imf26.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725592063; 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=10FN7twCLlNXTdK8QQjJaoUiev1Y4bp41Li4mPr2OyE=; b=Fnpr+17pl9xsS3MxN8Lwzkugf4+/Ng2Fqp/NFladNvnAKzFoqUgZQrHDffDh7Hu77+Tk9p IxuVL/c8rrbYhtidi+vlhz5Igx37Azh378iduzDzhHlyXX8+t6KxdIU8Cbx3IGg99BgefO At0fUEj0tyPcV6E/7oDe1TwJJw4dZPU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725592063; a=rsa-sha256; cv=none; b=4kpP/SS5JDNgf46//RjnG/elrC9CqCyUltqMCbDPl6wqg9/LFy4dc9OH2+9DSYf+DChe/Z lxzDXlddTa1v+GqYbka/chN760N1QIDtumlXoqzYP2dvfQ/+TC+W2rTgVGNJqyGs15gTk+ WLAzUDCXjNUTUoIo1rUvMN20kqTOb6k= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UCUMWvQA; spf=pass (imf26.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725592161; x=1757128161; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VYfkWvOLxKs5tnPDOT4ijueKx8g5WPX+vAVaKP8jU2Q=; b=UCUMWvQArr7yj+dc9nwk5s9ei9JpaqZMzVE+elW1zJmwjQJRdrdmpAwo ENKUXKUtv4nYveQnM2EljrPmkMQXR680bzBBjGl8uXcLMFOBfGcwW4beE iIXdcQIbGGZOtidUIECGBXJVPHtV/RtGVUcmg4DG7Z6T/03MN00M1W8YG LpnTaXmQVBrCi+HuhX6WErLnKt3npxL1JH0NgugH+G/FWLy4jqQ/wpjBY XigjkMOJP/IFrlcVXuniXmeAPgB5Yx5lms7LEylX/OVAB+JNe22H5ihNQ g5/NluLN3e9NaojomqtVoLAz3rjoeRmeKfI35f9IQdnP+ezRx1R0A+8FY w==; X-CSE-ConnectionGUID: 7bjBhl2yT76TeevlZuW+fw== X-CSE-MsgGUID: 0xlPJU/3Tr6mYo5UO8QHNQ== X-IronPort-AV: E=McAfee;i="6700,10204,11186"; a="28089461" X-IronPort-AV: E=Sophos;i="6.10,206,1719903600"; d="scan'208";a="28089461" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2024 20:09:21 -0700 X-CSE-ConnectionGUID: 8mcJYT+lQGWTBP+e48C9Ew== X-CSE-MsgGUID: vxdFwImFSbeOefxtS4vhhw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,206,1719903600"; d="scan'208";a="66364375" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.245.242.189]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2024 20:09:17 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, Huang Ying , Dan Williams , David Hildenbrand , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Alistair Popple , Andy Shevchenko , Bjorn Helgaas , Baoquan He Subject: [PATCH -v3 1/3] resource: Fix region_intersects() vs add_memory_driver_managed() Date: Fri, 6 Sep 2024 11:07:11 +0800 Message-Id: <20240906030713.204292-2-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240906030713.204292-1-ying.huang@intel.com> References: <20240906030713.204292-1-ying.huang@intel.com> MIME-Version: 1.0 X-Stat-Signature: hos8tm6st4ggngdhje5ddn3ekrrnixbc X-Rspamd-Queue-Id: ACBBF140008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1725592161-9699 X-HE-Meta: U2FsdGVkX1/Mg9e5R2jbnfE3/X8OLOHlqv0xvcHLUkYuzc2ZbuGZishquCHAp4bZxIeWZrl1SLoXWgO07WpTM2keyBlVzcHUyJqgIBqJ26XpM58c9vYS+WEAKDtoXKLpaYvIx1Q6kjdmj9RF94HzHhwqUXwgflbg03Iv1TQga/HyUCYFAhihV5SBgF4PIlzYuD3LBcVSRI+fXCezlDjWVtUnEWCcXnu8J4cuUhF6V3rec3Se9tKGIBpf8WuzDAa2Gqi1KKi4jZqpOiovu97CGmJ8CPyAul28JHpDW+P4xwsR5+1YN4TTl1krRF7YOcPxFUOlIsr6GSIwWfviJF9TKakzOWFMjX3nTgVMralfMmb9v5mpFp3BQ1MgH+kQaCeW+DwrR7dqq7cvGMpli1l13mojtvEiPYv4h8JUWJtN1OOy8Jlon3qYwnsiMN5re0R1xrXJvygyllPUszaBYTbdfqZLk0mZRyOvzsF9LiKti1oKz96WzfX3CqwMmiFbBatQvgfwqetAl5g9x16mTFalJ34/nFaETUUEPj5SHE57EKzwc+LSMXh9akKpGRGyCeKl03vPZ6mjdfUCL1UbxmwyPfRt/xtYIbFOWbDLPShUlSeJFa1TTr9sMKzKeH5Wjbmd0EcHObnOYjfSEdux6gj/0dlAGoByjt46IwlWneZyR1nPxQMmY29Df3WpMqMESebsnXLE+VrXnL3PTbqg21BaVWU/OVshvhVbgoGHQbMqE8l/6QEdp+OEEpjWQ71URPbMeSsFpEqyMT+Lp4OpP46jJEkhvmDdhjqafvQfR0eU9glVta2ZEzYEqjYn/h76ItIAK6dqhA90cj3OCutzCe7+iO7FaI3zzOiTsksQh0VolvffO1EEw0t7wXcL4YSKmn8jhps4GISXwkiS2mT30kqH+5Mry5oSV3R8rOTa6wTE7XaN8wM8Ir2F5bDykYvzzA+MZRgDWMR1ivAqUXDMixo 34ujRYWn C+VHFQfxSLJCPNMXGVziBKkHzD/Qt88o0g9Q7U6H6x1a1KC65PmnZtDQwYz+WhI86PTTemq4BH0oER/p8yUL9C8get7yba35qOqal3GX4so0CGpUktTXGJi/Kzq5+2YY6ADBfgDuXrknvlVpnWZiA9g2PKL0tGWuC9xRSKkIZrqfRqKjispe7A4CubHAgy0Mvl5fEj5+FdCUSaMgyO3AKZonBsyPx0Z2LIPXPJUuossEStL01pI3iBqOtmkAKKvo+3SM7FdleXPjiwqCXCRskDeQN/lX1rzJypvoQO9o/JHxpXU6cJvqcrN9wE37hxKuDaOGAW1z/alo+wZoHUEAuL9a2Pq9olSLN74+Gub+slbcJS29vw2YnPcnP52LTYuNosCNR 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: On a system with CXL memory, the resource tree (/proc/iomem) related to CXL memory may look like something as follows. 490000000-50fffffff : CXL Window 0 490000000-50fffffff : region0 490000000-50fffffff : dax0.0 490000000-50fffffff : System RAM (kmem) Because drivers/dax/kmem.c calls add_memory_driver_managed() during onlining CXL memory, which makes "System RAM (kmem)" a descendant of "CXL Window X". This confuses region_intersects(), which expects all "System RAM" resources to be at the top level of iomem_resource. This can lead to bugs. For example, when the following command line is executed to write some memory in CXL memory range via /dev/mem, $ dd if=data of=/dev/mem bs=$((1 << 10)) seek=$((0x490000000 >> 10)) count=1 dd: error writing '/dev/mem': Bad address 1+0 records in 0+0 records out 0 bytes copied, 0.0283507 s, 0.0 kB/s the command fails as expected. However, the error code is wrong. It should be "Operation not permitted" instead of "Bad address". More seriously, the /dev/mem permission checking in devmem_is_allowed() passes incorrectly. Although the accessing is prevented later because ioremap() isn't allowed to map system RAM, it is a potential security issue. During command executing, the following warning is reported in the kernel log for calling ioremap() on system RAM. ioremap on RAM at 0x0000000490000000 - 0x0000000490000fff WARNING: CPU: 2 PID: 416 at arch/x86/mm/ioremap.c:216 __ioremap_caller.constprop.0+0x131/0x35d Call Trace: memremap+0xcb/0x184 xlate_dev_mem_ptr+0x25/0x2f write_mem+0x94/0xfb vfs_write+0x128/0x26d ksys_write+0xac/0xfe do_syscall_64+0x9a/0xfd entry_SYSCALL_64_after_hwframe+0x4b/0x53 The details of command execution process are as follows. In the above resource tree, "System RAM" is a descendant of "CXL Window 0" instead of a top level resource. So, region_intersects() will report no System RAM resources in the CXL memory region incorrectly, because it only checks the top level resources. Consequently, devmem_is_allowed() will return 1 (allow access via /dev/mem) for CXL memory region incorrectly. Fortunately, ioremap() doesn't allow to map System RAM and reject the access. So, region_intersects() needs to be fixed to work correctly with the resource tree with "System RAM" not at top level as above. To fix it, if we found a unmatched resource in the top level, we will continue to search matched resources in its descendant resources. So, we will not miss any matched resources in resource tree anymore. In the new implementation, an example resource tree |------------- "CXL Window 0" ------------| |-- "System RAM" --| will behave similar as the following fake resource tree for region_intersects(, IORESOURCE_SYSTEM_RAM, ), |-- "System RAM" --||-- "CXL Window 0a" --| Where "CXL Window 0a" is part of the original "CXL Window 0" that isn't covered by "System RAM". Fixes: c221c0b0308f ("device-dax: "Hotplug" persistent memory for use like normal RAM") Signed-off-by: "Huang, Ying" Cc: Dan Williams Cc: David Hildenbrand Cc: Davidlohr Bueso Cc: Jonathan Cameron Cc: Dave Jiang Cc: Alison Schofield Cc: Vishal Verma Cc: Ira Weiny Cc: Alistair Popple Cc: Andy Shevchenko Cc: Bjorn Helgaas Cc: Baoquan He --- kernel/resource.c | 58 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index 14777afb0a99..235dc77f8add 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -540,20 +540,62 @@ static int __region_intersects(struct resource *parent, resource_size_t start, size_t size, unsigned long flags, unsigned long desc) { - struct resource res; + resource_size_t ostart, oend; int type = 0; int other = 0; - struct resource *p; + struct resource *p, *dp; + bool is_type, covered; + struct resource res; res.start = start; res.end = start + size - 1; for (p = parent->child; p ; p = p->sibling) { - bool is_type = (((p->flags & flags) == flags) && - ((desc == IORES_DESC_NONE) || - (desc == p->desc))); - - if (resource_overlaps(p, &res)) - is_type ? type++ : other++; + if (!resource_overlaps(p, &res)) + continue; + is_type = (p->flags & flags) == flags && + (desc == IORES_DESC_NONE || desc == p->desc); + if (is_type) { + type++; + continue; + } + /* + * Continue to search in descendant resources as if the + * matched descendant resources cover some ranges of 'p'. + * + * |------------- "CXL Window 0" ------------| + * |-- "System RAM" --| + * + * will behave similar as the following fake resource + * tree when searching "System RAM". + * + * |-- "System RAM" --||-- "CXL Window 0a" --| + */ + covered = false; + ostart = max(res.start, p->start); + oend = min(res.end, p->end); + for_each_resource(p, dp, false) { + if (!resource_overlaps(dp, &res)) + continue; + is_type = (dp->flags & flags) == flags && + (desc == IORES_DESC_NONE || desc == dp->desc); + if (is_type) { + type++; + /* + * Range from 'ostart' to 'dp->start' + * isn't covered by matched resource. + */ + if (dp->start > ostart) + break; + if (dp->end >= oend) { + covered = true; + break; + } + /* Remove covered range */ + ostart = max(ostart, dp->end + 1); + } + } + if (!covered) + other++; } if (type == 0) From patchwork Fri Sep 6 03:07:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13793168 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 BFEA1CE7AA5 for ; Fri, 6 Sep 2024 03:09:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AD9B6B008A; Thu, 5 Sep 2024 23:09:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 45CBC6B0092; Thu, 5 Sep 2024 23:09:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E1D56B0093; Thu, 5 Sep 2024 23:09:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0F4796B008A for ; Thu, 5 Sep 2024 23:09:29 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id B7EFE40D4B for ; Fri, 6 Sep 2024 03:09:28 +0000 (UTC) X-FDA: 82532832816.14.CDBCFFD Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by imf21.hostedemail.com (Postfix) with ESMTP id 935DB1C000C for ; Fri, 6 Sep 2024 03:09:26 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=l+3wBS7B; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725592094; a=rsa-sha256; cv=none; b=z6d0Ywx4nfTSr8cefNihJCbLG4GYEzExxjiHmbfLBpolCXAOqT4tAFw+iPan2Wut6U7ljP friBr4oPube7CmtZCpJhuFfJ/2vmYLpBZCWTGLTLyEO/MHTYxK4sDxwn6a3oCLqBfKihHw 9cljB5oXORS6Xhcnz62rq9zQkKfogBw= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=l+3wBS7B; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725592094; 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=z+EHTFnjwcKj608X0cDnnlPS8vNfF3r+0nwUHhMlex4=; b=ZBlBRwSzAXiWuKSCeITaTXz6IbIs9MXwad4JfDKyJrcDGXIGYHRmHnYNxSsPHNf2he1MTX MIUagCueIwEDbP7IxsrdYpKAPTdS8syY2smadplUJRBZqb4Stix3BZPUitkmVzF6/qgdp4 gdFGGNwZAMy4UEk3LVa+8krCfXMD1TA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725592166; x=1757128166; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3Ij6u3xpNnYxJGL++yR1zK7P57z01gPw8JrpH/RINL8=; b=l+3wBS7BTVWD1jLQiKPgjPRUR36yDLfLkASuPUn3dN2YM50UszGXU3wb SdZudGjwbnZ7NL2PKUQjABem5b7J632wF8R90j4Wq8OklUXxPHQ2vuX10 hJzum5v27aIdRKgJdGsRn5D6N2xSp3n5FtrxGIbMofgxESHUg0vpDv75A JwRe2Dpnv+j1s4IDD6W0uP7Pbcy3jEgH9OEi4mC+/oQKxCxP80Aou6aur 4LFBnUnmqcAfYP8h08KTrEQ6FesqbrB0NERYsfCADmI1BpEWccDMZ2ozh apFvOLeWruXGlXWN7HyISk+vB9buZxbLu1n+5IpEI5L+brZiHw6smbti8 A==; X-CSE-ConnectionGUID: X5QO7GkfQmuMD79Wi54EHA== X-CSE-MsgGUID: u3gt3v4cSVuQcjBSLZo24Q== X-IronPort-AV: E=McAfee;i="6700,10204,11186"; a="28089475" X-IronPort-AV: E=Sophos;i="6.10,206,1719903600"; d="scan'208";a="28089475" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2024 20:09:25 -0700 X-CSE-ConnectionGUID: nKPr5kD7TPSkUShYudxjNw== X-CSE-MsgGUID: zSY50cl/Tb2/KgutidFYzQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,206,1719903600"; d="scan'208";a="66364396" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.245.242.189]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2024 20:09:22 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, Huang Ying , Dan Williams , David Hildenbrand , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Alistair Popple , Andy Shevchenko , Bjorn Helgaas , Baoquan He Subject: [PATCH -v3 2/3] resource: Make alloc_free_mem_region() works for iomem_resource Date: Fri, 6 Sep 2024 11:07:12 +0800 Message-Id: <20240906030713.204292-3-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240906030713.204292-1-ying.huang@intel.com> References: <20240906030713.204292-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 935DB1C000C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: nnbf8kup9ogu1cr8idbg8kjsz91fuiku X-HE-Tag: 1725592166-944617 X-HE-Meta: U2FsdGVkX19ghxG2rKhEouP43RppW/l1Nzthm2cTS0Jyqk7tqbcqZIQPzROSgU0O4aMuzKh+bRQMbQIhIg7myMgNeWwJIMKwcp7KwfWsuzXyleonCaJiHpJmljTD4xNQCZFmslbtj7TwUkp+pL3ugiFVJPB7ol/yiVXX05vs+Ssf+u1AGn0mskqlsay6VFDNiHWpu4s/66urq+yWFPTcHf727nOeC0q2jNpH5GHUq5zxGIanYKIAhi90vPzEkNcBuWNbAR2NR8kkmXYbIGwhcWIIdkv0RJwSrvvnDcooWMFP/7rzDEhmLxjWeRkFRxlXmpGFmI0Z6bImLcbbDaTYmG1zJ4v+MzB4URlRiNN/tUTklc1yBtG0yU4/wyRzGRrnRN19ra/FIlWvhmD/Lf+P8EA6wGQ3jvEVySfjZx2proPYnt4uHA/naHCY/tfRihfn9nH1b1uF8iW7by+rag88+sB6hnYxoFXYUjjwbXk0rpE9t6dcZ1NDF2G/AmFcWoWX2p6mY3U/ksLDsaL92b03rYLwMaxJ195e3f0qoUa6291caNiTVSjUDiz8Vo94sor4EOEYsb72IgIZxF7X5XWLw7WCamXMCesy+MtzCk1BAYnuUf6YgdrvdfIRmkcMP1saItbjTIClfVLc4uw4SZm2/mJ3FSL/N0N9O1BvpPxVnazsN39F9I2bw14bOHEw6tw5yKQzYaGDyKvPbZvRPlHy2PM5E5hy8RrYDgry1a8r/IEBWnfUtr1yHHK36BekWpC2TFtDaeeUCHPkE3M8OOZsSSjvtDj9hPQCm4d0cpl61RqlfNKYvDhCxw5v0us4Vmj6zo82MgKLbroc83zVGXQucb7EhQ8H2B9kOsXejdzkHHJMGc7tWY4ztGe1UTXYxcPmGCxK1hz9MUKr2uJUv1pISq587c8LmMdSAZS+wrPUGCrz4PUjaI2aEDvgSRHHYrGB3zhuvfpoEVTtWUMHu+c s0yFJK1I NiwTkHoT2z9E1KM2X2R4gU0azbfqyhnd1II9FF3bDfhzz1Tz8N508KDRTrLzukxal79p8idyKXluQZ3IvjOeK8PdS/Zs5cynB4570CP05FQMABPnjrD9KsHPwCzBcqZRIgrRZPOMHOJjrr6fmrnPCqmj9Q3o1sY+4i/vzd48ii+4EUK6hF5njfz1Vf/Pu0sirGaq/4pn2hWusv1JMiLk4jltt3b523j88Hkg5/iVh8QUzK1oM3omfFa7byglJhMZ41RzDV/oszl4cz8bJq8seuhPKE3dk+Ohlqx2H+nTo1FeLI99APQjmWQcgPbPQmyQW9MiyQR2nWKr/Y6lz2L0xvElFZHBvsxt2b7aJcOzexW9VTwSk7EFeVhPigA== 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: During developing a kunit test case for region_intersects(), some fake resources need to be inserted into iomem_resource. To do that, a resource hole needs to be found first in iomem_resource. However, alloc_free_mem_region() cannot work for iomem_resource now. Because the start address to check cannot be 0 to detect address wrapping 0 in gfr_continue(), while iomem_resource.start == 0. To make alloc_free_mem_region() works for iomem_resource, gfr_start() is changed to avoid to return 0 even if base->start == 0. We don't need to check 0 as start address. Signed-off-by: "Huang, Ying" Cc: Dan Williams Cc: David Hildenbrand Cc: Davidlohr Bueso Cc: Jonathan Cameron Cc: Dave Jiang Cc: Alison Schofield Cc: Vishal Verma Cc: Ira Weiny Cc: Alistair Popple Cc: Andy Shevchenko Cc: Bjorn Helgaas Cc: Baoquan He --- kernel/resource.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/resource.c b/kernel/resource.c index 235dc77f8add..035ef16c1a66 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1873,7 +1873,7 @@ static resource_size_t gfr_start(struct resource *base, resource_size_t size, return end - size + 1; } - return ALIGN(base->start, align); + return ALIGN(max(base->start, align), align); } static bool gfr_continue(struct resource *base, resource_size_t addr, From patchwork Fri Sep 6 03:07:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13793169 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 980C7CE7AA6 for ; Fri, 6 Sep 2024 03:09:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DA3F6B0096; Thu, 5 Sep 2024 23:09:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 262C86B0098; Thu, 5 Sep 2024 23:09:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DACC6B0099; Thu, 5 Sep 2024 23:09:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id E31BA6B0096 for ; Thu, 5 Sep 2024 23:09:32 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 954751C5194 for ; Fri, 6 Sep 2024 03:09:32 +0000 (UTC) X-FDA: 82532832984.04.5C9B625 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) by imf21.hostedemail.com (Postfix) with ESMTP id 5ED0D1C0008 for ; Fri, 6 Sep 2024 03:09:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=AEEnVWIM; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725592098; a=rsa-sha256; cv=none; b=SkPRLLUm48SYv7PVazWIKlaRlNOwgproU/IGAT+jFifZy1YqsDQP+8HAU6xyje4bwPPIWT 2+EfG6oFDd5dP/5NfCuoncTpgW9/MEsTXH51tCnOQwehUn6bVfChTSYmJk4zjp/bsysZFe UH0cvJKQZ1FZPqDhoK3dVIE2B5HIrjk= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=AEEnVWIM; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf21.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.15 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725592098; 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=3wQRUy3r1S8VBsOIXEbVAGjehdR3c56i1MbhTZZrqC0=; b=2sWrt1Zfa72BiSRqDJW6bIs9k3yPTvBB9BCE81FigGlrbsNQ4Aq1b/VTqGucWkROpaNPcg 0dBxqn49q9WHqeh+By98cfcdO0pnBOmJ6kUdGAMw/OMv1DGnAkqNk3YKv2EO7k5EBzK/xY OvFqdi5Ne4xrwL+Lj90NKWrOoSy7RpQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1725592170; x=1757128170; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PSFihkZgLasjDkyToU3y/y2OCLAaD/A4Hx/YphgS1CM=; b=AEEnVWIMPpnKFei5Scmnq1d3zHfG3iUZ2AMO92t0DOy5CDhZtLspIJ4S XOPF3y3yRzK67PvEaMRm3vyhsK3wCXDG9zXJKcYR7Um/JF6uZduK8F/MD 4/gz/JQZzk9/9KBFd2hT7oohUlG/yezRTfxTp3udBlfAOevsLV6a9tJv5 3jwu+Qf4+Ym3HHgMX4zmlsvoKkQ6tKSP2cX3T0D0viEP01kx1rXEPqCES gLVW/iuan3S4cEceZqMiyV3we39eGD1TpSuuqm1AGWbsAjw23KiOZw3e7 9dLdubMmhzxJZTGMPUk+D5UaxGNBHGxt2IDLf0OsANrz6Se/MfdBd8Lkm g==; X-CSE-ConnectionGUID: MKJW16GSROSo4mR0JHR/FA== X-CSE-MsgGUID: tFFRlOAfQ0mUHCuD4XsH4g== X-IronPort-AV: E=McAfee;i="6700,10204,11186"; a="28089489" X-IronPort-AV: E=Sophos;i="6.10,206,1719903600"; d="scan'208";a="28089489" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2024 20:09:30 -0700 X-CSE-ConnectionGUID: x+ooRBcEQECl/064bwq7yg== X-CSE-MsgGUID: 64nkZDu+REefnITy+IXeiw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,206,1719903600"; d="scan'208";a="66364412" Received: from unknown (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.245.242.189]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Sep 2024 20:09:26 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org, Huang Ying , Dan Williams , David Hildenbrand , Davidlohr Bueso , Jonathan Cameron , Dave Jiang , Alison Schofield , Vishal Verma , Ira Weiny , Alistair Popple , Andy Shevchenko , Bjorn Helgaas , Baoquan He Subject: [PATCH -v3 3/3] resource, kunit: Add test case for region_intersects() Date: Fri, 6 Sep 2024 11:07:13 +0800 Message-Id: <20240906030713.204292-4-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240906030713.204292-1-ying.huang@intel.com> References: <20240906030713.204292-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 5ED0D1C0008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: gkw6x3xzmrjffyz8tgzzmodf5aqoqf4o X-HE-Tag: 1725592170-992735 X-HE-Meta: U2FsdGVkX18W9wcxTr08HuLTHsme5NvPney/c1glbFsxDZMoTEhw8XmnjJlaMuCAdO3DxYhDJvFyIa4Hy85yzmNbeZlGL6mQLV6JcRdF/eu2Dct63yP68XGoRCjiRmRZJCGexpBbpJM+UqxukxV438SNUWfODodKUp/GxCp/nTBm4+7HkhlbRtDmn2lj6iqgEpwVxoiw9BUgUS4/SaEf2eJ10912QDtd42EfawTRwNPMGrYPIkQOC+s8OnSiR7KBgWK3zrD2nxfG5ZefqFmDhldy/SO9VLCJS53pQWSPodpABIy5o/naute/ZnQsi5v9nLfcGXRwd1v6I1+Px3sC37yAsQXQwr9G5St4pAseVTU0o+gRHsXnMZSxxKiWb0CKDNIfLBb5c2qYlSVaPxm8wFeeE0eV1Bv21qXI6ipS5It6IjGmvSnX1R8xRajJBqqa3InUXHDc8QOs6EJpI7ynqARwqYM0b0325fSuYi2X744SL/w8ncXfR/pOBpQ4wGQ1LQUhaH4JYrVJaxFU5PkwzrGAVYTQggPbmzvXp2MacchTIE6MaCByXP94I/hCxzOBedqySDbiHVAkAMJimvLWZxJmMPeZohpTNxpCna3l3fRK5eH6/9xp/nfTKwxKyYW1mobYJrEqVno2jsYeo1j2yhOi7I5ErysHt3Ln1xXN2F/gBkivr4OpLbL+Z6ksOicvVG7zMkuxDiEvKAastmFLwM/nieSUc9pgP8VBPajtlvR7o//3yE2qPCs+k75oRYWAyhlA5txnzS+9JllRtdCpZE0j0qXdxE2cQhPqQwrb1XE8lVLzBqdPtpIVzzJlmMXtjrU3soGCdlEB5DYJrbybMf+oG1q0r/dd9wtjMY7M6Bmd7cCN4xY7QaZt2KtyCHAQMc2wxaL3NkekesvoCx/n8F2DL0gYhDEgEiaJzu93nDiaI4zmyfUKvskr6wQq/enzXmA3kIMAD+vvHvzmyrh y/GkmUeK DTnecf2a1OboL3oy7ntxF5w8km9PqL7+ZEaN2zh+HUff+k1Utr9QAF0hUh8aO2MdyHCGpQHjHYhaRpmPO2I6auEWJC3FqobTnKFEvjHI0lFsCtkEvpiIi0MNkhU6dlV4xQBvI0yoHBIy8Kz0lV6WFLDLIJJQRq0JmU2SIYK3NH98zW4tQFzuw+/hXWpxwf/MC2Zk2KaH+FfRIr/sthbQuakmRgUi7Sxn9fFWFv6ZhLNgLK7/voiEYpA4GvOlJn27diWNHTTnjpVTyW3RD0naX4/Wo0ibnv5x1A2b0x5j9Rw8fF6/008Xdlw8L19uX+dPdokHnQe8vremRn7fxRE6NkS5pgCjEB52DV33AzrKpaXMjELfyjZYdZot/T6MHKehatj/J 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: region_intersects() is important because it's used for /dev/mem permission checking. To avoid possible bug of region_intersects() in the future, a kunit test case for region_intersects() is added. Signed-off-by: "Huang, Ying" Cc: Dan Williams Cc: David Hildenbrand Cc: Davidlohr Bueso Cc: Jonathan Cameron Cc: Dave Jiang Cc: Alison Schofield Cc: Vishal Verma Cc: Ira Weiny Cc: Alistair Popple Cc: Andy Shevchenko Cc: Bjorn Helgaas Cc: Baoquan He --- kernel/resource.c | 20 ++++-- kernel/resource_kunit.c | 143 ++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 1 + 3 files changed, 158 insertions(+), 6 deletions(-) diff --git a/kernel/resource.c b/kernel/resource.c index 035ef16c1a66..e6d222fb0f84 100644 --- a/kernel/resource.c +++ b/kernel/resource.c @@ -1860,7 +1860,17 @@ EXPORT_SYMBOL(resource_list_free); #ifdef CONFIG_GET_FREE_REGION #define GFR_DESCENDING (1UL << 0) #define GFR_REQUEST_REGION (1UL << 1) -#define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT) +#ifdef PA_SECTION_SHIFT +#define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT) +#else +#define GFR_DEFAULT_ALIGN PAGE_SIZE +#endif + +#ifdef MAX_PHYSMEM_BITS +#define MAX_PHYS_ADDR ((1ULL << MAX_PHYSMEM_BITS) - 1) +#else +#define MAX_PHYS_ADDR (-1ULL) +#endif static resource_size_t gfr_start(struct resource *base, resource_size_t size, resource_size_t align, unsigned long flags) @@ -1868,8 +1878,7 @@ static resource_size_t gfr_start(struct resource *base, resource_size_t size, if (flags & GFR_DESCENDING) { resource_size_t end; - end = min_t(resource_size_t, base->end, - (1ULL << MAX_PHYSMEM_BITS) - 1); + end = min_t(resource_size_t, base->end, MAX_PHYS_ADDR); return end - size + 1; } @@ -1886,8 +1895,7 @@ static bool gfr_continue(struct resource *base, resource_size_t addr, * @size did not wrap 0. */ return addr > addr - size && - addr <= min_t(resource_size_t, base->end, - (1ULL << MAX_PHYSMEM_BITS) - 1); + addr <= min_t(resource_size_t, base->end, MAX_PHYS_ADDR); } static resource_size_t gfr_next(resource_size_t addr, resource_size_t size, @@ -2048,7 +2056,7 @@ struct resource *alloc_free_mem_region(struct resource *base, return get_free_mem_region(NULL, base, size, align, name, IORES_DESC_NONE, flags); } -EXPORT_SYMBOL_NS_GPL(alloc_free_mem_region, CXL); +EXPORT_SYMBOL_GPL(alloc_free_mem_region); #endif /* CONFIG_GET_FREE_REGION */ static int __init strict_iomem(char *str) diff --git a/kernel/resource_kunit.c b/kernel/resource_kunit.c index 0e509985a44a..42d2d8d20f5d 100644 --- a/kernel/resource_kunit.c +++ b/kernel/resource_kunit.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #define R0_START 0x0000 #define R0_END 0xffff @@ -137,9 +139,150 @@ static void resource_test_intersection(struct kunit *test) } while (++i < ARRAY_SIZE(results_for_intersection)); } +/* + * The test resource tree for region_intersects() test: + * + * BASE-BASE+1M-1 : Test System RAM 0 + * # hole 0 (BASE+1M-BASE+2M) + * BASE+2M-BASE+3M-1 : Test CXL Window 0 + * BASE+3M-BASE+4M-1 : Test System RAM 1 + * BASE+4M-BASE+7M-1 : Test CXL Window 1 + * BASE+4M-BASE+5M-1 : Test System RAM 2 + * BASE+4M+128K-BASE+4M+256K-1: Test Code + * BASE+5M-BASE+6M-1 : Test System RAM 3 + */ +#define RES_TEST_RAM0_OFFSET 0 +#define RES_TEST_RAM0_SIZE SZ_1M +#define RES_TEST_HOLE0_OFFSET (RES_TEST_RAM0_OFFSET + RES_TEST_RAM0_SIZE) +#define RES_TEST_HOLE0_SIZE SZ_1M +#define RES_TEST_WIN0_OFFSET (RES_TEST_HOLE0_OFFSET + RES_TEST_HOLE0_SIZE) +#define RES_TEST_WIN0_SIZE SZ_1M +#define RES_TEST_RAM1_OFFSET (RES_TEST_WIN0_OFFSET + RES_TEST_WIN0_SIZE) +#define RES_TEST_RAM1_SIZE SZ_1M +#define RES_TEST_WIN1_OFFSET (RES_TEST_RAM1_OFFSET + RES_TEST_RAM1_SIZE) +#define RES_TEST_WIN1_SIZE (SZ_1M * 3) +#define RES_TEST_RAM2_OFFSET RES_TEST_WIN1_OFFSET +#define RES_TEST_RAM2_SIZE SZ_1M +#define RES_TEST_CODE_OFFSET (RES_TEST_RAM2_OFFSET + SZ_128K) +#define RES_TEST_CODE_SIZE SZ_128K +#define RES_TEST_RAM3_OFFSET (RES_TEST_RAM2_OFFSET + RES_TEST_RAM2_SIZE) +#define RES_TEST_RAM3_SIZE SZ_1M +#define RES_TEST_TOTAL_SIZE ((RES_TEST_WIN1_OFFSET + RES_TEST_WIN1_SIZE)) + +static void remove_free_resource(void *ctx) +{ + struct resource *res = (struct resource *)ctx; + + remove_resource(res); + kfree(res); +} + +static void resource_test_request_region(struct kunit *test, struct resource *parent, + resource_size_t start, resource_size_t size, + const char *name, unsigned long flags) +{ + struct resource *res; + + res = __request_region(parent, start, size, name, flags); + KUNIT_ASSERT_NOT_NULL(test, res); + kunit_add_action_or_reset(test, remove_free_resource, res); +} + +static void resource_test_insert_resource(struct kunit *test, struct resource *parent, + resource_size_t start, resource_size_t size, + const char *name, unsigned long flags) +{ + struct resource *res; + + res = kzalloc(sizeof(*res), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, res); + + res->name = name; + res->start = start; + res->end = start + size - 1; + res->flags = flags; + if (insert_resource(parent, res)) { + kfree(res); + KUNIT_FAIL_AND_ABORT(test, "Fail to insert resource %pR\n", res); + } + + kunit_add_action_or_reset(test, remove_free_resource, res); +} + +static void resource_test_region_intersects(struct kunit *test) +{ + unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; + struct resource *parent; + resource_size_t start; + + /* Find an iomem_resource hole to hold test resources */ + parent = alloc_free_mem_region(&iomem_resource, RES_TEST_TOTAL_SIZE, SZ_1M, + "test resources"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, parent); + start = parent->start; + kunit_add_action_or_reset(test, remove_free_resource, parent); + + resource_test_request_region(test, parent, start + RES_TEST_RAM0_OFFSET, + RES_TEST_RAM0_SIZE, "Test System RAM 0", flags); + resource_test_insert_resource(test, parent, start + RES_TEST_WIN0_OFFSET, + RES_TEST_WIN0_SIZE, "Test CXL Window 0", + IORESOURCE_MEM); + resource_test_request_region(test, parent, start + RES_TEST_RAM1_OFFSET, + RES_TEST_RAM1_SIZE, "Test System RAM 1", flags); + resource_test_insert_resource(test, parent, start + RES_TEST_WIN1_OFFSET, + RES_TEST_WIN1_SIZE, "Test CXL Window 1", + IORESOURCE_MEM); + resource_test_request_region(test, parent, start + RES_TEST_RAM2_OFFSET, + RES_TEST_RAM2_SIZE, "Test System RAM 2", flags); + resource_test_insert_resource(test, parent, start + RES_TEST_CODE_OFFSET, + RES_TEST_CODE_SIZE, "Test Code", flags); + resource_test_request_region(test, parent, start + RES_TEST_RAM3_OFFSET, + RES_TEST_RAM3_SIZE, "Test System RAM 3", flags); + kunit_release_action(test, remove_free_resource, parent); + + KUNIT_EXPECT_EQ(test, REGION_INTERSECTS, + region_intersects(start + RES_TEST_RAM0_OFFSET, PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_INTERSECTS, + region_intersects(start + RES_TEST_RAM0_OFFSET + + RES_TEST_RAM0_SIZE - PAGE_SIZE, 2 * PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_DISJOINT, + region_intersects(start + RES_TEST_HOLE0_OFFSET, PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_DISJOINT, + region_intersects(start + RES_TEST_HOLE0_OFFSET + + RES_TEST_HOLE0_SIZE - PAGE_SIZE, 2 * PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_MIXED, + region_intersects(start + RES_TEST_WIN0_OFFSET + + RES_TEST_WIN0_SIZE - PAGE_SIZE, 2 * PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_INTERSECTS, + region_intersects(start + RES_TEST_RAM1_OFFSET + + RES_TEST_RAM1_SIZE - PAGE_SIZE, 2 * PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_INTERSECTS, + region_intersects(start + RES_TEST_RAM2_OFFSET + + RES_TEST_RAM2_SIZE - PAGE_SIZE, 2 * PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_INTERSECTS, + region_intersects(start + RES_TEST_CODE_OFFSET, PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_INTERSECTS, + region_intersects(start + RES_TEST_RAM2_OFFSET, + RES_TEST_RAM2_SIZE + PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); + KUNIT_EXPECT_EQ(test, REGION_MIXED, + region_intersects(start + RES_TEST_RAM3_OFFSET, + RES_TEST_RAM3_SIZE + PAGE_SIZE, + IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE)); +} + static struct kunit_case resource_test_cases[] = { KUNIT_CASE(resource_test_union), KUNIT_CASE(resource_test_intersection), + KUNIT_CASE(resource_test_region_intersects), {} }; diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index a30c03a66172..383453cbf4af 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2616,6 +2616,7 @@ config RESOURCE_KUNIT_TEST tristate "KUnit test for resource API" if !KUNIT_ALL_TESTS depends on KUNIT default KUNIT_ALL_TESTS + select GET_FREE_REGION help This builds the resource API unit test. Tests the logic of API provided by resource.c and ioport.h.