Received: by 2002:ab2:3c46:0:b0:1f5:f2ab:c469 with SMTP id x6csp64925lqf; Thu, 25 Apr 2024 22:35:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCULCBhaxR+xuO+rdL6A8gxkfk9LLqtRB6yP56lJGMJSP96ij7jaYVuR5oh3jOkrGZ7+BZTXFCHecWHwHj539yB6jap19BBzuk+oyN0fNA== X-Google-Smtp-Source: AGHT+IGfK4R2u0akVDOyLdtiAShlZCUc8k3tzAOUUxResRhCD8Q3+JgwwQ5MHzx5FvALuVQvUTXz X-Received: by 2002:a50:9b49:0:b0:572:4681:8c1d with SMTP id a9-20020a509b49000000b0057246818c1dmr950123edj.9.1714109718250; Thu, 25 Apr 2024 22:35:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714109718; cv=pass; d=google.com; s=arc-20160816; b=iZtrCAWtS8kWoCkn17zlnCeNT1UjShBmeSoNPZWEUBQS9EpDdSC4+GuRV6lBBNFA10 8VVNXXCHaJ3Zzst70dBAdkK3wfdRSNRv5izjYsel83mmpLCOn4o0Kmy1+gZJ282S8Ak8 PkA6h7WITrRXA3SrZL6/xCgvfZC+/cgd1TN19Bdpn0bNPYcxOALVtB+tOjSfx7cSMQIO XkFYcIu4XWShKce86IU2VCnRvE1KU/cCMQMgoFkG+junQoEvTDuam5o4cO5YRzJz4Yyr Ai8/m5wgmTlnSKIVtZb8nZuijrtuo/rhHOJNzAFjLZBRe4dVpqIG37XkHV0/B4WdVZZo sNfQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=foq4Q8PvZkq9zU4IigJFrLsXDgHAc8sJOzoG8ZuvD9U=; fh=XQnbXcD6ju+/fcrIXvdzvOipe34KokyizoKLTe/HqGA=; b=ar/4tMii/1jp15badyzu/rPHI9z8i8W6h1HVVFAHi655lYZhDx5TFftp7kHJCK62KH NZhLuCj0ZhqFqSu7J7StVKYmTUDS9j2AW17wKTqJhzJuLZzovVDqpNjVxH2eDBFf7UFU ghZ9c0ezOTj963DMcEwlLsVK8cWTVS7QwzUWCdXF3bcPo2gwYIMnXf9/68WE1MFp6dhN iPK+6EtFENzYq3fWpRCXeDuP/YsAWATWmsoqxd3snPUSzXBQH2U4D/F3/QOR3ptrNBAd ANuwzBn+xapc6QxtQU43TtVCqR7Qakt5s0S05gofrLTblP4pOJtN/LTl0OE3cNhahLPV qZgg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="D9U1FN/2"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-159523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159523-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id da6-20020a056402176600b0056bfcbdb535si10778198edb.396.2024.04.25.22.35.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 22:35:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="D9U1FN/2"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-159523-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159523-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 am.mirrors.kernel.org (Postfix) with ESMTPS id E482A1F2261C for ; Fri, 26 Apr 2024 05:35:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 882E913A3ED; Fri, 26 Apr 2024 05:35:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="D9U1FN/2" 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 AFBF91849; Fri, 26 Apr 2024 05:35:09 +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=1714109709; cv=none; b=i0TyRNldMASKg9XKHzL/rmXAArvegyTrJImjf7Ijze5cQxoD7qNMuJqxc1jLsb9z67i+G+DHvvWlyAYd8d8o8+ETpJ9THkGVRY5wlfImjdQKML+Ssft8lpkePiaBdxQ5Q/rs+8JDlhA+Il8/yQYNfp19v5hC/1DfsLDrULTPQdI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714109709; c=relaxed/simple; bh=1WCFz9g7vTUTNKQ3TiTEs820xuQE/CC/C1Y28k+/o60=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WWiHq+sUaKDg/klaEmVG2ck8Dcr+YtCOnck7KEi+lj1NvleJPSh1yAOt40MWxicZVbXNBBTYNrjSMnomlHXBFhgwW2tJdaWCKkBfGhqJ+lY0j+IFDPrPMVy9Rc848dgmOAfbQXHI7oljub8Z7ComCDvEXTLelMsftu15commrM0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=D9U1FN/2; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3F1DC113CD; Fri, 26 Apr 2024 05:35:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714109709; bh=1WCFz9g7vTUTNKQ3TiTEs820xuQE/CC/C1Y28k+/o60=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=D9U1FN/20RXfKopPasfFAShCCrhI3NZlsOg+C2eP02+0f+PVfhCFII+xKjnSQlGiA WDtJWv9d9PJoPXD6bMntp9AlPegnDBvKFHnValE1Slnn1GwGwkCbfyChnxSq4pf8IS IlIGbt3gh+bI7cmVaw6gZ10KEhToTg0YZ2qcGa/9+EU2l5v52L6oalNAdhS0TSe6u/ 0Kk7MZA1SmDJrP5HU7QISnArPbpTRD6P3/7KuAUDPEm/TRiSA0Ufnoe5QEptNkmw1X 3g5D8qANo93l/o0OFPibSM8NgVzIy9w9G2/ZcjTfejylb25AXON8HJnIb593qoAXRt jM7RBaqkgkRqw== Date: Fri, 26 Apr 2024 08:33:47 +0300 From: Mike Rapoport To: Nam Cao Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Andrew Morton , Baoquan He , Sami Tolvanen , Chen Jiahao , =?iso-8859-1?Q?Bj=F6rn_T=F6pel?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, =?iso-8859-1?Q?Bj=F6rn_T=F6pel?= , stable@vger.kernel.org Subject: Re: [PATCH] riscv: fix overlap of allocated page and PTR_ERR Message-ID: References: <20240425115201.3044202-1-namcao@linutronix.de> 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=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20240425115201.3044202-1-namcao@linutronix.de> On Thu, Apr 25, 2024 at 01:52:01PM +0200, Nam Cao wrote: > 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 page. > 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?rn T?pel > 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: Reviewed-by: Mike Rapoport (IBM) > --- > 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 = PAGE_OFFSET - phys_ram_base; > > /* > - * 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 = __pa(~(ulong)0); > - if (max_mapped_addr == (phys_ram_end - 1)) > - memblock_set_current_limit(max_mapped_addr - 4096); > + max_mapped_addr = __va_to_pa_nodebug(-PAGE_SIZE); > + memblock_reserve(max_mapped_addr, (phys_addr_t)-max_mapped_addr); > } > > min_low_pfn = PFN_UP(phys_ram_base); > -- > 2.39.2 > -- Sincerely yours, Mike.