Skip to content

Commit 8e1611e

Browse files
Al Virodavem330
authored andcommitted
make sock_alloc_file() do sock_release() on failures
This changes calling conventions (and simplifies the hell out the callers). New rules: once struct socket had been passed to sock_alloc_file(), it's been consumed either by struct file or by sock_release() done by sock_alloc_file(). Either way the caller should not do sock_release() after that point. Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 016a266 commit 8e1611e

File tree

5 files changed

+11
-31
lines changed

5 files changed

+11
-31
lines changed

drivers/staging/lustre/lnet/lnet/lib-socket.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,12 @@ lnet_sock_ioctl(int cmd, unsigned long arg)
7171
}
7272

7373
sock_filp = sock_alloc_file(sock, 0, NULL);
74-
if (IS_ERR(sock_filp)) {
75-
sock_release(sock);
76-
rc = PTR_ERR(sock_filp);
77-
goto out;
78-
}
74+
if (IS_ERR(sock_filp))
75+
return PTR_ERR(sock_filp);
7976

8077
rc = kernel_sock_unlocked_ioctl(sock_filp, cmd, arg);
8178

8279
fput(sock_filp);
83-
out:
8480
return rc;
8581
}
8682

net/9p/trans_fd.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,6 @@ static int p9_socket_open(struct p9_client *client, struct socket *csocket)
839839
if (IS_ERR(file)) {
840840
pr_err("%s (%d): failed to map fd\n",
841841
__func__, task_pid_nr(current));
842-
sock_release(csocket);
843842
kfree(p);
844843
return PTR_ERR(file);
845844
}

net/kcm/kcmsock.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,6 @@ static struct file *kcm_clone(struct socket *osock)
16291629
{
16301630
struct socket *newsock;
16311631
struct sock *newsk;
1632-
struct file *file;
16331632

16341633
newsock = sock_alloc();
16351634
if (!newsock)
@@ -1649,11 +1648,7 @@ static struct file *kcm_clone(struct socket *osock)
16491648
sock_init_data(newsock, newsk);
16501649
init_kcm_sock(kcm_sk(newsk), kcm_sk(osock->sk)->mux);
16511650

1652-
file = sock_alloc_file(newsock, 0, osock->sk->sk_prot_creator->name);
1653-
if (IS_ERR(file))
1654-
sock_release(newsock);
1655-
1656-
return file;
1651+
return sock_alloc_file(newsock, 0, osock->sk->sk_prot_creator->name);
16571652
}
16581653

16591654
static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)

net/sctp/socket.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5080,7 +5080,6 @@ static int sctp_getsockopt_peeloff_common(struct sock *sk, sctp_peeloff_arg_t *p
50805080
*newfile = sock_alloc_file(newsock, 0, NULL);
50815081
if (IS_ERR(*newfile)) {
50825082
put_unused_fd(retval);
5083-
sock_release(newsock);
50845083
retval = PTR_ERR(*newfile);
50855084
*newfile = NULL;
50865085
return retval;

net/socket.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,22 @@ struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
406406
name.len = strlen(name.name);
407407
}
408408
path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
409-
if (unlikely(!path.dentry))
409+
if (unlikely(!path.dentry)) {
410+
sock_release(sock);
410411
return ERR_PTR(-ENOMEM);
412+
}
411413
path.mnt = mntget(sock_mnt);
412414

413415
d_instantiate(path.dentry, SOCK_INODE(sock));
414416

415417
file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
416418
&socket_file_ops);
417419
if (IS_ERR(file)) {
418-
/* drop dentry, keep inode */
420+
/* drop dentry, keep inode for a bit */
419421
ihold(d_inode(path.dentry));
420422
path_put(&path);
423+
/* ... and now kill it properly */
424+
sock_release(sock);
421425
return file;
422426
}
423427

@@ -1330,19 +1334,9 @@ SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
13301334

13311335
retval = sock_create(family, type, protocol, &sock);
13321336
if (retval < 0)
1333-
goto out;
1334-
1335-
retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1336-
if (retval < 0)
1337-
goto out_release;
1338-
1339-
out:
1340-
/* It may be already another descriptor 8) Not kernel problem. */
1341-
return retval;
1337+
return retval;
13421338

1343-
out_release:
1344-
sock_release(sock);
1345-
return retval;
1339+
return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
13461340
}
13471341

13481342
/*
@@ -1412,15 +1406,13 @@ SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
14121406
newfile1 = sock_alloc_file(sock1, flags, NULL);
14131407
if (IS_ERR(newfile1)) {
14141408
err = PTR_ERR(newfile1);
1415-
sock_release(sock1);
14161409
sock_release(sock2);
14171410
goto out;
14181411
}
14191412

14201413
newfile2 = sock_alloc_file(sock2, flags, NULL);
14211414
if (IS_ERR(newfile2)) {
14221415
err = PTR_ERR(newfile2);
1423-
sock_release(sock2);
14241416
fput(newfile1);
14251417
goto out;
14261418
}
@@ -1549,7 +1541,6 @@ SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
15491541
if (IS_ERR(newfile)) {
15501542
err = PTR_ERR(newfile);
15511543
put_unused_fd(newfd);
1552-
sock_release(newsock);
15531544
goto out_put;
15541545
}
15551546

0 commit comments

Comments
 (0)