From: Tiffany Yang <ynaffit@google.com>
To: linux-kernel@vger.kernel.org
Cc: "Carlos Llamas" <cmllamas@google.com>,
"Joel Fernandes" <joelagnelf@nvidia.com>,
kernel-team@android.com,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Arve Hjønnevåg" <arve@android.com>,
"Todd Kjos" <tkjos@android.com>,
"Martijn Coenen" <maco@android.com>,
"Christian Brauner" <brauner@kernel.org>,
"Suren Baghdasaryan" <surenb@google.com>,
"Kees Cook" <kees@kernel.org>
Subject: [PATCH v2 2/2] binder: Use seq_buf in binder_alloc kunit tests
Date: Tue, 22 Jul 2025 16:45:07 -0700 [thread overview]
Message-ID: <20250722234508.232228-2-ynaffit@google.com> (raw)
In-Reply-To: <20250722234508.232228-1-ynaffit@google.com>
Replace instances of snprintf with seq_buf functions, as suggested by
Kees [1].
[1] https://lore.kernel.org/all/202507160743.15E8044@keescook/
Fixes: d1934ed9803c ("binder: encapsulate individual alloc test cases")
Suggested-by: Kees Cook <kees@kernel.org>
Cc: Joel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: Tiffany Yang <ynaffit@google.com>
---
This patch is based on top of char-misc-next.
---
drivers/android/tests/binder_alloc_kunit.c | 46 ++++++++++------------
1 file changed, 21 insertions(+), 25 deletions(-)
diff --git a/drivers/android/tests/binder_alloc_kunit.c b/drivers/android/tests/binder_alloc_kunit.c
index f8c05bf15c2d..9b884d977f76 100644
--- a/drivers/android/tests/binder_alloc_kunit.c
+++ b/drivers/android/tests/binder_alloc_kunit.c
@@ -15,6 +15,7 @@
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/mman.h>
+#include <linux/seq_buf.h>
#include <linux/sizes.h>
#include "../binder_alloc.h"
@@ -107,40 +108,33 @@ static const char *const buf_end_align_type_strs[LOOP_END] = {
};
struct binder_alloc_test_case_info {
+ char alignments[ALIGNMENTS_BUFLEN];
+ struct seq_buf alignments_sb;
size_t *buffer_sizes;
int *free_sequence;
- char alignments[ALIGNMENTS_BUFLEN];
bool front_pages;
};
-static void stringify_free_seq(struct kunit *test, int *seq, char *buf,
- size_t buf_len)
+static void stringify_free_seq(struct kunit *test, int *seq, struct seq_buf *sb)
{
- size_t bytes = 0;
int i;
- for (i = 0; i < BUFFER_NUM; i++) {
- bytes += snprintf(buf + bytes, buf_len - bytes, "[%d]", seq[i]);
- if (bytes >= buf_len)
- break;
- }
- KUNIT_EXPECT_LT(test, bytes, buf_len);
+ for (i = 0; i < BUFFER_NUM; i++)
+ seq_buf_printf(sb, "[%d]", seq[i]);
+
+ KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb));
}
static void stringify_alignments(struct kunit *test, int *alignments,
- char *buf, size_t buf_len)
+ struct seq_buf *sb)
{
- size_t bytes = 0;
int i;
- for (i = 0; i < BUFFER_NUM; i++) {
- bytes += snprintf(buf + bytes, buf_len - bytes, "[ %d:%s ]", i,
- buf_end_align_type_strs[alignments[i]]);
- if (bytes >= buf_len)
- break;
- }
+ for (i = 0; i < BUFFER_NUM; i++)
+ seq_buf_printf(sb, "[ %d:%s ]", i,
+ buf_end_align_type_strs[alignments[i]]);
- KUNIT_EXPECT_LT(test, bytes, buf_len);
+ KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb));
}
static bool check_buffer_pages_allocated(struct kunit *test,
@@ -311,19 +305,20 @@ static void permute_frees(struct kunit *test, struct binder_alloc *alloc,
int i;
if (index == BUFFER_NUM) {
- char freeseq_buf[FREESEQ_BUFLEN];
+ DECLARE_SEQ_BUF(freeseq_sb, FREESEQ_BUFLEN);
case_failed = binder_alloc_test_alloc_free(test, alloc, tc, end);
*runs += 1;
*failures += case_failed;
if (case_failed || PRINT_ALL_CASES) {
- stringify_free_seq(test, tc->free_sequence, freeseq_buf,
- FREESEQ_BUFLEN);
+ stringify_free_seq(test, tc->free_sequence,
+ &freeseq_sb);
kunit_err(test, "case %lu: [%s] | %s - %s - %s", *runs,
case_failed ? "FAILED" : "PASSED",
tc->front_pages ? "front" : "back ",
- tc->alignments, freeseq_buf);
+ seq_buf_str(&tc->alignments_sb),
+ seq_buf_str(&freeseq_sb));
}
return;
@@ -383,8 +378,9 @@ static void gen_buf_offsets(struct kunit *test, struct binder_alloc *alloc,
if (index == BUFFER_NUM) {
struct binder_alloc_test_case_info tc = {0};
- stringify_alignments(test, alignments, tc.alignments,
- ALIGNMENTS_BUFLEN);
+ seq_buf_init(&tc.alignments_sb, tc.alignments,
+ ALIGNMENTS_BUFLEN);
+ stringify_alignments(test, alignments, &tc.alignments_sb);
gen_buf_sizes(test, alloc, &tc, end_offset, runs, failures);
return;
--
2.50.0.727.gbf7dc18ff4-goog
next prev parent reply other threads:[~2025-07-22 23:45 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-22 23:45 [PATCH v2 1/2] binder: Add copyright notice to new kunit files Tiffany Yang
2025-07-22 23:45 ` Tiffany Yang [this message]
2025-07-23 7:32 ` [PATCH v2 2/2] binder: Use seq_buf in binder_alloc kunit tests Kees Cook
2025-07-25 19:38 ` Carlos Llamas
2025-07-25 19:38 ` [PATCH v2 1/2] binder: Add copyright notice to new kunit files Carlos Llamas
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=20250722234508.232228-2-ynaffit@google.com \
--to=ynaffit@google.com \
--cc=arve@android.com \
--cc=brauner@kernel.org \
--cc=cmllamas@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=joelagnelf@nvidia.com \
--cc=kees@kernel.org \
--cc=kernel-team@android.com \
--cc=linux-kernel@vger.kernel.org \
--cc=maco@android.com \
--cc=surenb@google.com \
--cc=tkjos@android.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.