From owner-dev-commits-src-main@freebsd.org Wed Jan 13 15:02:13 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C38634DFABE; Wed, 13 Jan 2021 15:02:13 +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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DG9dx5Bd3z4n2L; Wed, 13 Jan 2021 15:02:13 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 A581D1989A; Wed, 13 Jan 2021 15:02:13 +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 10DF2DcK025496; Wed, 13 Jan 2021 15:02:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10DF2D49025495; Wed, 13 Jan 2021 15:02:13 GMT (envelope-from git) Date: Wed, 13 Jan 2021 15:02:13 GMT Message-Id: <202101131502.10DF2D49025495@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 8f54940f019c - main - x86 busdma_bounce: do not make assumptions about alignment of malloc(9) results. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8f54940f019ca586bcfbf189ef9974eeb0a8194a Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jan 2021 15:02:13 -0000 The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=8f54940f019ca586bcfbf189ef9974eeb0a8194a commit 8f54940f019ca586bcfbf189ef9974eeb0a8194a Author: Konstantin Belousov AuthorDate: 2021-01-11 23:42:30 +0000 Commit: Konstantin Belousov 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)) &&