Skip to contentSkip to conversation

libselinux: improve performance with pcre matches

RFC PATCH New Carlo Marcelo Arenas Belón 3 participants 8 messages Jan 21, 2023 – Jul 21, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 libselinux: improve performance with pcre matches

Since 30b3e9d2 (libselinux: Workaround for heap overhead of pcre, 2023-01-12), performance of PCRE2 matches has been affected due to excesive recreation of the match_data in an attempt to reduce memory utilization, but instead of a workaround, it would be better to address the problem and maybe even improve performance in the process.

The issue is that currently the structure that holds PCRE state has both a pcre2_code (which is per pattern) and a pcre2_match_data (which is per match), forcing us to add a mutex to prevent multiple matches to step on each other.

Lets remove the match_data and the mutex and instead allocate one once in a thread independent way that could be used and reused, by extending our pthread inteface to not only store TLS variables but also retrieve them, and use one of those.

Since we are not interested on the capture groups (if any) lets only allocate 1 pair which is all that will be needed and change the logic so that a return of 0 (which means the pattern matched but there were not enough fields) is also considered a match.

This will ensure that the memory use would be bound to the number of concurrent matches instead of the number of patterns and therefore reduce the impact that recent changes on the way the frames used for matching are allocated might had brough since 10.41 was released.

---
Sent as an RFC since it has been very lightly tested (I can't even build the whole project), and because I couldn't figure out where to destroy the key, so it is adding a once on a lifetime leak of a pointer.

It is also more verbose than it needs, because I am combining it with the revert of the workaround, and that could be split away, but that would hopefully help with testing as well.

 libselinux/src/regex.c            | 77 ++++++++++++-------------------
 libselinux/src/selinux_internal.h |  4 ++
 2 files changed, 34 insertions(+), 47 deletions(-)
libselinux/src/regex.c +30 -47
index 16df6790..201a6695 100644
@@ -30,6 +30,10 @@
 #endif
 
 #ifdef USE_PCRE2
+static pthread_key_t match_data_key;
+static int match_data_key_uninitialized = 1;
+static __thread char match_data_initialized;
+
 char const *regex_arch_string(void)
 {
 	static char arch_string_buffer[32];
@@ -60,14 +64,6 @@ char const *regex_arch_string(void)
 
 struct regex_data {
 	pcre2_code *regex; /* compiled regular expression */
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	/*
-	 * match data block required for the compiled
-	 * pattern in pcre2
-	 */
-	pcre2_match_data *match_data;
-#endif
-	pthread_mutex_t match_mutex;
 };
 
 int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
@@ -86,13 +82,6 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
 		goto err;
 	}
 
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	(*regex)->match_data =
-	    pcre2_match_data_create_from_pattern((*regex)->regex, NULL);
-	if (!(*regex)->match_data) {
-		goto err;
-	}
-#endif
 	return 0;
 
 err:
@@ -142,13 +131,6 @@ int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex,
 		if (rc != 1)
 			goto err;
 
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-		(*regex)->match_data =
-		    pcre2_match_data_create_from_pattern((*regex)->regex, NULL);
-		if (!(*regex)->match_data)
-			goto err;
-#endif
-
 		*regex_compiled = true;
 	}
 
@@ -204,18 +186,16 @@ out:
 	return rc;
 }
 
+static void match_data_thread_free(void *match_data)
+{
+	pcre2_match_data_free(match_data);
+}
+
 void regex_data_free(struct regex_data *regex)
 {
 	if (regex) {
 		if (regex->regex)
 			pcre2_code_free(regex->regex);
-
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-		if (regex->match_data)
-			pcre2_match_data_free(regex->match_data);
-#endif
-
-		__pthread_mutex_destroy(&regex->match_mutex);
 		free(regex);
 	}
 }
