Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1975698rdb; Tue, 20 Feb 2024 12:51:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX2YiHjjhe/HzRmzgJzGIFs+WV6/g7qi5gzKO7cc5v034T8X2DvAj1Uf9xXbd3mnM5jiYNVPpali8HfI3Zj2fzdU52Ls7gVKXuRPV3nWA== X-Google-Smtp-Source: AGHT+IFWiNEiKv+Jxk/S1j1JmVBrTDS1ReRhkX7zBGtyYN3k/aVois/vTdZFIboHevT9wsMy7gNi X-Received: by 2002:a05:6a21:31c7:b0:1a0:9149:5ff7 with SMTP id zb7-20020a056a2131c700b001a091495ff7mr8627861pzb.2.1708462261598; Tue, 20 Feb 2024 12:51:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708462261; cv=pass; d=google.com; s=arc-20160816; b=SD6Her5flwKoLknz1V1cRZ4ulZxLUKv174Z1JG04tQs4o5X2Vc5dthONFQcbtRHVWH yRZKqrJmJwb4VYQ8+Yq9CC4D0LS8d8y1aWz4u5qJ4AcTVXxMDflDxitePIyZBaFywdul 8XSEiH7YBRzRSIjHmK/FLWAjSkaIjeajP712pFlUA/UcJ6om65RVawj5K8oJE1Vv5WOs NndEBfMhJIE6vujKfEQrI7vHqLDjhPKZ2EcLfvFfcmPIrrthcUmRt8sWX4lI4I/GK8VY tgZraxDPsyQs7J5x8PIawUmIuJjSysFvdV1Qdg4t2NULmsx7YpSL32LPqv2oQ3UvbE5j ch1Q== 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=0dGB6b6W2iBBGppzYQ3zaXWNNMQ0jXoqG7lbe80bwbM=; fh=6yFKuO2qLQR8F27now8wCXlxr6G7+iuFmKmEz6ueBV8=; b=wNkGmBL82E3Pto/g5w9Lfy3WLEcqtXQZieE9A5AzPRsj+V2OPMTaCHCAGnJKKGgftF bmDf7JchZCEmWoDVAopb18xs4XCIDF3lGgODVwULNSEQDm96sUKRbsMsaTQ/6ZaeGc// AQkKIYGnlBxazFhX9N4x1IcKxomba/sxHMm5vLKm6Cukolykr1fwg2HXsIVFnmcw8QI0 EJiLH44bUy1TwTalyBZzGOKJr34PWGAG7mMaD+8aKfEwbLc2lhfKLGa5B2IeLAR93DLx R4ymDXkFvHRXDagRE38iJCwOdsW+cAWn/zMeF1+kbcVnwb78OLKuidIBEent7d/6A9Gb kYSw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=BPJaEjEg; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-73696-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73696-linux.lists.archive=gmail.com@vger.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 s128-20020a637786000000b005d8b7053276si6669574pgc.160.2024.02.20.12.51.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Feb 2024 12:51:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-73696-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=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=BPJaEjEg; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-73696-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-73696-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 9AB18283CAC for ; Tue, 20 Feb 2024 20:51:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9183C1509BD; Tue, 20 Feb 2024 20:50:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="BPJaEjEg" Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5B871509A5 for ; Tue, 20 Feb 2024 20:50:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708462255; cv=none; b=XhIJuO/9OohfeGk59GiFPm62C8VtHYiJ/LQwj7l3aWscOlSyPLjcGsgdVCqN1HxZOdhPn+GD5uWwU6cMvxn5ZoHnghqef9qhX8rZrv2bSd7CVMS2kRbGBZfmuxTUOEdoC5jDqDgmuiQMz1GN0VcyTIebXUtGJhb9iMXo7frCx4U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708462255; c=relaxed/simple; bh=RAe+U6DW6RT1iY/pRrGMdhEaQQttfMr1lO8qOA6ny8I=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=FVuJNcJuwkMrFdtj8eU+T6C6ZckcG+k/Z7+/CknETbqa19xwGtkJT3ElM2f286XKUI/rD4CDakE7EYWAvFRVb7c0Dma9thMzPWCKP6PGO+ipK7uG3UU06nZKw3MXAcnY61v2SU99UrSWa9bjK7ZhLYqV1H7w4ArIVdtz3FkoehA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=BPJaEjEg; arc=none smtp.client-ip=209.85.167.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-512b700c8ebso3287870e87.0 for ; Tue, 20 Feb 2024 12:50:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1708462251; x=1709067051; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0dGB6b6W2iBBGppzYQ3zaXWNNMQ0jXoqG7lbe80bwbM=; b=BPJaEjEgLvali7ABOAsgAaLtfIyOHLoLYt1tqxi+ZgX8rtJMBphcjvr5vsGBgEI5mJ oTUWvkENHCqmszzU7MNkN6GB/MS++JrwU5NWdGRDRJxBmjG7qmBvC8X43qprOAg2VfL+ qe/gmkLQYVL7fjPjR0hF00ucefchnYMtV5Yh80BFACinNJF1EBgkC6rQ3l0qHOPetbIF r72TzysCaFal/qiYGEPWz32R9zsZxqMZwLcahm1jdncy2raLrSK78YddMZtu/3e1517G ZZcoylVeox19ixCWjiK7Y3RI8fOoBlfiGBh6tteHKZ9KH0Nl+CB0R1Nz32tbRtNMAvt+ iw8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708462251; x=1709067051; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0dGB6b6W2iBBGppzYQ3zaXWNNMQ0jXoqG7lbe80bwbM=; b=E+EqWOzamI70hgLtHOX5kknmZb7p49TcDeDFBWR5bzG7biGd3gauPqzca7sddUNicP Tw5IugNhkOvekmlvPHgXiLNRoiDT1qTdH7kT22okeEnC4Nnzaw8eYjgJFXRQTs1uBN8f qzD61toZIoxNAVfxkxjpejFaeHEIXLfyrjh02SJTMgUw1+aWAJ4LKO6BG6Rhpj6HZtTA ajLDdYNMGjnjfLZnXs35p1i+9NO8OcmxSzoyEX/qTBB/hOEcNydI+F00UdVNA68rxSru O32hvdtfo4CN3XnI2xO/IEAmI8JtQLRxMM/iIf9vLN0T8kJFXs6d+MDy9MpN95DMCfOU 4uMA== X-Forwarded-Encrypted: i=1; AJvYcCW06UCY07WitpQe8arbuVR7J93Y5PT7IgsseFbE9G4hkGtOpxSQOT6fK7nK62mbSvKhLsriKOBq9kWh33CnKCLuwmyAz0HaH5BkCEZ6 X-Gm-Message-State: AOJu0YzQtlyDNkFDDsBNKPE7oE9pj7TWeVjq8HOQK/kQ0RJyrJrAGna2 QrPSwaiW9xiFfRvi1W0PrF4YN72P6Xb6+q9jGu5IwQFfqjVgdyOj0AAcC2M737aEpSglvj9Hb7C gZy989WnwzuKz3cPejQpCwOAiqBO/SXOj8ty4gg== X-Received: by 2002:ac2:4825:0:b0:512:ac36:e871 with SMTP id 5-20020ac24825000000b00512ac36e871mr4871541lft.43.1708462250816; Tue, 20 Feb 2024 12:50:50 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: In-Reply-To: From: Alexandre Ghiti Date: Tue, 20 Feb 2024 21:50:38 +0100 Message-ID: Subject: Re: [PATCH] [RFC] sparsemem: warn on out-of-bounds initialization To: dvlachos Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, clameter@sgi.com, akpm@linux-foundation.org, rppt@kernel.org, arnd@arndb.de, paul.walmsley@sifive.com, palmer@dabbelt.com, mick@ics.forth.gr, csd4492@csd.uoc.gr Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Dimitris, On Mon, Feb 19, 2024 at 3:59=E2=80=AFPM dvlachos wr= ote: > > Alexandre, > > Yes, you understood correctly, I indeed proposed to change > pfn_to_page()/page_to_pfn() but your solution appears to solve the > problem without risking any modifications to the conversion macros. In > addition, your solution seems to be valid for any phys_ram_base/pfn > value inside the limits of physical memory. > > However, I wanted to note that if the pfn is large enough, vmemmap will > not be a valid SV39/48 address unless a pfn offset is applied to it. I > can't think of a possible scenario where vmemmap would be used without > an offset. I would like to know your opinion on that, does it concern you= ? No, I don't see when that could happen, that would be a mistake and then it would be easy to catch with this patch :) > > Finally, do you want me to send a patch or will you do it? > I'd be happy to help if you need, and if it's too much of a pain for you, I'll do it, let me know. Thanks, Alex > Dimitris > > On 2/18/2024 10:58 PM, Dimitris Vlachos wrote: > > > > > > > > > > -------- Forwarded Message -------- > > Subject: Re: [PATCH] [RFC] sparsemem: warn on out-of-bounds > > initialization > > Date: Tue, 6 Feb 2024 22:17:44 +0100 > > From: Alexandre Ghiti > > To: Dimitris Vlachos > > CC: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, > > clameter@sgi.com, akpm@linux-foundation.org, rppt@kernel.org, > > arnd@arndb.de, paul.walmsley@sifive.com, palmer@dabbelt.com, > > mick@ics.forth.gr > > > > > > > > Hi Dimitris, > > > > On Fri, Feb 2, 2024 at 2:59=E2=80=AFPM Alexandre Ghiti > > wrote: > >> Hi Dimitris, > >> > >> On Fri, Feb 2, 2024 at 2:50=E2=80=AFPM Dimitris Vlachos wrote: > >>> From: Dimitris Vlachos > >>> > >>> Hello all > >>> > >>> I am sending this email with regards to a bug that I discovered in th= e Sparse Memory Model configuration and more specifically, the Virtual Memo= ry Map optimization. Moreover, I would like to inquire about possible ways = of fixing it. > >>> > >>> I work as a pre-graduate research assistant at ICS-FORTH in the Compu= ter Architecture and VLSI Systems laboratory. > >>> We were running some tests in our prototype hardware (RISC-V), where = we noticed that the Kernel crashes early in the boot process with the follo= wing setup: > >>> > >>> We are using the default Kconfig configurations (defconfig) that incl= udes Sparse Memory + Virtual Memory Map. > >>> The DRAM base address of our system is : 0x800000000000 > >>> A 3-level page table is used (Sv39). > >>> > >>> When the vmemmap optimization is enabled the macro pfn_to_page() is c= alled, which offsets the vmemmap with the pfn argument to acquire a struct = page pointer. > >>> > >>> As our DRAM starts at 0x800000000000, the initial pfn will be 0x80000= 0000000 divided by PAGE_SIZE. The calculation result will be: > >>> 0xffffffcf00000000 (vmemmap start) + (0x800000000 (pfn) * 64 (sizeof= (struct page)) > >>> > >>> This causes an overflow as the number is very large, the resulting ad= dress is 0x1c700000000 which is not a valid Sv39 address (all bits above bi= t 38 should be set) and does not belong to the kernel=E2=80=99s virtual add= ress space. > >>> > >>> The same will happen with all valid pfns as the memory is being initi= alized. When the Kernel will try to access a page using pfn_to_page, a page= fault will occur crashing the system. > >>> It should be noted that when the vmemmap is disabled the system boots= normally. > >>> > >>> However, I would like to emphasize another important detail. When the= DRAM base is small enough to avoid an overflow, the virtual memory map map= pings will be initialized out of bounds with regard to the vmemmap address = space which is 4GiB in Sv39. > >>> The system will not crash, but the address space used for this purpos= e will be outside of the designated range. > >>> > >>> Everything mentioned above holds true when Sv48 is used as well. Give= n a high enough DRAM base the system will either crash or perform false ini= tializations. Given the fact that virtual memory map is not only used by RI= SC-V systems, this could be an issue for other architectures as well. > >>> > >>> The kernel already contains mminit_validate_memmodel_limits() that ch= ecks memory limits. > >>> However, it only checks physical memory ranges. I added a few checks,= provided that virtual memory map is enabled, to determine whether offset s= tart and offset end fall inside the range of virtual memory map. Otherwise,= a warning will be printed. > >>> > >>> Finally, I would like to ask you for any information/advice that coul= d help me fix/prevent the issues that I mentioned (if it=E2=80=99s possible= ) or recover from them at runtime by disabling the optimization and reverti= ng back to the non-vmemmap mappings. > >>> > >>> Thank you in advance. > >>> Best regards, > >>> Dimitris Vlachos > >>> --- > >>> mm/sparse.c | 20 ++++++++++++++++++++ > >>> 1 file changed, 20 insertions(+) > >>> > >>> diff --git a/mm/sparse.c b/mm/sparse.c > >>> index 338cf946d..33b57758e 100644 > >>> --- a/mm/sparse.c > >>> +++ b/mm/sparse.c > >>> @@ -149,6 +149,26 @@ static void __meminit mminit_validate_memmodel_l= imits(unsigned long *start_pfn, > >>> WARN_ON_ONCE(1); > >>> *end_pfn =3D max_sparsemem_pfn; > >>> } > >>> + > >>> + /*check vmemmap limits*/ > >>> + #ifdef CONFIG_SPARSEMEM_VMEMMAP > >>> + > >>> + unsigned long vmemmap_offset_start =3D (unsigned long) pfn_to= _page(*start_pfn); > >>> + unsigned long vmemmap_offset_end =3D (unsigned long) pfn_to= _page(*end_pfn); > >>> + > >>> + if (vmemmap_offset_start < VMEMMAP_START) { > >>> + mminit_dprintk(MMINIT_WARNING, "pfnvalidation", > >>> + "Start of range %lu -> %lu exceeds bounds of = SPARSEMEM virtual memory map address range %lu -> %lu\n", > >>> + vmemmap_offset_start, vmemmap_offset_end, VME= MMAP_START,VMEMMAP_END); > >>> + WARN_ON_ONCE(1); > >>> + > >>> + } else if (vmemmap_offset_end > VMEMMAP_END ) { > >>> + mminit_dprintk(MMINIT_WARNING, "pfnvalidation", > >>> + "End of range %lu -> %lu exceeds bounds of SP= ARSEMEM virtual memory map address range %lu -> %lu\n", > >>> + vmemmap_offset_start, vmemmap_offset_end, VME= MMAP_START,VMEMMAP_END); > >>> + WARN_ON_ONCE(1); > >>> + } > >>> + #endif > >>> } > >>> > >>> /* > >>> -- > >>> 2.39.2 > >>> > >> > >> Since struct pages are only available for memory, we could offset > >> vmemmap so that VMEMMAP_START actually points to the first pfn? > > > >> Hello Alexandre, > >> My first thought was to use a "better" offset for vmemmap as well.Sinc= e pfn is essential for pfn_to_page and page_to_pfn i think that maybe we co= uld get a smaller number out of pfn to offset the vmemmap and ensure that i= t's bounds are respected for pfn_to_page operation. > >> If we use only a part of the pfn for the offset we could get the pfn = from page_to_pfn by adding PAGE-VMEMMAP_START > >> to the DRAM base/PAGE_SIZE which is the first pfn. > > > > Let's keep the discussion here. > > > > IIUC you propose to change pfn_to_page()/page_to_pfn() but what I > > proposed was to directly offset vmemmap like this: > > > > #define vmemmap ((struct page *)VMEMMAP_START - phys_ram_base > >>> PAGE_SHIFT) > > > > So that the first page in vmemmap corresponds to the first page of > > physical memory, that avoids the burden of changing the conversion > > macros, WDYT? > > > > Thanks, > > > > Alex