Skip to contentSkip to conversation

mm/kasan: use {READ,WRITE}_MODE not true,false

PATCH In review seokhoon.yoon 2 participants 4 messages Jun 5, 2016 – Jun 6, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/kasan: use {READ,WRITE}_MODE not true,false

When Kasan tell memory access is write or not, use true or false. This expression is simple and convenient.

But I think it is possible to more readable. and so change it.

---
 mm/kasan/kasan.c  | 32 ++++++++++++++++----------------
 mm/kasan/kasan.h  | 12 ++++++++++--
 mm/kasan/report.c | 16 ++++++++--------
 3 files changed, 34 insertions(+), 26 deletions(-)
mm/kasan/kasan.c +16 -16
index 18b6a2b..642d936 100644
@@ -274,7 +274,7 @@ static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
 }
 
 static __always_inline void check_memory_region_inline(unsigned long addr,
-						size_t size, bool write,
+						size_t size, enum acc_type type,
 						unsigned long ret_ip)
 {
 	if (unlikely(size == 0))
@@ -282,39 +282,39 @@ static __always_inline void check_memory_region_inline(unsigned long addr,
 
 	if (unlikely((void *)addr <
 		kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
-		kasan_report(addr, size, write, ret_ip);
+		kasan_report(addr, size, type, ret_ip);
 		return;
 	}
 
 	if (likely(!memory_is_poisoned(addr, size)))
 		return;
 
-	kasan_report(addr, size, write, ret_ip);
+	kasan_report(addr, size, type, ret_ip);
 }
 
 static void check_memory_region(unsigned long addr,
-				size_t size, bool write,
+				size_t size, enum acc_type type,
 				unsigned long ret_ip)
 {
-	check_memory_region_inline(addr, size, write, ret_ip);
+	check_memory_region_inline(addr, size, type, ret_ip);
 }
 
 void kasan_check_read(const void *p, unsigned int size)
 {
-	check_memory_region((unsigned long)p, size, false, _RET_IP_);
+	check_memory_region((unsigned long)p, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(kasan_check_read);
 
 void kasan_check_write(const void *p, unsigned int size)
 {
-	check_memory_region((unsigned long)p, size, true, _RET_IP_);
+	check_memory_region((unsigned long)p, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(kasan_check_write);
 
 #undef memset
 void *memset(void *addr, int c, size_t len)
 {
-	check_memory_region((unsigned long)addr, len, true, _RET_IP_);
+	check_memory_region((unsigned long)addr, len, WRITE_MODE, _RET_IP_);
 
 	return __memset(addr, c, len);
 }
@@ -322,8 +322,8 @@ void *memset(void *addr, int c, size_t len)
 #undef memmove
 void *memmove(void *dest, const void *src, size_t len)
 {
-	check_memory_region((unsigned long)src, len, false, _RET_IP_);
-	check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+	check_memory_region((unsigned long)src, len, READ_MODE, _RET_IP_);
+	check_memory_region((unsigned long)dest, len, WRITE_MODE, _RET_IP_);
 
 	return __memmove(dest, src, len);
 }
@@ -331,8 +331,8 @@ void *memmove(void *dest, const void *src, size_t len)
 #undef memcpy
 void *memcpy(void *dest, const void *src, size_t len)
 {
-	check_memory_region((unsigned long)src, len, false, _RET_IP_);
-	check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+	check_memory_region((unsigned long)src, len, READ_MODE, _RET_IP_);
+	check_memory_region((unsigned long)dest, len, WRITE_MODE, _RET_IP_);
 
 	return __memcpy(dest, src, len);
 }
@@ -709,7 +709,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
 #define DEFINE_ASAN_LOAD_STORE(size)					\
 	void __asan_load##size(unsigned long addr)			\
 	{								\
-		check_memory_region_inline(addr, size, false, _RET_IP_);\
+		check_memory_region_inline(addr, size, READ_MODE, _RET_IP_);\
 	}								\
 	EXPORT_SYMBOL(__asan_load##size);				\
 	__alias(__asan_load##size)					\
@@ -717,7 +717,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
 	EXPORT_SYMBOL(__asan_load##size##_noabort);			\
 	void __asan_store##size(unsigned long addr)			\
 	{								\
-		check_memory_region_inline(addr, size, true, _RET_IP_);	\
+		check_memory_region_inline(addr, size, WRITE_MODE, _RET_IP_);\
 	}								\
 	EXPORT_SYMBOL(__asan_store##size);				\
 	__alias(__asan_store##size)					\
@@ -732,7 +732,7 @@ DEFINE_ASAN_LOAD_STORE(16);
 
 void __asan_loadN(unsigned long addr, size_t size)
 {
-	check_memory_region(addr, size, false, _RET_IP_);
+	check_memory_region(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_loadN);
 
@@ -742,7 +742,7 @@ EXPORT_SYMBOL(__asan_loadN_noabort);
 
 void __asan_storeN(unsigned long addr, size_t size)
 {
-	check_memory_region(addr, size, true, _RET_IP_);
+	check_memory_region(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_storeN);
 
mm/kasan/kasan.h +10 -2
index 7f7ac51..47cb58c 100644
@@ -27,11 +27,19 @@
 #define KASAN_ABI_VERSION 1
 #endif
 
+/*
+ * Distinguish memory access
+ */
+enum acc_type {
+	READ_MODE,
+	WRITE_MODE
+};
+
 struct kasan_access_info {
 	const void *access_addr;
 	const void *first_bad_addr;
 	size_t access_size;
-	bool is_write;
+	enum acc_type access_type;
 	unsigned long ip;
 };
 
@@ -109,7 +117,7 @@ static inline bool kasan_report_enabled(void)
 }
 
 void kasan_report(unsigned long addr, size_t size,
-		bool is_write, unsigned long ip);
+		enum acc_type type, unsigned long ip);
 
 #ifdef CONFIG_SLAB
 void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
mm/kasan/report.c +8 -8
index b3c122d..e0bee22 100644
@@ -96,7 +96,7 @@ static void print_error_description(struct kasan_access_info *info)
 		bug_type, (void *)info->ip,
 		info->access_addr);
 	pr_err("%s of size %zu by task %s/%d\n",
-		info->is_write ? "Write" : "Read",
+		info->access_type == WRITE_MODE ? "Write" : "Read",
 		info->access_size, current->comm, task_pid_nr(current));
 }
 
@@ -267,7 +267,7 @@ static void kasan_report_error(struct kasan_access_info *info)
 		pr_err("BUG: KASAN: %s on address %p\n",
 			bug_type, info->access_addr);
 		pr_err("%s of size %zu by task %s/%d\n",
-			info->is_write ? "Write" : "Read",
+			info->access_type == WRITE_MODE ? "Write" : "Read",
 			info->access_size, current->comm,
 			task_pid_nr(current));
 		dump_stack();
@@ -283,7 +283,7 @@ static void kasan_report_error(struct kasan_access_info *info)
 }
 
 void kasan_report(unsigned long addr, size_t size,
-		bool is_write, unsigned long ip)
+		enum acc_type type, unsigned long ip)
 {
 	struct kasan_access_info info;
 
@@ -292,7 +292,7 @@ void kasan_report(unsigned long addr, size_t size,
 
 	info.access_addr = (void *)addr;
 	info.access_size = size;
-	info.is_write = is_write;
+	info.access_type = type;
 	info.ip = ip;
 
 	kasan_report_error(&info);
@@ -302,14 +302,14 @@ void kasan_report(unsigned long addr, size_t size,
 #define DEFINE_ASAN_REPORT_LOAD(size)                     \
 void __asan_report_load##size##_noabort(unsigned long addr) \
 {                                                         \
-	kasan_report(addr, size, false, _RET_IP_);	  \
+	kasan_report(addr, size, READ_MODE, _RET_IP_);	  \
 }                                                         \
 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
 
 #define DEFINE_ASAN_REPORT_STORE(size)                     \
 void __asan_report_store##size##_noabort(unsigned long addr) \
 {                                                          \
-	kasan_report(addr, size, true, _RET_IP_);	   \
+	kasan_report(addr, size, WRITE_MODE, _RET_IP_);	   \
 }                                                          \
 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
 
@@ -326,12 +326,12 @@ DEFINE_ASAN_REPORT_STORE(16);
 
 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
 {
-	kasan_report(addr, size, false, _RET_IP_);
+	kasan_report(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_load_n_noabort);
 
 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
 {
-	kasan_report(addr, size, true, _RET_IP_);
+	kasan_report(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_store_n_noabort);
Signature · 7 lines
1.9.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
reviewer
#1 lore raw
Quoted 226 lines · seokhoon.yoon

When Kasan tell memory access is write or not, use true or false. This expression is simple and convenient.

But I think it is possible to more readable. and so change it.

---
 mm/kasan/kasan.c  | 32 ++++++++++++++++----------------
 mm/kasan/kasan.h  | 12 ++++++++++--
 mm/kasan/report.c | 16 ++++++++--------
 3 files changed, 34 insertions(+), 26 deletions(-)
mm/kasan/kasan.c +16 -16
index 18b6a2b..642d936 100644
@@ -274,7 +274,7 @@ static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
 }
 
 static __always_inline void check_memory_region_inline(unsigned long addr,
-                                               size_t size, bool write,
+                                               size_t size, enum acc_type type,
                                                unsigned long ret_ip)
 {
        if (unlikely(size == 0))
@@ -282,39 +282,39 @@ static __always_inline void check_memory_region_inline(unsigned long addr,
 
        if (unlikely((void *)addr <
                kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
-               kasan_report(addr, size, write, ret_ip);
+               kasan_report(addr, size, type, ret_ip);
                return;
        }
 
        if (likely(!memory_is_poisoned(addr, size)))
                return;
 
-       kasan_report(addr, size, write, ret_ip);
+       kasan_report(addr, size, type, ret_ip);
 }
 
 static void check_memory_region(unsigned long addr,
-                               size_t size, bool write,
+                               size_t size, enum acc_type type,
                                unsigned long ret_ip)
 {
-       check_memory_region_inline(addr, size, write, ret_ip);
+       check_memory_region_inline(addr, size, type, ret_ip);
 }
 
 void kasan_check_read(const void *p, unsigned int size)
 {
-       check_memory_region((unsigned long)p, size, false, _RET_IP_);
+       check_memory_region((unsigned long)p, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(kasan_check_read);
 
 void kasan_check_write(const void *p, unsigned int size)
 {
-       check_memory_region((unsigned long)p, size, true, _RET_IP_);
+       check_memory_region((unsigned long)p, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(kasan_check_write);
 
 #undef memset
 void *memset(void *addr, int c, size_t len)
 {
-       check_memory_region((unsigned long)addr, len, true, _RET_IP_);
+       check_memory_region((unsigned long)addr, len, WRITE_MODE, _RET_IP_);
 
        return __memset(addr, c, len);
 }
@@ -322,8 +322,8 @@ void *memset(void *addr, int c, size_t len)
 #undef memmove
 void *memmove(void *dest, const void *src, size_t len)
 {
-       check_memory_region((unsigned long)src, len, false, _RET_IP_);
-       check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+       check_memory_region((unsigned long)src, len, READ_MODE, _RET_IP_);
+       check_memory_region((unsigned long)dest, len, WRITE_MODE, _RET_IP_);
 
        return __memmove(dest, src, len);
 }
@@ -331,8 +331,8 @@ void *memmove(void *dest, const void *src, size_t len)
 #undef memcpy
 void *memcpy(void *dest, const void *src, size_t len)
 {
-       check_memory_region((unsigned long)src, len, false, _RET_IP_);
-       check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+       check_memory_region((unsigned long)src, len, READ_MODE, _RET_IP_);
+       check_memory_region((unsigned long)dest, len, WRITE_MODE, _RET_IP_);
 
        return __memcpy(dest, src, len);
 }
@@ -709,7 +709,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
 #define DEFINE_ASAN_LOAD_STORE(size)                                   \
        void __asan_load##size(unsigned long addr)                      \
        {                                                               \
-               check_memory_region_inline(addr, size, false, _RET_IP_);\
+               check_memory_region_inline(addr, size, READ_MODE, _RET_IP_);\
        }                                                               \
        EXPORT_SYMBOL(__asan_load##size);                               \
        __alias(__asan_load##size)                                      \
@@ -717,7 +717,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
        EXPORT_SYMBOL(__asan_load##size##_noabort);                     \
        void __asan_store##size(unsigned long addr)                     \
        {                                                               \
-               check_memory_region_inline(addr, size, true, _RET_IP_); \
+               check_memory_region_inline(addr, size, WRITE_MODE, _RET_IP_);\
        }                                                               \
        EXPORT_SYMBOL(__asan_store##size);                              \
        __alias(__asan_store##size)                                     \
@@ -732,7 +732,7 @@ DEFINE_ASAN_LOAD_STORE(16);
 
 void __asan_loadN(unsigned long addr, size_t size)
 {
-       check_memory_region(addr, size, false, _RET_IP_);
+       check_memory_region(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_loadN);
 
@@ -742,7 +742,7 @@ EXPORT_SYMBOL(__asan_loadN_noabort);
 
 void __asan_storeN(unsigned long addr, size_t size)
 {
-       check_memory_region(addr, size, true, _RET_IP_);
+       check_memory_region(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_storeN);
 
mm/kasan/kasan.h +10 -2
index 7f7ac51..47cb58c 100644
@@ -27,11 +27,19 @@
 #define KASAN_ABI_VERSION 1
 #endif
 
+/*
+ * Distinguish memory access
+ */
+enum acc_type {
+       READ_MODE,
+       WRITE_MODE
+};
+
 struct kasan_access_info {
        const void *access_addr;
        const void *first_bad_addr;
        size_t access_size;
-       bool is_write;
+       enum acc_type access_type;
        unsigned long ip;
 };
 
@@ -109,7 +117,7 @@ static inline bool kasan_report_enabled(void)
 }
 
 void kasan_report(unsigned long addr, size_t size,
-               bool is_write, unsigned long ip);
+               enum acc_type type, unsigned long ip);
 
 #ifdef CONFIG_SLAB
 void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache);
mm/kasan/report.c +6 -7
index b3c122d..e0bee22 100644
@@ -96,7 +96,7 @@ static void print_error_description(struct kasan_access_info *info)
                bug_type, (void *)info->ip,
                info->access_addr);
        pr_err("%s of size %zu by task %s/%d\n",
-               info->is_write ? "Write" : "Read",
+               info->access_type == WRITE_MODE ? "Write" : "Read",
                info->access_size, current->comm, task_pid_nr(current));
 }
 
@@ -267,7 +267,7 @@ static void kasan_report_error(struct kasan_access_info *info)
                pr_err("BUG: KASAN: %s on address %p\n",
                        bug_type, info->access_addr);
                pr_err("%s of size %zu by task %s/%d\n",
-                       info->is_write ? "Write" : "Read",
+                       info->access_type == WRITE_MODE ? "Write" : "Read",
                        info->access_size, current->comm,
                        task_pid_nr(current));
                dump_stack();
@@ -283,7 +283,7 @@ static void kasan_report_error(struct kasan_access_info *info)
 }
 
 void kasan_report(unsigned long addr, size_t size,
-               bool is_write, unsigned long ip)
+               enum acc_type type, unsigned long ip)
 {
        struct kasan_access_info info;
 
@@ -292,7 +292,7 @@ void kasan_report(unsigned long addr, size_t size,
 
        info.access_addr = (void *)addr;
        info.access_size = size;
-       info.is_write = is_write;
+       info.access_type = type;
        info.ip = ip;
 
        kasan_report_error(&info);
@@ -302,14 +302,14 @@ void kasan_report(unsigned long addr, size_t size,
 #define DEFINE_ASAN_REPORT_LOAD(size)                     \
 void __asan_report_load##size##_noabort(unsigned long addr) \
 {                                                         \
-       kasan_report(addr, size, false, _RET_IP_);        \
+       kasan_report(addr, size, READ_MODE, _RET_IP_);    \
 }                                                         \
 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
 
 #define DEFINE_ASAN_REPORT_STORE(size)                     \
 void __asan_report_store##size##_noabort(unsigned long addr) \
 {                                                          \
-       kasan_report(addr, size, true, _RET_IP_);          \
+       kasan_report(addr, size, WRITE_MODE, _RET_IP_);    \
 }                                                          \
 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
 
@@ -326,12 +326,12 @@ DEFINE_ASAN_REPORT_STORE(16);
 
 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
 {
-       kasan_report(addr, size, false, _RET_IP_);

mm/kasan/report.c DEFINE_ASAN_REPORT_STORE(16); · line 337

Quoted 10 lines
mm/kasan/report.c +2 -1
+       kasan_report(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_load_n_noabort);
 
 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
 {
-       kasan_report(addr, size, true, _RET_IP_);
+       kasan_report(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_store_n_noabort);

Hello seokhoon.yoon,

Where exactly do you hit readability problems?
I would say that the only problematic place is where we initialize the value:

    kasan_report(addr, size, true, _RET_IP_);
    check_memory_region(addr, size, true, _RET_IP_);

Here it is really difficult to say what true/false mean. Even if you know that it is access type, you don't necessary remember if true means write or read. That could be solved by adding comments:

    kasan_report(addr, size, /* write = */ true, _RET_IP_);

In all other places one sees that we are talking about "write".

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw

2016-06-05 21:49 GMT+09:00 Dmitry Vyukov <[email protected]>:

Quoted 259 lines · seokhoon.yoon

mm/kasan/kasan.c static __always_inline bool

Quoted 17 lines

When Kasan tell memory access is write or not, use true or false. This expression is simple and convenient.

But I think it is possible to more readable. and so change it.

---
 mm/kasan/kasan.c  | 32 ++++++++++++++++----------------
 mm/kasan/kasan.h  | 12 ++++++++++--
 mm/kasan/report.c | 16 ++++++++--------
 3 files changed, 34 insertions(+), 26 deletions(-)
mm/kasan/kasan.c +0 -0
index 18b6a2b..642d936 100644
@@ -274,7 +274,7 @@ static __always_inline bool

memory_is_poisoned(unsigned long addr, size_t size)

mm/kasan/kasan.c static __always_inline bool · line 276

Quoted 3 lines
mm/kasan/kasan.c +0 -0
 }
 
 static __always_inline void check_memory_region_inline(unsigned long

addr,

mm/kasan/kasan.c static __always_inline bool · line 277

Quoted 2 lines
mm/kasan/kasan.c +1 -1
-                                               size_t size, bool write,
+                                               size_t size, enum

acc_type type,

mm/kasan/kasan.c static __always_inline void

Quoted 4 lines
mm/kasan/kasan.c +0 -0
                                                unsigned long ret_ip)
 {
        if (unlikely(size == 0))
@@ -282,39 +282,39 @@ static __always_inline void

check_memory_region_inline(unsigned long addr,

Quoted 25 lines
mm/kasan/kasan.c +4 -4
        if (unlikely((void *)addr <
                kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
-               kasan_report(addr, size, write, ret_ip);
+               kasan_report(addr, size, type, ret_ip);
                return;
        }
 
        if (likely(!memory_is_poisoned(addr, size)))
                return;
 
-       kasan_report(addr, size, write, ret_ip);
+       kasan_report(addr, size, type, ret_ip);
 }
 
 static void check_memory_region(unsigned long addr,
-                               size_t size, bool write,
+                               size_t size, enum acc_type type,
                                unsigned long ret_ip)
 {
-       check_memory_region_inline(addr, size, write, ret_ip);
+       check_memory_region_inline(addr, size, type, ret_ip);
 }
 
 void kasan_check_read(const void *p, unsigned int size)

mm/kasan/kasan.c static __always_inline void check_memory_region_inline(unsigned long addr,

Quoted 10 lines
mm/kasan/kasan.c +2 -2
 {
-       check_memory_region((unsigned long)p, size, false, _RET_IP_);
+       check_memory_region((unsigned long)p, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(kasan_check_read);
 
 void kasan_check_write(const void *p, unsigned int size)
 {
-       check_memory_region((unsigned long)p, size, true, _RET_IP_);
+       check_memory_region((unsigned long)p, size, WRITE_MODE,

_RET_IP_);

mm/kasan/kasan.c static __always_inline void check_memory_region_inline(unsigned long addr,

Quoted 8 lines
mm/kasan/kasan.c +1 -1
 }
 EXPORT_SYMBOL(kasan_check_write);
 
 #undef memset
 void *memset(void *addr, int c, size_t len)
 {
-       check_memory_region((unsigned long)addr, len, true, _RET_IP_);
+       check_memory_region((unsigned long)addr, len, WRITE_MODE,

_RET_IP_);

mm/kasan/kasan.c void *memset(void *addr, int c, size_t len) · line 325

Quoted 10 lines
mm/kasan/kasan.c +1 -2
        return __memset(addr, c, len);
 }
@@ -322,8 +322,8 @@ void *memset(void *addr, int c, size_t len)
 #undef memmove
 void *memmove(void *dest, const void *src, size_t len)
 {
-       check_memory_region((unsigned long)src, len, false, _RET_IP_);
-       check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+       check_memory_region((unsigned long)src, len, READ_MODE,

_RET_IP_);

mm/kasan/kasan.c void *memset(void *addr, int c, size_t len) · line 326

Quoted 1 line
mm/kasan/kasan.c +1 -0
+       check_memory_region((unsigned long)dest, len, WRITE_MODE,

_RET_IP_);

mm/kasan/kasan.c void *memmove(void *dest, const void *src, size_t

Quoted 4 lines
mm/kasan/kasan.c +0 -0
        return __memmove(dest, src, len);
 }
@@ -331,8 +331,8 @@ void *memmove(void *dest, const void *src, size_t

len)

mm/kasan/kasan.c void *memmove(void *dest, const void *src, size_t len)

Quoted 6 lines
mm/kasan/kasan.c +1 -2
 #undef memcpy
 void *memcpy(void *dest, const void *src, size_t len)
 {
-       check_memory_region((unsigned long)src, len, false, _RET_IP_);
-       check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+       check_memory_region((unsigned long)src, len, READ_MODE,

_RET_IP_);

mm/kasan/kasan.c void *memmove(void *dest, const void *src, size_t · line 335

Quoted 1 line
mm/kasan/kasan.c +1 -0
+       check_memory_region((unsigned long)dest, len, WRITE_MODE,

_RET_IP_);

mm/kasan/kasan.c EXPORT_SYMBOL(__asan_unregister_globals); · line 712

Quoted 9 lines
mm/kasan/kasan.c +1 -1
        return __memcpy(dest, src, len);
 }
@@ -709,7 +709,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
 #define DEFINE_ASAN_LOAD_STORE(size)                                   \
        void __asan_load##size(unsigned long addr)                      \
        {                                                               \
-               check_memory_region_inline(addr, size, false, _RET_IP_);\
+               check_memory_region_inline(addr, size, READ_MODE,

_RET_IP_);\

mm/kasan/kasan.c EXPORT_SYMBOL(__asan_unregister_globals); · line 720

Quoted 9 lines
mm/kasan/kasan.c +1 -1
        }                                                               \
        EXPORT_SYMBOL(__asan_load##size);                               \
        __alias(__asan_load##size)                                      \
@@ -717,7 +717,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
        EXPORT_SYMBOL(__asan_load##size##_noabort);                     \
        void __asan_store##size(unsigned long addr)                     \
        {                                                               \
-               check_memory_region_inline(addr, size, true, _RET_IP_); \
+               check_memory_region_inline(addr, size, WRITE_MODE,

_RET_IP_);\

Quoted 47 lines
mm/kasan/kasan.c +2 -2
        }                                                               \
        EXPORT_SYMBOL(__asan_store##size);                              \
        __alias(__asan_store##size)                                     \
@@ -732,7 +732,7 @@ DEFINE_ASAN_LOAD_STORE(16);
 
 void __asan_loadN(unsigned long addr, size_t size)
 {
-       check_memory_region(addr, size, false, _RET_IP_);
+       check_memory_region(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_loadN);
 
@@ -742,7 +742,7 @@ EXPORT_SYMBOL(__asan_loadN_noabort);
 
 void __asan_storeN(unsigned long addr, size_t size)
 {
-       check_memory_region(addr, size, true, _RET_IP_);
+       check_memory_region(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_storeN);
 
mm/kasan/kasan.h +9 -1
index 7f7ac51..47cb58c 100644
@@ -27,11 +27,19 @@
 #define KASAN_ABI_VERSION 1
 #endif
 
+/*
+ * Distinguish memory access
+ */
+enum acc_type {
+       READ_MODE,
+       WRITE_MODE
+};
+
 struct kasan_access_info {
        const void *access_addr;
        const void *first_bad_addr;
        size_t access_size;
-       bool is_write;
+       enum acc_type access_type;
        unsigned long ip;
 };
 

mm/kasan/kasan.h static inline bool kasan_report_enabled(void) · line 123

Quoted 8 lines
mm/kasan/kasan.h +1 -1
@@ -109,7 +117,7 @@ static inline bool kasan_report_enabled(void)
 }
 
 void kasan_report(unsigned long addr, size_t size,
-               bool is_write, unsigned long ip);
+               enum acc_type type, unsigned long ip);
 
 #ifdef CONFIG_SLAB
 void quarantine_put(struct kasan_free_meta *info, struct kmem_cache

*cache);

mm/kasan/report.c static void print_error_description(struct

Quoted 5 lines
mm/kasan/report.c +0 -0
index b3c122d..e0bee22 100644
@@ -96,7 +96,7 @@ static void print_error_description(struct

kasan_access_info *info)

mm/kasan/report.c static void kasan_report_error(struct

Quoted 9 lines
mm/kasan/report.c +1 -1
                bug_type, (void *)info->ip,
                info->access_addr);
        pr_err("%s of size %zu by task %s/%d\n",
-               info->is_write ? "Write" : "Read",
+               info->access_type == WRITE_MODE ? "Write" : "Read",
                info->access_size, current->comm, task_pid_nr(current));
 }
 
@@ -267,7 +267,7 @@ static void kasan_report_error(struct

kasan_access_info *info)

mm/kasan/report.c static void kasan_report_error(struct kasan_access_info *info)

Quoted 5 lines
mm/kasan/report.c +1 -1
                pr_err("BUG: KASAN: %s on address %p\n",
                        bug_type, info->access_addr);
                pr_err("%s of size %zu by task %s/%d\n",
-                       info->is_write ? "Write" : "Read",
+                       info->access_type == WRITE_MODE ? "Write" :

"Read",

mm/kasan/report.c static void kasan_report_error(struct

Quoted 4 lines
mm/kasan/report.c +0 -0
                        info->access_size, current->comm,
                        task_pid_nr(current));
                dump_stack();
@@ -283,7 +283,7 @@ static void kasan_report_error(struct

kasan_access_info *info)

Quoted 39 lines
mm/kasan/report.c +4 -5
 }
 
 void kasan_report(unsigned long addr, size_t size,
-               bool is_write, unsigned long ip)
+               enum acc_type type, unsigned long ip)
 {
        struct kasan_access_info info;
 
@@ -292,7 +292,7 @@ void kasan_report(unsigned long addr, size_t size,
 
        info.access_addr = (void *)addr;
        info.access_size = size;
-       info.is_write = is_write;
+       info.access_type = type;
        info.ip = ip;
 
        kasan_report_error(&info);
@@ -302,14 +302,14 @@ void kasan_report(unsigned long addr, size_t size,
 #define DEFINE_ASAN_REPORT_LOAD(size)                     \
 void __asan_report_load##size##_noabort(unsigned long addr) \
 {                                                         \
-       kasan_report(addr, size, false, _RET_IP_);        \
+       kasan_report(addr, size, READ_MODE, _RET_IP_);    \
 }                                                         \
 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
 
 #define DEFINE_ASAN_REPORT_STORE(size)                     \
 void __asan_report_store##size##_noabort(unsigned long addr) \
 {                                                          \
-       kasan_report(addr, size, true, _RET_IP_);          \
+       kasan_report(addr, size, WRITE_MODE, _RET_IP_);    \
 }                                                          \
 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
 
@@ -326,12 +326,12 @@ DEFINE_ASAN_REPORT_STORE(16);
 
 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
 {
-       kasan_report(addr, size, false, _RET_IP_);

mm/kasan/report.c DEFINE_ASAN_REPORT_STORE(16); · line 337

Quoted 10 lines
mm/kasan/report.c +2 -1
+       kasan_report(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_load_n_noabort);
 
 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
 {
-       kasan_report(addr, size, true, _RET_IP_);
+       kasan_report(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_store_n_noabort);

Hello seokhoon.yoon,

Quoted 1 line

Hi Dmitry,

thanks for your replies.

Quoted 3 lines

Where exactly do you hit readability problems?

I don`t hit readablity problem,but kasan need to abstract this expression. I think more abstraction give us more readablity. isn't it? :)

Quoted 6 lines

I would say that the only problematic place is where we initialize the value:

    kasan_report(addr, size, true, _RET_IP_);
    check_memory_region(addr, size, true, _RET_IP_);

Here it is really difficult to say what true/false mean. Even if you

Quoted 1 line

know that it is access type, you don't necessary remember if true

means write or read. That could be solved by adding comments:

Quoted 4 lines
    kasan_report(addr, size, /* write = */ true, _RET_IP_);

In all other places one sees that we are talking about "write".

thanks.

reviewer
#3 lore raw
Quoted 282 lines · SeokHoon Yoon

2016-06-05 21:49 GMT+09:00 Dmitry Vyukov <[email protected]>:

Quoted 265 lines · seokhoon.yoon
Quoted 263 lines

When Kasan tell memory access is write or not, use true or false. This expression is simple and convenient.

But I think it is possible to more readable. and so change it.

---
 mm/kasan/kasan.c  | 32 ++++++++++++++++----------------
 mm/kasan/kasan.h  | 12 ++++++++++--
 mm/kasan/report.c | 16 ++++++++--------
 3 files changed, 34 insertions(+), 26 deletions(-)
mm/kasan/kasan.c +1 -1
index 18b6a2b..642d936 100644
@@ -274,7 +274,7 @@ static __always_inline bool
 memory_is_poisoned(unsigned long addr, size_t size)
 }
 
 static __always_inline void check_memory_region_inline(unsigned long
 addr,
-                                               size_t size, bool write,
+                                               size_t size, enum
 acc_type type,

unsigned long ret_ip)

 {
        if (unlikely(size == 0))
mm/kasan/kasan.c +7 -7
@@ -282,39 +282,39 @@ static __always_inline void
 check_memory_region_inline(unsigned long addr,
 
        if (unlikely((void *)addr <
                kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
-               kasan_report(addr, size, write, ret_ip);
+               kasan_report(addr, size, type, ret_ip);
                return;
        }
 
        if (likely(!memory_is_poisoned(addr, size)))
                return;
 
-       kasan_report(addr, size, write, ret_ip);
+       kasan_report(addr, size, type, ret_ip);
 }
 
 static void check_memory_region(unsigned long addr,
-                               size_t size, bool write,
+                               size_t size, enum acc_type type,
                                unsigned long ret_ip)
 {
-       check_memory_region_inline(addr, size, write, ret_ip);
+       check_memory_region_inline(addr, size, type, ret_ip);
 }
 
 void kasan_check_read(const void *p, unsigned int size)
 {
-       check_memory_region((unsigned long)p, size, false, _RET_IP_);
+       check_memory_region((unsigned long)p, size, READ_MODE,
 _RET_IP_);
 }
 EXPORT_SYMBOL(kasan_check_read);
 
 void kasan_check_write(const void *p, unsigned int size)
 {
-       check_memory_region((unsigned long)p, size, true, _RET_IP_);
+       check_memory_region((unsigned long)p, size, WRITE_MODE,
 _RET_IP_);
 }
 EXPORT_SYMBOL(kasan_check_write);
 
 #undef memset
 void *memset(void *addr, int c, size_t len)
 {
-       check_memory_region((unsigned long)addr, len, true, _RET_IP_);
+       check_memory_region((unsigned long)addr, len, WRITE_MODE,

_RET_IP_);

        return __memset(addr, c, len);

}

mm/kasan/kasan.c +2 -2
@@ -322,8 +322,8 @@ void *memset(void *addr, int c, size_t len)
 #undef memmove
 void *memmove(void *dest, const void *src, size_t len)
 {
-       check_memory_region((unsigned long)src, len, false, _RET_IP_);
-       check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+       check_memory_region((unsigned long)src, len, READ_MODE,
 _RET_IP_);
+       check_memory_region((unsigned long)dest, len, WRITE_MODE,
 _RET_IP_);
 
        return __memmove(dest, src, len);

}

mm/kasan/kasan.c +2 -2
@@ -331,8 +331,8 @@ void *memmove(void *dest, const void *src, size_t
 len)
 #undef memcpy
 void *memcpy(void *dest, const void *src, size_t len)
 {
-       check_memory_region((unsigned long)src, len, false, _RET_IP_);
-       check_memory_region((unsigned long)dest, len, true, _RET_IP_);
+       check_memory_region((unsigned long)src, len, READ_MODE,
 _RET_IP_);
+       check_memory_region((unsigned long)dest, len, WRITE_MODE,
 _RET_IP_);
        return __memcpy(dest, src, len);

}

mm/kasan/kasan.c +0 -1
@@ -709,7 +709,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
 #define DEFINE_ASAN_LOAD_STORE(size)
 \
        void __asan_load##size(unsigned long addr)
 \
        {
 \
-               check_memory_region_inline(addr, size, false,

_RET_IP_);\
+ check_memory_region_inline(addr, size, READ_MODE,

_RET_IP_);\
        }
\
        EXPORT_SYMBOL(__asan_load##size);
\
        __alias(__asan_load##size)

\

mm/kasan/kasan.c +0 -1
@@ -717,7 +717,7 @@ EXPORT_SYMBOL(__asan_unregister_globals);
        EXPORT_SYMBOL(__asan_load##size##_noabort);
 \
        void __asan_store##size(unsigned long addr)
 \
        {
 \
-               check_memory_region_inline(addr, size, true, _RET_IP_);

\
+ check_memory_region_inline(addr, size, WRITE_MODE,

_RET_IP_);\
        }
\
        EXPORT_SYMBOL(__asan_store##size);
\
        __alias(__asan_store##size)

\

mm/kasan/kasan.c +2 -2
@@ -732,7 +732,7 @@ DEFINE_ASAN_LOAD_STORE(16);
 
 void __asan_loadN(unsigned long addr, size_t size)
 {
-       check_memory_region(addr, size, false, _RET_IP_);
+       check_memory_region(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_loadN);
 
@@ -742,7 +742,7 @@ EXPORT_SYMBOL(__asan_loadN_noabort);
 
 void __asan_storeN(unsigned long addr, size_t size)
 {
-       check_memory_region(addr, size, true, _RET_IP_);
+       check_memory_region(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_storeN);
 
mm/kasan/kasan.h +10 -2
index 7f7ac51..47cb58c 100644
@@ -27,11 +27,19 @@
 #define KASAN_ABI_VERSION 1
 #endif
 
+/*
+ * Distinguish memory access
+ */
+enum acc_type {
+       READ_MODE,
+       WRITE_MODE
+};
+
 struct kasan_access_info {
        const void *access_addr;
        const void *first_bad_addr;
        size_t access_size;
-       bool is_write;
+       enum acc_type access_type;
        unsigned long ip;
 };
 
@@ -109,7 +117,7 @@ static inline bool kasan_report_enabled(void)
 }
 
 void kasan_report(unsigned long addr, size_t size,
-               bool is_write, unsigned long ip);
+               enum acc_type type, unsigned long ip);
 
 #ifdef CONFIG_SLAB
 void quarantine_put(struct kasan_free_meta *info, struct kmem_cache

*cache);

mm/kasan/report.c +2 -2
index b3c122d..e0bee22 100644
@@ -96,7 +96,7 @@ static void print_error_description(struct
 kasan_access_info *info)
                bug_type, (void *)info->ip,
                info->access_addr);
        pr_err("%s of size %zu by task %s/%d\n",
-               info->is_write ? "Write" : "Read",
+               info->access_type == WRITE_MODE ? "Write" : "Read",
                info->access_size, current->comm, task_pid_nr(current));
 }
@@ -267,7 +267,7 @@ static void kasan_report_error(struct
 kasan_access_info *info)
                pr_err("BUG: KASAN: %s on address %p\n",
                        bug_type, info->access_addr);
                pr_err("%s of size %zu by task %s/%d\n",
-                       info->is_write ? "Write" : "Read",
+                       info->access_type == WRITE_MODE ? "Write" :
 "Read",
                        info->access_size, current->comm,
                        task_pid_nr(current));
                dump_stack();
mm/kasan/report.c +5 -5
@@ -283,7 +283,7 @@ static void kasan_report_error(struct
 kasan_access_info *info)
 }
 
 void kasan_report(unsigned long addr, size_t size,
-               bool is_write, unsigned long ip)
+               enum acc_type type, unsigned long ip)
 {
        struct kasan_access_info info;
@@ -292,7 +292,7 @@ void kasan_report(unsigned long addr, size_t size,
 
        info.access_addr = (void *)addr;
        info.access_size = size;
-       info.is_write = is_write;
+       info.access_type = type;
        info.ip = ip;
 
        kasan_report_error(&info);
@@ -302,14 +302,14 @@ void kasan_report(unsigned long addr, size_t size,
 #define DEFINE_ASAN_REPORT_LOAD(size)                     \
 void __asan_report_load##size##_noabort(unsigned long addr) \
 {                                                         \
-       kasan_report(addr, size, false, _RET_IP_);        \
+       kasan_report(addr, size, READ_MODE, _RET_IP_);    \
 }                                                         \
 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
 
 #define DEFINE_ASAN_REPORT_STORE(size)                     \
 void __asan_report_store##size##_noabort(unsigned long addr) \
 {                                                          \
-       kasan_report(addr, size, true, _RET_IP_);          \
+       kasan_report(addr, size, WRITE_MODE, _RET_IP_);    \
 }                                                          \
 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
 
@@ -326,12 +326,12 @@ DEFINE_ASAN_REPORT_STORE(16);
 
 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
 {
-       kasan_report(addr, size, false, _RET_IP_);
+       kasan_report(addr, size, READ_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_load_n_noabort);
 
Quoted 10 lines

mm/kasan/report.c DEFINE_ASAN_REPORT_STORE(16); · line 337

Quoted 6 lines
mm/kasan/report.c +1 -1
 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
 {
-       kasan_report(addr, size, true, _RET_IP_);
+       kasan_report(addr, size, WRITE_MODE, _RET_IP_);
 }
 EXPORT_SYMBOL(__asan_report_store_n_noabort);

Hello seokhoon.yoon,

Hi Dmitry,
thanks for your replies.

Quoted 2 lines
Quoted 5 lines
Quoted 1 line

Where exactly do you hit readability problems?

I don`t hit readablity problem,but kasan need to abstract this expression. I think more abstraction give us more readablity. isn't it? :)

Keeping it simple also gives readability.

Quoted 9 lines
Quoted 9 lines

I would say that the only problematic place is where we initialize the value:

    kasan_report(addr, size, true, _RET_IP_);
    check_memory_region(addr, size, true, _RET_IP_);

Here it is really difficult to say what true/false mean. Even if you know that it is access type, you don't necessary remember if true

Quoted 8 lines
Quoted 5 lines

means write or read. That could be solved by adding comments:

    kasan_report(addr, size, /* write = */ true, _RET_IP_);

In all other places one sees that we are talking about "write".

thanks.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>