Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp269317lqp; Wed, 22 May 2024 04:17:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXWtaTBvbCkPmtrY0MTzVsDN5qM1WV4MBPOnQ0x2NLAv+VXi4PG6hFvcvivdKtx8kzjQEz5V9733h/sktZ9d1By8b9jfcozP+7BWCzADg== X-Google-Smtp-Source: AGHT+IEUCyY3szGvcxpZg9CgjqB7LOQMwmdsdBkNHvEzYbTRfDe1fvr/H/72jhIDmW+9PDi6aDaw X-Received: by 2002:a05:6512:3eb:b0:51c:3f4c:ef6c with SMTP id 2adb3069b0e04-526bf82d6c6mr1147459e87.40.1716376641204; Wed, 22 May 2024 04:17:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716376641; cv=pass; d=google.com; s=arc-20160816; b=FvfbLUekmnIUD0LXST76vMDFvLLMWQay/7KhGHHrBQPKchlJkHc/ryGiRzKkpIZF81 DPdB61gUo6Wu7UnQVjMfpouY/mR9ZU3DD3Is2OH6fvX53jftB3hO3SBYma657BqyGnyE 0HujBu5yADSnb0l7xTVWZKyJ02WU3X0NoViy50P4QtZ6Bn8/WaXP39sbUptpnE8NIshz to1sC5WO87APkQRctwL7aaj4nHXjvCS9gQIbnnAck8mEpd34QcrT1WIPhIOOW6+3+/wJ LRXHgDMQRu3BtqkerMP8KjHjfP0fF+JKRLfIYrzZguUXOdBTyu15gOPXRAG/bI/MWEO2 MygQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=ARjiyEEoy2u+Txgwy/5g1PO+Xx+oZvNoYvkmJDOsUPI=; fh=M1AiTh1DN5xWvdd1dgiJK45QnetfuNFdyWv6luiIG8Y=; b=qwfeo8cnWjLK8GmwzOVOYxXy4M9LGfM1jSHo3zobC1aX/xI3NQarlxt+iGEuRNGBPi JcsNmcIl4TChVQjFYbeADz+h4rO3YVxMn7DcKsQ8G/Yrx8tL1CUYndD4zWdqSOEJ6vza ht6WqA142Q1UuYmbeSb/f2P0znp0Xx+ZtpKXr08SkpB60kcneBZLiAHa7+3fUYkYWAZV fxM/4tvubjv3uGhRzf20M6JWRNbn0y3VSLkaI4yCxPVWknQSX+RleEoMkV7xCnVfWpPz 36n5E10DGkV/63OqUfBAH1Hp0lK6HrqU7jUVkXZgRJ6DGH364dYcAw2QmNcJBrFBg/WH oOVA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-186124-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186124-linux.lists.archive=gmail.com@vger.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 a640c23a62f3a-a5a17b21ef3si1552272966b.450.2024.05.22.04.17.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 04:17:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-186124-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; arc=pass (i=1 spf=pass spfdomain=ghiti.fr); spf=pass (google.com: domain of linux-kernel+bounces-186124-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-186124-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 am.mirrors.kernel.org (Postfix) with ESMTPS id E6B791F21BE3 for ; Wed, 22 May 2024 11:17:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7DB78824AE; Wed, 22 May 2024 11:17:13 +0000 (UTC) Received: from relay1-d.mail.gandi.net (relay1-d.mail.gandi.net [217.70.183.193]) (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 8F6097BB17; Wed, 22 May 2024 11:17:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.193 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716376633; cv=none; b=elSUWCm/ZTD0vZJOi5ztw3VTfMh3ZQI6AwLrpCfRvLPemKRx4Pz5eU2b8N9TBMJK90o2NBEjrMQslMq6UjckP8l8mnPW6IHpJP6uQX6SxrcCM8HOSSFHJyanCCVHz382DZ4QHYT60lfxwDId1qaWUfpfAdIRu+XGSWg4lfBDiHk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716376633; c=relaxed/simple; bh=CPEqhqyGm2UZF8UZo0VoyFgQT3dnqGYzpOjGsl/H4IE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Hz34u17DKb3a7SFS1xXzII4J3LINXUNjuh4bem41a7LeE4igEtUXcF4vTEV1aHjoT7c5NnR23ZVd7it9cGUMVG/aZEWd/sZkjNKFhTFONxGXoM4zwfT2Aa0EfrwyxOaPkk8DGpV3972nuEKl0t8c1v0j1RbYZ+PeZ1tYyiLEOCY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr; spf=pass smtp.mailfrom=ghiti.fr; arc=none smtp.client-ip=217.70.183.193 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ghiti.fr Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ghiti.fr Received: by mail.gandi.net (Postfix) with ESMTPSA id F3A84240008; Wed, 22 May 2024 11:16:57 +0000 (UTC) Message-ID: <35bf5362-ae70-4fbe-acff-691bfe4a9e34@ghiti.fr> Date: Wed, 22 May 2024 13:16:56 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] riscv: force PAGE_SIZE linear mapping if debug_pagealloc is enabled To: Nam Cao , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: stable@vger.kernel.org References: <2e391fa6c6f9b3fcf1b41cefbace02ee4ab4bf59.1715750938.git.namcao@linutronix.de> Content-Language: en-US From: Alexandre Ghiti In-Reply-To: <2e391fa6c6f9b3fcf1b41cefbace02ee4ab4bf59.1715750938.git.namcao@linutronix.de> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-GND-Sasl: alex@ghiti.fr Hi Nam, On 15/05/2024 07:50, Nam Cao wrote: > debug_pagealloc is a debug feature which clears the valid bit in page table > entry for freed pages to detect illegal accesses to freed memory. > > For this feature to work, virtual mapping must have PAGE_SIZE resolution. > (No, we cannot map with huge pages and split them only when needed; because > pages can be allocated/freed in atomic context and page splitting cannot be > done in atomic context) > > Force linear mapping to use small pages if debug_pagealloc is enabled. > > Note that it is not necessary to force the entire linear mapping, but only > those that are given to memory allocator. Some parts of memory can keep > using huge page mapping (for example, kernel's executable code). But these > parts are minority, so keep it simple. This is just a debug feature, some > extra overhead should be acceptable. > > Fixes: 5fde3db5eb02 ("riscv: add ARCH_SUPPORTS_DEBUG_PAGEALLOC support") > Signed-off-by: Nam Cao > Cc: stable@vger.kernel.org > --- > Interestingly this feature somehow still worked when first introduced. > My guess is that back then only 2MB page size is used. When a 4KB page is > freed, the entire 2MB will be (incorrectly) invalidated by this feature. > But 2MB is quite small, so no one else happen to use other 4KB pages in > this 2MB area. In other words, it used to work by luck. > > Now larger page sizes are used, so this feature invalidate large chunk of > memory, and the probability that someone else access this chunk and > trigger a page fault is much higher. > > arch/riscv/mm/init.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c > index 2574f6a3b0e7..73914afa3aba 100644 > --- a/arch/riscv/mm/init.c > +++ b/arch/riscv/mm/init.c > @@ -682,6 +682,9 @@ void __init create_pgd_mapping(pgd_t *pgdp, > static uintptr_t __init best_map_size(phys_addr_t pa, uintptr_t va, > phys_addr_t size) > { > + if (debug_pagealloc_enabled()) > + return PAGE_SIZE; > + > if (pgtable_l5_enabled && > !(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE) > return P4D_SIZE; You can add: Reviewed-by: Alexandre Ghiti Thanks, Alex