netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Haller <thaller@redhat.com>
To: Phil Sutter <phil@nwl.cc>
Cc: NetFilter <netfilter-devel@vger.kernel.org>
Subject: Re: [PATCH nft 2/4] gmputil: add nft_gmp_free() to free strings from mpz_get_str()
Date: Wed, 20 Sep 2023 16:46:12 +0200	[thread overview]
Message-ID: <9d90f25dd24e76567e784c93b2a1a5493c14e379.camel@redhat.com> (raw)
In-Reply-To: <ZQr8KsFAXIT0mca9@orbyte.nwl.cc>

On Wed, 2023-09-20 at 16:05 +0200, Phil Sutter wrote:
> On Wed, Sep 20, 2023 at 03:13:39PM +0200, Thomas Haller wrote:
> > 
> > +       mp_get_memory_functions(NULL, NULL, &free_fcn);
> 
> Do we have to expect the returned pointer to change at run-time?
> Because
> if not, couldn't one make free_fcn static and call
> mp_get_memory_functions() only if it's NULL?

Hi Phil,


no, it's not expected to EVER change. Users must not change
mp_set_memory_functions() after any GMP objects were allocated,
otherwise there would be a mixup of allocators and crashes ahead.

However, I didn't cache the value, because I don't want to use global
data without atomic compare-exchange (or thread-local). Doing it
without regard of thread-safety so would be a code smell (even if
probably not an issue in practice). And getting it with atomic/thread-
local would be cumbersome. It's hard to ensure a code base has no
threading issues, when having lots of places that "most likely are
99.99% fine (but not 100%)". Hence, I want to avoid the global.

I think the call to mp_get_memory_functions() should be cheap.

Note that libnftables no longer calls mp_set_memory_functions() ([1]).
So for the patch to have practical effects, you would need to have
another part of the process call mp_set_memory_functions() and set a
free function incompatible with libc's free(). So the scenario is very
unlikely already. It's more about being clear about using the correct
free for an allocation (even if in practice it all ends up being the
same).

[1] https://git.netfilter.org/nftables/commit/?id=96ee78ec4a0707114d2f8ef7590d08cfd25080ea


Thomas


  reply	other threads:[~2023-09-20 14:48 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-20 13:13 [PATCH nft 0/4] remove xfree() and add free_const()+nft_gmp_free() Thomas Haller
2023-09-20 13:13 ` [PATCH nft 1/4] datatype: don't return a const string from cgroupv2_get_path() Thomas Haller
2023-09-20 13:13 ` [PATCH nft 2/4] gmputil: add nft_gmp_free() to free strings from mpz_get_str() Thomas Haller
2023-09-20 14:05   ` Phil Sutter
2023-09-20 14:46     ` Thomas Haller [this message]
2023-09-20 16:04       ` Phil Sutter
2023-09-20 13:13 ` [PATCH nft 3/4] all: add free_const() and use it instead of xfree() Thomas Haller
2023-09-20 14:13   ` Phil Sutter
2023-09-20 16:06     ` Pablo Neira Ayuso
2023-09-20 16:49       ` Phil Sutter
2023-09-20 16:52         ` Pablo Neira Ayuso
2023-09-20 18:03         ` Thomas Haller
2023-09-20 18:22           ` Phil Sutter
2023-09-20 19:48             ` Thomas Haller
2023-09-20 22:50               ` Phil Sutter
2023-09-21  9:08                 ` Thomas Haller
2023-09-20 13:13 ` [PATCH nft 4/4] all: remove xfree() and use plain free() Thomas Haller

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=9d90f25dd24e76567e784c93b2a1a5493c14e379.camel@redhat.com \
    --to=thaller@redhat.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=phil@nwl.cc \
    /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 NNTP newsgroup(s).