From: Qiuyu Zhang <qiuyu.zhang@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: Re: strange about copy_from_user
Date: Fri, 9 Jul 2004 16:11:12 -0700 [thread overview]
Message-ID: <c26fd82804070916115a1e238b@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.53.0407091752360.2731@chaos>
Thx,
I can describe what I do and my code simply.
I am try to do a module driver. So far I can insmod the module and
config ip address etc. There is a existed queue in user space which
is alloc by a user application. When I send a ping packet by the
device I create, it will call dev->hard_start_xmit. In this function I
need put the data into user space queue. Similarly, I also need read
the data from queue when user put a data into queue. That's what I
want to do.
Code description:
struct Queue
{
int read;
int write;
int length;
char *data;
};
At first, the module driver is inserted into kernel by calling
insmod. And then user space application call a function to tell
(register) module driver what is the pointer of the queue. The
function be called by user application is as following
int regQ2kernel( char *devname , char *queue){
struct ifrequ ifr;
int sockfd;
if((sockfd = socket(AF_INET,SOCK_DGRAM,0))<0){
....
}
strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name));
ifr.ifr_data = pQueue;
if((ret= ioctl(sockfd, SIOREGIFFLAGS, (int)&ifr))<0}{
}
close(sockfd);
return 1;
}
When the user application call the above function, the module driver
in kernel can get the pointer of queue. And then I just store the
pointer of queue.
After I configure IP address and startup the module driver, I send a
ping packet to the device. The packet arrived to the device correctly,
then I need copy the data to queue in user space.
Here, I have questions.
1) when I got the pointer of queue, can I access the item in the
struct directly such as read, write etc? I try to do it. Sometimes it
will crash OS.
2) Due to the above reason, I want to copy the struct of queue to
kernel space and then access the item of the Queue. But I cannot get
the correct content .
static int usbModem_dev_xmit(struct sk_buff *skb, struct net_device *dev){
struct Queue *p = (struct Queue *)dev->priv;
struct Queue kQueue;
copy_from_user(&kQueue, *p, sizeof(struct Queue));
......
// here , the data of kQueue is not the data in Queue in user
space, I don't know why.
}
The strange thing is that when I use copy_from_user at ioctl function,
everything is correct. How could figure it out?
So far the quesiton is clear ?
Thanks again.
On Fri, 9 Jul 2004 17:57:57 -0400 (EDT), Richard B. Johnson
<root@chaos.analogic.com> wrote:
>
>
> On Fri, 9 Jul 2004, Qiuyu Zhang wrote:
>
> > Hi all,
> > I am working on writing a module driver.
> >
> > I am trying to use API copy_from_user to copy a bunch of memory from
> > user space to kernel space. I write a ioctl function to register the
> > pointer of the memory to kernel. And in the ioctl function I can use
> > copy_from_user to get the correct data, but the strange thing is that
> > when I use copy_from_user in other kernel function such as
> > dev_hard_xmit function , I cannot
> > get the correct result. I don't konw what the reason is . Thx.
> > -
>
> Without looking at the code it's hard to figure out what you
> may be doing. However, copy_from_user() and copy_to_user() may
> not ever be executed with a spin-lock held. Generally, if
> you need to put user data into kernel "things", you need
> to buffer it, i.e., copy_from_user() into a kmalloc(ed) buffer,
> then work with it in kernel space.
>
> Cheers,
> Dick Johnson
> Penguin : Linux version 2.4.26 on an i686 machine (5570.56 BogoMips).
> Note 96.31% of all statistics are fiction.
>
>
prev parent reply other threads:[~2004-07-09 23:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-07-09 21:38 strange about copy_from_user Qiuyu Zhang
2004-07-09 21:57 ` Richard B. Johnson
2004-07-09 23:11 ` Qiuyu Zhang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c26fd82804070916115a1e238b@mail.gmail.com \
--to=qiuyu.zhang@gmail.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.