From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 378993BBA0F; Thu, 7 May 2026 15:03:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.251.105.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778166233; cv=none; b=LE5QNyBgPeUmJkt8er4BXfR0K1aeJ8nD9e7BpWO+sLReBO9IgdWnN/oR0+d17YgRsFyyyfFEQT9YpAAGkAwMcOfgN+hxUhZZzf2NINJGVd0E9L2CSJT3Jw5eCEvLJQkWiGamA+68k4/UniB6qyyOIQ5sAqT2RAqzawKfx5o8G6A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778166233; c=relaxed/simple; bh=EoEm50eq18e3JDHSSBDVklCEN8e3aV3FIWaW7RmeUkc=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WTtbvwMmq+TpEBXbqW6R9nOiX6kBcZqAERi07OfN1QITRagGVa9IwIio6UX+rOQ2Ewrhej7BSMlkXW6QeWQkE1rgmjh3lQTpaFmDTaAF6O0p1low0T2gZI/cbs91zDLu1gvzwpijD1G+4IJ5Q+uQT6GSO60TQimpAcGvlfVpX3E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=Tgcl6Rx6; arc=none smtp.client-ip=148.251.105.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="Tgcl6Rx6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1778166228; bh=EoEm50eq18e3JDHSSBDVklCEN8e3aV3FIWaW7RmeUkc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Tgcl6Rx6RUwgIgvHBRoTqoUS8RYyZeNRieaTPs7AscQKz8h28tpcnt5eHLaKf5UMF tAF/Gh85D3rtNa2HHmTxTY6E4/SSI37ekgLPteUh6i9FsDIjxfwRNzdwhxMx6aRq8L LS0KAkxnkQ7c1Lt1F+evXMlSNxUd3cV3q4u4pgU/q/wRmLj1r5niD+bvlV+goDu+hr gUQlXIqBilYqfOkC1V7tFjmkqjWAh0bOi71ovlE+hFEGeUTrj7+rbHs3FmiXzS7SQV 9HqGcxnKXQUsLgfYlIpcbPBVTvn5fijyml78CZjm6QKsRE7W7NDi+qzOfHicbxP3X8 tHfqvoWanU8sw== Received: from fedora (unknown [100.64.0.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by bali.collaboradmins.com (Postfix) with ESMTPSA id D46B617E124B; Thu, 7 May 2026 17:03:47 +0200 (CEST) Date: Thu, 7 May 2026 17:03:43 +0200 From: Boris Brezillon To: Liviu Dudau Cc: dri-devel@lists.freedesktop.org, Steven Price , Dmitry Osipenko , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Akash Goel , Chia-I Wu , Rob Clark , Dmitry Baryshkov , Abhinav Kumar , Jessica Zhang , Sean Paul , Marijn Suijten , linux-arm-msm@vger.kernel.org, freedreno@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/3] drm/panthor: Don't use the racy drm_gem_lru_remove() helper Message-ID: <20260507170343.044934a0@fedora> In-Reply-To: References: <20260506-panthor-shrinker-fixes-v1-0-e7721526de96@collabora.com> <20260506-panthor-shrinker-fixes-v1-1-e7721526de96@collabora.com> <20260507141027.166ab00d@fedora> Organization: Collabora X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-redhat-linux-gnu) Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 7 May 2026 15:40:51 +0100 Liviu Dudau wrote: > On Thu, May 07, 2026 at 02:10:27PM +0200, Boris Brezillon wrote: > > On Thu, 7 May 2026 11:01:25 +0100 > > Liviu Dudau wrote: > > > > > On Wed, May 06, 2026 at 02:16:26PM +0200, Boris Brezillon wrote: > > > > drm_gem_lru_remove() dereference stores drm_gem_object::lru in a local > > > > variable that's then dereferenced to acquire the LRU lock. Because this > > > > assignment in done without the LRU lock held, it can race with > > > > drm_gem_lru_scan() where drm_gem_object::lru is temporarily assigned > > > > a stack-allcated LRU that goes away when leaving the function. By > > > > the time we dereference this local lru variable, the object might already > > > > be gone. > > > > > > > > It feels like drm_gem_lru_move_tail() was never meant to be used this > > > > way, because there's no easy way we can avoid this race unless we defer > > > > the locking to the caller. Let's add an explicit LRU for unreclaimable > > > > BOs instead, and have all BOs added to this LRU at creation time. > > > > > > I would argue that drm_gem_lru_scan() is broken by design. If you're going > > > to release the LRU lock in the middle of a loop you can expect that someone > > > will get hold of your stack-allocated LRU and end up picking the pieces. > > > > I think it's fine as long as you always use the drm_gem_lru helpers to > > manipulate the lru field, which is true of a lot of kernel constructs. > > I think drm_gem_lru_scan() should never set an object's lru field to still_in_lru. > It should set it to NULL when the object's node is removed from its lru and add > it into still_in_lru without making the drm_gem_object->lru to point back to it. > At the very end when we splice back the still_in_lru list back into lru's list we > can then update obj->lru. Then you run into another race between drm_gem_lru_scan() and drm_gem_object_release(), where the LRU removal in _release() is skipped because obj->lru is NULL, and all of a sudden, the still_in_lru list has an element that's freed. Honestly, I don't think obj->lru pointing to a stack allocated object is a problem as long as we don't let gem users play freely with obj->lru (which we shouldn't do anyway). > > > > > > This patch is fine in itself by trying to avoid stepping into the fight, > > > but I think we should also add a warning in drm_gem_lru_scan() for future > > > users to be aware of the dangers. > > > > Warning the user about what? There's nothing they can do about it, and > > I don't even think it's unsafe per-se, unless someone goes off and > > stores the drm_gem_object::lru value somewhere else while their shrink() > > callback is called, and accesses it later, outside the shrinker path. > > Given drm_gem_lru is not refcounted, there's no way one could safely > > hold on the LRU they saw in the shrink() callback anyway, so I don't > > think that's fair to blame the drm_gem_lru API for this kind of misuse. > > Yeah, that would be the warning: don't store the object's lru as you might > get a temporary one that will become invalid after the shrinker has run. Oh, you mean a comment explaining this should be avoided, not an actual drm_warn(). Then yes, I think it's fine to document the expectations in the drm_gem_object::lru doc.