Skip to contentSkip to conversation

lib/stackdepot: use a non-instrumented version of memcmp()

PATCH Superseded Alexander Potapenko 2 participants 3 messages Nov 15, 2017 – Nov 16, 2017

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/stackdepot: use a non-instrumented version of memcmp()

stackdepot used to call memcmp(), which compiler tools normally instrument, therefore every lookup used to unnecessarily call instrumented code.
This is somewhat ok in the case of KASAN, but under KMSAN a lot of time was spent in the instrumentation.

---
 lib/stackdepot.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
lib/stackdepot.c +18 -3
index f87d138e9672..d372101e8dc2 100644
@@ -163,6 +163,23 @@ static inline u32 hash_stack(unsigned long *entries, unsigned int size)
 			       STACK_HASH_SEED);
 }
 
+/* Use our own, non-instrumented version of memcmp().
+ *
+ * We actually don't care about the order, just the equality.
+ */
+static inline
+int stackdepot_memcmp(const void *s1, const void *s2, unsigned int n)
+{
+	unsigned long *u1 = (unsigned long *)s1;
+	unsigned long *u2 = (unsigned long *)s2;
+
+	for ( ; n-- ; u1++, u2++) {
+		if (*u1 != *u2)
+			return 1;
+	}
+	return 0;
+}
+
 /* Find a stack that is equal to the one stored in entries in the hash */
 static inline struct stack_record *find_stack(struct stack_record *bucket,
 					     unsigned long *entries, int size,
@@ -173,10 +190,8 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
 	for (found = bucket; found; found = found->next) {
 		if (found->hash == hash &&
 		    found->size == size &&
-		    !memcmp(entries, found->entries,
-			    size * sizeof(unsigned long))) {
+		    !stackdepot_memcmp(entries, found->entries, size))
 			return found;
-		}
 	}
 	return NULL;
 }
Signature · 7 lines
2.15.0.448.gf294e3d99a-goog

--
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>
#1 lore raw
Quoted 16 lines · Alexander Potapenko

stackdepot used to call memcmp(), which compiler tools normally instrument, therefore every lookup used to unnecessarily call instrumented code.
This is somewhat ok in the case of KASAN, but under KMSAN a lot of time was spent in the instrumentation.

---
 lib/stackdepot.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
lib/stackdepot.c +0 -0
index f87d138e9672..d372101e8dc2 100644

lib/stackdepot.c static inline u32 hash_stack(unsigned long *entries, unsigned int size) · line 172

Quoted 10 lines
lib/stackdepot.c +7 -0
@@ -163,6 +163,23 @@ static inline u32 hash_stack(unsigned long *entries, unsigned int size)
 			       STACK_HASH_SEED);
 }
 
+/* Use our own, non-instrumented version of memcmp().
+ *
+ * We actually don't care about the order, just the equality.
+ */
+static inline
+int stackdepot_memcmp(const void *s1, const void *s2, unsigned int n)
+{

Why 'void *' types? The function treats s1, s2 as array of long, also 'n' is number of longs here.

Quoted 16 lines
lib/stackdepot.c +10 -0
+	unsigned long *u1 = (unsigned long *)s1;
+	unsigned long *u2 = (unsigned long *)s2;
+
+	for ( ; n-- ; u1++, u2++) {
+		if (*u1 != *u2)
+			return 1;
+	}
+	return 0;
+}
+
 /* Find a stack that is equal to the one stored in entries in the hash */
 static inline struct stack_record *find_stack(struct stack_record *bucket,
 					     unsigned long *entries, int size,
@@ -173,10 +190,8 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
 	for (found = bucket; found; found = found->next) {

lib/stackdepot.c static inline struct stack_record *find_stack(struct stack_record *bucket, · line 197

Quoted 10 lines
lib/stackdepot.c +1 -3
 		if (found->hash == hash &&
 		    found->size == size &&
-		    !memcmp(entries, found->entries,
-			    size * sizeof(unsigned long))) {
+		    !stackdepot_memcmp(entries, found->entries, size))
 			return found;
-		}
 	}
 	return NULL;
 }

--
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
Quoted 22 lines · Andrey Ryabinin
Quoted 19 lines · Alexander Potapenko

stackdepot used to call memcmp(), which compiler tools normally instrument, therefore every lookup used to unnecessarily call instrumented code.
This is somewhat ok in the case of KASAN, but under KMSAN a lot of time was spent in the instrumentation.

---
 lib/stackdepot.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
lib/stackdepot.c +0 -0
index f87d138e9672..d372101e8dc2 100644
@@ -163,6 +163,23 @@ static inline u32 hash_stack(unsigned long *entries, unsigned int size)
                             STACK_HASH_SEED);
 }
 
Quoted 9 lines

lib/stackdepot.c static inline u32 hash_stack(unsigned long *entries, unsigned int size) · line 172

Quoted 7 lines
lib/stackdepot.c +7 -0
+/* Use our own, non-instrumented version of memcmp().
+ *
+ * We actually don't care about the order, just the equality.
+ */
+static inline
+int stackdepot_memcmp(const void *s1, const void *s2, unsigned int n)
+{

Why 'void *' types? The function treats s1, s2 as array of long, also 'n' is number of longs here.

Agreed. I started with a plain memcpy, so the arg types were left over.

Quoted 26 lines
Quoted 26 lines
lib/stackdepot.c +11 -3
+     unsigned long *u1 = (unsigned long *)s1;
+     unsigned long *u2 = (unsigned long *)s2;
+
+     for ( ; n-- ; u1++, u2++) {
+             if (*u1 != *u2)
+                     return 1;
+     }
+     return 0;
+}
+
 /* Find a stack that is equal to the one stored in entries in the hash */
 static inline struct stack_record *find_stack(struct stack_record *bucket,
                                           unsigned long *entries, int size,
@@ -173,10 +190,8 @@ static inline struct stack_record *find_stack(struct stack_record *bucket,
      for (found = bucket; found; found = found->next) {
              if (found->hash == hash &&
                  found->size == size &&
-                 !memcmp(entries, found->entries,
-                         size * sizeof(unsigned long))) {
+                 !stackdepot_memcmp(entries, found->entries, size))
                      return found;
-             }
      }
      return NULL;
 }
Signature · 16 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg

--
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>