Merge pull request #9083 from liamwhite/take-a-chance-on-me

kernel: fix slab heap ABA
This commit is contained in:
liamwhite 2022-10-19 16:27:59 -04:00 committed by GitHub
commit 925fb63478
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,6 +8,7 @@
#include "common/assert.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/spin_lock.h"
namespace Kernel {
@ -36,28 +37,34 @@ public:
}
void* Allocate() {
Node* ret = m_head.load();
// KScopedInterruptDisable di;
do {
if (ret == nullptr) {
break;
m_lock.lock();
Node* ret = m_head;
if (ret != nullptr) [[likely]] {
m_head = ret->next;
}
} while (!m_head.compare_exchange_weak(ret, ret->next));
m_lock.unlock();
return ret;
}
void Free(void* obj) {
Node* node = static_cast<Node*>(obj);
// KScopedInterruptDisable di;
Node* cur_head = m_head.load();
do {
node->next = cur_head;
} while (!m_head.compare_exchange_weak(cur_head, node));
m_lock.lock();
Node* node = static_cast<Node*>(obj);
node->next = m_head;
m_head = node;
m_lock.unlock();
}
private:
std::atomic<Node*> m_head{};
Common::SpinLock m_lock;
};
} // namespace impl