Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1120632lqp; Fri, 22 Mar 2024 06:19:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWiTv625wq+8NkKzAnlsUTfw4L1hNbyZDPvgfVnELe8uFe8rxd/6c3BV/ZGJm9syNPIASvJLXWkidhwsjIsfTisGNUMm1BffjXCGYAEFA== X-Google-Smtp-Source: AGHT+IFcIkAh0ofSqzBNFnOPc1M7RtfdqxAlppqUkAqXPfMixSj/HJkk73N4tLBzR+8S1gqOGXNd X-Received: by 2002:a17:903:11c4:b0:1e0:64bd:51ac with SMTP id q4-20020a17090311c400b001e064bd51acmr2852765plh.22.1711113599187; Fri, 22 Mar 2024 06:19:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711113599; cv=pass; d=google.com; s=arc-20160816; b=aueZIrKNtP3QN7NwZX41xSgKaSLxbCQFwMrSBJQ7xo2hCauK8Th62kUxOSOdkt3MWF LLC+oY2R1kLrrXlQgEnes/cZ5wZL14LlJLOBzfcIfeojxM1t3kE+vQcNDGg6ZduJ8zHa fXbpzQEyW+FhFNW52x8BjG/+aeBH0XkcJskJy7SRTriLFqOPU/XuzoQI0Fvt1QwnPSWm qAo/+FfqcXCgYSw7XpFPG2fl+eXgRLdGKelf8BNHlIcTHRGhbmTK+QiIr7ei53eeeaqJ ZQ66wokpbgyxBCxR2ZjOK1RIGyFIE7QTWk+/363EKx+MMbvT+be8lb6sExbZbdfTPhI7 NDlA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=KDS+MAgJnU1DjD59jSxBRYGLnDJP02YqiljUP25uhsU=; fh=DSFa+HL36LTBuvX0GY9/vz03Td3msq4cP50Af921NWg=; b=0Z+Mqbb2wP7zSOrT10WLaCcgT3zNfF6BGo4YnkmcwPiJ5gPTqn2M6UAoLst/OMqvHQ Cwsjtd3Si1niI5Q/4I6jQn0JU+R21mDzDbKrSFndS8RB0I1xOmzn5Kk5k6Ahd4G8rEhb Z/A/0R23+B9ZtQhSzy5SBa6FNd97Dc3a53li7in4dEL7Mb1O0cijo0McTYmVJHDBNd74 AdK1XRmMsUYlNG3vxdO9iXUb1tfG7ZDzgJqmFH+VMbqVrU+mDzXS9ZhPVIX+nRFTb8TO xheVXJd+MVIWwMhl2PURBqZ/k+X7f37rJ2sCHtP0fg0HSLtL1DALVrC6VrF8UigeCMb8 r03A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Tg5Mqxje; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-111458-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111458-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id i3-20020a170902c94300b001dffa1e844bsi2046651pla.562.2024.03.22.06.19.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 06:19:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111458-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Tg5Mqxje; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-111458-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111458-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 26257284B03 for ; Fri, 22 Mar 2024 13:18:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5001445BF3; Fri, 22 Mar 2024 13:18:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Tg5Mqxje" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 727AA4501C; Fri, 22 Mar 2024 13:18:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711113488; cv=none; b=cGe0bo3KcYxBHfqkeHjvus13amhf6DeQriD8GuHVyW9ntJJAZ4Q51SXd/FW6gGT3usr/rmaYNv4qfQU4G0+NJarYzLye1G5V9nun/5cISI1YTs3MjFITHSW2chrm9CMGWPdyBM1JAJr4xAvkwSgB1RpONe1IX8KU7cbDQ5PT4tc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711113488; c=relaxed/simple; bh=HAKZ7cmBvqd6aSZFndMvBQg8DCq/3g1+oo1NIjMagHA=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=ATZDyjSCuxve+3SJtxx/Rm+zxmUBQmHyj4pRo2uqBcuo0vaqHZxbXBTQWrHFdXXuq8KOHuDHdH7T2Q8dljWu2eMVGrePLYeqdNjnDEsN6/QbIOpjw1U/SeR66tracT2k50lUNgTn9Hkvo648uz7+VIPd8qtxeknIE1ss0UdCKKA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Tg5Mqxje; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 478B8C433C7; Fri, 22 Mar 2024 13:18:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711113488; bh=HAKZ7cmBvqd6aSZFndMvBQg8DCq/3g1+oo1NIjMagHA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Tg5MqxjepdspHRR5uIeSUEeWCfndJZIpSgKe5FNHJJqmLE8TtJoPdgXPSaF1/BAnt 3b8xJx44vgVFDcWSHIiV4GNpwiA2jzR24jTfpzlP4KT/7RTVmhADBjAR5CXhEhylSZ vVuJ6xWDmq5X7aGKKemy+oRbwO1su6TyVwZwbE3kF1vD82zXUKLpXl9xRINP8wzIFJ ADmPiYCWiFmKbasxDDegdvYYnZV2VWsHjNW1Jntt6EjCuOl5TPdsqy5UFvNgfFXZmn 7otGsrHTDEi+gzhJR7EzrjSc7VCVMFcnyXi5oVipuDUyXyE4RmpzKepF5rcmUxro7F BpIEI9IYsunmA== Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-513e25afabaso2437770e87.2; Fri, 22 Mar 2024 06:18:08 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCXvjStzuX6anZY0Z9hec8pUrd9eNOKcbo4SFpMfhb7dVhICz/uPXlHAHJzguHRaxyNWd7u0F82P0rtFczHTE2bFHRtSfkxhfk6YnHKK X-Gm-Message-State: AOJu0YwM4Famppuy8l1XnonvhQcLvteK0BpiZxPltfVBXEHGnFmZyf+N U5ESU0NStKAJsVrApeW1eIfxxeCVUNzZKOccYgyaWTEZLGiIv1EVP9ppVBxJip23jHX+8J5eID6 yFjDkguiGbQe5t+1g7g7bhOpwB0U= X-Received: by 2002:a19:3813:0:b0:515:92a8:25c1 with SMTP id f19-20020a193813000000b0051592a825c1mr1495201lfa.56.1711113486649; Fri, 22 Mar 2024 06:18:06 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240322110058.557329-1-kazuma-kondo@nec.com> In-Reply-To: <20240322110058.557329-1-kazuma-kondo@nec.com> From: Ard Biesheuvel Date: Fri, 22 Mar 2024 14:17:55 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] efi/libstub: fix efi_random_alloc() to allocate memory at alloc_min or higher address To: =?UTF-8?B?S09ORE8gS0FaVU1BKOi/keiXpCDlkoznnJ8p?= Cc: "linux-efi@vger.kernel.org" , "tomenglund26@gmail.com" , "linux-kernel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, 22 Mar 2024 at 12:01, KONDO KAZUMA(=E8=BF=91=E8=97=A4=E3=80=80=E5= =92=8C=E7=9C=9F) wrote: > > Following warning is sometimes observed while booting my servers: > [ 3.594838] DMA: preallocated 4096 KiB GFP_KERNEL pool for atomic al= locations > [ 3.602918] swapper/0: page allocation failure: order:10, mode:0xcc1= (GFP_KERNEL|GFP_DMA), nodemask=3D(null),cpuset=3D/,mems_allowed=3D0-1 > ... > [ 3.851862] DMA: preallocated 1024 KiB GFP_KERNEL|GFP_DMA pool for a= tomic allocation > > If 'nokaslr' boot option is set, the warning always happens. > > On x86, ZONE_DMA is small zone at the first 16MB of physical address > space. When this problem happens, most of that space seems to be used > by decompressed kernel. Thereby, there is not enough space at DMA_ZONE > to meet the request of DMA pool allocation. > > The commit 2f77465b05b1 ("x86/efistub: Avoid placing the kernel below LOA= D_PHYSICAL_ADDR") > tried to fix this problem by introducing lower bound of allocation. > > But the fix is not complete. > > efi_random_alloc() allocates pages by following steps. > 1. Count total available slots ('total_slots') > 2. Select a slot ('target_slot') to allocate randomly > 3. Calculate a starting address ('target') to be included target_slot > 4. Allocate pages, which starting address is 'target' > > In step 1, 'alloc_min' is used to offset the starting address of > memory chunk. But in step 3 'alloc_min' is not considered at all. > As the result, 'target' can be miscalculated and become lower > than 'alloc_min'. > > When KASLR is disabled, 'target_slot' is always 0 and > the problem happens everytime if the EFI memory map of the system > meets the condition. > > Fix this problem by calculating 'target' considering 'alloc_min'. > > Cc: linux-efi@vger.kernel.org > Cc: Tom Englund > Cc: linux-kernel@vger.kernel.org > Fixes: 2f77465b05b1 ("x86/efistub: Avoid placing the kernel below LOAD_PH= YSICAL_ADDR") > Signed-off-by: Kazuma Kondo Hello Kazuma Kondo, Thanks for your patch. I will take it as a fix. You sent the same patch twice, right? Is there any difference between the t= wo? > --- > drivers/firmware/efi/libstub/randomalloc.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/firmware/efi/libstub/randomalloc.c b/drivers/firmwar= e/efi/libstub/randomalloc.c > index 4e96a855fdf4..7e1852859550 100644 > --- a/drivers/firmware/efi/libstub/randomalloc.c > +++ b/drivers/firmware/efi/libstub/randomalloc.c > @@ -120,7 +120,7 @@ efi_status_t efi_random_alloc(unsigned long size, > continue; > } > > - target =3D round_up(md->phys_addr, align) + target_slot *= align; > + target =3D round_up(max(md->phys_addr, alloc_min), align)= + target_slot * align; > pages =3D size / EFI_PAGE_SIZE; > > status =3D efi_bs_call(allocate_pages, EFI_ALLOCATE_ADDRE= SS, > -- > 2.39.3