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