From nobody Sat Dec 18 01:36:28 2021 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 31D1618FC4C1; Sat, 18 Dec 2021 01:36:29 +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 4JG7jm4Bklz4pwn; Sat, 18 Dec 2021 01:36:28 +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 70D202654D; Sat, 18 Dec 2021 01:36:28 +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 1BI1aSL1095238; Sat, 18 Dec 2021 01:36:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BI1aSj1095237; Sat, 18 Dec 2021 01:36:28 GMT (envelope-from git) Date: Sat, 18 Dec 2021 01:36:28 GMT Message-Id: <202112180136.1BI1aSj1095237@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Alexander Motin Subject: git: d87f1e2e3646 - stable/13 - Make msgbuf_peekbytes() not return leading zeroes. 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: mav X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d87f1e2e36461a2ab028fb25b2312903efe5b10a Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639791388; 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=uUFLpz2aeuYxy5+vMygiuOko7b2IRKJuGs3KJoXxdcU=; b=Pdpnvi3j57U/BpvzfCFb+at9VavrYIz6p+P2heAhF05RiIj9mrHxGk0arAxD3U+zAubDav pjCEBLLWuDsq90vyf4Epd//BEFNutigEycRPhCf0CSBYgx8Tsp/ZLapSE66VJ7ViQsKiZ1 HJ0xC+E9vBgO40cufOPtNJTQJuRQGjjPJG9H1El0nv4XrD9Sf7Hs9l+f94sn1q5EBQfwd/ MigblVEcszVvJ1aWXZKoMeVIKRj+bXYvKBCb39GxDNDZCqAMHa8cNO4pznqs2CGa4w1rGr gzn5RJrtWP+f2VI/mRmg4sGX8BPM3Cjdt1lOIvO05Y8LVxQp9oRgZLryb+6KLw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639791388; a=rsa-sha256; cv=none; b=b27bH5FRuFXBIl00C99Oj/Md1vn4z+UwQBYoaSU2Cz2OiKPulp+QkqWXI7fbnXpEzDOzI5 3Ca3cYQ2P3RRw+kumxEh7IqnnOiQsyXL49dHWIsmr/HYctDnMpjt/laGOiWZqjkoBMVvlM AJwHj2ltE3vRt8QYQ8A7c9X9N4pWLpJWqZSasbLa0u7bE++2eErTHR3UcEHBIPv/ICEeCy snJ/3VNGm78PY5F6mk0rMkOvrKBgvre1CZAHa7gbxg2Llhd/klSrVX96uTJFPtZ3a+xajs KP9x2lBcpNZCmH3LOvQJPm4EnUU3zFTRMEfstpXxaHUfYIN0jLG9aCjB5B2sMg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by mav: URL: https://cgit.FreeBSD.org/src/commit/?id=d87f1e2e36461a2ab028fb25b2312903efe5b10a commit d87f1e2e36461a2ab028fb25b2312903efe5b10a Author: Alexander Motin AuthorDate: 2021-12-11 04:18:52 +0000 Commit: Alexander Motin CommitDate: 2021-12-18 01:36:23 +0000 Make msgbuf_peekbytes() not return leading zeroes. Introduce new MSGBUF_WRAP flag, indicating that buffer has wrapped at least once and does not keep zeroes from the last msgbuf_clear(). It allows msgbuf_peekbytes() to return only real data, not requiring every consumer to trim the leading zeroes after doing pointless copy. The most visible effect is that kern.msgbuf sysctl now always returns proper zero-terminated string, not only after the first buffer wrap. MFC after: 1 week Sponsored by: iXsystems, Inc. (cherry picked from commit 81dc00331d148fab19af24833521334e39f3692d) --- sys/kern/subr_msgbuf.c | 55 +++++++++++++++++++++++--------------------------- sys/sys/msgbuf.h | 7 +++++-- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/sys/kern/subr_msgbuf.c b/sys/kern/subr_msgbuf.c index 8af013d52a2d..b41e78dd886e 100644 --- a/sys/kern/subr_msgbuf.c +++ b/sys/kern/subr_msgbuf.c @@ -69,10 +69,10 @@ msgbuf_init(struct msgbuf *mbp, void *ptr, int size) mbp->msg_ptr = ptr; mbp->msg_size = size; mbp->msg_seqmod = SEQMOD(size); - msgbuf_clear(mbp); - mbp->msg_magic = MSG_MAGIC; mbp->msg_lastpri = -1; mbp->msg_flags = 0; + msgbuf_clear(mbp); + mbp->msg_magic = MSG_MAGIC; bzero(&mbp->msg_lock, sizeof(mbp->msg_lock)); mtx_init(&mbp->msg_lock, "msgbuf", NULL, MTX_SPIN); } @@ -123,6 +123,7 @@ msgbuf_clear(struct msgbuf *mbp) mbp->msg_wseq = 0; mbp->msg_rseq = 0; mbp->msg_cksum = 0; + mbp->msg_flags &= ~MSGBUF_WRAP; } /* @@ -145,18 +146,17 @@ msgbuf_getcount(struct msgbuf *mbp) * * The caller should hold the message buffer spinlock. */ - static void -msgbuf_do_addchar(struct msgbuf * const mbp, u_int * const seq, const int c) +msgbuf_do_addchar(struct msgbuf * const mbp, const int c) { u_int pos; /* Make sure we properly wrap the sequence number. */ - pos = MSGBUF_SEQ_TO_POS(mbp, *seq); + pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_wseq); mbp->msg_cksum += (u_int)(u_char)c - (u_int)(u_char)mbp->msg_ptr[pos]; mbp->msg_ptr[pos] = c; - *seq = MSGBUF_SEQNORM(mbp, *seq + 1); + mbp->msg_wseq = MSGBUF_SEQADD(mbp, mbp->msg_wseq, 1); } /* @@ -167,7 +167,9 @@ msgbuf_addchar(struct msgbuf *mbp, int c) { mtx_lock_spin(&mbp->msg_lock); - msgbuf_do_addchar(mbp, &mbp->msg_wseq, c); + msgbuf_do_addchar(mbp, c); + if (mbp->msg_wseq >= mbp->msg_size) + mbp->msg_flags |= MSGBUF_WRAP; mtx_unlock_spin(&mbp->msg_lock); } @@ -183,7 +185,6 @@ msgbuf_addchar(struct msgbuf *mbp, int c) void msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr) { - u_int seq; size_t len, prefix_len; char prefix[MAXPRIBUF]; char buf[32]; @@ -205,11 +206,6 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr) if (pri != -1) prefix_len = sprintf(prefix, "<%d>", pri); - /* - * Starting write sequence number. - */ - seq = mbp->msg_wseq; - /* * Whenever there is a change in priority, we have to insert a * newline, and a priority prefix if the priority is not -1. Here @@ -218,7 +214,7 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr) * insert a newline before this string. */ if (mbp->msg_lastpri != pri && (mbp->msg_flags & MSGBUF_NEEDNL) != 0) { - msgbuf_do_addchar(mbp, &seq, '\n'); + msgbuf_do_addchar(mbp, '\n'); mbp->msg_flags &= ~MSGBUF_NEEDNL; } @@ -233,7 +229,7 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr) int j; for (j = 0; j < prefix_len; j++) - msgbuf_do_addchar(mbp, &seq, prefix[j]); + msgbuf_do_addchar(mbp, prefix[j]); } if (msgbuf_show_timestamp && needtime == 1 && @@ -241,7 +237,7 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr) snprintf(buf, sizeof(buf), "[%jd] ", (intmax_t)time_uptime); for (j = 0; buf[j] != '\0'; j++) - msgbuf_do_addchar(mbp, &seq, buf[j]); + msgbuf_do_addchar(mbp, buf[j]); needtime = 0; } @@ -265,14 +261,10 @@ msgbuf_addstr(struct msgbuf *mbp, int pri, const char *str, int filter_cr) else mbp->msg_flags |= MSGBUF_NEEDNL; - msgbuf_do_addchar(mbp, &seq, str[i]); + msgbuf_do_addchar(mbp, str[i]); } - /* - * Update the write sequence number for the actual number of - * characters we put in the message buffer. (Depends on whether - * carriage returns are filtered.) - */ - mbp->msg_wseq = seq; + if (mbp->msg_wseq >= mbp->msg_size) + mbp->msg_flags |= MSGBUF_WRAP; /* * Set the last priority. @@ -302,9 +294,9 @@ msgbuf_getchar(struct msgbuf *mbp) return (-1); } if (len > mbp->msg_size) - mbp->msg_rseq = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size); + mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size); c = (u_char)mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq)]; - mbp->msg_rseq = MSGBUF_SEQNORM(mbp, mbp->msg_rseq + 1); + mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, 1); mtx_unlock_spin(&mbp->msg_lock); @@ -329,7 +321,7 @@ msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen) return (0); } if (len > mbp->msg_size) { - mbp->msg_rseq = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size); + mbp->msg_rseq = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size); len = mbp->msg_size; } pos = MSGBUF_SEQ_TO_POS(mbp, mbp->msg_rseq); @@ -337,7 +329,7 @@ msgbuf_getbytes(struct msgbuf *mbp, char *buf, int buflen) len = min(len, (u_int)buflen); bcopy(&mbp->msg_ptr[pos], buf, len); - mbp->msg_rseq = MSGBUF_SEQNORM(mbp, mbp->msg_rseq + len); + mbp->msg_rseq = MSGBUF_SEQADD(mbp, mbp->msg_rseq, len); mtx_unlock_spin(&mbp->msg_lock); @@ -363,7 +355,10 @@ msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen, u_int *seqp) if (buf == NULL) { /* Just initialise *seqp. */ - *seqp = MSGBUF_SEQNORM(mbp, mbp->msg_wseq - mbp->msg_size); + if (mbp->msg_flags & MSGBUF_WRAP) + *seqp = MSGBUF_SEQSUB(mbp, mbp->msg_wseq, mbp->msg_size); + else + *seqp = 0; mtx_unlock_spin(&mbp->msg_lock); return (0); } @@ -375,14 +370,14 @@ msgbuf_peekbytes(struct msgbuf *mbp, char *buf, int buflen, u_int *seqp) return (0); } if (len > mbp->msg_size) { - *seqp = MSGBUF_SEQNORM(mbp, wseq - mbp->msg_size); + *seqp = MSGBUF_SEQSUB(mbp, wseq, mbp->msg_size); len = mbp->msg_size; } pos = MSGBUF_SEQ_TO_POS(mbp, *seqp); len = min(len, mbp->msg_size - pos); len = min(len, (u_int)buflen); bcopy(&mbp->msg_ptr[MSGBUF_SEQ_TO_POS(mbp, *seqp)], buf, len); - *seqp = MSGBUF_SEQNORM(mbp, *seqp + len); + *seqp = MSGBUF_SEQADD(mbp, *seqp, len); mtx_unlock_spin(&mbp->msg_lock); diff --git a/sys/sys/msgbuf.h b/sys/sys/msgbuf.h index 27aba1a8e0ed..6768b862faf7 100644 --- a/sys/sys/msgbuf.h +++ b/sys/sys/msgbuf.h @@ -50,6 +50,7 @@ struct msgbuf { int msg_lastpri; /* saved priority value */ u_int msg_flags; #define MSGBUF_NEEDNL 0x01 /* set when newline needed */ +#define MSGBUF_WRAP 0x02 /* buffer has wrapped around */ struct mtx msg_lock; /* mutex to protect the buffer */ }; @@ -57,8 +58,10 @@ struct msgbuf { #define MSGBUF_SEQNORM(mbp, seq) (((seq) + (mbp)->msg_seqmod) % \ (mbp)->msg_seqmod) #define MSGBUF_SEQ_TO_POS(mbp, seq) ((seq) % (mbp)->msg_size) -/* Subtract sequence numbers. Note that only positive values result. */ -#define MSGBUF_SEQSUB(mbp, seq1, seq2) (MSGBUF_SEQNORM((mbp), (seq1) - (seq2))) +/* Add/subtract normalized sequence numbers. Normalized values result. */ +#define MSGBUF_SEQADD(mbp, seq1, seq2) (((seq1) + (seq2)) % (mbp)->msg_seqmod) +#define MSGBUF_SEQSUB(mbp, seq1, seq2) ((seq1) >= (seq2) ? (seq1) - (seq2) : \ + (seq1) + (mbp)->msg_seqmod - (seq2)) #ifdef _KERNEL extern int msgbufsize;