Received: by 2002:a05:7208:70d5:b0:7f:5597:fa5c with SMTP id q21csp105233rba; Wed, 20 Mar 2024 12:18:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUBUfv9SHNckfNfV9XIbmiBoSeo6nEvDguoC0JOR+tBQ8Y6rsytiT2qJVQfeFLUfa37cAb+QCvnECbfT1qf58wRgGXFSc0F07NfmxdpkA== X-Google-Smtp-Source: AGHT+IEUyZVge7843cMPxz/x5wlhQJlZYZpxX/xUIRBNp6Nox/J6R4lg4T7lPJPaaQeS//YT3duJ X-Received: by 2002:ac8:59d3:0:b0:430:8c2e:2719 with SMTP id f19-20020ac859d3000000b004308c2e2719mr1255573qtf.7.1710962283824; Wed, 20 Mar 2024 12:18:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710962283; cv=pass; d=google.com; s=arc-20160816; b=rY85iVFA+vLvAhJiMCn5oGImqYYfOkNA8aV9e8E5lcgrsQclhFmlesxednbwwc4JBy 2syEzKypano7ehB5k+BR6JHpl5r7H+nEvq3jaomYWskdaCwWk9LAAJB99UIsR1qITcxX oS0O3eGymF7cRn8YrBkPPOTguho3yx5Rc6/2xqC6BG5pcvrMw0DbBrZIutG+7ZEdHyHb 8k0OacbzDcmCmpO3FJL9OBNSgxSKlM8goCU0iXWvQnSAARCg3WDtJ0l9K1GFn1FD6Nhx l13OmP23ChpN3adSxXH6e02rSUFcM+DTdt/bCn208BuzG4nr0OMBadlELZlKvJWLQ6Bi WIng== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=1Y590WxKOa1AMBWy35lAuMiZrWzlzJ7qTj6BnbQ/vj4=; fh=j65+2XE0UThIviosaYS3kQSxcrsCWmYHOVkMHH2HqaI=; b=Jn4I34zJA4LWeOR8iOfav2qhtNEVaPxiSKRA6iQWVk+AS1yfVz3eVbYoF/LiC06bmb czfc/HaYSS/tmNKWlgG2biDk9npJyZ3GvZ1L4jDG3m81dzQCQNzbbgQgVuD6dIfaVv2I JML5BRCBrGCmSzkWCors61yOLLwiJaaj+QdUR1ZD7VkhT06GCPyhbVCmDT3TE1nuIX5k yRAamy5DcA/4uqUFK1+30vqsbD2Sn4sAHgFtSJYO2eQUsbB7nEzhA1LcgxU6HqEWxZ4t U7GDhVp+bDzkHM/LUWENFwlGR42p9YC0TcxJhmn+hw4fopAfEKPJuIy2eW2MswCEXI/S 0KEg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=tukaani.org); spf=pass (google.com: domain of linux-kernel+bounces-109333-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109333-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id g6-20020ac85d46000000b00430a6328352si13210127qtx.95.2024.03.20.12.18.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 12:18:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-109333-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=tukaani.org); spf=pass (google.com: domain of linux-kernel+bounces-109333-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-109333-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 82C4D1C2114F for ; Wed, 20 Mar 2024 19:18:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 752CE8565A; Wed, 20 Mar 2024 19:17:47 +0000 (UTC) Received: from mailscanner01.zoner.fi (mailscanner01.zoner.fi [84.34.166.10]) (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 11A8385297 for ; Wed, 20 Mar 2024 19:17:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=84.34.166.10 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710962266; cv=none; b=T89IkXLUAr1Sc6YpaNqFqDGa/DPM1zsQYpGjYTaZAAGs4BgxB/ToV+GuDTCU3RNS011KyQGwfzUdbI00gaSXIDxPbCSK5qUoY9B7wdpYPL0wFJAPFswh+/+UDVBaZRJt1H0eoBgMmCKj0VP1uOBz09GDPoJ+6ndj9qgWFbES4Ao= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710962266; c=relaxed/simple; bh=BduBwpnC+lnh0fNzh+quQgYOi+N1VvuKwdT8DqQQw6I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FRA8J70IYqyLLkGuMQvjYNXcdYrGcpCr/pF7WUcwlyXsBTB1AmRB3rneOBceF9F2rCKdSP7NIw3WicaW8v7U4ewcqI/+8pz5xD4oO3hwiFtAM8MhO2Ni6TnCbQa9nD+EyKyPCyUz8Sen1rcPfpEX9NIrLuF2uybdc/4ouc4sZyw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tukaani.org; spf=pass smtp.mailfrom=tukaani.org; arc=none smtp.client-ip=84.34.166.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tukaani.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tukaani.org Received: from www25.zoner.fi (www25.zoner.fi [84.34.147.45]) by mailscanner01.zoner.fi (Postfix) with ESMTPS id 2E1EA4220B; Wed, 20 Mar 2024 21:17:42 +0200 (EET) Received: from mail.zoner.fi ([84.34.147.244]) by www25.zoner.fi with esmtp (Exim 4.96.1-7-g79877b70e) (envelope-from ) id 1rn0qU-0001dW-2z; Wed, 20 Mar 2024 20:39:26 +0200 From: Lasse Collin To: Andrew Morton Cc: Lasse Collin , Jia Tan , Jubin Zhong , Jules Maselbas , linux-kernel@vger.kernel.org Subject: [PATCH 11/11] xz: Adjust arch-specific options for better kernel compression Date: Wed, 20 Mar 2024 20:38:44 +0200 Message-ID: <20240320183846.19475-12-lasse.collin@tukaani.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240320183846.19475-1-lasse.collin@tukaani.org> References: <20240320183846.19475-1-lasse.collin@tukaani.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Use LZMA2 options that match the arch-specific alignment of instructions. This change reduces compressed kernel size 0-2 % depending on the arch. On 1-byte-aligned x86 it makes no difference and on 4-byte-aligned archs it helps the most. Use the ARM-Thumb filter for ARM-Thumb2 kernels. This reduces compressed kernel size about 5 %.[1] Previously such kernels were compressed using the ARM filter which didn't do anything useful with ARM-Thumb2 code. Add BCJ filter support for ARM64 and RISC-V. On ARM64 the compressed kernel size is reduced about 5 % and on RISC-V by 7-8 % compared to unfiltered XZ or plain LZMA. However: - arch/arm64/boot/Makefile and arch/riscv/boot/Makefile don't include the build rule (two lines) for XZ support even though they support six other compressors. It would be trivial to add the rule but boot loaders would need XZ support too. - A new enough version of the xz tool is required: 5.4.0 for ARM64 and 5.6.0 for RISC-V. With an old xz version a message is printed to standard error and the kernel is compressed without the filter. Update lib/decompress_unxz.c to match the changes to xz_wrap.sh. Update the CONFIG_KERNEL_XZ help text in init/Kconfig: - Add the RISC-V and ARM64 filters. - Clarify that the PowerPC filter is for big endian only. - Omit IA-64. Link: https://lore.kernel.org/lkml/1637379771-39449-1-git-send-email-zhongjubin@huawei.com/ [1] Reviewed-by: Jia Tan Signed-off-by: Lasse Collin --- init/Kconfig | 5 +- lib/decompress_unxz.c | 14 ++++- scripts/xz_wrap.sh | 141 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 151 insertions(+), 9 deletions(-) diff --git a/init/Kconfig b/init/Kconfig index f3ea5dea9c85..785e15aa5395 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -308,8 +308,9 @@ config KERNEL_XZ BCJ filters which can improve compression ratio of executable code. The size of the kernel is about 30% smaller with XZ in comparison to gzip. On architectures for which there is a BCJ - filter (i386, x86_64, ARM, IA-64, PowerPC, and SPARC), XZ - will create a few percent smaller kernel than plain LZMA. + filter (i386, x86_64, ARM, ARM64, RISC-V, big endian PowerPC, + and SPARC), XZ will create a few percent smaller kernel than + plain LZMA. The speed is about the same as with LZMA: The decompression speed of XZ is better than that of bzip2 but worse than gzip diff --git a/lib/decompress_unxz.c b/lib/decompress_unxz.c index 46aa3be13fc5..cae00395d7a6 100644 --- a/lib/decompress_unxz.c +++ b/lib/decompress_unxz.c @@ -126,11 +126,21 @@ #ifdef CONFIG_X86 # define XZ_DEC_X86 #endif -#ifdef CONFIG_PPC +#if defined(CONFIG_PPC) && defined(CONFIG_CPU_BIG_ENDIAN) # define XZ_DEC_POWERPC #endif #ifdef CONFIG_ARM -# define XZ_DEC_ARM +# ifdef CONFIG_THUMB2_KERNEL +# define XZ_DEC_ARMTHUMB +# else +# define XZ_DEC_ARM +# endif +#endif +#ifdef CONFIG_ARM64 +# define XZ_DEC_ARM64 +#endif +#ifdef CONFIG_RISCV +# define XZ_DEC_RISCV #endif #ifdef CONFIG_SPARC # define XZ_DEC_SPARC diff --git a/scripts/xz_wrap.sh b/scripts/xz_wrap.sh index c8c36441ab70..5bdf0c35cc85 100755 --- a/scripts/xz_wrap.sh +++ b/scripts/xz_wrap.sh @@ -6,14 +6,145 @@ # # Author: Lasse Collin +# This has specialized settings for the following archs. However, +# XZ-compressed kernel isn't currently supported on every listed arch. +# +# Arch Align Notes +# arm 2/4 ARM and ARM-Thumb2 +# arm64 4 +# csky 2 +# loongarch 4 +# mips 2/4 MicroMIPS is 2-byte aligned +# parisc 4 +# powerpc 4 Uses its own wrapper for compressors instead of this. +# riscv 2/4 +# s390 2 +# sh 2 +# sparc 4 +# x86 1 + +# A few archs use 2-byte or 4-byte aligned instructions depending on +# the kernel config. This function is used to check if the relevant +# config option is set to "y". +is_enabled() +{ + grep -q "^$1=y$" include/config/auto.conf +} + +# Set XZ_VERSION (and LIBLZMA_VERSION). This is needed to disable features +# that aren't available in old XZ Utils versions. +eval "$($XZ --robot --version)" || exit + +# Assume that no BCJ filter is available. BCJ= -LZMA2OPTS= +# Set the instruction alignment to 1, 2, or 4 bytes. +# +# Set the BCJ filter if one is available. +# It must match the #ifdef usage in lib/decompress_unxz.c. case $SRCARCH in - x86) BCJ=--x86 ;; - powerpc) BCJ=--powerpc ;; - arm) BCJ=--arm ;; - sparc) BCJ=--sparc ;; + arm) + if is_enabled CONFIG_THUMB2_KERNEL; then + ALIGN=2 + BCJ=--armthumb + else + ALIGN=4 + BCJ=--arm + fi + ;; + + arm64) + ALIGN=4 + + # ARM64 filter was added in XZ Utils 5.4.0. + if [ "$XZ_VERSION" -ge 50040002 ]; then + BCJ=--arm64 + else + echo "$0: Upgrading to xz >= 5.4.0" \ + "would enable the ARM64 filter" \ + "for better compression" >&2 + fi + ;; + + csky) + ALIGN=2 + ;; + + loongarch) + ALIGN=4 + ;; + + mips) + if is_enabled CONFIG_CPU_MICROMIPS; then + ALIGN=2 + else + ALIGN=4 + fi + ;; + + parisc) + ALIGN=4 + ;; + + powerpc) + ALIGN=4 + + # The filter is only for big endian instruction encoding. + if is_enabled CONFIG_CPU_BIG_ENDIAN; then + BCJ=--powerpc + fi + ;; + + riscv) + if is_enabled CONFIG_RISCV_ISA_C; then + ALIGN=2 + else + ALIGN=4 + fi + + # RISC-V filter was added in XZ Utils 5.6.0. + if [ "$XZ_VERSION" -ge 50060002 ]; then + BCJ=--riscv + else + echo "$0: Upgrading to xz >= 5.6.0" \ + "would enable the RISC-V filter" \ + "for better compression" >&2 + fi + ;; + + s390) + ALIGN=2 + ;; + + sh) + ALIGN=2 + ;; + + sparc) + ALIGN=4 + BCJ=--sparc + ;; + + x86) + ALIGN=1 + BCJ=--x86 + ;; + + *) + echo "$0: Arch-specific tuning is missing for '$SRCARCH'" >&2 + + # Guess 2-byte-aligned instructions. Guessing too low + # should hurt less than guessing too high. + ALIGN=2 + ;; +esac + +# Select the LZMA2 options matching the instruction alignment. +case $ALIGN in + 1) LZMA2OPTS= ;; + 2) LZMA2OPTS=lp=1 ;; + 4) LZMA2OPTS=lp=2,lc=2 ;; + *) echo "$0: ALIGN wrong or missing" >&2; exit 1 ;; esac # Use single-threaded mode because it compresses a little better -- 2.44.0