From patchwork Mon Feb 5 09:51:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Dobriyan X-Patchwork-Id: 13545133 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 8BDBFC4828D for ; Mon, 5 Feb 2024 09:51:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E76E36B007E; Mon, 5 Feb 2024 04:51:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E26EF6B0081; Mon, 5 Feb 2024 04:51:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CEEDD6B0082; Mon, 5 Feb 2024 04:51:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BD6986B007E for ; Mon, 5 Feb 2024 04:51:49 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 88C931A081F for ; Mon, 5 Feb 2024 09:51:49 +0000 (UTC) X-FDA: 81757283538.21.B10A004 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by imf28.hostedemail.com (Postfix) with ESMTP id C9343C0013 for ; Mon, 5 Feb 2024 09:51:47 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=I0wrkZcm; spf=pass (imf28.hostedemail.com: domain of adobriyan@gmail.com designates 209.85.167.44 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=1707126707; 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: references:dkim-signature; bh=i9QP8sLSXFD8c0XIiwWaagxTqni0W8MyH2JGoDbixps=; b=wTq4cW2XM7vPEIkAk2tSFZYwYer4kaIbDdYxW9b1OzEnFeFSACCXXpr/sOwXdElu4x3ujq 3HoI3bE6SGJuAZq5Ex/mYLBY3mTfYnURrH3NSn0vxgxIjFzpAaFjLGV+5bmRy0mGRTAoVl 2R+znleWCTSrDZL6QW+aLbqXozMMFvw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707126707; a=rsa-sha256; cv=none; b=APx41Kt0y+EsCMngZDBiGP3yf6nTLrxsJhom6qfFVcEgWT9XJTuELK+B7/Yps05E3VAE1J k3BWs97N8Cn0H7yMhFqlkfj57oFdvyDCOJUPZdYRhiznblbBRU7eqB9zEuHzyONeiacPlU 7q8MB46kCxCITO/HSgLJkyuHNQMfCTY= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=I0wrkZcm; spf=pass (imf28.hostedemail.com: domain of adobriyan@gmail.com designates 209.85.167.44 as permitted sender) smtp.mailfrom=adobriyan@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-51121637524so6302133e87.1 for ; Mon, 05 Feb 2024 01:51:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707126706; x=1707731506; darn=kvack.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=i9QP8sLSXFD8c0XIiwWaagxTqni0W8MyH2JGoDbixps=; b=I0wrkZcmBwpF/T7jtJRELipPw3o+9zMpRkxEltnQLxm1L7f1pn5cXQNREGjNM1fD9b LYrnS4yi+t2uCp7F6LO9FILPdV92N+o8Nh6tOvGf5pwXeBb1TamepYKJK9EXiVYek9wr YN8nCId+jl6cde94cgPiuB0Q95gWKeRhIWDKZhqM64tKIt47eDHpQS2UwNviFIsDcq7m NCPcEHneuquPkLVukwaj7S1+vjboTHtFOhJkvSUEN/OgBsuqovBCQ3olcf9YMau2WRKX JuWW6/wrgKoKalSrQ2nbfwOsQ1JhZ+TIjbuF31ne3C1qNJb6qi3DbU1wLqUO3rmNi7Ck firQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707126706; x=1707731506; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i9QP8sLSXFD8c0XIiwWaagxTqni0W8MyH2JGoDbixps=; b=KmOJKlCw0Z8A04ZjZZ7VvLkqPXnutUjz07keF4VA26V5IGpipk36VVd8ktS4Ob9XMm fbKvsyKC1kGD+wcs35fdQSsafh7oUIQck9RRdLRiG1Bwwd+qrgiKmClvB3RNFOWRUDUo x+dX5UVSNfRWGMy4AJXge7sGZzxZngcl+WoxWXTXFYoFLMAXcWAr/YduVM72/Hk9nnk8 uLNfDCVMCsdZEJBh8SEDawhyRaj0A5+FyCmVySYRp/eB+ovvDE0uLcMXYJlK/J18N7Cb yZIHqU5BDQJTwYmxC+TsPaKf9ES4yDKF6bALpc+ilbZhJsiEY7w6NgGs4k8TwHONDQex w4KQ== X-Gm-Message-State: AOJu0YxRw4qQ6eBuC0xzH2+QYxI1/RDWiQonIXeRM3G4BfF9WGhnp/yP Lsj9unY6l20mdHu9WkLuLN39N5mYtsNd8050/1GzN3ctNBgC600= X-Google-Smtp-Source: AGHT+IG3u5ggjEsSDL7MT/pLmzTNV8XHvX4QEZsaL47FKIg0HYDLm9WmIXoLP32VEFxp4cl1PLFfoA== X-Received: by 2002:a05:6512:5d2:b0:511:5026:abac with SMTP id o18-20020a05651205d200b005115026abacmr1291306lfo.52.1707126705874; Mon, 05 Feb 2024 01:51:45 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUfK2Xe0hei/qlHWOhySOhXm8UOa1jMRN0nvZP1IMVWQZ+EiUbp68D9txmHhcU/GAZZViUL4kEKBwvXVZGSpZze26FNr3hvcaZX2ZCye3pCe88m1gCZW7nEdAEPdlsZCFdomMV08jNIi0QhqcEXPNoQc/Lu9alGlDQiiNlUpz4JUCtzY7IEhy2N0kIDCdUEPCxhOJwUoRoaRSafBAyRQRy7AZvjTqCTqIJwu83eTTJV39uxKKJygWeZZ5gph+49GEBQGlOrcSDMCD1IfkOf0UIMkz3xPM0urMt2smjjibA6GHxIOQB59iDX37JXaFbahvm31Jp451YkUq5WmhOFeoCH0xRwQUfrkgZdDoyhIzbrLYaggghrff6dMOzx+pkz4M/uLiTOExRqsf2G Received: from p183 ([46.53.250.163]) by smtp.gmail.com with ESMTPSA id iv15-20020a05600c548f00b0040e9d507424sm8072628wmb.5.2024.02.05.01.51.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 01:51:45 -0800 (PST) Date: Mon, 5 Feb 2024 12:51:43 +0300 From: Alexey Dobriyan To: Kees Cook , Andrew Morton Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Florian Weimer , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, x86@kernel.org, Eric Biederman , linux-mm@kvack.org Subject: [PATCH v4] ELF: AT_PAGE_SHIFT_MASK -- supply userspace with available page shifts Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Rspamd-Queue-Id: C9343C0013 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: xd4mw6gq9ma9ahpnxmzp3z5e414rf5yg X-HE-Tag: 1707126707-824848 X-HE-Meta: U2FsdGVkX1+H/FTmcxV8ejw94n6VjGQGMHcmripO8200nmUSCecg5Nv4oEP8bxX2Hf6rhnwE4iurQtTRg1CbbqRGQVnZen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vack.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(3). Note 1! 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! Note 2! getauxval(AT_PAGE_SHIFT_MASK) output is a function of CPU capabilities only, it is not changed by system memory size, hugepage availability at any given moment, hugetlbfs being mounted, etc. Example output: 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 --- changes since v3 -- even better changelog 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