From nobody Wed Apr 06 13:24:04 2022 X-Original-To: dev-commits-src-main@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 C23C91A835D7; Wed, 6 Apr 2022 13:24:04 +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 4KYQFw3FNNz5890; Wed, 6 Apr 2022 13:24:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649251444; 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=Z1zYcd4Nk9iApzpVuxwBABAJt8kdDR1o7dWyP8rQIvc=; b=PZ9hXdE9CWzpKK/CijXIXoJX7gMtDS5IPTm1Z6JWGAK8Sj3uMPjVo5AGK9rO3bkYsQreGZ fpvOxSkphEurZKXh4ihxYQgNEPNsv8cQMSSmgQA1GLAbAqrG38cScQ8/gPEa0oZXZMYZYT 9OMrljpwKcdIJ1EuZl6CbC3Q8A73m5tqbx+GjFKwaQO5sBYR056JQJmqilZqSFOdaFSY4F 4ErxNJD1NasjT3aR9YDE00qlKQrc+NVkQrvVRAkIRJJO127a8JWmpQ27aVRWRT5JUR7NCz lYCp3Iux7C/3jHYXHGT9TUl1EnSLl5gGFQDm/l13iU+QAHr4j4ldisbv+oqb4A== 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 22F291BC2F; Wed, 6 Apr 2022 13:24:04 +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 236DO4fj071484; Wed, 6 Apr 2022 13:24:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 236DO48p071483; Wed, 6 Apr 2022 13:24:04 GMT (envelope-from git) Date: Wed, 6 Apr 2022 13:24:04 GMT Message-Id: <202204061324.236DO48p071483@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Andrew Turner Subject: git: d8bff5b67c9b - main - Handle non-page aligned/sized memory in physmem List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: d8bff5b67c9b6a85f409a7408e45e7e87d39c5a2 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649251444; 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=Z1zYcd4Nk9iApzpVuxwBABAJt8kdDR1o7dWyP8rQIvc=; b=oLyZBdVS1TfD61r+jr+npojvrX301RQ/BO4TGu0eTGf827sLwKu9GogiozNslRdmikI69Q JnClsqsIEU5NzxYUZSo8/6H8t1v7a+NRczZe67LV2//kErGq4Kur8x+Lz6b6tMTlX4A7/0 SnsNnzNGYJfQqf6/xHCTQXfnYkzz5O0J1bubrKrzTCgqibwwvt70Y6Ei2MB6T7eU8qUdPh dFLhgKJJuLCxSU39Y0JmvTSAaCBcHEviWfz+L6tNxJud9fB7aAUcQ45SFSEq+HQzkwU2tv nsT4mot2QJ+jib3zZGM/G/PjS2dj4UemwpNPIa17O+wGCq9QojzWobrI+k/jEg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1649251444; a=rsa-sha256; cv=none; b=qEsrIqTqr3b5G83BYFIcJwM1llq6anNWZ9QKaeI1YV8th3qy6I4X9AGl07Jm5HcDc/soPz OlBdNGKpoxWLd9WHN9cNXBaLab3cBSf4CcGV4Cms6UH8UsjbAkW81DSj9GRgN5ROsG+tRx rfG6LPUVsAt91GX54Q7ZKI7xpsAf6jzcqgmxReMOx1ajQ53Ahs+KTFmc+XL+ocD45TIlcR SurMgY29bNxNMgo/DQ88CLQK/bhMgs7RXF5mDiOv8FZS32Wzh70VY2fs9aJgS/up5oP858 pcyyznoHp8eaJ1UEuiKA88ycac1N+LqXudfMm1qDtdo/TO4NJvmf4LlxL8mqNw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by andrew: URL: https://cgit.FreeBSD.org/src/commit/?id=d8bff5b67c9b6a85f409a7408e45e7e87d39c5a2 commit d8bff5b67c9b6a85f409a7408e45e7e87d39c5a2 Author: Andrew Turner AuthorDate: 2022-03-28 11:37:09 +0000 Commit: Andrew Turner CommitDate: 2022-04-06 13:13:29 +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 --- 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 c149edaf0281..08f08f0c1a5d 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) { /* @@ -417,8 +418,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. @@ -451,14 +450,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()); }