Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1089452lqd; Thu, 25 Apr 2024 05:59:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUw3VV/v9ydv1qFDehmj/uJX7l+bfVndZXbjW2eWcXbvzBY+PN+XLdyNyZaG9vJWlbJJN8AQckRBdzDs8I7XhNdowGzE48jndFVKiktwQ== X-Google-Smtp-Source: AGHT+IH2gYEzEHMyMMrtSP3fwsfAKz4cHx+n5+/siDIHTPwTLko6dklbnqaynPnrCi/VBXuLAJ0j X-Received: by 2002:a17:90b:4c04:b0:2a2:3252:cb83 with SMTP id na4-20020a17090b4c0400b002a23252cb83mr5774259pjb.38.1714049975400; Thu, 25 Apr 2024 05:59:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714049975; cv=pass; d=google.com; s=arc-20160816; b=nQsfHM+aWjoK4CjvfLkCcvUCgqVMjMBFzAHYkGN0TTEg9q3yOZ+A3QDL14OXnZvjfT 3TRl2OpZxzo54zAYamEVv60IP7PtgyxTfWNC12oY7IkAKA/IgCmLIcRtcUiMPr+A996o FxyuyeP6eo+NgYvzvWuMXePHJgtFmkQnS5X4fdLpDFq8HNRiMnGF6VWHB6x13a4V8NHN RV8ak0vt1lsJ/Gl9JvDQY0okf/i4I33SNWSrDPhYWV/i724DUJ/lOL4dqseCLL8wRStz XJDyMDhljbCd8Jb/2t5XZv5AMrphuYGvyDJYh2n1n6lmZjkd4VrOrHgtoE9uGN6K0OAS 3aHQ== 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:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=WAs++0f4uShXSnqiMMs0bhexZ/nOUx6L44yMin5pUmg=; fh=WMlvzq5WChnobC5I+L3bg0pkRCuzyFD3GOCg0mSd6oc=; b=xAKtRFZGUzE3IUJn9ojPTQ5wtsQbM6AteCRDOtIVn8iVA4ROiLzMKShO2OgfVcYxiB dDK2U0ho8YIPVy6HTF2YcXkZ0GR2DpnBlpN6T0MhlMmOpNgsZXMjI/b7rBvspdIXlMP4 ccfNien0j9FLBhGoj+MfEGAG5n97ITtfItTAKo8Ano7fKTQwtoJu/xOIEu2j0UZDygex QkBSRc7i10nL8n5e1G1r7t2iIleRMZcgpziVWDSqoUXz8/GfB2Qh1ZZRl/kCyJamILh1 sQto3qMSSPgEiIWcZ46cSg6ITgvSIPJ3t5NEv4WEM3L9CBQySQY8wmwCkzhh1sUcal+R 1Szg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PkcNfV3L; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-158564-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158564-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. [139.178.88.99]) by mx.google.com with ESMTPS id bb11-20020a17090b008b00b002a0322b7450si15014258pjb.114.2024.04.25.05.59.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 05:59:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158564-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PkcNfV3L; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-158564-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158564-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 6C56E28671F for ; Thu, 25 Apr 2024 12:58:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EC116149C64; Thu, 25 Apr 2024 12:58:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PkcNfV3L" 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 206E0149C54; Thu, 25 Apr 2024 12:58:16 +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=1714049897; cv=none; b=Em9dE7PRjnt/utif5oOdVx1gTkVQ623oDVsbbjKWdv9gdDKJyVewovpw6ebn4H3t777/rclrIgt4fhQndkJ2K1sBkabAaVoEjF7aBdtYPuLrQ6Az0yC/YyDOgHnevK4iP+ySjEHUsHqZPaQX9bJ1MMn6jk6fSguediDloZLmNwQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714049897; c=relaxed/simple; bh=JyQm9VGmJkZTxCcFWAIKfFZx/qAJNcE1EJUqGNLUBZc=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=CDcVursr0N6MuFXRB0fMbbSXbgOVPEBweG3sYnqTwnu/k+mOs/BA7hUzBonZZtMS4tCZwocoX/Xi6mIlsXr7BjDMFdRatYG2ZgFxQTynzck6az+TDpFiLj9TTT5DQdP0QQuoNyZLfcccBXqeMFFqrEdbu9GjxPwAJhy2hCozFgo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PkcNfV3L; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31B1DC113CC; Thu, 25 Apr 2024 12:58:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714049896; bh=JyQm9VGmJkZTxCcFWAIKfFZx/qAJNcE1EJUqGNLUBZc=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=PkcNfV3LVD62NBTVN5zwqCBk8/ewv7iodL7F3a/SAIaUh2T5MMh0bSJooF51j5UEz MP4G6fU64nvIX49BeQBIFPQnXuTs0bghJGDbrwZBxWyyEX7G2zx58jfsCsjNKJQbbO EGUnKZrlA7r1Fn8SKH5AsnFGlKbOb1XfaBx/ogxkjSH3I6iBa96oRkIFBCbYaTv0zf JUkyQK94PZIc2XeMiNRmEi+tEL1U5IbrZ8YORU1Uk5xxOGwKYE3CKY58tAp+/fNpQh wKQqkNnLZges1L9IoV/SBAfR9BKWuZpy+IOIxVAjXw8TKEJgGD0Nhmz+tyDSfTCoLC dhfx0rfL12Kgw== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: Nam Cao , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Morton , Baoquan He , Sami Tolvanen , Chen Jiahao , =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, rppt@kernel.org Cc: Nam Cao , stable@vger.kernel.org Subject: Re: [PATCH] riscv: fix overlap of allocated page and PTR_ERR In-Reply-To: <20240425115201.3044202-1-namcao@linutronix.de> References: <20240425115201.3044202-1-namcao@linutronix.de> Date: Thu, 25 Apr 2024 14:58:12 +0200 Message-ID: <87bk5xbnvf.fsf@all.your.base.are.belong.to.us> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Nam Cao writes: > On riscv32, it is possible for the last page in virtual address space > (0xfffff000) to be allocated. This page overlaps with PTR_ERR, so that > shouldn't happen. > > There is already some code to ensure memblock won't allocate the last pag= e. > However, buddy allocator is left unchecked. > > Fix this by reserving physical memory that would be mapped at virtual > addresses greater than 0xfffff000. > > Reported-by: Bj=C3=B6rn T=C3=B6pel > Closes: https://lore.kernel.org/linux-riscv/878r1ibpdn.fsf@all.your.base.= are.belong.to.us > Fixes: 76d2a0493a17 ("RISC-V: Init and Halt Code") > Signed-off-by: Nam Cao > Cc: Thanks for picking it up again, Nam. This passes my test: Tested-by: Bj=C3=B6rn T=C3=B6pel > --- > arch/riscv/mm/init.c | 21 +++++++++++---------- > 1 file changed, 11 insertions(+), 10 deletions(-) > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > index 968761843203..7c985435b3fc 100644 > --- a/arch/riscv/mm/init.c > +++ b/arch/riscv/mm/init.c > @@ -235,18 +235,19 @@ static void __init setup_bootmem(void) > kernel_map.va_pa_offset =3D PAGE_OFFSET - phys_ram_base; >=20=20 > /* > - * memblock allocator is not aware of the fact that last 4K bytes of > - * the addressable memory can not be mapped because of IS_ERR_VALUE > - * macro. Make sure that last 4k bytes are not usable by memblock > - * if end of dram is equal to maximum addressable memory. For 64-bit > - * kernel, this problem can't happen here as the end of the virtual > - * address space is occupied by the kernel mapping then this check must > - * be done as soon as the kernel mapping base address is determined. > + * Reserve physical address space that would be mapped to virtual > + * addresses greater than (void *)(-PAGE_SIZE) because: > + * - This memory would overlap with ERR_PTR > + * - This memory belongs to high memory, which is not supported > + * > + * This is not applicable to 64-bit kernel, because virtual addresses > + * after (void *)(-PAGE_SIZE) are not linearly mapped: they are > + * occupied by kernel mapping. Also it is unrealistic for high memory > + * to exist on 64-bit platforms. > */ > if (!IS_ENABLED(CONFIG_64BIT)) { > - max_mapped_addr =3D __pa(~(ulong)0); > - if (max_mapped_addr =3D=3D (phys_ram_end - 1)) > - memblock_set_current_limit(max_mapped_addr - 4096); > + max_mapped_addr =3D __va_to_pa_nodebug(-PAGE_SIZE); > + memblock_reserve(max_mapped_addr, (phys_addr_t)-max_mapped_addr); Nit (and only if you respin for some reason): Move max_mapped_addr into the if-clause, or simply get rid of it (all on one line). Regardless: Reviewed-by: Bj=C3=B6rn T=C3=B6pel Bj=C3=B6rn