Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2881748lqp; Mon, 25 Mar 2024 11:52:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWawLQLgYlAuocfW5tIWjuWYBiA6OkY/5U6Y7W7NMEX5Me2+FXIaM8P66lESY4Tl2kZaJJLT+1cSeYyHdrJ3eYYpUR59e4cH4kPbuKf8w== X-Google-Smtp-Source: AGHT+IGnJ9klzwu16GdyK73N1dh8vD7So7F0ipV3NnHxyuoyBpg1//why2bnrJue3XBgZUAUKV6g X-Received: by 2002:a05:6a20:3c8f:b0:1a3:571b:13a3 with SMTP id b15-20020a056a203c8f00b001a3571b13a3mr10068190pzj.27.1711392771889; Mon, 25 Mar 2024 11:52:51 -0700 (PDT) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bv132-20020a632e8a000000b005dc88260f76si7903317pgb.330.2024.03.25.11.52.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 11:52:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-117711-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; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-117711-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-117711-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3B358323D5C for ; Mon, 25 Mar 2024 18:43:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7063511733; Mon, 25 Mar 2024 18:30:25 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9E6D44C7D for ; Mon, 25 Mar 2024 18:30:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711391425; cv=none; b=cdVimvrH2mH5qOiYuBbx+eE071v30Po9FbXDaYvlDOIlEhbICSgo6f5PvAbgtc7ryBNSexYBZ3LD4WzHY+3x6d2LIoClMTD/heLh2Imavuo50h4kVNYJX2XSxWEuPtVx9ybwAwCebkdFabMe3b+zpwW3Yqn/x6QS5qL/pY48E9I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711391425; c=relaxed/simple; bh=2VXxq70Xu5k4H6YqcuOfLx1H+38OuiTNGp8GyXVcVVU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=TIJheins2TJaOuXO1D18BKgXTQXYBSiKgESHmNA3gd4wZvCasERWq6C7nHLOjyqcK0+R3FAkmQI15ZSGHk+NciTXF8FB9kAqOT2KBz74q0e9YFTbs0fDvC9a8hG7kDiq+GCV3wSnpTge2rqchknJJdUjtkvYO8Yrai5iQPUDosA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 3769B2F4; Mon, 25 Mar 2024 11:30:54 -0700 (PDT) Received: from FVFF77S0Q05N (unknown [10.57.16.150]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5FEC93F64C; Mon, 25 Mar 2024 11:30:17 -0700 (PDT) Date: Mon, 25 Mar 2024 18:30:08 +0000 From: Mark Rutland To: Arnd Bergmann Cc: Alexandre Ghiti , David Laight , Samuel Holland , Alexandre Ghiti , Palmer Dabbelt , "linux-riscv@lists.infradead.org" , Albert Ou , Andrew Morton , Charlie Jenkins , guoren , Jisheng Zhang , Kemeng Shi , Matthew Wilcox , Mike Rapoport , Paul Walmsley , Xiao W Wang , Yangyu Chen , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] riscv: Define TASK_SIZE_MAX for __access_ok() Message-ID: References: <20240313180010.295747-1-samuel.holland@sifive.com> <88de4a1a-047e-4be9-b5b0-3e53434dc022@sifive.com> 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=us-ascii Content-Disposition: inline In-Reply-To: On Mon, Mar 25, 2024 at 07:02:13PM +0100, Arnd Bergmann wrote: > On Mon, Mar 25, 2024, at 17:39, Mark Rutland wrote: > > > Using a compile-time constant TASK_SIZE_MAX allows the compiler to generate > > much better code for access_ok(), and on arm64 we use a compile-time constant > > even when our page table depth can change at runtime (and when native/compat > > task sizes differ). The only abosolute boundary that needs to be maintained is > > that access_ok() fails for kernel addresses. > > As I understand, this works on arm64 and x86 because the kernel > mapping starts on negative 64-bit addresses, so the highest user > address (TASK_SIZE = 0x000fffffffffffff) is still smaller than the > lowest kernel address (PAGE_OFFSET = 0xfff0000000000000). Yep; the highest posible user address is always below the lowest possible kernel address, and any "non-canonical" address between the two ranges faults. There's some fun with TBI (Top Byte Ignore) and MTE, but that only affects how to mangle the pointer before the check, and doesn't affect the definition of the VA boundary. In general, so long as TASK_SIZE_MAX is <= the lowest possible kernel address and TASK_SIZE_MAX > the highest possible user address, it all works out. > If an architecture ignores all the top bits of a virtual address, > the largest TASK_SIZE would be higher than the smallest (positive, > unsigned) PAGE_OFFSET, so you need TASK_SIZE_MAX to be dynamic. Agreed, but do we even support such architectures within Linux? > It doesn't look like this is the case on riscv, but I'm not sure > about this part. It looks like riscv is in the same bucket as arm64 and x86 per: https://www.kernel.org/doc/html/next/riscv/vm-layout.html .. which says: | The RISC-V privileged architecture document states that the 64bit addresses | "must have bits 63-48 all equal to bit 47, or else a page-fault exception | will occur.": that splits the virtual address space into 2 halves separated | by a very big hole, the lower half is where the userspace resides, the upper | half is where the RISC-V Linux Kernel resides. Mark.