From nobody Tue May 03 14:04:41 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4086F1ABA0A0; Tue, 3 May 2022 14:04:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Kt1tK558Zz3RDH; Tue, 3 May 2022 14:04:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651586682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oRYWJznRxDkcuGA0Cg0+h7KO1fKYaZIgx3O578HyIe0=; b=ZSQQ/nbCFHVKv1YQUgA/ap0/zhdBfR8C+71CKGTn4iDtbiUJzVgRs6GeuGF+HbrfgX8BBc FBmGqIpx2XiXRVc+fo/H6996BKPk8YElNVJBjEjMJyQ3G56uUwoKg4ItFcNV38cpEEkyNH Y7JehcOXZOE7BgwoFAeINDKU4EOKyJcVShBuCYTpeNw+re+8CHXBTt+PSN9soSQolfq2n3 aT4wvVc5HGM9VS41mb1O6/EEmuPjlp8CiryM9hSwKSEgsQtqx3FU1ERAofPtdTyEMPu78n vIME+RhKUyeFb9jv015aDQ271iZvxiuCWiZk8zXjeA2scws1P5sq67itOST2NA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 79D381F9C5; Tue, 3 May 2022 14:04:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 243E4fK5077850; Tue, 3 May 2022 14:04:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 243E4fio077849; Tue, 3 May 2022 14:04:41 GMT (envelope-from git) Date: Tue, 3 May 2022 14:04:41 GMT Message-Id: <202205031404.243E4fio077849@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Andrew Turner Subject: git: fc5ab0227bba - stable/13 - Handle non-page aligned/sized memory in physmem List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: andrew X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: fc5ab0227bbaa265aa8e4e0247cf816040ac4b44 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1651586682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=oRYWJznRxDkcuGA0Cg0+h7KO1fKYaZIgx3O578HyIe0=; b=aavSf7CkPadtfhMnCJ6DTCuxrK6AefPCi9qwTR8sfjT940qSEYyyA99KgObCgloFeHg8zx J6UCb4HsCcGjNUdB/KBWhGAlaSYlaISp+6sYi8sUMC9uPTlLAWH7EMFvakDsvpU58HuE+1 dj2vyq1YR9WkRUwdd1rX20zAgFCXo7AJfb3YvdSjfr/3ihbCjLPldQfARlJY+meaXCx0pj G7y429hU0zxCzAzLDU5HgrgsuN8oWB3qRGUQL3ic8jVvM30Em+vhHjwJuYRFT0GRAokB7f dKS3LqvOojvzAcNOZMkKbExMYHsBmvWixgqYEXPZKsi9+4nFRmRcfV1LO0zPmQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1651586682; a=rsa-sha256; cv=none; b=bxTj0bMY+9GMCejrFoaIkg359VTMTmIJWoYAhPc90oebMIjhhvFV/NfXQi+AHX9vg0cq2n UGLTJYAGIbxN7+Xyb5QwOVz+RwZbEXf2yrHt+fli1UsrHHfKu7O+WPl6uuV8/NNJP40epl 8Zs8lxUfXbh5VLKMADGJ18dr3DSmMZbpc84xTeEMkLq9gZE8xDdSecYAxAvDz1tVc9grcJ pgs8LgJiK+OvSu6ctHND/55jIyMESqHaexqZVKYQCsi2vJ0DVZFcW9qx4qjqJ0ry3U6KJQ xHUn5ATY3fJSuQFYUbBvKNIe26aeixjj0jmfOWQDizdhYEh0vq3h4k85PWObiw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=fc5ab0227bbaa265aa8e4e0247cf816040ac4b44 commit fc5ab0227bbaa265aa8e4e0247cf816040ac4b44 Author: Andrew Turner AuthorDate: 2022-03-28 11:37:09 +0000 Commit: Andrew Turner CommitDate: 2022-05-03 14:04:04 +0000 Handle non-page aligned/sized memory in physmem In some configurations the firmware may pass memory regions that are not page sized or aligned, e.g. when using 16k pages on arm64. If this is the case we will calculate many small regions because the alignment is applied before being inserted. As we round the start up and end down this will leave a 1 page hole between what should have been a single region. Fix by keeping the original alignment until we are just about to insert the region into the avail array. Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D34694 (cherry picked from commit d8bff5b67c9b6a85f409a7408e45e7e87d39c5a2) --- sys/kern/subr_physmem.c | 17 ++++------------- tests/sys/kern/subr_physmem_test.c | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/sys/kern/subr_physmem.c b/sys/kern/subr_physmem.c index 106200dd814e..99b4c2141612 100644 --- a/sys/kern/subr_physmem.c +++ b/sys/kern/subr_physmem.c @@ -180,7 +180,7 @@ regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail, uint64_t maxphyssz, long *pavail, long *prealmem) { size_t acnt, exi, hwi; - uint64_t end, start, xend, xstart; + uint64_t adj, end, start, xend, xstart; long availmem, totalmem; const struct region *exp, *hwp; uint64_t availsz; @@ -190,8 +190,9 @@ regions_to_avail(vm_paddr_t *avail, uint32_t exflags, size_t maxavail, availsz = 0; acnt = 0; for (hwi = 0, hwp = hwregions; hwi < hwcnt; ++hwi, ++hwp) { - start = hwp->addr; - end = hwp->size + start; + adj = round_page(hwp->addr) - hwp->addr; + start = round_page(hwp->addr); + end = trunc_page(hwp->size + adj) + start; totalmem += atop((vm_offset_t)(end - start)); for (exi = 0, exp = exregions; exi < excnt; ++exi, ++exp) { /* @@ -337,8 +338,6 @@ insert_region(struct region *regions, size_t rcnt, vm_paddr_t addr, void physmem_hardware_region(uint64_t pa, uint64_t sz) { - vm_offset_t adj; - /* * Filter out the page at PA 0x00000000. The VM can't handle it, as * pmap_extract() == 0 means failure. @@ -371,14 +370,6 @@ physmem_hardware_region(uint64_t pa, uint64_t sz) sz -= 1024 * 1024; } - /* - * Round the starting address up to a page boundary, and truncate the - * ending page down to a page boundary. - */ - adj = round_page(pa) - pa; - pa = round_page(pa); - sz = trunc_page(sz - adj); - if (sz > 0 && hwcnt < nitems(hwregions)) hwcnt = insert_region(hwregions, hwcnt, pa, sz, 0); } diff --git a/tests/sys/kern/subr_physmem_test.c b/tests/sys/kern/subr_physmem_test.c index a562bacec65c..10824f7fe4ff 100644 --- a/tests/sys/kern/subr_physmem_test.c +++ b/tests/sys/kern/subr_physmem_test.c @@ -76,14 +76,12 @@ ATF_TC_BODY(hwregion, tc) ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); ATF_CHECK_EQ(avail[1], 6 * PAGE_SIZE); -#ifdef notyet /* This doesn't currently work */ /* Add the remaining part of the page */ physmem_hardware_region(6 * PAGE_SIZE + PAGE_SIZE / 2, PAGE_SIZE / 2); len = physmem_avail(avail, 4); ATF_CHECK_EQ(len, 2); ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE); ATF_CHECK_EQ(avail[1], 7 * PAGE_SIZE); -#endif } ATF_TC_WITHOUT_HEAD(hwregion_exclude); @@ -113,10 +111,30 @@ ATF_TC_BODY(hwregion_exclude, tc) ATF_CHECK_EQ(avail[3], 7 * PAGE_SIZE); } +ATF_TC_WITHOUT_HEAD(hwregion_unordered); +ATF_TC_BODY(hwregion_unordered, tc) +{ + vm_paddr_t avail[4]; + size_t len; + + /* Add a partial page */ + physmem_hardware_region(PAGE_SIZE, PAGE_SIZE / 2); + /* Add a full page not touching the previous */ + physmem_hardware_region( 2 * PAGE_SIZE, PAGE_SIZE); + /* Add the remainder of the first page */ + physmem_hardware_region(PAGE_SIZE + PAGE_SIZE / 2, PAGE_SIZE / 2); + + len = physmem_avail(avail, 4); + ATF_CHECK_EQ(len, 2); + ATF_CHECK_EQ(avail[0], PAGE_SIZE); + ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, hwregion); ATF_TP_ADD_TC(tp, hwregion_exclude); + ATF_TP_ADD_TC(tp, hwregion_unordered); return (atf_no_error()); }