Skip to content

Commit 543537b

Browse files
Paulo MarquesLinus Torvalds
authored andcommitted
[PATCH] create a kstrdup library function
This patch creates a new kstrdup library function and changes the "local" implementations in several places to use this function. Most of the changes come from the sound and net subsystems. The sound part had already been acknowledged by Takashi Iwai and the net part by David S. Miller. I left UML alone for now because I would need more time to read the code carefully before making changes there. Signed-off-by: Paulo Marques <pmarques@grupopie.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 991114c commit 543537b

21 files changed

Lines changed: 75 additions & 94 deletions

File tree

‎drivers/md/dm-ioctl.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,6 @@ static struct hash_cell *__get_uuid_cell(const char *str)
122122
/*-----------------------------------------------------------------
123123
* Inserting, removing and renaming a device.
124124
*---------------------------------------------------------------*/
125-
static inline char *kstrdup(const char *str)
126-
{
127-
char *r = kmalloc(strlen(str) + 1, GFP_KERNEL);
128-
if (r)
129-
strcpy(r, str);
130-
return r;
131-
}
132-
133125
static struct hash_cell *alloc_cell(const char *name, const char *uuid,
134126
struct mapped_device *md)
135127
{
@@ -139,7 +131,7 @@ static struct hash_cell *alloc_cell(const char *name, const char *uuid,
139131
if (!hc)
140132
return NULL;
141133

142-
hc->name = kstrdup(name);
134+
hc->name = kstrdup(name, GFP_KERNEL);
143135
if (!hc->name) {
144136
kfree(hc);
145137
return NULL;
@@ -149,7 +141,7 @@ static struct hash_cell *alloc_cell(const char *name, const char *uuid,
149141
hc->uuid = NULL;
150142

151143
else {
152-
hc->uuid = kstrdup(uuid);
144+
hc->uuid = kstrdup(uuid, GFP_KERNEL);
153145
if (!hc->uuid) {
154146
kfree(hc->name);
155147
kfree(hc);
@@ -273,7 +265,7 @@ static int dm_hash_rename(const char *old, const char *new)
273265
/*
274266
* duplicate new.
275267
*/
276-
new_name = kstrdup(new);
268+
new_name = kstrdup(new, GFP_KERNEL);
277269
if (!new_name)
278270
return -ENOMEM;
279271

‎drivers/parport/probe.c‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ static void pretty_print(struct parport *port, int device)
4848
printk("\n");
4949
}
5050

51-
static char *strdup(char *str)
52-
{
53-
int n = strlen(str)+1;
54-
char *s = kmalloc(n, GFP_KERNEL);
55-
if (!s) return NULL;
56-
return strcpy(s, str);
57-
}
58-
5951
static void parse_data(struct parport *port, int device, char *str)
6052
{
6153
char *txt = kmalloc(strlen(str)+1, GFP_KERNEL);
@@ -88,16 +80,16 @@ static void parse_data(struct parport *port, int device, char *str)
8880
if (!strcmp(p, "MFG") || !strcmp(p, "MANUFACTURER")) {
8981
if (info->mfr)
9082
kfree (info->mfr);
91-
info->mfr = strdup(sep);
83+
info->mfr = kstrdup(sep, GFP_KERNEL);
9284
} else if (!strcmp(p, "MDL") || !strcmp(p, "MODEL")) {
9385
if (info->model)
9486
kfree (info->model);
95-
info->model = strdup(sep);
87+
info->model = kstrdup(sep, GFP_KERNEL);
9688
} else if (!strcmp(p, "CLS") || !strcmp(p, "CLASS")) {
9789
int i;
9890
if (info->class_name)
9991
kfree (info->class_name);
100-
info->class_name = strdup(sep);
92+
info->class_name = kstrdup(sep, GFP_KERNEL);
10193
for (u = sep; *u; u++)
10294
*u = toupper(*u);
10395
for (i = 0; classes[i].token; i++) {
@@ -112,15 +104,15 @@ static void parse_data(struct parport *port, int device, char *str)
112104
!strcmp(p, "COMMAND SET")) {
113105
if (info->cmdset)
114106
kfree (info->cmdset);
115-
info->cmdset = strdup(sep);
107+
info->cmdset = kstrdup(sep, GFP_KERNEL);
116108
/* if it speaks printer language, it's
117109
probably a printer */
118110
if (strstr(sep, "PJL") || strstr(sep, "PCL"))
119111
guessed_class = PARPORT_CLASS_PRINTER;
120112
} else if (!strcmp(p, "DES") || !strcmp(p, "DESCRIPTION")) {
121113
if (info->description)
122114
kfree (info->description);
123-
info->description = strdup(sep);
115+
info->description = kstrdup(sep, GFP_KERNEL);
124116
}
125117
}
126118
rock_on:

‎include/linux/netdevice.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -925,10 +925,6 @@ extern int skb_checksum_help(struct sk_buff *skb, int inward);
925925
extern void net_enable_timestamp(void);
926926
extern void net_disable_timestamp(void);
927927

928-
#ifdef CONFIG_SYSCTL
929-
extern char *net_sysctl_strdup(const char *s);
930-
#endif
931-
932928
#endif /* __KERNEL__ */
933929

934930
#endif /* _LINUX_DEV_H */

‎include/linux/string.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ extern int memcmp(const void *,const void *,__kernel_size_t);
8888
extern void * memchr(const void *,int,__kernel_size_t);
8989
#endif
9090

91+
extern char *kstrdup(const char *s, int gfp);
92+
9193
#ifdef __cplusplus
9294
}
9395
#endif

‎include/sound/core.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ void *snd_hidden_kcalloc(size_t n, size_t size, int flags);
292292
void snd_hidden_kfree(const void *obj);
293293
void *snd_hidden_vmalloc(unsigned long size);
294294
void snd_hidden_vfree(void *obj);
295+
char *snd_hidden_kstrdup(const char *s, int flags);
295296
#define kmalloc(size, flags) snd_hidden_kmalloc(size, flags)
296297
#define kcalloc(n, size, flags) snd_hidden_kcalloc(n, size, flags)
297298
#define kfree(obj) snd_hidden_kfree(obj)
@@ -301,6 +302,7 @@ void snd_hidden_vfree(void *obj);
301302
#define vmalloc_nocheck(size) snd_wrapper_vmalloc(size)
302303
#define kfree_nocheck(obj) snd_wrapper_kfree(obj)
303304
#define vfree_nocheck(obj) snd_wrapper_vfree(obj)
305+
#define kstrdup(s, flags) snd_hidden_kstrdup(s, flags)
304306
#else
305307
#define snd_memory_init() /*NOP*/
306308
#define snd_memory_done() /*NOP*/
@@ -311,7 +313,6 @@ void snd_hidden_vfree(void *obj);
311313
#define kfree_nocheck(obj) kfree(obj)
312314
#define vfree_nocheck(obj) vfree(obj)
313315
#endif
314-
char *snd_kmalloc_strdup(const char *string, int flags);
315316
int copy_to_user_fromio(void __user *dst, const volatile void __iomem *src, size_t count);
316317
int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size_t count);
317318

‎mm/slab.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#include <linux/sysctl.h>
9393
#include <linux/module.h>
9494
#include <linux/rcupdate.h>
95+
#include <linux/string.h>
9596

9697
#include <asm/uaccess.h>
9798
#include <asm/cacheflush.h>
@@ -3082,3 +3083,26 @@ unsigned int ksize(const void *objp)
30823083

30833084
return size;
30843085
}
3086+
3087+
3088+
/*
3089+
* kstrdup - allocate space for and copy an existing string
3090+
*
3091+
* @s: the string to duplicate
3092+
* @gfp: the GFP mask used in the kmalloc() call when allocating memory
3093+
*/
3094+
char *kstrdup(const char *s, int gfp)
3095+
{
3096+
size_t len;
3097+
char *buf;
3098+
3099+
if (!s)
3100+
return NULL;
3101+
3102+
len = strlen(s) + 1;
3103+
buf = kmalloc(len, gfp);
3104+
if (buf)
3105+
memcpy(buf, s, len);
3106+
return buf;
3107+
}
3108+
EXPORT_SYMBOL(kstrdup);

‎net/core/neighbour.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <net/sock.h>
3333
#include <linux/rtnetlink.h>
3434
#include <linux/random.h>
35+
#include <linux/string.h>
3536

3637
#define NEIGH_DEBUG 1
3738

@@ -2592,7 +2593,7 @@ int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
25922593
t->neigh_vars[17].extra1 = dev;
25932594
}
25942595

2595-
dev_name = net_sysctl_strdup(dev_name_source);
2596+
dev_name = kstrdup(dev_name_source, GFP_KERNEL);
25962597
if (!dev_name) {
25972598
err = -ENOBUFS;
25982599
goto free;

‎net/core/sysctl_net_core.c‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,6 @@ extern int sysctl_somaxconn;
3535
extern char sysctl_divert_version[];
3636
#endif /* CONFIG_NET_DIVERT */
3737

38-
/*
39-
* This strdup() is used for creating copies of network
40-
* device names to be handed over to sysctl.
41-
*/
42-
43-
char *net_sysctl_strdup(const char *s)
44-
{
45-
char *rv = kmalloc(strlen(s)+1, GFP_KERNEL);
46-
if (rv)
47-
strcpy(rv, s);
48-
return rv;
49-
}
50-
5138
ctl_table core_table[] = {
5239
#ifdef CONFIG_NET
5340
{
@@ -177,6 +164,4 @@ ctl_table core_table[] = {
177164
{ .ctl_name = 0 }
178165
};
179166

180-
EXPORT_SYMBOL(net_sysctl_strdup);
181-
182167
#endif

‎net/ipv4/devinet.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ static void devinet_sysctl_register(struct in_device *in_dev,
14711471
* by sysctl and we wouldn't want anyone to change it under our feet
14721472
* (see SIOCSIFNAME).
14731473
*/
1474-
dev_name = net_sysctl_strdup(dev_name);
1474+
dev_name = kstrdup(dev_name, GFP_KERNEL);
14751475
if (!dev_name)
14761476
goto free;
14771477

‎net/ipv6/addrconf.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#endif
5858
#include <linux/delay.h>
5959
#include <linux/notifier.h>
60+
#include <linux/string.h>
6061

6162
#include <net/sock.h>
6263
#include <net/snmp.h>
@@ -3437,7 +3438,7 @@ static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf
34373438
* by sysctl and we wouldn't want anyone to change it under our feet
34383439
* (see SIOCSIFNAME).
34393440
*/
3440-
dev_name = net_sysctl_strdup(dev_name);
3441+
dev_name = kstrdup(dev_name, GFP_KERNEL);
34413442
if (!dev_name)
34423443
goto free;
34433444

0 commit comments

Comments
 (0)