Re: [Mesa-dev] [PATCH 3/3] gallium/hud: protect against and initialization race

2016-11-07 Thread Steven Toth
>> A humble ping on this and the two others.
>>
>> - Steve
>>
>
> Series looks OK to me.
>
> Reviewed-by: Brian Paul 
>
> Need me to push these for you?
>
> -Brian
>

That would be a helpful, yes please.

I think they fell through the cracks after the last round of reviews.

Thanks.

-- 
Steven Toth - Kernel Labs
http://www.kernellabs.com
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH 3/3] gallium/hud: protect against and initialization race

2016-11-07 Thread Brian Paul

On 11/07/2016 06:00 AM, Steven Toth wrote:

On Mon, Oct 24, 2016 at 10:10 AM, Steven Toth  wrote:

In the event that multiple threads attempt to install a graph
concurrently, protect the shared list.

Signed-off-by: Steven Toth 



A humble ping on this and the two others.

- Steve



Series looks OK to me.

Reviewed-by: Brian Paul 

Need me to push these for you?

-Brian

___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH 3/3] gallium/hud: protect against and initialization race

2016-11-07 Thread Steven Toth
On Mon, Oct 24, 2016 at 10:10 AM, Steven Toth  wrote:
> In the event that multiple threads attempt to install a graph
> concurrently, protect the shared list.
>
> Signed-off-by: Steven Toth 


A humble ping on this and the two others.

- Steve

-- 
Steven Toth - Kernel Labs
http://www.kernellabs.com

