From patchwork Thu Apr 6 01:53:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 80030 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp727085vqo; Wed, 5 Apr 2023 19:38:19 -0700 (PDT) X-Google-Smtp-Source: AKy350bxAuYfVO1SfGCtyIUEQayoUuKgiiYo/eov/De9ogLsnR6WgZ0UMkBcjpGqdqHojEVQURj1 X-Received: by 2002:a17:907:1c13:b0:947:bf71:a548 with SMTP id nc19-20020a1709071c1300b00947bf71a548mr5934447ejc.4.1680748699260; Wed, 05 Apr 2023 19:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680748699; cv=none; d=google.com; s=arc-20160816; b=er8T355gz3IxGV1E6V2+wJKVtgVWk830QB/necKYLTEMBzOyGkYWkWAPgBNeJmpY/u NNCv5wkDJfIVXvGfr5p3JRQIvJjaj/K4jbcfcwlKXerrfpsfgGOmQECZC1LeGEaLC9ry 6tvk//UTWj09EQ0EiDteKLJKfcOrKVh2Z292WH3gCDwXRnZ4fJMkzEoHL6veAOG0RXLB f2mmQQJbnJ+69pGvkmaUEriC/RMidrKxwffGfpscHvSz636zdp4Z97RiFRdaqRd2cZkZ Jy37qwzxZYp/m0fEyqSz5iFOizPGRNA2qBJQDELqCcjTZZJpL2KdDGWTSVltswR61WyY 0sTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=pRFv2Hy5rKG8S0IckUBNV9qtKgpB+vXSKM1Yi5fsDqs=; b=cub9LCHYc1jvFWpCTB7/NAoPTvBjBrutxOoA5tFGN4Ukc51DjIo1zSUkhjjduErWO7 e8VgxIr2XQLmQvu1ZrE51GsaBCfRt59DXr7ZN+s0hr81rm8J87FpSdjzXHbqDZ1o19PY WvQgh/tXeGgEPlqsnDq5U+DP0gWCvR9g1RvKnW+ratxh7/PI2Glkr22VMNdAF9lMxH41 wVDc2KnCmJrFKiYOCOfR52BrwjyJSgeZJOtUhvdA7V30AaskLuUivQIudUqd4VqofmFT n4Pm2Zma3suoihs3eEhFzAOCOZoNw/CH8WomMGoAbdruIpuJFePb26H/9oRJoiIGnqqy OXEQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=m8PruuXO; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=Wc6uKvlQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id zg19-20020a170907249300b008b17dfa77fdsi298831ejb.127.2023.04.05.19.37.35; Wed, 05 Apr 2023 19:38:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=m8PruuXO; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=Wc6uKvlQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234914AbjDFCgm (ORCPT + 99 others); Wed, 5 Apr 2023 22:36:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234923AbjDFCfE (ORCPT ); Wed, 5 Apr 2023 22:35:04 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [161.53.235.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E07883D0 for ; Wed, 5 Apr 2023 19:35:02 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 90AFB604ED; Thu, 6 Apr 2023 04:34:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1680748500; bh=moyjB0vCzLhLbgsmt8YV49FqUNROO5qsChsvEK2tqEw=; h=From:To:Cc:Subject:Date:From; b=m8PruuXOs0gkDxPuepXn7L3Sv+xfM980nDlvUrdRZOxqCSEWBIbCWaiWeVjGMpx1y xzMrv2Pv2RAKG6g8HMbq+/J6rmCFSYr2ZfO6uHlq7UDvCb39JzgWBlQtX6lqZtqrVG nfpn+t0SLSwM4yR9ONdJml0SmTWInXBTQxNSEi9XRf3+lpDRPXBuNXM6bfvEG1SQsF HXBLadOWaNAVbSg/j+fO1EOs1BOusk6OOvu9ikptL1aeoeY0UI2210z8Ipo2I2ElYl mhS+dxljAvfbGy3glKQ1Yh+FOeOutmXvIdhI6itZu9dGyi2hsS1m5Am9+78pCApyf5 l8+cw13kMmtJQ== X-Virus-Scanned: Debian amavisd-new at domac.alu.hr Received: from domac.alu.hr ([127.0.0.1]) by localhost (domac.alu.hr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Jx_nZKwZw-ez; Thu, 6 Apr 2023 04:34:57 +0200 (CEST) Received: by domac.alu.hr (Postfix, from userid 1014) id 31915604EF; Thu, 6 Apr 2023 04:34:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1680748497; bh=moyjB0vCzLhLbgsmt8YV49FqUNROO5qsChsvEK2tqEw=; h=From:To:Cc:Subject:Date:From; b=Wc6uKvlQ6W+whn6zqWuHrfayS68UrU/JRL5RRNrPm/RX11WI7HyCnrZkmoi15HTWo n3ETlKN9S1Xhv9EfZn+eDzy3VOdV+SJvm3uvqW7wDtay6Wc2oYarPiscNf36DFqlNC k2V7/4BFgx+lzns4c8xg8h4tKELZelvy1D6ad0f0L18n/TsXD7ppDF6Htq6UJ5ZICM QAFbS/FBnIo0UpTN4uR+L0VtghXWuvkahSKpM3HLUa1y9tHsiAuAfAW62n6E0vHGYD u+he7wi/sOGCYB9nEIzSLM2KOArtmApr2Wkhyc1WtRGVbGSfqGWaH3TskSmY03rU0S 2GZ6kTnneTBrA== From: Mirsad Goran Todorovac To: Greg Kroah-Hartman , Russ Weight , linux-kernel@vger.kernel.org Cc: Mirsad Goran Todorovac , Luis Chamberlain , Tianfei zhang , Christophe JAILLET , Zhengchao Shao , Colin Ian King , Takashi Iwai , Dan Carpenter Subject: [PATCH v3 1/2] test_firmware: Fix some racing conditions in test_fw_config locking. Date: Thu, 6 Apr 2023 03:53:17 +0200 Message-Id: <20230406015315.31505-1-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=0.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762392748257164369?= X-GMAIL-MSGID: =?utf-8?q?1762392748257164369?= Some functions were called both from locked and unlocked context, so the lock was dropped prematurely, introducing a race condition when deadlock was avoided. Having two locks wouldn't assure a race-proof mutual exclusion. test_dev_config_update_bool_unlocked(), test_dev_config_update_u8_unlocked() and test_dev_config_update_size_t_unlocked() versions of the functions were introduced to be called from the locked contexts as a workaround without releasing the main driver's lock and causing a race condition, much like putc() and putc_unlocked() in stdio glibc library. This should guarantee mutual exclusion and prevent any race conditions. Cc: Greg Kroah-Hartman Cc: Luis Chamberlain Cc: Russ Weight Cc: Tianfei zhang Cc: Mirsad Goran Todorovac Cc: Christophe JAILLET Cc: Zhengchao Shao Cc: Colin Ian King Cc: linux-kernel@vger.kernel.org Cc: Takashi Iwai Suggested-by: Dan Carpenter Signed-off-by: Mirsad Goran Todorovac --- lib/test_firmware.c | 52 +++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/lib/test_firmware.c b/lib/test_firmware.c index 05ed84c2fc4c..272af8dc54b0 100644 --- a/lib/test_firmware.c +++ b/lib/test_firmware.c @@ -353,16 +353,26 @@ static ssize_t config_test_show_str(char *dst, return len; } -static int test_dev_config_update_bool(const char *buf, size_t size, +static inline int test_dev_config_update_bool_unlocked(const char *buf, size_t size, bool *cfg) { int ret; - mutex_lock(&test_fw_mutex); if (kstrtobool(buf, cfg) < 0) ret = -EINVAL; else ret = size; + + return ret; +} + +static int test_dev_config_update_bool(const char *buf, size_t size, + bool *cfg) +{ + int ret; + + mutex_lock(&test_fw_mutex); + ret = test_dev_config_update_bool_unlocked(buf, size, cfg); mutex_unlock(&test_fw_mutex); return ret; @@ -373,7 +383,8 @@ static ssize_t test_dev_config_show_bool(char *buf, bool val) return snprintf(buf, PAGE_SIZE, "%d\n", val); } -static int test_dev_config_update_size_t(const char *buf, +static int test_dev_config_update_size_t_unlocked( + const char *buf, size_t size, size_t *cfg) { @@ -384,9 +395,7 @@ static int test_dev_config_update_size_t(const char *buf, if (ret) return ret; - mutex_lock(&test_fw_mutex); *(size_t *)cfg = new; - mutex_unlock(&test_fw_mutex); /* Always return full write size even if we didn't consume all */ return size; @@ -402,6 +411,21 @@ static ssize_t test_dev_config_show_int(char *buf, int val) return snprintf(buf, PAGE_SIZE, "%d\n", val); } +static int test_dev_config_update_u8_unlocked(const char *buf, size_t size, u8 *cfg) +{ + u8 val; + int ret; + + ret = kstrtou8(buf, 10, &val); + if (ret) + return ret; + + *(u8 *)cfg = val; + + /* Always return full write size even if we didn't consume all */ + return size; +} + static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg) { u8 val; @@ -471,10 +495,10 @@ static ssize_t config_num_requests_store(struct device *dev, mutex_unlock(&test_fw_mutex); goto out; } - mutex_unlock(&test_fw_mutex); - rc = test_dev_config_update_u8(buf, count, - &test_fw_config->num_requests); + rc = test_dev_config_update_u8_unlocked(buf, count, + &test_fw_config->num_requests); + mutex_unlock(&test_fw_mutex); out: return rc; @@ -518,10 +542,10 @@ static ssize_t config_buf_size_store(struct device *dev, mutex_unlock(&test_fw_mutex); goto out; } - mutex_unlock(&test_fw_mutex); - rc = test_dev_config_update_size_t(buf, count, - &test_fw_config->buf_size); + rc = test_dev_config_update_size_t_unlocked(buf, count, + &test_fw_config->buf_size); + mutex_unlock(&test_fw_mutex); out: return rc; @@ -548,10 +572,10 @@ static ssize_t config_file_offset_store(struct device *dev, mutex_unlock(&test_fw_mutex); goto out; } - mutex_unlock(&test_fw_mutex); - rc = test_dev_config_update_size_t(buf, count, - &test_fw_config->file_offset); + rc = test_dev_config_update_size_t_unlocked(buf, count, + &test_fw_config->file_offset); + mutex_unlock(&test_fw_mutex); out: return rc;