mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] list: add missing empty list check to list_cut_before()
@ 2026-09-16 10:00 Ziran Zhang
  2026-09-16 10:53 ` Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Ziran Zhang @ 2026-09-16 10:00 UTC (permalink / raw)
  To: Paul E . McKenney, David Howells, Jonathan Corbet,
	Jordan R Abrahams-Whitehead, Marco Elver, Nilay Shroff,
	Edward Cree, Andy Shevchenko, Simona Vetter
  Cc: linux-kernel, Ziran Zhang

list_cut_before() lacks the list_empty() guard present
in list_cut_position().

With an empty head and an entry not on the list, it
corrupts the list.

Add the missing check.  When head is empty, initialize
@list as empty, consistent with the existing
head->next == entry case.

Signed-off-by: Ziran Zhang <zhangcoder@yeah.net>
---
 include/linux/list.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/linux/list.h b/include/linux/list.h
index 77fb62f79928..4d2061d35beb 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -555,6 +555,11 @@ static inline void list_cut_before(struct list_head *list,
 				   struct list_head *head,
 				   struct list_head *entry)
 {
+	if (list_empty(head)) {
+		INIT_LIST_HEAD(list);
+		return;
+	}
+
 	if (head->next == entry) {
 		INIT_LIST_HEAD(list);
 		return;
-- 
2.51.0


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

* Re: [PATCH] list: add missing empty list check to list_cut_before()
  2026-09-16 10:00 [PATCH] list: add missing empty list check to list_cut_before() Ziran Zhang
@ 2026-09-16 10:53 ` Andy Shevchenko
  2026-09-16 11:11   ` Ziran Zhang
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-09-16 10:53 UTC (permalink / raw)
  To: Ziran Zhang
  Cc: Paul E . McKenney, David Howells, Jonathan Corbet,
	Jordan R Abrahams-Whitehead, Marco Elver, Nilay Shroff,
	Edward Cree, Simona Vetter, linux-kernel

On Wed, Sep 16, 2026 at 06:00:01PM +0800, Ziran Zhang wrote:
> list_cut_before() lacks the list_empty() guard present
> in list_cut_position().
> 
> With an empty head and an entry not on the list, it
> corrupts the list.

Is it IRL case or the code analysis? If the former, add necessary information
like hardware description (if applicable) and the important ~3-5 lines from
traceback (I assume you rung with CONFIG_LIST_DEBUG=y).

> Add the missing check.  When head is empty, initialize
> @list as empty, consistent with the existing
> head->next == entry case.

Nice! Where is the test case?

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH] list: add missing empty list check to list_cut_before()
  2026-09-16 10:53 ` Andy Shevchenko
@ 2026-09-16 11:11   ` Ziran Zhang
  2026-09-16 15:37     ` Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Ziran Zhang @ 2026-09-16 11:11 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Paul E . McKenney, David Howells, Jonathan Corbet,
	Jordan R Abrahams-Whitehead, Marco Elver, Nilay Shroff,
	Edward Cree, Simona Vetter, linux-kernel, Ziran Zhang

On Wed, 16 Sep 2026 13:53:56 +0300, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> Is it IRL case or the code analysis? If the former, add necessary information
> like hardware description (if applicable) and the important ~3-5 lines from
> traceback (I assume you rung with CONFIG_LIST_DEBUG=y).

It is code analysis, not an IRL case.  The motivation is consistency
with list_cut_position(), which already has the same guard.

> Nice! Where is the test case?

There is no test case.  If a test is required for this change, I can
try to add one.  I wanted to first check whether the consistency
argument alone is acceptable.

Thanks,
Ziran Zhang


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

* Re: [PATCH] list: add missing empty list check to list_cut_before()
  2026-09-16 11:11   ` Ziran Zhang
@ 2026-09-16 15:37     ` Andy Shevchenko
  2026-09-16 15:52       ` Ziran Zhang
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-09-16 15:37 UTC (permalink / raw)
  To: Ziran Zhang
  Cc: Paul E . McKenney, David Howells, Jonathan Corbet,
	Jordan R Abrahams-Whitehead, Marco Elver, Nilay Shroff,
	Edward Cree, Simona Vetter, linux-kernel

On Wed, Sep 16, 2026 at 07:11:12PM +0800, Ziran Zhang wrote:
> On Wed, 16 Sep 2026 13:53:56 +0300, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > Is it IRL case or the code analysis? If the former, add necessary information
> > like hardware description (if applicable) and the important ~3-5 lines from
> > traceback (I assume you rung with CONFIG_LIST_DEBUG=y).
> 
> It is code analysis, not an IRL case.  The motivation is consistency
> with list_cut_position(), which already has the same guard.
> 
> > Nice! Where is the test case?
> 
> There is no test case.  If a test is required for this change, I can
> try to add one.  I wanted to first check whether the consistency
> argument alone is acceptable.

Any new code to lib must be accompanied with a test case(s) as documented [1].
Look at lib/tests/, there is already a set of them.

[1] 0a83293322fd ("doc: development-process: add notice on testing")

-- 
With Best Regards,
Andy Shevchenko



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

* Re: [PATCH] list: add missing empty list check to list_cut_before()
  2026-09-16 15:37     ` Andy Shevchenko
@ 2026-09-16 15:52       ` Ziran Zhang
  0 siblings, 0 replies; 5+ messages in thread
From: Ziran Zhang @ 2026-09-16 15:52 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Paul E . McKenney, David Howells, Jonathan Corbet,
	Jordan R Abrahams-Whitehead, Marco Elver, Nilay Shroff,
	Edward Cree, Simona Vetter, linux-kernel, Ziran Zhang

On Wed, 16 Sep 2026 18:37:30 +0300, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> Any new code to lib must be accompanied with a test case(s) as documented [1].
> Look at lib/tests/, there is already a set of them.
> 
> [1] 0a83293322fd ("doc: development-process: add notice on testing")

Thanks for the suggestion! I will add a KUnit test case and send the v2 patches
in a few days.

Thanks,
Ziran Zhang


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

end of thread, other threads:[~2026-09-16 15:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-16 10:00 [PATCH] list: add missing empty list check to list_cut_before() Ziran Zhang
2026-09-16 10:53 ` Andy Shevchenko
2026-09-16 11:11   ` Ziran Zhang
2026-09-16 15:37     ` Andy Shevchenko
2026-09-16 15:52       ` Ziran Zhang

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

all inboxes | Powered by JetHome®