Received: by 2002:a05:7412:798b:b0:fc:a2b0:25d7 with SMTP id fb11csp9609rdb; Wed, 21 Feb 2024 15:05:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV4yZSemGgqE0aPxm2Z8JFsMXYudttUrZ/Da6KQLiGi2LH1psFUh6gambSgCblP5B/jiWnwi80IRewQGw/GyGyD9tCedrP3IK6kvwqlsQ== X-Google-Smtp-Source: AGHT+IHz/OxEs+qxyqfX+NtnZTCTL2hMOQp7yLhOY48Awk/MP5f+IoBre/dX9FWdkpp30wPAW6aO X-Received: by 2002:aa7:d702:0:b0:564:2519:5a84 with SMTP id t2-20020aa7d702000000b0056425195a84mr8167659edq.8.1708556749517; Wed, 21 Feb 2024 15:05:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708556749; cv=pass; d=google.com; s=arc-20160816; b=mvMXU3/A58SbspBGoxwSJ0oJTh3vbm9JTRP7Wm5izB1Kt3AuhwSsybMwvk4Ufi/ONC Rs8wpDRp50Dvn+doH7Rm6uCuwCVG15EYKmqbns+gUEILzG7ECgvQhSZx2cWKt14UYb5C aifch70S9x8qJBFstnLEqlqCMoiTtFs0/vIygjtiWadDFqSxfPlNb7svNLC8yK7EIZv1 +B9t+9BZI6bjeUYObyxkPQh0N5AQi1ejyUpSKTiW5TBSMiAkFSVQ/8CwlI42fmN0gL2A rmtLcnxDeDQmHK7VUpzKqJXz4WWR5f2VsTKXDYYlajYuVZD5fqpO8dGjq85JwPKgR+16 CPfg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=IaQv6IYiHqQsQRwxagN42nrCoy2L36X/io0NIlSKpEE=; fh=VuulZcEO6lpru37KuN/K+Cvym9c/awS/vEYu318i5yc=; b=BiuFmI/fPufb44nqZeBM1Y7sKewBwxTOtlwEIFRIy96jh4Ol+/AMHPjNIIPimuRV58 ahVDcvPqz7qGN73gi7jNiju9pfi3u5xL9d7LDMXdZKkGN8ZEke0gQEIj2BGLlgoGRRgb p4dyVbom110/eawpHueDZ8wZBU0xHs9f/6sm34SIgLGd0yp4GKTyDG9Lz0+1ZNEbqWFK CAQRIj30Onhia2e26qwYf/ND77n+8jcWCAj+V6NGyBfbCFPvHb2XYtCLKIebpMsQHcSe KZaItpUjBNzp22ymbO5eCh+5Xq5tMSG4VXQJoSCd4t5uhJjsVgYz1OahbZyqEDaif5sX S0SA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YaN0kVu6; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-75630-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75630-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id dz12-20020a0564021d4c00b0056442cf5b81si4014034edb.522.2024.02.21.15.05.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 15:05:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-75630-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YaN0kVu6; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-75630-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-75630-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.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 am.mirrors.kernel.org (Postfix) with ESMTPS id 166791F23445 for ; Wed, 21 Feb 2024 23:05:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DC7D01292F2; Wed, 21 Feb 2024 23:05:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="YaN0kVu6" Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 4BAAB86632 for ; Wed, 21 Feb 2024 23:05:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708556736; cv=none; b=jrTYJsunJ/JY5jQIVHHfRsZBt/ElvqhoWm6C22p88/sWZl61i/UqAyt12xekVgGkmXrugCuao+jk/LC+QUxt3F9VZMyVYH5SWeZjv72a58h0MkiFmmmvuxUTLN1y8HjF5ypILUk9Q4b5eHoi9sidDJSqbVAIADdi641+yXa3SXg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708556736; c=relaxed/simple; bh=zssxqx37knagHcWIMKikX95cYC9xskvO7oxvfidP8XA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=GqiHcwCExBcc4806C3oscm3wHo1MlUlmUW/sZDVv4YSd6167ilkykBmO9Ad4umMk/Po/oB/lmVyCNOHq0nf16y/dmFfYlEgCCjEJahQ9zAhAYZYBJiX2iuopGDGT1SsBqKCRHf+IFs1ekcpZ7F3unnW7r4k/qh4oGYGZjTyfE3M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=YaN0kVu6; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1dc49b00bdbso1423145ad.3 for ; Wed, 21 Feb 2024 15:05:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1708556733; x=1709161533; darn=vger.kernel.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=IaQv6IYiHqQsQRwxagN42nrCoy2L36X/io0NIlSKpEE=; b=YaN0kVu68tjoY2VWfrn+E6uvbAVTphdtX2sn1L28iRKphMDLBI9svC4bsq2VmT/eR8 rwRvkCCtNjWgHofEkG8E7Fa9EScJZEtSHQyWJ/LpBDz2lqo6oxVVa3zIO1ynxdFuk3sE 2H4E1XYMIw5Mwkk//ZqWOw1PorI89LsVlxO/A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708556733; x=1709161533; 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=IaQv6IYiHqQsQRwxagN42nrCoy2L36X/io0NIlSKpEE=; b=nnqDKx4KAafVE1uLgrw9jXuSf10Ss+EzmInm3hs+qYsNZq38O+vsDA0wZt0kG0+aRJ 4A6GDB7S192+SY0+/9WWQ6pi1i5WkWofmr/baBkgMmHzTNd0kWKPS4Pp/qJg7RrsSSYS xaenKjVz6kl8NkEJUX2Ke+t1W88LesMe8PZJbU08HBOrgnaI+1db4uDZjgVy4GDhhtNr IgrPGKouVzpbOdhSQPRxgdR7+b8RIP1ITJLNA3jd6h23wL5Z7Sp1ZKGNxQGxGlMgYrfe aGa9SUjGg+G3ocSwVfVOrZwd/Zc3wZ5fZ+vV3loiPUQGPANCKBpPIcMgN96EaTB0nJ0Y uueQ== X-Forwarded-Encrypted: i=1; AJvYcCWu2/znwhRhmO9FzPp7hjwpwU7/+SoSPz0GrTI7zaqf6TS5rvcdwRNws5dKARDGaMGF54eLjWAdZdM8vlis5vIEz9LtcFpOZMLoHi8b X-Gm-Message-State: AOJu0YwwDXtNgRSWtl+O2iTd2rkSZExdjKWnL3ohiZsuu4vQLbcgDhIe i02Qo5QNcuZNWZaWCxG7FVc2p32M0sqpCM28CC1AJFaBmo7kr5NkfqOswDmF9w== X-Received: by 2002:a17:902:c086:b0:1dc:7b6:867a with SMTP id j6-20020a170902c08600b001dc07b6867amr6026373pld.21.1708556733655; Wed, 21 Feb 2024 15:05:33 -0800 (PST) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id h18-20020a170902f2d200b001d913992d8csm8631586plc.242.2024.02.21.15.05.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Feb 2024 15:05:33 -0800 (PST) Date: Wed, 21 Feb 2024 15:05:32 -0800 From: Kees Cook To: Suren Baghdasaryan Cc: akpm@linux-foundation.org, kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, ndesaulniers@google.com, vvvvvv@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org Subject: Re: [PATCH v4 14/36] lib: add allocation tagging support for memory allocation profiling Message-ID: <202402211449.401382D2AF@keescook> References: <20240221194052.927623-1-surenb@google.com> <20240221194052.927623-15-surenb@google.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: <20240221194052.927623-15-surenb@google.com> On Wed, Feb 21, 2024 at 11:40:27AM -0800, Suren Baghdasaryan wrote: > [...] > +struct alloc_tag { > + struct codetag ct; > + struct alloc_tag_counters __percpu *counters; > +} __aligned(8); > [...] > +#define DEFINE_ALLOC_TAG(_alloc_tag) \ > + static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ > + static struct alloc_tag _alloc_tag __used __aligned(8) \ > + __section("alloc_tags") = { \ > + .ct = CODE_TAG_INIT, \ > + .counters = &_alloc_tag_cntr }; > [...] > +static inline struct alloc_tag *alloc_tag_save(struct alloc_tag *tag) > +{ > + swap(current->alloc_tag, tag); > + return tag; > +} Future security hardening improvement idea based on this infrastructure: it should be possible to implement per-allocation-site kmem caches. For example, we could create: struct alloc_details { u32 flags; union { u32 size; /* not valid after __init completes */ struct kmem_cache *cache; }; }; - add struct alloc_details to struct alloc_tag - move the tags section into .ro_after_init - extend alloc_hooks() to populate flags and size: .flags = __builtin_constant_p(size) ? KMALLOC_ALLOCATE_FIXED : KMALLOC_ALLOCATE_BUCKETS; .size = __builtin_constant_p(size) ? size : SIZE_MAX; - during kernel start or module init, walk the alloc_tag list and create either a fixed-size kmem_cache or to allocate a full set of kmalloc-buckets, and update the "cache" member. - adjust kmalloc core routines to use current->alloc_tag->cache instead of using the global buckets. This would get us fully separated allocations, producing better than type-based levels of granularity, exceeding what we have currently with CONFIG_RANDOM_KMALLOC_CACHES. Does this look possible, or am I misunderstanding something in the infrastructure being created here? -- Kees Cook