Skip to content

Commit e72ceb8

Browse files
ebiedermgregkh
authored andcommitted
sysfs: Remove sysfs_get/put_active_two
It turns out that holding an active reference on a directory is pointless. The purpose of the active references are to allows us to block when removing sysfs entries that have custom methods so we don't remove modules while running modular code and to keep those custom methods from accessing data structures after the files have been removed. Further sysfs_remove_dir remove all elements in the directory before removing the directory itself, so there is no chance we will remove a directory with active children. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Cc: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 3c31f07 commit e72ceb8

4 files changed

Lines changed: 38 additions & 77 deletions

File tree

‎fs/sysfs/bin.c‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
5454
int rc;
5555

5656
/* need attr_sd for attr, its parent for kobj */
57-
if (!sysfs_get_active_two(attr_sd))
57+
if (!sysfs_get_active(attr_sd))
5858
return -ENODEV;
5959

6060
rc = -EIO;
6161
if (attr->read)
6262
rc = attr->read(kobj, attr, buffer, off, count);
6363

64-
sysfs_put_active_two(attr_sd);
64+
sysfs_put_active(attr_sd);
6565

6666
return rc;
6767
}
@@ -125,14 +125,14 @@ flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
125125
int rc;
126126

127127
/* need attr_sd for attr, its parent for kobj */
128-
if (!sysfs_get_active_two(attr_sd))
128+
if (!sysfs_get_active(attr_sd))
129129
return -ENODEV;
130130

131131
rc = -EIO;
132132
if (attr->write)
133133
rc = attr->write(kobj, attr, buffer, offset, count);
134134

135-
sysfs_put_active_two(attr_sd);
135+
sysfs_put_active(attr_sd);
136136

137137
return rc;
138138
}
@@ -184,12 +184,12 @@ static void bin_vma_open(struct vm_area_struct *vma)
184184
if (!bb->vm_ops || !bb->vm_ops->open)
185185
return;
186186

187-
if (!sysfs_get_active_two(attr_sd))
187+
if (!sysfs_get_active(attr_sd))
188188
return;
189189

190190
bb->vm_ops->open(vma);
191191

192-
sysfs_put_active_two(attr_sd);
192+
sysfs_put_active(attr_sd);
193193
}
194194

195195
static void bin_vma_close(struct vm_area_struct *vma)
@@ -201,12 +201,12 @@ static void bin_vma_close(struct vm_area_struct *vma)
201201
if (!bb->vm_ops || !bb->vm_ops->close)
202202
return;
203203

204-
if (!sysfs_get_active_two(attr_sd))
204+
if (!sysfs_get_active(attr_sd))
205205
return;
206206

207207
bb->vm_ops->close(vma);
208208

209-
sysfs_put_active_two(attr_sd);
209+
sysfs_put_active(attr_sd);
210210
}
211211

212212
static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
@@ -219,12 +219,12 @@ static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
219219
if (!bb->vm_ops || !bb->vm_ops->fault)
220220
return VM_FAULT_SIGBUS;
221221

222-
if (!sysfs_get_active_two(attr_sd))
222+
if (!sysfs_get_active(attr_sd))
223223
return VM_FAULT_SIGBUS;
224224

225225
ret = bb->vm_ops->fault(vma, vmf);
226226

227-
sysfs_put_active_two(attr_sd);
227+
sysfs_put_active(attr_sd);
228228
return ret;
229229
}
230230

@@ -241,12 +241,12 @@ static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
241241
if (!bb->vm_ops->page_mkwrite)
242242
return 0;
243243

244-
if (!sysfs_get_active_two(attr_sd))
244+
if (!sysfs_get_active(attr_sd))
245245
return VM_FAULT_SIGBUS;
246246

247247
ret = bb->vm_ops->page_mkwrite(vma, vmf);
248248

249-
sysfs_put_active_two(attr_sd);
249+
sysfs_put_active(attr_sd);
250250
return ret;
251251
}
252252

@@ -261,12 +261,12 @@ static int bin_access(struct vm_area_struct *vma, unsigned long addr,
261261
if (!bb->vm_ops || !bb->vm_ops->access)
262262
return -EINVAL;
263263

264-
if (!sysfs_get_active_two(attr_sd))
264+
if (!sysfs_get_active(attr_sd))
265265
return -EINVAL;
266266

267267
ret = bb->vm_ops->access(vma, addr, buf, len, write);
268268

269-
sysfs_put_active_two(attr_sd);
269+
sysfs_put_active(attr_sd);
270270
return ret;
271271
}
272272

