CIFS: Do not kmalloc under the flocks spinlock
Subsystems (2)
PATCH 1/1 CIFS: Do not kmalloc under the flocks spinlock
Reorganize the code to make the memory already allocated before spinlock'ed loop.
--- fs/cifs/file.c | 49 ++++++++++++++++++++++++++++++++----------------- 1 files changed, 32 insertions(+), 17 deletions(-)
fs/cifs/file.c +32 -17
@@ -926,10 +926,10 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)struct cifsInodeInfo *cinode =CIFS_I (cfile->dentry->d_inode);struct cifs_tcon *tcon =tlink_tcon (cfile->tlink);struct file_lock *flock, **before; -struct cifsLockInfo *lck, *tmp; +struct cifsLockInfo *lcks, *lck; +unsigned int count =0 , i =0 ;int rc =0 , xid, type;__u64 length; -struct list_head locks_to_send; xid =GetXid ();
@@ -940,7 +940,19 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)return rc; } -INIT_LIST_HEAD (&locks_to_send); +lock_flocks (); +cifs_for_each_lock (cfile->dentry->d_inode, before) + count++; +unlock_flocks (); + +/* +* We can safely allocate count locks, because the hold lock_mutex. +* Allocate (count + 1) * 2 locks to be more safe. +*/ + count = (count +1 ) *2 ; + lcks =kmalloc (count *sizeof (struct cifsLockInfo ),GFP_KERNEL ); +if (!lcks) + goto out;lock_flocks ();cifs_for_each_lock (cfile->dentry->d_inode, before) {
@@ -950,35 +962,38 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) type =CIFS_RDLCK ; else type =CIFS_WRLCK ; - - lck =cifs_lock_init (flock->fl_start, length, type, - cfile->netfid); -if (!lck) { - rc = -ENOMEM ; - goto send_locks; - } + lck = &lcks[i]; lck->pid = flock->fl_pid; - -list_add_tail (&lck->llist, &locks_to_send); + lck->netfid = cfile->netfid; + lck->length = length; + lck->type = type; + lck->offset = flock->fl_start; + i++; +if (i == count) +break ; } - -send_locks:unlock_flocks (); -list_for_each_entry_safe (lck, tmp, &locks_to_send, llist) { +if (i == count) +cERROR (1 ,"Can't push all brlocks!" ); + + count = i; + +for (i =0 ; i < count; i++) {struct file_lock tmp_lock;int stored_rc; + lck = &lcks[i]; tmp_lock.fl_start = lck->offset; stored_rc =CIFSSMBPosixLock (xid, tcon, lck->netfid, lck->pid,0 , lck->length, &tmp_lock, lck->type,0 );if (stored_rc) rc = stored_rc; -list_del (&lck->llist); -kfree (lck); } +kfree (lcks); +out: cinode->can_cache_brlcks = false;mutex_unlock (&cinode->lock_mutex);
Signature · 1 line
1.7.1
A message that is not in the archive (sent off-list or not yet received): 1329901855-11683-1-git-send-email-piastry-7qunaywFIewox3rIn2DAYQ@public.gmane.org
Quoted 34 lines · On Wed, 22 Feb 2012 12:10:55 +0300 Pavel Shilovsky…
Reorganize the code to make the memory already allocated before spinlock'ed loop.
Signed-off-by Pavel Shilovsky <[email protected]>--- fs/cifs/file.c | 49 ++++++++++++++++++++++++++++++++----------------- 1 files changed, 32 insertions(+), 17 deletions(-)fs/cifs/file.c +6
-3@@ -926,10 +926,10 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)struct cifsInodeInfo *cinode =CIFS_I (cfile->dentry->d_inode);struct cifs_tcon *tcon =tlink_tcon (cfile->tlink);struct file_lock *flock, **before; -struct cifsLockInfo *lck, *tmp; +struct cifsLockInfo *lcks, *lck; +unsigned int count =0 , i =0 ;int rc =0 , xid, type;__u64 length; -struct list_head locks_to_send; xid =GetXid ();@@ -940,7 +940,19 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile)return rc; } -INIT_LIST_HEAD (&locks_to_send); +lock_flocks (); +cifs_for_each_lock (cfile->dentry->d_inode, before) + count++; +unlock_flocks ();
fs/cifs/file.c cifs_push_posix_locks(struct cifsFileInfo *cfile) · line 956
Quoted 10 lines
fs/cifs/file.c +9
-0+ +/* +* We can safely allocate count locks, because the hold lock_mutex. +* Allocate (count + 1) * 2 locks to be more safe. +*/ + count = (count +1 ) *2 ; + lcks =kmalloc (count *sizeof (struct cifsLockInfo ),GFP_KERNEL ); +if (!lcks) + goto out;
Hmm...looking at my machine:
(gdb) p sizeof(struct cifsLockInfo)
$1 = 144
So that's 144 bytes per struct...what's the upper limit on "count"?
It seems like that could potentially be a large allocation, and we have to do this in the oplock break codepath.
Instead of doing this, would it be better to allocate these when the VFS first calls ->lock, and attach them to a new member of file_lock.fl_u?
Quoted 23 lines
fs/cifs/file.c +7
-9lock_flocks ();cifs_for_each_lock (cfile->dentry->d_inode, before) {@@ -950,35 +962,38 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) type =CIFS_RDLCK ; else type =CIFS_WRLCK ; - - lck =cifs_lock_init (flock->fl_start, length, type, - cfile->netfid); -if (!lck) { - rc = -ENOMEM ; - goto send_locks; - } + lck = &lcks[i]; lck->pid = flock->fl_pid; - -list_add_tail (&lck->llist, &locks_to_send); + lck->netfid = cfile->netfid; + lck->length = length; + lck->type = type; + lck->offset = flock->fl_start; + i++; +if (i == count)
fs/cifs/file.c cifs_push_posix_locks(struct cifsFileInfo *cfile) · line 979
Quoted 10 lines
fs/cifs/file.c +4
-3+break ; } - -send_locks:unlock_flocks (); -list_for_each_entry_safe (lck, tmp, &locks_to_send, llist) { +if (i == count) +cERROR (1 ,"Can't push all brlocks!" ); +
This sort of thing always makes me a little worried. Sure, you're allocating a bunch of extra locks, but what's the potential effect of not being able to push all the locks here? The user is going to see this message and have no clue of what it means.
Even I'm not sure what this means in practice. The VFS will have a record of this lock, but the server won't...
Quoted 22 lines
fs/cifs/file.c +6
-2+ count = i; + +for (i =0 ; i < count; i++) {struct file_lock tmp_lock;int stored_rc; + lck = &lcks[i]; tmp_lock.fl_start = lck->offset; stored_rc =CIFSSMBPosixLock (xid, tcon, lck->netfid, lck->pid,0 , lck->length, &tmp_lock, lck->type,0 );if (stored_rc) rc = stored_rc; -list_del (&lck->llist); -kfree (lck); } +kfree (lcks); +out: cinode->can_cache_brlcks = false;mutex_unlock (&cinode->lock_mutex);
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20120222073446.1fbdddac-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org
2012/2/22 Jeff Layton <[email protected]>:
Quoted 6 lines
Hmm...looking at my machine:
(gdb) p sizeof(struct cifsLockInfo)
$1 = 144
Quoted 8 lines
So that's 144 bytes per struct...what's the upper limit on "count"?
It seems like that could potentially be a large allocation, and we have to do this in the oplock break codepath.
Instead of doing this, would it be better to allocate these when the VFS first calls ->lock, and attach them to a new member of file_lock.fl_u?
Every time we can have a different number of locks - so, can't pre-allocate it. I can create new struct for this only - without extra llist, blist, block_q fields, that can save enough memory.
Quoted 40 lines
Quoted 30 lines
lock_flocks();cifs_for_each_lock(cfile->dentry->d_inode, before){fs/cifs/file.c +0
-0@@ -950,35 +962,38 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) type = CIFS_RDLCK;else
type =CIFS_WRLCK;
-
- lck = cifs_lock_init(flock->fl_start, length, type,
- cfile->netfid);
- if (!lck) {
- rc =-ENOMEM;
- gotosend_locks;
- }
+ lck = &lcks[i];
lck->pid=flock->fl_pid;
-
-list_add_tail(&lck->llist, &locks_to_send);
+lck->netfid=cfile->netfid;
+lck->length= length;
+lck->type= type;
+lck->offset=flock->fl_start;
+ i++;
+ if (i == count)
+ break;
}(quoted diff) +0
-2- -send_locks:
unlock_flocks();Quoted 4 lines
-
list_for_each_entry_safe(lck, tmp, &locks_to_send, llist){
+ if (i == count)
+cERROR(1, "Can't push all brlocks!");
+This sort of thing always makes me a little worried. Sure, you're allocating a bunch of extra locks, but what's the potential effect of not being able to push all the locks here? The user is going to see this message and have no clue of what it means.
Quoted 3 lines
Even I'm not sure what this means in practice. The VFS will have a record of this lock, but the server won't...
I really think that there will be no such situations, because we hold inode->lock_mutex - the locking state should leave the same.
Signature · 2 lines
Best regards, Pavel Shilovsky.
A message that is not in the archive (sent off-list or not yet received): CAKywueRMobe5OEm4K2Xrh3KwTDKXk4Uh-68gM==UnHXvQNBonQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 12 lines · On Thu, 23 Feb 2012 10:56:40 +0400 Pavel Shilovsky…
2012/2/22 Jeff Layton <[email protected]>:
Quoted 11 lines
Hmm...looking at my machine:
(gdb) p sizeof(struct cifsLockInfo)
$1 = 144So that's 144 bytes per struct...what's the upper limit on "count"?
It seems like that could potentially be a large allocation, and we have to do this in the oplock break codepath.
Quoted 6 lines
Quoted 3 lines
Instead of doing this, would it be better to allocate these when the VFS first calls ->lock, and attach them to a new member of file_lock.fl_u?
Every time we can have a different number of locks - so, can't pre-allocate it.
You could preallocate these:
1/ Do the allocation in cifs_setlk
2/ Attach it to a new fl_u member in the file_lock struct 3/ Add a fl_release_private op to free that object
Of course, you will need to consider how to deal with lock splitting and merging if you do that...
Quoted 3 lines
I can create new struct for this only - without extra llist, blist,
block_qfields, that can save enough memory.
That sounds preferable. We really don't want to allocate more memory than is needed. Plus, anytime you duplicate information at least one copy is going to be wrong at some point in time.
Quoted 40 lines
Quoted 40 lines
Quoted 30 lines
lock_flocks();cifs_for_each_lock(cfile->dentry->d_inode, before){fs/cifs/file.c +0
-0@@ -950,35 +962,38 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) type = CIFS_RDLCK;else
type =CIFS_WRLCK;
-
- lck = cifs_lock_init(flock->fl_start, length, type,
- cfile->netfid);
- if (!lck) {
- rc =-ENOMEM;
- gotosend_locks;
- }
+ lck = &lcks[i];
lck->pid=flock->fl_pid;
-
-list_add_tail(&lck->llist, &locks_to_send);
+lck->netfid=cfile->netfid;
+lck->length= length;
+lck->type= type;
+lck->offset=flock->fl_start;
+ i++;
+ if (i == count)
+ break;
}(quoted diff) +0
-2- -send_locks:
unlock_flocks();Quoted 4 lines
-
list_for_each_entry_safe(lck, tmp, &locks_to_send, llist){
+ if (i == count)
+cERROR(1, "Can't push all brlocks!");
+This sort of thing always makes me a little worried. Sure, you're allocating a bunch of extra locks, but what's the potential effect of not being able to push all the locks here? The user is going to see this message and have no clue of what it means.
Quoted 7 lines
Quoted 3 lines
Even I'm not sure what this means in practice. The VFS will have a record of this lock, but the server won't...
I really think that there will be no such situations, because we hold
inode->lock_mutex- the locking state should leave the same.
I don't share that optimism.
The lock_mutex protects the cinode->llist, but here you're walking the inode->i_flock list. It's certainly possible that it could change in between unlock_flocks and lock_flocks.
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20120223064931.23539689-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org
2012/2/23 Jeff Layton <[email protected]>:
Quoted 19 lines
Quoted 16 lines · On Thu, 23 Feb 2012 10:56:40 +0400 Pavel Shilovsky…
2012/2/22 Jeff Layton <[email protected]>:
Quoted 14 lines
Hmm...looking at my machine:
(gdb) p sizeof(struct cifsLockInfo)
$1 = 144So that's 144 bytes per struct...what's the upper limit on "count"?
It seems like that could potentially be a large allocation, and we have to do this in the oplock break codepath.
Instead of doing this, would it be better to allocate these when the VFS first calls ->lock, and attach them to a new member of file_lock.fl_u?
Quoted 7 lines
Quoted 2 lines
Every time we can have a different number of locks - so, can't pre-allocate it.
You could preallocate these:
1/ Do the allocation in
cifs_setlk
2/ Attach it to a newfl_umember in thefile_lockstruct
I am sure I got the idea. Do you mean to allocate the memory in inode->i_flock?
Quoted 4 lines
3/ Add a
fl_release_privateop to free that objectOf course, you will need to consider how to deal with lock splitting and merging if you do that...
Yes, how can we deal with this sort of things? We can calculate the lock number after every cifs_setlk but it's rather time consumption.
If we use this struct:
struct lock_to_push {
__u64 offset;
__u64 length;
__u32 pid;
__u16 netfid;
__u8 type;
};it's sizeof is 24 bytes. If we have e.g 10^5 locks we end up with 2400000 bytes = 2.4 megabyte - don't think allocating this plays any big role in whole time we need to push 10^5 locks to the server. What's the problem with allocating it here?
Quoted 57 lines
Quoted 4 lines
I can create new struct for this only - without extra llist, blist,
block_qfields, that can save enough memory.That sounds preferable. We really don't want to allocate more memory than is needed. Plus, anytime you duplicate information at least one copy is going to be wrong at some point in time.
Quoted 40 lines
Quoted 40 lines
Quoted 30 lines
lock_flocks();cifs_for_each_lock(cfile->dentry->d_inode, before){(quoted diff) +0
-0@@ -950,35 +962,38 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) type = CIFS_RDLCK;else
type =CIFS_WRLCK;
-
- lck = cifs_lock_init(flock->fl_start, length, type,
- cfile->netfid);
- if (!lck) {
- rc =-ENOMEM;
- gotosend_locks;
- }
+ lck = &lcks[i];
lck->pid=flock->fl_pid;
-
-list_add_tail(&lck->llist, &locks_to_send);
+lck->netfid=cfile->netfid;
+lck->length= length;
+lck->type= type;
+lck->offset=flock->fl_start;
+ i++;
+ if (i == count)
+ break;
}(quoted diff) +0
-2- -send_locks:
unlock_flocks();Quoted 4 lines
-
list_for_each_entry_safe(lck, tmp, &locks_to_send, llist){
+ if (i == count)
+cERROR(1, "Can't push all brlocks!");
+This sort of thing always makes me a little worried. Sure, you're allocating a bunch of extra locks, but what's the potential effect of not being able to push all the locks here? The user is going to see this message and have no clue of what it means.
Quoted 7 lines
Quoted 3 lines
Even I'm not sure what this means in practice. The VFS will have a record of this lock, but the server won't...
I really think that there will be no such situations, because we hold
inode->lock_mutex- the locking state should leave the same.
Quoted 6 lines
I don't share that optimism.
The
lock_mutexprotects thecinode->llist, but here you're walking theinode->i_flocklist. It's certainly possible that it could change in betweenunlock_flocksandlock_flocks.
The lock_mutex now protects not only cinode->llist, but locks from the inode structure reached from cifs code as well. The only thing I missed is posix_lock_file_wait in the end of the cifs_setlk function - it should be surrounded with lock_mutex as well. According to this, there is no other way to increase POSIX lock number from the inode except locking/unlocking lock_mutex.
Signature · 2 lines
Best regards, Pavel Shilovsky.
A message that is not in the archive (sent off-list or not yet received): CAKywueRJHQiAjkqDgX=ZmSKcc1bAi7o4AuyFzCj0Grjq0HSE9g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 23 lines · On Fri, 24 Feb 2012 15:32:32 +0400 Pavel Shilovsky…
2012/2/23 Jeff Layton <[email protected]>:
Quoted 22 lines
Quoted 18 lines · On Thu, 23 Feb 2012 10:56:40 +0400 Pavel Shilovsky…
2012/2/22 Jeff Layton <[email protected]>:
Quoted 6 lines
Hmm...looking at my machine:
(gdb) p sizeof(struct cifsLockInfo)
$1 = 144Quoted 8 lines
So that's 144 bytes per struct...what's the upper limit on "count"?
It seems like that could potentially be a large allocation, and we have to do this in the oplock break codepath.
Instead of doing this, would it be better to allocate these when the VFS first calls ->lock, and attach them to a new member of file_lock.fl_u?
Every time we can have a different number of locks - so, can't pre-allocate it.
Quoted 7 lines
Quoted 4 lines
You could preallocate these:
1/ Do the allocation in
cifs_setlk
2/ Attach it to a newfl_umember in thefile_lockstructI am sure I got the idea. Do you mean to allocate the memory in
inode->i_flock?
Well, no...I meant that you would want to add a new file_lock->fl_u union member and stick the pointer there and do the allocation earlier in cifs_setlk.
Quoted 18 lines
Quoted 4 lines
3/ Add a
fl_release_privateop to free that objectOf course, you will need to consider how to deal with lock splitting and merging if you do that...
Yes, how can we deal with this sort of things? We can calculate the lock number after every
cifs_setlkbut it's rather time consumption.If we use this struct:
struct lock_to_push { __u64 offset; __u64 length; __u32 pid; __u16 netfid; __u8 type; };
Quoted 5 lines
it's sizeof is 24 bytes. If we have e.g 10^5 locks we end up with 2400000 bytes = 2.4 megabyte - don't think allocating this plays any big role in whole time we need to push 10^5 locks to the server. What's the problem with allocating it here?
2.4 M would be an enormous allocation in the kernel and would almost certainly fail. :)
In order to do a large allocation in the kernel via kmem_cache_alloc, you need contiguous physical memory. Over time, memory gets fragmented. The page allocator may need to do things like initiate writeback or migrate pages in order to satisfy your request. That can be very slow and creates extra load on the box.
It's best to avoid that if possible, especially in codepaths like this where you're doing this in workqueue context and reporting an error back to the user is pretty much impossible.
If you do these allocations up front, when you set the lock the caller can get back an ENOMEM or whatever on the fcntl call. At this point, recovering from an allocation error will always be problematic.
Quoted 64 lines
Quoted 57 lines
Quoted 4 lines
I can create new struct for this only - without extra llist, blist,
block_qfields, that can save enough memory.That sounds preferable. We really don't want to allocate more memory than is needed. Plus, anytime you duplicate information at least one copy is going to be wrong at some point in time.
Quoted 40 lines
Quoted 40 lines
Quoted 30 lines
lock_flocks();cifs_for_each_lock(cfile->dentry->d_inode, before){(quoted diff) +0
-0@@ -950,35 +962,38 @@ cifs_push_posix_locks(struct cifsFileInfo *cfile) type = CIFS_RDLCK;else
type =CIFS_WRLCK;
-
- lck = cifs_lock_init(flock->fl_start, length, type,
- cfile->netfid);
- if (!lck) {
- rc =-ENOMEM;
- gotosend_locks;
- }
+ lck = &lcks[i];
lck->pid=flock->fl_pid;
-
-list_add_tail(&lck->llist, &locks_to_send);
+lck->netfid=cfile->netfid;
+lck->length= length;
+lck->type= type;
+lck->offset=flock->fl_start;
+ i++;
+ if (i == count)
+ break;
}(quoted diff) +0
-2- -send_locks:
unlock_flocks();Quoted 4 lines
-
list_for_each_entry_safe(lck, tmp, &locks_to_send, llist){
+ if (i == count)
+cERROR(1, "Can't push all brlocks!");
+This sort of thing always makes me a little worried. Sure, you're allocating a bunch of extra locks, but what's the potential effect of not being able to push all the locks here? The user is going to see this message and have no clue of what it means.
Quoted 7 lines
Quoted 3 lines
Even I'm not sure what this means in practice. The VFS will have a record of this lock, but the server won't...
I really think that there will be no such situations, because we hold
inode->lock_mutex- the locking state should leave the same.Quoted 6 lines
I don't share that optimism.
The
lock_mutexprotects thecinode->llist, but here you're walking theinode->i_flocklist. It's certainly possible that it could change in betweenunlock_flocksandlock_flocks.
Quoted 7 lines
The
lock_mutexnow protects not onlycinode->llist, but locks from the inode structure reached from cifs code as well. The only thing I missed isposix_lock_file_waitin the end of thecifs_setlkfunction - it should be surrounded withlock_mutexas well. According to this, there is no other way to increase POSIX lock number from the inode except locking/unlockinglock_mutex.
Ok, that will probably cover it this for now. Here's what I'd suggest:
1/ that locking needs to be documented, or were almost certain to break it later.
2/ don't allocate more structs than you need. If you know that the list won't change while you're holding the lock_mutex then there's no reason to do so.
That said...this whole scheme seems quite fragile. I wonder if there's a better way to do this that doesn't require quite so much to happen at oplock break time...
Signature · 1 line
Jeff Layton <[email protected]>