Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp503654lqt; Mon, 18 Mar 2024 14:29:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXENESAy/v8V/h+QC8dy12QfIUlCgK+c9MX+9EQEly3c05ULvpnEUfJtmhCYdhsa7nwCNoTL5pEg1ahcZEBNxlvoHmL0AJTw/M05Z+tqw== X-Google-Smtp-Source: AGHT+IHzdI6T/EtyER0g9gTKIeu4EL8iiSt2lWRcXfdnN7gslnI9+hFgAVOfv/L+KDb4ru7qkYKT X-Received: by 2002:a0c:e7cd:0:b0:696:300c:b181 with SMTP id c13-20020a0ce7cd000000b00696300cb181mr876666qvo.18.1710797385463; Mon, 18 Mar 2024 14:29:45 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710797385; cv=pass; d=google.com; s=arc-20160816; b=Itzw9shXVD9kEK7XYgio56uy1NbwP1vKsKU7FldZsvfeK9N/et6Hl/O0tNS2sSJWit glF0seXHWoTG3USum0LF+p3oF3TVNZ7s2U6SjrmcWlxLXDrga5XFVLi2B9SwdU0ZQUGG omrKb35ZdMKB7BqN2jBWOzL4MQ1Mn5Lld1m6KfVocMGaACzHAHv6NAljtHRc5L+wHpdw lBp4Qu2RpRcA+FotnsQHJAFnkOH8zEIQxPPiOa62yjMlJyC7BTgAcaZ+l2M6aUFR62pf mw387+JTc84b7fVWdax/hQ9K1m5jrTrLdCBajxgFf+5ZeqVwzc5aDZjJ3FFbOwAIE3k+ gz5A== 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:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=/Nul1eVuq4XLe3SGFf2DVNn6S2hK2sqq4oKFGkGjGXg=; fh=igfkpi3S2T+9A1/gqXhulWfZ1fYW0kJULK/bAkOjOmM=; b=tV+w31bGyD1K/OPN5xdYXKvnmHSYOz6JV1Xwpue2H+pkgaSFTvqj7T34/GVd867Ch5 /clMA3mL2AkhjS2Z6d3t73heZOxlbLDek6HnXiOTkfAQZWtU0R6VKU9/iZDGvGDKpiOb ndWHQtXGd2J5V1c4st1oD3LUdD1bfkMgFO+zGXCBh3NnRR5sEA7F2eBZFLllTBkaRFx3 h/RFr15TnAvCXFRW+wDFREKjCHFCJsb7kxbfdvR56vtwgqbMIh4w8K79Lip7HFVhTV2H criqx0cz3a89geVTpeX2AvTz30QTnk7vWEO5ArbwSZOL2tG4crw4X6qEbH4iF+v7RpUy cK+w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=mRbbR7m8; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-106701-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-106701-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id dv5-20020ad44ee5000000b00692d61f01b3si5400505qvb.87.2024.03.18.14.29.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 14:29:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-106701-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=mRbbR7m8; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-106701-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-106701-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 241231C21257 for ; Mon, 18 Mar 2024 21:29:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3140F58AC7; Mon, 18 Mar 2024 21:29:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="mRbbR7m8" Received: from mail-il1-f174.google.com (mail-il1-f174.google.com [209.85.166.174]) (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 99E8058233 for ; Mon, 18 Mar 2024 21:29:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710797375; cv=none; b=V/YjtzXnAHXbHTiSDwH0IDmbv+ZUKKMYca46c0NX+ltm+S6TMxPkr18gFTArj2tOPUBPeba7bGnTeLi69QGwpcl1TwakFTYTRmYhSmwlpDuf98ybMKxugBR5JNxtZTxk2TqFdnxNz9QZQLDdYS3YIGPMWLVul4jZO4+MpOGnGtY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710797375; c=relaxed/simple; bh=wa4AsEHowlTcQ7Yg5/g6MIQX/SG3V6t/gqBj8pYSItg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=jOpLWVN8s9j4fz/IN0G1jDJwcICaiYgNq8RmPjxywO+19UfxJk1RoqeRyYQfhs5IwK3JAl8fmGqsva9F0QSIWF1r3YQ52iCdmHJ8fx9XIW7bM8JzrMxGpNI8ddCt3yU1JVlWhfCkdPo4ebufNS1KWYuFrpiXCChXDbWG1FOk9+k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=mRbbR7m8; arc=none smtp.client-ip=209.85.166.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-il1-f174.google.com with SMTP id e9e14a558f8ab-366ad40144fso5896345ab.0 for ; Mon, 18 Mar 2024 14:29:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1710797373; x=1711402173; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=/Nul1eVuq4XLe3SGFf2DVNn6S2hK2sqq4oKFGkGjGXg=; b=mRbbR7m8X5JfixQi24ou2bbV4TNAeyCcRHYyskqLRwWQlu2hIMJrgWZeltOcocde8V oYTGVIrrQCr9CzaZ5+4P7hw5oyNx67zWgThX5yxPKmvlLwf8DARgGuQyKSiBHzb3snJd GTFA6eQQVAb2Affv1ujR6K1z4J95S38g9t5xHt7JCkyUymxWO04gLEYxBi7CEzitPFQY VrTmNLE7k00lL9ol2ddDeeswc8UaMiQ9KPSZ5D0ZE+UEsslVqF7wYORfJSHQjC8LXphb 2VwR0zLl+kMurAh1akG3ZiQgHP/3HyX+N3OCUwODOSH/x4ZirgLQWOHiPn1wYhc6X5It Jt1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710797373; x=1711402173; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/Nul1eVuq4XLe3SGFf2DVNn6S2hK2sqq4oKFGkGjGXg=; b=oaV7ZWN3WVq3sfZT5QAORtXB4YKhaMIHQ8Ixx7RmmXFB6AQhoB4SeVQjhYV6oMkvL9 NdVw93vYwqxwlF46X5nt6ZwAvSMe/yAPeTpfRG/Sq5NLG5xg9xAIbmDmfJ0hcZ5wG+TT JrpWALxCoGLBgXLmP37BTCFtAa3sLAgyzfij0bgzFYF5vYMr86pc3Oz3s0lqXhyArwEN VgAVXgwd+SDE0s6raVADldwSNaNT7cEn1uDYM8J4BSxdEGFfuKJw15eIsSQur55xWmzk vfNP/2KguTVekWKKgrtTTvOriD2rvjSkS5PGzD/4Hul1pK+6O8OoiN3cCKIDwyE5hHLM hqEQ== X-Forwarded-Encrypted: i=1; AJvYcCWu8kaXtAWNJynk9LDppRTCQebX5K3ZRQzpTAMkkWmX7O+rFHRy5npbbJk5XFY1mxLomMzFwY+FMx0vhs5UIAR8u+Jb3LIj6XlXlz3B X-Gm-Message-State: AOJu0YxGviObRz4PnjgcxQfPmzIShOxvSvqdgO7O83lIe6+Q0nRJhKoE XVUe5Cr0BYg2KLOSx8gzntk0U5Bt6TtF7L34y2+URCFCdHkCb4bSsSSofMeKXIU= X-Received: by 2002:a05:6e02:1064:b0:366:8c58:42ea with SMTP id q4-20020a056e02106400b003668c5842eamr887284ilj.11.1710797372753; Mon, 18 Mar 2024 14:29:32 -0700 (PDT) Received: from [100.64.0.1] ([136.226.86.189]) by smtp.gmail.com with ESMTPSA id l13-20020a056e0205cd00b00366164573dfsm2512024ils.63.2024.03.18.14.29.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Mar 2024 14:29:32 -0700 (PDT) Message-ID: <88de4a1a-047e-4be9-b5b0-3e53434dc022@sifive.com> Date: Mon, 18 Mar 2024 16:29:30 -0500 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] riscv: Define TASK_SIZE_MAX for __access_ok() Content-Language: en-US To: Alexandre Ghiti Cc: Palmer Dabbelt , linux-riscv@lists.infradead.org, Albert Ou , Andrew Morton , Charlie Jenkins , Guo Ren , Jisheng Zhang , Kemeng Shi , "Matthew Wilcox (Oracle)" , "Mike Rapoport (IBM)" , Paul Walmsley , Xiao Wang , Yangyu Chen , linux-kernel@vger.kernel.org References: <20240313180010.295747-1-samuel.holland@sifive.com> From: Samuel Holland In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hi Alex, On 2024-03-18 3:50 PM, Alexandre Ghiti wrote: > On Wed, Mar 13, 2024 at 7:00 PM Samuel Holland > wrote: >> >> TASK_SIZE_MAX should be set to the largest userspace address under any >> runtime configuration. This optimizes the check in __access_ok(), which >> no longer needs to compute the current value of TASK_SIZE. It is still >> safe because addresses between TASK_SIZE and TASK_SIZE_MAX are invalid >> at the hardware level. >> >> This removes about half of the references to pgtable_l[45]_enabled. >> >> Signed-off-by: Samuel Holland >> --- >> >> arch/riscv/include/asm/pgtable-64.h | 1 + >> arch/riscv/include/asm/pgtable.h | 1 + >> 2 files changed, 2 insertions(+) >> >> diff --git a/arch/riscv/include/asm/pgtable-64.h b/arch/riscv/include/asm/pgtable-64.h >> index b99bd66107a6..a677ef3c0fe2 100644 >> --- a/arch/riscv/include/asm/pgtable-64.h >> +++ b/arch/riscv/include/asm/pgtable-64.h >> @@ -17,6 +17,7 @@ extern bool pgtable_l5_enabled; >> #define PGDIR_SHIFT_L4 39 >> #define PGDIR_SHIFT_L5 48 >> #define PGDIR_SIZE_L3 (_AC(1, UL) << PGDIR_SHIFT_L3) >> +#define PGDIR_SIZE_L5 (_AC(1, UL) << PGDIR_SHIFT_L5) >> >> #define PGDIR_SHIFT (pgtable_l5_enabled ? PGDIR_SHIFT_L5 : \ >> (pgtable_l4_enabled ? PGDIR_SHIFT_L4 : PGDIR_SHIFT_L3)) >> diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h >> index 6066822e7396..2032f8ac5fc5 100644 >> --- a/arch/riscv/include/asm/pgtable.h >> +++ b/arch/riscv/include/asm/pgtable.h >> @@ -867,6 +867,7 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte) >> #ifdef CONFIG_64BIT >> #define TASK_SIZE_64 (PGDIR_SIZE * PTRS_PER_PGD / 2) >> #define TASK_SIZE_MIN (PGDIR_SIZE_L3 * PTRS_PER_PGD / 2) >> +#define TASK_SIZE_MAX (PGDIR_SIZE_L5 * PTRS_PER_PGD / 2) >> >> #ifdef CONFIG_COMPAT >> #define TASK_SIZE_32 (_AC(0x80000000, UL)) >> -- >> 2.43.1 >> > > I think you also need to change the check in handle_page_fault() by > using TASK_SIZE_MAX instead of TASK_SIZE, otherwise the fixup can't > happen (https://elixir.bootlin.com/linux/latest/source/arch/riscv/mm/fault.c#L273). It is not necessary to change that check in fault.c unless we expect to handle exceptions (outside of userspace access routines) for addresses between TASK_SIZE and TASK_SIZE_MAX. It looks like the call to fixup_exception() [added in 416721ff05fd ("riscv, mm: Perform BPF exhandler fixup on page fault")] is only intended to catch null pointer dereferences. So making the change wouldn't have any functional impact, but it would still be a valid optimization. > Or I was wondering if it would not be better to do like x86 and use an > alternative, it would be more correct (even though I believe your > solution works) > https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/page_64.h#L82. What would be the benefit of using an alternative? Any access to an address between TASK_SIZE and TASK_SIZE_MAX is guaranteed to generate a page fault, so the only benefit I see is returning -EFAULT slightly faster at the cost of applying a few hundred alternatives at boot. But it's possible I'm missing something. Regards, Samuel