Message ID | 20221018202734.140489-1-Jason@zx2c4.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp2148502wrs; Tue, 18 Oct 2022 13:33:02 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5m4Nb2/RczEJA4GpV2Pv8iaC5NEYRVMY+JTznpFoCU/s76qjyqsoB4u92zWqHtKYJQA4ST X-Received: by 2002:a17:907:80a:b0:783:2585:5d73 with SMTP id wv10-20020a170907080a00b0078325855d73mr3924466ejb.642.1666125182237; Tue, 18 Oct 2022 13:33:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666125182; cv=none; d=google.com; s=arc-20160816; b=rR8J6EYHMgwa1bvmaZET+972i03B5oR/0WYXJPuvuEEL2J2elANurg+UlP2oM+P15Q 3q6vPsIwxJEVdOOPEmagrsx4Vmdv71oCrQFKqCdVJI8u6PL5TJ7pjhsnUABJVtqop76m 2c77c84Cuq+JOXtNGxN8wVh8zYO1fmOKT08Ca7qIkTcfSXs9yCznkgEUez6HBt+yEHWq XB18LFqEIy9ZhG46G7Bsf4IhCb9KtUBZL0LJZGbx8VSIG+rncXO6L2ek3JFjzXI2gL2A cwbtzHFXHhq06TwXzf2c61HKEKwadaSW/WyNovoLXj4XnHlpxB5x9jsTDBfYqp/hpNcL fFUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Z9dNg5RUH3ZUYvEP9bPk55wn/6KP5eKiCL7817xOPJo=; b=ToIEU8nNWlSqo5XhmbTBE/5PD8htri3XSDWvfEskV4tNCzDZ5k2VDbD1A9JRN/Bnuk vbc2h4amoevKQD5vyUdzCEt8KHziy5yeogn1n+ZUVtkYAoxCDe3JqkZUYOWsVpav4AZa Kk7K+0kmd1BJWtR/l583fJg70Ct7J0g+MgbhoaUhyq9Cxpazl83jdZAZd7uer3d38Ara BF7VQWHVWhCWlTzOGPMy7L0vuUOee8/ovU/JECW6T3KNeBdNBxHBnWG/Ef0aaEyMgDgt QEW2jUB+csZ2XwDFRjlf1K8zAJrhArx73KISxjn9G3x8q9Aw6LIiudNDVhPKKotSPN03 DWYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=AfpjIH9c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hq8-20020a1709073f0800b0078c37681f89si15454549ejc.650.2022.10.18.13.32.37; Tue, 18 Oct 2022 13:33:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=AfpjIH9c; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229919AbiJRU1t (ORCPT <rfc822;toshivichauhan@gmail.com> + 99 others); Tue, 18 Oct 2022 16:27:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229604AbiJRU1r (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 18 Oct 2022 16:27:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A4D47F13F; Tue, 18 Oct 2022 13:27:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 05B7D616EA; Tue, 18 Oct 2022 20:27:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A16D7C433D6; Tue, 18 Oct 2022 20:27:45 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="AfpjIH9c" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1666124863; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Z9dNg5RUH3ZUYvEP9bPk55wn/6KP5eKiCL7817xOPJo=; b=AfpjIH9cDCFgTUBq9oDwFF23ExG/kMnTNG/BdqNSodzUqas/Qs1Ff+i/Tkfdk3qCikxF2A Gf1m5C/q+h9bIvjQfT62a63b1Gd1YOBJqzdqAtD7sPIpvc6Ugpa1AJlT5JSGx84LUXVQ4o nTBiOSN2Eqnjn7VirEb05LDPLfwmF2E= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 3a8f4031 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 18 Oct 2022 20:27:43 +0000 (UTC) From: "Jason A. Donenfeld" <Jason@zx2c4.com> To: linux-wireless@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>, Andrew Morton <akpm@linux-foundation.org>, Andy Shevchenko <andriy.shevchenko@linux.intel.com>, Stanislaw Gruszka <stf_xl@wp.pl>, Helmut Schaa <helmut.schaa@googlemail.com>, Kalle Valo <kvalo@kernel.org> Subject: [PATCH] wifi: rt2x00: use explicitly signed type for clamping Date: Tue, 18 Oct 2022 14:27:34 -0600 Message-Id: <20221018202734.140489-1-Jason@zx2c4.com> In-Reply-To: <202210190108.ESC3pc3D-lkp@intel.com> References: <202210190108.ESC3pc3D-lkp@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747058878815736913?= X-GMAIL-MSGID: =?utf-8?q?1747058878815736913?= |
Series |
wifi: rt2x00: use explicitly signed type for clamping
|
|
Commit Message
Jason A. Donenfeld
Oct. 18, 2022, 8:27 p.m. UTC
On some platforms, `char` is unsigned, which makes casting -7 to char
overflow, which in turn makes the clamping operation bogus. Instead,
deal with an explicit `s8` type, so that the comparison is always
signed, and return an s8 result from the function as well. Note that
this function's result is assigned to a `short`, which is always signed.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Stanislaw Gruszka <stf_xl@wp.pl>
Cc: Helmut Schaa <helmut.schaa@googlemail.com>
Cc: Kalle Valo <kvalo@kernel.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
drivers/net/wireless/ralink/rt2x00/rt2800lib.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
Comments
On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > On some platforms, `char` is unsigned, which makes casting -7 to char > overflow, which in turn makes the clamping operation bogus. Instead, > deal with an explicit `s8` type, so that the comparison is always > signed, and return an s8 result from the function as well. Note that > this function's result is assigned to a `short`, which is always signed. Why not to use short? See my patch I just sent.
On Tue, Oct 18, 2022 at 11:40:54PM +0300, Andy Shevchenko wrote: > On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > > On some platforms, `char` is unsigned, which makes casting -7 to char > > overflow, which in turn makes the clamping operation bogus. Instead, > > deal with an explicit `s8` type, so that the comparison is always > > signed, and return an s8 result from the function as well. Note that > > this function's result is assigned to a `short`, which is always signed. > > Why not to use short? See my patch I just sent. Trying to have the most minimal change here that doesn't rock the boat. I'm not out to rewrite the driver. I don't know the original author's rationales. This patch here is correct and will generate the same code as before on architectures where it wasn't broken. However, if you want your "change the codegen" patch to be taken seriously, you should probably send it to the wireless maintainers like this one, and they can decide. Personally, I don't really care either way. Jason
On Tue, Oct 18, 2022 at 02:52:43PM -0600, Jason A. Donenfeld wrote: > On Tue, Oct 18, 2022 at 11:40:54PM +0300, Andy Shevchenko wrote: > > On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > > > On some platforms, `char` is unsigned, which makes casting -7 to char > > > overflow, which in turn makes the clamping operation bogus. Instead, > > > deal with an explicit `s8` type, so that the comparison is always > > > signed, and return an s8 result from the function as well. Note that > > > this function's result is assigned to a `short`, which is always signed. > > > > Why not to use short? See my patch I just sent. > > Trying to have the most minimal change here that doesn't rock the boat. > I'm not out to rewrite the driver. I don't know the original author's > rationales. This patch here is correct and will generate the same code > as before on architectures where it wasn't broken. > > However, if you want your "change the codegen" patch to be taken > seriously, you should probably send it to the wireless maintainers like > this one, and they can decide. Personally, I don't really care either > way. I have checked the code paths there and I found no evidence that short can't be used. That's why my patch. Okay, I will formally send it to the corresponding maintainers. But if they want, they can always download this thread using `b4` tool and at least comment on it.
On Tue, Oct 18, 2022 at 2:57 PM Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote: > > On Tue, Oct 18, 2022 at 02:52:43PM -0600, Jason A. Donenfeld wrote: > > On Tue, Oct 18, 2022 at 11:40:54PM +0300, Andy Shevchenko wrote: > > > On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > > > > On some platforms, `char` is unsigned, which makes casting -7 to char > > > > overflow, which in turn makes the clamping operation bogus. Instead, > > > > deal with an explicit `s8` type, so that the comparison is always > > > > signed, and return an s8 result from the function as well. Note that > > > > this function's result is assigned to a `short`, which is always signed. > > > > > > Why not to use short? See my patch I just sent. > > > > Trying to have the most minimal change here that doesn't rock the boat. > > I'm not out to rewrite the driver. I don't know the original author's > > rationales. This patch here is correct and will generate the same code > > as before on architectures where it wasn't broken. > > > > However, if you want your "change the codegen" patch to be taken > > seriously, you should probably send it to the wireless maintainers like > > this one, and they can decide. Personally, I don't really care either > > way. > > I have checked the code paths there and I found no evidence that short can't be > used. That's why my patch. Do you have a rationale why you want to change codegen? Jason
On Tue, 18 Oct 2022 14:27:34 -0600 "Jason A. Donenfeld" <Jason@zx2c4.com> wrote: > On some platforms, `char` is unsigned, which makes casting -7 to char > overflow, which in turn makes the clamping operation bogus. Instead, > deal with an explicit `s8` type, so that the comparison is always > signed, and return an s8 result from the function as well. Note that > this function's result is assigned to a `short`, which is always signed. Thanks. I'll grab this for now to make -next happier. Stephen will tell us when the patch (or one like it) appears via the wireless tree.
On Tue, Oct 18, 2022 at 02:58:30PM -0600, Jason A. Donenfeld wrote: > On Tue, Oct 18, 2022 at 2:57 PM Andy Shevchenko > <andriy.shevchenko@linux.intel.com> wrote: > > On Tue, Oct 18, 2022 at 02:52:43PM -0600, Jason A. Donenfeld wrote: > > > On Tue, Oct 18, 2022 at 11:40:54PM +0300, Andy Shevchenko wrote: > > > > On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > > > > > On some platforms, `char` is unsigned, which makes casting -7 to char > > > > > overflow, which in turn makes the clamping operation bogus. Instead, > > > > > deal with an explicit `s8` type, so that the comparison is always > > > > > signed, and return an s8 result from the function as well. Note that > > > > > this function's result is assigned to a `short`, which is always signed. > > > > > > > > Why not to use short? See my patch I just sent. > > > > > > Trying to have the most minimal change here that doesn't rock the boat. > > > I'm not out to rewrite the driver. I don't know the original author's > > > rationales. This patch here is correct and will generate the same code > > > as before on architectures where it wasn't broken. > > > > > > However, if you want your "change the codegen" patch to be taken > > > seriously, you should probably send it to the wireless maintainers like > > > this one, and they can decide. Personally, I don't really care either > > > way. > > > > I have checked the code paths there and I found no evidence that short can't be > > used. That's why my patch. > > Do you have a rationale why you want to change codegen? It's not a hot path as far as I understand and keeping data types aligned seems to me worth it even if codegen is changed. IS it so awful with short?
From: Andy Shevchenko > Sent: 18 October 2022 22:10 .... > It's not a hot path as far as I understand and keeping data types aligned seems > to me worth it even if codegen is changed. IS it so awful with short? (without looking at the generated code) Why is it even short, what is wrong with int? It is extremely unlikely that the code requires any calculation results be masked to 8 (or 16) bits, but using s8 or s16 requires the compiler emit code to mask any calculated values. Remember, all C arithmetic requires promoting the values to (at least) int. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > On some platforms, `char` is unsigned, which makes casting -7 to char > overflow, which in turn makes the clamping operation bogus. Instead, > deal with an explicit `s8` type, so that the comparison is always > signed, and return an s8 result from the function as well. Note that > this function's result is assigned to a `short`, which is always signed. > > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > Cc: Stanislaw Gruszka <stf_xl@wp.pl> > Cc: Helmut Schaa <helmut.schaa@googlemail.com> > Cc: Kalle Valo <kvalo@kernel.org> > Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> I prefer s8 just because is shorter name than short :-) Acked-by: Stanislaw Gruszka <stf_xl@wp.pl>
On Wed, Oct 19, 2022 at 10:52:19AM +0200, Stanislaw Gruszka wrote: > On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > > On some platforms, `char` is unsigned, which makes casting -7 to char > > overflow, which in turn makes the clamping operation bogus. Instead, > > deal with an explicit `s8` type, so that the comparison is always > > signed, and return an s8 result from the function as well. Note that > > this function's result is assigned to a `short`, which is always signed. > > > > Cc: Andrew Morton <akpm@linux-foundation.org> > > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > > Cc: Stanislaw Gruszka <stf_xl@wp.pl> > > Cc: Helmut Schaa <helmut.schaa@googlemail.com> > > Cc: Kalle Valo <kvalo@kernel.org> > > Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> > > I prefer s8 just because is shorter name than short :-) Shouldn't the corresponding data structure type be fixed accordingly?
On Wed, Oct 19, 2022 at 02:04:57PM +0300, Andy Shevchenko wrote: > On Wed, Oct 19, 2022 at 10:52:19AM +0200, Stanislaw Gruszka wrote: > > On Tue, Oct 18, 2022 at 02:27:34PM -0600, Jason A. Donenfeld wrote: > > > On some platforms, `char` is unsigned, which makes casting -7 to char > > > overflow, which in turn makes the clamping operation bogus. Instead, > > > deal with an explicit `s8` type, so that the comparison is always > > > signed, and return an s8 result from the function as well. Note that > > > this function's result is assigned to a `short`, which is always signed. > > > > > > Cc: Andrew Morton <akpm@linux-foundation.org> > > > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> > > > Cc: Stanislaw Gruszka <stf_xl@wp.pl> > > > Cc: Helmut Schaa <helmut.schaa@googlemail.com> > > > Cc: Kalle Valo <kvalo@kernel.org> > > > Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> > > > > I prefer s8 just because is shorter name than short :-) > > Shouldn't the corresponding data structure type be fixed accordingly? We can change types of channel_info default_power* fields in rt2x00.h, but I'm a bit reluctant to do so, as I'm afraid this could change actual power values sent to the hardware and will require careful verification. Regards Stanislaw
diff --git a/drivers/net/wireless/ralink/rt2x00/rt2800lib.c b/drivers/net/wireless/ralink/rt2x00/rt2800lib.c index cbbb1a4849cf..e233ef9892b3 100644 --- a/drivers/net/wireless/ralink/rt2x00/rt2800lib.c +++ b/drivers/net/wireless/ralink/rt2x00/rt2800lib.c @@ -4035,23 +4035,23 @@ static void rt2800_iq_calibrate(struct rt2x00_dev *rt2x00dev, int channel) rt2800_bbp_write(rt2x00dev, 159, cal != 0xff ? cal : 0); } -static char rt2800_txpower_to_dev(struct rt2x00_dev *rt2x00dev, +static s8 rt2800_txpower_to_dev(struct rt2x00_dev *rt2x00dev, unsigned int channel, - char txpower) + s8 txpower) { if (rt2x00_rt(rt2x00dev, RT3593) || rt2x00_rt(rt2x00dev, RT3883)) txpower = rt2x00_get_field8(txpower, EEPROM_TXPOWER_ALC); if (channel <= 14) - return clamp_t(char, txpower, MIN_G_TXPOWER, MAX_G_TXPOWER); + return clamp_t(s8, txpower, MIN_G_TXPOWER, MAX_G_TXPOWER); if (rt2x00_rt(rt2x00dev, RT3593) || rt2x00_rt(rt2x00dev, RT3883)) - return clamp_t(char, txpower, MIN_A_TXPOWER_3593, + return clamp_t(s8, txpower, MIN_A_TXPOWER_3593, MAX_A_TXPOWER_3593); else - return clamp_t(char, txpower, MIN_A_TXPOWER, MAX_A_TXPOWER); + return clamp_t(s8, txpower, MIN_A_TXPOWER, MAX_A_TXPOWER); } static void rt3883_bbp_adjust(struct rt2x00_dev *rt2x00dev,