> ---
>  src/gallium/auxiliary/hud/hud_cpufreq.c  | 12 ++--
>  src/gallium/auxiliary/hud/hud_diskstat.c | 13 +++--
>  src/gallium/auxiliary/hud/hud_nic.c  | 12 ++--
>  src/gallium/auxiliary/hud/hud_sensors_temp.c | 12 ++--
>  4 files changed, 41 insertions(+), 8 deletions(-)
>
> diff --git a/src/gallium/auxiliary/hud/hud_cpufreq.c 
> b/src/gallium/auxiliary/hud/hud_cpufreq.c
> index e66c3e4..19a6f08 100644
> --- a/src/gallium/auxiliary/hud/hud_cpufreq.c
> +++ b/src/gallium/auxiliary/hud/hud_cpufreq.c
> @@ -36,6 +36,7 @@
>  #include "hud/hud_private.h"
>  #include "util/list.h"
>  #include "os/os_time.h"
> +#include "os/os_thread.h"
>  #include "util/u_memory.h"
>  #include 
>  #include 
> @@ -61,6 +62,7 @@ struct cpufreq_info
>
>  static int gcpufreq_count = 0;
>  static struct list_head gcpufreq_list;
> +pipe_static_mutex(gcpufreq_mutex);
>
>  static struct cpufreq_info *
>  find_cfi_by_index(int cpu_index, int mode)
> @@ -186,16 +188,21 @@ hud_get_num_cpufreq(bool displayhelp)
> int cpu_index;
>
> /* Return the number of CPU metrics we support. */
> -   if (gcpufreq_count)
> +   pipe_mutex_lock(gcpufreq_mutex);
> +   if (gcpufreq_count) {
> +  pipe_mutex_unlock(gcpufreq_mutex);
>return gcpufreq_count;
> +   }
>
> /* Scan /sys/devices.../cpu, for every object type we support, create
>  * and persist an object to represent its different metrics.
>  */
> list_inithead(_list);
> DIR *dir = opendir("/sys/devices/system/cpu");
> -   if (!dir)
> +   if (!dir) {
> +  pipe_mutex_unlock(gcpufreq_mutex);
>return 0;
> +   }
>
> while ((dp = readdir(dir)) != NULL) {
>
> @@ -239,6 +246,7 @@ hud_get_num_cpufreq(bool displayhelp)
>}
> }
>
> +   pipe_mutex_unlock(gcpufreq_mutex);
> return gcpufreq_count;
>  }
>
> diff --git a/src/gallium/auxiliary/hud/hud_diskstat.c 
> b/src/gallium/auxiliary/hud/hud_diskstat.c
> index d4306cd..af6e62d 100644
> --- a/src/gallium/auxiliary/hud/hud_diskstat.c
> +++ b/src/gallium/auxiliary/hud/hud_diskstat.c
> @@ -35,6 +35,7 @@
>  #include "hud/hud_private.h"
>  #include "util/list.h"
>  #include "os/os_time.h"
> +#include "os/os_thread.h"
>  #include "util/u_memory.h"
>  #include 
>  #include 
> @@ -81,6 +82,7 @@ struct diskstat_info
>   */
>  static int gdiskstat_count = 0;
>  static struct list_head gdiskstat_list;
> +pipe_static_mutex(gdiskstat_mutex);
>
>  static struct diskstat_info *
>  find_dsi_by_name(const char *n, int mode)
> @@ -244,16 +246,21 @@ hud_get_num_disks(bool displayhelp)
> char name[64];
>
> /* Return the number of block devices and partitions. */
> -   if (gdiskstat_count)
> +   pipe_mutex_lock(gdiskstat_mutex);
> +   if (gdiskstat_count) {
> +  pipe_mutex_unlock(gdiskstat_mutex);
>return gdiskstat_count;
> +   }
>
> /* Scan /sys/block, for every object type we support, create and
>  * persist an object to represent its different statistics.
>  */
> list_inithead(_list);
> DIR *dir = opendir("/sys/block/");
> -   if (!dir)
> +   if (!dir) {
> +  pipe_mutex_unlock(gdiskstat_mutex);
>return 0;
> +   }
>
> while ((dp = readdir(dir)) != NULL) {
>
> @@ -278,6 +285,7 @@ hud_get_num_disks(bool displayhelp)
>struct dirent *dpart;
>DIR *pdir = opendir(basename);
>if (!pdir) {
> + pipe_mutex_unlock(gdiskstat_mutex);
>   closedir(dir);
>   return 0;
>}
> @@ -312,6 +320,7 @@ hud_get_num_disks(bool displayhelp)
>   puts(line);
>}
> }
> +   pipe_mutex_unlock(gdiskstat_mutex);
>
> return gdiskstat_count;
>  }
> diff --git a/src/gallium/auxiliary/hud/hud_nic.c 
> b/src/gallium/auxiliary/hud/hud_nic.c
> index 2795c93..f9935de 100644
> --- a/src/gallium/auxiliary/hud/hud_nic.c
> +++ b/src/gallium/auxiliary/hud/hud_nic.c
> @@ -35,6 +35,7 @@
>  #include "hud/hud_private.h"
>  #include "util/list.h"
>  #include "os/os_time.h"
> +#include "os/os_thread.h"
>  #include "util/u_memory.h"
>  #include 
>  #include 
> @@ -66,6 +67,7 @@ struct nic_info
>   */
>  static int gnic_count = 0;
>  static struct list_head gnic_list;
> +pipe_static_mutex(gnic_mutex);
>
>  static struct nic_info *
>  find_nic_by_name(const char *n, int mode)
> @@ -329,16 +331,21 @@ hud_get_num_nics(bool displayhelp)
> char name[64];
>
> /* Return the number if network interfaces. */
> -   if (gnic_count)
> +   pipe_mutex_lock(gnic_mutex);
> +   if (gnic_count) {
> +  pipe_mutex_unlock(gnic_mutex);
>return gnic_count;
> +   }
>
> /* Scan /sys/block, for every object type we support, create and
>  * 

[Mesa-dev] [PATCH 3/3] gallium/hud: protect against and initialization race

2016-10-27 Thread Steven Toth
In the event that multiple threads attempt to install a graph
concurrently, protect the shared list.

Signed-off-by: Steven Toth 
---
 src/gallium/auxiliary/hud/hud_cpufreq.c  | 12 ++--
 src/gallium/auxiliary/hud/hud_diskstat.c | 13 +++--
 src/gallium/auxiliary/hud/hud_nic.c  | 12 ++--
 src/gallium/auxiliary/hud/hud_sensors_temp.c | 12 ++--
 4 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/src/gallium/auxiliary/hud/hud_cpufreq.c 
b/src/gallium/auxiliary/hud/hud_cpufreq.c
index e66c3e4..19a6f08 100644
--- a/src/gallium/auxiliary/hud/hud_cpufreq.c
+++ b/src/gallium/auxiliary/hud/hud_cpufreq.c
@@ -36,6 +36,7 @@
 #include "hud/hud_private.h"
 #include "util/list.h"
 #include "os/os_time.h"
+#include "os/os_thread.h"
 #include "util/u_memory.h"
 #include 
 #include 
@@ -61,6 +62,7 @@ struct cpufreq_info
 
 static int gcpufreq_count = 0;
 static struct list_head gcpufreq_list;
+pipe_static_mutex(gcpufreq_mutex);
 
 static struct cpufreq_info *
 find_cfi_by_index(int cpu_index, int mode)
@@ -186,16 +188,21 @@ hud_get_num_cpufreq(bool displayhelp)
int cpu_index;
 
/* Return the number of CPU metrics we support. */
-   if (gcpufreq_count)
+   pipe_mutex_lock(gcpufreq_mutex);
+   if (gcpufreq_count) {
+  pipe_mutex_unlock(gcpufreq_mutex);
   return gcpufreq_count;
+   }
 
