Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp7138040yba; Thu, 2 May 2019 05:04:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqx/UO3VOUmEcwQpcX+mkcOJQJ7qQ4Dwm9lCvQSNI7RJ+2n/SLVvEoKVygaFCFT7PZNjY/P2 X-Received: by 2002:a62:2a55:: with SMTP id q82mr1110552pfq.90.1556798649887; Thu, 02 May 2019 05:04:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556798649; cv=none; d=google.com; s=arc-20160816; b=Gsuqm3mSWT60n2wkth3+8ZiuFvNdQuFqbRd4ehQu0mz+NSK6XAf44xNGAgD3ETe+tG JznEqwz3WYWMA46Ue5OW5mSM3luMyP1QexKrJxzDoXesLuEZkBOIZA2KoWhSg3e6Xk6D VT5di6Vi8kp1Z3Sx5f+/utnFBZgW4PDUPBKCuvYpPPFwIiiRJ9NKMViuwtMwNhRvGRk2 uyALvXC8ayGpLbimfOd2/9+3bCI0gimC6lP6QLl5LcyH8PQVPvMTQQR1VxgyGHNRfQMV gdwBOF84s9m8Rhjh9ihcdrCX/TdwbDc+JdCddLCzvAFY+YPptXPB2zvW6FuYh5fjUymB rBuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:organization:references:in-reply-to:date:cc:to:from :subject:message-id; bh=z8vQT8Se/P0eXIgzfZREd+0V6NcdziNz0aLp/1JxFGo=; b=xqz5swYZVE22vF97fPNWu0QlFPMMVXweEQTPoT7gwPkbZnOfJB+ebDQyXpUCSgLJKs cj7JQTxGpzFXcGz7u5P3DfD8AZ+80x8rtsAWa9+tFf8KHB7E5Qeno2ns5O/akOgMnB1G BOsZDq7GkTqEg0OjUQhVByBLfMdCPxbkMoGi9+ReJSivGkjMHHYVIAmmNfS7UhaSIsUD jXpg9whrDgW4V3XpyfFazdWV2ad3fUxnWuAh2BoHz7fN0i5d1cUr3fh/hbOYDvjyD0ei q97ya45qKeAQtj7nY567MznBhk/EkvEMCfGsdWPjYOxZmDOXYee+gbz6A0HrQCLyVC54 zMAw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v6si46029720plg.51.2019.05.02.05.03.53; Thu, 02 May 2019 05:04:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726335AbfEBMCJ (ORCPT + 99 others); Thu, 2 May 2019 08:02:09 -0400 Received: from relay11.mail.gandi.net ([217.70.178.231]:52503 "EHLO relay11.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726270AbfEBMCJ (ORCPT ); Thu, 2 May 2019 08:02:09 -0400 Received: from aptenodytes (aaubervilliers-681-1-29-145.w90-88.abo.wanadoo.fr [90.88.149.145]) (Authenticated sender: paul.kocialkowski@bootlin.com) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 88FBD10000E; Thu, 2 May 2019 12:02:06 +0000 (UTC) Message-ID: <5d8dadb34c9f845e21349253ff21c036c417f37a.camel@bootlin.com> Subject: Re: [PATCH v7 4/4] drm/vc4: Allocate binner bo when starting to use the V3D From: Paul Kocialkowski To: Eric Anholt , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Cc: David Airlie , Daniel Vetter , Maxime Ripard , Eben Upton Date: Thu, 02 May 2019 14:02:05 +0200 In-Reply-To: <87tvemj80z.fsf@anholt.net> References: <20190425122917.26536-1-paul.kocialkowski@bootlin.com> <20190425122917.26536-5-paul.kocialkowski@bootlin.com> <87tvemj80z.fsf@anholt.net> Organization: Bootlin Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, On Thu, 2019-04-25 at 10:42 -0700, Eric Anholt wrote: > Paul Kocialkowski writes: > > > The binner BO is not required until the V3D is in use, so avoid > > allocating it at probe and do it on the first non-dumb BO allocation. > > > > Keep track of which clients are using the V3D and liberate the buffer > > when there is none left, using a kref. Protect the logic with a > > mutex to avoid race conditions. > > > > The binner BO is created at the time of the first render ioctl and is > > destroyed when there is no client and no exec job using it left. > > > > The Out-Of-Memory (OOM) interrupt also gets some tweaking, to avoid > > enabling it before having allocated a binner bo. > > > > We also want to keep the BO alive during runtime suspend/resume to avoid > > failing to allocate it at resume. This happens when the CMA pool is > > full at that point and results in a hard crash. > > > > Signed-off-by: Paul Kocialkowski > > --- > > drivers/gpu/drm/vc4/vc4_bo.c | 33 +++++++++++++++++++- > > drivers/gpu/drm/vc4/vc4_drv.c | 6 ++++ > > drivers/gpu/drm/vc4/vc4_drv.h | 14 +++++++++ > > drivers/gpu/drm/vc4/vc4_gem.c | 13 ++++++++ > > drivers/gpu/drm/vc4/vc4_irq.c | 21 +++++++++---- > > drivers/gpu/drm/vc4/vc4_v3d.c | 58 +++++++++++++++++++++++++++-------- > > 6 files changed, 125 insertions(+), 20 deletions(-) > > > > diff --git a/drivers/gpu/drm/vc4/vc4_bo.c b/drivers/gpu/drm/vc4/vc4_bo.c > > index 88ebd681d7eb..2b3ec5926fe2 100644 > > --- a/drivers/gpu/drm/vc4/vc4_bo.c > > +++ b/drivers/gpu/drm/vc4/vc4_bo.c > > @@ -799,13 +799,38 @@ vc4_prime_import_sg_table(struct drm_device *dev, > > return obj; > > } > > > > +static int vc4_grab_bin_bo(struct vc4_dev *vc4, struct vc4_file *vc4file) > > +{ > > + int ret; > > + > > + if (!vc4->v3d) > > + return -ENODEV; > > + > > + if (vc4file->bin_bo_used) > > + return 0; > > + > > + ret = vc4_v3d_bin_bo_get(vc4); > > + if (ret) > > + return ret; > > + > > + vc4file->bin_bo_used = true; > > I think I found one last race. Multiple threads could be in an ioctl > trying to grab the bin BO at the same time (while this is only during > app startup, since the fd only needs to get the ref once, it's > particularly plausible given that allocating the bin BO is slow). I > think if you replace this line with: > > mutex_lock(&vc4->bin_bo_lock); > if (vc4file->bin_bo_used) { > mutex_unlock(&vc4->bin_bo_lock); > vc4_v3d_bin_bo_put(vc4); > } else { > vc4file->bin_bo_used = true; > mutex_unlock(&vc4->bin_bo_lock); > } Huh, very good catch once again, thanks! It took me some time to grasp this one, but as far as I understand, the risk is that we could ref our bin bo twice (although it would only be allocated once) since bin_bo_used is not protected. I'd like to suggest another solution, which would avoid re-locking and doing an extra put if we got an extra ref: adding a "bool *used" argument to vc4_v3d_bin_bo_get and, which only gets dereferenced with the bin_bo lock held. Then we can skip obtaining a new reference if (used && *used) in vc4_v3d_bin_bo_get. So we could pass a pointer to vc4file->bin_bo_used for vc4_grab_bin_bo and exec->bin_bo_used for the exec case (where there is no such issue since we'll only ever try to _get the bin bo once there anyway). What do you think? Cheers, Paul > that will be the last change we need. If you agree with this, feel free > to squash it in and apply the series with: > > Reviewed-by: Eric Anholt > > > + > > + return 0; > > +} > > + > > int vc4_create_bo_ioctl(struct drm_device *dev, void *data, > > struct drm_file *file_priv) > > { > > struct drm_vc4_create_bo *args = data; > > + struct vc4_file *vc4file = file_priv->driver_priv; > > + struct vc4_dev *vc4 = to_vc4_dev(dev); > > struct vc4_bo *bo = NULL; > > int ret; > > > > + ret = vc4_grab_bin_bo(vc4, vc4file); > > + if (ret) > > + return ret; > > + > > /* > > * We can't allocate from the BO cache, because the BOs don't > > * get zeroed, and that might leak data between users. > > @@ -846,6 +871,8 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data, > > struct drm_file *file_priv) > > { > > struct drm_vc4_create_shader_bo *args = data; > > + struct vc4_file *vc4file = file_priv->driver_priv; > > + struct vc4_dev *vc4 = to_vc4_dev(dev); > > struct vc4_bo *bo = NULL; > > int ret; > > > > @@ -865,6 +892,10 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data, > > return -EINVAL; > > } > > > > + ret = vc4_grab_bin_bo(vc4, vc4file); > > + if (ret) > > + return ret; > > + > > bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER); > > if (IS_ERR(bo)) > > return PTR_ERR(bo); > > @@ -894,7 +925,7 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data, > > */ > > ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle); > > > > - fail: > > +fail: > > drm_gem_object_put_unlocked(&bo->base.base); > > > > return ret; > > Extraneous whitespace change? -- Paul Kocialkowski, Bootlin Embedded Linux and kernel engineering https://bootlin.com