Skip to content

nxmutex replace nxsem when used as a lock#6965

Merged
masayuki2009 merged 3 commits into
apache:masterfrom
anjiahao1:nxmutex
Oct 17, 2022
Merged

nxmutex replace nxsem when used as a lock#6965
masayuki2009 merged 3 commits into
apache:masterfrom
anjiahao1:nxmutex

Conversation

@anjiahao1

@anjiahao1 anjiahao1 commented Aug 31, 2022

Copy link
Copy Markdown
Contributor

Signed-off-by: anjiahao anjiahao@xiaomi.com

Summary

pull5070 is the first step, this is step 2, then change sem default behavior

Impact

schedule

Testing

CI

@anjiahao1

Copy link
Copy Markdown
Contributor Author

image
this error can't fix

@anjiahao1 anjiahao1 force-pushed the nxmutex branch 7 times, most recently from ef3fe2a to 6ce12d3 Compare August 31, 2022 14:14
Comment thread arch/arm/src/lpc17xx_40xx/lpc17_40_gpdma.c Outdated
Comment thread drivers/input/button_upper.c Outdated
Comment thread drivers/input/button_upper.c
Comment thread drivers/wireless/ieee80211/bcm43xxx/bcmf_driver.c Outdated
Comment thread fs/hostfs/hostfs.c Outdated
Comment thread net/local/local_connect.c
Comment thread net/local/local_connect.c Outdated
Comment thread include/nuttx/semaphore.h Outdated
Comment thread include/nuttx/semaphore.h Outdated
Comment thread libs/libc/libc.h
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@anjiahao1 please fix the warning:
https://github.com/apache/incubator-nuttx/runs/8127486548?check_suite_focus=true
and fix the conflict.

@anjiahao1 anjiahao1 force-pushed the nxmutex branch 17 times, most recently from 9b179fd to e93f6a4 Compare September 7, 2022 03:43
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

The change is already simple and consistent now, it's very error prone and time consuming to split the change into two commit again. @pkarashchenko and I already spend a lot of time to review and refine the change. Please respect our work, especially many code is vendor specific. I already spend many time to improve the code that I don't really use. If you insist I have to drop all code that I don't care about from this patch.

@xiaoxiang781216 @pkarashchenko Thank you for your comments and efforts so far. OK, I understand the current situation. However, when you create a new PR next time, please consider dividing a commit into smaller ones that are independent. Reviewing each commit file will be much easier to find any simple mistake.

Sure, thanks for suggestion.

@jerpelea

Copy link
Copy Markdown
Contributor

The change is already simple and consistent now, it's very error prone and time consuming to split the change into two commit again. @pkarashchenko and I already spend a lot of time to review and refine the change. Please respect our work, especially many code is vendor specific. I already spend many time to improve the code that I don't really use. If you insist I have to drop all code that I don't care about from this patch.

@xiaoxiang781216 @pkarashchenko Thank you for your comments and efforts so far. OK, I understand the current situation. However, when you create a new PR next time, please consider dividing a commit into smaller ones that are independent. Reviewing each commit file will be much easier to find any simple mistake.

Sure, thanks for suggestion.

considering the current PR and the work spent on it I think that we should do our best to merge it then we should think how we can avoid this situation by working with smaller PR.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@pkarashchenko @masayuki2009 @jerpelea all comment get addressed, please review again.

@masayuki2009

Copy link
Copy Markdown
Contributor

@pkarashchenko @masayuki2009 @jerpelea all comment get addressed, please review again.

@xiaoxiang781216

I think it would be better to merge the following style change in the second commit into the first commit because the first commit also changes the lines.

diff --git a/arch/arm/src/cxd56xx/cxd56_udmac.c b/arch/arm/src/cxd56xx/cxd56_udmac.c
index 69d2c87aa0..6a490e9cd0 100644
--- a/arch/arm/src/cxd56xx/cxd56_udmac.c
+++ b/arch/arm/src/cxd56xx/cxd56_udmac.c
@@ -68,8 +68,8 @@ struct dma_channel_s

 struct dma_controller_s
 {
-  mutex_t lock;     /* Protects channel table */
-  sem_t chansem;    /* Count of free channels */
+  mutex_t lock;                 /* Protects channel table */
+  sem_t chansem;                /* Count of free channels */
 };