/* Scan /sys/devices.../cpu, for every object type we support, create
 * and persist an object to represent its different metrics.
 */
list_inithead(_list);
DIR *dir = opendir("/sys/devices/system/cpu");
-   if (!dir)
+   if (!dir) {
+  pipe_mutex_unlock(gcpufreq_mutex);
   return 0;
+   }
 
while ((dp = readdir(dir)) != NULL) {
 
@@ -239,6 +246,7 @@ hud_get_num_cpufreq(bool displayhelp)
   }
}
 
+   pipe_mutex_unlock(gcpufreq_mutex);
return gcpufreq_count;
 }
 
diff --git a/src/gallium/auxiliary/hud/hud_diskstat.c 
b/src/gallium/auxiliary/hud/hud_diskstat.c
index d4306cd..af6e62d 100644
--- a/src/gallium/auxiliary/hud/hud_diskstat.c
+++ b/src/gallium/auxiliary/hud/hud_diskstat.c
@@ -35,6 +35,7 @@
 #include "hud/hud_private.h"
 #include "util/list.h"
 #include "os/os_time.h"
+#include "os/os_thread.h"
 #include "util/u_memory.h"
 #include 
 #include 
@@ -81,6 +82,7 @@ struct diskstat_info
  */
 static int gdiskstat_count = 0;
 static struct list_head gdiskstat_list;
+pipe_static_mutex(gdiskstat_mutex);
 
 static struct diskstat_info *
 find_dsi_by_name(const char *n, int mode)
@@ -244,16 +246,21 @@ hud_get_num_disks(bool displayhelp)
char name[64];
 
/* Return the number of block devices and partitions. */
-   if (gdiskstat_count)
+   pipe_mutex_lock(gdiskstat_mutex);
+   if (gdiskstat_count) {
+  pipe_mutex_unlock(gdiskstat_mutex);
   return gdiskstat_count;
+   }
 
/* Scan /sys/block, for every object type we support, create and
 * persist an object to represent its different statistics.
 */
list_inithead(_list);
DIR *dir = opendir("/sys/block/");
-   if (!dir)
+   if (!dir) {
+  pipe_mutex_unlock(gdiskstat_mutex);
   return 0;
+   }
 
while ((dp = readdir(dir)) != NULL) {
 
@@ -278,6 +285,7 @@ hud_get_num_disks(bool displayhelp)
   struct dirent *dpart;
   DIR *pdir = opendir(basename);
   if (!pdir) {
+ pipe_mutex_unlock(gdiskstat_mutex);
  closedir(dir);
  return 0;
   }
@@ -312,6 +320,7 @@ hud_get_num_disks(bool displayhelp)
  puts(line);
   }
}
+   pipe_mutex_unlock(gdiskstat_mutex);
 
return gdiskstat_count;
 }
diff --git a/src/gallium/auxiliary/hud/hud_nic.c 
b/src/gallium/auxiliary/hud/hud_nic.c
index 2795c93..f9935de 100644
--- a/src/gallium/auxiliary/hud/hud_nic.c
+++ b/src/gallium/auxiliary/hud/hud_nic.c
@@ -35,6 +35,7 @@
 #include "hud/hud_private.h"
 #include "util/list.h"
 #include "os/os_time.h"
+#include "os/os_thread.h"
 #include "util/u_memory.h"
 #include 
 #include 
