linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Jani Nikula <jani.nikula@linux.intel.com>
To: Maxime Ripard <mripard@kernel.org>
Cc: "Ville Syrjälä" <ville.syrjala@linux.intel.com>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Emma Anholt" <emma@anholt.net>,
	"Jonathan Corbet" <corbet@lwn.net>,
	linux-kernel@vger.kernel.org,
	"Samuel Holland" <samuel@sholland.org>,
	"Sandy Huang" <hjc@rock-chips.com>,
	"Jernej Skrabec" <jernej.skrabec@gmail.com>,
	linux-doc@vger.kernel.org, "Hans Verkuil" <hverkuil@xs4all.nl>,
	linux-rockchip@lists.infradead.org,
	"Chen-Yu Tsai" <wens@csie.org>,
	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
Date: Wed, 29 Nov 2023 13:40:38 +0200	[thread overview]
Message-ID: <87ttp46b49.fsf@intel.com> (raw)
In-Reply-To: <kygezdrfz56zj6lmq6l5s5yyys2urgq3id7r5n4mb3afn5kc5q@eswnd6a2ihqc>

On Wed, 29 Nov 2023, Maxime Ripard <mripard@kernel.org> wrote:
> On Wed, Nov 29, 2023 at 11:38:42AM +0200, Jani Nikula wrote:
>> On Wed, 29 Nov 2023, Maxime Ripard <mripard@kernel.org> wrote:
>> > Hi Ville,
>> >
>> > On Tue, Nov 28, 2023 at 03:49:08PM +0200, Ville Syrjälä 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 <mripard@kernel.org> wrote:
>> >> > > > All the drm_connector_init variants take at least a pointer to the
>> >> > > > device, connector and hooks implementation.
>> >> > > >
>> >> > > > However, none of them check their value before dereferencing those
>> >> > > > pointers which can lead to a NULL-pointer dereference if the author
>> >> > > > isn't careful.
>> >> > > 
>> >> > > Arguably oopsing on the spot is preferrable when this can't be caused by
>> >> > > user input. It's always a mistake that should be caught early during
>> >> > > development.
>> >> > > 
>> >> > > Not everyone checks the return value of drm_connector_init and friends,
>> >> > > so those cases will lead to more mysterious bugs later. And probably
>> >> > > oopses as well.
>> >> > 
>> >> > So maybe we can do both then, with something like
>> >> > 
>> >> > if (WARN_ON(!dev))
>> >> >    return -EINVAL
>> >> > 
>> >> > if (drm_WARN_ON(dev, !connector || !funcs))
>> >> >    return -EINVAL;
>> >> > 
>> >> > 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 inconsistent.
>> >> 
>> >> 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?
>> 
>> 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.
>> 
>> 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).

Those are all dynamic things that can fail.

Quite different from passing NULL dev, connector, or funcs to
drm_connector_init() and friends.

I think it's wrong to set the example that everything needs to be
checked, everything needs to return an error, every call needs to check
for error return, all the time, everywhere. People absolutely will cargo
cult that, and that's what Ville is referring to.

If you pass NULL dev, connector, or funcs to drm_connector_init() I
think you absolutely deserve to get an oops.

For dev, you could possibly not have reached the function with NULL
dev. (And __drm_connector_init() has dev->mode_config before the check,
so you'll get a static analyzer warning about dereference before the
check.) If you have NULL connector, you didn't check for allocation
failure earlier. If you have NULL funcs, you just passed NULL, because
it's generally supposed to be a pointer to a static const struct.

>> 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.

Again, dynamic in nature and can fail.

>> 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.

But where do you draw the line? If we keep adding these checks to things
that actually can't happen, we teach developers we need to check for
impossible things. And we teach them not to trust anything.

I scroll down the file and reach
drm_connector_attach_edid_property(). Should we NULL check connector?
Should we change the function to int and return a value? Should the
caller check the value? Then there's drm_connector_attach_encoder(). And
drm_connector_has_possible_encoder(). And so on and so forth.

Where do you draw the line?


BR,
Jani.


