From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B53FD420E71 for ; Wed, 5 Aug 2026 10:59:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785927553; cv=none; b=rNbBHfKMM7qWvFMNEE5VVTXFREkTo8nzwMxQlIHZszaAuok6EcyQNIRus7BimWFO1Z2Un22i/OAYb0qquAh3BFBn0jYqWrspVUpcqHi1l3ZZA1jWi/o5ECCC+Q/sjERGWiPI9yRMPUi6YcwHWsPn+A/ZEqcM4BwN4dSJuseMHYA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785927553; c=relaxed/simple; bh=/1dtEFjUK+P0pvRVxCDasuHqjQBqBeraGQRhegPMj5o=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ISFVpk6tSRrQNTrZEZrEyfKE/DDCpJbyQXmzvIQ4+1PjUX8PpXbuk5JwNCDLLdcfiKLZT+/vk/TGR/3+ZbinfpZKpHJH7GrR5WMoXU8ROepsPas7WYbRtIVyuTK8Aue/3LuDPKnFndoJaRom7dPIIH0nZo/2YJvmWLz6deou9IY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SZEXOh1K; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SZEXOh1K" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-4799b3f7c83so500557f8f.2 for ; Wed, 05 Aug 2026 03:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785927546; x=1786532346; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=qChqnXEV/RUrII3F6FtB3OpL17H6S0GQj9i1G4DFOPk=; b=SZEXOh1KpuYJfaF3im7hVoma9sONKG+vqzLlaf7ooZ3+DAVgQF2n8nHiu3IBzKzRRw dp61JCqy4D2c2x5T+fFsho0Kc4LPfYzRhYnaRPQxTWRUmJNY2O3uEtNUJ9g0jtsQzB78 acwHo/V/p7Z+a/CycbdYZGZ0/BQXrBPXtv82DmWLOJ6CXmegiDSc5Vpr9pIheKRjGqMF r+UcPmzDpbWcI8H13x2BkfrjOdsL8Xsy75IrRzcWKT2gi+twbWrzsVK/oIe0qJM2wJoa yEryF/AJxdc6tNkssc5mLthTAMvFEWY1cWRMtRboKcJGJ7HfUeRlxV4Ne690/an9Vv8/ 6wYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785927546; x=1786532346; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qChqnXEV/RUrII3F6FtB3OpL17H6S0GQj9i1G4DFOPk=; b=bzMA2ks9SrhuNNwNJbHc5w0rD8xioXUppCOCJbo3PpF1e+AFFzJwNPGIbbPhduTeCs QSsWvvqJeFGvZCVKhoj5iqLo6g661k1NNl+Z39QDPiAOxtv5bfYJk2nxf4yBH2JNC0e0 8P+Uy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mXmVyA9gBqP4ZSzI3iOL6Nv1114uT X-Received: by 2002:a05:600c:4e94:b0:497:ff73:68d5 with SMTP id 5b1f17b1804b1-4994e6c5eafmr60022735e9.0.1785927545848; Wed, 05 Aug 2026 03:59:05 -0700 (PDT) Received: from ?IPV6:2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c? ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4994e035900sm80665575e9.11.2026.08.05.03.59.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Aug 2026 03:59:05 -0700 (PDT) Message-ID: <8285ede8-bebb-403a-8a37-b9987cd37a8f@gmail.com> Date: Wed, 5 Aug 2026 11:59:10 +0100 Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 01/14] dma-buf: introduce initial file I/O infrastructure To: =?UTF-8?Q?Christian_K=C3=B6nig?= , Jens Axboe , Keith Busch , Christoph Hellwig , Sagi Grimberg , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org Cc: Alexander Viro , Christian Brauner , Andrew Morton , Sumit Semwal , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Jason Gunthorpe , Damien Le Moal , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , Vishal Verma , David Sterba , Ilya Dryomov , dm-devel@lists.linux.dev, nvdimm@lists.linux.dev, linux-btrfs@vger.kernel.org, ceph-devel@vger.kernel.org References: <181b08be-04bc-4ae9-bb88-15321a82440f@amd.com> Content-Language: en-US From: Pavel Begunkov In-Reply-To: <181b08be-04bc-4ae9-bb88-15321a82440f@amd.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 8/5/26 09:27, Christian König wrote: ...>> +struct dma_buf_io_fence { >> + struct dma_fence base; >> + spinlock_t lock; >> +}; > > Upstream has change to allow embedding the spinlock into the dma_fence, so this structure here is most likely not necessary any more. ok >> +static const char *dma_buf_io_fence_drv_name(struct dma_fence *fence) >> +{ >> + /* default fence release kfree's the base pointer */ >> + BUILD_BUG_ON(offsetof(struct dma_buf_io_fence, base)); >> + >> + return "dma-buf-io-ctx"; >> +} ...>> +static void dma_buf_io_map_release_work(struct work_struct *work) >> +{ >> + struct dma_buf_io_map *map = container_of(work, struct dma_buf_io_map, >> + release_work); >> + struct dma_buf_io_fence *fence = map->fence; >> + struct dma_buf_io_ctx *ctx = map->ctx; >> + struct dma_buf *dmabuf = ctx->dmabuf; >> + >> + /* the release path must wait for fences */ >> + if (WARN_ON_ONCE(refcount_read(&ctx->refs) == 0)) >> + return; > > Stuff like that is usually illegal. Should be fine, it's just a warning. The map holds a ctx reference so can't be 0. IIRC, it was synchronised a bit differently before. I can kill it, refcount_inc() has the same warning anyway. > And why are you using refcount directly instead of kref? Not sure it'd make much difference here. > >> + >> + /* Prevent from destoying the ctx while unmapping */ >> + refcount_inc(&ctx->refs); > >> + >> + /* >> + * There are no more requests using the map, we can signal the fence. >> + * It should be done before taking the resv lock as someone could be >> + * waiting for the fence while holding the lock. >> + */ >> + dma_fence_signal(&fence->base); > > Signaling fences has a whole bunch of very strict rules associated with it. E.g. you can't alocate memory for example. > > Are you sure you actually need and want a dma_fence here? Waiting for potentially a ton of IO synchronously on invalidate sounds like a bad idea though. Hmm. >> + >> + dma_resv_lock(dmabuf->resv, NULL); >> + ctx->dev_ops->unmap(ctx, map); >> + dma_resv_unlock(dmabuf->resv); >> + >> + dma_fence_put(&fence->base); > > You should probably set map->fence to NULL after that. The map is freed two lines below, but I can add it as a defensive measure. >> + percpu_ref_exit(&map->refs); >> + kfree(map); ...>> +struct dma_buf_io_map *dma_buf_io_create_map(struct dma_buf_io_ctx *ctx) >> +{ >> + struct dma_buf *dmabuf = ctx->dmabuf; >> + struct dma_buf_io_map *map; >> + long ret; >> + >> +retry: >> + /* >> + * ->dmabuf_map() will be calling dma_buf_map_attachment(), for which >> + * we'll need to wait for fences. Do a bit nicer and try to wait >> + * without the resv lock first. >> + */ > > Clear NAK to that. Always wait while holding the resv lock if you can! > > It is absolutely not beneficial to do this outside of the lock and usually just hides problems instead and prevent fixing them. Ok ... >> + ret = dma_resv_reserve_fences(dmabuf->resv, 1); >> + if (WARN_ON_ONCE(ret)) { >> + struct dma_fence *fence = &map->fence->base; >> + >> + dma_fence_get(fence); >> + percpu_ref_kill(&map->refs); >> + dma_fence_wait(fence, false); >> + dma_fence_put(fence); >> + return; >> + } >> + >> + dma_resv_add_fence(dmabuf->resv, &map->fence->base, >> + DMA_RESV_USAGE_KERNEL); > > That sequence is clearly incorrect! > > The fence must be created after dma_resv_reserve_fences(), otherwise you definately have an illegal memory operation here. I'm not sure what you mean, can you elaborate? I only cared about pre-allocating it to avoid allocations here. We add / signal the fence only once, no reuse. The map is going to be killed here, and if we create a new map, it'll have its own fence. I can move the dma_fence_init() call here if that makes a difference? -- Pavel Begunkov