Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3197850pxb; Mon, 9 Nov 2020 05:23:25 -0800 (PST) X-Google-Smtp-Source: ABdhPJzI3ovotbflTbIjFGuUAT/guMtMiGEgpD+Uwm9wEy43j0mtGEIipJ9K6nQCsId/RwfnOeAm X-Received: by 2002:a17:906:7f95:: with SMTP id f21mr14660374ejr.340.1604928205439; Mon, 09 Nov 2020 05:23:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604928205; cv=none; d=google.com; s=arc-20160816; b=i2z7QATqxOeVVUsSQSLUGZ7H14rHcKh2SCWJnLrry4zu0ZGMdAtEFoiUNIsvzVdu7O eGKCmV9kIA2AuPNiPIbl7wb7U93G4b/e6OKC/BI17LsbMMm3v0c9QbvphPRFxevosbko zKeXEP0kCrsZR48tcjNeLWW/JFE0YLOka/tu4E2tuP+glZNdkMbgd747O4J9fk4BF4Yh iyK5JoUoKoHcjK0kdl/7udq9wuV1BzGGHG1Atv94XZNFdD5UuzQQFA1XrZl7WQMuWnMf VtLcECq/xurTWBfOKQyl5Yz2XsdtwYVLsplNznTgb5sa9hR1prIWmQoLtXsE9WK+LxV3 uaUw== 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=MRs4MmJFz6b8l0idozsMwisTyGjcugs9cnx0telnGCuAJTg6vEctiJyvN1KQronRNB AbFArPL5+WWhXg7GwF7eqNtg6FvS5z8kKCulsteZB8Ane3ij/soVDhIWjn01B10qnIFV +1FaskFB6RodAE0HT0Or0VI+k7Pz/+R88m+ZpaWqXR34JaZ0Am7VijV+hg2XkHj1AxKz TNIryUDftSZ6/KmKRZ657J3V8Nk6RYlUtMnm6exZNCx1GMTV+OKHUfB/uJrlIn2FWDbF kx53l/pnxgYaDwl1D5ZRnawS3CtZajwgKVWXawM8wOxZwwKfOqW7F9eiiteCr3uBGF65 weeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kE+w92fi; 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 o9si7101419edi.568.2020.11.09.05.23.01; Mon, 09 Nov 2020 05:23:25 -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=kE+w92fi; 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 S2388118AbgKINVp (ORCPT + 99 others); Mon, 9 Nov 2020 08:21:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:49594 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388112AbgKINVm (ORCPT ); Mon, 9 Nov 2020 08:21:42 -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 97D4620897; Mon, 9 Nov 2020 13:21:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604928101; bh=lhJw9MLSxeuMlKwr7XJWbLBrhen8ZwYinPn9KBaU/dY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kE+w92fiSA2eBksJeaayTd0YpvHOahtWBm3pXNwwV5RF3/tskkkXagVrAg4Gpzlyj aSctwkoxaUrPEyQh4qA8lB19kQ36QqO3CTuCmdiGmXgorNHxxyOwbw8fkQbRKyLqzZ udQgSAgC15G4jGOP6YeSDvb1YKq/Fyc7KSJjaHrU= 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.9 126/133] drm/panfrost: Fix a deadlock between the shrinker and madvise path Date: Mon, 9 Nov 2020 13:56:28 +0100 Message-Id: <20201109125036.748734051@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109125030.706496283@linuxfoundation.org> References: <20201109125030.706496283@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