Skip to content

Commit 0fa5334

Browse files
Carlos Llamasgregkh
authored andcommitted
binder: add lockless binder_alloc_(set|get)_vma()
Bring back the original lockless design in binder_alloc to determine whether the buffer setup has been completed by the ->mmap() handler. However, this time use smp_load_acquire() and smp_store_release() to wrap all the ordering in a single macro call. Also, add comments to make it evident that binder uses alloc->vma to determine when the binder_alloc has been fully initialized. In these scenarios acquiring the mmap_lock is not required. Fixes: a43cfc8 ("android: binder: stop saving a pointer to the VMA") Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Suren Baghdasaryan <surenb@google.com> Cc: stable@vger.kernel.org Signed-off-by: Carlos Llamas <cmllamas@google.com> Link: https://lore.kernel.org/r/20230502201220.1756319-3-cmllamas@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c0fd210 commit 0fa5334

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

drivers/android/binder_alloc.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,18 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
309309
return vma ? -ENOMEM : -ESRCH;
310310
}
311311

312+
static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
313+
struct vm_area_struct *vma)
314+
{
315+
/* pairs with smp_load_acquire in binder_alloc_get_vma() */
316+
smp_store_release(&alloc->vma, vma);
317+
}
318+
312319
static inline struct vm_area_struct *binder_alloc_get_vma(
313320
struct binder_alloc *alloc)
314321
{
315-
struct vm_area_struct *vma = NULL;
316-
317-
if (alloc->vma) {
318-
/* Look at description in binder_alloc_set_vma */
319-
smp_rmb();
320-
vma = alloc->vma;
321-
}
322-
return vma;
322+
/* pairs with smp_store_release in binder_alloc_set_vma() */
323+
return smp_load_acquire(&alloc->vma);
323324
}
324325

325326
static bool debug_low_async_space_locked(struct binder_alloc *alloc, int pid)
@@ -382,6 +383,7 @@ static struct binder_buffer *binder_alloc_new_buf_locked(
382383
size_t size, data_offsets_size;
383384
int ret;
384385

386+
/* Check binder_alloc is fully initialized */
385387
if (!binder_alloc_get_vma(alloc)) {
386388
binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
387389
"%d: binder_alloc_buf, no vma\n",
@@ -777,7 +779,9 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
777779
buffer->free = 1;
778780
binder_insert_free_buffer(alloc, buffer);
779781
alloc->free_async_space = alloc->buffer_size / 2;
780-
alloc->vma = vma;
782+
783+
/* Signal binder_alloc is fully initialized */
784+
binder_alloc_set_vma(alloc, vma);
781785

782786
return 0;
783787

@@ -959,7 +963,7 @@ int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
959963
*/
960964
void binder_alloc_vma_close(struct binder_alloc *alloc)
961965
{
962-
alloc->vma = 0;
966+
binder_alloc_set_vma(alloc, NULL);
963967
}
964968

965969
/**

0 commit comments

Comments
 (0)