Re: regarding synchronization code

2010-09-25 Thread Bond
On Sat, Sep 25, 2010 at 10:34 PM, Sri Ram Vemulpali  wrote:

> I am just saying I know inline keyword. But what is always_inline.
>

I wonder why you were not given any lecture to Google etc as I get in my
other threads.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Sun, Sep 26, 2010 at 12:10 PM, Venkatram Tummala
wrote:


> Yes, it is possible to pass in 0 for register_chrdev. Please be clear what
> you want to achieve, from your subject line & the post. The subject line  &
> the first few lines of the post indicates that
>
That is not my fault if you do not read the thread for one character even I
was able to make it.


> you need something which prints only the last character.  And you go on
> creating a rucus around here.
>
Who is creating rucus me or people giving me non sensical lecture to read
malloc they hide their incompetency by giving lecture to Google.


> I have updated the code & attached it. This can only read & write 10
> characters. Now, please dont tell us that it is not you wanted or it doesn't
> work.  Please spare us.
>

Your code is giving  me some error
I did
Step 1) mknod /dev/bond c 0 0
Step 2) chmod 666 /dev/bond

Step 3) insmod bond.ko

Step 4) r...@bond:~# lsmod | grep bond
bond2031  0

Step5) echo -n abcde > /dev/bond

gives me error
*bash: /dev/bond: No such device or address

*Step 6) where* *as  I can see ls -l /dev/bond*

*crw-rw-rw- 1 root root 0, 0 2010-09-26 12:14 /dev/bond

in your code you have done  in memory_read
  *f_pos = *f_pos + count;

why have you done this?
What purpose it serves?


Re: character device driver reading only last character of buffer

2010-09-25 Thread Venkatram Tummala
On Sat, Sep 25, 2010 at 9:46 PM, Bond  wrote:

>
>
> On Sun, Sep 26, 2010 at 3:43 AM, Venkatram Tummala  > wrote:
>
>> This will print the the last character of the string you  wrote.
>>
> If you would have read the thread from my first post printing the last
> character is not my objective  I am trying to print the complete string.
>

Yes, it is possible to pass in 0 for register_chrdev. Please be clear what
you want to achieve, from your subject line & the post. The subject line  &
the first few lines of the post indicates that  you need something which
prints only the last character.  And you go on creating a rucus around here.
I have updated the code & attached it. This can only read & write 10
characters. Now, please dont tell us that it is not you wanted or it doesn't
work.  Please spare us.

>
>> As a side note, please spare us all with all this non-sense. We have
>> better things to do in our life. Please try your level best to understand
>> things before asking the kernel mailing list. If this BS continues, i am
>> afraid you will get yourself kicked out of the mailing list. Please do
>> yourself a favour by not posting such crap.
>>
>> Venkatram Tummala
>>
>> Please read the thread before giving a lecture on kernel newbies.
>
>
/* Necessary includes for device drivers */
#include 
//#include 
#include 
#include  /* printk() */
#include  /* kmalloc() */
#include  /* everything... */
#include  /* error codes */
#include  /* size_t */
#include 
#include  /* O_ACCMODE */
#include  /* cli(), *_flags */
#include  /* copy_from/to_user */

MODULE_LICENSE("Dual BSD/GPL");

/* Declaration of memory.c functions */
int memory_open(struct inode *inode, struct file *filp);
int memory_release(struct inode *inode, struct file *filp);
ssize_t memory_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
ssize_t memory_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos);
void memory_exit(void);
int memory_init(void);

/* Structure that declares the usual file */
/* access functions */
struct file_operations memory_fops = {
	read: memory_read,
   	write: memory_write,
	open: memory_open,
	release: memory_release
};
/* Declaration of the init and exit functions */
module_init(memory_init);
module_exit(memory_exit);

/* Buffer to store data */
char *memory_buffer;
int result;

int memory_init(void) {

	/* Registering device */
	result = register_chrdev(0, "bond", &memory_fops);
	if (result < 0) {
		printk(KERN_ALERT  "memory: cannot obtain major number \n");
		return result;
	}

	/* Allocating memory for the buffer */
	memory_buffer = kmalloc(10, GFP_KERNEL); 
	if (!memory_buffer) { 
		result = -ENOMEM;
		goto fail; 
	} 
	memset(memory_buffer, 0, 10);

	printk(KERN_ALERT "Inserting bond module\n"); 
	return 0;

fail: 
	memory_exit(); 
	return result;
}