@@ -66,6 +67,7 @@ struct nic_info
  */
 static int gnic_count = 0;
 static struct list_head gnic_list;
+pipe_static_mutex(gnic_mutex);
 
 static struct nic_info *
 find_nic_by_name(const char *n, int mode)
@@ -329,16 +331,21 @@ hud_get_num_nics(bool displayhelp)
char name[64];
 
/* Return the number if network interfaces. */
-   if (gnic_count)
+   pipe_mutex_lock(gnic_mutex);
+   if (gnic_count) {
+  pipe_mutex_unlock(gnic_mutex);
   return gnic_count;
+   }
 
/* Scan /sys/block, for every object type we support, create and
 * persist an object to represent its different statistics.
 */
list_inithead(_list);
DIR *dir = opendir("/sys/class/net/");
-   if (!dir)
+   if (!dir) {
+  pipe_mutex_unlock(gnic_mutex);
   return 0;
+   }
 
while ((dp = readdir(dir)) != NULL) {
 
@@ -412,6 +419,7 @@ hud_get_num_nics(bool displayhelp)
 
}
 
+   pipe_mutex_unlock(gnic_mutex);
return gnic_count;
 }
 
diff --git a/src/gallium/auxiliary/hud/hud_sensors_temp.c 

Re: [Mesa-dev] [PATCH 3/3] gallium/hud: protect against and initialization race

2016-10-24 Thread Nicolai Hähnle

On 21.10.2016 18:34, Eduardo Lima Mitev wrote:

On 10/20/2016 08:45 PM, Steven Toth wrote:

In the event that multiple threads attempt to install a graph
concurrently, protect the shared list.

Signed-off-by: Steven Toth 
---
 src/gallium/auxiliary/hud/hud_cpufreq.c  | 12 ++--
 src/gallium/auxiliary/hud/hud_diskstat.c | 13 +++--
 src/gallium/auxiliary/hud/hud_nic.c  | 12 ++--
 src/gallium/auxiliary/hud/hud_sensors_temp.c | 12 ++--
 4 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/src/gallium/auxiliary/hud/hud_cpufreq.c 
b/src/gallium/auxiliary/hud/hud_cpufreq.c
index e66c3e4..19a6f08 100644
--- a/src/gallium/auxiliary/hud/hud_cpufreq.c
+++ b/src/gallium/auxiliary/hud/hud_cpufreq.c
@@ -36,6 +36,7 @@
 #include "hud/hud_private.h"
 #include "util/list.h"
 #include "os/os_time.h"
+#include "os/os_thread.h"
 #include "util/u_memory.h"
 #include 
 #include 
@@ -61,6 +62,7 @@ struct cpufreq_info

 static int gcpufreq_count = 0;
 static struct list_head gcpufreq_list;
+pipe_static_mutex(gcpufreq_mutex);

 static struct cpufreq_info *
 find_cfi_by_index(int cpu_index, int mode)
@@ -186,16 +188,21 @@ hud_get_num_cpufreq(bool displayhelp)
int cpu_index;

/* Return the number of CPU metrics we support. */
-   if (gcpufreq_count)
+   pipe_mutex_lock(gcpufreq_mutex);
+   if (gcpufreq_count) {
+  pipe_mutex_unlock(gcpufreq_mutex);
   return gcpufreq_count;
+   }



Notice that this won't return the correct value. The value of
'gcpufreq_count' might have changed to zero between the unlock and the
return. Maybe what you want is to save the value in a temporary variable
inside the protected region, then return that temporary. Something like:

int tmp_count;
pipe_mutex_lock(gcpufreq_mutex);
if (gcpufreq_count) {
   tmp_count = gcpufreq_count;
   pipe_mutex_unlock(gcpufreq_mutex);
   return tmp_count;
}

Same comment applies to several other similar chunks in this patch.


I actually think the code is fine as is, because gcpufreq_count is only 
set _once_ by hud_get_num_cpufreq while holding the lock. So if we find 
that gcpufreq_count != 0 while holding the lock, we have a guarantee 
that nobody will ever change that variable again.


Nicolai
___
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev


Re: [Mesa-dev] [PATCH 3/3] gallium/hud: protect against and initialization race

