From patchwork Wed May 7 00:10:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "T.J. Mercier" X-Patchwork-Id: 889289 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 0B5431DFF0 for ; Wed, 7 May 2025 00:10:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746576649; cv=none; b=JBZUn1AJdgb4AAKDlADkGcX0rm8E6i1ZvCbupl225WHITQYHkuL719AwTry54DSVaHdhMqSFFt4jsyffRS3zgG9x9TbQ9SppUiXbvmu5mBpuexOHpvimKt5q0IP27Tqgct9S3aW9PDL68MGfy7cCzhzV9lDlRKRE8OdfHGexoqU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746576649; c=relaxed/simple; bh=GtprxiIp9QPv7QUqV5NZztK4owcRpS325n/d3dKhKjY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GbDfNznbxGiOTJAjcTn0sNM6tnCEO5GuGqDBhSIhY+EcaWgQSOX2N7I3ldhKUfdAeWi3Kzasktp12HeVvlxif/6cXJDVUIu1IrItsii0Ma2c73X6eN3htsfa56TCridGet7aOnSkuixXBOsDxtjvtVPkwPtdy6j0SHzmSq3fmQs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iFe53FlM; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tjmercier.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iFe53FlM" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-309f0d465bdso9373244a91.3 for ; Tue, 06 May 2025 17:10:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746576647; x=1747181447; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=J2Exutn/ZWXBPWobLv3f+bgAg3/+JyztWu0aq/wV+8U=; b=iFe53FlMKniurYy9wOmqsNlyeDKz2Um1/Ulxy/p/dXqSmLb1kDenB+4z0H4FP8KEv0 r+meGJJBQnd71umHwS4iS8hRDw2oEEH6VaxVsDGbPqb0CHyRDv+TlAtmc2vB4eewSn8R hXpqVK/gn4bxmWSQv6+lZNuVJpY1w7u61DFFnY+WEiL/Xiq264wqWHan8c/6hQ5zxG0Y B6lViwg2Hua+DTRo6Mlg0R3JPZxsQX9t13ip263i5QSmHuA5HC2k6y9vd2ZneOQLWTqS xSbLQP37wUAH4/nzWUIMBpp6Ahoidw9QC28TnIAeRm1PTCoxcFjuAbT3meHemAedVn3t TL1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746576647; x=1747181447; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=J2Exutn/ZWXBPWobLv3f+bgAg3/+JyztWu0aq/wV+8U=; b=alh8sOD7RzZapoYgREwAv5yajI2nAcE+iCf9Kx+vsZClY8dnoFKq0pbPz9BJN3/VaG FCPnBOgtciRy1L9q6ZhelH1gcYvHU4pH+IBTNW7/a96F4Dm/ytZOs71x6d6seaoepSYe j6rLiI7EYrmwPWChFhRjfARm+KPiwEVfM9JzHFgYrMXriGulKEZlFGFqXJsNx1fTuDi2 ixEGQyCBS7t2yWtIwK+tDi0t/YQIGDUsqQRuNomXODKNgVzCLCiznxzniG5DfataUsiW m29q9E46EO1/OjQ//OL7hUw1iYF66Qa1N84ofM7OaaZWke7RlBnIguibdQXdOzUX8Fgj Zl2g== X-Forwarded-Encrypted: i=1; AJvYcCVjJidVCyaA00Li6jzVkKd3u23LzFUMDrIojNJeQrUF5XEvUu7lJUeGX8/5z9/r7WAcPykISd9KgaEwdA==@vger.kernel.org X-Gm-Message-State: AOJu0YyBsxNH6aCnXmCujORbpsM8BQ7NWKqw58yyXOtpVGsKClMlaaH1 Vmwgyb5ovYLlAhLNmGjXQNrGr7P6kjqsPDZwmakuyu+JKYBvE8wbWZF/YCgu1bUbLQLBZSNkRw8 6JBB1ws9P6Wc+Mg== X-Google-Smtp-Source: AGHT+IFL0AeKYJWX5uQDx4vtlik1jrjuYKA2AIFKy1hdyB/rg+TpkzIfgKkb1XNyAaj5vjstqCOldZHytqVX2N0= X-Received: from pjf8.prod.google.com ([2002:a17:90b:3f08:b0:30a:8830:9f6b]) (user=tjmercier job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2249:b0:2ee:c291:765a with SMTP id 98e67ed59e1d1-30aac179571mr2222024a91.8.1746576647544; Tue, 06 May 2025 17:10:47 -0700 (PDT) Date: Wed, 7 May 2025 00:10:33 +0000 In-Reply-To: <20250507001036.2278781-1-tjmercier@google.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250507001036.2278781-1-tjmercier@google.com> X-Mailer: git-send-email 2.49.0.1045.g170613ef41-goog Message-ID: <20250507001036.2278781-3-tjmercier@google.com> Subject: [PATCH bpf-next v3 2/5] bpf: Add dmabuf iterator From: "T.J. Mercier" To: sumit.semwal@linaro.org, christian.koenig@amd.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, skhan@linuxfoundation.org, alexei.starovoitov@gmail.com, song@kernel.org Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, android-mm@google.com, simona@ffwll.ch, eddyz87@gmail.com, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, jolsa@kernel.org, mykolal@fb.com, "T.J. Mercier" The dmabuf iterator traverses the list of all DMA buffers. DMA buffers are refcounted through their associated struct file. A reference is taken on each buffer as the list is iterated to ensure each buffer persists for the duration of the bpf program execution without holding the list mutex. Signed-off-by: T.J. Mercier --- drivers/dma-buf/dma-buf.c | 64 ++++++++++++++++++++++++ include/linux/dma-buf.h | 3 ++ kernel/bpf/Makefile | 3 ++ kernel/bpf/dmabuf_iter.c | 102 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 kernel/bpf/dmabuf_iter.c diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 8d151784e302..9fee2788924e 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -19,7 +19,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -55,6 +57,68 @@ static void __dma_buf_list_del(struct dma_buf *dmabuf) mutex_unlock(&dmabuf_list_mutex); } +/** + * get_first_dmabuf - begin iteration through global list of DMA-bufs + * + * Returns the first buffer in the global list of DMA-bufs that's not in the + * process of being destroyed. Increments that buffer's reference count to + * prevent buffer destruction. Callers must release the reference, either by + * continuing iteration with get_next_dmabuf(), or with dma_buf_put(). + * + * Returns NULL If no active buffers are present. + */ +struct dma_buf *get_first_dmabuf(void) +{ + struct dma_buf *ret = NULL, *dmabuf; + + /* + * The list mutex does not protect a dmabuf's refcount, so it can be + * zeroed while we are iterating. We cannot call get_dma_buf() since the + * caller may not already own a reference to the buffer. + */ + mutex_lock(&dmabuf_list_mutex); + list_for_each_entry(dmabuf, &dmabuf_list, list_node) { + if (file_ref_get(&dmabuf->file->f_ref)) { + ret = dmabuf; + break; + } + } + mutex_unlock(&dmabuf_list_mutex); + return ret; +} + +/** + * get_next_dmabuf - continue iteration through global list of DMA-bufs + * @dmabuf: [in] pointer to dma_buf + * + * Decrements the reference count on the provided buffer. Returns the next + * buffer from the remainder of the global list of DMA-bufs with its reference + * count incremented. Callers must release the reference, either by continuing + * iteration with get_next_dmabuf(), or with dma_buf_put(). + * + * Returns NULL If no additional active buffers are present. + */ +struct dma_buf *get_next_dmabuf(struct dma_buf *dmabuf) +{ + struct dma_buf *ret = NULL; + + /* + * The list mutex does not protect a dmabuf's refcount, so it can be + * zeroed while we are iterating. We cannot call get_dma_buf() since the + * caller may not already own a reference to the buffer. + */ + mutex_lock(&dmabuf_list_mutex); + dma_buf_put(dmabuf); + list_for_each_entry_continue(dmabuf, &dmabuf_list, list_node) { + if (file_ref_get(&dmabuf->file->f_ref)) { + ret = dmabuf; + break; + } + } + mutex_unlock(&dmabuf_list_mutex); + return ret; +} + static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) { struct dma_buf *dmabuf; diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 8ff4add71f88..1820f6db6e58 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -568,6 +568,9 @@ static inline void get_dma_buf(struct dma_buf *dmabuf) get_file(dmabuf->file); } +struct dma_buf *get_first_dmabuf(void); +struct dma_buf *get_next_dmabuf(struct dma_buf *dmbuf); + /** * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings. * @dmabuf: the DMA-buf to check diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 70502f038b92..3a335c50e6e3 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -53,6 +53,9 @@ obj-$(CONFIG_BPF_SYSCALL) += relo_core.o obj-$(CONFIG_BPF_SYSCALL) += btf_iter.o obj-$(CONFIG_BPF_SYSCALL) += btf_relocate.o obj-$(CONFIG_BPF_SYSCALL) += kmem_cache_iter.o +ifeq ($(CONFIG_DMA_SHARED_BUFFER),y) +obj-$(CONFIG_BPF_SYSCALL) += dmabuf_iter.o +endif CFLAGS_REMOVE_percpu_freelist.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_bpf_lru_list.o = $(CC_FLAGS_FTRACE) diff --git a/kernel/bpf/dmabuf_iter.c b/kernel/bpf/dmabuf_iter.c new file mode 100644 index 000000000000..80bca8239c6d --- /dev/null +++ b/kernel/bpf/dmabuf_iter.c @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2025 Google LLC */ +#include +#include +#include +#include +#include + +BTF_ID_LIST_SINGLE(bpf_dmabuf_btf_id, struct, dma_buf) +DEFINE_BPF_ITER_FUNC(dmabuf, struct bpf_iter_meta *meta, struct dma_buf *dmabuf) + +static void *dmabuf_iter_seq_start(struct seq_file *seq, loff_t *pos) +{ + if (*pos) + return NULL; + + return get_first_dmabuf(); +} + +static void *dmabuf_iter_seq_next(struct seq_file *seq, void *v, loff_t *pos) +{ + struct dma_buf *dmabuf = v; + + ++*pos; + + return get_next_dmabuf(dmabuf); +} + +struct bpf_iter__dmabuf { + __bpf_md_ptr(struct bpf_iter_meta *, meta); + __bpf_md_ptr(struct dma_buf *, dmabuf); +}; + +static int __dmabuf_seq_show(struct seq_file *seq, void *v, bool in_stop) +{ + struct bpf_iter_meta meta = { + .seq = seq, + }; + struct bpf_iter__dmabuf ctx = { + .meta = &meta, + .dmabuf = v, + }; + struct bpf_prog *prog = bpf_iter_get_info(&meta, in_stop); + + if (prog) + return bpf_iter_run_prog(prog, &ctx); + + return 0; +} + +static int dmabuf_iter_seq_show(struct seq_file *seq, void *v) +{ + return __dmabuf_seq_show(seq, v, false); +} + +static void dmabuf_iter_seq_stop(struct seq_file *seq, void *v) +{ + struct dma_buf *dmabuf = v; + + if (dmabuf) + dma_buf_put(dmabuf); +} + +static const struct seq_operations dmabuf_iter_seq_ops = { + .start = dmabuf_iter_seq_start, + .next = dmabuf_iter_seq_next, + .stop = dmabuf_iter_seq_stop, + .show = dmabuf_iter_seq_show, +}; + +static void bpf_iter_dmabuf_show_fdinfo(const struct bpf_iter_aux_info *aux, + struct seq_file *seq) +{ + seq_puts(seq, "dmabuf iter\n"); +} + +static const struct bpf_iter_seq_info dmabuf_iter_seq_info = { + .seq_ops = &dmabuf_iter_seq_ops, + .init_seq_private = NULL, + .fini_seq_private = NULL, + .seq_priv_size = 0, +}; + +static struct bpf_iter_reg bpf_dmabuf_reg_info = { + .target = "dmabuf", + .feature = BPF_ITER_RESCHED, + .show_fdinfo = bpf_iter_dmabuf_show_fdinfo, + .ctx_arg_info_size = 1, + .ctx_arg_info = { + { offsetof(struct bpf_iter__dmabuf, dmabuf), + PTR_TO_BTF_ID_OR_NULL }, + }, + .seq_info = &dmabuf_iter_seq_info, +}; + +static int __init dmabuf_iter_init(void) +{ + bpf_dmabuf_reg_info.ctx_arg_info[0].btf_id = bpf_dmabuf_btf_id[0]; + return bpf_iter_reg_target(&bpf_dmabuf_reg_info); +} + +late_initcall(dmabuf_iter_init);