Also, I can see similar changes in the second commit.

diff --git a/arch/arm/src/rp2040/rp2040_dmac.c b/arch/arm/src/rp2040/rp2040_dmac.c
index 1604c9d829..7d54c70124 100644
--- a/arch/arm/src/rp2040/rp2040_dmac.c
+++ b/arch/arm/src/rp2040/rp2040_dmac.c
@@ -62,8 +62,8 @@ struct dma_channel_s

 struct dma_controller_s
 {
-  mutex_t lock;     /* Protects channel table */
-  sem_t chansem;    /* Count of free channels */
+  mutex_t lock;                 /* Protects channel table */
+  sem_t chansem;                /* Count of free channels */
 };

@masayuki2009

Copy link
Copy Markdown
Contributor

@xiaoxiang781216

The latest code passed my automatic tests.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@pkarashchenko @masayuki2009 @jerpelea all comment get addressed, please review again.

@xiaoxiang781216

I think it would be better to merge the following style change in the second commit into the first commit because the first commit also changes the lines.

diff --git a/arch/arm/src/cxd56xx/cxd56_udmac.c b/arch/arm/src/cxd56xx/cxd56_udmac.c
index 69d2c87aa0..6a490e9cd0 100644
--- a/arch/arm/src/cxd56xx/cxd56_udmac.c
+++ b/arch/arm/src/cxd56xx/cxd56_udmac.c
@@ -68,8 +68,8 @@ struct dma_channel_s

 struct dma_controller_s
 {
-  mutex_t lock;     /* Protects channel table */
-  sem_t chansem;    /* Count of free channels */
+  mutex_t lock;                 /* Protects channel table */
+  sem_t chansem;                /* Count of free channels */
 };

Also, I can see similar changes in the second commit.

diff --git a/arch/arm/src/rp2040/rp2040_dmac.c b/arch/arm/src/rp2040/rp2040_dmac.c
index 1604c9d829..7d54c70124 100644
--- a/arch/arm/src/rp2040/rp2040_dmac.c
+++ b/arch/arm/src/rp2040/rp2040_dmac.c
@@ -62,8 +62,8 @@ struct dma_channel_s

 struct dma_controller_s
 {
-  mutex_t lock;     /* Protects channel table */
-  sem_t chansem;    /* Count of free channels */
+  mutex_t lock;                 /* Protects channel table */
+  sem_t chansem;                /* Count of free channels */
 };

Done, move to the first patch.

@pkarashchenko pkarashchenko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

570 files reviewed