2016-10-21 Thread Eduardo Lima Mitev
On 10/20/2016 08:45 PM, Steven Toth wrote:
> In the event that multiple threads attempt to install a graph
> concurrently, protect the shared list.
> 
> Signed-off-by: Steven Toth 
> ---
>  src/gallium/auxiliary/hud/hud_cpufreq.c  | 12 ++--
>  src/gallium/auxiliary/hud/hud_diskstat.c | 13 +++--
>  src/gallium/auxiliary/hud/hud_nic.c  | 12 ++--
>  src/gallium/auxiliary/hud/hud_sensors_temp.c | 12 ++--
>  4 files changed, 41 insertions(+), 8 deletions(-)
> 
> diff --git a/src/gallium/auxiliary/hud/hud_cpufreq.c 
> b/src/gallium/auxiliary/hud/hud_cpufreq.c
> index e66c3e4..19a6f08 100644
> --- a/src/gallium/auxiliary/hud/hud_cpufreq.c
> +++ b/src/gallium/auxiliary/hud/hud_cpufreq.c
> @@ -36,6 +36,7 @@
>  #include "hud/hud_private.h"
>  #include "util/list.h"
>  #include "os/os_time.h"
> +#include "os/os_thread.h"
>  #include "util/u_memory.h"
>  #include 
>  #include 
> @@ -61,6 +62,7 @@ struct cpufreq_info
>  
>  static int gcpufreq_count = 0;
>  static struct list_head gcpufreq_list;
> +pipe_static_mutex(gcpufreq_mutex);
>  
>  static struct cpufreq_info *
>  find_cfi_by_index(int cpu_index, int mode)
> @@ -186,16 +188,21 @@ hud_get_num_cpufreq(bool displayhelp)
> int cpu_index;
>  
> /* Return the number of CPU metrics we support. */
> -   if (gcpufreq_count)
> +   pipe_mutex_lock(gcpufreq_mutex);
> +   if (gcpufreq_count) {
> +  pipe_mutex_unlock(gcpufreq_mutex);
>return gcpufreq_count;
> +   }


Notice that this won't return the correct value. The value of
'gcpufreq_count' might have changed to zero between the unlock and the
return. Maybe what you want is to save the value in a temporary variable
inside the protected region, then return that temporary. Something like:

int tmp_count;
pipe_mutex_lock(gcpufreq_mutex);
if (gcpufreq_count) {
   tmp_count = gcpufreq_count;
   pipe_mutex_unlock(gcpufreq_mutex);
   return tmp_count;
}

Same comment applies to several other similar chunks in this patch.

