From nobody Thu Aug 15 14:21:55 2024 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 4Wl6kr2wl9z5SFKg; Thu, 15 Aug 2024 14:21:56 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Wl6kr0r4Hz4NF9; Thu, 15 Aug 2024 14:21:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1723731716; 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=E5AZahXWTbqSlEcQcD04VJeFVTRUPcdj0me9zN73VcM=; b=KpcNJGn5hC+KJxSutolxt7EibL1Kj5e42WkC31EL91/gYCwPma/i1qS4jfpIKAcwIxq3pF CZxPAkr+ESC/2IYmkXvN3RZTeSaZO8LbXBavSZfyL1HRte+J3jH8Drxxrj+6mhTfR/I4yq 4K5HRtMoT1cbDq3uDSSEQf+nckwuxXa70FE+wTmuOvuituexvi503MlkoxtJBbqYWvayXh dUW5C/RreXkgg01MBLkFhUWgnYTrmEWVvS26TqXTXxsOeWCu/jPMGAXo/SO+hr7B7tt6yP 2AuKpij97NOgGlvJwQ6/nz2BFrsQIMP0Epc9TD85+082P8f02AiKB2dvYO/ESg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1723731716; a=rsa-sha256; cv=none; b=eMBRlnoOnT1M0hRBH3nImzdOMcSR+32UFn+fP6OK6GqzNGuVosZidQGD2P7PrL5o7/+4pK RCrYWU8bKfBzW60MQgMI0hi0F4hRmJl7wGlZHzx/eYpjSeEFnHFNQTwfVNz5O3WJGzEH0o 8xPYBmrLlgKcigHp5BeHb6zxLCjplTd1xz3/Gmdq0JitTCH6mNGqHjsbmq3ieQKXsKoN5u j0tAybb9Aj1y8wsmelxnRDRAyulxjH440Koh/aYPXMJJFfAgL91v7z+RnhkQHzfpWGR5uG vMTCsR8lNKDfhx9T9x5ZUBlhfXZfOZju+u41HHjM6ntJP4g6LCjmdw/Udim8yw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1723731716; 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=E5AZahXWTbqSlEcQcD04VJeFVTRUPcdj0me9zN73VcM=; b=deJK+e7fW7ERw5qAu3JU144rcGJ7JzJWGnxhemkuzaGx50d6aaRoZfT7/Se7STvQ5Pj0eQ jVf9rwhEC0kVrxYlul4Oin8rIdxsRqREB7shBHonQJn6YAWVdXq7mzaxIKqp/GJdigkSm5 9NAPLQZdpFTCdvUouQwohVoJ/NYB8Ubc0vumi1/ZjfFwej0zzMlQb2zrBETbfXaScalR2o sSdeeP3Z/6pEmplJqxTERBdrnt/u1GtfCmXWxIUOjoCPHuE4MHiu0NQzMsdfFITLGtZkM7 uQ8R859Ivk1y3WUP/B2sMmhNCo3dpyh2wnxOg5H2W97WdwFHF7QipwFuC0a4Pg== 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 4Wl6kr0RmBzQnG; Thu, 15 Aug 2024 14:21:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 47FELtFC012874; Thu, 15 Aug 2024 14:21:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 47FELtiv012871; Thu, 15 Aug 2024 14:21:55 GMT (envelope-from git) Date: Thu, 15 Aug 2024 14:21:55 GMT Message-Id: <202408151421.47FELtiv012871@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: bef079254f3d - main - arm64: Clamp segment sizes properly in bounce_bus_dmamap_load_buffer() 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: bef079254f3dcf443a61b4c2605cc6e2dcaf043f Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=bef079254f3dcf443a61b4c2605cc6e2dcaf043f commit bef079254f3dcf443a61b4c2605cc6e2dcaf043f Author: Mark Johnston AuthorDate: 2024-08-15 14:17:40 +0000 Commit: Mark Johnston CommitDate: 2024-08-15 14:19:22 +0000 arm64: Clamp segment sizes properly in bounce_bus_dmamap_load_buffer() Commit 099b59515499 ("Improve loading of multipage aligned buffers.") modified bounce_bus_dmamap_load_buffer() with the assumption that busdma memory allocations are physically contiguous, which is not always true: bounce_bus_dmamem_alloc() will allocate memory with kmem_alloc_attr_domainset() in some cases, and this function is not guaranteed to return contiguous memory. The damage seems to have been mitigated for most consumers by clamping the segment size to maxsegsz, but this was removed in commit a77e1f0f81df ("busdma: better handling of small segment bouncing"); in practice, it seems busdma memory is often allocated with maxsegsz == PAGE_SIZE. In particular, after commit a77e1f0f81df I see occasional random kernel memory corruption when benchmarking TCP through mlx5 interfaces. Fix the problem by using separate flags for contiguous and non-contiguous busdma memory allocations, and using that to decide whether to clamp. Fixes: 099b59515499 ("Improve loading of multipage aligned buffers.") Fixes: a77e1f0f81df ("busdma: better handling of small segment bouncing") Sponsored by: Klara, Inc. Sponsored by: Stormshield Differential Revision: https://reviews.freebsd.org/D46238 --- sys/arm64/arm64/busdma_bounce.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sys/arm64/arm64/busdma_bounce.c b/sys/arm64/arm64/busdma_bounce.c index da605d4e21f5..abfd5c195857 100644 --- a/sys/arm64/arm64/busdma_bounce.c +++ b/sys/arm64/arm64/busdma_bounce.c @@ -63,7 +63,9 @@ enum { BF_COULD_BOUNCE = 0x01, BF_MIN_ALLOC_COMP = 0x02, - BF_KMEM_ALLOC = 0x04, + BF_KMEM_ALLOC_PAGES = 0x04, + BF_KMEM_ALLOC_CONTIG = 0x08, + BF_KMEM_ALLOC = BF_KMEM_ALLOC_PAGES | BF_KMEM_ALLOC_CONTIG, BF_COHERENT = 0x10, }; @@ -580,14 +582,14 @@ bounce_bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags, *vaddr = kmem_alloc_attr_domainset( DOMAINSET_PREF(dmat->common.domain), dmat->alloc_size, mflags, 0ul, dmat->common.lowaddr, attr); - dmat->bounce_flags |= BF_KMEM_ALLOC; + dmat->bounce_flags |= BF_KMEM_ALLOC_PAGES; } else { *vaddr = kmem_alloc_contig_domainset( DOMAINSET_PREF(dmat->common.domain), dmat->alloc_size, mflags, 0ul, dmat->common.lowaddr, dmat->alloc_alignment != 0 ? dmat->alloc_alignment : 1ul, dmat->common.boundary, attr); - dmat->bounce_flags |= BF_KMEM_ALLOC; + dmat->bounce_flags |= BF_KMEM_ALLOC_CONTIG; } if (*vaddr == NULL) { CTR4(KTR_BUSDMA, "%s: tag %p tag flags 0x%x error %d", @@ -856,7 +858,8 @@ bounce_bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf, * Compute the segment size, and adjust counts. */ sgsize = buflen; - if ((map->flags & DMAMAP_FROM_DMAMEM) == 0) + if ((map->flags & DMAMAP_FROM_DMAMEM) == 0 || + (dmat->bounce_flags & BF_KMEM_ALLOC_CONTIG) == 0) sgsize = MIN(sgsize, PAGE_SIZE - (curaddr & PAGE_MASK)); if (map->pagesneeded != 0 &&