Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1102355rwb; Wed, 26 Jul 2023 07:38:37 -0700 (PDT) X-Google-Smtp-Source: APBJJlHDPwAJUpK57ZLLL8ioCC4owTJ8DbdgOA6F0+Az/OeAinTAKumG9y0JCXmpgQdugT1aJpAv X-Received: by 2002:a05:6a21:998e:b0:130:a15d:54eb with SMTP id ve14-20020a056a21998e00b00130a15d54ebmr2009551pzb.24.1690382316858; Wed, 26 Jul 2023 07:38:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690382316; cv=none; d=google.com; s=arc-20160816; b=P+1DjnqSpe0plzBJZCJzXifz1vgQg8CH9rdOkw14ugciB9Ikvrj/0BejZgRRWIpY3B HrpqS6QtvmeSpi9s8fuBM6U5vNF89L59l9lRXa/9oc1Y8Bjcj+Uvh9CatvhtvCYTB2F8 f8CtXIp/eBvIW06iR7wdNe5/XPHdqYWW/Sb8jx866L9m4jGvPmrOehhFAgRiWJC0Ldv0 /1hx3Yimpjh/fNzd95hMe4ZzIVaog5zRKHpfx+XxlPGJqDm6yktAWkGHSjns2KGEjkx5 PQ7Zvb/+D2sWkpPICA7FVN6PR4Q1rxvYStO3Scsoq9QRnxdWUbt1OIUCpu9TbVuht7X8 kCMA== 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=/Cn/Vk2uzm94+mNVIoB1KfY/KMElGM4ILhKOIOc4Uzc=; fh=cgAFYtQfjNb5ZyxE66wfUT+5fROTcwstqIktfnB9jq8=; b=ifwfCVAVXBlSg4lwP0u0oJcyXkY7AYH1WGg9ekzzxt4ifxpKAeBnQiZH/0MuM19DR3 kJqU3hQgTFCXWfD6Tah5RiHplj94UBzMH+oMe4Ttefs7bv56sSmY9W3/GC3is7v7UNY+ Ri+yo2EuUw5kIACp/MCoXd+H6qwH5voMxak8ldmzTRdyctjGVLytcvW1Tli3R3m/O91S aLdNMsFTSTQUVpYGyFz278UbjbhHDeSAsJ/LpTmAtoUskVr9cksYdDAUikES2eYCznOY jgmZZ1O+TTzLgZFnfDG70/ImO7QGMtBkT82bLQHHIN05/2dpvDDIxFYBFmEZcEVwOy4/ QSKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PSkx1fSG; 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 u17-20020a17090341d100b001b8bb6d9ba6si13499342ple.311.2023.07.26.07.38.23; Wed, 26 Jul 2023 07:38:36 -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=PSkx1fSG; 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 S231714AbjGZNtO (ORCPT + 99 others); Wed, 26 Jul 2023 09:49:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230274AbjGZNtN (ORCPT ); Wed, 26 Jul 2023 09:49:13 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A660F12C for ; Wed, 26 Jul 2023 06:49:12 -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 3B70F61A4F for ; Wed, 26 Jul 2023 13:49:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4CB21C433C8; Wed, 26 Jul 2023 13:49:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690379351; bh=hzmf8Bij70gVAgodLgWCiWCM75dWhG4k7zTGf0R3g9k=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=PSkx1fSGpfnBDl0HMa53d5n7KoFrCDxvrEBbCPxSj8j6gN6L1sPs0hCx+z23Fnvdu J9r6C8xITdnMOcMda8V0kaEpbaR7INOBhQAnJ/wng5F/4Fa6FJGdo3kudzXS4Y2NtJ 9biFtwHqDp35QzxlESq6ZrftU0ewwxbxHyFrZpwEs7CH8/acdhSLPZEViaKwQI5vEH uv+SGjReSmrqCzIw75FW6uWJMzB+RKecLDL0CJaeRbUtidvxTsB1dpRNgITaq+Y8LA RU6jAp/ZTCKjcRX7qjFHZmr5Un1k+ZhNKl0Hnnm4KMYo1NdAIJkFaR2RG4WxgJL40i aTOffreeEd6wQ== Date: Wed, 26 Jul 2023 15:49:09 +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="vpnwefjeratdjsnb" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 --vpnwefjeratdjsnb Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, On Wed, Jul 26, 2023 at 01:52:37PM +0200, Geert Uytterhoeven wrote: > On Wed, Jul 26, 2023 at 12:00=E2=80=AFPM Maxime Ripard wrote: > > 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_varia= nts[] =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 R= 1 */ > > > > + u8 *buffer; > > > > + /* Buffer that contains R1 pixels to be written to the pane= l */ > > > > + u8 *data_array; > > > > > > 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. > > > > > > 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. >=20 > The second buffer (containing the hardware format) has a size that > depends on the full screen size, not the current mode (I believe that's > also the size of the frame buffer backing the plane?). So its size is > fixed. In KMS in general, no. For that particular case, yes. And about the framebuffer size =3D=3D full screen size, there's multiple sizes involved. I guess what you call full screen size is the CRTC size. You can't make the assumption in KMS that CRTC size =3D=3D (primary) plane size =3D=3D framebuffer size. If you're using scaling for example, you will have a framebuffer size smaller or larger than it plane size. If you're using cropping, then the plane size or framebuffer size will be different from the CRTC size. Some ways to work around overscan is also to have a smaller plane size than the CRTC size. You don't have to have the CRTC size =3D=3D primary plane size, and then you don't have to have plane size =3D=3D framebuffer size. you can't really make that assumption in the general case either: scaling or cropping will have a different framebuffer size than the CRTC primary plane size (which doesn't have to be "full screen" either). > Given the allocations are now done based on plane state, I think the > first buffer should be sized according to the frame buffer backing > the plane? Currently it uses the full screen size, too (cfr. below). Yeah, probably. > > > > @@ -159,23 +173,23 @@ static int ssd130x_buf_alloc(struct ssd130x_d= evice *ssd130x) > > > > > > > > pitch =3D drm_format_info_min_pitch(fi, 0, ssd130x->width); > > > > > > > > - ssd130x->buffer =3D kcalloc(pitch, ssd130x->height, GFP_KER= NEL); > > > > - if (!ssd130x->buffer) > > > > + ssd130x_state->buffer =3D kcalloc(pitch, ssd130x->height, G= FP_KERNEL); >=20 > Based on full screen width and height. >=20 > > > > + if (!ssd130x_state->buffer) > > > > return -ENOMEM; > > > > > > > > - ssd130x->data_array =3D kcalloc(ssd130x->width, pages, GFP_= KERNEL); > > > > - if (!ssd130x->data_array) { > > > > - kfree(ssd130x->buffer); > > > > + ssd130x_state->data_array =3D kcalloc(ssd130x->width, pages= , GFP_KERNEL); >=20 > Based on full screen width and height (hardware page size). >=20 > > > > + if (!ssd130x_state->data_array) { > > > > + kfree(ssd130x_state->buffer); > > > > > > 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. >=20 > It should still be safe for (at least) the data_array buffer. That > buffer is only used to store pixels in hardware format, and immediately > send them to the hardware. If this can be called that late, it will > fail horribly, as you can no longer talk to the hardware at that point > (as ssd130x is an i2c driver, it might actually work; but a DRM driver > that calls devm_platform_ioremap_resource() will crash when writing > to its MMIO registers)?!? Yep, that's exactly why we have drm_dev_enter/drm_dev_exit :) Not a lot of drivers are using it but all should. vc4 does exactly what you are describing for example. Maxime --vpnwefjeratdjsnb Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQRcEzekXsqa64kGDp7j7w1vZxhRxQUCZMEkVQAKCRDj7w1vZxhR xWVDAP4y5VTZuIqxW5+rqkdxd3DgI0a0i7hK9BvwYwzjZT1l3AD/Ugz8n89RVZbk AwKQaGtmr5CKK0Lw5mWPGc+d3luMyAY= =TwRG -----END PGP SIGNATURE----- --vpnwefjeratdjsnb--