public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 2/3] mm/kmemleak.c: Use list_for_each_entry_safe to reconstruct function scan_gray_list
@ 2013-05-14 11:49 majianpeng
  2013-05-30 14:40 ` Catalin Marinas
  0 siblings, 1 reply; 3+ messages in thread
From: majianpeng @ 2013-05-14 11:49 UTC (permalink / raw)
  To: Catalin Marinas; +Cc: linux-mm, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1108 bytes --]

Signed-off-by: Jianpeng Ma <majianpeng@gmail.com>
---
 mm/kmemleak.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index b1525db..f0ece93 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1225,22 +1225,16 @@ static void scan_gray_list(void)
      * from inside the loop. The kmemleak objects cannot be freed from
      * outside the loop because their use_count was incremented.
      */
-    object = list_entry(gray_list.next, typeof(*object), gray_list);
-    while (&object->gray_list != &gray_list) {
+    list_for_each_entry_safe(object, tmp, &gray_list, gray_list) {
         cond_resched();
 
         /* may add new objects to the list */
         if (!scan_should_stop())
             scan_object(object);
 
-        tmp = list_entry(object->gray_list.next, typeof(*object),
-                 gray_list);
-
         /* remove the object from the list and release it */
         list_del(&object->gray_list);
         put_object(object);
-
-        object = tmp;
     }
     WARN_ON(!list_empty(&gray_list));
 }
-- 
1.8.3.rc1.44.gb387c77


[-- Attachment #2: 0002-mm-kmemleak.c-Use-list_for_each_entry_safe-to-recons.patch --]
[-- Type: text/x-patch, Size: 1273 bytes --]

>From 095c9fc03bacb14a896cc9b56c7e2e3843c6051b Mon Sep 17 00:00:00 2001
From: Jianpeng Ma <majianpeng@gmail.com>
Date: Tue, 14 May 2013 15:36:14 +0800
Subject: [PATCH 2/3] mm/kmemleak.c: Use list_for_each_entry_safe to
 reconstruct function scan_gray_list.

Signed-off-by: Jianpeng Ma <majianpeng@gmail.com>
---
 mm/kmemleak.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index b1525db..f0ece93 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1225,22 +1225,16 @@ static void scan_gray_list(void)
 	 * from inside the loop. The kmemleak objects cannot be freed from
 	 * outside the loop because their use_count was incremented.
 	 */
-	object = list_entry(gray_list.next, typeof(*object), gray_list);
-	while (&object->gray_list != &gray_list) {
+	list_for_each_entry_safe(object, tmp, &gray_list, gray_list) {
 		cond_resched();
 
 		/* may add new objects to the list */
 		if (!scan_should_stop())
 			scan_object(object);
 
-		tmp = list_entry(object->gray_list.next, typeof(*object),
-				 gray_list);
-
 		/* remove the object from the list and release it */
 		list_del(&object->gray_list);
 		put_object(object);
-
-		object = tmp;
 	}
 	WARN_ON(!list_empty(&gray_list));
 }
-- 
1.8.3.rc1.44.gb387c77


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/3] mm/kmemleak.c: Use list_for_each_entry_safe to reconstruct function scan_gray_list
  2013-05-14 11:49 [PATCH 2/3] mm/kmemleak.c: Use list_for_each_entry_safe to reconstruct function scan_gray_list majianpeng
@ 2013-05-30 14:40 ` Catalin Marinas
  2013-06-03  0:58   ` majianpeng
  0 siblings, 1 reply; 3+ messages in thread
From: Catalin Marinas @ 2013-05-30 14:40 UTC (permalink / raw)
  To: majianpeng; +Cc: linux-mm, linux-kernel

On Tue, May 14, 2013 at 12:49:44PM +0100, majianpeng wrote:
> Signed-off-by: Jianpeng Ma <majianpeng@gmail.com>
> ---
>  mm/kmemleak.c | 8 +-------
>  1 file changed, 1 insertion(+), 7 deletions(-)
> 
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index b1525db..f0ece93 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1225,22 +1225,16 @@ static void scan_gray_list(void)
>  	 * from inside the loop. The kmemleak objects cannot be freed from
>  	 * outside the loop because their use_count was incremented.
>  	 */
> -	object = list_entry(gray_list.next, typeof(*object), gray_list);
> -	while (&object->gray_list != &gray_list) {
> +	list_for_each_entry_safe(object, tmp, &gray_list, gray_list) {
>  		cond_resched();
>  
>  		/* may add new objects to the list */
>  		if (!scan_should_stop())
>  			scan_object(object);
>  
> -		tmp = list_entry(object->gray_list.next, typeof(*object),
> -				 gray_list);
> -
>  		/* remove the object from the list and release it */
>  		list_del(&object->gray_list);
>  		put_object(object);
> -
> -		object = tmp;
>  	}
>  	WARN_ON(!list_empty(&gray_list));

I tried this patch for a few days and I hit the WARN_ON after the loop.
During scanning, new entries may be added at the end of the loop but we
need to loop until all the entries have been removed. I probably had a
reason why I had the 'while' loop.

The key difference is that list_for_each_entry_safe() gets the next
entry (n or tmp above) before scan_object() and it may hit the end of
the list. However, scan_object() may do a list_add_tail(&gray_list)
hence we need to get the next entry after this function.

Basically list_for_each_entry_safe() is not safe with tail additions.
I'll revert this patch (hasn't reached mainline anyway).

Thanks.

-- 
Catalin

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Re: [PATCH 2/3] mm/kmemleak.c: Use list_for_each_entry_safe to reconstruct function scan_gray_list
  2013-05-30 14:40 ` Catalin Marinas
