From nobody Mon Apr 08 20:29:46 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 4VD10q0DxXz5Hj5p; Mon, 8 Apr 2024 20:29:47 +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 4VD10p4fXRz45v4; Mon, 8 Apr 2024 20:29:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712608186; 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=XG99dqYeQcoe2qx8PHLOD/deL9YMK67bJmRotZuhVT8=; b=wYYy20NcfZJFEBSAv4jQlaNQgaxePURDPa/1YVBszIfS3E5ysfUA9shhPC+wTiqGxVEtWE 068YtCc7ieFbIofBkvJk+WL7hPckaOroGO2D2tqixeW9AN8reYLOp2fOfstRCWxGp1LT4P sEfY71HLTaw1Bz0v5N6Q7w3EwythIaD8C/NyJejjmY2LlIZeGVAs4aPkq1lODwy65BvYhH WhuwTxz5it+dktvBt1nL6gFKKMr5OjTTRggz7+oLftN+bzzjIYSZwIH5fLZu28V61ARlNn r0k5245ubd9Ose/LDa3svUxd5U67I1vG0pRlxTWgeAbSzdBreWItdPskchAp9w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712608186; a=rsa-sha256; cv=none; b=EcW/9+1c/4OwRXp9u3AYosxeHXTbMP/v/8Y6gopksByaFThovOzEmnaEtC4Gq3fc7Fg6Wz rp9cFfQu0GVUH9hU87QU+s5Pf3ivRSNtmazTdt5mNqdnkkNUG9uISWU5hkAXehGcvkSj2D QXDXaWyM+3s+tV9BsfF7CoBYYDc3jntGjJm/6W8PeE6E62rNOiPzKT7ylvhRPeBbddifif uXgnEsc6YNb9ybYrpnKEbaGiHDS/3hkEscB/b3htKhT66qDNGtBJ81sjAaK2luuk9TnoXv Yn7fKbRMS7yw/+L4SxWSEBfyawIdqYFX2FhFqWwkCJXEZvmN+lCDbaK4pyfsjA== 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=1712608186; 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=XG99dqYeQcoe2qx8PHLOD/deL9YMK67bJmRotZuhVT8=; b=pdy+JgTFFyxhw7aEB2TtN3GfKJitWhxwJOImOKZIQj0c3yoR7Wh41PnPdUQiQGMRBlOAHh tmmT7QCKOztwmHzjFviSzXFPpEoYfwt4APF6u5WL1nL4j8Kze3No1Qj8NplUvl6Ff4UyIQ 1q41S16wJwKF9vJ4+tthHsQ89Aw/7RXSQK+L8sxdfwEW95UiIXTeMWS21jxu0hfSk24eCu cn/CNmyW+wKwfT7SU4vYaQ0TkpfvRwPjKSp9qexxIfn2GF0nDVLS2HLy/8mUZl8PdUNc62 X2HiRN4H8+uCvxV+xW+Iv4iNEdOfJhbEFVJ0v4kanUW10bW038bLbZr89ndLUw== 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 4VD10p4Gw6zPmJ; Mon, 8 Apr 2024 20:29:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 438KTkvM041789; Mon, 8 Apr 2024 20:29:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 438KTkeN041787; Mon, 8 Apr 2024 20:29:46 GMT (envelope-from git) Date: Mon, 8 Apr 2024 20:29:46 GMT Message-Id: <202404082029.438KTkeN041787@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Gleb Smirnoff Subject: git: 71f8702f49a7 - main - mbuf: provide mc_get() that allocates struct mchain of given length 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: glebius X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 71f8702f49a7c3294429e227a2b07cc3a0c408c7 Auto-Submitted: auto-generated The branch main has been updated by glebius: URL: https://cgit.FreeBSD.org/src/commit/?id=71f8702f49a7c3294429e227a2b07cc3a0c408c7 commit 71f8702f49a7c3294429e227a2b07cc3a0c408c7 Author: Gleb Smirnoff AuthorDate: 2024-04-08 20:16:51 +0000 Commit: Gleb Smirnoff CommitDate: 2024-04-08 20:16:51 +0000 mbuf: provide mc_get() that allocates struct mchain of given length Implement m_getm2(), which is widely used via m_getm() macro, as a wrapper around mc_get(). New code is advised to use mc_get(). Reviewed by: markj, tuexen Differential Revision: https://reviews.freebsd.org/D44149 --- sys/kern/kern_mbuf.c | 89 ++++++++++++++++++++++++++++++++-------------------- sys/sys/mbuf.h | 1 + 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/sys/kern/kern_mbuf.c b/sys/kern/kern_mbuf.c index 60c638735ec4..0df807e47bd6 100644 --- a/sys/kern/kern_mbuf.c +++ b/sys/kern/kern_mbuf.c @@ -1446,34 +1446,36 @@ m_getjcl(int how, short type, int flags, int size) } /* - * Allocate a given length worth of mbufs and/or clusters (whatever fits - * best) and return a pointer to the top of the allocated chain. If an - * existing mbuf chain is provided, then we will append the new chain - * to the existing one and return a pointer to the provided mbuf. + * Allocate mchain of a given length of mbufs and/or clusters (whatever fits + * best). May fail due to ENOMEM. In case of failure state of mchain is + * inconsistent. */ -struct mbuf * -m_getm2(struct mbuf *m, int len, int how, short type, int flags) +int +mc_get(struct mchain *mc, u_int length, int how, short type, int flags) { - struct mbuf *mb, *nm = NULL, *mtail = NULL; + struct mbuf *mb; + u_int progress; - KASSERT(len >= 0, ("%s: len is < 0", __func__)); + MPASS(length >= 0); - /* Validate flags. */ + *mc = MCHAIN_INITIALIZER(mc); flags &= (M_PKTHDR | M_EOR); - - /* Packet header mbuf must be first in chain. */ - if ((flags & M_PKTHDR) && m != NULL) - flags &= ~M_PKTHDR; + progress = 0; /* Loop and append maximum sized mbufs to the chain tail. */ - while (len > 0) { - mb = NULL; - if (len > MCLBYTES) { + do { + if (length - progress > MCLBYTES) { + /* + * M_NOWAIT here is intentional, it avoids blocking if + * the jumbop zone is exhausted. See 796d4eb89e2c and + * D26150 for more detail. + */ mb = m_getjcl(M_NOWAIT, type, (flags & M_PKTHDR), MJUMPAGESIZE); - } + } else + mb = NULL; if (mb == NULL) { - if (len >= MINCLSIZE) + if (length - progress >= MINCLSIZE) mb = m_getcl(how, type, (flags & M_PKTHDR)); else if (flags & M_PKTHDR) mb = m_gethdr(how, type); @@ -1485,31 +1487,50 @@ m_getm2(struct mbuf *m, int len, int how, short type, int flags) * allocated. */ if (mb == NULL) { - m_freem(nm); - return (NULL); + m_freem(mc_first(mc)); + return (ENOMEM); } } - /* Book keeping. */ - len -= M_SIZE(mb); - if (mtail != NULL) - mtail->m_next = mb; - else - nm = mb; - mtail = mb; - flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */ - } + progress += M_SIZE(mb); + mc_append(mc, mb); + /* Only valid on the first mbuf. */ + flags &= ~M_PKTHDR; + } while (progress < length); if (flags & M_EOR) - mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */ + /* Only valid on the last mbuf. */ + mc_last(mc)->m_flags |= M_EOR; + + return (0); +} + +/* + * Allocate a given length worth of mbufs and/or clusters (whatever fits + * best) and return a pointer to the top of the allocated chain. If an + * existing mbuf chain is provided, then we will append the new chain + * to the existing one and return a pointer to the provided mbuf. + */ +struct mbuf * +m_getm2(struct mbuf *m, int len, int how, short type, int flags) +{ + struct mchain mc; + + /* Packet header mbuf must be first in chain. */ + if (m != NULL && (flags & M_PKTHDR)) + flags &= ~M_PKTHDR; + + if (__predict_false(mc_get(&mc, len, how, type, flags) != 0)) + return (NULL); /* If mbuf was supplied, append new chain to the end of it. */ if (m != NULL) { - for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next) - ; - mtail->m_next = nm; + struct mbuf *mtail; + + mtail = m_last(m); + mtail->m_next = mc_first(&mc); mtail->m_flags &= ~M_EOR; } else - m = nm; + m = mc_first(&mc); return (m); } diff --git a/sys/sys/mbuf.h b/sys/sys/mbuf.h index a5af2291123b..76c0005ae963 100644 --- a/sys/sys/mbuf.h +++ b/sys/sys/mbuf.h @@ -1795,6 +1795,7 @@ mc_remove(struct mchain *mc, struct mbuf *m) mc_dec(mc, m); } +int mc_get(struct mchain *, u_int, int, short, int); int mc_split(struct mchain *, struct mchain *, u_int, int); #ifdef _SYS_TIMESPEC_H_