Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3201585pxb; Mon, 9 Nov 2020 05:28:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJxpy8CLrEpdooN5Huwoab3qZOMMM/EN8i9cH7VFHg85QEEOzKtvGfgehJjY8w4a9UeHh0Hm X-Received: by 2002:a17:907:1008:: with SMTP id ox8mr14695000ejb.189.1604928532115; Mon, 09 Nov 2020 05:28:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604928532; cv=none; d=google.com; s=arc-20160816; b=R+CJBBbUHWOqAy6LrTNJh8Hl0WIWaDdAksPl2bekds6mQG/BKXAy/zV5gwPDFcpxmc BxSGVJ5VNn7CMUZSf4vm9XAjifBK6kN2al8UER2quf0SO0wZuMDxBW4zFgJZ3zn0T2mT pzfVjC08hRjWRqOfZvkZ03fG7pwRxxjoXpYg/l0HEQKTb1qb95OwV/88lu6aMLLE0jP+ CHDYbsxknCm7fAs23etQSWq7YD32EHpepP95ffuX2eXHzZV6Z/EPRr+hAdBbHbcFM7Iy KYCZdLUajxHcF3sp/gYBZ0ws9NHtUh5E+s9XrtWqJR403t9FcD01syydLVE4oDM3UiFc DFLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=CFgtDRXm/7pX48P3AbJhx4ZnGK3O+JZbHvR6e/1V1vw=; b=jqQGbiRp13N3mOBRFoQ0q8TlMpz8QiV0qMwkM8fcGAnOGMBLu6jgTX8+sTt+1tYxe+ LZ3HOvNNEUTgH+/WXO4g//qX9MVblFof25ybl8bfLuGUuG6uAcBWgfjTM3xGVm7eKbbD i1PzY8DXq7gXuiAVXRd/xbRbidDKQxHoDuOMJU82bLesRDAwY1ZxS72OxTjnvQBn/3Pg U5mD7/8DKwrctXTVh4PVLn/Mt4AQkcbjfNWm49VBZsd1e+1qxGabnyGenz0eGQptuZ5/ sw2+1Ygrsz+S3B2cYwFdJqqCGW39M4gmfBxTagamDx9RZdww28Lnu88Bby4e7H8U0GCt kEFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="WBz/o7ub"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l19si7066517edr.128.2020.11.09.05.28.28; Mon, 09 Nov 2020 05:28:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="WBz/o7ub"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733272AbgKINPD (ORCPT + 99 others); Mon, 9 Nov 2020 08:15:03 -0500 Received: from mail.kernel.org ([198.145.29.99]:41580 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731267AbgKINPB (ORCPT ); Mon, 9 Nov 2020 08:15:01 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8095C20663; Mon, 9 Nov 2020 13:14:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604927700; bh=lhJw9MLSxeuMlKwr7XJWbLBrhen8ZwYinPn9KBaU/dY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WBz/o7ub1u1/p0Ib4uGbKwgSBapq56u0V4IeVFY70z/cBkucQCWCNKdcTu2jZ47QR YbU6rpKIl8A8sYaW/2nOzLjtw9gvQ/yREeY8hgIx13ckgpyNHCN4t9QinQaqiP+9vo olttbGArJJ6nkdid1MOfeH5Zuy+brV8Wsmw79l0Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Christian Hewitt , Boris Brezillon , Steven Price Subject: [PATCH 5.4 78/85] drm/panfrost: Fix a deadlock between the shrinker and madvise path Date: Mon, 9 Nov 2020 13:56:15 +0100 Message-Id: <20201109125026.325891765@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125022.614792961@linuxfoundation.org> References: <20201109125022.614792961@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Boris Brezillon commit 7d2d6d01293e6d9b42a6cb410be4158571f7fe9d upstream. panfrost_ioctl_madvise() and panfrost_gem_purge() acquire the mappings and shmem locks in different orders, thus leading to a potential the mappings lock first. Fixes: bdefca2d8dc0 ("drm/panfrost: Add the panfrost_gem_mapping concept") Cc: Cc: Christian Hewitt Reported-by: Christian Hewitt Signed-off-by: Boris Brezillon Reviewed-by: Steven Price Link: https://patchwork.freedesktop.org/patch/msgid/20201101174016.839110-1-boris.brezillon@collabora.com Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/panfrost/panfrost_gem.c | 4 +--- drivers/gpu/drm/panfrost/panfrost_gem.h | 2 +- drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c | 14 +++++++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) --- a/drivers/gpu/drm/panfrost/panfrost_gem.c +++ b/drivers/gpu/drm/panfrost/panfrost_gem.c @@ -105,14 +105,12 @@ void panfrost_gem_mapping_put(struct pan kref_put(&mapping->refcount, panfrost_gem_mapping_release); } -void panfrost_gem_teardown_mappings(struct panfrost_gem_object *bo) +void panfrost_gem_teardown_mappings_locked(struct panfrost_gem_object *bo) { struct panfrost_gem_mapping *mapping; - mutex_lock(&bo->mappings.lock); list_for_each_entry(mapping, &bo->mappings.list, node) panfrost_gem_teardown_mapping(mapping); - mutex_unlock(&bo->mappings.lock); } int panfrost_gem_open(struct drm_gem_object *obj, struct drm_file *file_priv) --- a/drivers/gpu/drm/panfrost/panfrost_gem.h +++ b/drivers/gpu/drm/panfrost/panfrost_gem.h @@ -82,7 +82,7 @@ struct panfrost_gem_mapping * panfrost_gem_mapping_get(struct panfrost_gem_object *bo, struct panfrost_file_priv *priv); void panfrost_gem_mapping_put(struct panfrost_gem_mapping *mapping); -void panfrost_gem_teardown_mappings(struct panfrost_gem_object *bo); +void panfrost_gem_teardown_mappings_locked(struct panfrost_gem_object *bo); void panfrost_gem_shrinker_init(struct drm_device *dev); void panfrost_gem_shrinker_cleanup(struct drm_device *dev); --- a/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c +++ b/drivers/gpu/drm/panfrost/panfrost_gem_shrinker.c @@ -40,18 +40,26 @@ static bool panfrost_gem_purge(struct dr { struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); struct panfrost_gem_object *bo = to_panfrost_bo(obj); + bool ret = false; if (atomic_read(&bo->gpu_usecount)) return false; - if (!mutex_trylock(&shmem->pages_lock)) + if (!mutex_trylock(&bo->mappings.lock)) return false; - panfrost_gem_teardown_mappings(bo); + if (!mutex_trylock(&shmem->pages_lock)) + goto unlock_mappings; + + panfrost_gem_teardown_mappings_locked(bo); drm_gem_shmem_purge_locked(obj); + ret = true; mutex_unlock(&shmem->pages_lock); - return true; + +unlock_mappings: + mutex_unlock(&bo->mappings.lock); + return ret; } static unsigned long