Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4497189rdh; Wed, 29 Nov 2023 03:14:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IGGq/x4YbHDQZWi78FU8mJ4FwRFihzGoj3gbuI2QoA/DGh1e3j+p3sS1blRSJicIUvFCtAz X-Received: by 2002:a05:6808:2104:b0:3ad:9540:5475 with SMTP id r4-20020a056808210400b003ad95405475mr22489397oiw.45.1701256469531; Wed, 29 Nov 2023 03:14:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701256469; cv=none; d=google.com; s=arc-20160816; b=bdp4NgOCGlHo+dE0zUnqR8DMCMGjLoiHsLbPhn2ZXXhnw8COeszMs0QSj+yeSWzt9q /X3bJzefpoFI0MemxY1dag791+NIgObnzLYSYR9bGdR/BCfPLNKgT88c3GoAXz2QjVap Fk3vRWXSPsxTfDzSqwO3CSi4W/LvXhLQGHD3gXR8Q84/y8rNg3Li1abzhsu1YY+M1Hqd zEx30KGv5P8H3WDHNAM6J3WN5ZmAL7LU/ETguCDzDvUo7lTn+Ydwl3a8yCBexOkCwk4n PyRMFPAIr9exYIOLYt0vtTQvHM8N1mixZWGvZvz3Vw5JoCPSpTP0cG/r0+6IzU/4GoTJ lmNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Dy03HPbyl/dX4arceqJjVyZy4RnNWD+gvRpg3/El48I=; fh=P9MdYpX0zTeJn4b8xCtK+DFBAF1TcCSxmtuy/oj4bu4=; b=mpPzSi4eUzGg7mMUiwVR55sABy/ev2dl4QEALDR4iwo8+CUr7e4tYg9J3Ol21NwsQV cRJKTzCqnKiM8slE+Q/OZot0a2Ryw3V8jqVkh2Gg4+L1D6J8ctYyzzbnffiFR5Tai614 cuihZ4p+8EfVzj8km9AQ07Yn9eoiTdxTqgDE6PlGtMJiFXZQBD6h/WOmhhZqmT244o59 Iu6wnLGeVMb0Qx+xpMnn+upVZ5SE+usD3QiHSFymCU1KVVS5jXt6M3nRLbIng0OQtcF7 h6wwMxLaYINwMnkN8wrkTQaanKZAzwbZsDnObQXl3bwNuTADisu1QOuJZNa6FWP1QkSG HCXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=O5zVZl8h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id k26-20020a63ff1a000000b005bdbdd396eesi13992673pgi.633.2023.11.29.03.14.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 03:14:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=O5zVZl8h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 02AD4803A504; Wed, 29 Nov 2023 03:14:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233023AbjK2LOE (ORCPT + 99 others); Wed, 29 Nov 2023 06:14:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232353AbjK2LNo (ORCPT ); Wed, 29 Nov 2023 06:13:44 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F97E273F for ; Wed, 29 Nov 2023 03:10:59 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B9AAAC433C7; Wed, 29 Nov 2023 11:10:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701256259; bh=TgKyGRCEeshKQvQjUN7AFNof2EniJcjgu0CRQnVYZ/w=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=O5zVZl8he0ZR2AllaDzQjsJvcC48I2DVX+YcZFM4U4lJRKdlzKQj01rXDkKkH+LiF lHPugHnBXU9Lz3GIPchKCA3swnNkGfRCeEF7Buegp/GVjESt8XFaaaMPn8uO0Np3fb QH4uoT6GVpVu5f8G7K3G8tLfTfynCUbzOB/07Y+ZEgy164kJsj+v5TkYpKomkznNh+ o5wraA67njkrtd/dGw69TlIN3Ax9ir38Qz9LqnnMp4EoQtIsRef7Y62C7rehvUiPF4 ju3lZ56sfX8cpuSMTaWug99glEWX2Km1aBuUBK4q0AvF6dx7s7cm86D7FR90U6fgLj W0tL9BmfmuMqg== Date: Wed, 29 Nov 2023 12:10:56 +0100 From: Maxime Ripard To: Jani Nikula Cc: Ville =?utf-8?B?U3lyasOkbMOk?= , Thomas Zimmermann , Emma Anholt , Jonathan Corbet , linux-kernel@vger.kernel.org, Samuel Holland , Sandy Huang , Jernej Skrabec , linux-doc@vger.kernel.org, Hans Verkuil , linux-rockchip@lists.infradead.org, Chen-Yu Tsai , dri-devel@lists.freedesktop.org, linux-media@vger.kernel.org, linux-sunxi@lists.linux.dev, linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH v4 05/45] drm/connector: Check drm_connector_init pointers arguments Message-ID: References: <20231128-kms-hdmi-connector-state-v4-0-c7602158306e@kernel.org> <20231128-kms-hdmi-connector-state-v4-5-c7602158306e@kernel.org> <87h6l66nth.fsf@intel.com> <2mnodqvu2oo674vspiy4gxhglu3it5cq47acx5itnbwevgc4cf@c7h2bvnx3m2n> <8734wo7vbx.fsf@intel.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="yiklpqzxs4i4jbph" Content-Disposition: inline In-Reply-To: <8734wo7vbx.fsf@intel.com> X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 29 Nov 2023 03:14:27 -0800 (PST) --yiklpqzxs4i4jbph Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Nov 29, 2023 at 11:38:42AM +0200, Jani Nikula wrote: > On Wed, 29 Nov 2023, Maxime Ripard wrote: > > Hi Ville, > > > > On Tue, Nov 28, 2023 at 03:49:08PM +0200, Ville Syrj=E4l=E4 wrote: > >> On Tue, Nov 28, 2023 at 02:29:40PM +0100, Maxime Ripard wrote: > >> > On Tue, Nov 28, 2023 at 02:54:02PM +0200, Jani Nikula wrote: > >> > > On Tue, 28 Nov 2023, Maxime Ripard wrote: > >> > > > All the drm_connector_init variants take at least a pointer to t= he > >> > > > device, connector and hooks implementation. > >> > > > > >> > > > However, none of them check their value before dereferencing tho= se > >> > > > pointers which can lead to a NULL-pointer dereference if the aut= hor > >> > > > isn't careful. > >> > >=20 > >> > > Arguably oopsing on the spot is preferrable when this can't be cau= sed by > >> > > user input. It's always a mistake that should be caught early duri= ng > >> > > development. > >> > >=20 > >> > > Not everyone checks the return value of drm_connector_init and fri= ends, > >> > > so those cases will lead to more mysterious bugs later. And probab= ly > >> > > oopses as well. > >> >=20 > >> > So maybe we can do both then, with something like > >> >=20 > >> > if (WARN_ON(!dev)) > >> > return -EINVAL > >> >=20 > >> > if (drm_WARN_ON(dev, !connector || !funcs)) > >> > return -EINVAL; > >> >=20 > >> > I'd still like to check for this, so we can have proper testing, and= we > >> > already check for those pointers in some places (like funcs in > >> > drm_connector_init), so if we don't cover everything we're inconsist= ent. > >>=20 > >> People will invariably cargo-cult this kind of stuff absolutely > >> everywhere and then all your functions will have tons of dead > >> code to check their arguments. > > > > And that's a bad thing because... ? > > > > Also, are you really saying that checking that your arguments make sense > > is cargo-cult? >=20 > It's a powerful thing to be able to assume a NULL argument is always a > fatal programming error on the caller's side, and should oops and get > caught immediately. It's an assertion. Yeah, but we're not really doing that either. We have no explicit assertion anywhere. We take a pointer in, and just hope that it will be dereferenced later on and that the kernel will crash. The pointer to the functions especially is only deferenced very later on. And assertions might be powerful, but being able to notice errors and debug them is too. A panic takes away basically any remote access to debug. If you don't have a console, you're done. > We're not talking about user input or anything like that here. >=20 > If you start checking for things that can't happen, and return errors > for them, you start gracefully handling things that don't have anything > graceful about them. But there's nothing graceful to do here: you just return from your probe function that you couldn't probe and that's it. Just like you do when you can't map your registers, or get your interrupt, or register into any framework (including drm_dev_register that pretty much every driver handles properly if it returns an error, without being graceful about it). > Having such checks in place trains people to think they *may* happen. In most cases, kmalloc can't fail. We seem to have a very different policy towards it. > While it should fail fast and loud at the developer's first smoke test, > and get fixed then and there. Returning an error + a warning also qualifies for "fail fast and loud". But keeps the system alive for someone to notice in any case. Maxime --yiklpqzxs4i4jbph Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQRcEzekXsqa64kGDp7j7w1vZxhRxQUCZWccQAAKCRDj7w1vZxhR xQWeAP9Ze0DmGeFKXN1BEGWfARxDJxHLh2vej5D8shdyccTj2AEAop8XqkWN1XF3 9wqNkaKiWk7HPoVA26RTjT1FaSG8DA8= =jh/M -----END PGP SIGNATURE----- --yiklpqzxs4i4jbph--