diff options
author | Tejun Heo <tj@kernel.org> | 2013-12-10 09:29:17 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2013-12-11 00:33:31 -0500 |
commit | 9b2db6e1894577d48f4e290381bac6e573593838 (patch) | |
tree | 517322169277f2ea3d5e540df2f1c4f1950d06e8 /fs/kernfs | |
parent | 13ccb93f4127baf53961a9497780a0f52e3c24e7 (diff) |
sysfs: bail early from kernfs_file_mmap() to avoid spurious lockdep warning
This is v3.14 fix for the same issue that a8b14744429f ("sysfs: give
different locking key to regular and bin files") addresses for v3.13.
Due to the extensive kernfs reorganization in v3.14 branch, the same
fix couldn't be ported as-is. The v3.13 fix was ignored while merging
it into v3.14 branch.
027a485d12e0 ("sysfs: use a separate locking class for open files
depending on mmap") assigned different lockdep key to
sysfs_open_file->mutex depending on whether the file implements mmap
or not in an attempt to avoid spurious lockdep warning caused by
merging of regular and bin file paths.
While this restored some of the original behavior of using different
locks (at least lockdep is concerned) for the different clases of
files. The restoration wasn't full because now the lockdep key
assignment depends on whether the file has mmap or not instead of
whether it's a regular file or not.
This means that bin files which don't implement mmap will get assigned
the same lockdep class as regular files. This is problematic because
file_operations for bin files still implements the mmap file operation
and checking whether the sysfs file actually implements mmap happens
in the file operation after grabbing @sysfs_open_file->mutex. We
still end up adding locking dependency from mmap locking to
sysfs_open_file->mutex to the regular file mutex which triggers
spurious circular locking warning.
For v3.13, a8b14744429f ("sysfs: give different locking key to regular
and bin files") fixed it by giving sysfs_open_file->mutex different
lockdep keys depending on whether the file is regular or bin instead
of whether mmap exists or not; however, due to the way sysfs is now
layered behind kernfs, this approach is no longer viable. kernfs can
tell whether a sysfs node has mmap implemented or not but can't tell
whether a bin file from a regular one.
This patch updates kernfs such that kernfs_file_mmap() checks
SYSFS_FLAG_HAS_MMAP and bail before grabbing sysfs_open_file->mutex so
that it doesn't add spurious locking dependency from mmap to
sysfs_open_file->mutex and changes sysfs so that it specifies
kernfs_ops->mmap iff the sysfs file implements mmap. Combined, this
ensures that sysfs_open_file->mutex is grabbed under mmap path iff the
sysfs file actually implements mmap. As sysfs_open_file->mutex is
already given a different lockdep key if mmap is implemented, this
removes the spurious locking dependency.
Signed-off-by: Tejun Heo <tj@kernel.org>
Reported-by: Dave Jones <davej@redhat.com>
Link: http://lkml.kernel.org/g/20131203184324.GA11320@redhat.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs/kernfs')
-rw-r--r-- | fs/kernfs/file.c | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 4a5863b79de9..fa053151fa96 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c | |||
@@ -421,6 +421,16 @@ static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma) | |||
421 | const struct kernfs_ops *ops; | 421 | const struct kernfs_ops *ops; |
422 | int rc; | 422 | int rc; |
423 | 423 | ||
424 | /* | ||
425 | * mmap path and of->mutex are prone to triggering spurious lockdep | ||
426 | * warnings and we don't want to add spurious locking dependency | ||
427 | * between the two. Check whether mmap is actually implemented | ||
428 | * without grabbing @of->mutex by testing HAS_MMAP flag. See the | ||
429 | * comment in kernfs_file_open() for more details. | ||
430 | */ | ||
431 | if (!(of->sd->s_flags & SYSFS_FLAG_HAS_MMAP)) | ||
432 | return -ENODEV; | ||
433 | |||
424 | mutex_lock(&of->mutex); | 434 | mutex_lock(&of->mutex); |
425 | 435 | ||
426 | rc = -ENODEV; | 436 | rc = -ENODEV; |
@@ -428,10 +438,7 @@ static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma) | |||
428 | goto out_unlock; | 438 | goto out_unlock; |
429 | 439 | ||
430 | ops = kernfs_ops(of->sd); | 440 | ops = kernfs_ops(of->sd); |
431 | if (ops->mmap) | 441 | rc = ops->mmap(of, vma); |
432 | rc = ops->mmap(of, vma); | ||
433 | if (rc) | ||
434 | goto out_put; | ||
435 | 442 | ||
436 | /* | 443 | /* |
437 | * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() | 444 | * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup() |
@@ -596,6 +603,9 @@ static int kernfs_file_open(struct inode *inode, struct file *file) | |||
596 | * happen on the same file. At this point, we can't easily give | 603 | * happen on the same file. At this point, we can't easily give |
597 | * each file a separate locking class. Let's differentiate on | 604 | * each file a separate locking class. Let's differentiate on |
598 | * whether the file has mmap or not for now. | 605 | * whether the file has mmap or not for now. |
606 | * | ||
607 | * Both paths of the branch look the same. They're supposed to | ||
608 | * look that way and give @of->mutex different static lockdep keys. | ||
599 | */ | 609 | */ |
600 | if (has_mmap) | 610 | if (has_mmap) |
601 | mutex_init(&of->mutex); | 611 | mutex_init(&of->mutex); |