From patchwork Fri Nov 29 21:25:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Hauser X-Patchwork-Id: 13888947 Received: from sonic305-21.consmr.mail.ir2.yahoo.com (sonic305-21.consmr.mail.ir2.yahoo.com [77.238.177.83]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5E073224 for ; Fri, 29 Nov 2024 21:35:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=77.238.177.83 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732916138; cv=none; b=PVGPWqsb0zUyLrrIqukzxOZJMNxbxaRRrpH/8QnskP7OEFS+LUOwd4VTpot6a9B9KI5v90zF4LK3/P9hKWfdYdIDsxN9RgR9w1MiZweOoXHqi2lXeZQDJx6Niux/f1T9wJDGB5UIOFuGxrPa5agN5YEYJS2PzFNEz8poBHSF2LE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732916138; c=relaxed/simple; bh=SxiSf8BuguILzkj9Rs29j4f5ZlVFhtCZpSfmR0MOXxw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZlzhijtT6Mu5WgDhEd+u/gIcwkneAsR31RgTBeHA8G8nnALw99zzrQPWj1rEPDHjQQ8kshPxfpfAftI3x+ecifaemvZg6zVcQVDlUb5n7FARUZKnlkeCDIHOjzt306Lc8acvjBEqJ3GMl/t6qYLC1RQfqr9lTXBNayLDcSBXhCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=rocketmail.com; spf=pass smtp.mailfrom=rocketmail.com; dkim=pass (2048-bit key) header.d=rocketmail.com header.i=@rocketmail.com header.b=g0ASI+3A; arc=none smtp.client-ip=77.238.177.83 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=rocketmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rocketmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rocketmail.com header.i=@rocketmail.com header.b="g0ASI+3A" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rocketmail.com; s=s2048; t=1732916135; bh=G0smavV7bDd9712eK/n12vhKXxEOPpjsobTA/EgGKuc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Subject:Reply-To; b=g0ASI+3ApKYzVLRovITotvvPfYT2u1Vt8OeE8D73xRzZjo8fRlygOzqMnFcgbClyvcJYzHwpkqoJYx9z9ebwGQ5U9BdMesrZ7L5DP1RdL50Ex+ozBTfGzkMbYQRHUyL1hKS6SxQ4UDbNqpbj60YyMaEhv9M1VbKSJaF3P6IRQ3WfD3y0VQc+9F6JHErom0xln5QbGSY6yT93n7Kn1ArBESzU5j/Tg7TyS0Hu+fI5sWAFympUtC8ir5q5DT9gEX3lzGHgbgaRbXCt5RCubvkcZrYBrJ4sJ9EYNYMEjAgqWtz7CykEIcr/qWS9dDiOGh1SS+HSQ8VRWV3tuJOhLW8LNA== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1732916135; bh=sZaadzcqns39VcIObWnhZg5StWeh7smTT3jnGoiQeJ3=; h=X-Sonic-MF:From:To:Subject:Date:From:Subject; b=jSwbjj9kjldCx4P5Gk30NeBVkk9zJSWCeWspvUK0PEbwJKeGS6qquhLmKoJClR3u/8/OqnipiENvrYN5rMb9+6/ud9NRLLTam5U0xhWUyUQPteAvUmCPcagLihhrZ83IxLfVeJ43ixMOJDoF/4GX9WvSFbx/e4KxVVthfNGZrUlpE8ImqufWf1mai8Qjn0u5ZBZNYBVHVXenOsRWcOGahPxKqm2ZbRq+7JAcxF4hyxqiaS5QugHNRyKD/A7Su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azA21WjVEauJ9Wv6fE_auEAx9YSHgmXG.U2HQMuOpcQr4re7DKtDqwCITQOwPGVBURzF_vP dwAGIRwBD7_xrOkJ2yWihKubjABTXLsL3GI_Iq7icrPKfmZS4kq9nrkAnGI_8kay5JIlhUminsdp liyFJ2JshiLVmUnLTKx10rQmXM9z7qQKg8YlY2WcjLYATZAgKIJzQB5Nt8SiPWm6PzF8hVwrmuHe pyHW2STmhVnxCZsjioi3.OX26yLVRDk3npZElv_8xiw5r8vaa9ounRxqqvAf04QzchXYTmF7mEV9 gj5ikfMqNd1vwD3dibKiTo81tU9xcQi1mIbDa9mW.Jg-- X-Sonic-MF: X-Sonic-ID: d0072ecb-2302-422b-a388-5525090a8772 Received: from sonic.gate.mail.ne1.yahoo.com by sonic305.consmr.mail.ir2.yahoo.com with HTTP; Fri, 29 Nov 2024 21:35:35 +0000 Received: by hermes--production-ir2-c694d79d9-mr45v (Yahoo Inc. Hermes SMTP Server) with ESMTPA ID 2edcbec70aefc2846e815ac55c2b6c1e; Fri, 29 Nov 2024 21:25:25 +0000 (UTC) From: Jakob Hauser To: Jonathan Cameron , Lars-Peter Clausen Cc: David Laight , Linus Walleij , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jakob Hauser , kernel test robot Subject: [PATCH v2] iio: magnetometer: yas530: Use signed integer type for clamp limits Date: Fri, 29 Nov 2024 22:25:07 +0100 Message-Id: <11609b2243c295d65ab4d47e78c239d61ad6be75.1732914810.git.jahau@rocketmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <11609b2243c295d65ab4d47e78c239d61ad6be75.1732914810.git.jahau@rocketmail.com> References: <11609b2243c295d65ab4d47e78c239d61ad6be75.1732914810.git.jahau@rocketmail.com> Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In the function yas537_measure() there is a clamp_val() with limits of -BIT(13) and BIT(13) - 1. The input clamp value h[] is of type s32. The BIT() is of type unsigned long integer due to its define in include/vdso/bits.h. The lower limit -BIT(13) is recognized as -8192 but expressed as an unsigned long integer. The size of an unsigned long integer differs between 32-bit and 64-bit architectures. Converting this to type s32 may lead to undesired behavior. Additionally, in the calculation lines h[0], h[1] and h[2] the unsigned long integer divisor BIT(13) causes an unsigned division, shifting the left-hand side of the equation back and forth, possibly ending up in large positive values instead of negative values on 32-bit architectures. To solve those two issues, declare a signed integer with a value of BIT(13). There is another omission in the clamp line: clamp_val() returns a value and it's going nowhere here. Self-assign it to h[i] to make use of the clamp macro. Finally, replace clamp_val() macro by clamp() because after changing the limits from type unsigned long integer to signed integer it's fine that way. Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202411230458.dhZwh3TT-lkp@intel.com/ Closes: https://lore.kernel.org/oe-kbuild-all/202411282222.oF0B4110-lkp@intel.com/ Fixes: 65f79b501030 ("iio: magnetometer: yas530: Add YAS537 variant") Cc: David Laight Signed-off-by: Jakob Hauser --- The patch is based on torvalds/linux v6.12. The calculation lines h[0], h[1] and h[2] exceed the limit of 80 characters per line. In terms of readability I would prefer to keep it that way. Changes in v2: - Self-assigned the clamp macro to h[i]. - Changed from clamp_val() macro to clamp(). - In commit message added issues on divisor BIT(13) and missing clamp assignment. - In tags added another (duplicate) report by the kernel test robot. Link to v1: https://lore.kernel.org/linux-iio/20241126234021.19749-1-jahau@rocketmail.com/T/#t --- drivers/iio/magnetometer/yamaha-yas530.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/iio/magnetometer/yamaha-yas530.c b/drivers/iio/magnetometer/yamaha-yas530.c index 65011a8598d3..c55a38650c0d 100644 --- a/drivers/iio/magnetometer/yamaha-yas530.c +++ b/drivers/iio/magnetometer/yamaha-yas530.c @@ -372,6 +372,7 @@ static int yas537_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y u8 data[8]; u16 xy1y2[3]; s32 h[3], s[3]; + int half_range = BIT(13); int i, ret; mutex_lock(&yas5xx->lock); @@ -406,13 +407,13 @@ static int yas537_measure(struct yas5xx *yas5xx, u16 *t, u16 *x, u16 *y1, u16 *y /* The second version of YAS537 needs to include calibration coefficients */ if (yas5xx->version == YAS537_VERSION_1) { for (i = 0; i < 3; i++) - s[i] = xy1y2[i] - BIT(13); - h[0] = (c->k * (128 * s[0] + c->a2 * s[1] + c->a3 * s[2])) / BIT(13); - h[1] = (c->k * (c->a4 * s[0] + c->a5 * s[1] + c->a6 * s[2])) / BIT(13); - h[2] = (c->k * (c->a7 * s[0] + c->a8 * s[1] + c->a9 * s[2])) / BIT(13); + s[i] = xy1y2[i] - half_range; + h[0] = (c->k * (128 * s[0] + c->a2 * s[1] + c->a3 * s[2])) / half_range; + h[1] = (c->k * (c->a4 * s[0] + c->a5 * s[1] + c->a6 * s[2])) / half_range; + h[2] = (c->k * (c->a7 * s[0] + c->a8 * s[1] + c->a9 * s[2])) / half_range; for (i = 0; i < 3; i++) { - clamp_val(h[i], -BIT(13), BIT(13) - 1); - xy1y2[i] = h[i] + BIT(13); + h[i] = clamp(h[i], -half_range, half_range - 1); + xy1y2[i] = h[i] + half_range; } }