void memory_exit(void) {
	/* Freeing the major number */
	unregister_chrdev(result, "bond");

	/* Freeing buffer memory */
	if (memory_buffer) {
		kfree(memory_buffer);
	}

	printk( KERN_ALERT "Removing bond module\n");

}


int memory_open(struct inode *inode, struct file *filp) {

	/* Success */
	return 0;
}


int memory_release(struct inode *inode, struct file *filp) {

	/* Success */
	return 0;
}


ssize_t memory_read(struct file *filp, char __user *buf, 
		size_t count, loff_t *f_pos) 
{
	if (*f_pos > 0)
		return 0;
	if (count > strlen(memory_buffer))
		count = strlen(memory_buffer);
	copy_to_user(buf,memory_buffer,count);
	*f_pos = *f_pos + count;

	return count; 
}

ssize_t memory_write( struct file *filp, const char __user *buf,
		size_t count, loff_t *f_pos) 
{
	copy_from_user(memory_buffer, buf, count);
	return count;
}


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Sun, Sep 26, 2010 at 9:41 AM, Bond  wrote:

>
>
> On Sun, Sep 26, 2010 at 3:43 AM, Venkatram Tummala  > wrote:
>
>>
>>> Hey buddy, i took the module code in your first post & modified it to
>> make it work . I am attaching the code.
>>
> I am sure that it will not work the thing what I asked the question.
If you are to copy only 1 byte that even my code has been doing.
There is no improvement in your code.
You are also giving
memory_buffer = kmalloc(1, GFP_KERNEL);
 memset(memory_buffer, 0, 1);

but since you were smart not to paste the code as I did or rather say it was
my problem so I am trying to understand so no one is giving lecture to you
to read what malloc is or start making programs for insertion sort.
You people are free to kick me out of the list but make sure when some one
asks a question you do not give them fundas give them some thing which
actually works
your code does not work what in what I am trying to do.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
Your code
   /* Registering device */
result = register_chrdev(0, "bond", &bond_fops);
registers the device with major number 0.Is that possible ?

On Sun, Sep 26, 2010 at 10:16 AM, Bond  wrote:

>
>
> On Sun, Sep 26, 2010 at 3:43 AM, Venkatram Tummala  > wrote:
>
>> This will print the the last character of the string you  wrote.
>>
> If you would have read the thread from my first post printing the last
> character is not my objective  I am trying to print the complete string.
>
>>
>> As a side note, please spare us all with all this non-sense. We have
>> better things to do in our life. Please try your level best to understand
>> things before asking the kernel mailing list. If this BS continues, i am
>> afraid you will get yourself kicked out of the mailing list. Please do
>> yourself a favour by not posting such crap.
>>
>> Venkatram Tummala
>>
>> Please read the thread before giving a lecture on kernel newbies.
>
>


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Sun, Sep 26, 2010 at 3:43 AM, Venkatram Tummala
wrote:

> This will print the the last character of the string you  wrote.
>
If you would have read the thread from my first post printing the last
character is not my objective  I am trying to print the complete string.

>
> As a side note, please spare us all with all this non-sense. We have better
> things to do in our life. Please try your level best to understand things
> before asking the kernel mailing list. If this BS continues, i am afraid you
> will get yourself kicked out of the mailing list. Please do yourself a
> favour by not posting such crap.
>
> Venkatram Tummala
>
> Please read the thread before giving a lecture on kernel newbies.


Re: copy_to_user() and copy_from_user(): confusing code

2010-09-25 Thread Dave Hylands
Hi,

Replying to the list this time.

On Sat, Sep 25, 2010 at 10:22 PM, Bond  wrote:
>>> write(1, "abcde", 5)= 1

User mode tried to write 5 characters.
The driver only accepted one (which is evidenced by the return code
which is the number of characters which the driver accepted).

>>> write(1, "bcde", 4) = 1

So usermode then tried to write the remaining 4 characters.
The driver only accepted one.

>>> write(1, "cde", 3)  = 1

etc.

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: copy_to_user() and copy_from_user(): confusing code

2010-09-25 Thread Bond
On Sat, Sep 25, 2010 at 11:06 PM, Chetan Nanda wrote:

>
>
>
>
>
> as per my understanding (may be wrong), issue is not with the
> copy_to/from_user. May be with the return values from  bond_read/write
> function.
> Drivers read/write should return the number of bytes read or written.
>
Agreed that was a mistake in my code.

