From: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
To: linux-kernel@vger.kernel.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
Peter Oberparleiter <oberparleiter@googlemail.com>
Subject: [PATCH] consolidate all within() implementations
Date: Mon, 19 May 2008 10:45:23 +0200 [thread overview]
Message-ID: <48313E23.4030104@de.ibm.com> (raw)
From: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
This patch consolidates a number of different implementations of the
within() function which checks whether an address is within a specified
address range. Apart from parameter typing, existing implementations can
be classified in two categories which differ in the way the range is
specified:
1) by start and end address
2) by start and size
These categories are covered by the within() macro (case 1) and the
within_len() macro (case 2). Both macros can be used with any pointer
or pointer-equivalent type as parameter.
Signed-off-by: Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
---
arch/x86/mm/pageattr.c | 7 +------
include/linux/kernel.h | 24 ++++++++++++++++++++++++
kernel/lockdep.c | 10 +++-------
kernel/module.c | 25 ++++++++++---------------
4 files changed, 38 insertions(+), 28 deletions(-)
Index: linux-2.6.26-rc3/include/linux/kernel.h
===================================================================
--- linux-2.6.26-rc3.orig/include/linux/kernel.h
+++ linux-2.6.26-rc3/include/linux/kernel.h
@@ -434,6 +434,30 @@ static inline char *pack_hex_byte(char *
__val > __max ? __max: __val; })
/**
+ * within - check whether address is within a start-and-end address range
+ * @val: address
+ * @start: start address (included in range)
+ * @end: end address (excluded from range)
+ */
+#define within(val, start, end) ({ \
+ unsigned long __val = (unsigned long) (val); \
+ unsigned long __start = (unsigned long) (start); \
+ unsigned long __end = (unsigned long) (end); \
+ (__val >= __start) && (__val < __end); })
+
+/**
+ * within_len - check whether address is within a start-and-length address range
+ * @val: address
+ * @start: start of range
+ * @len: number of bytes in range
+ */
+#define within_len(val, start, len) ({ \
+ unsigned long __val = (unsigned long) (val); \
+ unsigned long __start = (unsigned long) (start); \
+ unsigned long __end = __start + (unsigned long) (len); \
+ (__val >= __start) && (__val < __end); })
+
+/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
Index: linux-2.6.26-rc3/kernel/lockdep.c
===================================================================
--- linux-2.6.26-rc3.orig/kernel/lockdep.c
+++ linux-2.6.26-rc3/kernel/lockdep.c
@@ -25,6 +25,7 @@
* Thanks to Arjan van de Ven for coming up with the initial idea of
* mapping lock dependencies runtime.
*/
+#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/delay.h>
@@ -2932,11 +2933,6 @@ static void zap_class(struct lock_class
}
-static inline int within(const void *addr, void *start, unsigned long size)
-{
- return addr >= start && addr < start + size;
-}
-
void lockdep_free_key_range(void *start, unsigned long size)
{
struct lock_class *class, *next;
@@ -2956,9 +2952,9 @@ void lockdep_free_key_range(void *start,
if (list_empty(head))
continue;
list_for_each_entry_safe(class, next, head, hash_entry) {
- if (within(class->key, start, size))
+ if (within_len(class->key, start, size))
zap_class(class);
- else if (within(class->name, start, size))
+ else if (within_len(class->name, start, size))
zap_class(class);
}
}
Index: linux-2.6.26-rc3/kernel/module.c
===================================================================
--- linux-2.6.26-rc3.orig/kernel/module.c
+++ linux-2.6.26-rc3/kernel/module.c
@@ -2262,11 +2262,6 @@ sys_init_module(void __user *umod,
return 0;
}
-static inline int within(unsigned long addr, void *start, unsigned long size)
-{
- return ((void *)addr >= start && (void *)addr < start + size);
-}
-
#ifdef CONFIG_KALLSYMS
/*
* This ignores the intensely annoying "mapping symbols" found
@@ -2287,7 +2282,7 @@ static const char *get_ksymbol(struct mo
unsigned long nextval;
/* At worse, next value is at end of module */
- if (within(addr, mod->module_init, mod->init_size))
+ if (within_len(addr, mod->module_init, mod->init_size))
nextval = (unsigned long)mod->module_init+mod->init_text_size;
else
nextval = (unsigned long)mod->module_core+mod->core_text_size;
@@ -2335,8 +2330,8 @@ const char *module_address_lookup(unsign
preempt_disable();
list_for_each_entry(mod, &modules, list) {
- if (within(addr, mod->module_init, mod->init_size)
- || within(addr, mod->module_core, mod->core_size)) {
+ if (within_len(addr, mod->module_init, mod->init_size)
+ || within_len(addr, mod->module_core, mod->core_size)) {
if (modname)
*modname = mod->name;
ret = get_ksymbol(mod, addr, size, offset);
@@ -2358,8 +2353,8 @@ int lookup_module_symbol_name(unsigned l
preempt_disable();
list_for_each_entry(mod, &modules, list) {
- if (within(addr, mod->module_init, mod->init_size) ||
- within(addr, mod->module_core, mod->core_size)) {
+ if (within_len(addr, mod->module_init, mod->init_size) ||
+ within_len(addr, mod->module_core, mod->core_size)) {
const char *sym;
sym = get_ksymbol(mod, addr, NULL, NULL);
@@ -2382,8 +2377,8 @@ int lookup_module_symbol_attrs(unsigned
preempt_disable();
list_for_each_entry(mod, &modules, list) {
- if (within(addr, mod->module_init, mod->init_size) ||
- within(addr, mod->module_core, mod->core_size)) {
+ if (within_len(addr, mod->module_init, mod->init_size) ||
+ within_len(addr, mod->module_core, mod->core_size)) {
const char *sym;
sym = get_ksymbol(mod, addr, size, offset);
@@ -2579,7 +2574,7 @@ int is_module_address(unsigned long addr
preempt_disable();
list_for_each_entry(mod, &modules, list) {
- if (within(addr, mod->module_core, mod->core_size)) {
+ if (within_len(addr, mod->module_core, mod->core_size)) {
preempt_enable();
return 1;
}
@@ -2597,8 +2592,8 @@ struct module *__module_text_address(uns
struct module *mod;
list_for_each_entry(mod, &modules, list)
- if (within(addr, mod->module_init, mod->init_text_size)
- || within(addr, mod->module_core, mod->core_text_size))
+ if (within_len(addr, mod->module_init, mod->init_text_size)
+ || within_len(addr, mod->module_core, mod->core_text_size))
return mod;
return NULL;
}
Index: linux-2.6.26-rc3/arch/x86/mm/pageattr.c
===================================================================
--- linux-2.6.26-rc3.orig/arch/x86/mm/pageattr.c
+++ linux-2.6.26-rc3/arch/x86/mm/pageattr.c
@@ -2,6 +2,7 @@
* Copyright 2002 Andi Kleen, SuSE Labs.
* Thanks to Ben LaHaise for precious feedback.
*/
+#include <linux/kernel.h>
#include <linux/highmem.h>
#include <linux/bootmem.h>
#include <linux/module.h>
@@ -54,12 +55,6 @@ static inline unsigned long highmap_end_
# define debug_pagealloc 0
#endif
-static inline int
-within(unsigned long addr, unsigned long start, unsigned long end)
-{
- return addr >= start && addr < end;
-}
-
/*
* Flushing functions
*/
next reply other threads:[~2008-05-19 8:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-19 8:45 Peter Oberparleiter [this message]
2008-05-19 20:50 ` [PATCH] consolidate all within() implementations Harvey Harrison
2008-05-20 8:08 ` Peter Oberparleiter
2008-05-20 9:45 ` Andrew Morton
2008-05-20 15:42 ` Peter Oberparleiter
2008-05-21 10:04 ` Peter Zijlstra
2008-05-21 10:33 ` Peter 1 Oberparleiter
2008-05-21 10:48 ` Peter Zijlstra
2008-05-21 13:50 ` Peter 1 Oberparleiter
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=48313E23.4030104@de.ibm.com \
--to=peter.oberparleiter@de.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=oberparleiter@googlemail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.