Skip to content

Commit 18d4bcd

Browse files
Carlos Llamassmb49
authored andcommitted
binder: add lockless binder_alloc_(set|get)_vma()
BugLink: https://bugs.launchpad.net/bugs/2028979 commit 0fa5334 upstream. 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> Signed-off-by: Kamal Mostafa <kamal@canonical.com> Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
1 parent 4ffb79f commit 18d4bcd

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

drivers/android/binder_alloc.c

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

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

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

385+
/* Check binder_alloc is fully initialized */
384386
if (!binder_alloc_get_vma(alloc)) {
385387
binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
386388
"%d: binder_alloc_buf, no vma\n",
@@ -776,7 +778,9 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
776778
buffer->free = 1;
777779
binder_insert_free_buffer(alloc, buffer);
778780
alloc->free_async_space = alloc->buffer_size / 2;
779-
alloc->vma = vma;
781+
782+
/* Signal binder_alloc is fully initialized */
783+
binder_alloc_set_vma(alloc, vma);
780784

781785
return 0;
782786

@@ -958,7 +962,7 @@ int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
958962
*/
959963
void binder_alloc_vma_close(struct binder_alloc *alloc)
960964
{
961-
alloc->vma = 0;
965+
binder_alloc_set_vma(alloc, NULL);
962966
}
963967

964968
/**

0 commit comments

Comments
 (0)