Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp818039rwb; Wed, 26 Jul 2023 03:28:29 -0700 (PDT) X-Google-Smtp-Source: APBJJlGJSC7sIq4kBC3kULytUePR1r+GeOb8gJ3NX60I82sFTdaUioh1sWiOke56Mra6P+VedOhu X-Received: by 2002:a17:903:484:b0:1bb:893e:5df5 with SMTP id jj4-20020a170903048400b001bb893e5df5mr1174943plb.34.1690367309303; Wed, 26 Jul 2023 03:28:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690367309; cv=none; d=google.com; s=arc-20160816; b=pIIvmq8YjP3BjRm8t2/oZ+cYqvN2PU1rGu07TtNE6khB+WHkT3v0SwUBw+nkofu5Hp OTNf6GAVwLidac8JmiOuCjY1nzX94g9bboOAAaX+IhRH0NN0QJWwFtUokSuxZR+sN5/0 PvqIr5qwBLHttUqDBZjS7DOocf5g/aDSvqiK4ePaWXioSO0RXLOrLD7meU8ZF8hfeNgm pTX224xDmseLkTU9WZSutw9RKc2RPLlu1LGYCu8IoY0V/aA4Zv34mem1gIUAuJNd2taE 4Fc+FKZFd63gsi/3WQJAqJBpDwCfjKnyvlyQutWe5QkPiPiFGrf+8WrlvX3bsxfeaAVV p2FA== 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=MHRBLSh85uDGOrdQlHS6sBI9uNJN30bB9HjRuxpO1FM=; fh=cgAFYtQfjNb5ZyxE66wfUT+5fROTcwstqIktfnB9jq8=; b=MLkTUPgHoecUTzuf0i4WptHL8M/uqSky8oi8YhbKpfIlm4Araqwk0Dz5NHrlDGg/CX CwQI9RctmvuNT874I+FLOH/WvFnPms5wi6ZfYHtvn0IRUAN5W3Tzit0gEce1jNhuzD1c tHav87i8WATcB8lCvzMyJSX0VCZwM8QR78McqHPqMynVSkBZvlIpkc2PTkBMaffVwlk6 8SZhr/BjtfIv12zV/Jhqp2icOJm8mZ3XL/62mV1G/fIpsUjYQyGHZzfEPWCjD3UhH9nu Xqyx5mi3BtdeFV5lh1mRz2Xa99xNhSaG4NHZX7Tupdz1OEjysB6iejMVpq3/BnK5sKA5 PkdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dxqYIoKm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y8-20020a17090264c800b001b89b691c7asi7962277pli.196.2023.07.26.03.28.16; Wed, 26 Jul 2023 03:28:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dxqYIoKm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231489AbjGZKAR (ORCPT + 99 others); Wed, 26 Jul 2023 06:00:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229567AbjGZKAD (ORCPT ); Wed, 26 Jul 2023 06:00:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BF4CC1 for ; Wed, 26 Jul 2023 03:00:02 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EF29C61A25 for ; Wed, 26 Jul 2023 10:00:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03B43C433C7; Wed, 26 Jul 2023 10:00:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690365601; bh=QkLFfIXX7QKXCZKwEwSrlhmVUv0XbDTrkJ+oGtM5Z3I=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dxqYIoKmIkZKpEnvveTEkQAzUt6sKfLUy7MnlkajdbR4uHHciLFYy7CVrGrFfGgmD 84YYuH4OAELFwnbayttE/g1GmgSDZhjTPBGS/Lb+4SgIJqQIxQPv+ebZXg9PmaNLYz DtoeQYPisRr3VcDup8b7c5ROZRaNiSVII/FDwZisOIqb1rw2ioLRq6iS5vyy1AW+NW fbWRdOhloWz4BPRqGnDWzVNF4G+tkSNGeDAXbh3kAzF7/hg3Up6IkIHNWdpkZeanjY Wlir/Zbly8NIOloEyjQE9bi9ARMIWC3YAC6qjDbBOo4SW+BP31vFY7MlYSFOPSZLAN YL2eh8qiqevsQ== Date: Wed, 26 Jul 2023 11:59:58 +0200 From: Maxime Ripard To: Geert Uytterhoeven Cc: Javier Martinez Canillas , linux-kernel@vger.kernel.org, Thomas Zimmermann , Daniel Vetter , Daniel Vetter , David Airlie , dri-devel@lists.freedesktop.org Subject: Re: [PATCH v4] drm/ssd130x: Allocate buffers in the plane's .atomic_check callback Message-ID: References: <20230721070955.1170974-1-javierm@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="33ssxdeultiog556" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --33ssxdeultiog556 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Jul 25, 2023 at 09:21:52PM +0200, Geert Uytterhoeven wrote: > > --- a/drivers/gpu/drm/solomon/ssd130x.c > > +++ b/drivers/gpu/drm/solomon/ssd130x.c > > @@ -141,12 +141,26 @@ const struct ssd130x_deviceinfo ssd130x_variants[= ] =3D { > > }; > > EXPORT_SYMBOL_NS_GPL(ssd130x_variants, DRM_SSD130X); > > > > +struct ssd130x_plane_state { > > + struct drm_plane_state base; > > + /* Intermediate buffer to convert pixels from XRGB8888 to R1 */ > > + u8 *buffer; > > + /* Buffer that contains R1 pixels to be written to the panel */ > > + u8 *data_array; >=20 > The second buffer actually contains pixels in hardware format. > For now that is a transposed buffer of R1 pixels, but that will change > when you will add support for greyscale displays. >=20 > So I'd write "hardware format" instead of R1 for both. > > BTW, I still think data_array should be allocated during probing, > as it is related to the hardware, not to a plane. I somewhat disagree. If I understood right during our discussion with Javier, the buffer size derives from the mode size (height and width). In KMS, the mode is tied to the KMS state, and thus you can expect the mode to change every state commit. So the more logical thing to do is to tie the buffer size (and thus the buffer pointer) to the state since it's only valid for that particular state for all we know. Of course, our case is allows use to simplify things since it's a fixed mode, but one of Javier's goal with this driver was to provide a good example we can refer people to, so I think it's worth keeping. > > @@ -159,23 +173,23 @@ static int ssd130x_buf_alloc(struct ssd130x_devic= e *ssd130x) > > > > pitch =3D drm_format_info_min_pitch(fi, 0, ssd130x->width); > > > > - ssd130x->buffer =3D kcalloc(pitch, ssd130x->height, GFP_KERNEL); > > - if (!ssd130x->buffer) > > + ssd130x_state->buffer =3D kcalloc(pitch, ssd130x->height, GFP_K= ERNEL); > > + if (!ssd130x_state->buffer) > > return -ENOMEM; > > > > - ssd130x->data_array =3D kcalloc(ssd130x->width, pages, GFP_KERN= EL); > > - if (!ssd130x->data_array) { > > - kfree(ssd130x->buffer); > > + ssd130x_state->data_array =3D kcalloc(ssd130x->width, pages, GF= P_KERNEL); > > + if (!ssd130x_state->data_array) { > > + kfree(ssd130x_state->buffer); >=20 > Should ssd130x_state->buffer be reset to NULL here? > I.e. if .atomic_check() fails, will .atomic_destroy_state() be called, > leading to a double free? That's a good question. I never really thought of that, but yeah, AFAIK atomic_destroy_state() will be called when atomic_check() fails. Which means that it's probably broken in a lot of drivers. Also, Javier pointed me to a discussion you had on IRC about using devm allocation here. We can't really do that. KMS devices are only freed once the last userspace application closes its fd to the device file, so you have an unbounded window during which the driver is still callable by userspace (and thus can still trigger an atomic commit) but the buffer would have been freed for a while. The driver could use a bit more work on that area (by adding drm_dev_enter/drm_dev_exit) but it still creates unnecessary risks to use devm there. > > +static struct drm_plane_state *ssd130x_primary_plane_duplicate_state(s= truct drm_plane *plane) > > +{ > > + struct ssd130x_plane_state *old_ssd130x_state; > > + struct ssd130x_plane_state *ssd130x_state; > > + > > + if (WARN_ON(!plane->state)) > > + return NULL; > > + > > + old_ssd130x_state =3D to_ssd130x_plane_state(plane->state); > > + ssd130x_state =3D kmemdup(old_ssd130x_state, sizeof(*ssd130x_st= ate), GFP_KERNEL); >=20 > I know this is the standard pattern, but the "dup" part is a bit > silly here: > - The ssd130x-specific parts in ssd130x_plane_state are zeroed below, > - The base part is copied again in > __drm_atomic_helper_plane_duplicate_state). > So (for now) you might as well just call kzalloc() ;-) Still if we ever add a field in the state structure, it will be surprising that it's not copied over. The code is there and looks good to me, so I'd rather keep it. --33ssxdeultiog556 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQRcEzekXsqa64kGDp7j7w1vZxhRxQUCZMDumgAKCRDj7w1vZxhR xcgOAP9rbkUy8jWdFUWoMJgWQmcP2f7Bxf5+j2rMz97lyzqaFQD+KlUaiQmYQxpj XA/D7Omm1bNzTMaVrtoW/NcGxxU2rww= =6Jaw -----END PGP SIGNATURE----- --33ssxdeultiog556--