On Sat, Sep 25, 2010 at 7:34 PM, Bond  wrote:

> mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
> 0x7f2a2f03b000
>
>> write(1, "abcde", 5)= 1
>> write(1, "bcde", 4) = 1
>> write(1, "cde", 3)  = 1
>> write(1, "de", 2)   = 1
>> write(1, "e", 1)= 1
>> close(1)= 0
>> munmap(0x7f2a2f03b000, 4096)= 0
>> close(2)= 0
>> exit_group(0)   = ?
>>
>>
>> Can you say any thing about the strace log above as why did I got the
above output.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Sun, Sep 26, 2010 at 3:43 AM, Venkatram Tummala
wrote:

>
>> Hey buddy, i took the module code in your first post & modified it to make
> it work . I am attaching the code. It works on the latest kernel 2.6.35. If
> you are using any other kernel, you may have to change the file_operations
> function pointers (memory_read & memory_write signatures) by looking at
> struct file_operations in include/linux/fs.h. Rest assured, the code does
> exactly what you want it do.
>
I am also using 2.6.35 and it did not worked on my machine.

echo -n somehing > /dev/bond
where as
ls -l /dev/bond
crw-rw-rw- 1 root root 60, 0 2010-09-26 09:32 /dev/bond
exists.
I see you modified the read and write functions and added __user to
datatypes.
more over here
ssize_t memory_read(struct file *filp, char __user *buf,
size_t count, loff_t *f_pos)
{
if (*f_pos != 0)
return 0;
copy_to_user(buf,memory_buffer,1);
if(*f_pos == 0)
*f_pos = *f_pos + 1;

return 1;
}

you are always returning one byte on reading how can you say that the code
will write complete thing on successful call of memory_read
similarly on memory_write you are returning one byte where as I am trying to
attempt to write more than one byte at one time if I give a string
something it is 8 bytes so in that case memory_read will always return the
number of bytes as 1 which I do not want.
Since on my machine it gave me error so I am not in a position to verify
what you are saying.


Re: printk and \n

2010-09-25 Thread Dave Hylands
Hi guys,

On Sat, Sep 25, 2010 at 5:08 PM, Philip Downer wrote:
> javad karabi wrote:
>>
>> I am very curious about this.
>> Where exactly does the "hello" text go, before it is 'flushed' ?
>> I am thinking that it gets put in some other buffer, then 'flushing' the
>> buffer does something
>> to the effect of memcpy the buffer to the framebuffer?
>
> stdout is buffered, the '\n' newline character means that fflush is called
> on stdout and so the buffer is flushed. You can turn the buffer off using
> "setbuf(stdout, NULL);" and the output will immediately be displayed.

Actually, there are 3 buffering modes supported in userspace.

1 - unbuffered. Every character is flushed out immediately
2 - line buffered. Characters are not flushed until a newline is encountered
3 - fully buffererd. Characters are not flushed until the buffer is filled.

In cases 2 or 3, if you perform an input operation, then any
unbuffered characters will be flushed.

Here's some documentation:


-- 
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: printk and \n

2010-09-25 Thread Philip Downer

javad karabi wrote:

I am very curious about this.
Where exactly does the "hello" text go, before it is 'flushed' ?
I am thinking that it gets put in some other buffer, then 'flushing' the 
buffer does something

to the effect of memcpy the buffer to the framebuffer?


stdout is buffered, the '\n' newline character means that fflush is 
called on stdout and so the buffer is flushed. You can turn the buffer 
off using "setbuf(stdout, NULL);" and the output will immediately be 
displayed.


Phil

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: printk and \n

2010-09-25 Thread Philip Downer

mohit verma wrote:

can u please write a code to prove urself( in user space)?
coz till now what i have code ,i have never seen this.(  or may be not 
noticed this)


See this really simple bit of code below, it probably doesn't do what 
you expect.



#include 

int main(void)
{
int i;
for (i=0; i< 5; i++) {
printf("hello");
sleep(30);
}
printf("display output\n");
}


Phil

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: printk and \n

2010-09-25 Thread mohit verma
can u please write a code to prove urself( in user space)?
coz till now what i have code ,i have never seen this.(  or may be not
noticed this)


Re: character device driver reading only last character of buffer

2010-09-25 Thread Venkatram Tummala
On Sat, Sep 25, 2010 at 8:34 AM, Bond  wrote:

