From: Jani Nikula <jani.nikula@intel.com>
To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org
Cc: intel-gfx@lists.freedesktop.org, jani.nikula@intel.com,
Kees Cook <keescook@chromium.org>,
Andy Shevchenko <andy@kernel.org>
Subject: [PATCH v2 1/2] string: add mem_is_zero() helper to check if memory area is all zeros
Date: Wed, 14 Aug 2024 13:00:34 +0300 [thread overview]
Message-ID: <20240814100035.3100852-1-jani.nikula@intel.com> (raw)
Almost two thirds of the memchr_inv() usages check if the memory area is
all zeros, with no interest in where in the buffer the first non-zero
byte is located. Checking for !memchr_inv(s, 0, n) is also not very
intuitive or discoverable. Add an explicit mem_is_zero() helper for this
use case.
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
---
Cc: Kees Cook <keescook@chromium.org>
Cc: Andy Shevchenko <andy@kernel.org>
---
include/linux/string.h | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 9edace076ddb..5855c5626b4b 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -279,6 +279,18 @@ static inline void memcpy_flushcache(void *dst, const void *src, size_t cnt)
void *memchr_inv(const void *s, int c, size_t n);
char *strreplace(char *str, char old, char new);
+/**
+ * mem_is_zero - Check if an area of memory is all 0's.
+ * @s: The memory area
+ * @n: The size of the area
+ *
+ * Return: True if the area of memory is all 0's.
+ */
+static inline bool mem_is_zero(const void *s, size_t n)
+{
+ return !memchr_inv(s, 0, n);
+}
+
extern void kfree_const(const void *x);
extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
--
2.39.2
next reply other threads:[~2024-08-14 10:00 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-14 10:00 Jani Nikula [this message]
2024-08-14 10:00 ` [PATCH v2 2/2] drm: use mem_is_zero() instead of !memchr_inv(s, 0, n) Jani Nikula
2024-08-15 16:07 ` Kees Cook
2024-08-16 13:12 ` Jani Nikula
2024-08-14 10:34 ` ✗ Fi.CI.SPARSE: warning for series starting with [v2,1/2] string: add mem_is_zero() helper to check if memory area is all zeros Patchwork
2024-08-14 10:49 ` ✓ Fi.CI.BAT: success " Patchwork
2024-08-14 12:44 ` [PATCH v2 1/2] " Andy Shevchenko
2024-08-14 16:04 ` ✗ Fi.CI.IGT: failure for series starting with [v2,1/2] " Patchwork
2024-08-15 16:07 ` [PATCH v2 1/2] " Kees Cook
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=20240814100035.3100852-1-jani.nikula@intel.com \
--to=jani.nikula@intel.com \
--cc=andy@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=intel-gfx@lists.freedesktop.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
/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