Skip to content

Commit 23c84eb

Browse files
Matthew Wilcox (Oracle)djbw
authored andcommitted
dax: Fix missed wakeup with PMD faults
RocksDB can hang indefinitely when using a DAX file. This is due to a bug in the XArray conversion when handling a PMD fault and finding a PTE entry. We use the wrong index in the hash and end up waiting on the wrong waitqueue. There's actually no need to wait; if we find a PTE entry while looking for a PMD entry, we can return immediately as we know we should fall back to a PTE fault (which may not conflict with the lock held). We reuse the XA_RETRY_ENTRY to signal a conflicting entry was found. This value can never be found in an XArray while holding its lock, so it does not create an ambiguity. Cc: <[email protected]> Link: http://lkml.kernel.org/r/CAPcyv4hwHpX-MkUEqxwdTj7wCCZCN4RV-L4jsnuwLGyL_UEG4A@mail.gmail.com Fixes: b15cd80 ("dax: Convert page fault handlers to XArray") Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Tested-by: Dan Williams <[email protected]> Reported-by: Robert Barror <[email protected]> Reported-by: Seema Pandit <[email protected]> Reviewed-by: Jan Kara <[email protected]> Signed-off-by: Dan Williams <[email protected]>
1 parent 40cdc60 commit 23c84eb

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

fs/dax.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ static int dax_is_empty_entry(void *entry)
123123
return xa_to_value(entry) & DAX_EMPTY;
124124
}
125125

126+
/*
127+
* true if the entry that was found is of a smaller order than the entry
128+
* we were looking for
129+
*/
130+
static bool dax_is_conflict(void *entry)
131+
{
132+
return entry == XA_RETRY_ENTRY;
133+
}
134+
126135
/*
127136
* DAX page cache entry locking
128137
*/
@@ -195,11 +204,13 @@ static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all)
195204
* Look up entry in page cache, wait for it to become unlocked if it
196205
* is a DAX entry and return it. The caller must subsequently call
197206
* put_unlocked_entry() if it did not lock the entry or dax_unlock_entry()
198-
* if it did.
207+
* if it did. The entry returned may have a larger order than @order.
208+
* If @order is larger than the order of the entry found in i_pages, this
209+
* function returns a dax_is_conflict entry.
199210
*
200211
* Must be called with the i_pages lock held.
201212
*/
202-
static void *get_unlocked_entry(struct xa_state *xas)
213+
static void *get_unlocked_entry(struct xa_state *xas, unsigned int order)
203214
{
204215
void *entry;
205216
struct wait_exceptional_entry_queue ewait;
@@ -210,6 +221,8 @@ static void *get_unlocked_entry(struct xa_state *xas)
210221

211222
for (;;) {
212223
entry = xas_find_conflict(xas);
224+
if (dax_entry_order(entry) < order)
225+
return XA_RETRY_ENTRY;
213226
if (!entry || WARN_ON_ONCE(!xa_is_value(entry)) ||
214227
!dax_is_locked(entry))
215228
return entry;
@@ -254,7 +267,7 @@ static void wait_entry_unlocked(struct xa_state *xas, void *entry)
254267
static void put_unlocked_entry(struct xa_state *xas, void *entry)
255268
{
256269
/* If we were the only waiter woken, wake the next one */
257-
if (entry)
270+
if (entry && dax_is_conflict(entry))
258271
dax_wake_entry(xas, entry, false);
259272
}
260273

@@ -461,28 +474,25 @@ void dax_unlock_page(struct page *page, dax_entry_t cookie)
461474
* overlap with xarray value entries.
462475
*/
463476
static void *grab_mapping_entry(struct xa_state *xas,
464-
struct address_space *mapping, unsigned long size_flag)
477+
struct address_space *mapping, unsigned int order)
465478
{
466479
unsigned long index = xas->xa_index;
467480
bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */
468481
void *entry;
469482

470483
retry:
471484
xas_lock_irq(xas);
472-
entry = get_unlocked_entry(xas);
485+
entry = get_unlocked_entry(xas, order);
473486

474487
if (entry) {
488+
if (dax_is_conflict(entry))
489+
goto fallback;
475490
if (!xa_is_value(entry)) {
476491
xas_set_err(xas, EIO);
477492
goto out_unlock;
478493
}
479494

480-
if (size_flag & DAX_PMD) {
481-
if (dax_is_pte_entry(entry)) {
482-
put_unlocked_entry(xas, entry);
483-
goto fallback;
484-
}
485-
} else { /* trying to grab a PTE entry */
495+
if (order == 0) {
486496
if (dax_is_pmd_entry(entry) &&
487497
(dax_is_zero_entry(entry) ||
488498
dax_is_empty_entry(entry))) {
@@ -523,7 +533,11 @@ static void *grab_mapping_entry(struct xa_state *xas,
523533
if (entry) {
524534
dax_lock_entry(xas, entry);
525535
} else {
526-
entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY);
536+
unsigned long flags = DAX_EMPTY;
537+
538+
if (order > 0)
539+
flags |= DAX_PMD;
540+
entry = dax_make_entry(pfn_to_pfn_t(0), flags);
527541
dax_lock_entry(xas, entry);
528542
if (xas_error(xas))
529543
goto out_unlock;
@@ -594,7 +608,7 @@ struct page *dax_layout_busy_page(struct address_space *mapping)
594608
if (WARN_ON_ONCE(!xa_is_value(entry)))
595609
continue;
596610
if (unlikely(dax_is_locked(entry)))
597-
entry = get_unlocked_entry(&xas);
611+
entry = get_unlocked_entry(&xas, 0);
598612
if (entry)
599613
page = dax_busy_page(entry);
600614
put_unlocked_entry(&xas, entry);
@@ -621,7 +635,7 @@ static int __dax_invalidate_entry(struct address_space *mapping,
621635
void *entry;
622636

623637
xas_lock_irq(&xas);
624-
entry = get_unlocked_entry(&xas);
638+
entry = get_unlocked_entry(&xas, 0);
625639
if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
626640
goto out;
627641
if (!trunc &&
@@ -849,7 +863,7 @@ static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
849863
if (unlikely(dax_is_locked(entry))) {
850864
void *old_entry = entry;
851865

852-
entry = get_unlocked_entry(xas);
866+
entry = get_unlocked_entry(xas, 0);
853867

854868
/* Entry got punched out / reallocated? */
855869
if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
@@ -1510,7 +1524,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
15101524
* entry is already in the array, for instance), it will return
15111525
* VM_FAULT_FALLBACK.
15121526
*/
1513-
entry = grab_mapping_entry(&xas, mapping, DAX_PMD);
1527+
entry = grab_mapping_entry(&xas, mapping, PMD_ORDER);
15141528
if (xa_is_internal(entry)) {
15151529
result = xa_to_internal(entry);
15161530
goto fallback;
@@ -1659,11 +1673,10 @@ dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
16591673
vm_fault_t ret;
16601674

16611675
xas_lock_irq(&xas);
1662-
entry = get_unlocked_entry(&xas);
1676+
entry = get_unlocked_entry(&xas, order);
16631677
/* Did we race with someone splitting entry or so? */
1664-
if (!entry ||
1665-
(order == 0 && !dax_is_pte_entry(entry)) ||
1666-
(order == PMD_ORDER && !dax_is_pmd_entry(entry))) {
1678+
if (!entry || dax_is_conflict(entry) ||
1679+
(order == 0 && !dax_is_pte_entry(entry))) {
16671680
put_unlocked_entry(&xas, entry);
16681681
xas_unlock_irq(&xas);
16691682
trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,

0 commit comments

Comments
 (0)