> Since you people think me to be a newbie or moron or some thing like copy
> pasting the code so I am explaining each and every line of it what it is
> doing but still lectures are lectures and funda's don't work show me real
> thing or else do not reply.
>
>
>
> #include 
>
> #include 
> #include  /* printk() */
> #include  /* kmalloc() */
> #include  /* everything... */
> #include  /* error codes */
> #include  /* size_t */
> #include 
> #include  /* O_ACCMODE */
> #include  /* cli(), *_flags */
> #include  /* copy_from/to_user */
>
> The section declared above is used to include header files.
>
>
> MODULE_LICENSE("Dual BSD/GPL");
>
> MODULE_LICENSE is an exported symbol.
>
> int bond_open(struct inode *inode, struct file *filp);
> int bond_release(struct inode *inode, struct file *filp);
> ssize_t bond_read(struct file *filp, char *buf, size_t count, loff_t
> *f_pos);
> ssize_t bond_write(struct file *filp, char *buf, size_t count, loff_t
> *f_pos);
>
> just above defined four functions which will be used in structure
> file_operations which is of type defined in fs.h
>
>
> void bond_exit(void);
> int bond_init(void);
>
> my init and exit modules of code.
>
> struct file_operations bond_fops = {
>   .read = bond_read,
>   .write = bond_write,
>   .open = bond_open,
>   .release = bond_release
> declared as above as per new C99 rules so that rest of the file_operations
> which are not defined be declared NULL
> };
>
> above  operations corresponding to the system calls an application can
> apply to a file : file operations as in fs.h
>
> module_init(bond_init);
> module_exit(bond_exit);
>
> my init and exit modules
>
> int bond_major = 60;
>
> there is another way alloc_chrdrv but I am using register_chrdrv so
> defining the major number
>
>
> char *bond_buffer;
>
> device buffer to store data when user programs access it
>
> int bond_init(void) {
>   int result;
>
>
>   result = register_chrdev(bond_major, "bond", &bond_fops);
>   if (result < 0) {
> printk(KERN_ALERT  "memory: cannot obtain major number %d\n",
> bond_major);
> return result;
>   }
>
>
>   bond_buffer = kmalloc(14, GFP_KERNEL);
> giving 14 bytes to buffer three types GFP_KERNEL,GFP_ATOMIC one more I
> forgot
>
>   if (!bond_buffer) {
> result = -ENOMEM;
> goto fail;
>   }
> in case of error above will stop execution
>
>   memset(bond_buffer, 0, 14);
>
> filling all the bytes of memory Claros pointed me here that if I ever used
> malloc function in my life so mentioning this was important for him.
>
>
>
>   printk(KERN_ALERT "Inserting bond module\n");
>   return 0;
>
>   fail:
> bond_exit();
> return result;
> same when returns a -ve value if fail to register the major number 60
> }
>
>
>
>
> void bond_exit(void) {
>
>   unregister_chrdev(bond_major, "bond");
>
>
>   if (bond_buffer) {
> kfree(bond_buffer);
>
>   }
>
>   printk( KERN_ALERT "Removing bond module\n");
>
> }
>
> just above is a clean up module when driver exits or unloads
>
>
> int bond_open(struct inode *inode, struct file *filp) {
>
>
>   return 0;
> }
>
> will be needed when a process opens the file
>
> int bond_release(struct inode *inode, struct file *filp) {
>
>
>   return 0;
> }
>
> when releasing device driver
>
> ssize_t bond_read(struct file *filp, char *buf,
> size_t count, loff_t *f_pos) {
>
>
>   copy_to_user(buf,bond_buffer,count<14 ? count:14);
> This function works as given here
> http://www.gnugeneration.com/mirrors/kernel-api/r4299.html
> and an example of this in the current kernel is
> linux-2.6/drivers/char/snsc.c
> they implemented a function scdrv_write I implemented bond_write both
> implementations are different
> do you people get that or still some lecture is missing.
> }
>
> ssize_t bond_write( struct file *filp, char *buf,
>   size_t count, loff_t *f_pos) {
>
>   copy_from_user(bond_buffer,buf,count<14 ? count : 14);
> from the userspace buf copy the bytes whose total number is equal to count
> to bond_buffer
>   return 1;
> }
>
> The above fundas and lectures do not work.You people give lectures or show
> attitude but only if the code works.
>

Hey buddy, i took the module code in your first post & modified it to make
it work . I am attaching the code. It works on the latest kernel 2.6.35. If
you are using any other kernel, you may have to change the file_operations
function pointers (memory_read & memory_write signatures) by looking at
struct file_operations in include/linux/fs.h. Rest assured, the code does
exactly what you want it do.

