From patchwork Wed Aug 14 10:00:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jani Nikula X-Patchwork-Id: 13763221 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D71F5C531DC for ; Wed, 14 Aug 2024 10:00:51 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5368C10E245; Wed, 14 Aug 2024 10:00:51 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="cxiTa8MA"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by gabe.freedesktop.org (Postfix) with ESMTPS id DBEBE10E21D; Wed, 14 Aug 2024 10:00:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1723629650; x=1755165650; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=+3bm3qAXRhIk14a/ZVhcXtY4VT/IWncFvTwGUHAz/Wk=; b=cxiTa8MAmuypfbKMcU4tuIaO0P0uILqcfggGHI4o8WrVJ1fKhYOLjmRv oPejx8Wq4hOGlm/ytuCuMd2o0ChWVJRdQYZ7vqnDY6motjZIWTbf+oi9j yVnuOar3kv9+bHmbJFMtRzas4MH73KnyVpsrrJJd1W9Qxm1VZWpPfXLRm W2foFWvuydei9W9MNLnlGguRS7iB8uOvLvyLdc5P4iUBYXoE3CerPCjq1 9K49N8jbHsyFeG0D+IjS9tw/ezPdjeFUsTeGeL0HprExFaHUj1iyVgMCX aUBTCjfW/mgaGvYCGH/okyeN87yG5B29WcsHLOfVjygFtGkTtVbZoNzOk g==; X-CSE-ConnectionGUID: T7TKO82TTEeOvdvj/jr24Q== X-CSE-MsgGUID: ogW13fkKQdqBjrdn69jvFw== X-IronPort-AV: E=McAfee;i="6700,10204,11163"; a="44355137" X-IronPort-AV: E=Sophos;i="6.09,145,1716274800"; d="scan'208";a="44355137" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Aug 2024 03:00:49 -0700 X-CSE-ConnectionGUID: GONln07nRUaVUHd6sNRnTw== X-CSE-MsgGUID: +qU923aYRgKL1ChpRIvXBg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,145,1716274800"; d="scan'208";a="58922107" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO localhost) ([10.245.246.65]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Aug 2024 03:00:47 -0700 From: Jani Nikula To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: intel-gfx@lists.freedesktop.org, jani.nikula@intel.com, Kees Cook , Andy Shevchenko 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 Message-Id: <20240814100035.3100852-1-jani.nikula@intel.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" 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 Reviewed-by: Andy Shevchenko Reviewed-by: Kees Cook --- Cc: Kees Cook Cc: Andy Shevchenko --- 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; From patchwork Wed Aug 14 10:00:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jani Nikula X-Patchwork-Id: 13763222 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B2093C531DF for ; Wed, 14 Aug 2024 10:00:56 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 58F7910E25A; Wed, 14 Aug 2024 10:00:56 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="M2cpNHXM"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) by gabe.freedesktop.org (Postfix) with ESMTPS id E399510E25A; Wed, 14 Aug 2024 10:00:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1723629656; x=1755165656; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fbyDemeCEd+Wj+VZzePbEzVldroXXA4In/tCe5qY8c8=; b=M2cpNHXMfyXR6TfnOH2OZ0IC40gJrIird4uXrgeatSi3Pk9ydq3HsEsQ 54xg51dMgTytovRLpYdyI7bddoD1uLAYV/B4OnVHpWHnKvexsL8euzWeH o91oKSglDShnrqGy5ucMvwqn26adTB73nRyKwv7Wx3DiLulOaaJ1G8vIX QLlj82JdvYHP6C/LSKxxAxadPeDCt8SuYPLB4GASnACwIshbjdOvsGP4s faxp7Q8ppKYtNr+QFvjUWsH60fdurlKEXCuFtUDcPS7anVmzpHVx7kckM XvkKpxSeF3Wxft0ACDHGCoXMI2lHcCFKFUMQDINDB0tLWxDIcQefqVmT1 w==; X-CSE-ConnectionGUID: BzSUUCpjRWaRDDPV0gEWPQ== X-CSE-MsgGUID: zuAiKFdJRVqMzdMuNjMztw== X-IronPort-AV: E=McAfee;i="6700,10204,11163"; a="21993262" X-IronPort-AV: E=Sophos;i="6.09,145,1716274800"; d="scan'208";a="21993262" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Aug 2024 03:00:55 -0700 X-CSE-ConnectionGUID: 8wCEPYKcTne7qDTJ/5n0aw== X-CSE-MsgGUID: EQvajPwfSFCxcj/HMqie0g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,145,1716274800"; d="scan'208";a="63831566" Received: from fdefranc-mobl3.ger.corp.intel.com (HELO localhost) ([10.245.246.65]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Aug 2024 03:00:53 -0700 From: Jani Nikula To: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: intel-gfx@lists.freedesktop.org, jani.nikula@intel.com, Kees Cook , Andy Shevchenko Subject: [PATCH v2 2/2] drm: use mem_is_zero() instead of !memchr_inv(s, 0, n) Date: Wed, 14 Aug 2024 13:00:35 +0300 Message-Id: <20240814100035.3100852-2-jani.nikula@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240814100035.3100852-1-jani.nikula@intel.com> References: <20240814100035.3100852-1-jani.nikula@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" Use the mem_is_zero() helper where possible. Conversion done using cocci: | @@ | expression PTR; | expression SIZE; | @@ | | <... | ( | - memchr_inv(PTR, 0, SIZE) == NULL | + mem_is_zero(PTR, SIZE) | | | - !memchr_inv(PTR, 0, SIZE) | + mem_is_zero(PTR, SIZE) | | | - memchr_inv(PTR, 0, SIZE) | + !mem_is_zero(PTR, SIZE) | ) | ...> Signed-off-by: Jani Nikula Reviewed-by: Kees Cook --- v2: Exclude GUID conversions, which are covered in [1]. [1] https://lore.kernel.org/r/20240812122312.1567046-1-jani.nikula@intel.com Cc: Kees Cook Cc: Andy Shevchenko --- drivers/gpu/drm/drm_edid.c | 2 +- drivers/gpu/drm/i915/display/intel_dp.c | 2 +- drivers/gpu/drm/i915/display/intel_opregion.c | 2 +- drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c | 2 +- drivers/gpu/drm/imagination/pvr_device.h | 2 +- drivers/gpu/drm/udl/udl_edid.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/drm_edid.c b/drivers/gpu/drm/drm_edid.c index ff1e47a9c83e..855beafb76ff 100644 --- a/drivers/gpu/drm/drm_edid.c +++ b/drivers/gpu/drm/drm_edid.c @@ -1817,7 +1817,7 @@ static int edid_block_tag(const void *_block) static bool edid_block_is_zero(const void *edid) { - return !memchr_inv(edid, 0, EDID_LENGTH); + return mem_is_zero(edid, EDID_LENGTH); } static bool drm_edid_eq(const struct drm_edid *drm_edid, diff --git a/drivers/gpu/drm/i915/display/intel_dp.c b/drivers/gpu/drm/i915/display/intel_dp.c index 977f149551f6..6a0c7ae654f4 100644 --- a/drivers/gpu/drm/i915/display/intel_dp.c +++ b/drivers/gpu/drm/i915/display/intel_dp.c @@ -5184,7 +5184,7 @@ intel_dp_check_mst_status(struct intel_dp *intel_dp) ack[3] |= DP_TUNNELING_IRQ; } - if (!memchr_inv(ack, 0, sizeof(ack))) + if (mem_is_zero(ack, sizeof(ack))) break; if (!intel_dp_ack_sink_irq_esi(intel_dp, ack)) diff --git a/drivers/gpu/drm/i915/display/intel_opregion.c b/drivers/gpu/drm/i915/display/intel_opregion.c index dfa1d9f30d33..ff11836459de 100644 --- a/drivers/gpu/drm/i915/display/intel_opregion.c +++ b/drivers/gpu/drm/i915/display/intel_opregion.c @@ -1117,7 +1117,7 @@ const struct drm_edid *intel_opregion_get_edid(struct intel_connector *connector /* Validity corresponds to number of 128-byte blocks */ len = (opregion->asle_ext->phed & ASLE_PHED_EDID_VALID_MASK) * 128; - if (!len || !memchr_inv(edid, 0, len)) + if (!len || mem_is_zero(edid, len)) return NULL; drm_edid = drm_edid_alloc(edid, len); diff --git a/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c b/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c index 3527b8f446fe..2fda549dd82d 100644 --- a/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c +++ b/drivers/gpu/drm/i915/gem/selftests/i915_gem_dmabuf.c @@ -506,7 +506,7 @@ static int igt_dmabuf_export_vmap(void *arg) goto out; } - if (memchr_inv(ptr, 0, dmabuf->size)) { + if (!mem_is_zero(ptr, dmabuf->size)) { pr_err("Exported object not initialised to zero!\n"); err = -EINVAL; goto out; diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h index ecdd5767d8ef..b574e23d484b 100644 --- a/drivers/gpu/drm/imagination/pvr_device.h +++ b/drivers/gpu/drm/imagination/pvr_device.h @@ -668,7 +668,7 @@ pvr_ioctl_union_padding_check(void *instance, size_t union_offset, void *padding_start = ((u8 *)instance) + union_offset + member_size; size_t padding_size = union_size - member_size; - return !memchr_inv(padding_start, 0, padding_size); + return mem_is_zero(padding_start, padding_size); } /** diff --git a/drivers/gpu/drm/udl/udl_edid.c b/drivers/gpu/drm/udl/udl_edid.c index d67e6bf1f2ae..12f48ae17073 100644 --- a/drivers/gpu/drm/udl/udl_edid.c +++ b/drivers/gpu/drm/udl/udl_edid.c @@ -69,7 +69,7 @@ bool udl_probe_edid(struct udl_device *udl) * The adapter sends all-zeros if no monitor has been * connected. We consider anything else a connection. */ - return !!memchr_inv(hdr, 0, sizeof(hdr)); + return !mem_is_zero(hdr, sizeof(hdr)); } const struct drm_edid *udl_edid_read(struct drm_connector *connector)