Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Jan 2021 15:02:13 GMT
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 8f54940f019c - main - x86 busdma_bounce: do not make assumptions about alignment of malloc(9) results.
Message-ID:  <202101131502.10DF2D49025495@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=8f54940f019ca586bcfbf189ef9974eeb0a8194a

commit 8f54940f019ca586bcfbf189ef9974eeb0a8194a
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-01-11 23:42:30 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-01-13 15:00:49 +0000

    x86 busdma_bounce: do not make assumptions about alignment of malloc(9) results.
    
    Reported by:    dim
    Reviewed by:    dim, jah
    Tested by:      dim, pho
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D28108
---
 sys/x86/x86/busdma_bounce.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/sys/x86/x86/busdma_bounce.c b/sys/x86/x86/busdma_bounce.c
index 65fbbb7ec1a4..fec6e2144a22 100644
--- a/sys/x86/x86/busdma_bounce.c
+++ b/sys/x86/x86/busdma_bounce.c
@@ -403,6 +403,8 @@ static int
 bounce_bus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags,
     bus_dmamap_t *mapp)
 {
+	uintptr_t ma;
+	size_t malloc_sz;
 	vm_memattr_t attr;
 	int mflags;
 
@@ -445,19 +447,20 @@ bounce_bus_dmamem_alloc(bus_dma_tag_t dmat, void **vaddr, int flags,
 	 * else allocate a block of contiguous pages because one or more of the
 	 * constraints is something that only the contig allocator can fulfill.
 	 *
-	 * NOTE: The (dmat->common.alignment <= dmat->maxsize) check
-	 * below is just a quick hack. The exact alignment guarantees
-	 * of malloc(9) need to be nailed down, and the code below
-	 * should be rewritten to take that into account.
-	 *
-	 * In the meantime warn the user if malloc gets it wrong.
+	 * Warn the user if we get it wrong.
 	 */
 	if (dmat->common.maxsize <= PAGE_SIZE &&
-	    dmat->common.alignment <= dmat->common.maxsize &&
 	    dmat->common.lowaddr >= ptoa((vm_paddr_t)Maxmem) &&
 	    attr == VM_MEMATTR_DEFAULT) {
-		*vaddr = malloc_domainset(dmat->common.maxsize, M_DEVBUF,
+		malloc_sz = roundup2(dmat->common.maxsize,
+		    dmat->common.alignment);
+		ma = (uintptr_t)malloc_domainset(malloc_sz, M_DEVBUF,
 		    DOMAINSET_PREF(dmat->common.domain), mflags);
+		if (ma != 0) {
+			*vaddr = (void *)roundup2(ma, dmat->common.alignment);
+		} else {
+			*vaddr = NULL;
+		}
 	} else if (dmat->common.nsegments >=
 	    howmany(dmat->common.maxsize, MIN(dmat->common.maxsegsz,
 	    PAGE_SIZE)) &&



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202101131502.10DF2D49025495>