>  
> /* Scan /sys/devices.../cpu, for every object type we support, create
>  * and persist an object to represent its different metrics.
>  */
> list_inithead(_list);
> DIR *dir = opendir("/sys/devices/system/cpu");
> -   if (!dir)
> +   if (!dir) {
> +  pipe_mutex_unlock(gcpufreq_mutex);
>return 0;
> +   }
>  
> while ((dp = readdir(dir)) != NULL) {
>  
> @@ -239,6 +246,7 @@ hud_get_num_cpufreq(bool displayhelp)
>}
> }
>  
> +   pipe_mutex_unlock(gcpufreq_mutex);
> return gcpufreq_count;
>  }
>  
> diff --git a/src/gallium/auxiliary/hud/hud_diskstat.c 
> b/src/gallium/auxiliary/hud/hud_diskstat.c
> index 5fada1f..f1cc2bb 100644
> --- a/src/gallium/auxiliary/hud/hud_diskstat.c
> +++ b/src/gallium/auxiliary/hud/hud_diskstat.c
> @@ -35,6 +35,7 @@
>  #include "hud/hud_private.h"
>  #include "util/list.h"
>  #include "os/os_time.h"
> +#include "os/os_thread.h"
>  #include "util/u_memory.h"
>  #include 
>  #include 
> @@ -81,6 +82,7 @@ struct diskstat_info
>   */
>  static int gdiskstat_count = 0;
>  static struct list_head gdiskstat_list;
> +pipe_static_mutex(gdiskstat_mutex);
>  
>  static struct diskstat_info *
>  find_dsi_by_name(const char *n, int mode)
> @@ -244,16 +246,21 @@ hud_get_num_disks(bool displayhelp)
> char name[64];
>  
> /* Return the number of block devices and partitions. */
> -   if (gdiskstat_count)
> +   pipe_mutex_lock(gdiskstat_mutex);
> +   if (gdiskstat_count) {
> +  pipe_mutex_unlock(gdiskstat_mutex);
>return gdiskstat_count;
> +   }
>  
> /* Scan /sys/block, for every object type we support, create and
>  * persist an object to represent its different statistics.
>  */
> list_inithead(_list);
> DIR *dir = opendir("/sys/block/");
> -   if (!dir)
> +   if (!dir) {
> +  pipe_mutex_unlock(gdiskstat_mutex);
>return 0;
> +   }
>  
> while ((dp = readdir(dir)) != NULL) {
>  
> @@ -278,6 +285,7 @@ hud_get_num_disks(bool displayhelp)
>struct dirent *dpart;
>DIR *pdir = opendir(basename);
>if (!pdir) {
> + pipe_mutex_unlock(gdiskstat_mutex);
>   close(dir);
>   return 0;
>}
> @@ -312,6 +320,7 @@ hud_get_num_disks(bool displayhelp)
>   puts(line);
>}
> }
> +   pipe_mutex_unlock(gdiskstat_mutex);
>  
> return gdiskstat_count;
>  }
> diff --git a/src/gallium/auxiliary/hud/hud_nic.c 
> b/src/gallium/auxiliary/hud/hud_nic.c
> index 2795c93..f9935de 100644
> --- a/src/gallium/auxiliary/hud/hud_nic.c
> +++ b/src/gallium/auxiliary/hud/hud_nic.c
> @@ -35,6 +35,7 @@
>  #include "hud/hud_private.h"
>  #include "util/list.h"
>  #include "os/os_time.h"
> +#include "os/os_thread.h"
>  #include "util/u_memory.h"
>  #include 
>  #include 
> @@ -66,6 +67,7 @@ struct nic_info
>   */
>  static int gnic_count = 0;
>  static struct list_head gnic_list;
> +pipe_static_mutex(gnic_mutex);
>  
>  static struct nic_info *
>  

[Mesa-dev] [PATCH 3/3] gallium/hud: protect against and initialization race

2016-10-20 Thread Steven Toth
In the event that multiple threads attempt to install a graph
concurrently, protect the shared list.

Signed-off-by: Steven Toth 
---
 src/gallium/auxiliary/hud/hud_cpufreq.c  | 12 ++--
 src/gallium/auxiliary/hud/hud_diskstat.c | 13 +++--
 src/gallium/auxiliary/hud/hud_nic.c  | 12 ++--
 src/gallium/auxiliary/hud/hud_sensors_temp.c | 12 ++--
 4 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/src/gallium/auxiliary/hud/hud_cpufreq.c 
b/src/gallium/auxiliary/hud/hud_cpufreq.c
index e66c3e4..19a6f08 100644
--- a/src/gallium/auxiliary/hud/hud_cpufreq.c
+++ b/src/gallium/auxiliary/hud/hud_cpufreq.c
@@ -36,6 +36,7 @@
 #include "hud/hud_private.h"
 #include "util/list.h"
 #include "os/os_time.h"
+#include "os/os_thread.h"
 #include "util/u_memory.h"
 #include 
 #include 
@@ -61,6 +62,7 @@ struct cpufreq_info
 
 static int gcpufreq_count = 0;
 static struct list_head gcpufreq_list;
+pipe_static_mutex(gcpufreq_mutex);
 
 static struct cpufreq_info *
 find_cfi_by_index(int cpu_index, int mode)
@@ -186,16 +188,21 @@ hud_get_num_cpufreq(bool displayhelp)
int cpu_index;
 
/* Return the number of CPU metrics we support. */
-   if (gcpufreq_count)
+   pipe_mutex_lock(gcpufreq_mutex);
+   if (gcpufreq_count) {
+  pipe_mutex_unlock(gcpufreq_mutex);
   return gcpufreq_count;
+   }
 
/* Scan /sys/devices.../cpu, for every object type we support, create
 * and persist an object to represent its different metrics.
 */
list_inithead(_list);
DIR *dir = opendir("/sys/devices/system/cpu");
-   if (!dir)
+   if (!dir) {
+  pipe_mutex_unlock(gcpufreq_mutex);
   return 0;
+   }
 
