Skip to contentSkip to conversation

Sysfs cleanups

PATCH In review Eric W. Biederman 8 participants 200 messages May 20, 2009 – Jun 4, 2009 Page 2 of 2

Subsystems (3)
lore
Share & tools
lore
#189 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

In reply to Alan Stern
Quoted 8 lines · Alan Stern
Quoted 6 lines · Kay Sievers
Quoted 2 lines · James Bottomley

OK ... perhaps we have to wait a little harder: try this; it waits until all the targets have disappeared from visibility via an event.

Quoted 6 lines
Quoted 1 line

That seems to work fine here.

It's good for a short-term fix. For the longer term, I still think it's a mistake to wait for the sdevs to be released before deleting the target. It gives user programs the ability to block the host-removal thread indefinitely.

How can user programs block removal indefinitely today?

Eric

#190 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

Quoted 10 lines · Eric W. Biederman
Quoted 8 lines · Alan Stern
Quoted 6 lines · Kay Sievers
Quoted 2 lines · James Bottomley

OK ... perhaps we have to wait a little harder: try this; it waits until all the targets have disappeared from visibility via an event.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

That seems to work fine here.

It's good for a short-term fix. For the longer term, I still think it's a mistake to wait for the sdevs to be released before deleting the target. It gives user programs the ability to block the host-removal thread indefinitely.

How can user programs block removal indefinitely today?

As fas as I know, they can't. Instead, they can cause the SCSI layer to unregister a sysfs directory containing a child directory. :-)

Basically, a user program can delay removal of the child (i.e., the target) directory indefinitely, because currently the target isn't unregistered when all its children are removed -- it's unregistered when all its children are _released_.

Alan Stern

#191 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

Quoted 8 lines · Alan Stern

As fas as I know, they can't. Instead, they can cause the SCSI layer to unregister a sysfs directory containing a child directory. :-)

Basically, a user program can delay removal of the child (i.e., the target) directory indefinitely, because currently the target isn't unregistered when all its children are removed -- it's unregistered when all its children are _released_.

Ok. Is this opens of /dev/sda1 and the like that are being held open by userspace that are potentially causing problems?

I think I have the fix to that...

Eric

#192 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

Quoted 6 lines · Eric W. Biederman
Quoted 4 lines · Alan Stern

As fas as I know, they can't. Instead, they can cause the SCSI layer to unregister a sysfs directory containing a child directory. :-)

Quoted 7 lines
Quoted 4 lines

Basically, a user program can delay removal of the child (i.e., the target) directory indefinitely, because currently the target isn't unregistered when all its children are removed -- it's unregistered when all its children are _released_.

Ok. Is this opens of /dev/sda1 and the like that are being held open by userspace that are potentially causing problems?

Yes, plus any other mechanism for preventing a struct device's refcount from going to 0.

Quoted 1 line

I think I have the fix to that...

The fix is to delete the target when its children are deleted, and not wait until the children are released.

Alan Stern

#193 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

Quoted 13 lines · Alan Stern
Quoted 11 lines · Eric W. Biederman
Quoted 8 lines · Alan Stern

As fas as I know, they can't. Instead, they can cause the SCSI layer to unregister a sysfs directory containing a child directory. :-)

Basically, a user program can delay removal of the child (i.e., the target) directory indefinitely, because currently the target isn't unregistered when all its children are removed -- it's unregistered when all its children are _released_.

Quoted 5 lines
Quoted 2 lines

Ok. Is this opens of /dev/sda1 and the like that are being held open by userspace that are potentially causing problems?

Yes, plus any other mechanism for preventing a struct device's refcount from going to 0.

Thanks. The discussion makes sense now.

Quoted 4 lines
Quoted 1 line

I think I have the fix to that...

The fix is to delete the target when its children are deleted, and not wait until the children are released.

I think I can do both.

I am currently working on a patchset which at the VFS layer disconnects a fd from an underlying device. It does the necessary use count tracking and when the disconnect is done it returns. As part of the disconnect it calls the release method.

We already do this in at least sysfs, proc, sysctl, and sound. So I figure it is time to move this into some generic code so we don't need to duplicate the bugs and the insanities.

Once merged it would take just a few lines of code to use this functionality.

Eric

#194 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

Quoted 12 lines · Eric W. Biederman
Quoted 4 lines
Quoted 1 line

I think I have the fix to that...

The fix is to delete the target when its children are deleted, and not wait until the children are released.

I think I can do both.

I am currently working on a patchset which at the VFS layer disconnects a fd from an underlying device. It does the necessary use count tracking and when the disconnect is done it returns. As part of the disconnect it calls the release method.

Quoted 5 lines

We already do this in at least sysfs, proc, sysctl, and sound. So I figure it is time to move this into some generic code so we don't need to duplicate the bugs and the insanities.

Once merged it would take just a few lines of code to use this functionality.

That's good, but it isn't enough to solve this problem. Even though user programs might not be able to pin the device by holding the file open any more, other mechanisms (internal to the kernel) could have the same effect. Only a short delay is needed.

