[haiku-commits] haiku: hrev53651 - in src/add-ons/kernel: bus_managers/usb busses/usb

  • From: waddlesplash <waddlesplash@xxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Mon, 23 Dec 2019 15:54:11 -0500 (EST)

hrev53651 adds 3 changesets to branch 'master'
old head: 006add310a0378f1661bdf24ade5a16a5b031762
new head: 21a5c628fb858cc8b8e18a9e7367dccfcb6a1171
overview: 
https://git.haiku-os.org/haiku/log/?qt=range&q=21a5c628fb85+%5E006add310a03

----------------------------------------------------------------------------

a77769a54917: USB: Rework the PhysicalMemoryAllocator to use MutexLocker.
  
  No functional change, but will make some subsequent
  changes easier and more fail-proof.

93904285cfe5: USB: Add overall timeout to the PhysicalMemoryAllocator.
  
  The within-an-attempt timeout did not successfully break up
  deadlocks that occur on a system with a lot of USB transfers
  going, as we may never hit 2 seconds in between wake-ups,
  but the size requested may be un-fulfillable regardless.
  
  So, now we have a 2-second overall timeout. This fixes
  the system freeze in #15569, but now attached USB disk
  drives enter a Stall state, making the system unusable
  anyway.

21a5c628fb85: XHCI: Add buffer count to "unable to allocate" trace, fix freeing.
  
  FreeDescriptor needs to know buffer_size and buffer_count,
  so we have to initialize these for the fail-exit case to work.

                              [ Augustin Cavalier <waddlesplash@xxxxxxxxx> ]

----------------------------------------------------------------------------

3 files changed, 18 insertions(+), 33 deletions(-)
.../bus_managers/usb/PhysicalMemoryAllocator.cpp | 39 +++++++-------------
.../bus_managers/usb/PhysicalMemoryAllocator.h   |  3 --
src/add-ons/kernel/busses/usb/xhci.cpp           |  9 +++--

############################################################################

Commit:      a77769a54917b6b98b124c1668b58a87b522a708
URL:         https://git.haiku-os.org/haiku/commit/?id=a77769a54917
Author:      Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date:        Mon Dec 23 20:29:43 2019 UTC

USB: Rework the PhysicalMemoryAllocator to use MutexLocker.

No functional change, but will make some subsequent
changes easier and more fail-proof.

----------------------------------------------------------------------------

diff --git a/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp 
b/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp
index a1f09e0167..e864080765 100644
--- a/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp
+++ b/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp
@@ -10,6 +10,7 @@
 #include <string.h>
 #include <KernelExport.h>
 #include <SupportDefs.h>
+#include <util/AutoLock.h>
 #include <util/kernel_cpp.h>
 
 #include "PhysicalMemoryAllocator.h"
@@ -97,7 +98,7 @@ PhysicalMemoryAllocator::PhysicalMemoryAllocator(const char 
*name,
 
 PhysicalMemoryAllocator::~PhysicalMemoryAllocator()
 {
-       _Lock();
+       mutex_lock(&fLock);
 
        for (int32 i = 0; i < fArrayCount; i++)
                free(fArray[i]);
@@ -113,20 +114,6 @@ PhysicalMemoryAllocator::~PhysicalMemoryAllocator()
 }
 
 
-bool
-PhysicalMemoryAllocator::_Lock()
-{
-       return (mutex_lock(&fLock) == B_OK);
-}
-
-
-void
-PhysicalMemoryAllocator::_Unlock()
-{
-       mutex_unlock(&fLock);
-}
-
-
 status_t
 PhysicalMemoryAllocator::Allocate(size_t size, void **logicalAddress,
        phys_addr_t *physicalAddress)
@@ -169,7 +156,8 @@ PhysicalMemoryAllocator::Allocate(size_t size, void 
**logicalAddress,
                }
        }
 
