cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
joancab
Advisor
Advisor
373 Views
Registered: ‎05-11-2015

Segfault with Linux because of data size

I have a Linux app that processes large chunks of data coming from the PL. The Linux system is almost minimal as well as the app. With a data size of 1 MB, everything runs fine. If I try 16 MB it gives me a segmentation fault right at the beginning of the app, and I would say before executing any line. My board has 4 GB RAM and I guess Linux itself uses 1 GB at the most, so there should be 3 GB free, so what could be wrong?

0 Kudos
10 Replies
rfs613
Scholar
Scholar
343 Views
Registered: ‎05-28-2013

Is there any chance that your data size units are actually GB rather than MB? Are you allocating this statically (global variables) or dynamically (runtime malloc() or similar)?

I would suggest adding printf("hello word\n"); fflush(stdout); directly at the top of your app, just to be able to confirm if it gets that far. You could also try running the app under strace, to see where it fails.

0 Kudos
joancab
Advisor
Advisor
340 Views
Registered: ‎05-11-2015

My data size is a number (of bytes), no units.

What I have is the PL writing in memory, then I map that area and use that data in user space.

I do copy (memcpy) from the mapped area to a global array (of that size) before further processing (fwrite to a file). I wonder:

a) if I do need to memcpy that data as mmap gives me a pointer to it

b) if such a large array, char[16000000] could be the problem

When I set the size to 16,000,000 (bytes), I don't even get the 'hello world' that I already have at the beginning, but the same software with a size of 1,000,000 does work.

0 Kudos
rfs613
Scholar
Scholar
327 Views
Registered: ‎05-28-2013

You don't really need to do the memcpy() in this case. However, it should not cause a segfault error.

How large is the mapping area of your PL? Maybe the memcpy() is reading past the end, and that is why you are getting a segfault?

There is no problem allocating 16M data and using it:

#include <stdio.h>
#include <string.h>

char buf[16000000];

int main(int argc, char **argv)
{
	memset(buf, 0xff, sizeof(buf));
	return 0;
}

 You can use "size" command to confirm that this executable has a large data section:

$ size test16M
   text	   data	    bss	    dec	    hex	filename
    760	    240	16000028	16001028	 f42804	test16M

This runs fine on my system (which only has 256MB of RAM).

joancab
Advisor
Advisor
317 Views
Registered: ‎05-11-2015

How large is the mapping area of your PL?

The PL writes alternatively to two neighbour areas of 16 MB, but eventually it will have to be 512 MB each = 1 GB.

So far I'm writing (the PL is) to the top 256 MB of the 4 GB present (0xF000_0000 .. 0xFFFF_FFFF)

I haven't done anything special with my linux to reserve that memory, I wonder if I should.

 

0 Kudos
rfs613
Scholar
Scholar
311 Views
Registered: ‎05-28-2013

> The PL writes alternatively to two neighbour areas of 16 MB, but eventually it will have to be 512 MB each = 1 GB.

> So far I'm writing (the PL is) to the top 256 MB of the 4 GB present (0xF000_0000 .. 0xFFFF_FFFF)

What I meant is, how are you mapping this region so that Linux can access it? Using mmap() on /dev/mem perhaps? Or UIO? In either case, check that the length of your mapping is sufficient to access all 32MB (and later the whole 1G).

> I haven't done anything special with my linux to reserve that memory, I wonder if I should.

Indeed you should. Eventually Linux will decide to use that memory for something (code, data, cache, etc). At which point things will go quite bad...

You can try the old trick of passing mem=NNN on the kernel commandline, where NNN is smaller than the total RAM. Or the more modern method using CMA (Contiguous Memory Allocator).

0 Kudos
joancab
Advisor
Advisor
256 Views
Registered: ‎05-11-2015

I'm more and more confused. My board has 4 GB of PS RAM but the maximum I can set in the petalinux config is 2 GB (up to 0x8000_0000), is the upper 2 GB accessible at all?

 I managed to work with up to 256 and 512 MB chunks by limiting petalinux memory to 1 GB (to 0x4000_0000) in petalinux-config and using 0x4000_0000 upwards. With 512 MB I got errors that I believe are because timing, not size.

0 Kudos
rfs613
Scholar
Scholar
239 Views
Registered: ‎05-28-2013

Some background that might be useful: https://elinux.org/images/4/4c/Ott.pdf

If you are on a 32-bit processor, then the total address space of the CPU is 4G. Obviously this cannot all be RAM, since all the other stuff (memory-mapped peripherals etc) must go somewhere. A common arrangement is 2G/2G split (CONFIG_VMSPLIT_2G) meaning that userspace gets (up to) 2G of space, and the remaining 2G are used by kernel for everything else. See the kernel config: https://github.com/torvalds/linux/blob/master/arch/arm/Kconfig#L1259

If you want to use more than 2 or 3G on a 32-bit system, some kind of paging scheme is needed, on ARM this is called LPAE. Check that you have this enabled in your kernel configuraiton. For more info see https://elinux.org/images/6/6a/Elce11_marinas.pdf

Finally, on 64-bit CPUs the situation is again different - the the limit is much much higher, so I suspect you are not on a 64-bit system

joancab
Advisor
Advisor
232 Views
Registered: ‎05-11-2015

"The ARM A53 CPUs of MPSOC are 64 bits by default. The ZCU102 board has 4 GB of DDR but only 2 GB is in the lower 32 bit address range and the other 2 GB is in the 64 bit address range. Linux uses all the memory such that 64 addresses are required for PL Masters. At the time of the 2017.4 release, 64 bit addressing is not the default in the system with PL Masters. IP cores such as AXI DMA default to 32 bit addressing"

(https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18842169/Zynq+UltraScale+PL+Masters?showComments=true&showCommentArea=true)

The High Address bit in the PS configuration is gone for my version (3.3 in Vivado 2020.2) but in the DDR Configuration tab it appears as 4 GB

0 Kudos
rfs613
Scholar
Scholar
230 Views
Registered: ‎05-28-2013

Sounds like you might need to visit the Vitis/Vivado area of the forum... afraid I can't help much on the configuration of the IP cores.

0 Kudos
joancab
Advisor
Advisor
209 Views
Registered: ‎05-11-2015

Not much problem with that, is linux what kills me. 

0 Kudos