From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej2-f12.google.com (mail-ej2-f12.google.com [74.125.228.140]) (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 00535472782 for ; Mon, 5 Oct 2026 09:56:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.228.140 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791194166; cv=none; b=V9bF/pJlSwvrJELNvrlBonxIWNgSCq9skM/ufS1gdSSeOO19nVhgriataskYs10EhAHsGlgCOwRAJCFec3y8aMsM0kCY4Ac+p1b2CUgEiQt7RjGUnA93htzw92uIiOsFswFYLqN2sYURkUuHN5sLZONDJd+IcbuHcE6MaIzl1hw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791194166; c=relaxed/simple; bh=W+faagf9X6giuP7Jvxi6Qt0uOgcW4oIP8+vZrnvBrKc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LlJR4RkyebNEZj4cmc24qQuKoRlKQEc0EclMsWd0OngQMru+OPQQjxJmM+HivIyadlxbWZ9k0wCT+IBWvwRV0126gKBSny7B9Q7pyztw4dk6m+pzfB37j0Sn/ZATiM7Psr6Efccfg7GJVnF1jACpkS9UpJd934qybho3JAu1AcA= 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=R4boc6Wm; arc=none smtp.client-ip=74.125.228.140 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="R4boc6Wm" Received: by mail-ej2-f12.google.com with SMTP id a640c23a62f3a-c29703cb470so266547566b.0 for ; Mon, 05 Oct 2026 02:56:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791194158; x=1791798958; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=lD6pTqRDMSfKXNqb4UUFNdsEKglv/1NbFFOX0KLFSbY=; b=R4boc6WmXoOAcU08VrXFZ0meOdcCTFkRq5SxerArgCeiozC47cY089LLcDzhol20Ch 7gL8IHouyBAVV0Kbsu55Df3e5gmT+Pmm/bjjIAmvPB+zF8WG3uQ0EYAavig6Y01QJH/G vTzw3rXEYMZiB3YLI2AbsFo8wzQK5bxUBKMB5tlnGxHvOU5E0cpa138GaHtdJJjIohSc ueGoeg9I9WZI50bldWwelbEE+NQkZpVxlCyPlefmNKUa9YhrhNa4PKFozJg2E5fl8JBG JVijrLKLszaSsXSi0hN1yeXqTi4STr44vdpkmb7eJC2e7fKh8Bl59YlbZKPvsMy+LC34 u4pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791194158; x=1791798958; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=lD6pTqRDMSfKXNqb4UUFNdsEKglv/1NbFFOX0KLFSbY=; b=o+eRRIm1nA2zBNvDeBPw+CprsGEAuL+WRifFbTE0AcIyCU+FE3vR+F1Nyy9nDJDxFI z3WJA9/2A45KI74QP7IirdjfA913nRV5p4xsjiBM2COBpz386GJEELpeHyk9jHgXeKZR 63tYv2+9E3xom7xvxVjn9R5+f2IW4GScerZhNQtg+mwXKCiPqa3sSp6u7zsowPSjLlzZ /IG1VxltyfOXjpiRGXm8gD2x3qSQdYfUURjO8hiuy+AbZhwlEM+Rx1n9/b21e5wT40D4 wAGuSRsPBnLYWwxoRjayOpLKDSWXtvKqR5tsRWME7jFR6z8v9PCxUZlaO5kftdaoqMkL ab7A== X-Forwarded-Encrypted: i=1; AKwUvBzy4NRza1TwcxeZj9Ve+dpsl4yP8ytUNAnuxwHBFe61gtFwPFUthfB6U34ASBBqVcIZQtTnQw==@lists.linux.dev X-Gm-Message-State: AFq9FYIB20b0lggKv47pq8NZspToJV67Td6lbNYUVlI7JSwCIbNrhkI4 NP2qvpsMOcbgBlaPFTf4kMy0I2ID2VdaQtX1wg5FssxdcbghPnSBN4YL X-Gm-Gg: AYBFou0j8Pcw022+xXkZKXATEu5AKL8Ux99w5tKGUrXMgT8K6bylZig2sUQ/AfeSRTT qiXg93OmTpGoGJ/iHf+LEf5BuUToUAtw5wqXPLvfBAarmM0ygo3Z8620+ofuhFOh5RxRq8bCuRo s13Epdj96kA84S72AG8MvXZ4OIganMd2k0orpOQkNR6usToNVS1cqNZ5BktfPEsNFpoPd/krK7D 4+U2mUSp1YKAzIunAvAjdPSgESCrtwDF9AYu0xQClnvpq2o8hgFqpKTEyXHeE7U0mQEv+IVlbjQ NrEaPsSrylT/ekVhrnMoSQwHfK9rPu6+LYPPtpDD7Wv21ogv5KDwEeO8pd/fqyInINY5tj8a0kB m8+LmybDPu0PDu1Ssp6YyQhnQ6N427mzmxecOiBisnep6E/UEuikMXMDW1yC0wYgdaMUy6J8IrF a9iuOCR46KEaScVdJhvpua9TP+hloypaPBCXu5zZnpQ4FSgCfWJT99b4m0MWB+JyZDAdLrkuJVx qC4BQWalrnENDUAtQQAz5Kt5klul7NIgxY0JVdfgBdrY4DSKeXSYTUGTQ5pbJCalRbANM2Td4la nDtugm6MNhiRf+wQnMoEmlEBswSyaXdyU+Y= X-Received: by 2002:a17:907:3da3:b0:c2e:7fe7:288e with SMTP id a640c23a62f3a-c2e7fe736eamr411177566b.32.1791194158114; Mon, 05 Oct 2026 02:55:58 -0700 (PDT) Received: from dev-dsk-fgriffo-1c-93421965.eu-west-1.amazon.com (54-240-197-234.amazon.com. [54.240.197.234]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c31562aba16sm50753266b.23.2026.10.05.02.55.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Oct 2026 02:55:57 -0700 (PDT) From: Fred Griffoul To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Jason Gunthorpe , Kevin Tian Cc: David Woodhouse , Ackerley Tng , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Steffen Eiden , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Joerg Roedel , Robin Murphy , Alex Williamson , Shuah Khan , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH 3/6] dma-buf: Add ranged mapping invalidation Date: Mon, 5 Oct 2026 09:55:49 +0000 Message-ID: <20261005095552.52748-4-griffoul@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261005095552.52748-1-griffoul@gmail.com> References: <20260720111259.122911-1-dwmw2@infradead.org> <20261005095552.52748-1-griffoul@gmail.com> Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Fred Griffoul dma_buf_invalidate_mappings() tells every importer that the whole buffer changed. An exporter that changes one part of its memory cannot say which bytes changed, so importers throw away mappings that are still valid. Add an exporter helper that invalidates a byte range, and an importer callback that receives it. The callback means that the address, the attributes or the backing of the range changed. If part of the range is no longer backed, get_phys() returns -ENOENT for it. Importers must stop using their old answer before the callback returns. Importers that do not implement the callback still receive a whole-buffer invalidation. Signed-off-by: Fred Griffoul --- drivers/dma-buf/dma-buf.c | 30 ++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 66b85d53ed22..e7010163eb2f 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -1389,6 +1389,36 @@ void dma_buf_invalidate_mappings(struct dma_buf *dmabuf) } EXPORT_SYMBOL_NS_GPL(dma_buf_invalidate_mappings, "DMA_BUF"); +/** + * dma_buf_invalidate_mappings_range - notify attachments that a range changed + * @dmabuf: buffer whose layout changed + * @offset: first changed byte + * @length: number of changed bytes + * + * Importers with a ranged callback stop using their old mappings of the range + * before returning. Other importers receive the existing whole-buffer + * callback, which is correct but coarser. The reservation lock must be held. + */ +void dma_buf_invalidate_mappings_range(struct dma_buf *dmabuf, + unsigned long offset, + unsigned long length) +{ + struct dma_buf_attachment *attach; + + dma_resv_assert_held(dmabuf->resv); + list_for_each_entry(attach, &dmabuf->attachments, node) { + const struct dma_buf_attach_ops *ops = attach->importer_ops; + + if (!ops) + continue; + if (ops->invalidate_mappings_range) + ops->invalidate_mappings_range(attach, offset, length); + else if (ops->invalidate_mappings) + ops->invalidate_mappings(attach); + } +} +EXPORT_SYMBOL_NS_GPL(dma_buf_invalidate_mappings_range, "DMA_BUF"); + /** * dma_buf_get_phys - describe the run that starts at an offset * @attach: attachment to query diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index b223962e20c2..55c3fe60a0ba 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -485,6 +485,19 @@ struct dma_buf_attach_ops { * required behavior. */ void (*invalidate_mappings)(struct dma_buf_attachment *attach); + + /** + * @invalidate_mappings_range: [optional] a byte range changed + * + * The exporter changed the address, attributes or backing of + * [@offset, @offset + @length). The importer must stop using its old + * answer for that range before returning. + * Importers without this callback receive @invalidate_mappings for + * the whole buffer instead. + */ + void (*invalidate_mappings_range)(struct dma_buf_attachment *attach, + unsigned long offset, + unsigned long length); }; /** @@ -600,6 +613,9 @@ struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *, void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *, enum dma_data_direction); void dma_buf_invalidate_mappings(struct dma_buf *dma_buf); +void dma_buf_invalidate_mappings_range(struct dma_buf *dma_buf, + unsigned long offset, + unsigned long length); bool dma_buf_attach_revocable(struct dma_buf_attachment *attach); /* bits 0-7: memory type (a value, not flags) */ #define DMA_BUF_PHYS_ATTR_TYPE_MASK GENMASK(7, 0) -- 2.47.3