IB/mthca: Fix how mthca_map_user_db() calls gup
PATCH 1/1 IB/mthca: Fix how mthca_map_user_db() calls gup
mthca_map_user_db() has two problems regarding the call to get_user_pages():
(i) It is not done under mmap_sem.
(ii) It is done under the db_table mutex, which protects all database related operations. Should any of these be called under mmap_sem, we get an ABBA deadlock. In addition, gup can be performance intensive, which could contend other mapping/ unmapping ops.
To fix this, we can drop the mutex while doing a gup_fast(), once done, recheck to see the page was mapped while we didn't hold the mutex, and exit out with the corresponding housekeeping.
---
- Compile tested only.
- Should I be wrong about no callers already holding mmap_sem, I still think calling gup without the mutex makes sense for improved paralellism. Now, if callers can hold the mmap_sem, it's wrong to do copy_from_user right before calling mthca_map_user_db.
drivers/infiniband/hw/mthca/mthca_memfree.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/infiniband/hw/mthca/mthca_memfree.c +19 -1
@@ -472,9 +472,27 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, goto out; } - ret =get_user_pages (uaddr &PAGE_MASK ,1 ,FOLL_WRITE , pages,NULL ); +mutex_unlock (&db_tab->mutex); + + ret =get_user_pages_fast (uaddr &PAGE_MASK ,1 ,FOLL_WRITE , pages);if (ret <0 ) +return ret; + +mutex_lock (&db_tab->mutex); + +if (db_tab->page[i].refcount >=MTHCA_DB_REC_PER_PAGE || + (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr)) { +put_page (pages[0 ]); + ret = -EINVAL ; goto out; + } + +/* page was already mapped by another task while we were doing gup */ +if (db_tab->page[i].refcount) { +put_page (pages[0 ]); + ++db_tab->page[i].refcount; + goto out; + }sg_set_page (&db_tab->page[i].mem, pages[0 ],MTHCA_ICM_PAGE_SIZE , uaddr & ~PAGE_MASK );
Signature · 1 line
2.13.6
Quoted 15 lines · Davidlohr Bueso
mthca_map_user_db()has two problems regarding the call toget_user_pages():(i) It is not done under
mmap_sem.(ii) It is done under the
db_tablemutex, which protects all database related operations. Should any of these be called undermmap_sem, we get an ABBA deadlock. In addition, gup can be performance intensive, which could contend other mapping/ unmapping ops.To fix this, we can drop the mutex while doing a
gup_fast(), once done, recheck to see the page was mapped while we didn't hold the mutex, and exit out with the corresponding housekeeping.
Quoted 5 lines
Suggested-by Al Viro <viro-RmSDqhL/[email protected]>Signed-off-by Davidlohr Bueso <[email protected]>---
- Compile tested only.
Jason and I talked about this offline a bit. We're concerned about taking a patch like this into an ancient, unmaintained, but working driver. Especially when it's only compile tested. I have mthca hardware on hand and I can test it, but in this case, testing your patch would require triggering a race condition that we really don't have a way to test.
Quoted 4 lines
- Should I be wrong about no callers already holding
mmap_sem, I still think calling gup without the mutex makes sense for improved paralellism. Now, if callers can hold themmap_sem, it's wrong to docopy_from_userright before callingmthca_map_user_db.
So, if I understand you correctly, we (well, you and Al would be more correct, we haven't looked into the situation yet, so Mellanox people that worked on this in the day might now, or someone taking the time to research it could find out) don't have a clear understanding of all the conditions this function is called under, and so we actually don't know what the best way forward is to fix it?
Quoted 37 lines
drivers/infiniband/hw/mthca/mthca_memfree.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)drivers/infiniband/hw/mthca/mthca_memfree.c +19
-1@@ -472,9 +472,27 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar, goto out; } - ret =get_user_pages (uaddr &PAGE_MASK ,1 ,FOLL_WRITE , pages,NULL ); +mutex_unlock (&db_tab->mutex); + + ret =get_user_pages_fast (uaddr &PAGE_MASK ,1 ,FOLL_WRITE , pages);if (ret <0 ) +return ret; + +mutex_lock (&db_tab->mutex); + +if (db_tab->page[i].refcount >=MTHCA_DB_REC_PER_PAGE || + (db_tab->page[i].uvirt && db_tab->page[i].uvirt != uaddr)) { +put_page (pages[0 ]); + ret = -EINVAL ; goto out; + } + +/* page was already mapped by another task while we were doing gup */ +if (db_tab->page[i].refcount) { +put_page (pages[0 ]); + ++db_tab->page[i].refcount; + goto out; + }sg_set_page (&db_tab->page[i].mem, pages[0 ],MTHCA_ICM_PAGE_SIZE , uaddr & ~PAGE_MASK );
Signature · 3 lines
Doug Ledford <[email protected]> GPG KeyID: B826A3330E572FDD Key fingerprint = AE6B 1BDA 122B 23B4 265B 1274 B826 A333 0E57 2FDD
Quoted 11 lines · Doug Ledford
Quoted 4 lines
- Should I be wrong about no callers already holding
mmap_sem, I still think calling gup without the mutex makes sense for improved paralellism. Now, if callers can hold themmap_sem, it's wrong to docopy_from_userright before callingmthca_map_user_db.So, if I understand you correctly, we (well, you and Al would be more correct, we haven't looked into the situation yet, so Mellanox people that worked on this in the day might now, or someone taking the time to research it could find out) don't have a clear understanding of all the conditions this function is called under, and so we actually don't know what the best way forward is to fix it?
I looked at it enough to be confident that mthca_map_user_db is never called with mmap_sem held.
Also pretty confident that mthca_unmap_user_db is never called with mmap_sem.
So how about just grabbing mmap_sem around the call to get_user_pages and no other changes?
Jason
Quoted 13 lines · Jason Gunthorpe
Quoted 11 lines · Doug Ledford
Quoted 4 lines
- Should I be wrong about no callers already holding
mmap_sem, I still think calling gup without the mutex makes sense for improved paralellism. Now, if callers can hold themmap_sem, it's wrong to docopy_from_userright before callingmthca_map_user_db.So, if I understand you correctly, we (well, you and Al would be more correct, we haven't looked into the situation yet, so Mellanox people that worked on this in the day might now, or someone taking the time to research it could find out) don't have a clear understanding of all the conditions this function is called under, and so we actually don't know what the best way forward is to fix it?
Quoted 8 lines
I looked at it enough to be confident that
mthca_map_user_dbis never called withmmap_semheld.Also pretty confident that
mthca_unmap_user_dbis never called withmmap_sem.So how about just grabbing
mmap_semaround the call toget_user_pagesand no other changes?
Since the original post was referred to an ABBA deadlock, wouldn't we have to drop db_tab->mutex, then grab both in the proper order?
Signature · 3 lines
Doug Ledford <[email protected]> GPG KeyID: B826A3330E572FDD Key fingerprint = AE6B 1BDA 122B 23B4 265B 1274 B826 A333 0E57 2FDD
Quoted 17 lines · Doug Ledford
Quoted 16 lines · Jason Gunthorpe
Quoted 11 lines · Doug Ledford
Quoted 4 lines
- Should I be wrong about no callers already holding
mmap_sem, I still think calling gup without the mutex makes sense for improved paralellism. Now, if callers can hold themmap_sem, it's wrong to docopy_from_userright before callingmthca_map_user_db.So, if I understand you correctly, we (well, you and Al would be more correct, we haven't looked into the situation yet, so Mellanox people that worked on this in the day might now, or someone taking the time to research it could find out) don't have a clear understanding of all the conditions this function is called under, and so we actually don't know what the best way forward is to fix it?
I looked at it enough to be confident that
mthca_map_user_dbis never called withmmap_semheld.
Quoted 8 lines
Quoted 5 lines
Also pretty confident that
mthca_unmap_user_dbis never called withmmap_sem.So how about just grabbing
mmap_semaround the call toget_user_pagesand no other changes?Since the original post was referred to an ABBA deadlock, wouldn't we have to drop
db_tab->mutex, then grab both in the proper order?
I had understood that was only a concern because Davidlohr was having trouble proving the callchain didn't include mmap_sem already..
I can see the call chain all ends on verbs ops, and I know verbs ops with ucontext's are never called under mmap_sem by the core code..
Jason
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · Jason Gunthorpe
Quoted 3 lines
Since the original post was referred to an ABBA deadlock, wouldn't we have to drop
db_tab->mutex, then grab both in the proper order?I had understood that was only a concern because Davidlohr was having trouble proving the callchain didn't include
mmap_semalready..I can see the call chain all ends on verbs ops, and I know verbs ops with ucontext's are never called under
mmap_semby the core code..
Right. Ok so this simplifies things and we can just use gup_fast().
Thanks,
Davidlohr
---8<--------------------------------------------------- [PATCH v2] IB/mthca: Fix gup usage in mthca_map_user_db()
get_user_pages() must be called with mmap_sem held, currently it is not. In fact it is called under the user db_table->mutex. To fix this we can convert gup to use the fast alternative, and safely avoid taking mmap_sem, if possible. Furthermore this is safe wrt to the mutex as other callers that take the lock (unmap and alloc_db) are not called under mmap_sem (hence possible deadlock).
---drivers/infiniband/hw/mthca/mthca_memfree.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/hw/mthca/mthca_memfree.c +0 -0
@@ -472,7 +472,7 @@ int mthca_map_user_db(struct mthca_dev *dev, struct mthca_uar *uar,
goto out; }
- ret = get_user_pages(uaddr & PAGE_MASK, 1, FOLL_WRITE, pages, NULL);
+ ret = get_user_pages_fast(uaddr & PAGE_MASK, 1, FOLL_WRITE, pages); if (ret < 0) goto out;
Signature · 6 lines
2.13.6 -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Doug Ledford
AppliedQuoted 9 lines · Davidlohr Bueso
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines
Since the original post was referred to an ABBA deadlock, wouldn't we have to drop
db_tab->mutex, then grab both in the proper order?I had understood that was only a concern because Davidlohr was having trouble proving the callchain didn't include
mmap_semalready..
Quoted 7 lines
Quoted 2 lines
I can see the call chain all ends on verbs ops, and I know verbs ops with ucontext's are never called under
mmap_semby the core code..Right. Ok so this simplifies things and we can just use
gup_fast().Thanks,
Davidlohr
Thanks, applied.
Signature · 3 lines
Doug Ledford <[email protected]> GPG KeyID: B826A3330E572FDD Key fingerprint = AE6B 1BDA 122B 23B4 265B 1274 B826 A333 0E57 2FDD