ANDROID: dma-buf: Hoist files_fdtable()->max_fds The READ_ONCE inside files_fdtable depends on volatile to prevent caching the result in a register, resulting in 2 loads every loop iteration. Where max_fds is used (entirely from within locked sections) max_fds cannot change, so avoid the overhead by reading once into a local variable. Bug: 424648392 Change-Id: If53534445752a50c3b4dffee186606dc16b0614f Suggested-by: Suren Baghdasaryan <surenb@google.com> Signed-off-by: T.J. Mercier <tjmercier@google.com>
diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index c3ae181..bcb645e 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c
@@ -664,7 +664,8 @@ void put_dmabuf_info(struct task_dma_buf_info *dmabuf_info) #define COUNT_DMABUF_FDS(file_lookup_func) ({ \ size_t count = 0; \ - for (unsigned int n = 0; n < files_fdtable(current->files)->max_fds; ++n) { \ + unsigned int max_fds = files_fdtable(current->files)->max_fds; \ + for (unsigned int n = 0; n < max_fds; ++n) { \ struct file *file = file_lookup_func(current->files, n); \ if (file && is_dma_buf_file(file)) \ ++count; \ @@ -697,6 +698,7 @@ int dma_buf_begin_new_exec(struct files_struct *old_files) if (my_files) { size_t num_dmabuf_fds, num_dmabuf_fds_check; unsigned int retries = 0; + unsigned int max_fds; /* Attempt to count dmabuf FDs locklessly before allocating */ rcu_read_lock(); @@ -725,7 +727,8 @@ int dma_buf_begin_new_exec(struct files_struct *old_files) goto retry; } - for (unsigned int n = 0; n < files_fdtable(my_files)->max_fds; n++) { + max_fds = files_fdtable(my_files)->max_fds; + for (unsigned int n = 0; n < max_fds; n++) { struct file *file = files_lookup_fd_locked(my_files, n); int err;