From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756570AbcKWVCY (ORCPT ); Wed, 23 Nov 2016 16:02:24 -0500 Received: from quartz.orcorp.ca ([184.70.90.242]:54603 "EHLO quartz.orcorp.ca" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753438AbcKWVCV (ORCPT ); Wed, 23 Nov 2016 16:02:21 -0500 Date: Wed, 23 Nov 2016 14:01:39 -0700 From: Jason Gunthorpe To: "Winkler, Tomas" Cc: "tpmdd-devel@lists.sourceforge.net" , Jarkko Sakkinen , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] tpm: use get_unaligned_be32 unaligned buffer access. Message-ID: <20161123210138.GB15803@obsidianresearch.com> References: <1479899094-9486-1-git-send-email-tomas.winkler@intel.com> <20161123165734.GA2763@obsidianresearch.com> <5B8DA87D05A7694D9FA63FD143655C1B543308CB@hasmsx108.ger.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5B8DA87D05A7694D9FA63FD143655C1B543308CB@hasmsx108.ger.corp.intel.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-Broken-Reverse-DNS: no host name found for IP address 10.0.0.151 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 23, 2016 at 08:56:25PM +0000, Winkler, Tomas wrote: > > On Wed, Nov 23, 2016 at 01:04:54PM +0200, Tomas Winkler wrote: > > > Use get_unaligned_be32 as b32_to_cpu doesn't work correctly on all > > > platforms for unaligned access. > > > > > > The fix doesn't cover all the cases as also some cast structures have > > > members on unaligned addresses. > > > > I think this is a good idea.. > > > > > @@ -353,8 +353,8 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const u8 > > *buf, size_t bufsiz, > > > if (bufsiz > TPM_BUFSIZE) > > > bufsiz = TPM_BUFSIZE; > > > > > > - count = be32_to_cpu(*((__be32 *) (buf + 2))); > > > - ordinal = be32_to_cpu(*((__be32 *) (buf + 6))); > > > + count = get_unaligned_be32(buf + 2); > > > + ordinal = get_unaligned_be32(buf + 6); > > > > But lets fix this better and get rid of the constants too... > > > const tpm_input_header *hdr = buf; > > count = be32_to_cpu(hdr->length); > > ordinal = be32_to_cpu(hdr->ordinal); > > > > Compiler will take care of unaligned for __packed. > > Yes, compiler takes care at performance penalty but probably we > don't care about that much, Hmm? get_unaligned_be32 boils down to the same __packed construct. As is today we must be hitting the in-kernel unaligned access trap (eg on ARM) which is *very* expensive so this is a very worthwhile fix ... Jason