From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CEC51E3DED for ; Fri, 21 Mar 2025 18:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742582346; cv=none; b=RgKTC5zp0QaiAaAqFTHLc+3v+VCCvkPB/YkKRUA2VIw802MR2RZfZgkmP2X4YfcXc8f3F0elFMIetfNzF8GlBmbvILCI+5S2TT7VC1OJLnvWQyyxPd3JPypZrJOcSE+0ulqow+u1b6lTF4qdt4raYoepsOsKc8n168TBxCi8gJU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742582346; c=relaxed/simple; bh=n+K0u+Wd6us+fG8T0NJ0mq5za5Sn6Pp6ZvidZGcV8+4=; h=Message-ID:Date:MIME-Version:From:Subject:To:Cc:References: In-Reply-To:Content-Type; b=aRubJ6Pu7Gwoliu7xYd+K/9NA7T/1h3st8LV6h6QDZk50eYwahhfDqMJQvTL5pQPX8Lz4G+KcaQSMkq1HSQcNK5B74jYBe1D7ZlwrZpr6RR4UucPg8jB0O7pq95PUOJxPUaWBtBclZYhEk2bDW2fhNp4lM3h95kVxC5l2Yfcu1Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=iencinas.com; spf=pass smtp.mailfrom=iencinas.com; dkim=pass (2048-bit key) header.d=iencinas.com header.i=@iencinas.com header.b=LQr8od9E; arc=none smtp.client-ip=91.218.175.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=iencinas.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iencinas.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iencinas.com header.i=@iencinas.com header.b="LQr8od9E" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iencinas.com; s=key1; t=1742582339; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/l61PMZ8ZONjm1VBcYbvcyTIE/eIAlBdEyNM703h8Wg=; b=LQr8od9Eoo1BpEq7o+7c/an+WLbBfCL0f8tZSGR68BOyccf1eIvgALCsoOWT+7qHzhfGwd yn0NDKfsiZoX26dbpZT1+rz9C9U1DMXK/0pTwtqI34yXRlZFbkzgqXuEjoyrlPlcJ0LJuK Bj0fXQFBZAtzXpgmscYRilgQjjdj5++IwrQhlHkMVw+0ZodToECeslq47XrqyuM+WzjI7s e352THyHY4+OVl7l9mLaOERKk71lq/DVWr2JrB3jTN03qYKQzUoP97bRvAVZ3lwq2pJX4v 6FZOSzOf/sskaOtvO/1ffD9W8jxenGDR/LxEDH+kkX3WyNCQKT/fE8xP6AJd/Q== Date: Fri, 21 Mar 2025 19:38:54 +0100 Precedence: bulk X-Mailing-List: linux-kernel-mentees@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Ignacio Encinas Rubio Subject: Re: [PATCH v2 1/2] include/uapi/linux/swab.h: move default implementation for swab macros into asm-generic To: Arnd Bergmann , Paul Walmsley , Palmer Dabbelt , Alexandre Ghiti Cc: Eric Biggers , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kernel-mentees@lists.linux.dev, Shuah Khan , Zhihang Shao , =?UTF-8?B?QmrDtnJuIFTDtnBlbA==?= , Linux-Arch References: <20250319-riscv-swab-v2-0-d53b6d6ab915@iencinas.com> <20250319-riscv-swab-v2-1-d53b6d6ab915@iencinas.com> <2afab9dc-e39c-4399-ac5a-87ade4da5ab0@app.fastmail.com> <4d45df0c-d44e-4bb6-8daa-0dba1b834bc4@iencinas.com> <07b8051b-9d5e-440e-b74d-1ca97402fe2a@app.fastmail.com> <583340a9-411d-406f-aee9-d3e2eb80ca43@app.fastmail.com> Content-Language: en-US In-Reply-To: <583340a9-411d-406f-aee9-d3e2eb80ca43@app.fastmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 21/3/25 11:23, Arnd Bergmann wrote: > On Thu, Mar 20, 2025, at 23:36, Ignacio Encinas Rubio wrote: >> On 19/3/25 22:49, Arnd Bergmann wrote: >>> On Wed, Mar 19, 2025, at 22:37, Ignacio Encinas Rubio wrote: >>>> On 19/3/25 22:12, Arnd Bergmann wrote: >>> Right, I do remember when we had a discussion about this maybe >>> 15 years ago when gcc didn't have the builtins on all architectures >>> yet, but those versions are long gone, and we never cleaned it up. >> >> I just had a chance to look at this and it looks a bit more complex than >> I initially thought. ___constant_swab macros are used in more places >> than I expected, and {little,big}_endian.h define their own macros that >> are used elsewhere, ... >> >> It is not clear to me how to proceed here. I could: >> >> 1) Just remove ___constant_swab macros and replace them with >> __builtin_swap everywhere >> >> 2) Go a step further and evaluate removing __constant_htonl and >> relatives >> >> Let me know what you think is the best option :) > > I think we can start enabling CONFIG_ARCH_USE_BUILTIN_BSWAP > on all architectures and removing the custom versions > from arch/*/include/uapi/asm/swab.h, which all seem to > predate the compiler builtins and likely produce worse code. This seems fine for some architectures but I don't think we can use this approach for RISC-V. RISC-V code assumes that the bitmanip extension might not be available (see arch/riscv/include/asm/bitops.h). The current approach [1] is to detect this at boot and patch the kernel to adapt it to the actual hardware running it (using specific instructions or not). On the other hand, I tried using __builtin_swap for the RISC-V version as an alternative to the "optimized" one (instead of relying on ___constant_swab, see [2]) and I immediately got compilation errors. Some architectures seem to require definitions for __bswapsi2 and __bswapdi2 [3]. I'm guessing this happens for the architectures that don't require bit manipulation instructions but have them as extensions. arm,csky,mips and xtensa seem to fit this description as they feature their own __bswapsi2 implementations. Note that they simply call ___constant_swab or are ___constant_swab written in assembly language [4] [5]. Unless I'm missing something, it seems to me that using compiler builtins (at least for RISC-V, and potentially others) is even more problematic than keeping ___constant_swab around. What do you think, should we keep patch 1 after all? We could remove __arch_swab for architectures that always assume bit manipulation instructions availability, but then the kernel would fall back into ___constant_swab when CONFIG_ARCH_USE_BUILTIN_BSWAP=n. Turning their custom implementations into #define __arch_swabXY __builtin_bswapXY would solve this issue, but I'm not sure it is an acceptable approach. Thanks! [1] https://lore.kernel.org/all/ce034f2b-2f6e-403a-81f1-680af4c72929@ghiti.fr/ [2] https://lore.kernel.org/all/20250319-riscv-swab-v2-2-d53b6d6ab915@iencinas.com/ [3] https://gcc.gnu.org/onlinedocs/gcc-13.3.0/gccint.pdf [4] https://lore.kernel.org/all/20230512164815.2150839-1-jcmvbkbc@gmail.com/ [5] https://lore.kernel.org/all/1664437198-31260-3-git-send-email-yangtiezhu@loongson.cn/