Received: by 2002:a05:7412:3290:b0:fa:6e18:a558 with SMTP id ev16csp29242rdb; Thu, 25 Jan 2024 07:23:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeYNEfJ+guuUSmreCStPwgPWKn8YlC6XHjkEgch6t140Y0KLz18HKQVnpvr3+0kR23RPNT X-Received: by 2002:a17:907:9492:b0:a31:5b30:bb07 with SMTP id dm18-20020a170907949200b00a315b30bb07mr1625831ejc.65.1706196202339; Thu, 25 Jan 2024 07:23:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706196202; cv=pass; d=google.com; s=arc-20160816; b=sa3v/OGPyn9vrzQ4pCKVbzQMDeTDZ7c7z0k0U7U8BuIadaVYyqKag/f9Ipt2x2p102 ejYrISvbjiZZ0w8eJDPVn8saxKjMc2vt2upNhWq1Dbsl4l3KtLNhN+LnPjZIhd1y4qW4 duHdcrgUExkI3TDtOJd37Jaevh+Z4am2ehn/pFcJtjwl2FrpPyid9YJbe73+4i0HEjDf eAh7qBkESMd3p5FjhwFQqN8xEtpN1fCRupVVuzpQsfGZ2GUUDxGycGVGrhe7hm6CHkJf 6UEbCCUVyX5Gm2PSAtBLPZImHSjFzRQLQb52UmTFtBF3MmvrdoHpyHfSzGhPBM/fMRu0 wBjA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=3uCm+3uv8GvMxy4OgNybvMflNJ+eaPMs5XH+LYlHVzk=; fh=gYnJk9rrTj8DqlYlSRmyE1+XNlCTxPOj5h/9JSN7oMo=; b=XMlprYmwdhfJqbG4T9bax5BTxUut0fy8zTA+kFZm3gCoYqGhBzgAnVeWb2zwqbR1qG PfmjWwwNEHcNTiXr38IlFkFl2UPYVEL48/2r9cJAqhSd16yOa/1phNhu45V6aYcMilR9 w6SNk7OzgdM5GSRV0gm+K+971kGldvDNJs/s87G2+ppDVU1SyDQKSvnqQWKf0Uw3qU96 ovYldJycVJRBBreEIswNhgjxU7QisE8cmRrBZG18d8h8rls+5YeHb58mjRuOdOQrEdHa 8m7YU1oy6QlciZ5JJPWLzGTj9h8LCAfymmuZDPo7CHmBmotzBbHIO6WsX3sYObOVluTk X3fw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=GMN1U2t2; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-38822-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38822-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id p11-20020a1709060dcb00b00a26ebacaaffsi1058483eji.552.2024.01.25.07.23.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 07:23:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38822-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@ti.com header.s=ti-com-17Q1 header.b=GMN1U2t2; arc=pass (i=1 spf=pass spfdomain=ti.com dkim=pass dkdomain=ti.com dmarc=pass fromdomain=ti.com); spf=pass (google.com: domain of linux-kernel+bounces-38822-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38822-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=ti.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 1FEA61F25621 for ; Thu, 25 Jan 2024 15:23:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6B88A73160; Thu, 25 Jan 2024 15:23:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="GMN1U2t2" Received: from fllv0015.ext.ti.com (fllv0015.ext.ti.com [198.47.19.141]) (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 E7A056EB59; Thu, 25 Jan 2024 15:23:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.19.141 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196192; cv=none; b=J1OmccDxwwlXzh1aNceg6rEOaZ+Nk6GWY4Kl+lTMZPzPGUfhPKR4bIJG0MMZ05kQw31mXN40ZJmSVYss/R4DxgoR18eqbOfyufwBVegRMFBYcgEe374fTLU0aHsWG/D8AYhNeV9/L/igOAe95hjakasuGRa0qkt9mVlk79Qjt6U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706196192; c=relaxed/simple; bh=nHHvVom1q2NfxuRV3Y+LvpL/ZmkiaOxOnUB91cTyLtk=; h=Message-ID:Date:MIME-Version:Subject:To:CC:References:From: In-Reply-To:Content-Type; b=cbRTMQPA8uH+ERMqGYcta/lCW0V/Ni4OysFFIRj/lSptnnr0+Ok80XPtfnZoaRftC8YKu3Eq/7Pt5fzdg2CqWgd6QxAQso7zE2Ztdpe5IEhCZRE8618KSaFxEf05UmU9pEtDalU0Qzqfmt2GyplVowPrPlAT1w0gGvKlrT+3h0k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b=GMN1U2t2; arc=none smtp.client-ip=198.47.19.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Received: from lelv0266.itg.ti.com ([10.180.67.225]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id 40PFMxEB029810; Thu, 25 Jan 2024 09:22:59 -0600 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1706196179; bh=3uCm+3uv8GvMxy4OgNybvMflNJ+eaPMs5XH+LYlHVzk=; h=Date:Subject:To:CC:References:From:In-Reply-To; b=GMN1U2t2nH/8OoBPHHzd8rA7ZqBV9C5ul0BVQm6Wfau2GNcBiwiIEQztFf9trtH+a 2VKObYP8D2NJhkUO/SgX/GzUhkx9C2Bv6VEZtVIm2hcYclBoEkqBmY3KYzEAEl9Ykf ngDTXrObEd5MQccdKVZyg/F5/AF+//T5lBd0QkBc= Received: from DFLE106.ent.ti.com (dfle106.ent.ti.com [10.64.6.27]) by lelv0266.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 40PFMxct048139 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 25 Jan 2024 09:22:59 -0600 Received: from DFLE111.ent.ti.com (10.64.6.32) by DFLE106.ent.ti.com (10.64.6.27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Thu, 25 Jan 2024 09:22:59 -0600 Received: from lelvsmtp6.itg.ti.com (10.180.75.249) by DFLE111.ent.ti.com (10.64.6.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23 via Frontend Transport; Thu, 25 Jan 2024 09:22:59 -0600 Received: from [10.249.42.149] ([10.249.42.149]) by lelvsmtp6.itg.ti.com (8.15.2/8.15.2) with ESMTP id 40PFMwBh079463; Thu, 25 Jan 2024 09:22:58 -0600 Message-ID: <9bb17376-7793-4cb1-984c-adffb1c7ab1e@ti.com> Date: Thu, 25 Jan 2024 09:22:58 -0600 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/3] udmabuf: Keep track current device mappings Content-Language: en-US To: "Kasireddy, Vivek" , Gerd Hoffmann , Sumit Semwal , =?UTF-8?Q?Christian_K=C3=B6nig?= , Paul Cercueil CC: "linaro-mm-sig@lists.linaro.org" , "linux-kernel@vger.kernel.org" , "dri-devel@lists.freedesktop.org" , "linux-media@vger.kernel.org" References: <20240123221227.868341-1-afd@ti.com> From: Andrew Davis In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 7bit X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 On 1/24/24 4:36 PM, Kasireddy, Vivek wrote: > Hi Andrew, > >> When a device attaches to and maps our buffer we need to keep track >> of this mapping/device. This is needed for synchronization with these >> devices when beginning and ending CPU access for instance. Add a list >> that tracks device mappings as part of {map,unmap}_udmabuf(). >> >> Signed-off-by: Andrew Davis >> --- >> drivers/dma-buf/udmabuf.c | 43 >> +++++++++++++++++++++++++++++++++++++-- >> 1 file changed, 41 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c >> index c406459996489..3a23f0a7d112a 100644 >> --- a/drivers/dma-buf/udmabuf.c >> +++ b/drivers/dma-buf/udmabuf.c >> @@ -28,6 +28,14 @@ struct udmabuf { >> struct page **pages; >> struct sg_table *sg; >> struct miscdevice *device; >> + struct list_head attachments; >> + struct mutex lock; >> +}; >> + >> +struct udmabuf_attachment { >> + struct device *dev; >> + struct sg_table *table; >> + struct list_head list; >> }; >> >> static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf) >> @@ -120,14 +128,42 @@ static void put_sg_table(struct device *dev, struct >> sg_table *sg, >> static struct sg_table *map_udmabuf(struct dma_buf_attachment *at, >> enum dma_data_direction direction) >> { >> - return get_sg_table(at->dev, at->dmabuf, direction); >> + struct udmabuf *ubuf = at->dmabuf->priv; >> + struct udmabuf_attachment *a; >> + >> + a = kzalloc(sizeof(*a), GFP_KERNEL); >> + if (!a) >> + return ERR_PTR(-ENOMEM); >> + >> + a->table = get_sg_table(at->dev, at->dmabuf, direction); >> + if (IS_ERR(a->table)) { >> + kfree(a); >> + return a->table; > Isn't that a use-after-free bug? Indeed it is, will fix. Seems coccicheck also caught this but I missed it when reviewing its output, my bad :( Andrew > Rest of the patch lgtm. > > Thanks, > Vivek > >> + } >> + >> + a->dev = at->dev; >> + >> + mutex_lock(&ubuf->lock); >> + list_add(&a->list, &ubuf->attachments); >> + mutex_unlock(&ubuf->lock); >> + >> + return a->table; >> } >> >> static void unmap_udmabuf(struct dma_buf_attachment *at, >> struct sg_table *sg, >> enum dma_data_direction direction) >> { >> - return put_sg_table(at->dev, sg, direction); >> + struct udmabuf_attachment *a = at->priv; >> + struct udmabuf *ubuf = at->dmabuf->priv; >> + >> + mutex_lock(&ubuf->lock); >> + list_del(&a->list); >> + mutex_unlock(&ubuf->lock); >> + >> + put_sg_table(at->dev, sg, direction); >> + >> + kfree(a); >> } >> >> static void release_udmabuf(struct dma_buf *buf) >> @@ -263,6 +299,9 @@ static long udmabuf_create(struct miscdevice >> *device, >> memfd = NULL; >> } >> >> + INIT_LIST_HEAD(&ubuf->attachments); >> + mutex_init(&ubuf->lock); >> + >> exp_info.ops = &udmabuf_ops; >> exp_info.size = ubuf->pagecount << PAGE_SHIFT; >> exp_info.priv = ubuf; >> -- >> 2.39.2 >