Do the following steps :

1) insmod 

2) cat /proc/devices |grep  This will fetch you a number.
Use it in step 3

3) mknod /dev/ c  0

4) echo  -n "whatever" > /dev/

5) cat /dev/

This will print the the last character of the string you  wrote.

As a side note, please spare us all with all this non-sense. We have better
things to do in our life. Please try your level best to understand thin

Re: Understanding Page Faults - find_get_page question

2010-09-25 Thread Mulyadi Santosa
Hi...



On Sat, Sep 25, 2010 at 06:51, moussa ba  wrote:
> The problem I am running into is that I see many instances where calls to
> find_get_page return NULL meaning the page could not be found in the page
> cache, however, they do not register as major page faults in the kernel (I
> instrumented filemap_fault in filemap.c) .  I do however see other major
> page fault events.

I am just thinking, perhaps you should hook into find_or_create_page()
too? check the related function comment. I think it make sense to
hook there too...considering you're tracking major page fault.

-- 
regards,

Mulyadi Santosa
Freelance Linux trainer and consultant

blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: copy_to_user() and copy_from_user(): confusing code

2010-09-25 Thread Chetan Nanda
On Sat, Sep 25, 2010 at 7:34 PM, Bond  wrote:

>
>
> On Fri, Sep 24, 2010 at 5:41 PM, sri  wrote:
>
>> what part is not able to understand?
>>
>> Here is a program I wrote
> but it is dropping characters though I gave 14 bytes to kmalloc and buffer
> but still the program is unable to read and write as I expect it to do.
> I did an strace on the program and have posted the log at the end
>
>
> #include 
>
> #include 
> #include  /* printk() */
> #include  /* kmalloc() */
> #include  /* everything... */
> #include  /* error codes */
> #include  /* size_t */
> #include 
> #include  /* O_ACCMODE */
> #include  /* cli(), *_flags */
> #include  /* copy_from/to_user */
>
> MODULE_LICENSE("Dual BSD/GPL");
>
>
> int bond_open(struct inode *inode, struct file *filp);
> int bond_release(struct inode *inode, struct file *filp);
> ssize_t bond_read(struct file *filp, char *buf, size_t count, loff_t
> *f_pos);
> ssize_t bond_write(struct file *filp, char *buf, size_t count, loff_t
> *f_pos);
> void bond_exit(void);
> int bond_init(void);
>
> struct file_operations bond_fops = {
>   read: bond_read,
>   write: bond_write,
>   open: bond_open,
>   release: bond_release
> };
>
> module_init(bond_init);
> module_exit(bond_exit);
>
> int bond_major = 60;
>
> char *bond_buffer;
>
> int bond_init(void) {
>   int result;
>
>
>   result = register_chrdev(bond_major, "bond", &bond_fops);
>   if (result < 0) {
> printk(KERN_ALERT  "memory: cannot obtain major number %d\n",
> bond_major);
> return result;
>   }
>
>
>   bond_buffer = kmalloc(14, GFP_KERNEL);
>   if (!bond_buffer) {
> result = -ENOMEM;
> goto fail;
>   }
>   memset(bond_buffer, 0, 14);
>
>   printk(KERN_ALERT "Inserting bond module\n");
>   return 0;
>
>   fail:
> bond_exit();
> return result;
> }
>
>
> void bond_exit(void) {
>
>   unregister_chrdev(bond_major, "bond");
>
>
>   if (bond_buffer) {
> kfree(bond_buffer);
>   }
>
>   printk( KERN_ALERT "Removing bond module\n");
>
> }
>
>
> int bond_open(struct inode *inode, struct file *filp) {
>
>
>   return 0;
> }
>
>
> int bond_release(struct inode *inode, struct file *filp) {
>
>
>   return 0;
> }
>
>
> ssize_t bond_read(struct file *filp, char *buf,
> size_t count, loff_t *f_pos) {
>
>
>   copy_to_user(buf,bond_buffer,count<14 ? count:14);
>
>
>  /* if (*f_pos == 0) {
> *f_pos+=1;
> return 1;
>   } else {
> return 0;
>   }*/
> }
>
> ssize_t bond_write( struct file *filp, char *buf,
>   size_t count, loff_t *f_pos) {
>
> //  char *tmp;
>
>   //tmp=buf+count-1;
>   copy_from_user(bond_buffer,buf,count<14 ? count : 14);
>   return 1;
> }
>
>
as per my understanding (may be wrong), issue is not with the
copy_to/from_user. May be with the return values from  bond_read/write
function.
Drivers read/write should return the number of bytes read or written.

