Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mei: avoid iterator usage outside of list_for_each_entry

Usage of the iterator outside of the list_for_each_entry
is considered harmful. https://lkml.org/lkml/2022/2/17/1032

Do not reference the loop variable outside of the loop,
by rearranging the orders of execution.
Instead of performing search loop and checking outside the loop
if the end of the list was hit and no matching element was found,
the execution is performed inside the loop upon a successful match
followed by a goto statement to the next step,
therefore no condition has to be performed after the loop has ended.

Cc: <stable@vger.kernel.org>
Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com>
Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
Link: https://lore.kernel.org/r/20220308095926.300412-1-tomas.winkler@intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alexander Usyskin and committed by
Greg Kroah-Hartman
c10187b1 7bbbd084

+15 -20
+15 -20
drivers/misc/mei/interrupt.c
··· 424 424 list_for_each_entry(cl, &dev->file_list, link) { 425 425 if (mei_cl_hbm_equal(cl, mei_hdr)) { 426 426 cl_dbg(dev, cl, "got a message\n"); 427 - break; 427 + ret = mei_cl_irq_read_msg(cl, mei_hdr, meta_hdr, cmpl_list); 428 + goto reset_slots; 428 429 } 429 430 } 430 431 431 432 /* if no recipient cl was found we assume corrupted header */ 432 - if (&cl->link == &dev->file_list) { 433 - /* A message for not connected fixed address clients 434 - * should be silently discarded 435 - * On power down client may be force cleaned, 436 - * silently discard such messages 437 - */ 438 - if (hdr_is_fixed(mei_hdr) || 439 - dev->dev_state == MEI_DEV_POWER_DOWN) { 440 - mei_irq_discard_msg(dev, mei_hdr, mei_hdr->length); 441 - ret = 0; 442 - goto reset_slots; 443 - } 444 - dev_err(dev->dev, "no destination client found 0x%08X\n", 445 - dev->rd_msg_hdr[0]); 446 - ret = -EBADMSG; 447 - goto end; 433 + /* A message for not connected fixed address clients 434 + * should be silently discarded 435 + * On power down client may be force cleaned, 436 + * silently discard such messages 437 + */ 438 + if (hdr_is_fixed(mei_hdr) || 439 + dev->dev_state == MEI_DEV_POWER_DOWN) { 440 + mei_irq_discard_msg(dev, mei_hdr, mei_hdr->length); 441 + ret = 0; 442 + goto reset_slots; 448 443 } 449 - 450 - ret = mei_cl_irq_read_msg(cl, mei_hdr, meta_hdr, cmpl_list); 451 - 444 + dev_err(dev->dev, "no destination client found 0x%08X\n", dev->rd_msg_hdr[0]); 445 + ret = -EBADMSG; 446 + goto end; 452 447 453 448 reset_slots: 454 449 /* reset the number of slots and header */