Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp274304ybl; Thu, 15 Aug 2019 17:11:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqzZdHt2JDDuzv4q6eJVySMYMZ3PSFJGPZhCuJvFGiaFfkyGxoaGeu9z7ww/mtWTvOyaj6wH X-Received: by 2002:a62:1715:: with SMTP id 21mr8088709pfx.134.1565914265518; Thu, 15 Aug 2019 17:11:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565914265; cv=none; d=google.com; s=arc-20160816; b=v5G1z2cl+LetGbYQ8dYF+YzF2LqGlzp/d8sVt/ew/6og0OkVKpGbf0EmmEYTUELZwE rZ1boY0VgIhqB8JeQRm0sPorhXfS0Ak1p+x2v9DfAgc9pW+4nr2BhhWHFAB4ZNv8hjL+ aqAzHNl4+pahaD/d0LfdQ3xU2OVbyB9IPiIzxGhN5crQyElQI/txK+IjoNVkU0MRt5qJ iI9x626E6wclTfeSYYDDrQIg3Jha/E8c/yMXu5V4UPkzZZUyk9QC/gAtjVZ4A6J6RwGA oHZvAerL4u7RV+9jkJwlums9bnLFssIPLlYK9djLtSbcY5BdgYwjojDqKuUAan5lL4Hz Hsjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=XVcmO8RzpRC/BFyAhdGsg6GtacmmL8vW5DzIGcj2y00=; b=LLPXAtvQJpd5kH/7sN/iLG7IboRrrui5VbKSEJDXmypAvGsdN5o2H0uW4G5JkmwruX fIXpA0vfVdS8eaehKoNSmyUY9f22317QlQnoKZeZx4EM3IfcUzDwGfkzTMuLNmUJJsET kc+xc8S6NU5QV6VOH5CrbmptDZrmQgXi5kyEKS59440gyr6sLm8IXyPZVVZ62nDwdgpi t+cyZWmoBUsT4q+0O+uzQCbCB9uyi+M5wiGzeoUklHHHzfCCZ4cSTwD7IsaqIJ4CPB0j /mDei/qfc8afpeEw7U4komckkRAfLksMZ7bNB7f3QC4kS4Uf84EFb4XT8t349j4QQFqs Jjgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=UyJlZup3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u128si2662468pgu.389.2019.08.15.17.10.50; Thu, 15 Aug 2019 17:11:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=UyJlZup3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726469AbfHPAKD (ORCPT + 99 others); Thu, 15 Aug 2019 20:10:03 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:45223 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726444AbfHPAKC (ORCPT ); Thu, 15 Aug 2019 20:10:02 -0400 Received: by mail-pg1-f195.google.com with SMTP id o13so2009270pgp.12 for ; Thu, 15 Aug 2019 17:10:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=XVcmO8RzpRC/BFyAhdGsg6GtacmmL8vW5DzIGcj2y00=; b=UyJlZup3Cv7yWgUuLvmQAnNfqDuhuM74GoMYvrExOhXCciNnhsHNjBZs2PEruaFfjG Jcui7w1fA8S36tDl5u6QHeKZF53YBkNnI8NRPTT9EsiCecxj3lysIb5IqKM09mKOGb1+ UDcxcyEFboiIxHynVYAC8K3EeVb9qsmZqVhaw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=XVcmO8RzpRC/BFyAhdGsg6GtacmmL8vW5DzIGcj2y00=; b=SsG2aswbiAhAJHxOGQBvhFnoBMYHZRRasB8RNqba8X1Wz2ehSDyUmufHzPZm4/qljB o0D1b91pLGWQbiU/FLY/733v8QOa3k8KhGxneI8p/rpsjYAVke6C3cD3vv9L3PTv1KTx wmsT5ynGuixD+aoUCscaYCm21wvZ9WYLr49YG5WdOtbQc8aaoZQNmd99rY7j/IFK0eg1 OtNV3d7FjlaxRsFp3Wl0TjYczyMUvMFMMMXPUJSFGg5Bd+TMfcB4y51luz72ReaR8pp8 fi+iBjRB5Pb5LJvnC91VXkXcRZUe+mxaFLDWRnfIRAaMtYQL8KJbIWiz6jdAgUmNOZn7 Zgkw== X-Gm-Message-State: APjAAAWZ/A8FNTQOdcmKObKKcV4wkeaUe53qQZmkFfE+TZUnbCJDi0x9 WsZGlCyOvrzuJI0+3btB4Nh2vw== X-Received: by 2002:a17:90a:b947:: with SMTP id f7mr4557928pjw.63.1565914200957; Thu, 15 Aug 2019 17:10:00 -0700 (PDT) Received: from lbrmn-lnxub113.broadcom.net ([192.19.228.250]) by smtp.gmail.com with ESMTPSA id g2sm4056916pfi.26.2019.08.15.17.09.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Aug 2019 17:10:00 -0700 (PDT) From: Scott Branden To: Luis Chamberlain , Greg Kroah-Hartman , Andy Gross , David Brown , Alexander Viro , Shuah Khan , bjorn.andersson@linaro.org Cc: "Rafael J . Wysocki" , linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-fsdevel@vger.kernel.org, BCM Kernel Feedback , Olof Johansson , Andrew Morton , Dan Carpenter , Colin Ian King , Kees Cook , Takashi Iwai , linux-kselftest@vger.kernel.org, Scott Branden Subject: [PATCH 3/3] firmware: add mutex fw_lock_fallback for race condition Date: Thu, 15 Aug 2019 17:09:45 -0700 Message-Id: <20190816000945.29810-4-scott.branden@broadcom.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190816000945.29810-1-scott.branden@broadcom.com> References: <20190816000945.29810-1-scott.branden@broadcom.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A race condition exists between _request_firmware_prepare checking if firmware is assigned and firmware_fallback_sysfs creating a sysfs entry (kernel trace below). To avoid such condition add a mutex fw_lock_fallback to protect against such condition. misc test_firmware: Falling back to sysfs fallback for: nope-test-firmware.bin sysfs: cannot create duplicate filename '/devices/virtual/misc/test_firmware/nope-test-firmware.bin' CPU: 4 PID: 2059 Comm: test_firmware-3 Not tainted 5.3.0-rc4 #1 Hardware name: Dell Inc. OptiPlex 7010/0KRC95, BIOS A13 03/25/2013 Call Trace: dump_stack+0x67/0x90 sysfs_warn_dup.cold+0x17/0x24 sysfs_create_dir_ns+0xb3/0xd0 kobject_add_internal+0xa6/0x2a0 kobject_add+0x7e/0xb0 ? _cond_resched+0x15/0x30 device_add+0x121/0x670 firmware_fallback_sysfs+0x15c/0x3c9 _request_firmware+0x432/0x5a0 ? devres_find+0x63/0xc0 request_firmware_into_buf+0x63/0x80 test_fw_run_batch_request+0x96/0xe0 kthread+0xfb/0x130 ? reset_store+0x30/0x30 ? kthread_park+0x80/0x80 ret_from_fork+0x3a/0x50 kobject_add_internal failed for nope-test-firmware.bin with -EEXIST, don't try to register things with the same name in the same directory. Signed-off-by: Scott Branden --- drivers/base/firmware_loader/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c index bf44c79beae9..ce9896e3b782 100644 --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -88,6 +88,7 @@ static inline struct fw_priv *to_fw_priv(struct kref *ref) /* fw_lock could be moved to 'struct fw_sysfs' but since it is just * guarding for corner cases a global lock should be OK */ DEFINE_MUTEX(fw_lock); +DEFINE_MUTEX(fw_lock_fallback); static struct firmware_cache fw_cache; @@ -758,6 +759,17 @@ _request_firmware(const struct firmware **firmware_p, const char *name, if (!firmware_p) return -EINVAL; + /* + * There is a race condition between _request_firmware_prepare checking + * if firmware is assigned and firmware_fallback_sysfs creating sysfs + * entries with duplicate names. + * Yet, with this lock the firmware_test locks up with cache enabled + * and no event used during firmware test. + * This points to some very racy code I don't know how to entirely fix. + */ + if (opt_flags & FW_OPT_NOCACHE) + mutex_lock(&fw_lock_fallback); + if (!name || name[0] == '\0') { ret = -EINVAL; goto out; @@ -791,6 +803,9 @@ _request_firmware(const struct firmware **firmware_p, const char *name, fw = NULL; } + if (opt_flags & FW_OPT_NOCACHE) + mutex_unlock(&fw_lock_fallback); + *firmware_p = fw; return ret; } -- 2.17.1