Here is the Makefile
>
> ifeq ($(KERNELRELEASE),)
> KERNELDIR ?= /lib/modules/$(shell uname -r)/build
> PWD := $(shell pwd)
> .PHONY: build clean
> build:
> $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
> clean:
> rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c
>  rm -rf modules.order Module.symvers
> else
> $(info Building with KERNELRELEASE =${KERNELRELEASE})
> obj-m := bond.o
>
> endif
>
> and here is the strace
> mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
> 0x7f2a2f03b000
> write(1, "abcde", 5)= 1
> write(1, "bcde", 4) = 1
> write(1, "cde", 3)  = 1
> write(1, "de", 2)   = 1
> write(1, "e", 1)= 1
> close(1)= 0
> munmap(0x7f2a2f03b000, 4096)= 0
> close(2)= 0
> exit_group(0)   = ?
>
> so copy_from_user and copy_to_write are not doing what I expect them to do
> this is not clear to me.
>


Re: regarding synchronization code

2010-09-25 Thread Sri Ram Vemulpali
I am just saying I know inline keyword. But what is always_inline.

Thanks for replies and explanations. I got it now.

Regards,
Sri.

On Sat, Sep 25, 2010 at 4:48 AM, Michael Blizek
 wrote:
> Hi!
>
> On 18:56 Fri 24 Sep     , Sri Ram Vemulpali wrote:
>> Hi all,
>>
>>   I am encountering alot macros in the code. I did not understand what
>> those macro means.
>>
>>   Can anyone explain them and the use of them putting them like that.
>>
>>    "unlikely"
>
> likely() and unlikely() are wrappers around gcc extensions to give hints about
> whether a given branch will likely be taken or not. When done correctly, this
> can improve performance.
>
>>    "always_inline"  -- defined at the signature of the function.
>
> This can be used because "inline" is not always inlined. There should be a gcc
> option which causes all inline code to be not inlined.
>
>>    "inline" -- I know inline keyword in compiler is used to place the
>> code in to the caller function at the time of compiler, but why
>> declared as macro
>
> Where do you see inline declared as a macro?
>
>        -Michi
> --
> programing a layer 3+4 network protocol for mesh networks
> see http://michaelblizek.twilightparadox.com
>
>



-- 
Regards,
Sri.

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Sat, Sep 25, 2010 at 7:57 PM, ptchinster  wrote:

> Maybe all those people lecturing you have a point?
>
> Here is working code/makefile to do what i think you want to do. I
> just ran and tested it running 2.6.35.
>
> http://paste.pocoo.org/show/267125/
>
> A *basic* tactic a programmer can use is to get working code, compare
> it with his non working code, spot the differences in function calls,
> parameters, data structures, variable types, compiler flags, etc, and
> start reading documentation as to why those differences exist and what
> they are doing.
>
> It has been more than 15 days I am stuck up with this code.
>
If you want me to explain each and every line I can do here.
Let me know if you people are feeling I am a newbie.


Re: printk and \n

2010-09-25 Thread Manish Katiyar
On Sat, Sep 25, 2010 at 9:15 AM, mohit verma  wrote:
> hello guys,
> are there any gains ( in terms of security or anything else)  to flush the
> buffer in kernel printk() as soon as "\n" is  encountred ?
>  i am asking because this does not happen in the user space as all of us
> know.

This happens in userspace too.

>



