mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Paulo da Silva <psdasilva@esoterica.pt>
To: Anton Altaparmakov <aia21@cam.ac.uk>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Accessing file mapped data inside the kernel
Date: Thu, 10 Nov 2005 01:44:22 +0000	[thread overview]
Message-ID: <4372A5F6.7030306@esoterica.pt> (raw)
In-Reply-To: <Pine.LNX.4.64.0511092143400.19282@hermes-1.csi.cam.ac.uk>

Anton Altaparmakov wrote:

>Hi,
>
>On Wed, 9 Nov 2005, Paulo da Silva wrote:
>  
>
>>I posted about this a few days ago but got no responses
>>so far! I think this should be a trivial question for those
>>involved in the kernel internals. May be I didn't develop
>>the problem enough to be understood.
>>
>>So, here is the question reformulated.
>>
>>A given file system must supply a procedure for mmap.
>>
>>int <fsname>_file_mmap(struct file * file, struct vm_area_struct * vma)
>>{
>>  int addr;
>>  addr=generic_file_mmap(file,vma);
>>  <Code to access addr pointed bytes or vma->vm_start>
>>  return addr;
>>}
>>
>>I could verify that "addr" is what is returned to the user as
>>a pointer to a string of bytes that maps a file when a user
>>program calls mmap or mmap2.
>>
>>In the user program, I can access those bytes (read/write)
>>as, for ex., a char pointer.
>>
>>I don't know how to access those bytes inside the kernel
>>at the point <Code to access addr pointed bytes or vma->vm_start>
>>
>>First trys led the program that invoked mmap to block.
>>I thought that there's something to do with a previous
>>   down_write(&current->mm->mmap_sem);
>>If I execute
>>   up_write(&current->mm->mmap_sem);
>>before accessing the data the block situation does not
>>occur anymore. I would like to hear something about
>>this.
>>
>>Anyway, I tryed to use "copy_from_user" but I got
>>garbage, not the file contents! Using "strncpy" crashes
>>the kernel (UML)!
>>
>>Can someone please write a fragment of code to safely
>>access those bytes, copying them to and from a
>>kernel char pointed area so that they are read/written
>>to the file?
>>    
>>
>
>Why do you want to do that?  If you explain what you are trying to do it 
>may be possible to help you better.  It is almost 100% certain that your 
>are going about it in completely the wrong way, so please describe what 
>you are trying to do...
>
>Best regards,
>
>	Anton
>  
>
Just try to understand the kernel filesystem.
So far I could understand the 1st layer of
reading and writing. mmap seems to be a
difficult task however. So, I made a 1st try
looking at mmap supplied by the filesystem,
but I couldn't even succeed with a printk
of the mapped area! I would like to understand
what is the meaning of the address (int) returned
by generic_file_mmap that is also into vma->vm_start
and is returned to the user as a char pointer.
I thought that this address, being accessible
by a user program as a char pointer, should also
be accessible by a copy-from-user inside the
kernel. Unfortunately, this didn't happen!
Why? That's my question. Did I make any mistake?
A basic fragment of code showing how to access
that area could enlight me so that I could go
deeply into the code.

Ex.
Suppose a file has a string of text ("foo")
and the user calls mmap.

Why does this code not work?

The supplied filesystem mmap is "generic_file_mmap".
So, I changed it to foo_file_mmap as follows:

int foo_file_mmap(struct file * file, struct vm_area_struct * vma)
{

  int addr;
  char tstr[100];
  addr=generic_file_mmap(file,vma);
  up_write(&current->mm->mmap_sem); /* Without this the user program is dead locked */
  copy_from_user(tstr,(char*)addr,4);
  printk("%s",tstr);

  return addr;
}



  reply	other threads:[~2005-11-10  1:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-09 20:15 Paulo da Silva
2005-11-09 20:46 ` linux-os (Dick Johnson)
2005-11-09 21:44 ` Anton Altaparmakov
2005-11-10  1:44   ` Paulo da Silva [this message]
2005-11-10  9:24     ` Anton Altaparmakov
2005-11-10 23:20       ` Paulo da Silva

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=4372A5F6.7030306@esoterica.pt \
    --to=psdasilva@esoterica.pt \
    --cc=aia21@cam.ac.uk \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®