Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp2944922ybc; Thu, 14 Nov 2019 01:24:59 -0800 (PST) X-Google-Smtp-Source: APXvYqwj2bugHWyIdOPoU4rRVJbrMc8o1U2QLhmnTzcl2eneR0OVzhJwFaoyZh+vsY/Kb7cwj+Ph X-Received: by 2002:a17:906:f24d:: with SMTP id gy13mr7341087ejb.159.1573723499462; Thu, 14 Nov 2019 01:24:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573723499; cv=none; d=google.com; s=arc-20160816; b=EztEJxWv4kegXP2/q/Zi71FcL6zHeXLTx2DiCnXqMpMyFahnUhOEHtVi+icr9gLN66 OiA8bcnRC6FSlivw70SdysNuyOmmY2cuGS5UMgTi3lkFayBcIMdsJQwW68oBYIZNCpHa IMS35+gedkFizHK9vd26sJM0sIbxI+TpIdZw5eKl+yd319JcJkiwBnhwC3aA8XcshCf0 tmz8o9Ff99XJfj1CS9hRn6wNE+9yNDZOP1ZPxLAHfDAF8Y2NwmSdqH/2ZaLnc/XfgvpT jRlmqLoQgnmYbbGf32SVWx1rHX0wQyTZFrUWS7LJHyMoLtizjlNjAvSrI2uWz1KGe9q8 fedQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=C1ngwk0YVOrwJgC/axGbt5G6YK/2DWIpVnGmkcn8phw=; b=wnhQYk+cQqGZnMDULa9aOeo8yt/C9aQ+9et64+3kUagpelaQpcbfFWEBW7MhdfevUi 05HchqLE5GS0WIJFafsIDJmb4P/SobQQEKTmtl23MxGQMsQaIl0uGyocvCK1QMcAarLT m4i7aLBc4UNJDmBvtEpVvLMUlJ4fM6aU9a3i6G4tHfT5FMgQ/NGFxH8KHCovfnk22nDb jg5uwoixk2WuWZILe2OMLTsdCMYWs+BRwO4J+TNRFIj38iNXL5SULcujz1bTXOp78qz8 fnEq66v1sO2+KY+tyIkK8ohnBMuROk2OLIcXIaKTEfilVAS3HvPd6Iwg3RrNhEYFiE2y jVCw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l15si3047730ejc.27.2019.11.14.01.24.34; Thu, 14 Nov 2019 01:24:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726838AbfKNJXg (ORCPT + 99 others); Thu, 14 Nov 2019 04:23:36 -0500 Received: from hostingweb31-40.netsons.net ([89.40.174.40]:40426 "EHLO hostingweb31-40.netsons.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726010AbfKNJXg (ORCPT ); Thu, 14 Nov 2019 04:23:36 -0500 Received: from [109.168.11.45] (port=58534 helo=[192.168.101.73]) by hostingweb31.netsons.net with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92) (envelope-from ) id 1iVBLh-001jwO-8H; Thu, 14 Nov 2019 10:23:33 +0100 Subject: Re: [PATCH v3 2/3] i2c: smbus: use get/put_unaligned_le16 when working with word data To: Dmitry Torokhov Cc: Wolfram Sang , linux-iio@vger.kernel.orgi, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org References: <20191112203132.163306-1-dmitry.torokhov@gmail.com> <20191112203132.163306-3-dmitry.torokhov@gmail.com> <099e8f9c-354a-8756-a79b-e66c72d36aa7@lucaceresoli.net> <20191113193937.GQ13374@dtor-ws> From: Luca Ceresoli Message-ID: <5f276a2a-4863-c81c-247f-686935b6c571@lucaceresoli.net> Date: Thu, 14 Nov 2019 10:23:32 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191113193937.GQ13374@dtor-ws> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hostingweb31.netsons.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lucaceresoli.net X-Get-Message-Sender-Via: hostingweb31.netsons.net: authenticated_id: luca+lucaceresoli.net/only user confirmed/virtual account not confirmed X-Authenticated-Sender: hostingweb31.netsons.net: luca@lucaceresoli.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Dmitry, On 13/11/19 20:39, Dmitry Torokhov wrote: > Hi Luca, > > On Wed, Nov 13, 2019 at 10:47:42AM +0100, Luca Ceresoli wrote: >> Hi Dmitry, >> >> On 12/11/19 21:31, Dmitry Torokhov wrote: >>> It is potentially more performant, and also shows intent more clearly, >>> to use get_unaligned_le16() and put_unaligned_le16() when working with >>> word data. >>> >>> Signed-off-by: Dmitry Torokhov >>> >>> --- >>> >>> Changes in v3: >>> - split put_unaligned_le16 into a separate patch >>> - more call sites converted to get/put_unaligned_le16 >>> >>> drivers/i2c/i2c-core-smbus.c | 12 +++++------- >>> 1 file changed, 5 insertions(+), 7 deletions(-) >>> >>> diff --git a/drivers/i2c/i2c-core-smbus.c b/drivers/i2c/i2c-core-smbus.c >>> index f8708409b4dbc..7b4e2270eeda1 100644 >>> --- a/drivers/i2c/i2c-core-smbus.c >>> +++ b/drivers/i2c/i2c-core-smbus.c >>> @@ -15,6 +15,7 @@ >>> #include >>> #include >>> #include >>> +#include >>> >>> #include "i2c-core.h" >>> >>> @@ -370,8 +371,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, >>> msg[1].len = 2; >>> else { >>> msg[0].len = 3; >>> - msgbuf0[1] = data->word & 0xff; >>> - msgbuf0[2] = data->word >> 8; >>> + put_unaligned_le16(data->word, msgbuf0 + 1); >>> } >>> break; >>> case I2C_SMBUS_PROC_CALL: >>> @@ -379,8 +379,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, >>> read_write = I2C_SMBUS_READ; >>> msg[0].len = 3; >>> msg[1].len = 2; >>> - msgbuf0[1] = data->word & 0xff; >>> - msgbuf0[2] = data->word >> 8; >>> + put_unaligned_le16(data->word, msgbuf0 + 1); >>> break; >>> case I2C_SMBUS_BLOCK_DATA: >>> if (read_write == I2C_SMBUS_READ) { >>> @@ -487,7 +486,7 @@ static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, >>> break; >>> case I2C_SMBUS_WORD_DATA: >>> case I2C_SMBUS_PROC_CALL: >>> - data->word = msgbuf1[0] | (msgbuf1[1] << 8); >>> + data->word = get_unaligned_le16(msgbuf1); >> >> Well, msgbuf1 cannot be unaligned, so it looks like you just need to >> convert little endian to host endian. Perhaps __le16_to_cpu(msgbuf1) is >> better (and equally or more efficient). > > Well, msgbuf1 (as is any other variable unless adjusted by special > alignment attribute) is naturally aligned for its own data type (which > for u8 means it can start at any address), but that does not mean that > is is aligned for the purpose of storing word data. In fact, the > preceding msgbuf0 variable is 32 + 3 = 35 bytes long, which means that > msgbuf1 is starting on an odd address, and is definitely not aligned for > word access and using __le16_to_cpu to fetch that word will trap on some > architectures. Uhm, you're obviously right. And I was probably drunk when I wrote the opposite! Sorry about that... Reviewed-by: Luca Ceresoli -- Luca