Comment thread arch/arm/src/stm32h7/stm32_flash.c Outdated
Comment thread arch/arm/src/stm32l4/stm32l4_i2c.c Outdated
{
irqstate_t irqs;

struct stm32l4_i2c_priv_s *priv = ((struct stm32l4_i2c_inst_s *)dev)->priv;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix again. Here and all similar places. Move after DEBUGASSERT(dev);

Comment thread drivers/mtd/mtd_config.c
Comment thread drivers/pipes/pipe.c Outdated
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@pkarashchenko do you have more comment for this patch?

@pkarashchenko

Copy link
Copy Markdown
Contributor

@pkarashchenko do you have more comment for this patch?

Please give me 1 more day to take a final look. Then we can move forward

@pkarashchenko pkarashchenko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new batch of comments mostly related to inconsistency during initialization. I do not expect that all will be changed in this PR, but rather use this review as an opportunity to highlight the things.
All major comments like missing of = NXMUTEX_INITIALIZER; or double free I'm expecting to be fixed of course. Or maybe a separate PR should be submitted for double free fix and some other critical issues, I'm not sure.
@masayuki2009 what do you think?

Comment thread drivers/sensors/qencoder.c
Comment thread drivers/sensors/mlx90393.c
Comment thread drivers/sensors/adxl345_base.c
Comment thread drivers/sensors/adxl372.c
Comment thread drivers/sensors/aht10.c
Comment thread libs/libc/wqueue/work_usrthread.c
Comment thread wireless/bluetooth/bt_ioctl.c
Comment thread wireless/ieee802154/mac802154_device.c
Comment thread wireless/ieee802154/mac802154_netdev.c
Comment thread wireless/pktradio/pktradio_metadata.c

@pkarashchenko pkarashchenko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few last changes

Comment thread drivers/input/stmpe811_base.c
Comment thread drivers/lcd/ht16k33_14seg.c Outdated
Comment thread drivers/lcd/st7032.c Outdated
@@ -67,7 +68,7 @@ struct st7032_dev_s
uint8_t col; /* Current col position to write on display */
uint8_t buffer[ST7032_MAX_ROW * ST7032_MAX_COL];
bool pendscroll;
sem_t sem_excl;
mutex_t lock;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional

Comment thread drivers/syslog/ramlog.c Outdated
Comment thread wireless/pktradio/pktradio_metadata.c
Comment thread drivers/sensors/lsm330_spi.c
anjiahao1 and others added 3 commits October 17, 2022 03:10
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
…zation

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
also correct the order to ensure the memory free is last step

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@pkarashchenko done.

@pkarashchenko pkarashchenko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@pkarashchenko @masayuki2009 could we merge this patch now?

@lupyuen

lupyuen commented Oct 26, 2022

Copy link
Copy Markdown
Member

Hi @anjiahao1 BL602 I2C Read seems to hang after this commit, I think it might be caused by the Semaphore Init Value.

Refer to the changes for arch/risc-v/src/bl602/bl602_i2c.c:

d1d4633#diff-b9e7242d24fc5f206885bdcd9fa90409e9c8356d041a968e786cfb8c8db32aeb

Previously the Semaphore sem_isr was set to Init Value 1:

static void bl602_i2c_sem_init(struct bl602_i2c_priv_s *priv)
{
  ...
  nxsem_init(&priv->sem_isr, 0, 1);

Now sem_isr is set to Init Value 0:

static struct bl602_i2c_priv_s bl602_i2c0_priv =
{
  ...
  .sem_isr  = SEM_INITIALIZER(0),

This causes nxsem_wait_uninterruptible to hang during I2C Transfer:

static int bl602_i2c_transfer(struct i2c_master_s *dev,
                              struct i2c_msg_s *   msgs,
                              int                      count)
{
  ...
  // Hangs here
  ret = nxsem_wait_uninterruptible(&priv->sem_isr);

When I changed sem_isr to Init Value 1:

  .sem_isr  = SEM_INITIALIZER(1),

The I2C Transfer now works OK. (Here's the log)

Can you verify if this fix SEM_INITIALIZER(1) is correct? Thanks :-)

@anjiahao1

Copy link
Copy Markdown
Contributor Author

Sorry, I'll check all the SEM modifications later.

Hi @anjiahao1 BL602 I2C Read seems to hang after this commit, I think it might be caused by the Semaphore Init Value.

Refer to the changes for arch/risc-v/src/bl602/bl602_i2c.c:

d1d4633#diff-b9e7242d24fc5f206885bdcd9fa90409e9c8356d041a968e786cfb8c8db32aeb

Previously the Semaphore sem_isr was set to Init Value 1:

static void bl602_i2c_sem_init(struct bl602_i2c_priv_s *priv)
{
  ...
  nxsem_init(&priv->sem_isr, 0, 1);

Now sem_isr is set to Init Value 0:

static struct bl602_i2c_priv_s bl602_i2c0_priv =
{
  ...
  .sem_isr  = SEM_INITIALIZER(0),

This causes nxsem_wait_uninterruptible to hang during I2C Transfer:

static int bl602_i2c_transfer(struct i2c_master_s *dev,
                              struct i2c_msg_s *   msgs,
                              int                      count)
{
  ...
  // Hangs here
  ret = nxsem_wait_uninterruptible(&priv->sem_isr);

When I changed sem_isr to Init Value 1:

  .sem_isr  = SEM_INITIALIZER(1),

The I2C Transfer now works OK. (Here's the log)

Can you verify if this fix SEM_INITIALIZER(1) is correct? Thanks :-)

Sorry, I'll check all the SEM modifications later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace all place which use semaphore as lock with mutex wrapper

6 participants