-       if (!_Lock())
+       MutexLocker locker(&fLock);
+       if (!locker.IsLocked())
                return B_ERROR;
 
        while (true) {
@@ -203,7 +191,6 @@ PhysicalMemoryAllocator::Allocate(size_t size, void 
**logicalAddress,
                                        arrayIndex >>= 1;
                                }
 
-                               _Unlock();
                                size_t offset = fBlockSize[arrayToUse] * i;
                                *logicalAddress = (void *)((uint8 
*)fLogicalBase + offset);
                                *physicalAddress = (phys_addr_t)(fPhysicalBase 
+ offset);
@@ -217,7 +204,7 @@ PhysicalMemoryAllocator::Allocate(size_t size, void 
**logicalAddress,
                fNoMemoryCondition.Add(&entry);
                fMemoryWaitersCount++;
 
-               _Unlock();
+               locker.Unlock();
 
                TRACE_ERROR(("PMA: found no free slot to store %ld bytes, 
waiting\n",
                        size));
@@ -227,7 +214,7 @@ PhysicalMemoryAllocator::Allocate(size_t size, void 
**logicalAddress,
                        break;
                }
 
-               if (!_Lock())
+               if (!locker.Lock())
                        return B_ERROR;
 
                fMemoryWaitersCount--;
@@ -283,7 +270,8 @@ PhysicalMemoryAllocator::Deallocate(size_t size, void 
*logicalAddress,
                return B_BAD_VALUE;
        }
 
-       if (!_Lock())
+       MutexLocker _(&fLock);
+       if (!_.IsLocked())
                return B_ERROR;
 
        // clear upwards to the smallest block
@@ -308,7 +296,6 @@ PhysicalMemoryAllocator::Deallocate(size_t size, void 
*logicalAddress,
        if (fMemoryWaitersCount > 0)
                fNoMemoryCondition.NotifyAll();
 
-       _Unlock();
        return B_OK;
 }
 
diff --git a/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.h 
b/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.h
index 053dcddcef..9fc27f7330 100644
--- a/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.h
+++ b/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.h
@@ -39,9 +39,6 @@ public:
                void                                            DumpFreeSlots();
 
 private:
-               bool                                            _Lock();
-               void                                            _Unlock();
-
                char                                            *fName;
 
                size_t                                          fOverhead;

############################################################################

Commit:      93904285cfe55a15ba86007df39beeaaab0b45da
URL:         https://git.haiku-os.org/haiku/commit/?id=93904285cfe5
Author:      Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date:        Mon Dec 23 20:31:34 2019 UTC

Ticket:      https://dev.haiku-os.org/ticket/15569

USB: Add overall timeout to the PhysicalMemoryAllocator.

The within-an-attempt timeout did not successfully break up
deadlocks that occur on a system with a lot of USB transfers
going, as we may never hit 2 seconds in between wake-ups,
but the size requested may be un-fulfillable regardless.

So, now we have a 2-second overall timeout. This fixes
the system freeze in #15569, but now attached USB disk
drives enter a Stall state, making the system unusable
anyway.

----------------------------------------------------------------------------

diff --git a/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp 
b/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp
index e864080765..51a652ee7d 100644
--- a/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp
+++ b/src/add-ons/kernel/bus_managers/usb/PhysicalMemoryAllocator.cpp
@@ -160,7 +160,8 @@ PhysicalMemoryAllocator::Allocate(size_t size, void 
**logicalAddress,
        if (!locker.IsLocked())
                return B_ERROR;
 
-       while (true) {
+       const bigtime_t limit = system_time() + 2 * 1000 * 1000;
+       do {
                TRACE(("PMA: will use array %ld (blocksize: %ld) to allocate 
%ld bytes\n", arrayToUse, fBlockSize[arrayToUse], size));
                uint8 *targetArray = fArray[arrayToUse];
                uint32 arrayOffset = fArrayOffset[arrayToUse] % arrayLength;
@@ -209,17 +210,16 @@ PhysicalMemoryAllocator::Allocate(size_t size, void 
**logicalAddress,
                TRACE_ERROR(("PMA: found no free slot to store %ld bytes, 
waiting\n",
                        size));
 
-               if (entry.Wait(B_RELATIVE_TIMEOUT, 2 * 1000 * 1000) == 
B_TIMED_OUT) {
-                       TRACE_ERROR(("PMA: timed out waiting for a free slot, 
giving up\n"));
+               if (entry.Wait(B_RELATIVE_TIMEOUT, 1 * 1000 * 1000) == 
B_TIMED_OUT)
                        break;
-               }
 
                if (!locker.Lock())
                        return B_ERROR;
 
                fMemoryWaitersCount--;
-       }
+       } while (system_time() < limit);
 
+       TRACE_ERROR(("PMA: timed out waiting for a free slot, giving up\n"));
        return B_NO_MEMORY;
 }
 

############################################################################

Revision:    hrev53651
Commit:      21a5c628fb858cc8b8e18a9e7367dccfcb6a1171
URL:         https://git.haiku-os.org/haiku/commit/?id=21a5c628fb85
Author:      Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date:        Mon Dec 23 20:32:26 2019 UTC

XHCI: Add buffer count to "unable to allocate" trace, fix freeing.

FreeDescriptor needs to know buffer_size and buffer_count,
so we have to initialize these for the fail-exit case to work.

----------------------------------------------------------------------------

diff --git a/src/add-ons/kernel/busses/usb/xhci.cpp 
b/src/add-ons/kernel/busses/usb/xhci.cpp
index 831b1feab3..d42683d2b6 100644
--- a/src/add-ons/kernel/busses/usb/xhci.cpp
+++ b/src/add-ons/kernel/busses/usb/xhci.cpp
@@ -1176,6 +1176,8 @@ XHCI::CreateDescriptor(uint32 trbCount, uint32 
bufferCount, size_t bufferSize)
                        return NULL;
                }
                result->buffer_addrs = 
(phys_addr_t*)&result->buffers[bufferCount];
+               result->buffer_size = bufferSize;
+               result->buffer_count = bufferCount;
 
                // Optimization: If the requested total size of all buffers is 
less
                // than 32*B_PAGE_SIZE (the maximum size that the physical 
memory
@@ -1200,8 +1202,9 @@ XHCI::CreateDescriptor(uint32 trbCount, uint32 
bufferCount, size_t bufferSize)
                        for (uint32 i = 0; i < bufferCount; i++) {
                                if (fStack->AllocateChunk(&result->buffers[i],
                                                &result->buffer_addrs[i], 
bufferSize) < B_OK) {
-                                       TRACE_ERROR("unable to allocate space 
for the buffer (size %ld)\n",
-                                               bufferSize);
+                                       TRACE_ERROR("unable to allocate space 
for a buffer (size "
+                                               "%" B_PRIuSIZE ", count %" 
B_PRIu32 ")\n",
+                                               bufferSize, bufferCount);
                                        FreeDescriptor(result);
                                        return NULL;
                                }
@@ -1211,8 +1214,6 @@ XHCI::CreateDescriptor(uint32 trbCount, uint32 
bufferCount, size_t bufferSize)
                result->buffers = NULL;
                result->buffer_addrs = NULL;
        }
-       result->buffer_size = bufferSize;
-       result->buffer_count = bufferCount;
 
        // Initialize all other fields.
        result->transfer = NULL;


Other related posts:

  • » [haiku-commits] haiku: hrev53651 - in src/add-ons/kernel: bus_managers/usb busses/usb - waddlesplash