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; From patchwork Thu Apr 6 01:43:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mirsad Todorovac X-Patchwork-Id: 80017 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp710098vqo; Wed, 5 Apr 2023 18:53:30 -0700 (PDT) X-Google-Smtp-Source: AKy350adcueoTouHgshWVytp4sEzKS8YBhUi695zx1/iHQxDloEp23MSzxTh6xP50Qln8LiQXTbN X-Received: by 2002:a17:906:1c19:b0:93b:a0c8:1cec with SMTP id k25-20020a1709061c1900b0093ba0c81cecmr4692267ejg.32.1680746010476; Wed, 05 Apr 2023 18:53:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680746010; cv=none; d=google.com; s=arc-20160816; b=zY8Ytu6kekk9rhnPCXe+yQxzNTFihYBvo8LpVjGaEXFWw/0WP5kMZALGwmB44+lKf6 GxlSfcl0baWJH2hqzINmMQx1e8Y2v9O7DyjSX04iOtKl5aYndyBEnWwYg/uc9JioN99x Sp6JzmmAc+qDVJWLoT+DuJpniyKu7/CoDTJKKoEHNtFoQiHELmcfeT4koY7ZwlbBm6Nj b8w9R/6RjKnIftoX/iwwvN2KrzPtDKrnInXjazhwx+5hfNqzZZDgIcU9Kv5itExm2vKx gyXSROsl1TIT2j1bZ94XQQp0kcDzSwggooR+v00y1DI6dWMMJVlEuwm1zy0JoBax27/i VoCg== 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=y0ORS9bcWQd1eTexi8iS0kkFQPn00n6kjm7AWCaGo3E=; b=BzFFbmBuztUmBtODktf3+iI/u13v5n+GMt1OPFQNrmFCOLd8KdcciC8b+ssDek4wm+ CauIxzE9R2aASzNwSW9VIEcZ2wEmpM+L2uzNgpR15qy9V6+Xp/+ZShiP0awoQi3Hltbc s/FiFVpFE3aJsHMvU/x0TgJDdAPc9NNkQWxvxSFzMHwngKTlqE1tU/Ieqyg/M6oiEaLn tVMVm25p9C3FmrNd1PKZTXOqYbTskCiSZRsPr9txiJjGIm+RZN453lH7M5e4I/l3F/hn h9ETZqhFPEZet7Rdx057YBr2xrBNKIvSfCp0jVz5CvcvimXsyx8UJzGUtTrY4ffYJONy nx/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=MyndSgiC; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=Li7H4Lzm; 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 a20-20020a1709063e9400b00947a3ecc3b7si184598ejj.624.2023.04.05.18.53.06; Wed, 05 Apr 2023 18:53:30 -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=MyndSgiC; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=Li7H4Lzm; 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 S234787AbjDFBqp (ORCPT + 99 others); Wed, 5 Apr 2023 21:46:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234774AbjDFBqn (ORCPT ); Wed, 5 Apr 2023 21:46:43 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [IPv6:2001:b68:2:2800::3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0291C729B for ; Wed, 5 Apr 2023 18:46:41 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 2E41B604ED; Thu, 6 Apr 2023 03:46:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1680745598; bh=7gR9b1/jHxuRqJoe09x+E/k2KTBJTWA8CRBAC0RautE=; h=From:To:Cc:Subject:Date:From; b=MyndSgiCAD/vVHTTf0/fc+RGYboQSxt7IORW0hy1Q2oVMsIT1+6vJGuUx/TOzXUzR +SwpUiMcng8qjQ5niW66D/o96zRwDQa2B5ftMgrQ502eAfUDYUPlSuiDhHBNuDvF7y 5r7y6/p5EfjAewG83YBBpDbux/RmGFQzM1Q59ky0suq2o/Gi8AejkDmcNjX057nlaA c5zKLrWKzJMsz5UUGKSaiKuRCobxFijC0mTZZfOvgaAaSmj8ZUPXk8beDPVjvK6hvj kFluan4EngQuFvzctpaPyzL+DV/JXQLNXWw9HvJ1EW4W8dw9Q3FXo450DI5soLxUmP LiZfm7xGUkyGQ== 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 whKfdZ4Z2Dov; Thu, 6 Apr 2023 03:46:36 +0200 (CEST) Received: by domac.alu.hr (Postfix, from userid 1014) id 2826D604EF; Thu, 6 Apr 2023 03:46:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1680745596; bh=7gR9b1/jHxuRqJoe09x+E/k2KTBJTWA8CRBAC0RautE=; h=From:To:Cc:Subject:Date:From; b=Li7H4LzmaibgVaTZZxB/xo1NLZg8ktDcCg9FQEDHFefKydb7dMaLxf4h+SUHD9kOB Fc3puyGd7U1xAziahi/5FAv3OIqmGQi151FWIaDNssDAuapm7hQ4pU1+lzLkxG/9k4 /jCufV4j+0bOF4Z9rSxg8UCI3lU2foMCrNzgJ9avlQagdV4Qk7hkzZ8oWvdAU3OtBD nSB+cjk/NRC2abDMWgGQZF7VuGMXh0U8fuwoNrTGkV1wtxPaMhFKmD/kZeD4B/BaMP EbMZ5vZn47RypZ4EwgUnKgwgevLbPx/pctaVRZU0rBNZNzurgi7notNnjJjTMpgmKS /LgzWZtlx6kgg== From: Mirsad Goran Todorovac To: Greg Kroah-Hartman , Russ Weight , linux-kernel@vger.kernel.org Cc: Mirsad Goran Todorovac Subject: [PATCH v3 2/2] Revert "lib/firmware: fix a memory leak with multiple calls of trigger_batched_requests_store()" Date: Thu, 6 Apr 2023 03:43:24 +0200 Message-Id: <20230406014324.31171-2-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?1762389928832769562?= X-GMAIL-MSGID: =?utf-8?q?1762389928832769562?= This reverts commit d59ff9027c628f0e9be344b28f3576566a274f2f. --- lib/test_firmware.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/test_firmware.c b/lib/test_firmware.c index 51377b9ab6e3..454992bfdb2a 100644 --- a/lib/test_firmware.c +++ b/lib/test_firmware.c @@ -1036,11 +1036,6 @@ ssize_t trigger_batched_requests_async_store(struct device *dev, mutex_lock(&test_fw_mutex); - if (test_fw_config->reqs) { - rc = -EBUSY; - goto out; - } - test_fw_config->reqs = vzalloc(array3_size(sizeof(struct test_batched_req), test_fw_config->num_requests, 2));