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>
Unified · Split
| @@ -130,45 +130,33 @@ struct bpf_iter_seq_task_file_info { · 129 unmodified lines | |||
| 130 | */ | 130 | */ |
| 131 | struct bpf_iter_seq_task_common common; | 131 | struct bpf_iter_seq_task_common common; |
| 132 | struct task_struct *task; | 132 | struct task_struct *task; |
| 133 | struct files_struct *files; | ||
| 134 | u32 tid; | 133 | u32 tid; |
| 135 | u32 fd; | 134 | u32 fd; |
| 136 | }; | 135 | }; |
| 137 | 136 | ||
| 138 | static struct file * | 137 | static struct file * |
| 139 | task_file_seq_get_next(struct bpf_iter_seq_task_file_info *info, | 138 | 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) |
| 141 | { | 140 | { |
| 142 | struct pid_namespace *ns = info->common.ns; | 141 | struct pid_namespace *ns = info->common.ns; |
| 143 | u32 curr_tid = info->tid, max_fds; | 142 | u32 curr_tid = info->tid; |
| 144 | struct files_struct *curr_files; | ||
| 145 | struct task_struct *curr_task; | 143 | struct task_struct *curr_task; |
| 146 | int curr_fd = info->fd; | 144 | unsigned int curr_fd = info->fd; |
| 147 | 145 | ||
| 148 | /* If this function returns a non-NULL file object, | 146 | /* 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. |
| 150 | * Otherwise, it does not hold any reference. | 148 | * Otherwise, it does not hold any reference. |
| 151 | */ | 149 | */ |
| 152 | again: | 150 | again: |
| 153 | if (*task) { | 151 | if (*task) { |
| 154 | curr_task = *task; | 152 | curr_task = *task; |
| 155 | curr_files = *fstruct; | ||
| 156 | curr_fd = info->fd; | 153 | curr_fd = info->fd; |
| 157 | } else { | 154 | } else { |
| 158 | curr_task = task_seq_get_next(ns, &curr_tid, true); | 155 | curr_task = task_seq_get_next(ns, &curr_tid, true); |
| 159 | if (!curr_task) | 156 | if (!curr_task) |
| 160 | return NULL; | 157 | return NULL; |
| 161 | 158 | ||
| 162 | curr_files = get_files_struct(curr_task); | 159 | /* set *task and info->tid */ |
| 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; | ||
| 172 | *task = curr_task; | 160 | *task = curr_task; |
| 173 | if (curr_tid == info->tid) { | 161 | if (curr_tid == info->tid) { |
| 174 | curr_fd = info->fd; | 162 | curr_fd = info->fd; |
| @@ -179,13 +167,11 @@ again: · 4 unmodified lines | |||
| 179 | } | 167 | } |
| 180 | 168 | ||
| 181 | rcu_read_lock(); | 169 | rcu_read_lock(); |
| 182 | max_fds = files_fdtable(curr_files)->max_fds; | 170 | for (;; curr_fd++) { |
| 183 | for (; curr_fd < max_fds; curr_fd++) { | ||
| 184 | struct file *f; | 171 | struct file *f; |
| 185 | 172 | f = task_lookup_next_fd_rcu(curr_task, &curr_fd); | |
| 186 | f = files_lookup_fd_rcu(curr_files, curr_fd); | ||
| 187 | if (!f) | 173 | if (!f) |
| 188 | continue; | 174 | break; |
| 189 | if (!get_file_rcu(f)) | 175 | if (!get_file_rcu(f)) |
| 190 | continue; | 176 | continue; |
| 191 | 177 | ||
| @@ -197,10 +183,8 @@ again: · 5 unmodified lines | |||
| 197 | 183 | ||
| 198 | /* the current task is done, go to the next task */ | 184 | /* the current task is done, go to the next task */ |
| 199 | rcu_read_unlock(); | 185 | rcu_read_unlock(); |
| 200 | put_files_struct(curr_files); | ||
| 201 | put_task_struct(curr_task); | 186 | put_task_struct(curr_task); |
| 202 | *task = NULL; | 187 | *task = NULL; |
| 203 | *fstruct = NULL; | ||
| 204 | info->fd = 0; | 188 | info->fd = 0; |
| 205 | curr_tid = ++(info->tid); | 189 | curr_tid = ++(info->tid); |
| 206 | goto again; | 190 | goto again; |
| @@ -209,13 +193,11 @@ again: · 2 unmodified lines | |||
| 209 | static void *task_file_seq_start(struct seq_file *seq, loff_t *pos) | 193 | static void *task_file_seq_start(struct seq_file *seq, loff_t *pos) |
| 210 | { | 194 | { |
| 211 | struct bpf_iter_seq_task_file_info *info = seq->private; | 195 | struct bpf_iter_seq_task_file_info *info = seq->private; |
| 212 | struct files_struct *files = NULL; | ||
| 213 | struct task_struct *task = NULL; | 196 | struct task_struct *task = NULL; |
| 214 | struct file *file; | 197 | struct file *file; |
| 215 | 198 | ||
| 216 | file = task_file_seq_get_next(info, &task, &files); | 199 | file = task_file_seq_get_next(info, &task); |
| 217 | if (!file) { | 200 | if (!file) { |
| 218 | info->files = NULL; | ||
| 219 | info->task = NULL; | 201 | info->task = NULL; |
| 220 | return NULL; | 202 | return NULL; |
| 221 | } | 203 | } |
| @@ -223,7 +205,6 @@ static void *task_file_seq_start(struct seq_file *seq, loff_t *pos) · 1 unmodified line | |||
| 223 | if (*pos == 0) | 205 | if (*pos == 0) |
| 224 | ++*pos; | 206 | ++*pos; |
| 225 | info->task = task; | 207 | info->task = task; |
| 226 | info->files = files; | ||
| 227 | 208 | ||
| 228 | return file; | 209 | return file; |
| 229 | } | 210 | } |
| @@ -231,22 +212,19 @@ static void *task_file_seq_start(struct seq_file *seq, loff_t *pos) · 1 unmodified line | |||
| 231 | static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos) | 212 | static void *task_file_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 232 | { | 213 | { |
| 233 | struct bpf_iter_seq_task_file_info *info = seq->private; | 214 | struct bpf_iter_seq_task_file_info *info = seq->private; |
| 234 | struct files_struct *files = info->files; | ||
| 235 | struct task_struct *task = info->task; | 215 | struct task_struct *task = info->task; |
| 236 | struct file *file; | 216 | struct file *file; |
| 237 | 217 | ||
| 238 | ++*pos; | 218 | ++*pos; |
| 239 | ++info->fd; | 219 | ++info->fd; |
| 240 | fput((struct file *)v); | 220 | fput((struct file *)v); |
| 241 | file = task_file_seq_get_next(info, &task, &files); | 221 | file = task_file_seq_get_next(info, &task); |
| 242 | if (!file) { | 222 | if (!file) { |
| 243 | info->files = NULL; | ||
| 244 | info->task = NULL; | 223 | info->task = NULL; |
| 245 | return NULL; | 224 | return NULL; |
| 246 | } | 225 | } |
| 247 | 226 | ||
| 248 | info->task = task; | 227 | info->task = task; |
| 249 | info->files = files; | ||
| 250 | 228 | ||
| 251 | return file; | 229 | return file; |
| 252 | } | 230 | } |
| @@ -295,9 +273,7 @@ static void task_file_seq_stop(struct seq_file *seq, void *v) · 42 unmodified lines | |||
| 295 | (void)__task_file_seq_show(seq, v, true); | 273 | (void)__task_file_seq_show(seq, v, true); |
| 296 | } else { | 274 | } else { |
| 297 | fput((struct file *)v); | 275 | fput((struct file *)v); |
| 298 | put_files_struct(info->files); | ||
| 299 | put_task_struct(info->task); | 276 | put_task_struct(info->task); |
| 300 | info->files = NULL; | ||
| 301 | info->task = NULL; | 277 | info->task = NULL; |
| 302 | } | 278 | } |
| 303 | } | 279 | } |