From patchwork Mon Jul 2 18:11:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 140850 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp249758ljj; Mon, 2 Jul 2018 11:14:58 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfqI+Mzs9Gc9u4yD3TB0hmpCi1VCXXoIBlKpA4w51UB/F5whWI4ovCu0YT6My2h1u8ZYTDV X-Received: by 2002:a62:170c:: with SMTP id 12-v6mr26419584pfx.139.1530555297958; Mon, 02 Jul 2018 11:14:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530555297; cv=none; d=google.com; s=arc-20160816; b=dnQQ3T5DEGVGsrq9Xr30v78hIemRn7x24y/RI3aeK8Dcx8s/zZqKPTh9pXocu9jG/u olBt/rx77pEk/udM3rT1VUshyZ4ZBsekcr1dsIHLN6PUqfGYpXVPvJSCPm86+MENQ3nF LDoYEcVJ4RuO+EUn2GCJf4e/C4KksjaYfbsQkbvxDIsXesVRWd8POZO/0F+/K+JiZYNO BDnTsaBabVo8lO+mlx4iKIcUlWC+aouO1ioqtbMo6qJVvV6taR3kSHRK9gUoPRAGQQdA yJzo1OfyFbjDmf10q6PjrkC1W8k4aG8KuHPD3LGHe0umrZoNOTNjhVBW6QsHG9LTEv4n ZXNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=bYIEjgYsFCspjdyZn23Ek0/9Xld2X4/AQrtzD7Bk1Ig=; b=XlYU7t7rNAQ/h9rm9nsl6UkTRjzzT0bAOVNxkDhqAoVC27L71tYQt0gTAP0tHs5XPJ QTXRNYsZ7xqEO9XYHAHs8U6vVp5ZJbHhhvnliGKXbOEXs2p0dMZxWlZRYYTiv/NqazwR 8jX+sURNDb+tlVC27HEFH3pj2IEjieHDWafTQ9noyv9kcIZyDz4VVby0U6vVKT8lRtCa CVckDNhROO4RX8LFlahm2elYJFI1hs8oh7IDfLJqK6vZNJNZZ2w9+ZOQmSZKBlUKowir sqWPVPZoJfQF71NtXwSeX7ZUJqxy5UWQTZ6IP9PdGJrn2sQpwndfky9L6HSLLLfutfww KcZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FkyoYASw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 6-v6si16787191plb.409.2018.07.02.11.14.57; Mon, 02 Jul 2018 11:14:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FkyoYASw; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753355AbeGBSOz (ORCPT + 31 others); Mon, 2 Jul 2018 14:14:55 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:35747 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753328AbeGBSLz (ORCPT ); Mon, 2 Jul 2018 14:11:55 -0400 Received: by mail-wr0-f193.google.com with SMTP id h40-v6so5338872wrh.2 for ; Mon, 02 Jul 2018 11:11:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bYIEjgYsFCspjdyZn23Ek0/9Xld2X4/AQrtzD7Bk1Ig=; b=FkyoYASwtLuwL6Ys+RF95vTBeo00E/h6AYLejkixx4YIeQU0cTYq08FQDO/RhDyvl0 pjVysDdtmPuDQb5zTxUNpxwi9jKuDKYg4aUvAGVXc5v5b5+ozJKscnU+swtD2RuB2VXr MfR7HAW5r8vkudT3oYUv0kfmrzgEd+c5uvu1w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bYIEjgYsFCspjdyZn23Ek0/9Xld2X4/AQrtzD7Bk1Ig=; b=X5kZDyAdnlBBQFkv5LDd0wF3zKlroEVAxw1iyyxKg3CQzgGPQXAj34G1yyUzBKX1xs 2YETIgjS0MwS/5/SbXWhkzvbBzbd3hKqANiM+GTDpWvgI43F2BRx44887huYWtf+fRc0 rrJOVGJ7TLZw1PfptJQxq3ONTFsFTx7/n1tAvelWYxxzIPiTn5bRDF2cvILnE6eqD84I hgWXLjLW7GTcx6Wfd2XWViY59bcAEb1tizgOupcKWtxGKr2b7t3czT7LJAO882XWmicZ AcNkzoJrR7Cvq3a87jrRp0VVwbECWZSh8WlqKm7c6bGdvOQ7P7mzuJyIvD/g5YY//yki wZkA== X-Gm-Message-State: APt69E0Q6gy3blRGAyB3sGCBjn7RSSVtcU7AkV1KNX3LECvgGy8JX5iY winHotm1KZlEcaT6jEE49TjtWw== X-Received: by 2002:adf:de84:: with SMTP id w4-v6mr8884955wrl.270.1530555114143; Mon, 02 Jul 2018 11:11:54 -0700 (PDT) Received: from localhost.localdomain (151.21.90.92.rev.sfr.net. [92.90.21.151]) by smtp.gmail.com with ESMTPSA id 189-v6sm10582822wmd.17.2018.07.02.11.11.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Jul 2018 11:11:53 -0700 (PDT) From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, linux-arch@vger.kernel.org Cc: Ard Biesheuvel , Arnd Bergmann , Heiko Carstens , Kees Cook , Will Deacon , Thomas Gleixner , Catalin Marinas , Ingo Molnar , Steven Rostedt , Martin Schwidefsky , Jessica Yu , Peter Zijlstra Subject: [PATCH v2 1/8] kernel/jump_label: abstract jump_entry member accessors Date: Mon, 2 Jul 2018 20:11:38 +0200 Message-Id: <20180702181145.4799-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180702181145.4799-1-ard.biesheuvel@linaro.org> References: <20180702181145.4799-1-ard.biesheuvel@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation of allowing architectures to use relative references in jump_label entries [which can dramatically reduce the memory footprint], introduce abstractions for references to the 'code', 'target' and 'key' members of struct jump_entry. Signed-off-by: Ard Biesheuvel --- include/linux/jump_label.h | 34 +++++++++++++++++ kernel/jump_label.c | 40 ++++++++------------ 2 files changed, 49 insertions(+), 25 deletions(-) -- 2.17.1 diff --git a/include/linux/jump_label.h b/include/linux/jump_label.h index b46b541c67c4..4603a1c88e48 100644 --- a/include/linux/jump_label.h +++ b/include/linux/jump_label.h @@ -119,6 +119,40 @@ struct static_key { #ifdef HAVE_JUMP_LABEL #include + +#ifndef __ASSEMBLY__ + +static inline unsigned long jump_entry_code(const struct jump_entry *entry) +{ + return entry->code; +} + +static inline unsigned long jump_entry_target(const struct jump_entry *entry) +{ + return entry->target; +} + +static inline struct static_key *jump_entry_key(const struct jump_entry *entry) +{ + return (struct static_key *)((unsigned long)entry->key & ~1UL); +} + +static inline bool jump_entry_is_branch(const struct jump_entry *entry) +{ + return (unsigned long)entry->key & 1UL; +} + +static inline bool jump_entry_is_init(const struct jump_entry *entry) +{ + return entry->code == 0; +} + +static inline void jump_entry_set_init(struct jump_entry *entry) +{ + entry->code = 0; +} + +#endif #endif #ifndef __ASSEMBLY__ diff --git a/kernel/jump_label.c b/kernel/jump_label.c index 01ebdf1f9f40..8a3ac4f5f490 100644 --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -38,10 +38,10 @@ static int jump_label_cmp(const void *a, const void *b) const struct jump_entry *jea = a; const struct jump_entry *jeb = b; - if (jea->key < jeb->key) + if (jump_entry_key(jea) < jump_entry_key(jeb)) return -1; - if (jea->key > jeb->key) + if (jump_entry_key(jea) > jump_entry_key(jeb)) return 1; return 0; @@ -261,8 +261,8 @@ EXPORT_SYMBOL_GPL(jump_label_rate_limit); static int addr_conflict(struct jump_entry *entry, void *start, void *end) { - if (entry->code <= (unsigned long)end && - entry->code + JUMP_LABEL_NOP_SIZE > (unsigned long)start) + if (jump_entry_code(entry) <= (unsigned long)end && + jump_entry_code(entry) + JUMP_LABEL_NOP_SIZE > (unsigned long)start) return 1; return 0; @@ -321,16 +321,6 @@ static inline void static_key_set_linked(struct static_key *key) key->type |= JUMP_TYPE_LINKED; } -static inline struct static_key *jump_entry_key(struct jump_entry *entry) -{ - return (struct static_key *)((unsigned long)entry->key & ~1UL); -} - -static bool jump_entry_branch(struct jump_entry *entry) -{ - return (unsigned long)entry->key & 1UL; -} - /*** * A 'struct static_key' uses a union such that it either points directly * to a table of 'struct jump_entry' or to a linked list of modules which in @@ -355,7 +345,7 @@ static enum jump_label_type jump_label_type(struct jump_entry *entry) { struct static_key *key = jump_entry_key(entry); bool enabled = static_key_enabled(key); - bool branch = jump_entry_branch(entry); + bool branch = jump_entry_is_branch(entry); /* See the comment in linux/jump_label.h */ return enabled ^ branch; @@ -370,12 +360,12 @@ static void __jump_label_update(struct static_key *key, * An entry->code of 0 indicates an entry which has been * disabled because it was in an init text area. */ - if (entry->code) { - if (kernel_text_address(entry->code)) + if (!jump_entry_is_init(entry)) { + if (kernel_text_address(jump_entry_code(entry))) arch_jump_label_transform(entry, jump_label_type(entry)); else WARN_ONCE(1, "can't patch jump_label at %pS", - (void *)(unsigned long)entry->code); + (void *)jump_entry_code(entry)); } } } @@ -430,8 +420,8 @@ void __init jump_label_invalidate_initmem(void) struct jump_entry *iter; for (iter = iter_start; iter < iter_stop; iter++) { - if (init_section_contains((void *)(unsigned long)iter->code, 1)) - iter->code = 0; + if (init_section_contains((void *)jump_entry_code(iter), 1)) + jump_entry_set_init(iter); } } @@ -441,7 +431,7 @@ static enum jump_label_type jump_label_init_type(struct jump_entry *entry) { struct static_key *key = jump_entry_key(entry); bool type = static_key_type(key); - bool branch = jump_entry_branch(entry); + bool branch = jump_entry_is_branch(entry); /* See the comment in linux/jump_label.h */ return type ^ branch; @@ -565,7 +555,7 @@ static int jump_label_add_module(struct module *mod) continue; key = iterk; - if (within_module(iter->key, mod)) { + if (within_module((unsigned long)key, mod)) { static_key_set_entries(key, iter); continue; } @@ -615,7 +605,7 @@ static void jump_label_del_module(struct module *mod) key = jump_entry_key(iter); - if (within_module(iter->key, mod)) + if (within_module((unsigned long)key, mod)) continue; /* No memory during module load */ @@ -659,8 +649,8 @@ static void jump_label_invalidate_module_init(struct module *mod) struct jump_entry *iter; for (iter = iter_start; iter < iter_stop; iter++) { - if (within_module_init(iter->code, mod)) - iter->code = 0; + if (within_module_init(jump_entry_code(iter), mod)) + jump_entry_set_init(iter); } }