@@ -281,12 +281,12 @@ static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
281281
if (!bb->vm_ops || !bb->vm_ops->set_policy)
282282
return 0;
283283

284-
if (!sysfs_get_active_two(attr_sd))
284+
if (!sysfs_get_active(attr_sd))
285285
return -EINVAL;
286286

287287
ret = bb->vm_ops->set_policy(vma, new);
288288

289-
sysfs_put_active_two(attr_sd);
289+
sysfs_put_active(attr_sd);
290290
return ret;
291291
}
292292

@@ -301,12 +301,12 @@ static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
301301
if (!bb->vm_ops || !bb->vm_ops->get_policy)
302302
return vma->vm_policy;
303303

304-
if (!sysfs_get_active_two(attr_sd))
304+
if (!sysfs_get_active(attr_sd))
305305
return vma->vm_policy;
306306

307307
pol = bb->vm_ops->get_policy(vma, addr);
308308

309-
sysfs_put_active_two(attr_sd);
309+
sysfs_put_active(attr_sd);
310310
return pol;
311311
}
312312

@@ -321,12 +321,12 @@ static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
321321
if (!bb->vm_ops || !bb->vm_ops->migrate)
322322
return 0;
323323

324-
if (!sysfs_get_active_two(attr_sd))
324+
if (!sysfs_get_active(attr_sd))
325325
return 0;
326326

327327
ret = bb->vm_ops->migrate(vma, from, to, flags);
328328

329-
sysfs_put_active_two(attr_sd);
329+
sysfs_put_active(attr_sd);
330330
return ret;
331331
}
332332
#endif
@@ -356,7 +356,7 @@ static int mmap(struct file *file, struct vm_area_struct *vma)
356356

357357
/* need attr_sd for attr, its parent for kobj */
358358
rc = -ENODEV;
359-
if (!sysfs_get_active_two(attr_sd))
359+
if (!sysfs_get_active(attr_sd))
360360
goto out_unlock;
361361

362362
rc = -EINVAL;
@@ -384,7 +384,7 @@ static int mmap(struct file *file, struct vm_area_struct *vma)
384384
bb->vm_ops = vma->vm_ops;
385385
vma->vm_ops = &bin_vm_ops;
386386
out_put:
387-
sysfs_put_active_two(attr_sd);
387+
sysfs_put_active(attr_sd);
388388
out_unlock:
389389
mutex_unlock(&bb->mutex);
390390

@@ -399,7 +399,7 @@ static int open(struct inode * inode, struct file * file)
399399
int error;
400400

401401
/* binary file operations requires both @sd and its parent */
402-
if (!sysfs_get_active_two(attr_sd))
402+
if (!sysfs_get_active(attr_sd))
403403
return -ENODEV;
404404

405405
error = -EACCES;
@@ -426,11 +426,11 @@ static int open(struct inode * inode, struct file * file)
426426
mutex_unlock(&sysfs_bin_lock);
427427

428428
/* open succeeded, put active references */
429-
sysfs_put_active_two(attr_sd);
429+
sysfs_put_active(attr_sd);
430430
return 0;
431431

432432
err_out:
433-
sysfs_put_active_two(attr_sd);
433+
sysfs_put_active(attr_sd);
434434
kfree(bb);
435435
return error;
436436
}