@@ -223,32 +203,30 @@ void regex_data_free(struct regex_data *regex)
 int regex_match(struct regex_data *regex, char const *subject, int partial)
 {
 	int rc;
+	bool slow;
 	pcre2_match_data *match_data;
-	__pthread_mutex_lock(&regex->match_mutex);
 
-#ifdef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	match_data = pcre2_match_data_create_from_pattern(
-	    regex->regex, NULL);
-	if (match_data == NULL) {
-		__pthread_mutex_unlock(&regex->match_mutex);
-		return REGEX_ERROR;
+	if (match_data_initialized == 0) {
+		match_data = pcre2_match_data_create(1, NULL);
+		match_data_initialized = 1;
+		__selinux_setspecific(match_data_key, match_data);
+	} else
+		match_data = __selinux_getspecific(match_data_key);
+
+	slow = (match_data == NULL);
+	if (slow) {
+		match_data = pcre2_match_data_create_from_pattern(regex->regex,
+									NULL);
 	}
-#else
-	match_data = regex->match_data;
-#endif
 
 	rc = pcre2_match(
 	    regex->regex, (PCRE2_SPTR)subject, PCRE2_ZERO_TERMINATED, 0,
 	    partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
 
-#ifdef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	// pcre2_match allocates heap and it won't be freed until
-	// pcre2_match_data_free, resulting in heap overhead.
-	pcre2_match_data_free(match_data);
-#endif
+	if (slow)
+		pcre2_match_data_free(match_data);
 
-	__pthread_mutex_unlock(&regex->match_mutex);
-	if (rc > 0)
+	if (rc >= 0)
 		return REGEX_MATCH;
 	switch (rc) {
 	case PCRE2_ERROR_PARTIAL:
@@ -290,7 +268,12 @@ struct regex_data *regex_data_create(void)
 	if (!regex_data)
 		return NULL;
 
-	__pthread_mutex_init(&regex_data->match_mutex, NULL);
+	if (match_data_key_uninitialized) {
+		match_data_key_uninitialized = __selinux_key_create(
+							&match_data_key,
+							match_data_thread_free);
+	}
+
 	return regex_data;
 }
 
libselinux/src/selinux_internal.h +4 -0
index 06f2c038..d1e6c50f 100644
@@ -13,6 +13,7 @@ extern int selinux_page_size ;
 #pragma weak pthread_key_create
 #pragma weak pthread_key_delete
 #pragma weak pthread_setspecific
+#pragma weak pthread_getspecific
 
 /* Call handler iff the first call.  */
 #define __selinux_once(ONCE_CONTROL, INIT_FUNCTION)	\
@@ -41,6 +42,9 @@ extern int selinux_page_size ;
 			pthread_setspecific(KEY, VALUE);	\
 	} while (0)
 
+#define __selinux_getspecific(KEY)				\
+	(pthread_getspecific != NULL ? pthread_getspecific(KEY) : NULL)
+
 /* selabel_lookup() is only thread safe if we're compiled with pthreads */
 
 #pragma weak pthread_mutex_init
Signature · 1 line
2.30.2
#1 lore raw

cover improve performance of pcre matches

The following series optimizes the way PCRE2 matches are done by using more efficiently the match_data resources and shows over 30% reduction in cpu utilization while only using 1/5 of the memory as shown in the bug report[1] that started it all.

The first patch is just an administrative change that I found useful while building and testing the code with a development version of PCRE2.

The second patch reverts the workaround merged in 30b3e9d2 (libselinux:
Workaround for heap overhead of pcre, 2023-01-12) while addressing the increased memory utilization that it was trying to prevent by changing the way the match_data required for each match was being handled.

The last patch changes the single threaded codepath to allow for a similar performance improvement done to the multithreaded codepath by intentionally leaking one match_data that could be reused for all matches.

Carlo Marcelo Arenas Belón (3):
scripts: respect an initial LD_LIBRARY_PATH with env_use_destdir
libselinux: improve performance with pcre matches
libselinux: use a static match_data if single threaded

 libselinux/src/regex.c            | 111 +++++++++++++++++-------------
 libselinux/src/selinux_internal.h |   4 ++
 scripts/env_use_destdir           |   8 ++-
 3 files changed, 73 insertions(+), 50 deletions(-)

[1] https://github.com/PCRE2Project/pcre2/issues/194

Signature · 1 line
2.39.0
#2 lore raw

PATCH 1/3 scripts: respect an initial LD_LIBRARY_PATH with env_use_destdir

In the highly unlikely that you would need to invoke a tool with an out of tree PCRE2 library, it comes handy to set LD_LIBRARY_PATH to point to such library and not get your setting blown away.

As usual, if no LD_LIBRARY_PATH exists previous to calling the script, nothing will change.

---
 scripts/env_use_destdir | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
scripts/env_use_destdir +7 -1
index 89d989a2..61ef7b26 100755
@@ -21,7 +21,13 @@ if [ -z "${DESTDIR:-}" ] ; then
     exit 1
 fi
 
-export LD_LIBRARY_PATH="$DESTDIR/usr/lib:$DESTDIR/lib"
+if [ -n "${LD_LIBRARY_PATH:-}" ] ; then
+    LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$DESTDIR/usr/lib:$DESTDIR/lib"
+else
+    LD_LIBRARY_PATH="$DESTDIR/usr/lib:$DESTDIR/lib"
+fi
+
+export LD_LIBRARY_PATH
 if [ -n "${PREFIX:-}" ] ; then
     LD_LIBRARY_PATH="$DESTDIR$PREFIX/lib:$LD_LIBRARY_PATH"
 fi
Signature · 1 line
2.39.0
#3 lore raw

PATCH 2/3 libselinux: improve performance with pcre matches

In reply to Carlo Marcelo Arenas Belón

Since 30b3e9d2 (libselinux: Workaround for heap overhead of pcre, 2023-01-12), performance of PCRE2 matches has been affected due to excesive recreation of the match_data in an attempt to reduce memory utilization; instead of a workaround, it would be better to address the problem and maybe even improve performance in the process.

The issue is that currently the structure that holds PCRE state has both a pcre2_code (which is per pattern) and a pcre2_match_data (which is per match), forcing us to add a mutex to prevent multiple matches to step on each other.

Lets remove the match_data and the mutex and instead allocate one once in a thread independent way that could be used and reused, by extending our pthread interface to not only store TLS variables but also retrieve them, and then use one of those.

Since we are not interested on the capture groups (if any) lets only allocate 1 pair which is all that will be needed and change the logic so that a return of 0 (which means the pattern matched but there were not enough capture spots) is also considered a match.

This will ensure that the memory use would be bound to the number of concurrent matches instead of the number of patterns and therefore reduce the impact that recent changes on the way that the frames used for matching are allocated might had brough since 10.41 was released.

For cases where threads are not available, just keep it working in slow mode as done before the workaround was reverted.

---
 libselinux/src/regex.c            | 108 +++++++++++++++++-------------
 libselinux/src/selinux_internal.h |   4 ++
 2 files changed, 64 insertions(+), 48 deletions(-)
libselinux/src/regex.c +60 -48
index 16df6790..54f24026 100644
@@ -30,6 +30,11 @@
 #endif
 
 #ifdef USE_PCRE2
+static pthread_key_t match_data_key;
+static int match_data_key_initialized = -1;
+static pthread_mutex_t key_mutex = PTHREAD_MUTEX_INITIALIZER;
+static __thread char match_data_initialized;
+
 char const *regex_arch_string(void)
 {
 	static char arch_string_buffer[32];
@@ -60,14 +65,6 @@ char const *regex_arch_string(void)
 
 struct regex_data {
 	pcre2_code *regex; /* compiled regular expression */
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	/*
-	 * match data block required for the compiled
-	 * pattern in pcre2
-	 */
-	pcre2_match_data *match_data;
-#endif
-	pthread_mutex_t match_mutex;
 };
 
 int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
@@ -86,13 +83,6 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
 		goto err;
 	}
 
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	(*regex)->match_data =
-	    pcre2_match_data_create_from_pattern((*regex)->regex, NULL);
-	if (!(*regex)->match_data) {
-		goto err;
-	}
-#endif
 	return 0;
 
 err:
@@ -142,13 +132,6 @@ int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex,
 		if (rc != 1)
 			goto err;
 
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-		(*regex)->match_data =
-		    pcre2_match_data_create_from_pattern((*regex)->regex, NULL);
-		if (!(*regex)->match_data)
-			goto err;
-#endif
-
 		*regex_compiled = true;
 	}
 
