Skip to contentSkip to conversation

nfc: hci: Save a few bytes of memory when registering a 'nfc_llc' engine

PATCH Applied Christophe JAILLET 3 participants 6 messages Feb 3, 2024 – Feb 6, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover 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(-)
Signature · 1 line
2.43.0
#1 lore raw

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().

---
 net/nfc/hci/llc.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
net/nfc/hci/llc.c +10 -8
index 2140f6724644..480c17f372a5 100644
@@ -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,
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 2/2 nfc: hci: Save a few bytes of memory when registering a 'nfc_llc' engine

In reply to Christophe JAILLET

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.

---
 net/nfc/hci/llc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/nfc/hci/llc.c +2 -2
index 480c17f372a5..ba91284f4086 100644
@@ -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;
Signature · 1 line
2.43.0
#5 lore raw
In reply to Christophe JAILLET
This series was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

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

Quoted 10 lines · 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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html