LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-02-2009, 06:36 PM   #1
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Rep: Reputation: 22
Function that only works when casting types?


Hey all,

I have been working on this function that I could only get to work by casting the types when assigning values to a structure pointer. I have other function that work fine without type casting, and even fail when I try to type cast them.

Is there any reason that this function would require the type to be cast in order to work? When I tried just simply assigning them with "currentsession->field = 0". I would get a kernel panic.

Code:
static void clearsession(struct index *currentindex){
	struct session *currentsession = NULL;

	/* Clear this session. */
	
	if (currentindex != NULL){
		spin_lock(&currentindex->lock);
		currentindex->used = (__u8)0;
		currentsession = currentindex->slot;
		
		if (currentsession != NULL){
			spin_lock(&currentsession->lock);
			currentsession->largerIP = (__u32)0;
			currentsession->largerIPPort = (__u16)0;
			currentsession->smallerIP = (__u32)0;
			currentsession->smallerIPPort = (__u16)0;
			currentsession->local = (__u32)0;
			currentsession->remote = (__u32)0;
			currentsession->state = (__u8)0;
			spin_unlock(&currentsession->lock);
		}
		spin_unlock(&currentindex->lock);
	}

}
 
Old 11-02-2009, 06:45 PM   #2
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 3,837

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
It is surprising that those casts even change the generated code. It is so surprising that I think looking at the change in the generated code might provide some insight into the nature of the failure.

Can you generate .s files (compiling with -S in gcc) with and without those casts, and find the difference in the generated code?
 
Old 11-02-2009, 07:24 PM   #3
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Original Poster
Rep: Reputation: 22
Well this is a kernel module, and I have not been able to compile directly with gcc, but am using a makefile to compile it.
 
Old 11-02-2009, 08:45 PM   #4
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Original Poster
Rep: Reputation: 22
I was able to compile them using gcc into h.gch files, and compare them. They are definatly different. I cannot upload them here, but I uploaded them to my skydrive if you want to see them.

http://cid-c52d2fb289c57329.skydrive...ger.h.gch?uc=2
 
Old 11-02-2009, 10:33 PM   #5
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 3,837

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
Sorry, I have no idea how to learn anything from a .gch file. The fact that the partially compiled code is different in that form does not mean that the fully compiled code would be different in the final machine code nor even in assembler.
 
Old 11-03-2009, 12:27 AM   #6
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Original Poster
Rep: Reputation: 22
Here is the entire LKM. So you can see all the code.
http://bbfdnq.blu.livefilestore.com/...7.zip?download
 
Old 11-03-2009, 09:49 AM   #7
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 3,837

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
I haven't done any Linux kernel debugging other than one small project so long ago that even if I remembered how, it wouldn't remain relevant.

So I lack the time and probably the expertise to do anything with your code. I also doubt others reading this thread have the time.

Regarding your first post in this thread, I'm pretty sure those casts are not directly relevant to the problem that causes your kernel panic.

If you have some random memory clobber and/or error in allocating, deallocating or mapping the memory, it is very possible for "stirring the pot" to move the symptoms around. That means you might make some change that is not related to the bug but will move or hide the symptoms.

I think those casts are just stirring the pot (not somehow fixing some bug). As I said before, I don't really understand how they are even stirring the pot. I would have expected the compiler to filter out all effects of that code change. So if you could post the before and after versions of that chunk of assembler code, I might give you some insight about it. But I'm not going to try to generate that before and after assembler code myself. Even if I wanted to spend the time, there are too many other variables between what you did and what I might do.

It looks like you were getting better help in your related previous thread
http://www.linuxquestions.org/questi...-array-760651/

But maybe you didn't really solve that problem, just moved the symptoms. I can't help noticing the similarity between the kind of problem you have in this thread and the kind of problem you would have if you hadn't gotten the solution to that thread quite right.
 
Old 11-03-2009, 03:37 PM   #8
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Original Poster
Rep: Reputation: 22
The previous thread had to do with allocating memory. I found that the method I was trying to use just would not work in my module because the functions I am writing are executed in an interrupt context, and there are some limitations of what can be done in that context. Primarily nothing can sleep. Sleeping in an interrupt context will cause the kernel to panic.

I re-wrote the method I was storing data to use a linked list rather than an array. This allows me to get the large quantity of memory I need using kmalloc()/kcalloc() atomically so its compatible with running in an interrupt context.

I have never used gcc to create assembler code. I will try to figure that out for a kernel module if that would help debugging. I thought that if it were trying to save "0" as a 32-bit or 64-bit version of "0" to some of the fields in the structure I am using it might be able to clobber memory next to the memory assigned to the structure. I am not sure if thats a possibility or not though.
 
Old 11-19-2009, 06:35 PM   #9
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu
Posts: 116
Blog Entries: 1

Original Poster
Rep: Reputation: 22
I found some other problems, and after getting them fixed this problem went away. It was definatly a case of "stiring the pot". Thanks for the help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid operands of types ‘<unresolved overloaded function type>’ and ‘double’ to jus71n742 Programming 2 01-05-2009 01:27 AM
returning different types of variables from a function knobby67 Programming 3 02-08-2008 09:53 PM
Casting Problem - Compiling code with g++ that works for gcc Division_By_Zero Programming 1 06-11-2007 07:56 AM
C Enumerated Types / Function Pointer Errors Centinul Programming 8 11-07-2006 08:46 PM
warning "conflicting types for buit-in function" while cross-compiling cheema Programming 0 07-07-2004 04:02 AM


All times are GMT -5. The time now is 12:04 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration