From patchwork Wed Jan 31 01:07:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Charlie Jenkins X-Patchwork-Id: 194420 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1600272dyb; Tue, 30 Jan 2024 17:08:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IH7PXG0VDaEPezd7LsUquLSN2q+MhkupX0pMR6LCzNVAp61v/uQTlys3Lrx8vGEtPqMK5Zw X-Received: by 2002:a17:902:7408:b0:1d5:8bf4:c79b with SMTP id g8-20020a170902740800b001d58bf4c79bmr304556pll.39.1706663292671; Tue, 30 Jan 2024 17:08:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706663292; cv=pass; d=google.com; s=arc-20160816; b=UtD0tSu0/CnGH8LtrfBfRaWk9/DAkfSzOuO2eHPDyu8mlpBVYVQprBeNEuJ/D1x8C0 yOIt/LJXgh64Q8UVQPmKWJXMFd3BYywjsBIN7ZqGHtMPuTsQwSLN9lcQniuCaOy8XI5D d7RjYV/SEw+oUyqonR61xIQ21HJvxtv+SusVn7lXC1GzLOG/b8D6yCNRelBFNrvXNWIz /7mA//mIgKxl7HwT/OwugvtkFF+lNq6XKF623goNme6VrGe02Z7GGQZUr37Vr9+sEY0G +o9qpWdXl8Efwtkd6yWB6Ojb/S42QUzCUDvFT31p0biZu78VFSUsL6+W8z3qi6P5Z3Z8 e7yA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=ISQ44kMse6Lkn35EK9G7PdGD5a4TdSkkmzrVy/QiW8Y=; fh=tx69x9izaew9pU58lAVqxvEXvLeJaCKei6PesWq3Qy4=; b=M+CTss3eTwXQxP6w+vDinApk3L/ilEy2rTHhuwALLoHBbCLHnl+bPUtO7bnWlL9UaN wOAz4pepfUVgQIpj77o2O0oZvmJJQyM+4MddR3NnUKqoEw6mwsWaHjcyMRDU3IWk/2A8 6rhl7oidzYuwutCysMuEL0ancj4lZuJSA47jUkbyY4g+XxWW88IQQ+N2AaNwRodXwxV3 Vh4tjtCUX6VXpvi0Ikv4MXoVOw2cRGil7kljpF68tOS3i1xcMth+vE5monvY9ZPa5pBw jHu3HETdoFICGAi9BiLaOJ6kXrAcWxqNF20LoQmtyo0dxESx+TfiGH1t7L3vl4WqcZ0l E6fw==; 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="s/LAlH4K"; 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-45567-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45567-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCXJJ+knt2P4FxpECkEmM2J8ePCntMd4+XBFqlXSSQ5wuOPPoKqH++fdWlC4nfP1gG1O9s+5Kw8i+n6AbfTiStkoRL/fUg== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id c9-20020a170902d48900b001d924d8a773si668978plg.234.2024.01.30.17.08.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 17:08:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-45567-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="s/LAlH4K"; 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-45567-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45567-ouuuleilei=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 sy.mirrors.kernel.org (Postfix) with ESMTPS id B60B4B23CE1 for ; Wed, 31 Jan 2024 01:08:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2816C538A; Wed, 31 Jan 2024 01:07:11 +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="s/LAlH4K" Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 3A83E17BC8 for ; Wed, 31 Jan 2024 01:07:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706663227; cv=none; b=e3BhrUlAG30UEAu/VKyin8tSJdgHknDGv3xXh/CVf06iCE0tMXUBnIkbzT5oD4SrZmc28QZ1qYUSxBlT2tup58lQAhUSFtFb77zdkKGPMEYpsZ2dWeWWvcwA5EZUMDHEv0v1dNPH1c8ffJlAV8/7+tq5XUE+lnSVJsofV8RgzDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706663227; c=relaxed/simple; bh=3agc++PZjt4adgBO71Cp8EyaaOma0q0miHLEwYoM4tM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gdl0I0GzFHYEsn3hhKaccl1HTPeWh4jx9F+9TeAYXmFeDT5+OySfUyIhUw44vrTBPMVlA/WuRV/OLg+Eryky7q70O1sDAnMx98ID7tPqRAPMCx8iNr2CjmV2zNl10iHJvT4DqTsii9w0hA8OQ1MRhraLZGvrUK6s8zOE1L1Y1gI= 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=s/LAlH4K; arc=none smtp.client-ip=209.85.214.182 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-pl1-f182.google.com with SMTP id d9443c01a7336-1d746ce7d13so38369875ad.0 for ; Tue, 30 Jan 2024 17:07:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706663225; x=1707268025; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=ISQ44kMse6Lkn35EK9G7PdGD5a4TdSkkmzrVy/QiW8Y=; b=s/LAlH4KYfzIG9i7MUYQHqhOQHjIGC7nOFBc25lm1enz8pGDTqsVDCnRzNRt3kIeAk tMPGA9fDaJtSKKM02P35uzSvWqSo8eucod8Wh8DwiYSN96d1AHpvdwhHO8expevUO1cE 77LN669pNyFgJQ125KHOwgiBrBED542MRB61Syu/TmueG5dXrTBFFsFFiUgVu7yKxu2o uQ4dnfW3iSIj8hVIby5E4RWh2xVgQXkDH5BsEKVBGwyyuSZk2uc5gj9/vTdDs48QIp/t yboel+Ok+xlxtUGonkxysrg2rP1gIvOX7THZwCu1ka3quDXvvLtOmiDHgfGuFgYZk4/W FGmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706663225; x=1707268025; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ISQ44kMse6Lkn35EK9G7PdGD5a4TdSkkmzrVy/QiW8Y=; b=n3OUcq0SY3rVQZ4waiuRjGX+T5wpjE27cBAaKW+SeMGZHE7GmWTIHYxwf7W012XhJT OelMTLco8/fPTMHeUEudEAnPYPw+79wGOy1KffO/XEE6bNuMEGbsh2f5ksimQ3iHUby7 IFx3XRkoYelVsdgoF+7Blz2mIkFFmOExNeqMv55JwD/QbZkEr5AzJ426q8F0FZ1Cugkh 4+T1spyKYy6+IHI6f7gkd++iS3MNBXxWDuD2UbhPdC9H8R6yBKLHSU8FRCVgJ1Np2aAe rdpBCjcmIX620gSh+PuzKe2CVFioyqBYDs49n+EX1niJwbZU4en5mTKWKuAaqFsN8l6Q ItUQ== X-Gm-Message-State: AOJu0YxRe/45JrNT0cvuBCzYOeSO+Lk8unO/VVUMbw6XEp8iM36GSU3S NCziRGjpAFpXrWZupFeCwm6Otqw9a9fiS1qpe5o2LHiBVcIE5PtvOvnHhY9aSLM= X-Received: by 2002:a17:902:d891:b0:1d5:36e8:9ac0 with SMTP id b17-20020a170902d89100b001d536e89ac0mr323253plz.50.1706663225597; Tue, 30 Jan 2024 17:07:05 -0800 (PST) Received: from charlie.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id r12-20020a170903410c00b001d8d3c276c4sm4970822pld.35.2024.01.30.17.07.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 17:07:05 -0800 (PST) From: Charlie Jenkins Date: Tue, 30 Jan 2024 17:07:00 -0800 Subject: [PATCH v3 1/3] riscv: mm: Use hint address in mmap if available Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240130-use_mmap_hint_address-v3-1-8a655cfa8bcb@rivosinc.com> References: <20240130-use_mmap_hint_address-v3-0-8a655cfa8bcb@rivosinc.com> In-Reply-To: <20240130-use_mmap_hint_address-v3-0-8a655cfa8bcb@rivosinc.com> To: Alexandre Ghiti , Paul Walmsley , Palmer Dabbelt , Albert Ou , Shuah Khan , Jonathan Corbet , Yangyu Chen Cc: linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, Charlie Jenkins X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1706663222; l=3095; i=charlie@rivosinc.com; s=20231120; h=from:subject:message-id; bh=3agc++PZjt4adgBO71Cp8EyaaOma0q0miHLEwYoM4tM=; b=AIKMSezNvXcphAUCcNCg21BBdaXUpcacXo54uEbl2OdGKMHckPpGf4N0pTfpvOAZ+GZExeI6t yXu3KlulVP2CvsGt2kGidzgw5nA924NJE/B8ZORdKYgHBzunKKkQbuk X-Developer-Key: i=charlie@rivosinc.com; a=ed25519; pk=t4RSWpMV1q5lf/NWIeR9z58bcje60/dbtxxmoSfBEcs= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789566168918659755 X-GMAIL-MSGID: 1789566168918659755 On riscv it is guaranteed that the address returned by mmap is less than the hint address. Allow mmap to return an address all the way up to addr, if provided, rather than just up to the lower address space. This provides a performance benefit as well, allowing mmap to exit after checking that the address is in range rather than searching for a valid address. It is possible to provide an address that uses at most the same number of bits, however it is significantly more computationally expensive to provide that number rather than setting the max to be the hint address. There is the instruction clz/clzw in Zbb that returns the highest set bit which could be used to performantly implement this, but it would still be slower than the current implementation. At worst case, half of the address would not be able to be allocated when a hint address is provided. Signed-off-by: Charlie Jenkins --- arch/riscv/include/asm/processor.h | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index f19f861cda54..8ece7a8f0e18 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,22 +14,16 @@ #include -#ifdef CONFIG_64BIT -#define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) -#define STACK_TOP_MAX TASK_SIZE_64 - #define arch_get_mmap_end(addr, len, flags) \ ({ \ unsigned long mmap_end; \ typeof(addr) _addr = (addr); \ - if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \ + if ((_addr) == 0 || \ + (IS_ENABLED(CONFIG_COMPAT) && is_compat_task()) || \ + ((_addr + len) > BIT(VA_BITS - 1))) \ mmap_end = STACK_TOP_MAX; \ - else if ((_addr) >= VA_USER_SV57) \ - mmap_end = STACK_TOP_MAX; \ - else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \ - mmap_end = VA_USER_SV48; \ else \ - mmap_end = VA_USER_SV39; \ + mmap_end = (_addr + len); \ mmap_end; \ }) @@ -39,17 +33,18 @@ typeof(addr) _addr = (addr); \ typeof(base) _base = (base); \ unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base); \ - if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \ + if ((_addr) == 0 || \ + (IS_ENABLED(CONFIG_COMPAT) && is_compat_task()) || \ + ((_addr + len) > BIT(VA_BITS - 1))) \ mmap_base = (_base); \ - else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \ - mmap_base = VA_USER_SV57 - rnd_gap; \ - else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \ - mmap_base = VA_USER_SV48 - rnd_gap; \ else \ - mmap_base = VA_USER_SV39 - rnd_gap; \ + mmap_base = (_addr + len) - rnd_gap; \ mmap_base; \ }) +#ifdef CONFIG_64BIT +#define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) +#define STACK_TOP_MAX TASK_SIZE_64 #else #define DEFAULT_MAP_WINDOW TASK_SIZE #define STACK_TOP_MAX TASK_SIZE