日期:2014-05-16  浏览次数:20659 次

linux内核里面频繁分配和释放空间造成的系统奔溃
各位大侠们:你们好:
我最近在在tcp.c(net\ipv4)文件的int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,size_t size)函数中加了一段代码(红色部分),奔溃信息在最后!我分析了一下是不是我在linux内核里面频繁分配和释放空间造成的系统奔溃!
忘高手织一条生路!!谢谢啦!!
int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
size_t size)
{
struct iovec *iov;
struct tcp_sock *tp = tcp_sk(sk);
struct sk_buff *skb;
int iovlen, flags;
int mss_now, size_goal;
int sg, err, copied;
long timeo;
/*2012 8 23 mouchange*/
char *mouiov;
char *duiov;
int duiov_len;
struct crypto_comp *tfm;
int mouflag=0;
int ret;
struct iovec *mdiov;


lock_sock(sk);

flags = msg->msg_flags;
timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);

/* Wait for a connection to finish. */
if ((1 << sk->sk_state) & ~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))
if ((err = sk_stream_wait_connect(sk, &timeo)) != 0)
goto out_err;

/* This should be in poll */
clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);

mss_now = tcp_send_mss(sk, &size_goal, flags);

/* Ok commence sending. */
iovlen = msg->msg_iovlen;
iov = msg->msg_iov;
copied = 0;

err = -EPIPE;
if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
goto out_err;

sg = sk->sk_route_caps & NETIF_F_SG;
/*2012 8 23 mouchange*/
if(iov->iov_len>2000)
{
mdiov=iov;

if((mouiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is Success\n");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is unSuccess\n");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is %d\n",ksize(mouiov));

if((duiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is Success\n");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is unSuccess\n");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is %d\n",ksize(duiov));

if(copy_from_user(mouiov,iov->iov_base,iov->iov_len))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is UnSuccess\n");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is Success\n");

tfm = crypto_alloc_comp("deflate",0,0);
if(IS_ERR(tfm)) {
printk("<1>failed to load transform for deflate !\n");
release_sock(sk);
return 0;
}
ret = crypto_comp_compress(tfm,mouiov,iov->iov_len,duiov,&duiov_len);
if(ret) {
printk("<1>failed to compress !\n");
release_sock(sk);
//return copied;
return 0;
}
printk(KERN_INFO " tcp_sendmsg iov->iov_len %d unSuccess\n",iov->iov_len);
printk(KERN_INFO " tcp_sendmsg duiov_len %d unSuccess\n",duiov_len);
iov->iov_base=duiov;
iov->iov_len=duiov_len;
crypto_free_comp(tfm);
kfree(mouiov);
mouflag=1;
}[color=#FF0000][/color][color=#FF0000][/color]

while (--iovlen >= 0) {
size_t seglen = iov->iov_len;
unsigned char __user *from = iov->iov_base;
奔溃信息:
Aug 30 20:02:00 ubuntu kernel: [ 266.030039] lftp: page allocation failure: order:4, mode:0x40d0
Aug 30 20:02:00 ubuntu kernel: [ 266.030046] Pid: 1416, comm: lftp Not tainted 3.1.4 #15
Aug 30 20:02:00 ubuntu kernel: [ 266.030050] Call Trace:
Aug 30 20:02:00 ubuntu kernel: [ 266.030063] [<c01e0857>] warn_alloc_failed+0x97/0xf0
Aug 30 20:02:00 ubuntu kernel: [ 266.030070] [<c01e17fc>]