public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Stubbs <ams@codesourcery.com>
To: Thomas Schwinge <thomas@codesourcery.com>, <gcc-patches@gcc.gnu.org>
Cc: Jakub Jelinek <jakub@redhat.com>,
	Tobias Burnus <tobias@codesourcery.com>,
	Tom de Vries <tdevries@suse.de>
Subject: Re: [og12] In 'libgomp/allocator.c:omp_realloc', route 'free' through 'MEMSPACE_FREE' (was: [PATCH] libgomp, OpenMP, nvptx: Low-latency memory allocator)
Date: Tue, 14 Feb 2023 15:11:14 +0000	[thread overview]
Message-ID: <82361586-1626-1243-337c-12509188ed9c@codesourcery.com> (raw)
In-Reply-To: <87h6voz9tl.fsf@euler.schwinge.homeip.net>

On 14/02/2023 12:54, Thomas Schwinge wrote:
> Hi Andrew!
> 
> On 2022-01-13T11:13:51+0000, Andrew Stubbs <ams@codesourcery.com> wrote:
>> Updated patch: this version fixes some missed cases of malloc in the
>> realloc implementation.
> 
> Right, and as it seems I've run into another issue: a stray 'free'.
> 
>> --- a/libgomp/allocator.c
>> +++ b/libgomp/allocator.c
> 
> Re 'omp_realloc':
> 
>> @@ -660,9 +709,10 @@ retry:
>>         gomp_mutex_unlock (&allocator_data->lock);
>>   #endif
>>         if (prev_size)
>> -     new_ptr = realloc (data->ptr, new_size);
>> +     new_ptr = MEMSPACE_REALLOC (allocator_data->memspace, data->ptr,
>> +                                 data->size, new_size);
>>         else
>> -     new_ptr = malloc (new_size);
>> +     new_ptr = MEMSPACE_ALLOC (allocator_data->memspace, new_size);
>>         if (new_ptr == NULL)
>>        {
>>   #ifdef HAVE_SYNC_BUILTINS
>> @@ -690,7 +740,11 @@ retry:
>>           && (free_allocator_data == NULL
>>               || free_allocator_data->pool_size == ~(uintptr_t) 0))
>>       {
>> -      new_ptr = realloc (data->ptr, new_size);
>> +      omp_memspace_handle_t memspace __attribute__((unused))
>> +     = (allocator_data
>> +        ? allocator_data->memspace
>> +        : predefined_alloc_mapping[allocator]);
>> +      new_ptr = MEMSPACE_REALLOC (memspace, data->ptr, data->size, new_size);
>>         if (new_ptr == NULL)
>>        goto fail;
>>         ret = (char *) new_ptr + sizeof (struct omp_mem_header);
>> @@ -701,7 +755,11 @@ retry:
>>       }
>>     else
>>       {
>> -      new_ptr = malloc (new_size);
>> +      omp_memspace_handle_t memspace __attribute__((unused))
>> +     = (allocator_data
>> +        ? allocator_data->memspace
>> +        : predefined_alloc_mapping[allocator]);
>> +      new_ptr = MEMSPACE_ALLOC (memspace, new_size);
>>         if (new_ptr == NULL)
>>        goto fail;
>>       }
>> @@ -735,32 +793,35 @@ retry:
> |    free (data->ptr);
>>     return ret;
> 
> I run into a SIGSEGV if a non-'malloc'-based allocation is 'free'd here.
> 
> The attached
> "In 'libgomp/allocator.c:omp_realloc', route 'free' through 'MEMSPACE_FREE'"
> appears to resolve my issue, but not yet regression-tested.  Does that
> look correct to you?

That looks correct. The only remaining use of "free" should be the one 
referring to the allocator object itself (i.e. the destructor).

> Or, instead of invoking 'MEMSPACE_FREE', should we scrap the
> 'used_pool_size' bookkeeping here, and just invoke 'omp_free' instead?
> 
>      --- libgomp/allocator.c
>      +++ libgomp/allocator.c
>      @@ -842,19 +842,7 @@ retry:
>         if (old_size - old_alignment < size)
>           size = old_size - old_alignment;
>         memcpy (ret, ptr, size);
>      -  if (__builtin_expect (free_allocator_data
>      -                   && free_allocator_data->pool_size < ~(uintptr_t) 0, 0))
>      -    {
>      -#ifdef HAVE_SYNC_BUILTINS
>      -      __atomic_add_fetch (&free_allocator_data->used_pool_size, -data->size,
>      -                     MEMMODEL_RELAXED);
>      -#else
>      -      gomp_mutex_lock (&free_allocator_data->lock);
>      -      free_allocator_data->used_pool_size -= data->size;
>      -      gomp_mutex_unlock (&free_allocator_data->lock);
>      -#endif
>      -    }
>      -  free (data->ptr);
>      +  ialias_call (omp_free) (ptr, free_allocator);
>         return ret;
> 
> (I've not yet analyzed whether that's completely equivalent.)

The used_pool_size code comes from upstream, so if you want to go beyond 
the mechanical substitution of "free" then you're adding a new patch 
(rather than tweaking an old one). I'll leave that for others to comment on.

Andrew

  reply	other threads:[~2023-02-14 15:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-20 15:58 [PATCH] libgomp, OpenMP, nvptx: Low-latency memory allocator Andrew Stubbs
2021-12-21 11:33 ` [PATCH] nvptx: bump default to PTX 4.1 Andrew Stubbs
2022-01-05 10:24   ` Tom de Vries
2022-01-05 10:33     ` Andrew Stubbs
2022-01-05 12:45       ` Tom de Vries
2022-01-05 11:08 ` [PATCH] libgomp, OpenMP, nvptx: Low-latency memory allocator Tom de Vries
2022-01-05 13:04   ` Tom de Vries
2022-01-05 14:36     ` Andrew Stubbs
2022-01-06  9:29       ` Tom de Vries
2022-01-06 17:53         ` Tom de Vries
2022-01-07 14:14           ` Andrew Stubbs
2022-01-13 11:13             ` Andrew Stubbs
2023-02-14 12:54               ` [og12] In 'libgomp/allocator.c:omp_realloc', route 'free' through 'MEMSPACE_FREE' (was: [PATCH] libgomp, OpenMP, nvptx: Low-latency memory allocator) Thomas Schwinge
2023-02-14 15:11                 ` Andrew Stubbs [this message]
2023-02-16 21:45                   ` Thomas Schwinge
2022-01-05 14:21   ` [PATCH] libgomp, OpenMP, nvptx: Low-latency memory allocator Andrew Stubbs

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=82361586-1626-1243-337c-12509188ed9c@codesourcery.com \
    --to=ams@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=tdevries@suse.de \
    --cc=thomas@codesourcery.com \
    --cc=tobias@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).