From patchwork Fri Feb 13 05:40:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 5823531 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CF864BF440 for ; Fri, 13 Feb 2015 06:07:19 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2DF972037B for ; Fri, 13 Feb 2015 06:07:15 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 98DAF20379 for ; Fri, 13 Feb 2015 06:07:07 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YM9Mc-00035Q-9K; Fri, 13 Feb 2015 06:04:30 +0000 Received: from szxga01-in.huawei.com ([119.145.14.64]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YM9Ay-0000Wk-49 for linux-arm-kernel@lists.infradead.org; Fri, 13 Feb 2015 05:52:48 +0000 Received: from 172.24.2.119 (EHLO lggeml426-hub.china.huawei.com) ([172.24.2.119]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CJM14214; Fri, 13 Feb 2015 13:47:45 +0800 (CST) Received: from kernel-host.huawei (10.107.197.247) by lggeml426-hub.china.huawei.com (10.72.61.36) with Microsoft SMTP Server id 14.3.158.1; Fri, 13 Feb 2015 13:47:38 +0800 From: Wang Nan To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH v3 17/26] early kprobes: introduces macros for allocing early kprobe resources. Date: Fri, 13 Feb 2015 13:40:58 +0800 Message-ID: <1423806058-62193-1-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.4 In-Reply-To: <1423805941-61407-1-git-send-email-wangnan0@huawei.com> References: <1423805941-61407-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.197.247] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150212_215228_532231_37D40C68 X-CRM114-Status: UNSURE ( 7.20 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.7 (/) Cc: lizefan@huawei.com, x86@kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Introduces macros to genearte common early kprobe related resource allocator. All early kprobe related resources are statically allocated during linking for each early kprobe slot. For each type of resource, a bitmap is used to track allocation. __DEFINE_EKPROBE_ALLOC_OPS defines alloc and free handler for them. The range of the resource and the bitmap should be provided for allocaing and freeing. DEFINE_EKPROBE_ALLOC_OPS defines bitmap and the array used by it. Signed-off-by: Wang Nan --- include/linux/kprobes.h | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/include/linux/kprobes.h b/include/linux/kprobes.h index 8d2e754..cd7a2a5 100644 --- a/include/linux/kprobes.h +++ b/include/linux/kprobes.h @@ -270,6 +270,84 @@ extern void show_registers(struct pt_regs *regs); extern void kprobes_inc_nmissed_count(struct kprobe *p); extern bool arch_within_kprobe_blacklist(unsigned long addr); +#ifdef CONFIG_EARLY_KPROBES + +#define NR_EARLY_KPROBES_SLOTS CONFIG_NR_EARLY_KPROBES_SLOTS +#define ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1)) +#define EARLY_KPROBES_BITMAP_SZ ALIGN_UP(NR_EARLY_KPROBES_SLOTS, BITS_PER_LONG) + +#define __ek_in_range(v, s, e) (((v) >= (s)) && ((v) < (e))) +#define __ek_buf_sz(s, e) ((void *)(e) - (void *)(s)) +#define __ek_elem_sz_b(s, e) (__ek_buf_sz(s, e) / NR_EARLY_KPROBES_SLOTS) +#define __ek_elem_sz(s, e) (__ek_elem_sz_b(s, e) / sizeof(s[0])) +#define __ek_elem_idx(v, s, e) (__ek_buf_sz(s, v) / __ek_elem_sz_b(s, e)) +#define __ek_get_elem(i, s, e) (&((s)[__ek_elem_sz(s, e) * (i)])) +#define __DEFINE_EKPROBE_ALLOC_OPS(__t, __name) \ +static inline __t *__ek_alloc_##__name(__t *__s, __t *__e, unsigned long *__b)\ +{ \ + int __i = find_next_zero_bit(__b, NR_EARLY_KPROBES_SLOTS, 0); \ + if (__i >= NR_EARLY_KPROBES_SLOTS) \ + return NULL; \ + set_bit(__i, __b); \ + return __ek_get_elem(__i, __s, __e); \ +} \ +static inline int __ek_free_##__name(__t *__v, __t *__s, __t *__e, unsigned long *__b) \ +{ \ + if (!__ek_in_range(__v, __s, __e)) \ + return 0; \ + clear_bit(__ek_elem_idx(__v, __s, __e), __b); \ + return 1; \ +} + +#define __DEFINE_EKPROBE_AREA(__t, __name, __static) \ +__static __t __ek_##__name##_slots[NR_EARLY_KPROBES_SLOTS]; \ +__static unsigned long __ek_##__name##_bitmap[EARLY_KPROBES_BITMAP_SZ]; + +#define DEFINE_EKPROBE_ALLOC_OPS(__t, __name, __static) \ +__DEFINE_EKPROBE_AREA(__t, __name, __static) \ +__DEFINE_EKPROBE_ALLOC_OPS(__t, __name) \ +static inline __t *ek_alloc_##__name(void) \ +{ \ + return __ek_alloc_##__name(&((__ek_##__name##_slots)[0]), \ + &((__ek_##__name##_slots)[NR_EARLY_KPROBES_SLOTS]),\ + (__ek_##__name##_bitmap)); \ +} \ +static inline int ek_free_##__name(__t *__s) \ +{ \ + return __ek_free_##__name(__s, &((__ek_##__name##_slots)[0]), \ + &((__ek_##__name##_slots)[NR_EARLY_KPROBES_SLOTS]),\ + (__ek_##__name##_bitmap)); \ +} + + +#else +#define __DEFINE_EKPROBE_ALLOC_OPS(__t, __name) \ +static inline __t *__ek_alloc_##__name(__t *__s, __t *__e, unsigned long *__b)\ +{ \ + return NULL; \ +} \ +static inline int __ek_free_##__name(__t *__v, __t *__s, __t *__e, unsigned long *__b)\ +{ \ + return 0; \ +} + +#define __DEFINE_EKPROBE_AREA(__t, __name, __static) \ +__static __t __ek_##__name##_slots[0]; \ +__static unsigned long __ek_##__name##_bitmap[0]; + +#define DEFINE_EKPROBE_ALLOC_OPS(__t, __name, __static) \ +__DEFINE_EKPROBE_ALLOC_OPS(__t, __name) \ +static inline __t *ek_alloc_##__name(void) \ +{ \ + return NULL; \ +} \ +static inline int ek_free_##__name(__t *__s) \ +{ \ + return 0; \ +} + +#endif + struct kprobe_insn_cache { struct mutex mutex; void *(*alloc)(void); /* allocate insn page */