-- 
Jani Nikula, Intel

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2023-11-29 11:41 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-28 10:24 [PATCH v4 00/45] drm/connector: Create HDMI Connector infrastructure Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 01/45] drm/tests: helpers: Include missing drm_drv header Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 02/45] drm/tests: helpers: Add atomic helpers Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 03/45] drm/tests: Add helper to create mock plane Maxime Ripard
2023-11-28 14:57   ` kernel test robot
2023-11-28 10:24 ` [PATCH v4 04/45] drm/tests: Add helper to create mock crtc Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 05/45] drm/connector: Check drm_connector_init pointers arguments Maxime Ripard
2023-11-28 12:54   ` Jani Nikula
2023-11-28 13:29     ` Maxime Ripard
2023-11-28 13:49       ` Ville Syrjälä
2023-11-29  9:11         ` Maxime Ripard
2023-11-29  9:18           ` Ville Syrjälä
2023-12-01  9:01             ` Maxime Ripard
2023-12-01 15:15               ` Ville Syrjälä
2023-11-29  9:38           ` Jani Nikula
2023-11-29 11:10             ` Maxime Ripard
2023-11-29 11:40               ` Jani Nikula [this message]
2023-11-29 13:26                 ` Maxime Ripard
2023-11-29 10:12         ` Pekka Paalanen
2023-11-29 10:25           ` Ville Syrjälä
2023-12-01 15:17             ` Ville Syrjälä
2023-11-28 10:24 ` [PATCH v4 06/45] drm/tests: connector: Add tests for drmm_connector_init Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 07/45] drm/connector: Introduce an HDMI connector initialization function Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 08/45] drm/connector: hdmi: Create an HDMI sub-state Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 09/45] drm/connector: hdmi: Add Broadcast RGB property Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 10/45] drm/connector: hdmi: Add RGB Quantization Range to the connector state Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 11/45] drm/connector: hdmi: Add output BPC " Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 12/45] drm/connector: hdmi: Add support for output format Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 13/45] drm/connector: hdmi: Add HDMI compute clock helper Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 14/45] drm/connector: hdmi: Calculate TMDS character rate Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 15/45] drm/connector: hdmi: Add custom hook to filter " Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 16/45] drm/connector: hdmi: Compute bpc and format automatically Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 17/45] drm/connector: hdmi: Add Infoframes generation Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 18/45] drm/connector: hdmi: Create Infoframe DebugFS entries Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 19/45] drm/vc4: hdmi: Create destroy state implementation Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 20/45] drm/vc4: hdmi: Switch to HDMI connector Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 21/45] drm/vc4: tests: Remove vc4_dummy_plane structure Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 22/45] drm/vc4: tests: Convert to plane creation helper Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 23/45] drm/rockchip: inno_hdmi: Remove useless mode_fixup Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 24/45] drm/rockchip: inno_hdmi: Remove useless copy of drm_display_mode Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 25/45] drm/rockchip: inno_hdmi: Switch encoder hooks to atomic Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 26/45] drm/rockchip: inno_hdmi: Get rid of mode_set Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 27/45] drm/rockchip: inno_hdmi: no need to store vic Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 28/45] drm/rockchip: inno_hdmi: Remove unneeded has audio flag Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 29/45] drm/rockchip: inno_hdmi: Remove useless input format Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 30/45] drm/rockchip: inno_hdmi: Remove useless output format Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 31/45] drm/rockchip: inno_hdmi: Remove useless colorimetry Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 32/45] drm/rockchip: inno_hdmi: Remove useless enum Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 33/45] drm/rockchip: inno_hdmi: Remove tmds rate from structure Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 34/45] drm/rockchip: inno_hdmi: Remove useless coeff_csc matrix Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 35/45] drm/rockchip: inno_hdmi: Remove useless mode_valid Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 36/45] drm/rockchip: inno_hdmi: Move infoframe disable to separate function Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 37/45] drm/rockchip: inno_hdmi: Create mask retrieval functions Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 38/45] drm/rockchip: inno_hdmi: Switch to infoframe type Maxime Ripard
2023-11-28 11:45   ` Johan Jonker
2023-11-28 10:24 ` [PATCH v4 39/45] drm/rockchip: inno_hdmi: Remove unused drm device pointer Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 40/45] drm/rockchip: inno_hdmi: Switch to HDMI connector Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 41/45] drm/sun4i: hdmi: Convert encoder to atomic Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 42/45] drm/sun4i: hdmi: Move mode_set into enable Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 43/45] drm/sun4i: hdmi: Switch to container_of_const Maxime Ripard
2023-11-28 14:11   ` [v4,43/45] " Sui Jingfeng
2023-11-28 10:24 ` [PATCH v4 44/45] drm/sun4i: hdmi: Consolidate atomic_check and mode_valid Maxime Ripard
2023-11-28 10:24 ` [PATCH v4 45/45] drm/sun4i: hdmi: Switch to HDMI connector Maxime Ripard
2023-11-28 10:45 ` [PATCH v4 00/45] drm/connector: Create HDMI Connector infrastructure Hans Verkuil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ttp46b49.fsf@intel.com \
    --to=jani.nikula@linux.intel.com \
    --cc=corbet@lwn.net \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=emma@anholt.net \
    --cc=hjc@rock-chips.com \
    --cc=hverkuil@xs4all.nl \
    --cc=jernej.skrabec@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-rockchip@lists.infradead.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=mripard@kernel.org \
    --cc=samuel@sholland.org \
    --cc=tzimmermann@suse.de \
    --cc=ville.syrjala@linux.intel.com \
    --cc=wens@csie.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).