@@ -204,18 +187,32 @@ out:
 	return rc;
 }
 
+static void __attribute__((destructor)) match_data_thread_free(void *key)
+{
+	void *value;
+	pcre2_match_data *match_data;
+
+	if (match_data_key_initialized <= 0 || !match_data_initialized)
+		return;
+
+	value = __selinux_getspecific(match_data_key);
+	match_data = value ? value : key;
+
+	pcre2_match_data_free(match_data);
+
+	__pthread_mutex_lock(&key_mutex);
+	if (--match_data_key_initialized == 1) {
+		__selinux_key_delete(match_data_key);
+		match_data_key_initialized = -1;
+	}
+	__pthread_mutex_unlock(&key_mutex);
+}
+
 void regex_data_free(struct regex_data *regex)
 {
 	if (regex) {
 		if (regex->regex)
 			pcre2_code_free(regex->regex);
-
-#ifndef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-		if (regex->match_data)
-			pcre2_match_data_free(regex->match_data);
-#endif
-
-		__pthread_mutex_destroy(&regex->match_mutex);
 		free(regex);
 	}
 }
@@ -223,32 +220,40 @@ void regex_data_free(struct regex_data *regex)
 int regex_match(struct regex_data *regex, char const *subject, int partial)
 {
 	int rc;
-	pcre2_match_data *match_data;
-	__pthread_mutex_lock(&regex->match_mutex);
+	bool slow;
+	pcre2_match_data *match_data = NULL;
+
+	if (match_data_key_initialized > 0) {
+		if (match_data_initialized == 0) {
+			match_data = pcre2_match_data_create(1, NULL);
+			if (match_data) {
+				match_data_initialized = 1;
+				__selinux_setspecific(match_data_key,
+							match_data);
+				__pthread_mutex_lock(&key_mutex);
+				match_data_key_initialized++;
+				__pthread_mutex_unlock(&key_mutex);
+			}
+		} else
+			match_data = __selinux_getspecific(match_data_key);
+	}
 
-#ifdef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	match_data = pcre2_match_data_create_from_pattern(
-	    regex->regex, NULL);
-	if (match_data == NULL) {
-		__pthread_mutex_unlock(&regex->match_mutex);
-		return REGEX_ERROR;
+	slow = (match_data_key_initialized <= 0 || match_data == NULL);
+	if (slow) {
+		match_data = pcre2_match_data_create_from_pattern(regex->regex,
+									NULL);
+		if (!match_data)
+			return REGEX_ERROR;
 	}
-#else
-	match_data = regex->match_data;
-#endif
 
 	rc = pcre2_match(
 	    regex->regex, (PCRE2_SPTR)subject, PCRE2_ZERO_TERMINATED, 0,
 	    partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
 
-#ifdef AGGRESSIVE_FREE_AFTER_REGEX_MATCH
-	// pcre2_match allocates heap and it won't be freed until
-	// pcre2_match_data_free, resulting in heap overhead.
-	pcre2_match_data_free(match_data);
-#endif
+	if (slow)
+		pcre2_match_data_free(match_data);
 
-	__pthread_mutex_unlock(&regex->match_mutex);
-	if (rc > 0)
+	if (rc >= 0)
 		return REGEX_MATCH;
 	switch (rc) {
 	case PCRE2_ERROR_PARTIAL:
@@ -290,7 +295,14 @@ struct regex_data *regex_data_create(void)
 	if (!regex_data)
 		return NULL;
 
-	__pthread_mutex_init(&regex_data->match_mutex, NULL);
+	__pthread_mutex_lock(&key_mutex);
+	if (match_data_key_initialized < 0) {
+		match_data_key_initialized = !__selinux_key_create(
+							&match_data_key,
+							match_data_thread_free);
+	}
+	__pthread_mutex_unlock(&key_mutex);
+
 	return regex_data;
 }
 
libselinux/src/selinux_internal.h +4 -0
index 06f2c038..d1e6c50f 100644
@@ -13,6 +13,7 @@ extern int selinux_page_size ;
 #pragma weak pthread_key_create
 #pragma weak pthread_key_delete
 #pragma weak pthread_setspecific
+#pragma weak pthread_getspecific
 
 /* Call handler iff the first call.  */
 #define __selinux_once(ONCE_CONTROL, INIT_FUNCTION)	\
@@ -41,6 +42,9 @@ extern int selinux_page_size ;
 			pthread_setspecific(KEY, VALUE);	\
 	} while (0)
 
+#define __selinux_getspecific(KEY)				\
+	(pthread_getspecific != NULL ? pthread_getspecific(KEY) : NULL)
+
 /* selabel_lookup() is only thread safe if we're compiled with pthreads */
 
 #pragma weak pthread_mutex_init
Signature · 1 line
2.39.0
#4 lore raw

Hi Carlo,

Quoted 26 lines · Carlo Marcelo Arenas Belón

Since 30b3e9d2 (libselinux: Workaround for heap overhead of pcre, 2023-01-12), performance of PCRE2 matches has been affected due to excesive recreation of the match_data in an attempt to reduce memory utilization; instead of a workaround, it would be better to address the problem and maybe even improve performance in the process.

The issue is that currently the structure that holds PCRE state has both a pcre2_code (which is per pattern) and a pcre2_match_data (which is per match), forcing us to add a mutex to prevent multiple matches to step on each other.

Lets remove the match_data and the mutex and instead allocate one once in a thread independent way that could be used and reused, by extending our pthread interface to not only store TLS variables but also retrieve them, and then use one of those.

Since we are not interested on the capture groups (if any) lets only allocate 1 pair which is all that will be needed and change the logic so that a return of 0 (which means the pattern matched but there were not enough capture spots) is also considered a match.

This will ensure that the memory use would be bound to the number of concurrent matches instead of the number of patterns and therefore reduce the impact that recent changes on the way that the frames used for matching are allocated might had brough since 10.41 was released.

Quoted 9 lines

For cases where threads are not available, just keep it working in slow mode as done before the workaround was reverted.

---
 libselinux/src/regex.c            | 108 +++++++++++++++++-------------
 libselinux/src/selinux_internal.h |   4 ++
 2 files changed, 64 insertions(+), 48 deletions(-)

Thanks for writing this patch! I notice it hasn't been applied upstream. Is this still being worked on?

A couple comments below:

Quoted 10 lines
libselinux/src/regex.c +10 -0
+static void __attribute__((destructor)) match_data_thread_free(void *key)
+{
+	void *value;
+	pcre2_match_data *match_data;
+
+	if (match_data_key_initialized <= 0 || !match_data_initialized)
+		return;
+
+	value = __selinux_getspecific(match_data_key);
+	match_data = value ? value : key;

libselinux/src/regex.c out: · line 209

Quoted 10 lines
libselinux/src/regex.c +10 -0
+
+	pcre2_match_data_free(match_data);
+
+	__pthread_mutex_lock(&key_mutex);
+	if (--match_data_key_initialized == 1) {
+		__selinux_key_delete(match_data_key);
+		match_data_key_initialized = -1;
+	}
+	__pthread_mutex_unlock(&key_mutex);
+}

This function is used as a pthread_key destructor. But, it's also marked with __attribute__((destructor)), making it a library-level destructor too. That's confusing. Is there a reason for that? I would have expected these to be two different functions. The pthread_key destructor should call pcre2_match_data_free on the value passed as an argument, while the library-level destructor should delete match_data_key.

Quoted 12 lines

int regex_match(struct regex_data *regex, char const *subject, int partial) {

libselinux/src/regex.c +7 -2
 	int rc;
-	pcre2_match_data *match_data;
-	__pthread_mutex_lock(&regex->match_mutex);
+	bool slow;
+	pcre2_match_data *match_data = NULL;
+
+	if (match_data_key_initialized > 0) {
+		if (match_data_initialized == 0) {
+			match_data = pcre2_match_data_create(1, NULL);
+			if (match_data) {

libselinux/src/regex.c void regex_data_free(struct regex_data *regex) · line 239

Quoted 10 lines
libselinux/src/regex.c +10 -0
+				match_data_initialized = 1;
+				__selinux_setspecific(match_data_key,
+							match_data);
+				__pthread_mutex_lock(&key_mutex);
+				match_data_key_initialized++;
+				__pthread_mutex_unlock(&key_mutex);
+			}
+		} else
+			match_data = __selinux_getspecific(match_data_key);
+	}

Since match_data_key_initialized is checked without holding key_mutex, can't the above code race with the below code in match_data_thread_free?

        __pthread_mutex_lock(&key_mutex);
        if (--match_data_key_initialized == 1) {
                __selinux_key_delete(match_data_key);
                match_data_key_initialized = -1;
        }
        __pthread_mutex_unlock(&key_mutex);

Perhaps deleting the pthread_key is just not something that should be done at all?

- Eric

maintainer
#5 lore raw
Quoted 106 lines · Eric Biggers

Hi Carlo,

Quoted 26 lines · Carlo Marcelo Arenas Belón

Since 30b3e9d2 (libselinux: Workaround for heap overhead of pcre, 2023-01-12), performance of PCRE2 matches has been affected due to excesive recreation of the match_data in an attempt to reduce memory utilization; instead of a workaround, it would be better to address the problem and maybe even improve performance in the process.

The issue is that currently the structure that holds PCRE state has both a pcre2_code (which is per pattern) and a pcre2_match_data (which is per match), forcing us to add a mutex to prevent multiple matches to step on each other.

Lets remove the match_data and the mutex and instead allocate one once in a thread independent way that could be used and reused, by extending our pthread interface to not only store TLS variables but also retrieve them, and then use one of those.

Since we are not interested on the capture groups (if any) lets only allocate 1 pair which is all that will be needed and change the logic so that a return of 0 (which means the pattern matched but there were not enough capture spots) is also considered a match.

This will ensure that the memory use would be bound to the number of concurrent matches instead of the number of patterns and therefore reduce the impact that recent changes on the way that the frames used for matching are allocated might had brough since 10.41 was released.

Quoted 9 lines

For cases where threads are not available, just keep it working in slow mode as done before the workaround was reverted.

---
 libselinux/src/regex.c            | 108 +++++++++++++++++-------------
 libselinux/src/selinux_internal.h |   4 ++
 2 files changed, 64 insertions(+), 48 deletions(-)

Thanks for writing this patch! I notice it hasn't been applied upstream. Is this still being worked on?

A couple comments below:

Quoted 10 lines
libselinux/src/regex.c +10 -0
+static void __attribute__((destructor)) match_data_thread_free(void *key)
+{
+     void *value;
+     pcre2_match_data *match_data;
+
+     if (match_data_key_initialized <= 0 || !match_data_initialized)
+             return;
+
+     value = __selinux_getspecific(match_data_key);
+     match_data = value ? value : key;

libselinux/src/regex.c out: · line 209

Quoted 10 lines
libselinux/src/regex.c +10 -0
+
+     pcre2_match_data_free(match_data);
+
+     __pthread_mutex_lock(&key_mutex);
+     if (--match_data_key_initialized == 1) {
+             __selinux_key_delete(match_data_key);
+             match_data_key_initialized = -1;
+     }
+     __pthread_mutex_unlock(&key_mutex);
+}

This function is used as a pthread_key destructor. But, it's also marked with __attribute__((destructor)), making it a library-level destructor too. That's confusing. Is there a reason for that? I would have expected these to be two different functions. The pthread_key destructor should call pcre2_match_data_free on the value passed as an argument, while the library-level destructor should delete match_data_key.

Quoted 12 lines

int regex_match(struct regex_data *regex, char const *subject, int partial)

 {
      int rc;
libselinux/src/regex.c +7 -2
-     pcre2_match_data *match_data;
-     __pthread_mutex_lock(&regex->match_mutex);
+     bool slow;
+     pcre2_match_data *match_data = NULL;
+
+     if (match_data_key_initialized > 0) {
+             if (match_data_initialized == 0) {
+                     match_data = pcre2_match_data_create(1, NULL);
+                     if (match_data) {

libselinux/src/regex.c void regex_data_free(struct regex_data *regex) · line 239

Quoted 10 lines
libselinux/src/regex.c +10 -0
+                             match_data_initialized = 1;
+                             __selinux_setspecific(match_data_key,
+                                                     match_data);
+                             __pthread_mutex_lock(&key_mutex);
+                             match_data_key_initialized++;
+                             __pthread_mutex_unlock(&key_mutex);
+                     }
+             } else
+                     match_data = __selinux_getspecific(match_data_key);
+     }

Since match_data_key_initialized is checked without holding key_mutex, can't the above code race with the below code in match_data_thread_free?

        __pthread_mutex_lock(&key_mutex);
        if (--match_data_key_initialized == 1) {
                __selinux_key_delete(match_data_key);
                match_data_key_initialized = -1;
        }
        __pthread_mutex_unlock(&key_mutex);
Quoted 2 lines

Perhaps deleting the pthread_key is just not something that should be done at all?

This patch appears to have been applied to AOSP here:
https://android-review.googlesource.com/c/platform/external/selinux/+/2411194
but I didn't see any reply to Eric's comments above. Is this still being worked on?

#6 lore raw

PATCH 3/3 libselinux: use a static match_data if single threaded

In reply to Carlo Marcelo Arenas Belón

A previous patch reimplemented a single threaded fallback while keeping the memory usage low as was done originally by the workaround, but the resulting code is too slow.

To improve performance, while keeping memory in check, use instead a static match_data that gets reused for all queries and let its contents (including the heapframes) leak.

---
 libselinux/src/regex.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
libselinux/src/regex.c +9 -8
index 54f24026..a6b44023 100644
@@ -220,7 +220,8 @@ void regex_data_free(struct regex_data *regex)
 int regex_match(struct regex_data *regex, char const *subject, int partial)
 {
 	int rc;
-	bool slow;
+	bool single_threaded;
+	static pcre2_match_data *static_match_data;
 	pcre2_match_data *match_data = NULL;
 
 	if (match_data_key_initialized > 0) {
@@ -238,10 +239,13 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 			match_data = __selinux_getspecific(match_data_key);
 	}
 
-	slow = (match_data_key_initialized <= 0 || match_data == NULL);
-	if (slow) {
-		match_data = pcre2_match_data_create_from_pattern(regex->regex,
-									NULL);
+	single_threaded = (match_data_key_initialized <= 0 || !match_data);
+	if (single_threaded) {
+		if (!static_match_data && !match_data_initialized)
+			static_match_data = pcre2_match_data_create(1, NULL);
+
+		match_data_initialized = 1;
+		match_data = static_match_data;
 		if (!match_data)
 			return REGEX_ERROR;
 	}
@@ -250,9 +254,6 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 	    regex->regex, (PCRE2_SPTR)subject, PCRE2_ZERO_TERMINATED, 0,
 	    partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
 
-	if (slow)
-		pcre2_match_data_free(match_data);
-
 	if (rc >= 0)
 		return REGEX_MATCH;
 	switch (rc) {
Signature · 1 line
2.39.0
maintainer
#7 lore raw

Re: [PATCH 0/3] improve performance of pcre matches

In reply to Carlo Marcelo Arenas Belón
Quoted 23 lines · Carlo Marcelo Arenas Belón

The following series optimizes the way PCRE2 matches are done by using more efficiently the match_data resources and shows over 30% reduction in cpu utilization while only using 1/5 of the memory as shown in the bug report[1] that started it all.

The first patch is just an administrative change that I found useful while building and testing the code with a development version of PCRE2.

The second patch reverts the workaround merged in 30b3e9d2 (libselinux:
Workaround for heap overhead of pcre, 2023-01-12) while addressing the increased memory utilization that it was trying to prevent by changing the way the match_data required for each match was being handled.

The last patch changes the single threaded codepath to allow for a similar performance improvement done to the multithreaded codepath by intentionally leaking one match_data that could be reused for all matches.

Carlo Marcelo Arenas Belón (3):
scripts: respect an initial LD_LIBRARY_PATH with env_use_destdir
libselinux: improve performance with pcre matches
libselinux: use a static match_data if single threaded

Quoted 6 lines
 libselinux/src/regex.c            | 111 +++++++++++++++++-------------
 libselinux/src/selinux_internal.h |   4 ++
 scripts/env_use_destdir           |   8 ++-
 3 files changed, 73 insertions(+), 50 deletions(-)

[1] https://github.com/PCRE2Project/pcre2/issues/194

Sorry for the delay in responding. Looking at AOSP, I only see the 2nd patch in this series applied, not the third one. Checking to see whether the 3rd patch is still desired/needed.