-- 
Thanks -
Manish
==
[$\*.^ -- I miss being one of them
==

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: printk and \n

2010-09-25 Thread mohit verma
hello guys,
are there any gains ( in terms of security or anything else)  to flush the
buffer in kernel printk() as soon as "\n" is  encountred ?
 i am asking because this does not happen in the user space as all of us
know.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
Since you people think me to be a newbie or moron or some thing like copy
pasting the code so I am explaining each and every line of it what it is
doing but still lectures are lectures and funda's don't work show me real
thing or else do not reply.



#include 

#include 
#include  /* printk() */
#include  /* kmalloc() */
#include  /* everything... */
#include  /* error codes */
#include  /* size_t */
#include 
#include  /* O_ACCMODE */
#include  /* cli(), *_flags */
#include  /* copy_from/to_user */

The section declared above is used to include header files.

MODULE_LICENSE("Dual BSD/GPL");

MODULE_LICENSE is an exported symbol.

int bond_open(struct inode *inode, struct file *filp);
int bond_release(struct inode *inode, struct file *filp);
ssize_t bond_read(struct file *filp, char *buf, size_t count, loff_t
*f_pos);
ssize_t bond_write(struct file *filp, char *buf, size_t count, loff_t
*f_pos);

just above defined four functions which will be used in structure
file_operations which is of type defined in fs.h


void bond_exit(void);
int bond_init(void);

my init and exit modules of code.

struct file_operations bond_fops = {
  .read = bond_read,
  .write = bond_write,
  .open = bond_open,
  .release = bond_release
declared as above as per new C99 rules so that rest of the file_operations
which are not defined be declared NULL
};

above  operations corresponding to the system calls an application can apply
to a file : file operations as in fs.h

module_init(bond_init);
module_exit(bond_exit);

my init and exit modules

int bond_major = 60;

there is another way alloc_chrdrv but I am using register_chrdrv so defining
the major number


char *bond_buffer;

device buffer to store data when user programs access it

int bond_init(void) {
  int result;


  result = register_chrdev(bond_major, "bond", &bond_fops);
  if (result < 0) {
printk(KERN_ALERT  "memory: cannot obtain major number %d\n",
bond_major);
return result;
  }


  bond_buffer = kmalloc(14, GFP_KERNEL);
giving 14 bytes to buffer three types GFP_KERNEL,GFP_ATOMIC one more I
forgot

  if (!bond_buffer) {
result = -ENOMEM;
goto fail;
  }
in case of error above will stop execution

  memset(bond_buffer, 0, 14);

filling all the bytes of memory Claros pointed me here that if I ever used
malloc function in my life so mentioning this was important for him.


  printk(KERN_ALERT "Inserting bond module\n");
  return 0;

  fail:
bond_exit();
return result;
same when returns a -ve value if fail to register the major number 60
}




void bond_exit(void) {

  unregister_chrdev(bond_major, "bond");


  if (bond_buffer) {
kfree(bond_buffer);
  }

  printk( KERN_ALERT "Removing bond module\n");

}

just above is a clean up module when driver exits or unloads


int bond_open(struct inode *inode, struct file *filp) {


  return 0;
}

will be needed when a process opens the file

int bond_release(struct inode *inode, struct file *filp) {


  return 0;
}

when releasing device driver

ssize_t bond_read(struct file *filp, char *buf,
size_t count, loff_t *f_pos) {


  copy_to_user(buf,bond_buffer,count<14 ? count:14);
This function works as given here
http://www.gnugeneration.com/mirrors/kernel-api/r4299.html
and an example of this in the current kernel is
linux-2.6/drivers/char/snsc.c
they implemented a function scdrv_write I implemented bond_write both
implementations are different
do you people get that or still some lecture is missing.
}

ssize_t bond_write( struct file *filp, char *buf,
  size_t count, loff_t *f_pos) {

  copy_from_user(bond_buffer,buf,count<14 ? count : 14);
from the userspace buf copy the bytes whose total number is equal to count
to bond_buffer
  return 1;
}

The above fundas and lectures do not work.You people give lectures or show
attitude but only if the code works.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Sat, Sep 25, 2010 at 8:36 PM, ptchinster  wrote:

> Yup. Thank you for placing where i got it, i didn't save info when i
> saved this source.
>
> No need to say thanks I am used to lectures on this list.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
running an strace on the program gave me
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x7f2a2f03b000
write(1, "abcde", 5)= 1
write(1, "bcde", 4) = 1
write(1, "cde", 3)  = 1
write(1, "de", 2)   = 1
write(1, "e", 1)= 1
close(1)= 0
munmap(0x7f2a2f03b000, 4096)= 0
close(2)= 0
exit_group(0)   = ?

any suggestions here on the list are not working and people are giving me
lectures.
You people show me some real code rather than speaking non sense.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Sat, Sep 25, 2010 at 8:36 PM, ptchinster  wrote:

> Yup. Thank you for placing where i got it, i didn't save info when i
> saved this source.
>
> It didn't even compiled on my machine.


Re: copy_to_user() and copy_from_user(): confusing code

2010-09-25 Thread Bond
On Fri, Sep 24, 2010 at 5:41 PM, sri  wrote:

> what part is not able to understand?
>
> Here is a program I wrote
but it is dropping characters though I gave 14 bytes to kmalloc and buffer
but still the program is unable to read and write as I expect it to do.
I did an strace on the program and have posted the log at the end


#include 

#include 
#include  /* printk() */
#include  /* kmalloc() */
#include  /* everything... */
#include  /* error codes */
#include  /* size_t */
#include 
#include  /* O_ACCMODE */
#include  /* cli(), *_flags */
#include  /* copy_from/to_user */

MODULE_LICENSE("Dual BSD/GPL");


int bond_open(struct inode *inode, struct file *filp);
int bond_release(struct inode *inode, struct file *filp);
ssize_t bond_read(struct file *filp, char *buf, size_t count, loff_t
*f_pos);
ssize_t bond_write(struct file *filp, char *buf, size_t count, loff_t
*f_pos);
void bond_exit(void);
int bond_init(void);

struct file_operations bond_fops = {
  read: bond_read,
  write: bond_write,
  open: bond_open,
  release: bond_release
};

module_init(bond_init);
module_exit(bond_exit);

int bond_major = 60;

char *bond_buffer;

int bond_init(void) {
  int result;


  result = register_chrdev(bond_major, "bond", &bond_fops);
  if (result < 0) {
printk(KERN_ALERT  "memory: cannot obtain major number %d\n",
bond_major);
return result;
  }


  bond_buffer = kmalloc(14, GFP_KERNEL);
  if (!bond_buffer) {
result = -ENOMEM;
goto fail;
  }
  memset(bond_buffer, 0, 14);

  printk(KERN_ALERT "Inserting bond module\n");
  return 0;

  fail:
bond_exit();
return result;
}


void bond_exit(void) {

  unregister_chrdev(bond_major, "bond");


  if (bond_buffer) {
kfree(bond_buffer);
  }

  printk( KERN_ALERT "Removing bond module\n");

}


int bond_open(struct inode *inode, struct file *filp) {


  return 0;
}


int bond_release(struct inode *inode, struct file *filp) {


  return 0;
}


ssize_t bond_read(struct file *filp, char *buf,
size_t count, loff_t *f_pos) {


  copy_to_user(buf,bond_buffer,count<14 ? count:14);


 /* if (*f_pos == 0) {
*f_pos+=1;
return 1;
  } else {
return 0;
  }*/
}

ssize_t bond_write( struct file *filp, char *buf,
  size_t count, loff_t *f_pos) {

//  char *tmp;

  //tmp=buf+count-1;
  copy_from_user(bond_buffer,buf,count<14 ? count : 14);
  return 1;
}

Here is the Makefile

ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
.PHONY: build clean
build:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c
rm -rf modules.order Module.symvers
else
$(info Building with KERNELRELEASE =${KERNELRELEASE})
obj-m := bond.o

endif

and here is the strace
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =
0x7f2a2f03b000
write(1, "abcde", 5)= 1
write(1, "bcde", 4) = 1
write(1, "cde", 3)  = 1
write(1, "de", 2)   = 1
write(1, "e", 1)= 1
close(1)= 0
munmap(0x7f2a2f03b000, 4096)= 0
close(2)= 0
exit_group(0)   = ?

so copy_from_user and copy_to_write are not doing what I expect them to do
this is not clear to me.


Re: character device driver reading only last character of buffer

2010-09-25 Thread Bond
On Fri, Sep 24, 2010 at 10:51 PM, Mulyadi Santosa  wrote:

> When a senior member like Greg Freemyer respond like that, it must be
>
Rather than giving me lecture here show me how this error can be fixed and
let him prove if he deserves respect I do not have any problem in that,.


Re: regarding synchronization code

2010-09-25 Thread Michael Blizek
Hi!

On 18:56 Fri 24 Sep , Sri Ram Vemulpali wrote:
> Hi all,
> 
>   I am encountering alot macros in the code. I did not understand what
> those macro means.
> 
>   Can anyone explain them and the use of them putting them like that.
> 
>"unlikely"

likely() and unlikely() are wrappers around gcc extensions to give hints about
whether a given branch will likely be taken or not. When done correctly, this
can improve performance.

>"always_inline"  -- defined at the signature of the function.

This can be used because "inline" is not always inlined. There should be a gcc
option which causes all inline code to be not inlined.

>"inline" -- I know inline keyword in compiler is used to place the
> code in to the caller function at the time of compiler, but why
> declared as macro

Where do you see inline declared as a macro?

-Michi
-- 
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com


--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