count number of loaded objects in each namespace only once

Message ID 20160724122420.GJ1018@scylladb.com
State New, archived
Headers

Commit Message

Gleb Natapov July 24, 2016, 12:24 p.m. UTC
  _ns_nloaded already has count of all objects in a namespace, no need to
add it while iterating over all objects.

--
			Gleb.
  

Comments

Gleb Natapov Aug. 1, 2016, 8:50 a.m. UTC | #1
Ping?

On Sun, Jul 24, 2016 at 03:24:20PM +0300, Gleb Natapov wrote:
> _ns_nloaded already has count of all objects in a namespace, no need to
> add it while iterating over all objects.
> 
> diff --git a/elf/dl-iteratephdr.c b/elf/dl-iteratephdr.c
> index 1cb6e26..4db6938 100644
> --- a/elf/dl-iteratephdr.c
> +++ b/elf/dl-iteratephdr.c
> @@ -47,18 +47,18 @@ __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
>    Lmid_t ns = 0;
>  #ifdef SHARED
>    const void *caller = RETURN_ADDRESS (0);
> -  for (Lmid_t cnt = GL(dl_nns) - 1; cnt > 0; --cnt)
> +  for (Lmid_t cnt = GL(dl_nns) - 1; cnt > 0; --cnt) {
> +    /* We have to count the total number of loaded objects.  */
> +    nloaded += GL(dl_ns)[cnt]._ns_nloaded;
>      for (struct link_map *l = GL(dl_ns)[cnt]._ns_loaded; l; l = l->l_next)
>        {
> -	/* We have to count the total number of loaded objects.  */
> -	nloaded += GL(dl_ns)[cnt]._ns_nloaded;
> -
>  	if (caller >= (const void *) l->l_map_start
>  	    && caller < (const void *) l->l_map_end
>  	    && (l->l_contiguous
>  		|| _dl_addr_inside_object (l, (ElfW(Addr)) caller)))
>  	  ns = cnt;
>        }
> +  }
>  #endif
>  
>    for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
> --
> 			Gleb.

--
			Gleb.
  
Florian Weimer Aug. 1, 2016, 9:09 a.m. UTC | #2
On 07/24/2016 02:24 PM, Gleb Natapov wrote:
> _ns_nloaded already has count of all objects in a namespace, no need to
> add it while iterating over all objects.

I think this needs a test case to show the difference in behavior and 
make sure we do not regress.

Thanks,
Florian
  
Gleb Natapov Aug. 1, 2016, 9:33 a.m. UTC | #3
On Mon, Aug 01, 2016 at 11:09:29AM +0200, Florian Weimer wrote:
> On 07/24/2016 02:24 PM, Gleb Natapov wrote:
> > _ns_nloaded already has count of all objects in a namespace, no need to
> > add it while iterating over all objects.
> 
> I think this needs a test case to show the difference in behavior and make
> sure we do not regress.
> 
OK. I had a reproducer (very ad hoc one). Will try to make it more
suitable for mainline.

--
			Gleb.
  
Gleb Natapov Aug. 1, 2016, 12:20 p.m. UTC | #4
On Mon, Aug 01, 2016 at 12:33:34PM +0300, Gleb Natapov wrote:
> On Mon, Aug 01, 2016 at 11:09:29AM +0200, Florian Weimer wrote:
> > On 07/24/2016 02:24 PM, Gleb Natapov wrote:
> > > _ns_nloaded already has count of all objects in a namespace, no need to
> > > add it while iterating over all objects.
> > 
> > I think this needs a test case to show the difference in behavior and make
> > sure we do not regress.
> > 
> OK. I had a reproducer (very ad hoc one). Will try to make it more
> suitable for mainline.
> 
I've just submitted the patch "add test for correct calculation of dl_phdr_info.dlpi_subs"

--
			Gleb.
  
Mike Frysinger Aug. 2, 2016, 4:08 p.m. UTC | #5
On 24 Jul 2016 15:24, Gleb Natapov wrote:
> --- a/elf/dl-iteratephdr.c
> +++ b/elf/dl-iteratephdr.c
> @@ -47,18 +47,18 @@ __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
>    Lmid_t ns = 0;
>  #ifdef SHARED
>    const void *caller = RETURN_ADDRESS (0);
> -  for (Lmid_t cnt = GL(dl_nns) - 1; cnt > 0; --cnt)
> +  for (Lmid_t cnt = GL(dl_nns) - 1; cnt > 0; --cnt) {

GNU style puts the brace on the next line all by itself
-mike
  

Patch

diff --git a/elf/dl-iteratephdr.c b/elf/dl-iteratephdr.c
index 1cb6e26..4db6938 100644
--- a/elf/dl-iteratephdr.c
+++ b/elf/dl-iteratephdr.c
@@ -47,18 +47,18 @@  __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
   Lmid_t ns = 0;
 #ifdef SHARED
   const void *caller = RETURN_ADDRESS (0);
-  for (Lmid_t cnt = GL(dl_nns) - 1; cnt > 0; --cnt)
+  for (Lmid_t cnt = GL(dl_nns) - 1; cnt > 0; --cnt) {
+    /* We have to count the total number of loaded objects.  */
+    nloaded += GL(dl_ns)[cnt]._ns_nloaded;
     for (struct link_map *l = GL(dl_ns)[cnt]._ns_loaded; l; l = l->l_next)
       {
-	/* We have to count the total number of loaded objects.  */
-	nloaded += GL(dl_ns)[cnt]._ns_nloaded;
-
 	if (caller >= (const void *) l->l_map_start
 	    && caller < (const void *) l->l_map_end
 	    && (l->l_contiguous
 		|| _dl_addr_inside_object (l, (ElfW(Addr)) caller)))
 	  ns = cnt;
       }
+  }
 #endif
 
   for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)