Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
* Trouble with allocating memory on platform driver
@ 2020-04-04 15:21 s.achterop
  2020-04-15  9:29 ` s.achterop
  0 siblings, 1 reply; 4+ messages in thread
From: s.achterop @ 2020-04-04 15:21 UTC (permalink / raw)
  To: kernelnewbies

Hello list,

Trying to create a device driver for a raspberry pi zero (BCM2835) to control a gpio and an spi device.
This eventually will be done in a FIQ interrupt handler.
A simplified version of this batradio-driver can be found at:
  https://github.com/SietseAchterop/Batradio/blob/master/batradio_module/alloctest.c

The problem is that modprobe-ing this driver directly yields the following in dmesg.

Apr  4 16:50:23 raspberrypi kernel: [  139.636850] batradio: loading out-of-tree module taints kernel.
Apr  4 16:50:23 raspberrypi kernel: [  139.653580] ------------[ cut here ]------------
Apr  4 16:50:23 raspberrypi kernel: [  139.653650] WARNING: CPU: 0 PID: 1208 at include/linux/dma-mapping.h:516 init_module+0x164/0x1e0 [batradio]
Apr  4 16:50:23 raspberrypi kernel: [  139.653664] Modules linked in: batradio(O+) fuse rfcomm cmac bnep hci_uart btbcm serdev bluetooth ecdh_generic 8021q garp stp llc sg uas brcmfmac brcmutil sha256_generic cfg80211 rfkill snd_bcm2835(C) raspberrypi_hwmon snd_pcm hwmon snd_timer snd 
bcm2835_v4l2(C) bcm2835_codec(C) v4l2_mem2mem bcm2835_mmal_vchiq(C) v4l2_common videobuf2_vmalloc videobuf2_dma_contig videobuf2_memops videobuf2_v4l2 videobuf2_common videodev media vc_sm_cma(C) fixed uio_pdrv_genirq uio i2c_dev ip_tables x_tables ipv6
Apr  4 16:50:23 raspberrypi kernel: [  139.653835] CPU: 0 PID: 1208 Comm: modprobe Tainted: G         C O      4.19.108+ #1
Apr  4 16:50:23 raspberrypi kernel: [  139.653844] Hardware name: BCM2835
Apr  4 16:50:23 raspberrypi kernel: [  139.653897] [<c00184f8>] (unwind_backtrace) from [<c0015064>] (show_stack+0x20/0x24)
Apr  4 16:50:23 raspberrypi kernel: [  139.653931] [<c0015064>] (show_stack) from [<c0753394>] (dump_stack+0x20/0x28)
Apr  4 16:50:23 raspberrypi kernel: [  139.653970] [<c0753394>] (dump_stack) from [<c0025b58>] (__warn.part.3+0xb8/0xe0)
Apr  4 16:50:23 raspberrypi kernel: [  139.653998] [<c0025b58>] (__warn.part.3) from [<c0025cf8>] (warn_slowpath_null+0x50/0x5c)
Apr  4 16:50:23 raspberrypi kernel: [  139.654037] [<c0025cf8>] (warn_slowpath_null) from [<bf698164>] (init_module+0x164/0x1e0 [batradio])
Apr  4 16:50:23 raspberrypi kernel: [  139.654110] [<bf698164>] (init_module [batradio]) from [<c000ae8c>] (do_one_initcall+0x4c/0x234)
Apr  4 16:50:23 raspberrypi kernel: [  139.654151] [<c000ae8c>] (do_one_initcall) from [<c00a7898>] (do_init_module+0x6c/0x1f8)
Apr  4 16:50:23 raspberrypi kernel: [  139.654181] [<c00a7898>] (do_init_module) from [<c00a9cdc>] (load_module+0x21ec/0x24cc)
Apr  4 16:50:23 raspberrypi kernel: [  139.654209] [<c00a9cdc>] (load_module) from [<c00aa1f8>] (sys_finit_module+0xcc/0xec)
Apr  4 16:50:23 raspberrypi kernel: [  139.654232] [<c00aa1f8>] (sys_finit_module) from [<c0009000>] (ret_fast_syscall+0x0/0x28)
Apr  4 16:50:23 raspberrypi kernel: [  139.654244] Exception stack(0xcc037fa8 to 0xcc037ff0)
Apr  4 16:50:23 raspberrypi kernel: [  139.654261] 7fa0:                   93da3c00 00401760 00000003 0002d064 00000000 0002ec3c
Apr  4 16:50:23 raspberrypi kernel: [  139.654279] 7fc0: 93da3c00 00401760 00000000 0000017b 00402f10 00000000 00402e68 00000000
Apr  4 16:50:23 raspberrypi kernel: [  139.654294] 7fe0: bed56338 bed56328 00022cb8 b6c26af0
Apr  4 16:50:23 raspberrypi kernel: [  139.654307] ---[ end trace deb280ac68ce7734 ]---
Apr  4 16:50:23 raspberrypi kernel: [  139.654330] platform batradio__.0: coherent DMA mask is unset
Apr  4 16:50:23 raspberrypi kernel: [  139.654344] platform batradio__.0: Couldn't allocate memory!

The crash is at the very beginning of the init function of the module, here is the first part of that function:

   pdev = platform_device_register_simple("batradio__", 0, NULL, 0);
   if (IS_ERR(pdev))
     return PTR_ERR(pdev);
	
   batradio_data = devm_kzalloc(&pdev->dev,
			       sizeof(*batradio_data),
			       GFP_KERNEL);
   if (!batradio_data)
     return -ENOMEM;

   batradio_data->fiq_base = dma_zalloc_coherent(&pdev->dev,
						FIQ_BUFFER_SIZE,
						&batradio_data->dma_handle,
						GFP_KERNEL);
   if (!batradio_data->fiq_base) {
     dev_err(&pdev->dev, "Couldn't allocate memory!\n");
     return -ENOMEM;
   }

Note that the crash seems to occur because of the call to dma_zalloc_coherent.
The allocation does not work, FIQ_BUFFER_SIZE is 256*1024. and this is directly after reboot.
Also note that I start the rpizero only with the console, so no X. And login with ssh.

Why is this happening? What does "coherent DMA mask is unset" means.
I do not intend to use dma, but I understood that allocating in this way is what I should do if I want to use this data also from my FIQ handler.

Does it has to do with the use of platform_device_register_simple? Maybe the device structure isn't initialized properly for this?
I actually use this function because I don't know which compatible value I have to use when using the devicetree, everything I tried failed.

I also read the following in platform_device.h about platform_device_register_simple:
       In particular, when such drivers are built as modules, they can't be "hotplugged".
Is this relevant. is using modprobe meant by this?

   Thanks in advance,
       Sietse


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

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

* Re: Trouble with allocating memory on platform driver
  2020-04-04 15:21 Trouble with allocating memory on platform driver s.achterop
@ 2020-04-15  9:29 ` s.achterop
  2020-04-17  8:47   ` s.achterop
  0 siblings, 1 reply; 4+ messages in thread
From: s.achterop @ 2020-04-15  9:29 UTC (permalink / raw)
  To: kernelnewbies

On 4/4/20 5:21 PM, s.achterop@rug.nl wrote:
> Hello list,
> 
> Trying to create a device driver for a raspberry pi zero (BCM2835) to control a gpio and an spi device.
> This eventually will be done in a FIQ interrupt handler.
> A simplified version of this batradio-driver can be found at:
>   https://github.com/SietseAchterop/Batradio/blob/master/batradio_module/alloctest.c

    Regrettably no answers until now.
I only can add that the same code fragments are used in an example that I found on
      https://bootlin.com/blog/fiq-handlers-in-the-arm-linux-kernel/
where it is contained in the probe function of the platform driver.

Is there maybe a better place to ask for help here?
     Thanks,
        Sietse


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

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

* Re: Trouble with allocating memory on platform driver
  2020-04-15  9:29 ` s.achterop
