Message ID | 20221021064453.3341050-1-gregkh@linuxfoundation.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp527468wrr; Thu, 20 Oct 2022 23:49:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7c4x9N0xCIerBkMok0uezpYSqyz0OG8WlJjPtMvnf4VMVAlCmoA8xXzqki81+EM2ei/1D/ X-Received: by 2002:a05:6402:2706:b0:45d:aaae:e74a with SMTP id y6-20020a056402270600b0045daaaee74amr15536878edd.72.1666334985352; Thu, 20 Oct 2022 23:49:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666334985; cv=none; d=google.com; s=arc-20160816; b=ZWXpRKQjgcys1oqPSw75cbyw0zW3z52c0oEOBoQyE3szc87W26z0XoBs9+d/GzCdzA UQLwT2pweHkz7RLZ7WHjlEVBqmAWlzkXtBcR9s/6LlZyrOEE4uyOiaAbbcBTItzBje+W yuxZaVeXCLyRM+kAtR/6bnUnM2GXV/vtZgltVHUCA2f8w1yrwbIBsNYr/8K+CC+4ZI3F VLei7r0iyi5fTwidMN+lE9gPyHXtPPAh1RmWHmxvoRCsgUwFS39eOpzFWjccCZoliD3K 1BD+khtQRS18eFN1tuAaVDgjh3wpf2rNDmOZ1e5GvvVQlCTBgcGkoFSUq0aRjlhVxnrv wGCA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=pAvlnod3xVNuUkD23gdvlySzhpIMs1Zr9/+Iyu6EPdA=; b=R7V7AtFK0nJneVmywD2ggylUSC6sRWnsYbTnQhvoJbMEsRwsNn+tLNJQO2KmS2wKmr 91EDBagMMMHmgzYwUzm4jsd+3ISx8MKXMMrrx+2z5074wFmy4cMp4K9q8VDkSFlqE5Rx YcOIJK8KqOollwzng/qWzGC1djrRlJnU06A/ba66fzYDB0AMwXc77cxaHLYNSsMwhnTI gBIOdW6WbIcg0smBqjv8U1dqqsZWUXVuPk0+VAkLdhR0C5sVuf61G+K6032odiN0ihTe sb3iYq9kGnPWvZuHbL061vu4Z423IymKpI8Lj/CgFC4JjmnsAxV7YKFYLD93TZEgifL/ zaLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=CB7H0A4Z; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id dm5-20020a170907948500b0078cff711da6si7807863ejc.976.2022.10.20.23.49.20; Thu, 20 Oct 2022 23:49:45 -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=@linuxfoundation.org header.s=korg header.b=CB7H0A4Z; 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=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229585AbiJUGpC (ORCPT <rfc822;pwkd43@gmail.com> + 99 others); Fri, 21 Oct 2022 02:45:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229911AbiJUGo7 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 21 Oct 2022 02:44:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08B608A1F9; Thu, 20 Oct 2022 23:44:58 -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 9917061B39; Fri, 21 Oct 2022 06:44:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82F38C433B5; Fri, 21 Oct 2022 06:44:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666334697; bh=Lh39dsSR2kSBUfou8Tu7BCiRrPulwLvLeS/qMsODS1s=; h=From:To:Cc:Subject:Date:From; b=CB7H0A4ZiBfH7FVc3Lc7ipzsK2/dBVP4OCKdl8GxWDL86BFWVJUxCZ0ZCfUO+vxej Vri98ayv2EJ5QNqBf40vZatfVuFFA4tqWkFhKmW0Aa4EGCQW7iV0wzHokPugCuobAi E4XmKtRgiLLbtHKesoGq4nkfvF3htpjWobmycpNc= From: Greg Kroah-Hartman <gregkh@linuxfoundation.org> To: linux-usb@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Felipe Balbi <balbi@kernel.org>, Jakob Koschel <jakobkoschel@gmail.com>, Randy Dunlap <rdunlap@infradead.org>, Ira Weiny <ira.weiny@intel.com>, Linus Torvalds <torvalds@linux-foundation.org> Subject: [PATCH] USB: gadget: dummy_hcd: switch char * to u8 * Date: Fri, 21 Oct 2022 08:44:53 +0200 Message-Id: <20221021064453.3341050-1-gregkh@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1528; i=gregkh@linuxfoundation.org; h=from:subject; bh=Lh39dsSR2kSBUfou8Tu7BCiRrPulwLvLeS/qMsODS1s=; b=owGbwMvMwCRo6H6F97bub03G02pJDMlB9o/D/S72sPYcWagmYZi1s8eZVSl4z+xKKeu/BztnWzN9 dBPsiGVhEGRikBVTZPmyjefo/opDil6Gtqdh5rAygQxh4OIUgIkYHWeY7//j275+Vaa8Uvbvd983H2 +2CH41j2F+uYPzhicx03i0PRRPyAWItZhGCbMCAA== X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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?1747278873820057781?= X-GMAIL-MSGID: =?utf-8?q?1747278873820057781?= |
Series |
USB: gadget: dummy_hcd: switch char * to u8 *
|
|
Commit Message
Greg KH
Oct. 21, 2022, 6:44 a.m. UTC
The function handle_control_request() casts the urb buffer to a char *,
and then treats it like a unsigned char buffer when assigning data to
it. On some architectures, "char" is really signed, so let's just
properly set this pointer to a u8 to take away any potential problems as
that's what is really wanted here.
Cc: Felipe Balbi <balbi@kernel.org>
Cc: Jakob Koschel <jakobkoschel@gmail.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Cc: Ira Weiny <ira.weiny@intel.com>
Reported-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/usb/gadget/udc/dummy_hcd.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Comments
On Thu, Oct 20, 2022 at 11:44 PM Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote: > > The function handle_control_request() casts the urb buffer to a char *, > and then treats it like a unsigned char buffer when assigning data to > it. On some architectures, "char" is really signed, so let's just > properly set this pointer to a u8 to take away any potential problems as > that's what is really wanted here. I think you might as well also remove the cast that was always a bit odd: buf[0] = (u8)dum->devstatus; although maybe it's intentional ("look, ma, I'm truncating this value") because 'devstatus' is a 'u16' type? I suspect a comment would be more readable than an odd cast that doesn't actually change anything (since the assignment does it anyway). Or maybe people wrote it that way on purpose, and used that variable name on purpose. Because 'dum' is Swedish for 'stupid', and maybe there's a coded message in that driver? Linus
From: Linus Torvalds > Sent: 21 October 2022 18:31 ... > I think you might as well also remove the cast that was always a bit odd: > > buf[0] = (u8)dum->devstatus; ... > I suspect a comment would be more readable than an odd cast that > doesn't actually change anything (since the assignment does it > anyway). I've seen a compiler generate an '& 0xff' for the cast before storing the low byte. Don't even think about what that compiler would generated for: buf[0] = (u8)(dum->devstatus & 0xff); The code is much easier to read as just an assignment :-) David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
On Fri, Oct 21, 2022 at 10:30:37AM -0700, Linus Torvalds wrote: > On Thu, Oct 20, 2022 at 11:44 PM Greg Kroah-Hartman > <gregkh@linuxfoundation.org> wrote: > > > > The function handle_control_request() casts the urb buffer to a char *, > > and then treats it like a unsigned char buffer when assigning data to > > it. On some architectures, "char" is really signed, so let's just > > properly set this pointer to a u8 to take away any potential problems as > > that's what is really wanted here. > > I think you might as well also remove the cast that was always a bit odd: > > buf[0] = (u8)dum->devstatus; > > although maybe it's intentional ("look, ma, I'm truncating this > value") because 'devstatus' is a 'u16' type? (adding Alan as he's the owner of this file now) Yes, devstatus is a u16 as that's what the USB spec says it should be, but so far only 7 of the lower bits have been used. I guess to do this properly we should also copy the upper 8 bits in to buf[1], eventhough in reality it's only ever going to be 0x00 for now. Although if we ever do get another 2 status bits defined, this code will break so we probably should do that too. I'll go do that for a v2 of this and properly comment it. > I suspect a comment would be more readable than an odd cast that > doesn't actually change anything (since the assignment does it > anyway). > > Or maybe people wrote it that way on purpose, and used that variable > name on purpose. > > Because 'dum' is Swedish for 'stupid', and maybe there's a coded > message in that driver? That whole driver is called "dummy" as it's a "fake" driver, not a "stupid" one :) thanks, greg k-h
On Sun, Oct 23, 2022 at 05:53:19PM +0200, Greg Kroah-Hartman wrote: > On Fri, Oct 21, 2022 at 10:30:37AM -0700, Linus Torvalds wrote: > > On Thu, Oct 20, 2022 at 11:44 PM Greg Kroah-Hartman > > <gregkh@linuxfoundation.org> wrote: > > > > > > The function handle_control_request() casts the urb buffer to a char *, > > > and then treats it like a unsigned char buffer when assigning data to > > > it. On some architectures, "char" is really signed, so let's just > > > properly set this pointer to a u8 to take away any potential problems as > > > that's what is really wanted here. > > > > I think you might as well also remove the cast that was always a bit odd: > > > > buf[0] = (u8)dum->devstatus; > > > > although maybe it's intentional ("look, ma, I'm truncating this > > value") because 'devstatus' is a 'u16' type? > > (adding Alan as he's the owner of this file now) > > Yes, devstatus is a u16 as that's what the USB spec says it should be, > but so far only 7 of the lower bits have been used. I guess to do this > properly we should also copy the upper 8 bits in to buf[1], eventhough > in reality it's only ever going to be 0x00 for now. Along these lines, do we really not have a predefined macro/inline function that does: (value >> 8) to give you the "high byte" of a 16bit value? I keep seeing people write their own macros for this in staging drivers, but I just attributed that to them not using the correct in-kernel macro, but I can't seem to find anything at the moment to do this (same with "give me just the lower 8 bits of a 16bit value"). Am I just blind? It's not like it's complex or tricky stuff, I just thought we had something in bits.h or bitops.h or the like. Oh well... thanks, greg k-h
On Sun, Oct 23, 2022 at 05:53:19PM +0200, Greg Kroah-Hartman wrote: > On Fri, Oct 21, 2022 at 10:30:37AM -0700, Linus Torvalds wrote: > > On Thu, Oct 20, 2022 at 11:44 PM Greg Kroah-Hartman > > <gregkh@linuxfoundation.org> wrote: > > > > > > The function handle_control_request() casts the urb buffer to a char *, > > > and then treats it like a unsigned char buffer when assigning data to > > > it. On some architectures, "char" is really signed, so let's just > > > properly set this pointer to a u8 to take away any potential problems as > > > that's what is really wanted here. > > > > I think you might as well also remove the cast that was always a bit odd: > > > > buf[0] = (u8)dum->devstatus; > > > > although maybe it's intentional ("look, ma, I'm truncating this > > value") because 'devstatus' is a 'u16' type? > > (adding Alan as he's the owner of this file now) > > Yes, devstatus is a u16 as that's what the USB spec says it should be, > but so far only 7 of the lower bits have been used. I guess to do this > properly we should also copy the upper 8 bits in to buf[1], eventhough > in reality it's only ever going to be 0x00 for now. I count only 5 of the bits being used, not 7. (See Figure 9-4 in section 9.4.5 of the USB-3.1 spec.) Maybe you're thinking of Feature flags rather than Status bits? They do have a lot of overlap. Dave Brownell wrote that code initially, so we'll never know why he included the cast. My guess is the same as Linus's: It's there to alert the reader that a 16-bit value is being shortened to squeeze into an 8-bit slot. > Although if we ever do get another 2 status bits defined, this code will > break so we probably should do that too. > > I'll go do that for a v2 of this and properly comment it. Note that there's an out-of-date comment line just above this part of the code: * device: remote wakeup, selfpowered Thanks to USB-3 support, the device recipient now defines three more bits in devstatus: LTM_ENABLED, U1_ENABLED, and U2_ENABLED. Alan Stern
On Sun, Oct 23, 2022 at 9:04 AM Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote: > > Along these lines, do we really not have a predefined macro/inline > function that does: > (value >> 8) > to give you the "high byte" of a 16bit value? No macros like that. And honestly, why would you want a macro that is more complicated than the operation itself? But it sounds like what you actually want is just put_unaligned_le16(dum->devstatus, buf); which does both bytes correctly (and turns into a plain 16-bit store on sane architectures).. Linus
diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c index 899ac9f9c279..774781968e55 100644 --- a/drivers/usb/gadget/udc/dummy_hcd.c +++ b/drivers/usb/gadget/udc/dummy_hcd.c @@ -1740,13 +1740,13 @@ static int handle_control_request(struct dummy_hcd *dum_hcd, struct urb *urb, if (setup->bRequestType == Dev_InRequest || setup->bRequestType == Intf_InRequest || setup->bRequestType == Ep_InRequest) { - char *buf; + u8 *buf; /* * device: remote wakeup, selfpowered * interface: nothing * endpoint: halt */ - buf = (char *)urb->transfer_buffer; + buf = urb->transfer_buffer; if (urb->transfer_buffer_length > 0) { if (setup->bRequestType == Ep_InRequest) { ep2 = find_endpoint(dum, w_index);