2024-02-03 07:52:47

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH v2 0/2] nfc: hci: Save a few bytes of memory when registering a 'nfc_llc' engine

nfc_llc_register() calls pass a string literal as the 'name' parameter.

So kstrdup_const() can be used instead of kfree() to avoid a memory
allocation in such cases.

v2: Add a new helper function, nfc_llc_del_engine(), to reduce code
duplication. This is needed to address Jakub Kicinski's comment
about nfc_llc_exit() that was not updated in v1.

v1: https://lore.kernel.org/all/6d2b8c390907dcac2e4dc6e71f1b2db2ef8abef1.1705744530.git.christophe.jaillet@wanadoo.fr/

Christophe JAILLET (2):
nfc: hci: Introduce nfc_llc_del_engine() to reduce code duplication
nfc: hci: Save a few bytes of memory when registering a 'nfc_llc'
engine

net/nfc/hci/llc.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)

--
2.43.0



2024-02-03 07:53:26

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH v2 1/2] nfc: hci: Introduce nfc_llc_del_engine() to reduce code duplication

Add a new helper to avoid code duplication between nfc_llc_exit() and
nfc_llc_unregister().

Signed-off-by: Christophe JAILLET <[email protected]>
---
net/nfc/hci/llc.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/net/nfc/hci/llc.c b/net/nfc/hci/llc.c
index 2140f6724644..480c17f372a5 100644
--- a/net/nfc/hci/llc.c
+++ b/net/nfc/hci/llc.c
@@ -30,15 +30,19 @@ int __init nfc_llc_init(void)
return r;
}

+static void nfc_llc_del_engine(struct nfc_llc_engine *llc_engine)
+{
+ list_del(&llc_engine->entry);
+ kfree(llc_engine->name);
+ kfree(llc_engine);
+}
+
void nfc_llc_exit(void)
{
struct nfc_llc_engine *llc_engine, *n;

- list_for_each_entry_safe(llc_engine, n, &llc_engines, entry) {
- list_del(&llc_engine->entry);
- kfree(llc_engine->name);
- kfree(llc_engine);
- }
+ list_for_each_entry_safe(llc_engine, n, &llc_engines, entry)
+ nfc_llc_del_engine(llc_engine);
}

int nfc_llc_register(const char *name, const struct nfc_llc_ops *ops)
@@ -82,9 +86,7 @@ void nfc_llc_unregister(const char *name)
if (llc_engine == NULL)
return;

- list_del(&llc_engine->entry);
- kfree(llc_engine->name);
- kfree(llc_engine);
+ nfc_llc_del_engine(llc_engine);
}

struct nfc_llc *nfc_llc_allocate(const char *name, struct nfc_hci_dev *hdev,
--
2.43.0


2024-02-03 07:53:38

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH v2 2/2] nfc: hci: Save a few bytes of memory when registering a 'nfc_llc' engine

nfc_llc_register() calls pass a string literal as the 'name' parameter.

So kstrdup_const() can be used instead of kfree() to avoid a memory
allocation in such cases.

Signed-off-by: Christophe JAILLET <[email protected]>
---
net/nfc/hci/llc.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/nfc/hci/llc.c b/net/nfc/hci/llc.c
index 480c17f372a5..ba91284f4086 100644
--- a/net/nfc/hci/llc.c
+++ b/net/nfc/hci/llc.c
@@ -33,7 +33,7 @@ int __init nfc_llc_init(void)
static void nfc_llc_del_engine(struct nfc_llc_engine *llc_engine)
{
list_del(&llc_engine->entry);
- kfree(llc_engine->name);
+ kfree_const(llc_engine->name);
kfree(llc_engine);
}

@@ -53,7 +53,7 @@ int nfc_llc_register(const char *name, const struct nfc_llc_ops *ops)
if (llc_engine == NULL)
return -ENOMEM;

- llc_engine->name = kstrdup(name, GFP_KERNEL);
+ llc_engine->name = kstrdup_const(name, GFP_KERNEL);
if (llc_engine->name == NULL) {
kfree(llc_engine);
return -ENOMEM;
--
2.43.0


2024-02-06 14:36:54

by Simon Horman

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] nfc: hci: Save a few bytes of memory when registering a 'nfc_llc' engine

On Sat, Feb 03, 2024 at 08:51:04AM +0100, Christophe JAILLET wrote:
> nfc_llc_register() calls pass a string literal as the 'name' parameter.
>
> So kstrdup_const() can be used instead of kfree() to avoid a memory
> allocation in such cases.
>
> Signed-off-by: Christophe JAILLET <[email protected]>

Reviewed-by: Simon Horman <[email protected]>


2024-02-06 14:40:42

by patchwork-bot+netdevbpf

[permalink] [raw]
Subject: Re: [PATCH v2 0/2] nfc: hci: Save a few bytes of memory when registering a 'nfc_llc' engine

Hello:

This series was applied to netdev/net-next.git (main)
by Paolo Abeni <[email protected]>:

On Sat, 3 Feb 2024 08:51:02 +0100 you wrote:
> nfc_llc_register() calls pass a string literal as the 'name' parameter.
>
> So kstrdup_const() can be used instead of kfree() to avoid a memory
> allocation in such cases.
>
> v2: Add a new helper function, nfc_llc_del_engine(), to reduce code
> duplication. This is needed to address Jakub Kicinski's comment
> about nfc_llc_exit() that was not updated in v1.
>
> [...]

Here is the summary with links:
- [v2,1/2] nfc: hci: Introduce nfc_llc_del_engine() to reduce code duplication
https://git.kernel.org/netdev/net-next/c/d6f4aac19ad4
- [v2,2/2] nfc: hci: Save a few bytes of memory when registering a 'nfc_llc' engine
https://git.kernel.org/netdev/net-next/c/83cdd8db7508

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



2024-02-06 15:00:59

by Simon Horman

[permalink] [raw]
Subject: Re: [PATCH v2 1/2] nfc: hci: Introduce nfc_llc_del_engine() to reduce code duplication

On Sat, Feb 03, 2024 at 08:51:03AM +0100, Christophe JAILLET wrote:
> Add a new helper to avoid code duplication between nfc_llc_exit() and
> nfc_llc_unregister().
>
> Signed-off-by: Christophe JAILLET <[email protected]>

Reviewed-by: Simon Horman <[email protected]>