‎fs/sysfs/dir.c‎

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
9393
* RETURNS:
9494
* Pointer to @sd on success, NULL on failure.
9595
*/
96-
static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
96+
struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
9797
{
9898
if (unlikely(!sd))
9999
return NULL;
@@ -124,7 +124,7 @@ static struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
124124
* Put an active reference to @sd. This function is noop if @sd
125125
* is NULL.
126126
*/
127-
static void sysfs_put_active(struct sysfs_dirent *sd)
127+
void sysfs_put_active(struct sysfs_dirent *sd)
128128
{
129129
struct completion *cmpl;
130130
int v;
@@ -144,45 +144,6 @@ static void sysfs_put_active(struct sysfs_dirent *sd)
144144
complete(cmpl);
145145
}
146146

147-
/**
148-
* sysfs_get_active_two - get active references to sysfs_dirent and parent
149-
* @sd: sysfs_dirent of interest
150-
*
151-
* Get active reference to @sd and its parent. Parent's active
152-
* reference is grabbed first. This function is noop if @sd is
153-
* NULL.
154-
*
155-
* RETURNS:
156-
* Pointer to @sd on success, NULL on failure.
157-
*/
158-
struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
159-
{
160-
if (sd) {
161-
if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
162-
return NULL;
163-
if (unlikely(!sysfs_get_active(sd))) {
164-
sysfs_put_active(sd->s_parent);
165-
return NULL;
166-
}
167-
}
168-
return sd;
169-
}
170-
171-
/**
172-
* sysfs_put_active_two - put active references to sysfs_dirent and parent
173-
* @sd: sysfs_dirent of interest
174-
*
175-
* Put active references to @sd and its parent. This function is
176-
* noop if @sd is NULL.
177-
*/
178-
void sysfs_put_active_two(struct sysfs_dirent *sd)
179-
{
180-
if (sd) {
181-
sysfs_put_active(sd);
182-
sysfs_put_active(sd->s_parent);
183-
}
184-
}
185-
186147
/**
187148
* sysfs_deactivate - deactivate sysfs_dirent
188149
* @sd: sysfs_dirent to deactivate

‎fs/sysfs/file.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer
8585
return -ENOMEM;
8686

8787
/* need attr_sd for attr and ops, its parent for kobj */
88-
if (!sysfs_get_active_two(attr_sd))
88+
if (!sysfs_get_active(attr_sd))
8989
return -ENODEV;
9090

9191
buffer->event = atomic_read(&attr_sd->s_attr.open->event);
9292
count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
9393

94-
sysfs_put_active_two(attr_sd);
94+
sysfs_put_active(attr_sd);
9595

9696
/*
9797
* The code works fine with PAGE_SIZE return but it's likely to
@@ -203,12 +203,12 @@ flush_write_buffer(struct dentry * dentry, struct sysfs_buffer * buffer, size_t
203203
int rc;
204204

205205
/* need attr_sd for attr and ops, its parent for kobj */
206-
if (!sysfs_get_active_two(attr_sd))
206+
if (!sysfs_get_active(attr_sd))
207207
return -ENODEV;
208208

209209
rc = ops->store(kobj, attr_sd->s_attr.attr, buffer->page, count);
210210

211-
sysfs_put_active_two(attr_sd);
211+
sysfs_put_active(attr_sd);
212212

213213
return rc;
214214
}
@@ -344,7 +344,7 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
344344
memmove(last_sysfs_file, p, strlen(p) + 1);
345345

346346
/* need attr_sd for attr and ops, its parent for kobj */
347-
if (!sysfs_get_active_two(attr_sd))
347+
if (!sysfs_get_active(attr_sd))
348348
return -ENODEV;
349349

350350
/* every kobject with an attribute needs a ktype assigned */
@@ -393,13 +393,13 @@ static int sysfs_open_file(struct inode *inode, struct file *file)
393393
goto err_free;
394394

395395
/* open succeeded, put active references */
396-
sysfs_put_active_two(attr_sd);
396+
sysfs_put_active(attr_sd);
397397
return 0;
398398

399399
err_free:
400400
kfree(buffer);
401401
err_out:
402-
sysfs_put_active_two(attr_sd);
402+
sysfs_put_active(attr_sd);
403403
return error;
404404
}
405405

@@ -437,12 +437,12 @@ static unsigned int sysfs_poll(struct file *filp, poll_table *wait)
437437
struct sysfs_open_dirent *od = attr_sd->s_attr.open;
438438

439439
/* need parent for the kobj, grab both */
440-
if (!sysfs_get_active_two(attr_sd))
440+
if (!sysfs_get_active(attr_sd))
441441
goto trigger;
442442

443443
poll_wait(filp, &od->poll, wait);
444444

445-
sysfs_put_active_two(attr_sd);
445+
sysfs_put_active(attr_sd);
446446

447447
if (buffer->event != atomic_read(&od->event))
448448
goto trigger;

‎fs/sysfs/sysfs.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ extern const struct file_operations sysfs_dir_operations;
124124
extern const struct inode_operations sysfs_dir_inode_operations;
125125

126126
struct dentry *sysfs_get_dentry(struct sysfs_dirent *sd);
127-
struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd);
128-
void sysfs_put_active_two(struct sysfs_dirent *sd);
127+
struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd);
128+
void sysfs_put_active(struct sysfs_dirent *sd);
129129
void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
130130
struct sysfs_dirent *parent_sd);
131131
int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd);

0 commit comments

Comments
 (0)