@ 2020-04-17  8:47   ` s.achterop
  2020-04-17 19:30     ` Valdis Klētnieks
  0 siblings, 1 reply; 4+ messages in thread
From: s.achterop @ 2020-04-17  8:47 UTC (permalink / raw)
  To: kernelnewbies

On 4/15/20 11:29 AM, s.achterop@rug.nl wrote:
> On 4/4/20 5:21 PM, s.achterop@rug.nl wrote:
>> Hello list,
>>
>> Trying to create a device driver for a raspberry pi zero (BCM2835) to control a gpio and an spi device.
>> This eventually will be done in a FIQ interrupt handler.
>> A simplified version of this batradio-driver can be found at:
>>   https://github.com/SietseAchterop/Batradio/blob/master/batradio_module/alloctest.c

     I found a working solution. I actually don't know what I am doing but it works.
Before the call to dma_zalloc_coherent I added:

   pdev->dev.coherent_dma_mask = 0xffffffff;

Now it works.

    Regards,
       Sietse

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

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

* Re: Trouble with allocating memory on platform driver
  2020-04-17  8:47   ` s.achterop
@ 2020-04-17 19:30     ` Valdis Klētnieks
  0 siblings, 0 replies; 4+ messages in thread
From: Valdis Klētnieks @ 2020-04-17 19:30 UTC (permalink / raw)
  To: s.achterop; +Cc: kernelnewbies

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

On Fri, 17 Apr 2020 10:47:33 +0200, "s.achterop@rug.nl" said:

>      I found a working solution. I actually don't know what I am doing but it works.
> Before the call to dma_zalloc_coherent I added:
>
>    pdev->dev.coherent_dma_mask = 0xffffffff;
DMA_BIT_MASK(32) rather than a hex constant would be a better choice.

>
> Now it works.

Note that "works" and "is correct" are two different things.  That should be
a mask that tells how many bits of the value are actually used by the hardware.
On most 32-bit systems, 32 will be correct, but there's other values in use
(on S/390, it's either 31 or 64 bits, the PowerPC can have 44, and the old PNP
stuff on x86 weirds out at the 24-bit line).


[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 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

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-04 15:21 Trouble with allocating memory on platform driver s.achterop
2020-04-15  9:29 ` s.achterop
2020-04-17  8:47   ` s.achterop
2020-04-17 19:30     ` Valdis Klētnieks

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git