Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3504487pxb; Mon, 24 Jan 2022 10:57:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJxAgUjweYdEZXSogzbo1IgeA1Ad0YROdoba8r+Gm9UK73R1fZkSulZjOzpyHNLNOf1Z/7qN X-Received: by 2002:a17:90b:1b46:: with SMTP id nv6mr3183908pjb.93.1643050653945; Mon, 24 Jan 2022 10:57:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643050653; cv=none; d=google.com; s=arc-20160816; b=nvQ3hnx5cCNnfe5l738l+vDhCbbHBwcp051u/LzzNHb86kAfovzaqdthXpNAtzsTQ4 PfQ6qL+Qzz3GDa8bDtDnztHLpa89q8v5LLAEu2PewcgHxTuMIHmJIUwxhP/uYK/lAtQC ErGsDhhl3B6WIibF7UgXZTJ0LX9DvvCDHcfFK/5JdwRpWXXni1A8mzTBhp3Q00d//Dgt tMZ93k+cagt0OJ5FC328DDtFMFbp0ed7xCh/Ap4RAhgcoCp1rGFBfkDJA2U+jiZPdsQl pP/vww25rilBDiActiOOlHEEpOZ1wiNzAyDgWyq/iovfXxqyus6SOjQkJLCXeJ0GqApf UhEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:accept-language:in-reply-to:references:message-id:date :thread-index:thread-topic:subject:cc:to:from; bh=dtqitnP2VUd2VUqEzn06HL4E4YZ/JeM7WoKJyIFsDWE=; b=DGahXx+ygD8I1Jvpos88fSws3XQlQe3EKRiHnVUMm15lyzTUVo3rwKGOKGuGoJy4Nk LP5WTWgP2gNdG0uXgAw03v+JuTiIM5uxV3Soq8m7YFltYDhgtG5IEbtLLZhy3ncAP7l2 VnV2yqxCUxkMMSO2b2gCcEVCTK5A2oiqPa5G4lVM97kylVG+YKpqrmzzJUL/SLQKm8zA BO+xajWvOwrhmSrPju7q2RtJ4ev3SbUUoIu30hq/iUjDkChW2gYB7o6AsQCwtM4G8Gpt f/JNRY/8iGT/ZkvAxWIbrrXt/sJNZrBXpRo5UhRqDLMY5xAxLmki3o0RBSQ8D+5feZxm NOyw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p8si2136951plr.585.2022.01.24.10.57.17; Mon, 24 Jan 2022 10:57:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232793AbiAXJV2 convert rfc822-to-8bit (ORCPT + 99 others); Mon, 24 Jan 2022 04:21:28 -0500 Received: from eu-smtp-delivery-151.mimecast.com ([185.58.85.151]:58646 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231851AbiAXJV1 (ORCPT ); Mon, 24 Jan 2022 04:21:27 -0500 Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-252-Qk4L0YCwNj64dPMhnPks7Q-1; Mon, 24 Jan 2022 09:21:24 +0000 X-MC-Unique: Qk4L0YCwNj64dPMhnPks7Q-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.28; Mon, 24 Jan 2022 09:21:21 +0000 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.028; Mon, 24 Jan 2022 09:21:21 +0000 From: David Laight To: "'michael@michaelkloos.com'" , "paul.walmsley@sifive.com" , "palmer@dabbelt.com" , "aou@eecs.berkeley.edu" CC: "linux-riscv@lists.infradead.org" , "linux-kernel@vger.kernel.org" Subject: RE: [PATCH v2] Fixed: Misaligned memory access. Fixed pointer comparison. Thread-Topic: [PATCH v2] Fixed: Misaligned memory access. Fixed pointer comparison. Thread-Index: AQHYEAxftwhRlaAJzU+hPBmorjaluqxx5R9A Date: Mon, 24 Jan 2022 09:21:21 +0000 Message-ID: <4fa1c4bbf27242cf9835dde0abdc89d5@AcuMS.aculab.com> References: <20220123034518.3717116-1-michael@michaelkloos.com> In-Reply-To: <20220123034518.3717116-1-michael@michaelkloos.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: michael@michaelkloos.com > Sent: 23 January 2022 03:45 > > Rewrote the riscv memmove() assembly implementation. The > previous implementation did not check memory alignment and it > compared 2 pointers with a signed comparison. The misaligned > memory access would cause the kernel to crash on systems that > did not emulate it in firmware and did not support it in hardware. > Firmware emulation is slow and may not exist. Additionally, > hardware support may not exist and would likely still run slower > than aligned accesses even if it did. That may not be true. On x86 the cost of misaligned accesses only just measurable. It isn't even one clock per cache line for reads (eg ipcsum). > The RISC-V spec does not > guarantee that support for misaligned memory accesses will exist. > It should not be depended on. > > This patch now checks for the maximum granularity of co-alignment > between the pointers and copies them with that, using single-byte > copy for any unaligned data at their terminations. It also now uses > unsigned comparison for the pointers. If the performance of misaligned copies ever matters it is probably better to use: *dst++ = src[0] >> n | src[1] << (64 - n); for the body of the misaligned loop. You can always read the aligned src[] even if outside the buffer. So the only difficult part is writing the odd bytes and getting 'n' and the direction of the shifts right! David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)