Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3618080rdb; Wed, 13 Sep 2023 18:54:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFBRPKfSaFwmie37Kq0nfmS/nJBI/rhdaYOSCpxwc2slF5Wmst2fjtSyCrf5sa+ay2ZIvjq X-Received: by 2002:a17:902:f688:b0:1c3:3b5c:1fbf with SMTP id l8-20020a170902f68800b001c33b5c1fbfmr5906239plg.10.1694656468988; Wed, 13 Sep 2023 18:54:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694656468; cv=none; d=google.com; s=arc-20160816; b=ZcpToLSLcfszRQxxSYlqiyjkTBIhTOLCLaRS3NgCqBIMlaSoMOnuCeevrIz9gAMWWL /DtsE+sf2z8KzSDIpo8C7Vx/qNLTvp03BJb2pLPZ7vO3hXEFgwwD8djq3EwkGDa9LRyp gnw2ADYbNnsTj1vcwk0reC91xdtvp5uvNnn6rT+UADWUoqj6GC5Oya0y3yBpd/cadhWB w9WTC6GVpQkEISYf6iAzzHoa2I+FQnsSiT+jQ2bGxs8HSLR8/Ufzp6HxAP2ljldBkWJe ers+OJHw/K/0eclEsvAd0/H0Obq7t8Lyyzb6WbNIvt9e7MXShfpZ8hebL/rFXxZ23Mtn LC+A== 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 :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=9OraKXxV3WffbqPzxN3XYx9bwTTqUoi0i3oo3TBLMwQ=; fh=uxLNaPoPRUzmkUc1KOgGwCEU/bTSjK8Yed2Up6ovXLY=; b=LtV/Loyjnmu5yAW5ViaZnj/drsqFpj/IReW5S3+H62ctY354rLF+5j83dewTAw6Fti vCixW9EZBMo5Y13Y2ySuqsWWP+M8mTN0pCC2ccGtAUXS1w6Z0rRWKEtueu9VdLGZQsRe Izt2go8zNXR274xozE3Gu3j2spQQIEUAJRM86Ls34UpSyzOrCaZ0YDOsGv8ZS0c8Wasi 6fScFKTmcSyZkRRYT+QTOt+hvySCkpCTwuTTEIvNcgVEoCo5QydGaQK6akkS/L58HFhD H09quyDpoJl12GdYzLCRyK3ikgDyN+UNH0K70GcvpphtBM3oW4Izz9tjC03asxMht8Kw gtkg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b="Ny8/LDYy"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id ky11-20020a170902f98b00b001bb8f59aca3si522098plb.583.2023.09.13.18.54.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 18:54:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b="Ny8/LDYy"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id EB73E802DA82; Wed, 13 Sep 2023 00:48:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238645AbjIMHsl (ORCPT + 99 others); Wed, 13 Sep 2023 03:48:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238622AbjIMHsk (ORCPT ); Wed, 13 Sep 2023 03:48:40 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 723B71989 for ; Wed, 13 Sep 2023 00:48:36 -0700 (PDT) Received: from localhost (unknown [IPv6:2a01:e0a:2c:6930:5cf4:84a1:2763:fe0d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by madras.collabora.co.uk (Postfix) with ESMTPSA id 88D0F6607319; Wed, 13 Sep 2023 08:48:34 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1694591315; bh=EGLX0+KxN7hUT9YpTYg5LgU9mcegZL1wrYeI+JeqTiA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Ny8/LDYyxVRceVVvo1mDSez4K8treZJHNLwGat1Kx7Ch8uyC4nbACQ38l2HmCMepj 9EmeCTADZ9KYKBjFmJ3llXRgUxX/n4xa1IzopwnvvTH0KtaLYHKWMH12Xqr1/2GLOY 2iWGVJDd5Pt7oAAh4k0jw0iMGEMoEbkyD4HBGcm3ZOoWUOporKCd1xJJ7+eqyEQGkQ jBS9nqhtV09nauFdB/+3HKQJeNuCMHSzPGLO4zV6Nmey7D1mmu0ZEXCmNeM7HT9o9c rGp9yblZmNpUHGWmvZBkgXR2a96y+P4UUFtNVe6n2u71/F3iptvf/XVusa2clwzM3a CSneCkV3NNjWg== Date: Wed, 13 Sep 2023 09:48:32 +0200 From: Boris Brezillon To: Dmitry Osipenko Cc: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Christian =?UTF-8?B?S8O2bmln?= , Qiang Yu , Steven Price , Emma Anholt , Melissa Wen , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org Subject: Re: [PATCH v16 15/20] drm/shmem-helper: Add memory shrinker Message-ID: <20230913094832.3317c2df@collabora.com> In-Reply-To: <26f7ba6d-3520-0311-35e2-ef5706a98232@collabora.com> References: <20230903170736.513347-1-dmitry.osipenko@collabora.com> <20230903170736.513347-16-dmitry.osipenko@collabora.com> <20230905100306.3564e729@collabora.com> <26f7ba6d-3520-0311-35e2-ef5706a98232@collabora.com> Organization: Collabora X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 13 Sep 2023 00:48:41 -0700 (PDT) On Wed, 13 Sep 2023 03:56:14 +0300 Dmitry Osipenko wrote: > On 9/5/23 11:03, Boris Brezillon wrote: > >> * But > >> + * acquiring the obj lock in drm_gem_shmem_release_pages_locked() can > >> + * cause a locking order inversion between reservation_ww_class_mutex > >> + * and fs_reclaim. > >> + * > >> + * This deadlock is not actually possible, because no one should > >> + * be already holding the lock when drm_gem_shmem_free() is called. > >> + * Unfortunately lockdep is not aware of this detail. So when the > >> + * refcount drops to zero, don't touch the reservation lock. > >> + */ > >> + if (shmem->got_pages_sgt && > >> + refcount_dec_and_test(&shmem->pages_use_count)) { > >> + drm_gem_shmem_do_release_pages_locked(shmem); > >> + shmem->got_pages_sgt = false; > >> } > > Leaking memory is the right thing to do if pages_use_count > 1 (it's > > better to leak than having someone access memory it no longer owns), but > > I think it's worth mentioning in the above comment. > > It's unlikely that it will be only a leak without a following up > use-after-free. Neither is acceptable. Not necessarily, if you have a page leak, it could be that the GPU has access to those pages, but doesn't need the GEM object anymore (pages are mapped by the iommu, which doesn't need shmem->sgt or shmem->pages after the mapping is created). Without a WARN_ON(), this can go unnoticed and lead to memory corruptions/information leaks. > > The drm_gem_shmem_free() could be changed such that kernel won't blow up > on a refcnt bug, but that's not worthwhile doing because drivers > shouldn't have silly bugs. We definitely don't want to fix that, but we want to complain loudly (WARN_ON()), and make sure the risk is limited (preventing memory from being re-assigned to someone else by not freeing it).