From patchwork Sat Sep 12 21:45:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Anderson X-Patchwork-Id: 1362933 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=UT/QlWIm; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BpmR42W8Jz9sT6 for ; Sun, 13 Sep 2020 07:46:24 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 16202822ED; Sat, 12 Sep 2020 23:46:10 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="UT/QlWIm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 24176822B5; Sat, 12 Sep 2020 23:46:07 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-qk1-x744.google.com (mail-qk1-x744.google.com [IPv6:2607:f8b0:4864:20::744]) (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 CAE3582242 for ; Sat, 12 Sep 2020 23:46:02 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=seanga2@gmail.com Received: by mail-qk1-x744.google.com with SMTP id g72so13417577qke.8 for ; Sat, 12 Sep 2020 14:46:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wheQFDE9vC52doLZfedT2oDfQmTRGr44alMZK/nRkng=; b=UT/QlWIm/dsNPqHgmDmtLR4BytBNAr7K3ym/PrGGYRx2T09MasDhawtRUGFInBzL8j AnDma5gad4J3P9LUqJKgSvshOnnmcNFooJJ1iSL606Sy/KRREAAKbObdfwHkHaB4g8+e nDkvPWiXi8hKlg7qnuJ66E7zD5cPeZvIx2K6BDkSRU+R2ue5js7quugj+DKD+bdUtmcZ uqWr/Jf1zaEpX1LQdwqyv0yf5U/PNHh4r/Z0TzTvG7XfwvclxVYMxvUbAPaC7FCEDsdQ +C3nn5uWrP6Zga8kZ+r27mob+TcpZSNyuNGM4n6LNnB+Y5TLfo1uRvGF2PizobGDsvUm iYoA== 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:mime-version:content-transfer-encoding; bh=wheQFDE9vC52doLZfedT2oDfQmTRGr44alMZK/nRkng=; b=VtMtHIYY9ZQoiy87RCH/G6syZ1wf0jkkO/sHZcyZ07NYzM5tG36rP9NnAL1oMexfAH X8pCeItyOTkGka0POcrk9zUn+4hy/qMjZoFMLBN448NTGXaJJ+g8AIjRwQnHvkPNN/UE twd1+jviQIbv1ArfYb6axj4wu+xN5KlPinrV37F4IKT96CoPZ7C+v+enqwlVRJMuq/Wt 81J4Y6cz+mXUZchh2AenR/T1EYktImr66u1VwE5fwbKSV8S32IHW/UDa42/RgpPWkLCy 1sDl3kcgmpnuaGtzGgAQLYf5uv2udaXa/1sqMJ69iOAfTvbBQGZDKuhM+HfvwsOXGIJ+ b18A== X-Gm-Message-State: AOAM533/4TEr7n8DNnEVY4RCwXVO2qbDtLGPpTLZ/vnQ1fmJel/WWPXT Yf/4b4RrwW0ycQMcCFBYPvz40xPmufpOeA== X-Google-Smtp-Source: ABdhPJxZ71Rp5r4DroljoItzTckjGo5agzxlLx3mDtcDcJ1H5vmGyPvDJTMNTQzWKVvnm3LS9XvORg== X-Received: by 2002:a37:a392:: with SMTP id m140mr6783148qke.195.1599947161468; Sat, 12 Sep 2020 14:46:01 -0700 (PDT) Received: from godwin.fios-router.home (pool-108-51-35-162.washdc.fios.verizon.net. [108.51.35.162]) by smtp.gmail.com with ESMTPSA id k20sm8745419qtb.34.2020.09.12.14.46.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Sep 2020 14:46:01 -0700 (PDT) From: Sean Anderson To: u-boot@lists.denx.de Cc: Simon Glass , Heinrich Schuchardt , Joe Hershberger , Sean Anderson Subject: [PATCH 1/3] dev: Disambiguate errors in uclass_find Date: Sat, 12 Sep 2020 17:45:42 -0400 Message-Id: <20200912214544.362594-2-seanga2@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200912214544.362594-1-seanga2@gmail.com> References: <20200912214544.362594-1-seanga2@gmail.com> MIME-Version: 1.0 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.102.3 at phobos.denx.de X-Virus-Status: Clean There are two cases where uclass_find can return an error. The second is when the uclass has not yet been init'd. The first is when the driver model has not been init'd (or has been only partially init'd) and there is no root uclass driver. If LOG_SYSLOG is enabled and LOG_DEFAULT_LEVEL is set to LOGL_DEBUG or higher, log_syslog_emit will try to call net_init before initf_dm has been called. This in turn (eventually) calls uclass_get for UCLASS_ETH. If the second error occurs, uclass_get should call uclass_add to create the uclass. If the first error occurs, then uclass_get should not call uclass_add (because the uclass list has not been initialized). However, the first error is expected when calling uclass_get for UCLASS_ROOT, so we add an exception. There are several alternative approaches to this patch. One option would be to duplicate the check against gd->dm_root in uclass_get and not change the behavior of uclass_find. I did not choose this approach because I think it it is less clear than the patch as-is. However, that is certainly subjective, and there is no other technical reason to do it this way. Another approach would have been to change log_syslog_emit to abort if it is called too early. I did not choose this approach because the check in uclass_find to see if gd->dm_root exists implies that functions in its file are allowed to be called at any time. There is an argument to be made that callers should ensure that they don't call certain functions too early. I think it is better to code defensively in these circumstances to make it easier to discover such errors. Signed-off-by: Sean Anderson --- drivers/core/uclass.c | 16 +++++++++++++++- test/dm/core.c | 2 +- test/dm/test-main.c | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c index c3f1b73cd6..2e098034c9 100644 --- a/drivers/core/uclass.c +++ b/drivers/core/uclass.c @@ -19,6 +19,7 @@ #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; @@ -27,7 +28,7 @@ struct uclass *uclass_find(enum uclass_id key) struct uclass *uc; if (!gd->dm_root) - return NULL; + return ERR_PTR(-EAGAIN); /* * TODO(sjg@chromium.org): Optimise this, perhaps moving the found * node to the start of the list, or creating a linear array mapping @@ -144,6 +145,19 @@ int uclass_get(enum uclass_id id, struct uclass **ucp) *ucp = NULL; uc = uclass_find(id); + if (IS_ERR(uc)) { + /* + * If we're getting the root uclass, then uclass_find will fail + * with -EAGAIN (since it thinks there's no uclass list to + * search), but we need to add it anyway (since otherwise it + * would never be created). + */ + if (PTR_ERR(uc) == -EAGAIN && id == UCLASS_ROOT) + uc = NULL; + else + return PTR_ERR(uc); + } + if (!uc) return uclass_add(id, ucp); *ucp = uc; diff --git a/test/dm/core.c b/test/dm/core.c index 8ed5bf7370..a4e0ac06f9 100644 --- a/test/dm/core.c +++ b/test/dm/core.c @@ -733,7 +733,7 @@ static int dm_test_uclass_before_ready(struct unit_test_state *uts) gd->dm_root_f = NULL; memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root)); - ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST)); + ut_asserteq_ptr(ERR_PTR(-EAGAIN), uclass_find(UCLASS_TEST)); return 0; } diff --git a/test/dm/test-main.c b/test/dm/test-main.c index 38b7b1481a..6ff07a5d32 100644 --- a/test/dm/test-main.c +++ b/test/dm/test-main.c @@ -70,7 +70,7 @@ static int dm_test_destroy(struct unit_test_state *uts) * check that here before we call uclass_find_device(). */ uc = uclass_find(id); - if (!uc) + if (IS_ERR_OR_NULL(uc)) continue; ut_assertok(uclass_destroy(uc)); }