From patchwork Thu Dec 7 18:44:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Dobriyan X-Patchwork-Id: 13483793 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A27BC4167B for ; Thu, 7 Dec 2023 18:44:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6FD36B007B; Thu, 7 Dec 2023 13:44:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D1EF36B007E; Thu, 7 Dec 2023 13:44:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BE60E6B0080; Thu, 7 Dec 2023 13:44:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id AE4E76B007B for ; Thu, 7 Dec 2023 13:44:39 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 70844401F6 for ; Thu, 7 Dec 2023 18:44:39 +0000 (UTC) X-FDA: 81540898278.17.58F89EE Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) by imf20.hostedemail.com (Postfix) with ESMTP id 94C961C0019 for ; Thu, 7 Dec 2023 18:44:37 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=REG6JY4x; spf=pass (imf20.hostedemail.com: domain of adobriyan@gmail.com designates 209.85.218.46 as permitted sender) smtp.mailfrom=adobriyan@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701974677; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=oXf2lAj8XDVwxOmvMwYO09ccy2NUDabGlY5Knm7jvKw=; b=h4A2lAXl4CdpS4xcK6O2NUhgbZ8PIRfYG8LKH1z0oGkGq9+OC2SOejg05AFysKKyJ8j52T bXnwnZkOMqQfwP0NgKZgLTd5DEa6a89kn5npFVrlbY7fEhULdrqObc7Hkj8LD+qGLsCOiR iK8gBIIM+1hRwxvWBp+tfS09UULonIA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701974677; a=rsa-sha256; cv=none; b=HD5MeAWm7t5AuZHAm7IoL+469KnJuzAqbTqVZAAwiHwJtOVz1P9YAPGSAHXr1XmU8mnpDA KRQ1edvLeN/GnnK7P304eaMAnxloRgu1M/xAnkUWvhYr5fVEfxazLV+attUdLKXS6ENn3Q +m/W8qqqanZ6xIB+v00w88BYCEX0PuA= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=REG6JY4x; spf=pass (imf20.hostedemail.com: domain of adobriyan@gmail.com designates 209.85.218.46 as permitted sender) smtp.mailfrom=adobriyan@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a1e2f34467aso124795366b.2 for ; Thu, 07 Dec 2023 10:44:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701974676; x=1702579476; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=oXf2lAj8XDVwxOmvMwYO09ccy2NUDabGlY5Knm7jvKw=; b=REG6JY4xSDOCsb3OfOuKa0eQLbzlFPWnCBwx9fp770FHn0uMv/SOCbGjxYVLCX0pZc Bsffwv8IU5atIg7XYmqUeJDa2ZMsIfQFrAS7NvukorY1xy6Yq5/JSwiiCzC6YmaP4A14 uSCLG+1buCvySSn7MHUM8XxKJJ5NJuu1doYpGMVFtggsIzAZ1dPyyAusTgBPMZ9+WwwY clk+P+bqB5Dnqoyqh8wPFDY6rGS3cwawEsZ1hS1IGnkW9PWhDPSE+c+CAPoBpG4PxpV9 /iAxGbNSuW7AFtIpDsDocRZ7h846n5fxWPbyF+ugo7eNbJMMKhbSoZf5y5AP+KtqX920 tylA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701974676; x=1702579476; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=oXf2lAj8XDVwxOmvMwYO09ccy2NUDabGlY5Knm7jvKw=; b=Kbpvya+4op/+r64fWrjCGnT68foxW9QD6UNWRDWLi31X0zC81oUP520d2JeKdcYGAR 4S6sw90aKE4ffdUpjxSCsgcObvzFl6TCiFI4RfbI0ju2Wat+RzUwzdy/20Ti2eG57naN dH6SClCIDn7/Nu632Bx0tUaw/SWDRGps5Iv3lDnzk+vALpuvqgNKsOQ5wKS+xKKfqKGr uQttopgvsv16x8kNSl3Xck4QyP9E5NTZrgZHELN0bv2+PQ3eftmLfK2hf8AbbsvexwXa eim2Uii2saQ78FBsFu4PwRhEqnSCxdzX6eh9FG7zoC0utrSC0fshE9luRP4LUWXPV901 rixQ== X-Gm-Message-State: AOJu0YyEniXhSLrNSW7w+aE45R/bA0C6YHjXvv5DfnflkyJFUCDUC3j1 R98pUA5gd4DKQ7Q05pZ8mg== X-Google-Smtp-Source: AGHT+IG8L55PCNrPucgL1/sFRpfhV8423aIsHP2Z6wo2jY09zJ7nIsTSpi7u44scNAUHxdQnF0wQYw== X-Received: by 2002:a17:907:bb90:b0:a12:635d:fcd1 with SMTP id xo16-20020a170907bb9000b00a12635dfcd1mr1717463ejc.35.1701974675864; Thu, 07 Dec 2023 10:44:35 -0800 (PST) Received: from p183 ([46.53.254.107]) by smtp.gmail.com with ESMTPSA id tz4-20020a170907c78400b00a1aad4d92dbsm69623ejc.123.2023.12.07.10.44.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 10:44:35 -0800 (PST) Date: Thu, 7 Dec 2023 21:44:33 +0300 From: Alexey Dobriyan To: Andrew Morton Cc: Florian Weimer , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, x86@kernel.org, Eric Biederman , Kees Cook , linux-mm@kvack.org Subject: [PATCH v3] ELF: AT_PAGE_SHIFT_MASK -- supply userspace with available page shifts Message-ID: <8582f7c9-b49d-4d21-8948-59d580e5317c@p183> References: <6b399b86-a478-48b0-92a1-25240a8ede54@p183> <87v89dvuxg.fsf@oldenburg.str.redhat.com> <1d679805-8a82-44a4-ba14-49d4f28ff597@p183> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1d679805-8a82-44a4-ba14-49d4f28ff597@p183> X-Rspamd-Queue-Id: 94C961C0019 X-Rspam-User: X-Stat-Signature: zsghj1cpazg54t8a639cg3yxry3k48u4 X-Rspamd-Server: rspam03 X-HE-Tag: 1701974677-962850 X-HE-Meta: U2FsdGVkX19nxvx57A7eGxMGcgyQaTYPQ0OitEsiK0Es0HR+ficzZaynTmUcoy+P+lA+gfo+OX1M52ImwB53k+xdZOgRulxWAu/jbMC9uIBZIJkFaPigItz7G7afFM2QhqY2gVdn84VQ4eXnnkle78O5+GHGzjK9nlbU50yJWjtRdASa4mWHV4/IBmIYvi33+ERldxvcWXKnIibOsLLc/I5k/liJWdGsyKZ9r9zHnB7P1BBOvc3kbFjdPJNNXHxd9TtTRddAcXR9iQkuud6HZ8VYx+AYHlNDM/NGgTMGpP00o1WB7V56frphOQzI8WJjf/dbMbbgO25kOPc7lgGXtuBXRtQIG+iqnJmjPfNGCuc5755Q2Auw2M+TI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Q/8jBPLuCi3zSuDkiRCLKDHvF0Vg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Report available page shifts in arch independent manner, so that userspace developers won't have to parse /proc/cpuinfo hunting for arch specific strings. Main users are supposed to be libhugetlbfs-like libraries which try to abstract huge mappings across multiple architectures. Regular code which queries hugepage support before using them benefits too because it doesn't have to deal with descriptors and parsing sysfs hierarchies while enjoying the simplicity and speed of getauxval(AT_PAGE_SHIFT_MASK). Note! This is strictly for userspace, if some page size is shutdown due to kernel command line option or CPU bug workaround, than it must not be reported in aux vector! x86_64 machine with 1 GiB pages: 00000030 06 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00000040 1d 00 00 00 00 00 00 00 00 10 20 40 00 00 00 00 x86_64 machine with 2 MiB pages only: 00000030 06 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00000040 1d 00 00 00 00 00 00 00 00 10 20 00 00 00 00 00 AT_PAGESZ always reports one smallest page size which is not interesting. Signed-off-by: Alexey Dobriyan --- v3: better comment and changelog v2: switch to page shifts, rename to ARCH_AT_PAGE_SHIFT_MASK arch/x86/include/asm/elf.h | 12 ++++++++++++ fs/binfmt_elf.c | 3 +++ include/uapi/linux/auxvec.h | 13 +++++++++++++ 3 files changed, 28 insertions(+) --- a/arch/x86/include/asm/elf.h +++ b/arch/x86/include/asm/elf.h @@ -358,6 +358,18 @@ else if (IS_ENABLED(CONFIG_IA32_EMULATION)) \ #define COMPAT_ELF_ET_DYN_BASE (TASK_UNMAPPED_BASE + 0x1000000) +#define ARCH_AT_PAGE_SHIFT_MASK \ + do { \ + u32 val = 1 << 12; \ + if (boot_cpu_has(X86_FEATURE_PSE)) { \ + val |= 1 << 21; \ + } \ + if (boot_cpu_has(X86_FEATURE_GBPAGES)) { \ + val |= 1 << 30; \ + } \ + NEW_AUX_ENT(AT_PAGE_SHIFT_MASK, val); \ + } while (0) + #endif /* !CONFIG_X86_32 */ #define VDSO_CURRENT_BASE ((unsigned long)current->mm->context.vdso) --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -240,6 +240,9 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec, #endif NEW_AUX_ENT(AT_HWCAP, ELF_HWCAP); NEW_AUX_ENT(AT_PAGESZ, ELF_EXEC_PAGESIZE); +#ifdef ARCH_AT_PAGE_SHIFT_MASK + ARCH_AT_PAGE_SHIFT_MASK; +#endif NEW_AUX_ENT(AT_CLKTCK, CLOCKS_PER_SEC); NEW_AUX_ENT(AT_PHDR, phdr_addr); NEW_AUX_ENT(AT_PHENT, sizeof(struct elf_phdr)); --- a/include/uapi/linux/auxvec.h +++ b/include/uapi/linux/auxvec.h @@ -33,6 +33,19 @@ #define AT_RSEQ_FEATURE_SIZE 27 /* rseq supported feature size */ #define AT_RSEQ_ALIGN 28 /* rseq allocation alignment */ +/* + * All page sizes supported by CPU encoded as bitmask. + * + * Example: x86_64 system with pse, pdpe1gb /proc/cpuinfo flags + * reports 4 KiB, 2 MiB and 1 GiB page support. + * + * $ LD_SHOW_AUXV=1 $(which true) | grep -e AT_PAGE_SHIFT_MASK + * AT_PAGE_SHIFT_MASK: 0x40201000 + * + * For 2^64 hugepage support please contact your Universe sales representative. + */ +#define AT_PAGE_SHIFT_MASK 29 + #define AT_EXECFN 31 /* filename of program */ #ifndef AT_MINSIGSTKSZ