mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org
Cc: Ingo Molnar <mingo@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jiri Kosina <jkosina@suse.cz>, Petr Mladek <pmladek@suse.cz>
Subject: [for-next][PATCH 09/15] seq_buf: Add seq_buf_can_fit() helper function
Date: Thu, 20 Nov 2014 10:21:29 -0500	[thread overview]
Message-ID: <20141120152717.083069310@goodmis.org> (raw)
In-Reply-To: <20141120152120.168868031@goodmis.org>

[-- Attachment #1: 0009-seq_buf-Add-seq_buf_can_fit-helper-function.patch --]
[-- Type: text/plain, Size: 2576 bytes --]

From: "Steven Rostedt (Red Hat)" <rostedt@goodmis.org>

Add a seq_buf_can_fit() helper function that removes the possible mistakes
of comparing the seq_buf length plus added data compared to the size of
the buffer.

Link: http://lkml.kernel.org/r/20141118164025.GL23958@pathway.suse.cz

Reviewed-by: Petr Mladek <pmladek@suse.cz>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
---
 kernel/trace/seq_buf.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/kernel/trace/seq_buf.c b/kernel/trace/seq_buf.c
index ce17f65268ed..6fc9d021cbef 100644
--- a/kernel/trace/seq_buf.c
+++ b/kernel/trace/seq_buf.c
@@ -17,6 +17,19 @@
 #include <linux/seq_buf.h>
 
 /**
+ * seq_buf_can_fit - can the new data fit in the current buffer?
+ * @s: the seq_buf descriptor
+ * @len: The length to see if it can fit in the current buffer
+ *
+ * Returns true if there's enough unused space in the seq_buf buffer
+ * to fit the amount of new data according to @len.
+ */
+static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
+{
+	return s->len + len < s->size;
+}
+
+/**
  * seq_buf_print_seq - move the contents of seq_buf into a seq_file
  * @m: the seq_file descriptor that is the destination
  * @s: the seq_buf descriptor that is the source.
@@ -48,7 +61,7 @@ int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
 
 	if (s->len < s->size) {
 		len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
-		if (s->len + len < s->size) {
+		if (seq_buf_can_fit(s, len)) {
 			s->len += len;
 			return 0;
 		}
@@ -137,7 +150,7 @@ int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
 
 	if (s->len < s->size) {
 		ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
-		if (s->len + ret < s->size) {
+		if (seq_buf_can_fit(s, ret)) {
 			s->len += ret;
 			return 0;
 		}
@@ -161,7 +174,7 @@ int seq_buf_puts(struct seq_buf *s, const char *str)
 
 	WARN_ON(s->size == 0);
 
-	if (s->len + len < s->size) {
+	if (seq_buf_can_fit(s, len)) {
 		memcpy(s->buffer + s->len, str, len);
 		s->len += len;
 		return 0;
@@ -183,7 +196,7 @@ int seq_buf_putc(struct seq_buf *s, unsigned char c)
 {
 	WARN_ON(s->size == 0);
 
-	if (s->len + 1 < s->size) {
+	if (seq_buf_can_fit(s, 1)) {
 		s->buffer[s->len++] = c;
 		return 0;
 	}
@@ -207,7 +220,7 @@ int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
 {
 	WARN_ON(s->size == 0);
 
-	if (s->len + len < s->size) {
+	if (seq_buf_can_fit(s, len)) {
 		memcpy(s->buffer + s->len, mem, len);
 		s->len += len;
 		return 0;
-- 
2.1.1



  parent reply	other threads:[~2014-11-20 15:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-20 15:21 [for-next][PATCH 00/15] tracing/seq-buf/printk: New seq-buf utility from trace_seq for printk in NMI Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 01/15] tracing: Create seq_buf layer in trace_seq Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 02/15] tracing: Convert seq_buf_path() to be like seq_path() Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 03/15] tracing: Convert seq_buf fields to be like seq_file fields Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 04/15] tracing: Add a seq_buf_clear() helper and clear len and readpos in init Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 05/15] seq_buf: Create seq_buf_used() to find out how much was written Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 06/15] tracing: Clean up tracing_fill_pipe_page() Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 07/15] tracing: Use trace_seq_used() and seq_buf_used() instead of len Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 08/15] tracing: Add paranoid size check in trace_printk_seq() Steven Rostedt
2014-11-20 15:21 ` Steven Rostedt [this message]
2014-11-20 15:21 ` [for-next][PATCH 10/15] tracing: Have seq_buf use full buffer Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 11/15] tracing: Add seq_buf_get_buf() and seq_buf_commit() helper functions Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 12/15] seq-buf: Make seq_buf_bprintf() conditional on CONFIG_BINARY_PRINTF Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 13/15] seq_buf: Move the seq_buf code to lib/ Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 14/15] printk: Add per_cpu printk func to allow printk to be diverted Steven Rostedt
2014-11-20 15:21 ` [for-next][PATCH 15/15] x86/nmi: Perform a safe NMI stack trace on all CPUs Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20141120152717.083069310@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=jkosina@suse.cz \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=pmladek@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®