Re: drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c:2267:16: warning: taking address of packed member 'hw_tx_obj' of class or structure 'mcp251xfd_tx_obj_load_buf::(anonymous)' may result in an unaligned pointer value

From: Marc Kleine-Budde
Date: Wed Jul 14 2021 - 16:04:32 EST


On 01.07.2021 15:25:24, kernel test robot wrote:
> FYI, the error/warning still remains.
>
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head: dbe69e43372212527abf48609aba7fc39a6daa27
> commit: eb79a267c9b3e608e7762a1b221428f37ace3aa3 can: mcp251xfd: rename all remaining occurrence to mcp251xfd
> date: 9 months ago
> config: mips-buildonly-randconfig-r005-20210630 (attached as .config)
> compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 8d21d5472501460933e78aead04cf59579025ba4)
> reproduce (this is a W=1 build):
> wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
> chmod +x ~/bin/make.cross
> # install mips cross compiling tool for clang build
> # apt-get install binutils-mips-linux-gnu
> # https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=eb79a267c9b3e608e7762a1b221428f37ace3aa3
> git remote add linus https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> git fetch --no-tags linus master
> git checkout eb79a267c9b3e608e7762a1b221428f37ace3aa3
> # save the attached .config to linux build tree
> COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=mips
>
> If you fix the issue, kindly add following tag as appropriate
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
>
> All warnings (new ones prefixed by >>):
>
> In file included from drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c:19:
> In file included from include/linux/netdevice.h:37:
> In file included from include/linux/ethtool.h:18:
> In file included from include/uapi/linux/ethtool.h:19:
> In file included from include/linux/if_ether.h:19:
> In file included from include/linux/skbuff.h:28:
> In file included from include/net/checksum.h:22:
> arch/mips/include/asm/checksum.h:195:9: error: unsupported inline asm: input with type 'unsigned long' matching output with type '__wsum' (aka 'unsigned int')
> : "0" ((__force unsigned long)daddr),
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is unrelated to this driver.

> In file included from drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c:26:
> In file included from drivers/net/can/spi/mcp251xfd/mcp251xfd.h:14:
> In file included from include/linux/can/dev.h:21:
> In file included from include/linux/can/skb.h:17:
> In file included from include/net/sock.h:61:
> include/linux/poll.h:142:27: warning: division by zero is undefined [-Wdivision-by-zero]
> M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
> ^~~~~~~~~
> include/linux/poll.h:140:32: note: expanded from macro 'M'
> #define M(X) (__force __poll_t)__MAP(val, POLL##X, (__force __u16)EPOLL##X)
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> include/linux/poll.h:126:51: note: expanded from macro '__MAP'
> (from < to ? (v & from) * (to/from) : (v & from) / (from/to))
> ^ ~~~~~~~~~
> include/linux/poll.h:142:39: warning: division by zero is undefined [-Wdivision-by-zero]
> M(RDNORM) | M(RDBAND) | M(WRNORM) | M(WRBAND) |
> ^~~~~~~~~
> include/linux/poll.h:140:32: note: expanded from macro 'M'
> #define M(X) (__force __poll_t)__MAP(val, POLL##X, (__force __u16)EPOLL##X)
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> include/linux/poll.h:126:51: note: expanded from macro '__MAP'
> (from < to ? (v & from) * (to/from) : (v & from) / (from/to))
> ^ ~~~~~~~~~
> >> drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c:2267:16: warning: taking address of packed member 'hw_tx_obj' of class or structure 'mcp251xfd_tx_obj_load_buf::(anonymous)' may result in an unaligned pointer value [-Waddress-of-packed-member]
> hw_tx_obj = &load_buf->crc.hw_tx_obj;
> ^~~~~~~~~~~~~~~~~~~~~~~
> drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c:2269:16: warning: taking address of packed member 'hw_tx_obj' of class or structure 'mcp251xfd_tx_obj_load_buf::(anonymous)' may result in an unaligned pointer value [-Waddress-of-packed-member]
> hw_tx_obj = &load_buf->nocrc.hw_tx_obj;
> ^~~~~~~~~~~~~~~~~~~~~~~~~
> 4 warnings and 1 error generated.

These warnings are technically correct, but the driver never
accesses this pointer directly, see below:

[...]

> eb79a267c9b3e6 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c Marc Kleine-Budde 2020-09-30 2266 if (priv->devtype_data.quirks & MCP251XFD_QUIRK_CRC_TX)
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 @2267 hw_tx_obj = &load_buf->crc.hw_tx_obj;
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2268 else
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2269 hw_tx_obj = &load_buf->nocrc.hw_tx_obj;
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2270
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2271 put_unaligned_le32(id, &hw_tx_obj->id);
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2272 put_unaligned_le32(flags, &hw_tx_obj->flags);

use put_unaligned_le32 instead of direct assignment

> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2273
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2274 /* Clear data at end of CAN frame */
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2275 offset = round_down(cfd->len, sizeof(u32));
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2276 len = round_up(can_dlc2len(dlc), sizeof(u32)) - offset;
> eb79a267c9b3e6 drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c Marc Kleine-Budde 2020-09-30 2277 if (MCP251XFD_SANITIZE_CAN && len)
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2278 memset(hw_tx_obj->data + offset, 0x0, len);
> 55e5b97f003e85 drivers/net/can/spi/mcp25xxfd/mcp25xxfd-core.c Marc Kleine-Budde 2020-09-18 2279 memcpy(hw_tx_obj->data, cfd->data, cfd->len);

memset() and memcpy() should be fine, too

Marc

--
Pengutronix e.K. | Marc Kleine-Budde |
Embedded Linux | https://www.pengutronix.de |
Vertretung West/Dortmund | Phone: +49-231-2826-924 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |

Attachment: signature.asc
Description: PGP signature