git: ff3ccf6f1a02 - main - bpf: refactor buffer pre-allocation for BIOCSETIF

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 21 Nov 2025 22:44:38 UTC
The branch main has been updated by glebius:

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

commit ff3ccf6f1a020800605bca1bec66985eb7be56d3
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-11-21 22:43:47 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2025-11-21 22:43:47 +0000

    bpf: refactor buffer pre-allocation for BIOCSETIF
    
    This basically refactors 4f42daa4a326f to use less indentation and
    variables.  The code is still not race proof.
---
 sys/net/bpf.c | 45 ++++++++++++++++++++-------------------------
 1 file changed, 20 insertions(+), 25 deletions(-)

diff --git a/sys/net/bpf.c b/sys/net/bpf.c
index d80eb8b80016..0ce823412b91 100644
--- a/sys/net/bpf.c
+++ b/sys/net/bpf.c
@@ -1609,33 +1609,28 @@ bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
 	 * Set interface.
 	 */
 	case BIOCSETIF:
-		{
-			int alloc_buf, size;
+		/*
+		 * Behavior here depends on the buffering model.  If we're
+		 * using kernel memory buffers, then we can allocate them here.
+		 * If we're using zero-copy, then the user process must have
+		 * registered buffers by the time we get here.
+		 */
+		BPFD_LOCK(d);
+		if (d->bd_bufmode == BPF_BUFMODE_BUFFER &&
+		    d->bd_sbuf == NULL) {
+			u_int size;
 
-			/*
-			 * Behavior here depends on the buffering model.  If
-			 * we're using kernel memory buffers, then we can
-			 * allocate them here.  If we're using zero-copy,
-			 * then the user process must have registered buffers
-			 * by the time we get here.
-			 */
-			alloc_buf = 0;
-			BPFD_LOCK(d);
-			if (d->bd_bufmode == BPF_BUFMODE_BUFFER &&
-			    d->bd_sbuf == NULL)
-				alloc_buf = 1;
+			size = d->bd_bufsize;
 			BPFD_UNLOCK(d);
-			if (alloc_buf) {
-				size = d->bd_bufsize;
-				error = bpf_buffer_ioctl_sblen(d, &size);
-				if (error != 0)
-					break;
-			}
-			BPF_LOCK();
-			error = bpf_setif(d, (struct ifreq *)addr);
-			BPF_UNLOCK();
-			break;
-		}
+			error = bpf_buffer_ioctl_sblen(d, &size);
+			if (error != 0)
+				break;
+		} else
+			BPFD_UNLOCK(d);
+		BPF_LOCK();
+		error = bpf_setif(d, (struct ifreq *)addr);
+		BPF_UNLOCK();
+		break;
 
 	/*
 	 * Set read timeout.