@ 2013-06-03  0:58   ` majianpeng
  0 siblings, 0 replies; 3+ messages in thread
From: majianpeng @ 2013-06-03  0:58 UTC (permalink / raw)
  To: Catalin Marinas; +Cc: linux-mm, linux-kernel

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="gb2312", Size: 2004 bytes --]

>On Tue, May 14, 2013 at 12:49:44PM +0100, majianpeng wrote:
>> Signed-off-by: Jianpeng Ma <majianpeng@gmail.com>
>> ---
>>  mm/kmemleak.c | 8 +-------
>>  1 file changed, 1 insertion(+), 7 deletions(-)
>> 
>> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
>> index b1525db..f0ece93 100644
>> --- a/mm/kmemleak.c
>> +++ b/mm/kmemleak.c
>> @@ -1225,22 +1225,16 @@ static void scan_gray_list(void)
>>  	 * from inside the loop. The kmemleak objects cannot be freed from
>>  	 * outside the loop because their use_count was incremented.
>>  	 */
>> -	object = list_entry(gray_list.next, typeof(*object), gray_list);
>> -	while (&object->gray_list != &gray_list) {
>> +	list_for_each_entry_safe(object, tmp, &gray_list, gray_list) {
>>  		cond_resched();
>>  
>>  		/* may add new objects to the list */
>>  		if (!scan_should_stop())
>>  			scan_object(object);
>>  
>> -		tmp = list_entry(object->gray_list.next, typeof(*object),
>> -				 gray_list);
>> -
>>  		/* remove the object from the list and release it */
>>  		list_del(&object->gray_list);
>>  		put_object(object);
>> -
>> -		object = tmp;
>>  	}
>>  	WARN_ON(!list_empty(&gray_list));
>
>I tried this patch for a few days and I hit the WARN_ON after the loop.
>During scanning, new entries may be added at the end of the loop but we
>need to loop until all the entries have been removed. I probably had a
>reason why I had the 'while' loop.
>
>The key difference is that list_for_each_entry_safe() gets the next
>entry (n or tmp above) before scan_object() and it may hit the end of
>the list. However, scan_object() may do a list_add_tail(&gray_list)
>hence we need to get the next entry after this function.
>
>Basically list_for_each_entry_safe() is not safe with tail additions.
>I'll revert this patch (hasn't reached mainline anyway).
>
Ok, i see. 
Thanks!
>Thanks.
>
>-- 
>Catalinÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±þG«éÿŠ{ayº\x1dʇڙë,j\a­¢f£¢·hšïêÿ‘êçz_è®\x03(­éšŽŠÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?™¨è­Ú&£ø§~á¶iO•æ¬z·švØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?–I¥

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-06-03  0:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-14 11:49 [PATCH 2/3] mm/kmemleak.c: Use list_for_each_entry_safe to reconstruct function scan_gray_list majianpeng
2013-05-30 14:40 ` Catalin Marinas
2013-06-03  0:58   ` majianpeng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox