Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60CC0C4338F for ; Sun, 1 Aug 2021 18:05:40 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7DAE1610A1 for ; Sun, 1 Aug 2021 18:05:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7DAE1610A1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4EABA83280; Sun, 1 Aug 2021 20:05:37 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Hj6eZj5p"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C5852832D8; Sun, 1 Aug 2021 20:05:34 +0200 (CEST) Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C4ECF83280 for ; Sun, 1 Aug 2021 20:05:30 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-oi1-x233.google.com with SMTP id u10so21389857oiw.4 for ; Sun, 01 Aug 2021 11:05:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=L1cm6F8l6xHJfkncsd6WpR/oGPKpKttNsyMhsXfhHjY=; b=Hj6eZj5pvN9tWe0hjE4/CULBvPareav0syqWDeLsfh9+u7IAnQDMYLLuudXKvn+97N duC6sJUlltShtd6cZVoj6AEmGIZaRk++YKsMu8tfOf63BSdo+kvWwAqTYfSiZidEVONW iEB9qtyHPF9Ki58NRih7Uxk9aBB+cl9kzQnqk= 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:mime-version :content-transfer-encoding; bh=L1cm6F8l6xHJfkncsd6WpR/oGPKpKttNsyMhsXfhHjY=; b=OLCaW9H4coN/H0YVtQ1K+ozp/q8C5YMSZ72nppTVe7TG3bAWdRi9OOL8+r9rV0A3rj geooYmkB4eEQAWk5ZUgXiRgklNTmPpIoXLZDkG5FaF0j81aVSkO+YVvoizXoSLKNSoUi FZNolR2SBBAEeLADRTOWQYgx1tpgXpQGpND/5qxXLVPRB3AegSJ1STNHF1kgfMya0yQA zkfvVG4qHYqqe2pPpDPaNPmFzGWb+NFDkwDMgWErFcUGZ3PlGmq0LPWo676gCpBlJLlf vY4ttrFrySWGdNbXX5eyn2jqn9Qt90b3dYouYqGQrw2gyUJ860fIG6xS+nqPEBLF5qzv 7qlQ== X-Gm-Message-State: AOAM533csWPpcrjtC7vfYEmkAOi5uqtUAppJPMHZ2puS4r3P7eUIgWhO HvCMt8QMGjkax/Jh3JOE/QeCioPtp6yoi4Uu X-Google-Smtp-Source: ABdhPJxSfNKoHtKASeE7cVBrzyQba5ucuO/pgl3hZtaesWuMZs3eOohJFxirsnd+c3ZkGRNSTLJwHQ== X-Received: by 2002:aca:3144:: with SMTP id x65mr8086174oix.157.1627841128998; Sun, 01 Aug 2021 11:05:28 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id n12sm1499328otq.74.2021.08.01.11.05.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 11:05:28 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Bin Meng , Simon Glass , Marek Vasut , Pavel Herrmann Subject: [PATCH] dm: core: Don't allow uclass use before ready Date: Sun, 1 Aug 2021 12:05:23 -0600 Message-Id: <20210801180523.2166782-1-sjg@chromium.org> X-Mailer: git-send-email 2.32.0.554.ge1b32706d8-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean At present it is possible to call uclass_get() before driver model is inited. In fact this happens on x86 boards which use Intel FSPv1, since mrccache_get_region() tries to get the SPI flash device very early during init. This has always been undefined behaviour. Previously it generally worked, i.e. returned an error code without crashing, because gd->uclass_root_s is zeroed and the uclass can be added despite driver model not being ready, due to the way lists are implemented. With the change to use a gd->uclass_root pointer, this no-longer works. For example, it causes a hang on minnowmax. Fix this by adding a check that driver model is ready when uclass_get() is called. This function is called in the process of locating any device, so it is a good place to add the check. This fixes booting on minnowmax. Signed-off-by: Simon Glass Fixes: 8a715530bb1 ("dm: core: Allow the uclass list to move") --- drivers/core/uclass.c | 3 +++ include/dm/uclass.h | 3 ++- test/dm/core.c | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index 117d35ac49c..3146dfd0320 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -146,6 +146,9 @@ int uclass_get(enum uclass_id id, struct uclass **ucp) { struct uclass *uc; + /* Immediately fail if driver model is not set up */ + if (!gd->uclass_root) + return -EDEADLK; *ucp = NULL; uc = uclass_find(id); if (!uc) { diff --git a/include/dm/uclass.h b/include/dm/uclass.h index 2778818b528..da0c1bfadb1 100644 --- a/include/dm/uclass.h +++ b/include/dm/uclass.h @@ -159,7 +159,8 @@ void *uclass_get_priv(const struct uclass *uc); * * @key: ID to look up * @ucp: Returns pointer to uclass (there is only one per ID) - * @return 0 if OK, -ve on error + * @return 0 if OK, -EDEADLK if driver model is not yet inited, other -ve on + * other error */ int uclass_get(enum uclass_id key, struct uclass **ucp); diff --git a/test/dm/core.c b/test/dm/core.c index 48e66b73335..c9a7606666c 100644 --- a/test/dm/core.c +++ b/test/dm/core.c @@ -994,6 +994,7 @@ static int dm_test_uclass_before_ready(struct unit_test_state *uts) memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root)); ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST)); + ut_asserteq(-EDEADLK, uclass_get(UCLASS_TEST, &uc)); return 0; } -- 2.32.0.554.ge1b32706d8-goog