History

bpf/task_iter: In task_file_seq_get_next use task_lookup_next_fd_rcu

When discussing[1] exec and posix file locks it was realized that none of the callers of get_files_struct fundamentally needed to call get_files_struct, and that by switching them to helper functions instead it will both simplify their code and remove unnecessary increments of files_struct.count. Those unnecessary increments can result in exec unnecessarily unsharing files_struct which breaking posix locks, and it can result in fget_light having to fallback to fget reducing system performance. Using task_lookup_next_fd_rcu simplifies task_file_seq_get_next, by moving the checking for the maximum file descritor into the generic code, and by remvoing the need for capturing and releasing a reference on files_struct. As the reference count of files_struct no longer needs to be maintained bpf_iter_seq_task_file_info can have it's files member removed and task_file_seq_get_next no longer needs it's fstruct argument. The curr_fd local variable does need to become unsigned to be used with fnext_task. As curr_fd is assigned from and assigned a u32 making curr_fd an unsigned int won't cause problems and might prevent them. [1] https://lkml.kernel.org/r/20180915160423.GA31461@redhat.com Suggested-by: Oleg Nesterov <oleg@redhat.com> v1: https://lkml.kernel.org/r/20200817220425.9389-11-ebiederm@xmission.com Link: https://lkml.kernel.org/r/20201120231441.29911-16-ebiederm@xmission.com Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>

Eric W. Biederman committed 5 years ago

Commit 66ed5941 parent 5b17b611 file · +10 −34Browse files

Unified · Split

kernel/bpf/task_iter.c+10−34
@@ -130,45 +130,33 @@ struct bpf_iter_seq_task_file_info { · 129 unmodified lines
130130 */
131131 struct bpf_iter_seq_task_common common;
132132 struct task_struct *task;
133− struct files_struct *files;
134133 u32 tid;
135134 u32 fd;
136135 };
137136
138137 static struct file *
139138 task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info,
140− struct task_struct **task, struct files_struct **fstruct)
139+ struct task_struct **task)
141140 {
142141 struct pid_namespace *ns = info->common.ns;
143− u32 curr_tid = info->tid, max_fds;
144− struct files_struct *curr_files;
142+ u32 curr_tid = info->tid;
145143 struct task_struct *curr_task;
146− int curr_fd = info->fd;
144+ unsigned int curr_fd = info->fd;
147145
148146 /* If this function returns a non-NULL file object,
149− * it held a reference to the task/files_struct/file.
147+ * it held a reference to the task/file.
150148 * Otherwise, it does not hold any reference.
151149 */
152150 again:
153151 if (*task) {
154152 curr_task = *task;
155− curr_files = *fstruct;
156153 curr_fd = info->fd;
157154 } else {
158155 curr_task = task_seq_get_next(ns, &curr_tid, true);
159156 if (!curr_task)
160157 return NULL;
161158
162− curr_files = get_files_struct(curr_task);
163− if (!curr_files) {
164− put_task_struct(curr_task);
165− curr_tid = ++(info->tid);
166− info->fd = 0;
167− goto again;
168− }
169−
170− /* set *fstruct, *task and info->tid */
171− *fstruct = curr_files;
159+ /* set *task and info->tid */
172160 *task = curr_task;
173161 if (curr_tid == info->tid) {
174162 curr_fd = info->fd;
@@ -179,13 +167,11 @@ again: · 4 unmodified lines
179167 }
180168
181169 rcu_read_lock();
182− max_fds = files_fdtable(curr_files)->max_fds;
183− for (; curr_fd < max_fds; curr_fd++) {
170+ for (;; curr_fd++) {
184171 struct file *f;
185−
186− f = files_lookup_fd_rcu(curr_files, curr_fd);
172+ f = task_lookup_next_fd_rcu(curr_task, &curr_fd);
187173 if (!f)
188− continue;
174+ break;
189175 if (!get_file_rcu(f))
190176 continue;
191177
@@ -197,10 +183,8 @@ again: · 5 unmodified lines
197183
198184 /* the current task is done, go to the next task */
199185 rcu_read_unlock();
200− put_files_struct(curr_files);
201186 put_task_struct(curr_task);
202187 *task = NULL;
203− *fstruct = NULL;
204188 info->fd = 0;
205189 curr_tid = ++(info->tid);
206190 goto again;
@@ -209,13 +193,11 @@ again: · 2 unmodified lines
209193 static void *task_file_seq_start(struct seq_file *seq, loff_t *pos)
210194 {
211195 struct bpf_iter_seq_task_file_info *info = seq->private;
212− struct files_struct *files = NULL;
213196 struct task_struct *task = NULL;
214197 struct file *file;
215198
216− file = task_file_seq_get_next(info, &task, &files);
199+ file = task_file_seq_get_next(info, &task);
217200 if (!file) {
218− info->files = NULL;
219201 info->task = NULL;
220202 return NULL;
221203 }
@@ -223,7 +205,6 @@ static void *task_file_seq_start(struct seq_file *seq, loff_t *pos) · 1 unmodified line
223205 if (*pos == 0)
224206 ++*pos;
225207 info->task = task;
226− info->files = files;
227208
228209 return file;
229210 }
@@ -231,22 +212,19 @@ static void *task_file_seq_start(struct seq_file *seq, loff_t *pos) · 1 unmodified line
231212 static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos)
232213 {
233214 struct bpf_iter_seq_task_file_info *info = seq->private;
234− struct files_struct *files = info->files;
235215 struct task_struct *task = info->task;
236216 struct file *file;
237217
238218 ++*pos;
239219 ++info->fd;
240220 fput((struct file *)v);
241− file = task_file_seq_get_next(info, &task, &files);
221+ file = task_file_seq_get_next(info, &task);
242222 if (!file) {
243− info->files = NULL;
244223 info->task = NULL;
245224 return NULL;
246225 }
247226
248227 info->task = task;
249− info->files = files;
250228
251229 return file;
252230 }
@@ -295,9 +273,7 @@ static void task_file_seq_stop(struct seq_file *seq, void *v) · 42 unmodified lines
295273 (void)__task_file_seq_show(seq, v, true);
296274 } else {
297275 fput((struct file *)v);
298− put_files_struct(info->files);
299276 put_task_struct(info->task);
300− info->files = NULL;
301277 info->task = NULL;
302278 }
303279 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t