From: Kienan Stewart Date: Wed, 22 Jan 2025 15:10:53 +0000 (+0000) Subject: Fix: f_count replaced with f_ref in Linux 6.13.0-rc1 X-Git-Tag: v2.13.17~5 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=643560cce60d183221a8afa9688f6a0db61dafd1;p=lttng-modules.git Fix: f_count replaced with f_ref in Linux 6.13.0-rc1 See upstream commit 90ee6ed776c06435a3fe79c7f5344761f52e1760: commit 90ee6ed776c06435a3fe79c7f5344761f52e1760 Author: Christian Brauner Date: Mon Oct 7 16:23:59 2024 +0200 fs: port files to file_ref Port files to rely on file_ref reference to improve scaling and gain overflow protection. - We continue to WARN during get_file() in case a file that is already marked dead is revived as get_file() is only valid if the caller already holds a reference to the file. This hasn't changed just the check changes. - The semantics for epoll and ttm's dmabuf usage have changed. Both epoll and ttm synchronize with __fput() to prevent the underlying file from beeing freed. Change-Id: I9f376af50835f15f74ff7fc82bdb752e09f77222 Signed-off-by: Kienan Stewart Signed-off-by: Mathieu Desnoyers --- diff --git a/include/wrapper/file_ref.h b/include/wrapper/file_ref.h new file mode 100644 index 00000000..fd2b1238 --- /dev/null +++ b/include/wrapper/file_ref.h @@ -0,0 +1,43 @@ +/* + * SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) + * SPDX-FileCopyrightText: 2024 Kienan Stewart + */ + +#ifndef LTTNG_WRAPPER_FILE_REF_H +#define LTTNG_WRAPPER_FILE_REF_H + +#include +#include + +#if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(6,13,0)) +#include + +static inline __must_check +bool lttng_file_ref_get(struct file *file) +{ + return file_ref_get(&file->f_ref); +} + +static inline +bool lttng_file_ref_put(struct file *file) +{ + /* This is __must_check */ + return file_ref_put(&file->f_ref); +} + +#else +static inline __must_check +bool lttng_file_ref_get(struct file *file) +{ + atomic_long_add_unless(&file->f_count, 1, LONG_MAX); + return true; +} + +static inline +bool lttng_file_ref_put(struct file *file) +{ + atomic_long_dec(&file->f_count); + return true; +} +#endif +#endif /* LTTNG_WRAPPER_FILE_REF_H */ diff --git a/src/lttng-abi.c b/src/lttng-abi.c index b66d434f..c708c0d2 100644 --- a/src/lttng-abi.c +++ b/src/lttng-abi.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -543,7 +544,7 @@ int lttng_abi_create_channel(struct file *session_file, transport_name = ""; break; } - if (!atomic_long_add_unless(&session_file->f_count, 1, LONG_MAX)) { + if (!lttng_file_ref_get(session_file)) { ret = -EOVERFLOW; goto refcount_error; } @@ -568,7 +569,7 @@ int lttng_abi_create_channel(struct file *session_file, return chan_fd; chan_error: - atomic_long_dec(&session_file->f_count); + lttng_file_ref_put(session_file); refcount_error: fput(chan_file); file_error: @@ -1740,7 +1741,7 @@ int lttng_abi_open_event_notifier_group_stream(struct file *notif_file) return -ENOENT; /* The event_notifier notification fd holds a reference on the event_notifier group */ - if (!atomic_long_add_unless(¬if_file->f_count, 1, LONG_MAX)) { + if (!lttng_file_ref_get(notif_file)) { ret = -EOVERFLOW; goto refcount_error; } @@ -1755,7 +1756,7 @@ int lttng_abi_open_event_notifier_group_stream(struct file *notif_file) return ret; fd_error: - atomic_long_dec(¬if_file->f_count); + lttng_file_ref_put(notif_file); refcount_error: event_notifier_group->ops->priv->buffer_read_close(buf); return ret; @@ -1881,7 +1882,7 @@ int lttng_abi_create_event(struct file *channel_file, goto file_error; } /* The event holds a reference on the channel */ - if (!atomic_long_add_unless(&channel_file->f_count, 1, LONG_MAX)) { + if (!lttng_file_ref_get(channel_file)) { ret = -EOVERFLOW; goto refcount_error; } @@ -1947,7 +1948,7 @@ int lttng_abi_create_event(struct file *channel_file, return event_fd; event_error: - atomic_long_dec(&channel_file->f_count); + lttng_file_ref_put(channel_file); refcount_error: fput(event_file); file_error: @@ -2109,7 +2110,7 @@ int lttng_abi_create_event_notifier(struct file *event_notifier_group_file, } /* The event notifier holds a reference on the event notifier group. */ - if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) { + if (!lttng_file_ref_get(event_notifier_group_file)) { ret = -EOVERFLOW; goto refcount_error; } @@ -2184,7 +2185,7 @@ int lttng_abi_create_event_notifier(struct file *event_notifier_group_file, return event_notifier_fd; event_notifier_error: - atomic_long_dec(&event_notifier_group_file->f_count); + lttng_file_ref_put(event_notifier_group_file); refcount_error: fput(event_notifier_file); file_error: @@ -2257,7 +2258,7 @@ long lttng_abi_event_notifier_group_create_error_counter( counter_len = error_counter_conf->dimensions[0].size; - if (!atomic_long_add_unless(&event_notifier_group_file->f_count, 1, LONG_MAX)) { + if (!lttng_file_ref_get(event_notifier_group_file)) { ret = -EOVERFLOW; goto refcount_error; } @@ -2289,7 +2290,7 @@ long lttng_abi_event_notifier_group_create_error_counter( return counter_fd; counter_error: - atomic_long_dec(&event_notifier_group_file->f_count); + lttng_file_ref_put(event_notifier_group_file); refcount_error: fput(counter_file); file_error: diff --git a/src/lttng-events.c b/src/lttng-events.c index e3ce3e74..a3ffd1e4 100644 --- a/src/lttng-events.c +++ b/src/lttng-events.c @@ -29,6 +29,7 @@ #include #include +#include #include #include /* for wrapper_vmalloc_sync_mappings() */ #include @@ -1948,7 +1949,7 @@ int lttng_session_list_tracker_ids(struct lttng_kernel_session *session, ret = PTR_ERR(tracker_ids_list_file); goto file_error; } - if (!atomic_long_add_unless(&session->priv->file->f_count, 1, LONG_MAX)) { + if (!lttng_file_ref_get(session->priv->file)) { ret = -EOVERFLOW; goto refcount_error; } @@ -1964,7 +1965,7 @@ int lttng_session_list_tracker_ids(struct lttng_kernel_session *session, return file_fd; open_error: - atomic_long_dec(&session->priv->file->f_count); + lttng_file_ref_put(session->priv->file); refcount_error: fput(tracker_ids_list_file); file_error: