-5

I tried using copy_to_user() inside a loop.

for_each_process(p) {
    copy_to_user(buf, "data of p", len);
}

But, the output that I got is different. It has only the last line of data in the user space, e.g.

#Data expected to copy to user space

123 1234 12 21
1243 124 423 12
1234 422 42 423

#Current Output:
1234 422 42 423

How to copy each line to the user space from the kernel space?

Krishna
  • 484
  • 7
  • 22
  • 3
    Please show your code. Instead of leading us to what you think the problem is (which might be totally wrong) show us the code and let us come to our own conclusions. By all means make your own assessment but also give us the data (ie code) to examine and verify. – kaylum Mar 13 '17 at 21:51
  • 2
    "*I thought if the data that is passed into the `copy_to_user()`*". No, Linux' `copy_to_user(src, dst, len)` copies data to the location `dst` (in the user process' address space) you point to. If you call it multiple times with the same pointer, you overwrite existing data at that location. – dhke Mar 13 '17 at 21:53

1 Answers1

5

How many times can you use copy_to_user() in a kernel program?

As many times as you want. But they have to make sense (because anything you do in any kind of program has to make sense).

I thought if the data that is passed into the copy_to_user() will append the data to the next line.

No, copy_to_user does not append anything. I'm not sure where you got that idea.

What is actually happening

Well, you're copying the data for the first process, then overwriting it with the data for the second process, then overwriting that with the data for the third process, and so on. At the end you're left with the third process's data.

How do transfer all three lines to the user space from the kernel space?

Store the data for each process at a different location.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • How do I store it in different locations? and How do I access from different locations? – Krishna Mar 13 '17 at 22:30
  • @Yggdrasil That's your program to figure out. I'll note that the first parameter (in this case `buf`) is the address the data gets copied to. – user253751 Mar 13 '17 at 22:52