The underlying cause should be fixed properly.

Alan Stern

#195 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

In reply to James Bottomley
Quoted 2 lines · James Bottomley

Um, well, you're right, it's wrong. The reap needs to be matched with the reap_ref++

I was afraid of that. Not understanding how the reap_ref counts are supposed to work makes it hard to get them right...

Quoted 4 lines

It's hard to follow the problem without full context, but if I understand correctly the problem is you want all the target directories removed before you call device_del() on the host and the thing that gets in the way is the necessary user context removal of the host.

Removal of the _target_, not the host. (Do targets ever get reaped from non-process context? I didn't notice any places where that would happen.)

That's part of the problem. The other part is that the target isn't unregistered until all the sdevs have been released, which might not happen for a long time if the sdevs are pinned for any reason.

That is, the target should be unregistered when all the sdevs are deleted, in __scsi_remove_device(), not when scsi_device_dev_release() or scsi_device_dev_release_usercontext() runs.

Quoted 15 lines

So a
simple solution, rather than mucking with the way it works, is to wait for the workqueues to complete. Does this fix it?

James

---

drivers/scsi/scsi_scan.c +0 -0
index c447838..5846c26 100644
@@ -1877,6 +1877,12 @@ void scsi_forget_host(struct Scsi_Host *shost)
 		goto restart;
 	}

drivers/scsi/scsi_scan.c void scsi_forget_host(struct Scsi_Host *shost) · line 1888

Quoted 10 lines
drivers/scsi/scsi_scan.c +6 -0
 	spin_unlock_irqrestore(shost->host_lock, flags);
+
+	/*
+	 * the sdev removal goes through a workqueue for user context, so
+	 * make sure it's all complete before we return
+	 */
+	flush_scheduled_work();
 }
 
 /*

This may well fix Kay's problem, but I think the other point needs to be addressed also.

Alan Stern

#196 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

In reply to Kay Sievers
Quoted 11 lines · Kay Sievers
Quoted 4 lines

Let's make the plan to investigate these, and see how hard it would be to actually remove these with the current device/sysfs infrastructure.

Fixing the users and adding back auto-deletion are the only two real options.

Seems, we should remove non-directory files, which in most cases belong to the kobject itself, but the user's cleanup logic does not cover the removal of the created files.

But I think, we should still warn, if we find a sub-directory inside a directory we are going to remove.

So far complaining about deleting non-empty directories is finding real bugs. It does not appear that too many users that delete non-empty directories.

My plan moving forward is to see what has goofed and how hard it is to change the callers to clean up after themselves. If it is not a pain to fix the callers who forget to delete their attributes that looks like the right way forward.

It is certainly the principle of least surprise.

Eric

#197 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

Quoted 18 lines · [email protected] (Eric W. Biederman) writes:
Quoted 11 lines · Kay Sievers
Quoted 4 lines

Let's make the plan to investigate these, and see how hard it would be to actually remove these with the current device/sysfs infrastructure.

Fixing the users and adding back auto-deletion are the only two real options.

Seems, we should remove non-directory files, which in most cases belong to the kobject itself, but the user's cleanup logic does not cover the removal of the created files.

But I think, we should still warn, if we find a sub-directory inside a directory we are going to remove.

So far complaining about deleting non-empty directories is finding real bugs. It does not appear that too many users that delete non-empty directories.

Quoted 6 lines

My plan moving forward is to see what has goofed and how hard it is to change the callers to clean up after themselves. If it is not a pain to fix the callers who forget to delete their attributes that looks like the right way forward.

It is certainly the principle of least surprise.

Currently I expect that those attributes that are not deleted may actually be bugs as well. If a driver manually adds sysfs files after device_create/device_register the uevent will have already been sent and you can not safely use those attributes in when processing a hotplug event.

Eric

#198 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

Quoted 7 lines · Eric W. Biederman

[email protected] (Eric W. Biederman) writes:

Currently I expect that those attributes that are not deleted may actually be bugs as well.  If a driver manually adds sysfs files after device_create/device_register the uevent will have already been sent and you can not safely use those attributes in when processing a hotplug event.

That's true, and would be good to fix. I guess we need to come up with a way of maintaining binary attributes in the same way as "default" attributes. Today they need to be created manually.

Maybe we can get your patch series in, with the version that removes the files, but warns on existing directories, and work on fixing the users, and possible remove the deletion of files when we fixed all the current users?

Thanks,
Kay
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#199 lore raw

Re: [PATCH 25/20] sysfs: Only support removing emtpy sysfs directories.

In reply to Eric W. Biederman
Quoted 9 lines · Eric W. Biederman

From: Eric W. Biederman <[email protected]>

I have looked and I have not found a single legitimate case today where we remove sysfs directories with anything in them. The only case I have found to date was a bug. It was a problem of ownership. The files in
the directory where not owned by the directory itself. Leaving open
the potential for double deletion of the directory contents.

For 21-24: Acked-by: Tejun Heo <[email protected]>

25 maybe can be folded into the eariler in the series?

Thanks.

Signature · 1 line
tejun