while ((dp = readdir(dir)) != NULL) {
 
@@ -239,6 +246,7 @@ hud_get_num_cpufreq(bool displayhelp)
   }
}
 
+   pipe_mutex_unlock(gcpufreq_mutex);
return gcpufreq_count;
 }
 
diff --git a/src/gallium/auxiliary/hud/hud_diskstat.c 
b/src/gallium/auxiliary/hud/hud_diskstat.c
index 5fada1f..f1cc2bb 100644
--- a/src/gallium/auxiliary/hud/hud_diskstat.c
+++ b/src/gallium/auxiliary/hud/hud_diskstat.c
@@ -35,6 +35,7 @@
 #include "hud/hud_private.h"
 #include "util/list.h"
 #include "os/os_time.h"
+#include "os/os_thread.h"
 #include "util/u_memory.h"
 #include 
 #include 
@@ -81,6 +82,7 @@ struct diskstat_info
  */
 static int gdiskstat_count = 0;
 static struct list_head gdiskstat_list;
+pipe_static_mutex(gdiskstat_mutex);
 
 static struct diskstat_info *
 find_dsi_by_name(const char *n, int mode)
@@ -244,16 +246,21 @@ hud_get_num_disks(bool displayhelp)
char name[64];
 
/* Return the number of block devices and partitions. */
-   if (gdiskstat_count)
+   pipe_mutex_lock(gdiskstat_mutex);
+   if (gdiskstat_count) {
+  pipe_mutex_unlock(gdiskstat_mutex);
   return gdiskstat_count;
+   }
 
/* Scan /sys/block, for every object type we support, create and
 * persist an object to represent its different statistics.
 */
list_inithead(_list);
DIR *dir = opendir("/sys/block/");
-   if (!dir)
+   if (!dir) {
+  pipe_mutex_unlock(gdiskstat_mutex);
   return 0;
+   }
 
while ((dp = readdir(dir)) != NULL) {
 
@@ -278,6 +285,7 @@ hud_get_num_disks(bool displayhelp)
   struct dirent *dpart;
   DIR *pdir = opendir(basename);
   if (!pdir) {
+ pipe_mutex_unlock(gdiskstat_mutex);
  close(dir);
  return 0;
   }
@@ -312,6 +320,7 @@ hud_get_num_disks(bool displayhelp)
  puts(line);
   }
}
+   pipe_mutex_unlock(gdiskstat_mutex);
 
return gdiskstat_count;
 }
diff --git a/src/gallium/auxiliary/hud/hud_nic.c 
b/src/gallium/auxiliary/hud/hud_nic.c
index 2795c93..f9935de 100644
--- a/src/gallium/auxiliary/hud/hud_nic.c
+++ b/src/gallium/auxiliary/hud/hud_nic.c
@@ -35,6 +35,7 @@
 #include "hud/hud_private.h"
 #include "util/list.h"
 #include "os/os_time.h"
+#include "os/os_thread.h"
 #include "util/u_memory.h"
 #include 
 #include 
@@ -66,6 +67,7 @@ struct nic_info
  */
 static int gnic_count = 0;
 static struct list_head gnic_list;
+pipe_static_mutex(gnic_mutex);
 
 static struct nic_info *
 find_nic_by_name(const char *n, int mode)
@@ -329,16 +331,21 @@ hud_get_num_nics(bool displayhelp)
char name[64];
 
/* Return the number if network interfaces. */
-   if (gnic_count)
+   pipe_mutex_lock(gnic_mutex);
+   if (gnic_count) {
+  pipe_mutex_unlock(gnic_mutex);
   return gnic_count;
+   }
 
/* Scan /sys/block, for every object type we support, create and
 * persist an object to represent its different statistics.
 */
list_inithead(_list);
DIR *dir = opendir("/sys/class/net/");
-   if (!dir)
+   if (!dir) {
+  pipe_mutex_unlock(gnic_mutex);
   return 0;
+   }
 
while ((dp = readdir(dir)) != NULL) {
 
@@ -412,6 +419,7 @@ hud_get_num_nics(bool displayhelp)
 
}
 
+   pipe_mutex_unlock(gnic_mutex);
return gnic_count;
 }
 
diff --git a/src/gallium/auxiliary/hud/hud_sensors_temp.c