linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Will Deacon <will@kernel.org>
To: linux-kernel@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org,
	Will Deacon <will@kernel.org>,
	Sami Tolvanen <samitolvanen@google.com>,
	Kees Cook <keescook@chromium.org>,
	Catalin Marinas <catalin.marinas@arm.com>,
	Mark Rutland <mark.rutland@am.com>, Jann Horn <jannh@google.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	kernel-team@android.com
Subject: [PATCH 2/6] scs: Move accounting into alloc/free functions
Date: Fri, 15 May 2020 18:27:52 +0100	[thread overview]
Message-ID: <20200515172756.27185-3-will@kernel.org> (raw)
In-Reply-To: <20200515172756.27185-1-will@kernel.org>

There's no need to perform the shadow stack page accounting independently
of the lifetime of the underlying allocation, so call the accounting code
from the {alloc,free}() functions and simplify the code in the process.

Signed-off-by: Will Deacon <will@kernel.org>
---
 kernel/scs.c | 45 +++++++++++++++++++++------------------------
 1 file changed, 21 insertions(+), 24 deletions(-)

diff --git a/kernel/scs.c b/kernel/scs.c
index 5ff8663e4a67..aea841cd7586 100644
--- a/kernel/scs.c
+++ b/kernel/scs.c
@@ -14,25 +14,35 @@
 
 static struct kmem_cache *scs_cache;
 
+static void __scs_account(void *s, int account)
+{
+	struct page *scs_page = virt_to_page(s);
+
+	mod_zone_page_state(page_zone(scs_page), NR_KERNEL_SCS_KB,
+			    account * (SCS_SIZE / SZ_1K));
+}
+
 static void *scs_alloc(int node)
 {
-	void *s;
-
-	s = kmem_cache_alloc_node(scs_cache, GFP_SCS, node);
-	if (s) {
-		*__scs_magic(s) = SCS_END_MAGIC;
-		/*
-		 * Poison the allocation to catch unintentional accesses to
-		 * the shadow stack when KASAN is enabled.
-		 */
-		kasan_poison_object_data(scs_cache, s);
-	}
+	void *s = kmem_cache_alloc_node(scs_cache, GFP_SCS, node);
+
+	if (!s)
+		return NULL;
 
+	*__scs_magic(s) = SCS_END_MAGIC;
+
+	/*
+	 * Poison the allocation to catch unintentional accesses to
+	 * the shadow stack when KASAN is enabled.
+	 */
+	kasan_poison_object_data(scs_cache, s);
+	__scs_account(s, 1);
 	return s;
 }
 
 static void scs_free(void *s)
 {
+	__scs_account(s, -1);
 	kasan_unpoison_object_data(scs_cache, s);
 	kmem_cache_free(scs_cache, s);
 }
@@ -42,17 +52,6 @@ void __init scs_init(void)
 	scs_cache = kmem_cache_create("scs_cache", SCS_SIZE, 0, 0, NULL);
 }
 
-static struct page *__scs_page(struct task_struct *tsk)
-{
-	return virt_to_page(task_scs(tsk));
-}
-
-static void scs_account(struct task_struct *tsk, int account)
-{
-	mod_zone_page_state(page_zone(__scs_page(tsk)), NR_KERNEL_SCS_KB,
-		account * (SCS_SIZE / 1024));
-}
-
 int scs_prepare(struct task_struct *tsk, int node)
 {
 	void *s = scs_alloc(node);
@@ -61,7 +60,6 @@ int scs_prepare(struct task_struct *tsk, int node)
 		return -ENOMEM;
 
 	task_scs(tsk) = task_scs_sp(tsk) = s;
-	scs_account(tsk, 1);
 	return 0;
 }
 
@@ -102,6 +100,5 @@ void scs_release(struct task_struct *tsk)
 
 	WARN(scs_corrupted(tsk), "corrupted shadow stack detected when freeing task\n");
 	scs_check_usage(tsk);
-	scs_account(tsk, -1);
 	scs_free(s);
 }
-- 
2.26.2.761.g0e0b3e54be-goog


  parent reply	other threads:[~2020-05-15 17:28 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-15 17:27 [PATCH 0/6] Clean up Shadow Call Stack patches for 5.8 Will Deacon
2020-05-15 17:27 ` [PATCH 1/6] arm64: scs: Store absolute SCS stack pointer value in thread_info Will Deacon
2020-05-18 11:37   ` Mark Rutland
2020-05-18 13:37     ` Will Deacon
2020-05-15 17:27 ` Will Deacon [this message]
2020-05-18 11:38   ` [PATCH 2/6] scs: Move accounting into alloc/free functions Mark Rutland
2020-05-18 13:39     ` Will Deacon
2020-05-15 17:27 ` [PATCH 3/6] arm64: scs: Use 'scs_sp' register alias for x18 Will Deacon
2020-05-18 11:55   ` Mark Rutland
2020-05-18 13:03     ` Will Deacon
2020-05-18 13:13       ` Mark Rutland
2020-05-15 17:27 ` [PATCH 4/6] scs: Move scs_overflow_check() out of architecture code Will Deacon
2020-05-18 12:12   ` Mark Rutland
2020-05-18 13:23     ` Will Deacon
2020-05-18 13:32       ` Mark Rutland
2020-05-18 15:31         ` Kees Cook
2020-05-18 16:44           ` Will Deacon
2020-05-15 17:27 ` [PATCH 5/6] scs: Remove references to asm/scs.h from core code Will Deacon
2020-05-18 12:15   ` Mark Rutland
2020-05-15 17:27 ` [PATCH 6/6] scs: Move DEFINE_SCS macro into " Will Deacon
2020-05-18 12:14   ` Mark Rutland
2020-05-18 13:26     ` Will Deacon
2020-05-18 13:37       ` Mark Rutland
2020-05-15 20:42 ` [PATCH 0/6] Clean up Shadow Call Stack patches for 5.8 Sami Tolvanen
2020-05-18 13:52   ` Will Deacon
2020-05-18 15:43     ` Sami Tolvanen
2020-05-18 16:49       ` Will Deacon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200515172756.27185-3-will@kernel.org \
    --to=will@kernel.org \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=jannh@google.com \
    --cc=keescook@chromium.org \
    --cc=kernel-team@android.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@am.com \
    --cc=peterz@infradead.org \
    --cc=samitolvanen@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).