当前位置: 技术问答>linux和unix
linux内核里面频繁分配和释放空间造成的系统奔溃
来源: 互联网 发布时间:2017-03-12
本文导语: 各位大侠们:你们好: 我最近在在tcp.c(netipv4)文件的int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,size_t size)函数中加了一段代码(红色部分),奔溃信息在最后!我分析了一下是不是我在linux内核...
各位大侠们:你们好:
我最近在在tcp.c(netipv4)文件的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_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 Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is %dn",ksize(mouiov));
if((duiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is %dn",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 UnSuccessn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is Successn");
tfm = crypto_alloc_comp("deflate",0,0);
if(IS_ERR(tfm)) {
printk("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("failed to compress !n");
release_sock(sk);
//return copied;
return 0;
}
printk(KERN_INFO " tcp_sendmsg iov->iov_len %d unSuccessn",iov->iov_len);
printk(KERN_INFO " tcp_sendmsg duiov_len %d unSuccessn",duiov_len);
iov->iov_base=duiov;
iov->iov_len=duiov_len;
crypto_free_comp(tfm);
kfree(mouiov);
mouflag=1;
}
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] [] warn_alloc_failed+0x97/0xf0
Aug 30 20:02:00 ubuntu kernel: [ 266.030070] [] __alloc_pages_nodemask+0x3dc/0x660
Aug 30 20:02:00 ubuntu kernel: [ 266.030077] [] __get_free_pages+0x1c/0x30
Aug 30 20:02:00 ubuntu kernel: [ 266.030084] [] kmalloc_order_trace+0x27/0x90
Aug 30 20:02:00 ubuntu kernel: [ 266.030093] [] ? tcp_established_options+0x30/0xb0
Aug 30 20:02:00 ubuntu kernel: [ 266.030099] [] __kmalloc+0x158/0x1b0
Aug 30 20:02:00 ubuntu kernel: [ 266.030106] [] ? tcp_send_mss+0x20/0xe0
Aug 30 20:02:00 ubuntu kernel: [ 266.030112] [] tcp_sendmsg+0xe3/0xae0
Aug 30 20:02:00 ubuntu kernel: [ 266.030118] [] ? get_page_from_freelist+0x258/0x4d0
Aug 30 20:02:00 ubuntu kernel: [ 266.030126] [] inet_sendmsg+0x47/0xb0
Aug 30 20:02:00 ubuntu kernel: [ 266.030133] [] sock_aio_write+0x14e/0x160
Aug 30 20:02:Aug 30 20:04:43 ubuntu kernel: imklog 4.2.0, log source = /proc/kmsg started.
Aug 30 20:04:43 ubuntu rsyslogd: [origin software="rsyslogd" swVersion="4.2.0" x-pid="648" x-info="http://www.rsyslog.com"] (re)start
Aug 30 20:04:43 ubuntu rsyslogd: rsyslogd's groupid changed to 103
Aug 30 20:04:43 ubuntu rsyslogd: rsyslogd's userid changed to 101
Aug 30 20:04:43 ubuntu rsyslogd-2039: Could no open output file '/dev/xconsole' [try http://www.rsyslog.com/e/2039 ]
Aug 30 20:04:43 ubuntu kernel: [ 0.000000] Initializing cgroup subsys cpuset
我最近在在tcp.c(netipv4)文件的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_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 Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 mouiov=kmalloc is %dn",ksize(mouiov));
if((duiov=kmalloc(iov->iov_len, GFP_KERNEL)))
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is Successn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is unSuccessn");
printk(KERN_INFO " tcp_sendmsg 2012 8 23 duiov=kmalloc is %dn",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 UnSuccessn");
else
printk(KERN_INFO " tcp_sendmsg 2012 8 23 copy_from_user is Successn");
tfm = crypto_alloc_comp("deflate",0,0);
if(IS_ERR(tfm)) {
printk("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("failed to compress !n");
release_sock(sk);
//return copied;
return 0;
}
printk(KERN_INFO " tcp_sendmsg iov->iov_len %d unSuccessn",iov->iov_len);
printk(KERN_INFO " tcp_sendmsg duiov_len %d unSuccessn",duiov_len);
iov->iov_base=duiov;
iov->iov_len=duiov_len;
crypto_free_comp(tfm);
kfree(mouiov);
mouflag=1;
}
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] [] warn_alloc_failed+0x97/0xf0
Aug 30 20:02:00 ubuntu kernel: [ 266.030070] [] __alloc_pages_nodemask+0x3dc/0x660
Aug 30 20:02:00 ubuntu kernel: [ 266.030077] [] __get_free_pages+0x1c/0x30
Aug 30 20:02:00 ubuntu kernel: [ 266.030084] [] kmalloc_order_trace+0x27/0x90
Aug 30 20:02:00 ubuntu kernel: [ 266.030093] [] ? tcp_established_options+0x30/0xb0
Aug 30 20:02:00 ubuntu kernel: [ 266.030099] [] __kmalloc+0x158/0x1b0
Aug 30 20:02:00 ubuntu kernel: [ 266.030106] [] ? tcp_send_mss+0x20/0xe0
Aug 30 20:02:00 ubuntu kernel: [ 266.030112] [] tcp_sendmsg+0xe3/0xae0
Aug 30 20:02:00 ubuntu kernel: [ 266.030118] [] ? get_page_from_freelist+0x258/0x4d0
Aug 30 20:02:00 ubuntu kernel: [ 266.030126] [] inet_sendmsg+0x47/0xb0
Aug 30 20:02:00 ubuntu kernel: [ 266.030133] [] sock_aio_write+0x14e/0x160
Aug 30 20:02:Aug 30 20:04:43 ubuntu kernel: imklog 4.2.0, log source = /proc/kmsg started.
Aug 30 20:04:43 ubuntu rsyslogd: [origin software="rsyslogd" swVersion="4.2.0" x-pid="648" x-info="http://www.rsyslog.com"] (re)start
Aug 30 20:04:43 ubuntu rsyslogd: rsyslogd's groupid changed to 103
Aug 30 20:04:43 ubuntu rsyslogd: rsyslogd's userid changed to 101
Aug 30 20:04:43 ubuntu rsyslogd-2039: Could no open output file '/dev/xconsole' [try http://www.rsyslog.com/e/2039 ]
Aug 30 20:04:43 ubuntu kernel: [ 0.000000] Initializing cgroup subsys cpuset
|
lz在应用空间编程时,栈内存 和 堆内存 的生命周期了解否?
这里是内核态编程,对程序员要求更高,
栈内存能自动回收,kmalloc申请的,你不kfree,那就只有等到关机了
这里是内核态编程,对程序员要求更高,
栈内存能自动回收,kmalloc申请的,你不kfree,那就只有等到关机了
|
你的kmalloc和kfree个数有差异,kmalloc更多,会造成内存泄漏
此外,你这里的直接原因是kmalloc的大小超过了kmalloc的限制,kmalloc最大只能申请2M还是4M记不清了
你应该学习和选用更合理的设计,比如内存池
此外,你这里的直接原因是kmalloc的大小超过了kmalloc的限制,kmalloc最大只能申请2M还是4M记不清了
你应该学习和选用更合理的设计,比如内存池
|
楼上说的很对,好好设计一下吧