Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 30 Apr 2025 06:01:12 GMT
From:      Gleb Smirnoff <glebius@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 2e891fce9de5 - main - mbufs: let mc_get() and mc_uiotomc() return a consistent mc when failed
Message-ID:  <202504300601.53U61Cap017679@gitrepo.freebsd.org>

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

URL: https://cgit.FreeBSD.org/src/commit/?id=2e891fce9de5f7b1521060916082e1a38ba8920d

commit 2e891fce9de5f7b1521060916082e1a38ba8920d
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-04-30 05:31:41 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2025-04-30 05:31:41 +0000

    mbufs: let mc_get() and mc_uiotomc() return a consistent mc when failed
    
    This was a documented behavior when functions were introduced, but on
    second thought it seems easier to do extra work in the leaf functions,
    instead of putting the burden on KPI users, even if some of the KPI
    users may not care about consistency.
---
 sys/kern/kern_mbuf.c | 6 +++---
 sys/kern/uipc_mbuf.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/sys/kern/kern_mbuf.c b/sys/kern/kern_mbuf.c
index 2b2b032e644c..48886c1e5b0c 100644
--- a/sys/kern/kern_mbuf.c
+++ b/sys/kern/kern_mbuf.c
@@ -1462,8 +1462,7 @@ m_getjcl(int how, short type, int flags, int size)
 
 /*
  * 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.
+ * best).  May fail due to ENOMEM.
  */
 int
 mc_get(struct mchain *mc, u_int length, int how, short type, int flags)
@@ -1501,8 +1500,9 @@ mc_get(struct mchain *mc, u_int length, int how, short type, int flags)
 			 * Fail the whole operation if one mbuf can't be
 			 * allocated.
 			 */
-			if (mb == NULL) {
+			if (__predict_false(mb == NULL)) {
 				m_freem(mc_first(mc));
+				*mc = MCHAIN_INITIALIZER(mc);
 				return (ENOMEM);
 			}
 		}
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c
index f3e2f13e89ec..377a36287e3e 100644
--- a/sys/kern/uipc_mbuf.c
+++ b/sys/kern/uipc_mbuf.c
@@ -1986,7 +1986,6 @@ m_uiotombuf(struct uio *uio, int how, int len, int lspace, int flags)
 
 /*
  * Copy the contents of uio into a properly sized mbuf chain.
- * In case of failure state of mchain is inconsistent.
  * @param length Limit copyout length.  If 0 entire uio_resid is copied.
  * @param lspace Provide leading space in the first mbuf in the chain.
  */
@@ -2032,6 +2031,7 @@ mc_uiotomc(struct mchain *mc, struct uio *uio, u_int length, u_int lspace,
 		error = uiomove(mtod(mb, void *), mlen, uio);
 		if (__predict_false(error)) {
 			mc_freem(mc);
+			*mc = MCHAIN_INITIALIZER(mc);
 			return (error);
 		}
 		mb->m_len = mlen;



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