2009-11-18 03:31:40 +01:00
|
|
|
/* QQ: TODO multi-pinbox */
|
2011-06-30 17:46:53 +02:00
|
|
|
/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2019-05-11 20:29:06 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
wait-free concurrent allocator based on pinning addresses
|
|
|
|
|
|
|
|
It works as follows: every thread (strictly speaking - every CPU, but
|
|
|
|
it's too difficult to do) has a small array of pointers. They're called
|
|
|
|
"pins". Before using an object its address must be stored in this array
|
|
|
|
(pinned). When an object is no longer necessary its address must be
|
|
|
|
removed from this array (unpinned). When a thread wants to free() an
|
|
|
|
object it scans all pins of all threads to see if somebody has this
|
|
|
|
object pinned. If yes - the object is not freed (but stored in a
|
|
|
|
"purgatory"). To reduce the cost of a single free() pins are not scanned
|
|
|
|
on every free() but only added to (thread-local) purgatory. On every
|
|
|
|
LF_PURGATORY_SIZE free() purgatory is scanned and all unpinned objects
|
|
|
|
are freed.
|
|
|
|
|
|
|
|
Pins are used to solve ABA problem. To use pins one must obey
|
|
|
|
a pinning protocol:
|
|
|
|
|
|
|
|
1. Let's assume that PTR is a shared pointer to an object. Shared means
|
|
|
|
that any thread may modify it anytime to point to a different object
|
|
|
|
and free the old object. Later the freed object may be potentially
|
|
|
|
allocated by another thread. If we're unlucky that other thread may
|
|
|
|
set PTR to point to this object again. This is ABA problem.
|
|
|
|
2. Create a local pointer LOCAL_PTR.
|
|
|
|
3. Pin the PTR in a loop:
|
|
|
|
do
|
|
|
|
{
|
|
|
|
LOCAL_PTR= PTR;
|
|
|
|
pin(PTR, PIN_NUMBER);
|
|
|
|
} while (LOCAL_PTR != PTR)
|
|
|
|
4. It is guaranteed that after the loop has ended, LOCAL_PTR
|
|
|
|
points to an object (or NULL, if PTR may be NULL), that
|
|
|
|
will never be freed. It is not guaranteed though
|
|
|
|
that LOCAL_PTR == PTR (as PTR can change any time)
|
|
|
|
5. When done working with the object, remove the pin:
|
|
|
|
unpin(PIN_NUMBER)
|
|
|
|
6. When copying pins (as in the list traversing loop:
|
|
|
|
pin(CUR, 1);
|
|
|
|
while ()
|
|
|
|
{
|
|
|
|
do // standard
|
|
|
|
{ // pinning
|
|
|
|
NEXT=CUR->next; // loop
|
|
|
|
pin(NEXT, 0); // see #3
|
|
|
|
} while (NEXT != CUR->next); // above
|
|
|
|
...
|
|
|
|
...
|
|
|
|
CUR=NEXT;
|
|
|
|
pin(CUR, 1); // copy pin[0] to pin[1]
|
|
|
|
}
|
|
|
|
which keeps CUR address constantly pinned), note than pins may be
|
|
|
|
copied only upwards (!!!), that is pin[N] to pin[M], M > N.
|
|
|
|
7. Don't keep the object pinned longer than necessary - the number of
|
|
|
|
pins you have is limited (and small), keeping an object pinned
|
|
|
|
prevents its reuse and cause unnecessary mallocs.
|
|
|
|
|
|
|
|
Explanations:
|
|
|
|
|
|
|
|
3. The loop is important. The following can occur:
|
|
|
|
thread1> LOCAL_PTR= PTR
|
|
|
|
thread2> free(PTR); PTR=0;
|
|
|
|
thread1> pin(PTR, PIN_NUMBER);
|
|
|
|
now thread1 cannot access LOCAL_PTR, even if it's pinned,
|
|
|
|
because it points to a freed memory. That is, it *must*
|
|
|
|
verify that it has indeed pinned PTR, the shared pointer.
|
|
|
|
|
|
|
|
6. When a thread wants to free some LOCAL_PTR, and it scans
|
|
|
|
all lists of pins to see whether it's pinned, it does it
|
|
|
|
upwards, from low pin numbers to high. Thus another thread
|
|
|
|
must copy an address from one pin to another in the same
|
|
|
|
direction - upwards, otherwise the scanning thread may
|
|
|
|
miss it.
|
|
|
|
|
|
|
|
Implementation details:
|
|
|
|
|
|
|
|
Pins are given away from a "pinbox". Pinbox is stack-based allocator.
|
|
|
|
It used dynarray for storing pins, new elements are allocated by dynarray
|
|
|
|
as necessary, old are pushed in the stack for reuse. ABA is solved by
|
|
|
|
versioning a pointer - because we use an array, a pointer to pins is 16 bit,
|
|
|
|
upper 16 bits are used for a version.
|
|
|
|
|
|
|
|
It is assumed that pins belong to a THD and are not transferable
|
|
|
|
between THD's (LF_PINS::stack_ends_here being a primary reason
|
|
|
|
for this limitation).
|
|
|
|
*/
|
2020-01-29 13:50:26 +01:00
|
|
|
#include "mysys_priv.h"
|
2009-11-18 03:31:40 +01:00
|
|
|
#include <lf.h>
|
2020-04-15 19:23:12 +02:00
|
|
|
#include "my_cpu.h"
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
#define LF_PINBOX_MAX_PINS 65536
|
|
|
|
|
2015-01-12 17:03:45 +01:00
|
|
|
static void lf_pinbox_real_free(LF_PINS *pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Initialize a pinbox. Normally called from lf_alloc_init.
|
|
|
|
See the latter for details.
|
|
|
|
*/
|
|
|
|
void lf_pinbox_init(LF_PINBOX *pinbox, uint free_ptr_offset,
|
|
|
|
lf_pinbox_free_func *free_func, void *free_func_arg)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(free_ptr_offset % sizeof(void *) == 0);
|
|
|
|
lf_dynarray_init(&pinbox->pinarray, sizeof(LF_PINS));
|
|
|
|
pinbox->pinstack_top_ver= 0;
|
|
|
|
pinbox->pins_in_array= 0;
|
|
|
|
pinbox->free_ptr_offset= free_ptr_offset;
|
|
|
|
pinbox->free_func= free_func;
|
|
|
|
pinbox->free_func_arg= free_func_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lf_pinbox_destroy(LF_PINBOX *pinbox)
|
|
|
|
{
|
|
|
|
lf_dynarray_destroy(&pinbox->pinarray);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get pins from a pinbox. Usually called via lf_alloc_get_pins() or
|
|
|
|
lf_hash_get_pins().
|
|
|
|
|
2017-02-26 16:49:47 +01:00
|
|
|
SYNOPSIS
|
2009-11-18 03:31:40 +01:00
|
|
|
pinbox -
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
get a new LF_PINS structure from a stack of unused pins,
|
|
|
|
or allocate a new one out of dynarray.
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
It is assumed that pins belong to a thread and are not transferable
|
|
|
|
between threads.
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
LF_PINS *lf_pinbox_get_pins(LF_PINBOX *pinbox)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
uint32 pins, next, top_ver;
|
|
|
|
LF_PINS *el;
|
|
|
|
/*
|
|
|
|
We have an array of max. 64k elements.
|
|
|
|
The highest index currently allocated is pinbox->pins_in_array.
|
|
|
|
Freed elements are in a lifo stack, pinstack_top_ver.
|
|
|
|
pinstack_top_ver is 32 bits; 16 low bits are the index in the
|
|
|
|
array, to the first element of the list. 16 high bits are a version
|
|
|
|
(every time the 16 low bits are updated, the 16 high bits are
|
2017-12-13 07:30:08 +01:00
|
|
|
incremented). Versioning prevents the ABA problem.
|
2009-11-18 03:31:40 +01:00
|
|
|
*/
|
|
|
|
top_ver= pinbox->pinstack_top_ver;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(pins= top_ver % LF_PINBOX_MAX_PINS))
|
|
|
|
{
|
|
|
|
/* the stack of free elements is empty */
|
|
|
|
pins= my_atomic_add32((int32 volatile*) &pinbox->pins_in_array, 1)+1;
|
|
|
|
if (unlikely(pins >= LF_PINBOX_MAX_PINS))
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
note that the first allocated element has index 1 (pins==1).
|
|
|
|
index 0 is reserved to mean "NULL pointer"
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
el= (LF_PINS *)lf_dynarray_lvalue(&pinbox->pinarray, pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
if (unlikely(!el))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-12 17:03:45 +01:00
|
|
|
el= (LF_PINS *)lf_dynarray_value(&pinbox->pinarray, pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
next= el->link;
|
|
|
|
} while (!my_atomic_cas32((int32 volatile*) &pinbox->pinstack_top_ver,
|
|
|
|
(int32*) &top_ver,
|
|
|
|
top_ver-pins+next+LF_PINBOX_MAX_PINS));
|
|
|
|
/*
|
|
|
|
set el->link to the index of el in the dynarray (el->link has two usages:
|
|
|
|
- if element is allocated, it's its own index
|
|
|
|
- if element is free, it's its next element in the free stack
|
|
|
|
*/
|
|
|
|
el->link= pins;
|
|
|
|
el->purgatory_count= 0;
|
|
|
|
el->pinbox= pinbox;
|
2019-11-25 22:48:50 +01:00
|
|
|
|
2009-11-18 03:31:40 +01:00
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Put pins back to a pinbox. Usually called via lf_alloc_put_pins() or
|
|
|
|
lf_hash_put_pins().
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
empty the purgatory (XXX deadlock warning below!),
|
|
|
|
push LF_PINS structure to a stack
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
void lf_pinbox_put_pins(LF_PINS *pins)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
LF_PINBOX *pinbox= pins->pinbox;
|
|
|
|
uint32 top_ver, nr;
|
|
|
|
nr= pins->link;
|
2012-03-28 17:54:30 +02:00
|
|
|
|
|
|
|
#ifndef DBUG_OFF
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
2012-03-28 17:54:30 +02:00
|
|
|
/* This thread should not hold any pin. */
|
2009-11-18 03:31:40 +01:00
|
|
|
int i;
|
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
|
|
|
DBUG_ASSERT(pins->pin[i] == 0);
|
|
|
|
}
|
2012-03-28 17:54:30 +02:00
|
|
|
#endif /* DBUG_OFF */
|
|
|
|
|
2009-11-18 03:31:40 +01:00
|
|
|
/*
|
|
|
|
XXX this will deadlock if other threads will wait for
|
2015-01-12 17:03:45 +01:00
|
|
|
the caller to do something after lf_pinbox_put_pins(),
|
2009-11-18 03:31:40 +01:00
|
|
|
and they would have pinned addresses that the caller wants to free.
|
|
|
|
Thus: only free pins when all work is done and nobody can wait for you!!!
|
|
|
|
*/
|
|
|
|
while (pins->purgatory_count)
|
|
|
|
{
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_pinbox_real_free(pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
if (pins->purgatory_count)
|
|
|
|
pthread_yield();
|
|
|
|
}
|
|
|
|
top_ver= pinbox->pinstack_top_ver;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
pins->link= top_ver % LF_PINBOX_MAX_PINS;
|
|
|
|
} while (!my_atomic_cas32((int32 volatile*) &pinbox->pinstack_top_ver,
|
|
|
|
(int32*) &top_ver,
|
|
|
|
top_ver-pins->link+nr+LF_PINBOX_MAX_PINS));
|
|
|
|
}
|
|
|
|
|
2023-03-11 01:27:42 +01:00
|
|
|
/*
|
|
|
|
Get the next pointer in the purgatory list.
|
|
|
|
Note that next_node is not used to avoid the extra volatile.
|
|
|
|
*/
|
|
|
|
#define pnext_node(P, X) (*((void **)(((char *)(X)) + (P)->free_ptr_offset)))
|
|
|
|
|
|
|
|
static inline void add_to_purgatory(LF_PINS *pins, void *addr)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
2023-03-11 01:27:42 +01:00
|
|
|
pnext_node(pins->pinbox, addr)= pins->purgatory;
|
|
|
|
pins->purgatory= addr;
|
|
|
|
pins->purgatory_count++;
|
2009-11-18 03:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Free an object allocated via pinbox allocator
|
|
|
|
|
|
|
|
DESCRIPTION
|
2015-01-12 17:03:45 +01:00
|
|
|
add an object to purgatory. if necessary, calllf_pinbox_real_free()
|
2009-11-18 03:31:40 +01:00
|
|
|
to actually free something.
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
void lf_pinbox_free(LF_PINS *pins, void *addr)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
add_to_purgatory(pins, addr);
|
2012-09-04 12:12:28 +02:00
|
|
|
if (pins->purgatory_count % LF_PURGATORY_SIZE == 0)
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_pinbox_real_free(pins);
|
MDEV-31185 rw_trx_hash_t::find() unpins pins too early
rw_trx_hash_t::find() acquires element->mutex, then unpins pins, used for
lf_hash element search. After that the "element" can be deallocated and
reused by some other thread.
If we take a look rw_trx_hash_t::insert()->lf_hash_insert()->lf_alloc_new()
calls, we will not find any element->mutex acquisition, as it was not
initialized yet before it's allocation. rw_trx_hash_t::insert() can reuse
the chunk, unpinned in rw_trx_hash_t::find().
The scenario is the following:
1. Thread 1 have just executed lf_hash_search() in
rw_trx_hash_t::find(), but have not acquired element->mutex yet.
2. Thread 2 have removed the element from hash table with
rw_trx_hash_t::erase() call.
3. Thread 1 acquired element->mutex and unpinned pin 2 pin with
lf_hash_search_unpin(pins) call.
4. Some thread purged memory of the element.
5. Thread 3 reused the memory for the element, filled element->id,
element->trx.
6. Thread 1 crashes with failed "DBUG_ASSERT(trx_id == trx->id)"
assertion.
Note that trx_t objects are also reused, see the code around trx_pools
for details.
The fix is to invoke "lf_hash_search_unpin(pins);" after element->trx is
stored in local variable in rw_trx_hash_t::find().
Reviewed by: Nikita Malyavin, Marko Mäkelä.
2023-05-12 11:11:53 +02:00
|
|
|
DBUG_EXECUTE_IF("unconditional_pinbox_free",
|
|
|
|
if (pins->purgatory_count % LF_PURGATORY_SIZE)
|
|
|
|
lf_pinbox_real_free(pins););
|
2009-11-18 03:31:40 +01:00
|
|
|
}
|
|
|
|
|
2023-03-11 01:27:42 +01:00
|
|
|
struct st_match_and_save_arg {
|
|
|
|
LF_PINS *pins;
|
|
|
|
LF_PINBOX *pinbox;
|
|
|
|
void *old_purgatory;
|
2009-11-18 03:31:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2023-03-11 01:27:42 +01:00
|
|
|
Callback for lf_dynarray_iterate:
|
|
|
|
Scan all pins of all threads, for each active (non-null) pin,
|
|
|
|
scan the current thread's purgatory. If present there, move it
|
|
|
|
to a new purgatory. At the end, the old purgatory will contain
|
|
|
|
pointers not pinned by any thread.
|
2009-11-18 03:31:40 +01:00
|
|
|
*/
|
2023-03-11 01:27:42 +01:00
|
|
|
static int match_and_save(void *e, void *a)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
2024-06-10 11:35:33 +02:00
|
|
|
LF_PINS *el= e;
|
2023-03-11 01:27:42 +01:00
|
|
|
struct st_match_and_save_arg *arg= a;
|
|
|
|
|
2009-11-18 03:31:40 +01:00
|
|
|
int i;
|
2023-03-11 01:27:42 +01:00
|
|
|
LF_PINS *el_end= el + LF_DYNARRAY_LEVEL_LENGTH;
|
2009-11-18 03:31:40 +01:00
|
|
|
for (; el < el_end; el++)
|
|
|
|
{
|
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
|
|
|
{
|
MDEV-28430: Fix memory barrier missing of lf_alloc on Arm64
When testing MariaDB on Arm64, a stall issue will occur, jira link:
https://jira.mariadb.org/browse/MDEV-28430.
The stall occurs because of an unexpected circular reference in the
LF_PINS->purgatory list which is traversed in lf_pinbox_real_free().
We found that on Arm64, ABA problem in LF_ALLOCATOR->top list was not
solved, and various undefined problems will occur, including circular
reference in LF_PINS->purgatory list.
The following codes are used to solve ABA problem, code copied
from below link.
https://github.com/MariaDB/server/blob/cb4c2713553c5f522d2a4ebf186c6505384c748d/mysys/lf_alloc-pin.c#L501-#L505
do
{
503 node= allocator->top;
504 lf_pin(pins, 0, node);
505 } while (node != allocator->top && LF_BACKOFF());
1. ABA problem on Arm64
Combine the below steps to analyze how ABA problem occur on Arm64, the
relevant codes in steps are simplified, code line numbers below are in
MariaDB v10.4.
------------------------------------------------------------------------
Abnormal case.
Initial state: pin = 0, top = A, top list: A->B
T1 T2
step1. write top=B //seq-cst, #L517
step2. write A->next= "any"
step3. read pin==0 //relaxed, #L295
step1. write pin=A //seq-cst, #L504
step2. read old value of top==A //relaxed, #L505
step3. next=A->next="any" //#L517
step4. write A->next=B,top=A //#L420-435
step4. CAS(top,A,next) //#L517
step5. write pin=0 //#L521
------------------------------------------------------------------------
Above case is due to T1.step2 reading the old value of top, causing
"T1.step3, T1.step4" and "T2.step4" to occur at the same time, in other
words, they are not mutually exclusive.
It may happen that T2.step4 is sandwiched between T1.step3 and T1.step4,
which cause top to be updated to "any", which may be in-use or invalid
address.
2. Analyze above issue with Dekker's algorithm
Above problem can be mapped to Dekker's algorithm, link is as below
https://en.wikipedia.org/wiki/Dekker%27s_algorithm.
The following extracts the read and write operations on 'top' and 'pin',
and maps them to Dekker's algorithm to analyze the root cause.
------------------------------------------------------------------------
Initial state: top = A, pin = 0
T1 T2
store_seq_cst(pin, A) // write pin store_seq_cst(top, B) //write top
rt= load_relaxed(top) // read top rp= load_relaxed(pin) //read pin
if (rt == A && rp == 0) printf("oops\n"); // will "oops" be printed?
------------------------------------------------------------------------
How T1 and T2 enter their critical section:
(1) T1, write pin, if T1 reads that top has not been updated, T1 enter
its critical section(T1.step3 and T1.step4, try to obtain 'A', #L517),
otherwise just give up (T1 without priority).
(2) T2, write top, if T2 reads that pin has not been updated, T2 enter
critical section(T2.step4, try to add 'A' to top list again, #L420-435),
otherwise wait until pin!=A (T2 with priority).
In the previous code, due to load 'top' and 'pin' with relaxed semantic,
on arm and ppc, there is no guarantee that the above critical sections
are mutually exclusive, in other words, "oops" will be printed.
This bug only happens on arm and ppc, not x86. On current x86
implementation, load is always seq-cst (relaxed and seq-cst load
generates same machine code), as shown in https://godbolt.org/z/sEzMvnjd9
3. Fix method
Add sequential-consistency semantic to read 'top' in #L505(T1.step2),
Add sequential-consistency semantic to read "el->pin[i]" in #L295
and #L320.
4. Issue reproduce
Add "delay" after #L503 in lf_alloc-pin.c, When run unit.lf, can quickly
get segment fault because "top" point to an invalid address. For detail,
see comment area of below link.
https://jira.mariadb.org/browse/MDEV-28430.
5. Futher improvement
To make this code more robust and safe on all platforms, we recommend
replacing volatile with C11 atomics and to fix all data races. This will
also make the code easier to reason.
Signed-off-by: Xiaotong Niu <xiaotong.niu@arm.com>
2023-10-27 06:44:57 +02:00
|
|
|
void *p= my_atomic_loadptr((void **)&el->pin[i]);
|
2009-11-18 03:31:40 +01:00
|
|
|
if (p)
|
2023-03-11 01:27:42 +01:00
|
|
|
{
|
|
|
|
void *cur= arg->old_purgatory;
|
|
|
|
void **list_prev= &arg->old_purgatory;
|
|
|
|
while (cur)
|
|
|
|
{
|
|
|
|
void *next= pnext_node(arg->pinbox, cur);
|
|
|
|
|
|
|
|
if (p == cur)
|
|
|
|
{
|
|
|
|
/* pinned - keeping */
|
|
|
|
add_to_purgatory(arg->pins, cur);
|
|
|
|
/* unlink from old purgatory */
|
|
|
|
*list_prev= next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
list_prev= (void **)((char *)cur+arg->pinbox->free_ptr_offset);
|
|
|
|
cur= next;
|
|
|
|
}
|
|
|
|
if (!arg->old_purgatory)
|
|
|
|
return 1;
|
|
|
|
}
|
2009-11-18 03:31:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Scan the purgatory and free everything that can be freed
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
static void lf_pinbox_real_free(LF_PINS *pins)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
LF_PINBOX *pinbox= pins->pinbox;
|
|
|
|
|
2023-03-11 01:27:42 +01:00
|
|
|
/* Store info about current purgatory. */
|
|
|
|
struct st_match_and_save_arg arg = {pins, pinbox, pins->purgatory};
|
|
|
|
/* Reset purgatory. */
|
|
|
|
pins->purgatory= NULL;
|
|
|
|
pins->purgatory_count= 0;
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
|
2023-03-11 01:27:42 +01:00
|
|
|
lf_dynarray_iterate(&pinbox->pinarray, match_and_save, &arg);
|
|
|
|
|
|
|
|
if (arg.old_purgatory)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
2023-03-11 01:27:42 +01:00
|
|
|
/* Some objects in the old purgatory were not pinned, free them. */
|
|
|
|
void *last= arg.old_purgatory;
|
|
|
|
while (pnext_node(pinbox, last))
|
|
|
|
last= pnext_node(pinbox, last);
|
|
|
|
pinbox->free_func(arg.old_purgatory, last, pinbox->free_func_arg);
|
2009-11-18 03:31:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-11 01:27:42 +01:00
|
|
|
#define next_node(P, X) (*((uchar * volatile *)(((uchar *)(X)) + (P)->free_ptr_offset)))
|
|
|
|
#define anext_node(X) next_node(&allocator->pinbox, (X))
|
|
|
|
|
2009-11-18 03:31:40 +01:00
|
|
|
/* lock-free memory allocator for fixed-size objects */
|
|
|
|
|
|
|
|
/*
|
2015-01-12 17:03:45 +01:00
|
|
|
callback forlf_pinbox_real_free to free a list of unpinned objects -
|
2009-11-18 03:31:40 +01:00
|
|
|
add it back to the allocator stack
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
'first' and 'last' are the ends of the linked list of nodes:
|
|
|
|
first->el->el->....->el->last. Use first==last to free only one element.
|
|
|
|
*/
|
2024-06-10 11:35:33 +02:00
|
|
|
static void alloc_free(void *f, void *l, void *alloc)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
2024-06-10 11:35:33 +02:00
|
|
|
uchar *first= f;
|
|
|
|
uchar volatile *last= l;
|
|
|
|
LF_ALLOCATOR *allocator= alloc;
|
2009-11-18 03:31:40 +01:00
|
|
|
/*
|
|
|
|
we need a union here to access type-punned pointer reliably.
|
|
|
|
otherwise gcc -fstrict-aliasing will not see 'tmp' changed in the loop
|
|
|
|
*/
|
|
|
|
union { uchar * node; void *ptr; } tmp;
|
|
|
|
tmp.node= allocator->top;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
anext_node(last)= tmp.node;
|
|
|
|
} while (!my_atomic_casptr((void **)(char *)&allocator->top,
|
2017-12-07 14:03:59 +01:00
|
|
|
(void **)&tmp.ptr, first) && LF_BACKOFF());
|
2009-11-18 03:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialize lock-free allocator
|
|
|
|
|
2017-02-26 16:49:47 +01:00
|
|
|
SYNOPSIS
|
2009-11-18 03:31:40 +01:00
|
|
|
allocator -
|
|
|
|
size a size of an object to allocate
|
|
|
|
free_ptr_offset an offset inside the object to a sizeof(void *)
|
|
|
|
memory that is guaranteed to be unused after
|
|
|
|
the object is put in the purgatory. Unused by ANY
|
|
|
|
thread, not only the purgatory owner.
|
|
|
|
This memory will be used to link waiting-to-be-freed
|
|
|
|
objects in a purgatory list.
|
|
|
|
*/
|
|
|
|
void lf_alloc_init(LF_ALLOCATOR *allocator, uint size, uint free_ptr_offset)
|
|
|
|
{
|
2024-06-10 11:35:33 +02:00
|
|
|
lf_pinbox_init(&allocator->pinbox, free_ptr_offset, alloc_free, allocator);
|
2009-11-18 03:31:40 +01:00
|
|
|
allocator->top= 0;
|
|
|
|
allocator->mallocs= 0;
|
|
|
|
allocator->element_size= size;
|
2008-07-29 16:10:24 +02:00
|
|
|
allocator->constructor= 0;
|
|
|
|
allocator->destructor= 0;
|
2009-11-18 03:31:40 +01:00
|
|
|
DBUG_ASSERT(size >= sizeof(void*) + free_ptr_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
destroy the allocator, free everything that's in it
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
As every other init/destroy function here and elsewhere it
|
|
|
|
is not thread safe. No, this function is no different, ensure
|
|
|
|
that no thread needs the allocator before destroying it.
|
|
|
|
We are not responsible for any damage that may be caused by
|
|
|
|
accessing the allocator when it is being or has been destroyed.
|
|
|
|
Oh yes, and don't put your cat in a microwave.
|
|
|
|
*/
|
|
|
|
void lf_alloc_destroy(LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
uchar *node= allocator->top;
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
uchar *tmp= anext_node(node);
|
2008-07-29 16:10:24 +02:00
|
|
|
if (allocator->destructor)
|
|
|
|
allocator->destructor(node);
|
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled
Essentially, the problem is that safemalloc is excruciatingly
slow as it checks all allocated blocks for overrun at each
memory management primitive, yielding a almost exponential
slowdown for the memory management functions (malloc, realloc,
free). The overrun check basically consists of verifying some
bytes of a block for certain magic keys, which catches some
simple forms of overrun. Another minor problem is violation
of aliasing rules and that its own internal list of blocks
is prone to corruption.
Another issue with safemalloc is rather the maintenance cost
as the tool has a significant impact on the server code.
Given the magnitude of memory debuggers available nowadays,
especially those that are provided with the platform malloc
implementation, maintenance of a in-house and largely obsolete
memory debugger becomes a burden that is not worth the effort
due to its slowness and lack of support for detecting more
common forms of heap corruption.
Since there are third-party tools that can provide the same
functionality at a lower or comparable performance cost, the
solution is to simply remove safemalloc. Third-party tools
can provide the same functionality at a lower or comparable
performance cost.
The removal of safemalloc also allows a simplification of the
malloc wrappers, removing quite a bit of kludge: redefinition
of my_malloc, my_free and the removal of the unused second
argument of my_free. Since free() always check whether the
supplied pointer is null, redudant checks are also removed.
Also, this patch adds unit testing for my_malloc and moves
my_realloc implementation into the same file as the other
memory allocation primitives.
client/mysqldump.c:
Pass my_free directly as its signature is compatible with the
callback type -- which wasn't the case for free_table_ent.
2010-07-08 23:20:08 +02:00
|
|
|
my_free(node);
|
2009-11-18 03:31:40 +01:00
|
|
|
node= tmp;
|
|
|
|
}
|
|
|
|
lf_pinbox_destroy(&allocator->pinbox);
|
|
|
|
allocator->top= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Allocate and return an new object.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Pop an unused object from the stack or malloc it is the stack is empty.
|
|
|
|
pin[0] is used, it's removed on return.
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
void *lf_alloc_new(LF_PINS *pins)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
LF_ALLOCATOR *allocator= (LF_ALLOCATOR *)(pins->pinbox->free_func_arg);
|
|
|
|
uchar *node;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
node= allocator->top;
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_pin(pins, 0, node);
|
MDEV-28430: Fix memory barrier missing of lf_alloc on Arm64
When testing MariaDB on Arm64, a stall issue will occur, jira link:
https://jira.mariadb.org/browse/MDEV-28430.
The stall occurs because of an unexpected circular reference in the
LF_PINS->purgatory list which is traversed in lf_pinbox_real_free().
We found that on Arm64, ABA problem in LF_ALLOCATOR->top list was not
solved, and various undefined problems will occur, including circular
reference in LF_PINS->purgatory list.
The following codes are used to solve ABA problem, code copied
from below link.
https://github.com/MariaDB/server/blob/cb4c2713553c5f522d2a4ebf186c6505384c748d/mysys/lf_alloc-pin.c#L501-#L505
do
{
503 node= allocator->top;
504 lf_pin(pins, 0, node);
505 } while (node != allocator->top && LF_BACKOFF());
1. ABA problem on Arm64
Combine the below steps to analyze how ABA problem occur on Arm64, the
relevant codes in steps are simplified, code line numbers below are in
MariaDB v10.4.
------------------------------------------------------------------------
Abnormal case.
Initial state: pin = 0, top = A, top list: A->B
T1 T2
step1. write top=B //seq-cst, #L517
step2. write A->next= "any"
step3. read pin==0 //relaxed, #L295
step1. write pin=A //seq-cst, #L504
step2. read old value of top==A //relaxed, #L505
step3. next=A->next="any" //#L517
step4. write A->next=B,top=A //#L420-435
step4. CAS(top,A,next) //#L517
step5. write pin=0 //#L521
------------------------------------------------------------------------
Above case is due to T1.step2 reading the old value of top, causing
"T1.step3, T1.step4" and "T2.step4" to occur at the same time, in other
words, they are not mutually exclusive.
It may happen that T2.step4 is sandwiched between T1.step3 and T1.step4,
which cause top to be updated to "any", which may be in-use or invalid
address.
2. Analyze above issue with Dekker's algorithm
Above problem can be mapped to Dekker's algorithm, link is as below
https://en.wikipedia.org/wiki/Dekker%27s_algorithm.
The following extracts the read and write operations on 'top' and 'pin',
and maps them to Dekker's algorithm to analyze the root cause.
------------------------------------------------------------------------
Initial state: top = A, pin = 0
T1 T2
store_seq_cst(pin, A) // write pin store_seq_cst(top, B) //write top
rt= load_relaxed(top) // read top rp= load_relaxed(pin) //read pin
if (rt == A && rp == 0) printf("oops\n"); // will "oops" be printed?
------------------------------------------------------------------------
How T1 and T2 enter their critical section:
(1) T1, write pin, if T1 reads that top has not been updated, T1 enter
its critical section(T1.step3 and T1.step4, try to obtain 'A', #L517),
otherwise just give up (T1 without priority).
(2) T2, write top, if T2 reads that pin has not been updated, T2 enter
critical section(T2.step4, try to add 'A' to top list again, #L420-435),
otherwise wait until pin!=A (T2 with priority).
In the previous code, due to load 'top' and 'pin' with relaxed semantic,
on arm and ppc, there is no guarantee that the above critical sections
are mutually exclusive, in other words, "oops" will be printed.
This bug only happens on arm and ppc, not x86. On current x86
implementation, load is always seq-cst (relaxed and seq-cst load
generates same machine code), as shown in https://godbolt.org/z/sEzMvnjd9
3. Fix method
Add sequential-consistency semantic to read 'top' in #L505(T1.step2),
Add sequential-consistency semantic to read "el->pin[i]" in #L295
and #L320.
4. Issue reproduce
Add "delay" after #L503 in lf_alloc-pin.c, When run unit.lf, can quickly
get segment fault because "top" point to an invalid address. For detail,
see comment area of below link.
https://jira.mariadb.org/browse/MDEV-28430.
5. Futher improvement
To make this code more robust and safe on all platforms, we recommend
replacing volatile with C11 atomics and to fix all data races. This will
also make the code easier to reason.
Signed-off-by: Xiaotong Niu <xiaotong.niu@arm.com>
2023-10-27 06:44:57 +02:00
|
|
|
} while (node != my_atomic_loadptr((void **)(char *)&allocator->top)
|
|
|
|
&& LF_BACKOFF());
|
2009-11-18 03:31:40 +01:00
|
|
|
if (!node)
|
|
|
|
{
|
2020-01-29 13:50:26 +01:00
|
|
|
node= (void *)my_malloc(key_memory_lf_node, allocator->element_size,
|
|
|
|
MYF(MY_WME));
|
2008-07-29 16:10:24 +02:00
|
|
|
if (allocator->constructor)
|
|
|
|
allocator->constructor(node);
|
2009-11-18 03:31:40 +01:00
|
|
|
#ifdef MY_LF_EXTRA_DEBUG
|
|
|
|
if (likely(node != 0))
|
|
|
|
my_atomic_add32(&allocator->mallocs, 1);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (my_atomic_casptr((void **)(char *)&allocator->top,
|
|
|
|
(void *)&node, anext_node(node)))
|
|
|
|
break;
|
|
|
|
}
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_unpin(pins, 0);
|
2009-11-18 03:31:40 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
count the number of objects in a pool.
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
This is NOT thread-safe !!!
|
|
|
|
*/
|
|
|
|
uint lf_alloc_pool_count(LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
uchar *node;
|
|
|
|
for (node= allocator->top, i= 0; node; node= anext_node(node), i++)
|
|
|
|
/* no op */;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|