kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* kmalloc - Crashing
@ 2021-08-18 11:33 Lloyd
  2021-08-18 12:08 ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: Lloyd @ 2021-08-18 11:33 UTC (permalink / raw)
  To: kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 509 bytes --]

Hi,

I am starting to write linux kernel mode code. I am on Ubuntu 20.04. I have
written the following code

The line   DynPage[i]=0; //CRASH  causes a crash. Can you please guide on
the reason for the crash?

Thanks a lot in advance,
  Lloyd

#pragma GCC push_options
#pragma GCC optimize("O0")
void ClearMem(void)
{
unsigned int* DynPage=NULL;
unsigned int i=0;
DynPage=kmalloc(1024*16,GFP_KERNEL);
if(DynPage!=NULL)
{
DynPage[i]=0; //CRASH
}
kfree(DynPage);
DynPage=NULL;
return;
}
#pragma GCC pop_options

[-- Attachment #1.2: Type: text/html, Size: 3695 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: kmalloc - Crashing
  2021-08-18 11:33 kmalloc - Crashing Lloyd
@ 2021-08-18 12:08 ` Greg KH
  2021-08-18 12:37   ` Lloyd
  0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2021-08-18 12:08 UTC (permalink / raw)
  To: Lloyd; +Cc: kernelnewbies

On Wed, Aug 18, 2021 at 05:03:07PM +0530, Lloyd wrote:
> Hi,
> 
> I am starting to write linux kernel mode code. I am on Ubuntu 20.04. I have
> written the following code
> 
> The line   DynPage[i]=0; //CRASH  causes a crash. Can you please guide on
> the reason for the crash?
> 
> Thanks a lot in advance,
>   Lloyd
> 
> #pragma GCC push_options
> #pragma GCC optimize("O0")

Why are you using these options?  Try removing them and see what
happens.


_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: kmalloc - Crashing
  2021-08-18 12:08 ` Greg KH
@ 2021-08-18 12:37   ` Lloyd
  2021-08-18 12:45     ` Greg KH
  0 siblings, 1 reply; 4+ messages in thread
From: Lloyd @ 2021-08-18 12:37 UTC (permalink / raw)
  To: kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 1004 bytes --]

On Wed, Aug 18, 2021 at 5:38 PM Greg KH <greg@kroah.com> wrote:

> On Wed, Aug 18, 2021 at 05:03:07PM +0530, Lloyd wrote:
> > Hi,
> >
> > I am starting to write linux kernel mode code. I am on Ubuntu 20.04. I
> have
> > written the following code
> >
> > The line   DynPage[i]=0; //CRASH  causes a crash. Can you please guide on
> > the reason for the crash?
> >
> > Thanks a lot in advance,
> >   Lloyd
> >
> > #pragma GCC push_options
> > #pragma GCC optimize("O0")
>
> Why are you using these options?  Try removing them and see what
> happens.
>
>
Thanks a lot Greg. Yes, the code works now.

The assignment inside the for loop (DynPage[i]=0) is not used
anywhere below. I thought that, when the compiler optimizes the code, it
can remove that line. I am writing a small benchmarking application. So, I
don't want that line to be removed by the compiler. Is there a better way
to achieve this?

I am also surprised to see that disabling compiler optimization for
a function caused my code to crash!!

[-- Attachment #1.2: Type: text/html, Size: 1526 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: kmalloc - Crashing
  2021-08-18 12:37   ` Lloyd
@ 2021-08-18 12:45     ` Greg KH
  0 siblings, 0 replies; 4+ messages in thread
From: Greg KH @ 2021-08-18 12:45 UTC (permalink / raw)
  To: Lloyd; +Cc: kernelnewbies

On Wed, Aug 18, 2021 at 06:07:26PM +0530, Lloyd wrote:
> On Wed, Aug 18, 2021 at 5:38 PM Greg KH <greg@kroah.com> wrote:
> 
> > On Wed, Aug 18, 2021 at 05:03:07PM +0530, Lloyd wrote:
> > > Hi,
> > >
> > > I am starting to write linux kernel mode code. I am on Ubuntu 20.04. I
> > have
> > > written the following code
> > >
> > > The line   DynPage[i]=0; //CRASH  causes a crash. Can you please guide on
> > > the reason for the crash?
> > >
> > > Thanks a lot in advance,
> > >   Lloyd
> > >
> > > #pragma GCC push_options
> > > #pragma GCC optimize("O0")
> >
> > Why are you using these options?  Try removing them and see what
> > happens.
> >
> >
> Thanks a lot Greg. Yes, the code works now.
> 
> The assignment inside the for loop (DynPage[i]=0) is not used
> anywhere below. I thought that, when the compiler optimizes the code, it
> can remove that line. I am writing a small benchmarking application. So, I
> don't want that line to be removed by the compiler. Is there a better way
> to achieve this?
> 
> I am also surprised to see that disabling compiler optimization for
> a function caused my code to crash!!

Be careful, and do not modify any of the existing compiler options that
the kernel uses, as they are set very specifically so that stuff like
this will not crash.

And writing benchmarks is an art, be careful to determine what you are
trying to test, and to see if what you are writing actually tests that.

good luck!

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-18 12:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-18 11:33 kmalloc - Crashing Lloyd
2021-08-18 12:08 ` Greg KH
2021-08-18 12:37   ` Lloyd
2021-08-18 12:45     ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).