From e1512566e322eb1fbc8f5d5997f9bfcd022feac7 Mon Sep 17 00:00:00 2001 From: Kiita Date: Tue, 9 Nov 2021 16:31:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3dmesg=20-s=E5=8F=82?= =?UTF-8?q?=E6=95=B0double=20lock=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【背景】自研shell或者mksh拉起后使用dmesg -s命令出现自旋锁double lock的问题。 【修改方案】 dmesg -s参数设置dmesg缓冲区过程需要访问UartOutput所访问的全局缓冲区,这意味着两个功能模块 使用了同一把自旋锁,若在dmesg命令执行过程使用了打印,则就可能会导致double lock。因此拆分 了dmesg -s命令过程中自旋锁的使用区域,避开内核中必要的打印。 re #I4HIJK Signed-off-by: yansira Change-Id: Iad74c058c9a8090fd3d9f338caab7d8f2170f9ac --- shell/full/src/cmds/dmesg.c | 49 ++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/shell/full/src/cmds/dmesg.c b/shell/full/src/cmds/dmesg.c index 0892f169..619a65ab 100644 --- a/shell/full/src/cmds/dmesg.c +++ b/shell/full/src/cmds/dmesg.c @@ -177,13 +177,17 @@ STATIC INT32 OsCopyToNew(const VOID *addr, UINT32 size) CHAR *newBuf = (CHAR *)addr + sizeof(DmesgInfo); UINT32 bufSize = size - sizeof(DmesgInfo); INT32 ret; + UINT32 intSave; + LOS_SpinLockSave(&g_dmesgSpin, &intSave); if (g_dmesgInfo->logSize == 0) { + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); return 0; } temp = (CHAR *)malloc(g_dmesgInfo->logSize); if (temp == NULL) { + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); return -1; } @@ -195,21 +199,24 @@ STATIC INT32 OsCopyToNew(const VOID *addr, UINT32 size) ret = OsDmesgRead(temp, g_dmesgInfo->logSize); if (ret <= 0) { - PRINT_ERR("%s,%d failed, err:%d!\n", __FUNCTION__, __LINE__, ret); - free(temp); - return -1; + goto FREE_OUT; } /* if new buf size smaller than logSize */ ret = memcpy_s(newBuf, bufSize, temp + copyStart, copyLen); if (ret != EOK) { - PRINT_ERR("%s,%d memcpy_s failed, err:%d!\n", __FUNCTION__, __LINE__, ret); - free(temp); - return -1; + goto FREE_OUT; } + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); free(temp); return (INT32)copyLen; + +FREE_OUT: + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); + PRINT_ERR("%s,%d failed, err:%d!\n", __FUNCTION__, __LINE__, ret); + free(temp); + return -1; } STATIC UINT32 OsDmesgResetMem(const VOID *addr, UINT32 size) @@ -224,12 +231,13 @@ STATIC UINT32 OsDmesgResetMem(const VOID *addr, UINT32 size) LOS_SpinLockSave(&g_dmesgSpin, &intSave); temp = g_dmesgInfo; + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); copyLen = OsCopyToNew(addr, size); if (copyLen < 0) { - LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); return LOS_NOK; } + LOS_SpinLockSave(&g_dmesgSpin, &intSave); g_logBufSize = size - sizeof(DmesgInfo); g_dmesgInfo = (DmesgInfo *)addr; g_dmesgInfo->logBuf = (CHAR *)addr + sizeof(DmesgInfo); @@ -239,12 +247,17 @@ STATIC UINT32 OsDmesgResetMem(const VOID *addr, UINT32 size) /* if old mem came from malloc */ if (temp == g_mallocAddr) { - g_mallocAddr = NULL; - free(temp); + goto FREE_OUT; } LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); return LOS_OK; + +FREE_OUT: + g_mallocAddr = NULL; + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); + free(temp); + return LOS_OK; } STATIC UINT32 OsDmesgChangeSize(UINT32 size) @@ -265,14 +278,14 @@ STATIC UINT32 OsDmesgChangeSize(UINT32 size) LOS_SpinLockSave(&g_dmesgSpin, &intSave); temp = g_dmesgInfo; + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); copyLen = OsCopyToNew(newString, size + sizeof(DmesgInfo)); if (copyLen < 0) { - LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); - free(newString); - return LOS_NOK; + goto ERR_OUT; } + LOS_SpinLockSave(&g_dmesgSpin, &intSave); g_logBufSize = size; g_dmesgInfo = (DmesgInfo *)newString; g_dmesgInfo->logBuf = (CHAR *)newString + sizeof(DmesgInfo); @@ -281,13 +294,21 @@ STATIC UINT32 OsDmesgChangeSize(UINT32 size) g_dmesgInfo->logHead = 0; if (temp == g_mallocAddr) { - g_mallocAddr = NULL; - free(temp); + goto FREE_OUT; } g_mallocAddr = newString; LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); return LOS_OK; + +ERR_OUT: + free(newString); + return LOS_NOK; +FREE_OUT: + g_mallocAddr = newString; + LOS_SpinUnlockRestore(&g_dmesgSpin, intSave); + free(temp); + return LOS_OK; } UINT32 OsCheckConsoleLock(VOID) -- GitLab