5,594,564 Members 32,795 Now Online
XDA Developers Android and Mobile Development Forum

Temporary solution for camera crash on KitKat ROMs (might fix other issues).

Tip us?
 
Diffeomorphism
Old
(Last edited by Diffeomorphism; 22nd December 2013 at 06:58 AM.)
#1  
Junior Member - OP
Thanks Meter 2
Posts: 4
Join Date: Jul 2013
Default Temporary solution for camera crash on KitKat ROMs (might fix other issues).

I will start with the solution and then follow it up with a technical analysis of the problem.

To the KitKat ROM maintainers here, @jamal2367 , @janjan , here please add the following to /system/etc/sysctl.conf:
Code:
vm.min_free_kbytes=32768
To be clear this will set a low watermark of 32MB of free memory. The kernel will attempt to reclaim memory as soon as the system dips below 32MB of free memory.

Now on to the technical analysis. So it looks like all of the crashes I experience on the KitKat ROMs are triggered by the ION memory allocator.

Code:
<4>[   78.327331] mediaserver: page allocation failure: order:0, mode:0xe0
...
<4>[   78.327484] [<c02da24c>] (sg_alloc_table+0x1c/0x44) from [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c)
<4>[   78.327484] [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c) from [<c03a02e8>] (ion_alloc+0xd4/0x354)
<4>[   78.327514] [<c03a02e8>] (ion_alloc+0xd4/0x354) from [<c03a0af0>] (ion_ioctl+0x108/0x434)
...

Code:
<4>[   84.951995] Binder_4: page allocation failure: order:0, mode:0xe0
...
<4>[   84.952117] [<c02da24c>] (sg_alloc_table+0x1c/0x44) from [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c)
<4>[   84.952148] [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c) from [<c03a02e8>] (ion_alloc+0xd4/0x354)
<4>[   84.952148] [<c03a02e8>] (ion_alloc+0xd4/0x354) from [<c03a0af0>] (ion_ioctl+0x108/0x434)
...
The key here is the mode of allocation. Notice that it is 0xe0. The mode corresponds to the gfp_mask of the allocation. A reference of the GFP flags can be found in the Linux kernel source: /source/include/linux/gfp.h

So we see that 0xe0 = 0x20 | 0x40 | 0x80, translating that to the corresponding GFP flags we see that it is
Code:
0xe0 = ___GFP_HIGH |  ___GFP_IO  | ___GFP_FS
Notice that ___GFP_HIGH is set and not ___GFP_WAIT. This means that this type of allocation will not wait for reclaimable memory to be reclaimed. In particular this means that if most of the memory is occupied by cached pages or VFS cache the allocation will fail. One way around this problem is to simply increase the minimum amount of memory the kernel will attempt to keep available. That's what the above suggested fix does. It certainly seems to solve the problem as the free memory never dips below the point crashes begin to occur when starting the camera. However, a better solution might be to fix the kernel code leading up to the allocation. I believe if @faux123 adds the latest version of scatterlist.c to his kernel repo it may solve the problem as the lines modifying the GFP mask which is originally a standard GFP_KERNEL to the "non-waiting" kind is no longer present.

The problematic code in __sg_alloc_table is
Code:
int __sg_alloc_table(struct sg_table *table, unsigned int nents,
                     unsigned int max_ents, gfp_t gfp_mask,
                     sg_alloc_fn *alloc_fn)
{
...
                gfp_mask &= ~__GFP_WAIT;
                gfp_mask |= __GFP_HIGH;
...
}
Perhaps I'll fork the repo and try to apply the fix myself later.
The Following 2 Users Say Thank You to Diffeomorphism For This Useful Post: [ Click to Expand ]
 
jamal2367
Old
#2  
jamal2367's Avatar
Senior Member
Thanks Meter 4691
Posts: 2,248
Join Date: Oct 2010
Quote:
Originally Posted by Diffeomorphism View Post
I will start with the solution and then follow it up with a technical analysis of the problem.

To the KitKat ROM maintainers here, @jamal2367 , @janjan , here please add the following to /system/etc/sysctl.conf:
Code:
vm.min_free_kbytes=32768
To be clear this will set a low watermark of 32MB of free memory. The kernel will attempt to reclaim memory as soon as the system dips below 32MB of free memory.

Now on to the technical analysis. So it looks like all of the crashes I experience on the KitKat ROMs are triggered by the ION memory allocator.

Code:
<4>[   78.327331] mediaserver: page allocation failure: order:0, mode:0xe0
...
<4>[   78.327484] [<c02da24c>] (sg_alloc_table+0x1c/0x44) from [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c)
<4>[   78.327484] [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c) from [<c03a02e8>] (ion_alloc+0xd4/0x354)
<4>[   78.327514] [<c03a02e8>] (ion_alloc+0xd4/0x354) from [<c03a0af0>] (ion_ioctl+0x108/0x434)
...

Code:
<4>[   84.951995] Binder_4: page allocation failure: order:0, mode:0xe0
...
<4>[   84.952117] [<c02da24c>] (sg_alloc_table+0x1c/0x44) from [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c)
<4>[   84.952148] [<c03a3704>] (ion_iommu_heap_allocate+0x1f4/0x54c) from [<c03a02e8>] (ion_alloc+0xd4/0x354)
<4>[   84.952148] [<c03a02e8>] (ion_alloc+0xd4/0x354) from [<c03a0af0>] (ion_ioctl+0x108/0x434)
...
The key here is the mode of allocation. Notice that it is 0xe0. The mode corresponds to the gfp_mask of the allocation. A reference of the GFP flags can be found in the Linux kernel source: /source/include/linux/gfp.h

So we see that 0xe0 = 0x20 | 0x40 | 0x80, translating that to the corresponding GFP flags we see that it is
Code:
0xe0 = ___GFP_HIGH |  ___GFP_IO  | ___GFP_FS
Notice that ___GFP_HIGH is set and not ___GFP_WAIT. This means that this type of allocation will not wait for reclaimable memory to be reclaimed. In particular this means that if most of the memory is occupied by cached pages or VFS cache the allocation will fail. One way around this problem is to simply increase the minimum amount of memory the kernel will attempt to keep available. That's what the above suggested fix does. It certainly seems to solve the problem as the free memory never dips below the point crashes begin to occur when starting the camera. However, a better solution might be to fix the kernel code leading up to the allocation. I believe if @faux123 adds the latest version of scatterlist.c to his kernel repo it may solve the problem as the lines modifying the GFP mask which is originally a standard GFP_KERNEL to the "non-waiting" kind is no longer present.

The problematic code in __sg_alloc_table is
Code:
int __sg_alloc_table(struct sg_table *table, unsigned int nents,
                     unsigned int max_ents, gfp_t gfp_mask,
                     sg_alloc_fn *alloc_fn)
{
...
                gfp_mask &= ~__GFP_WAIT;
                gfp_mask |= __GFP_HIGH;
...
}
Perhaps I'll fork the repo and try to apply the fix myself later.

Wow this is very very a good think .....i hope you can fixed the problems with the random reboots ......im not the big developers .....

THX best regards
Current Phone:
Nexus 5 Black - 16GB - Unlocked & Rooted


ROM:
4.4.2 - Stock with Root


Baseband:
1.0.25.0.23
 
-sandro-
Old
#3  
-sandro-'s Avatar
Senior Member
Thanks Meter 144
Posts: 1,271
Join Date: May 2011
Location: Pescara, Italy
hi, could this be related to reboots?
great find!
Galaxy S4 Google Edition ROMs free mirror: www.livewinter.com/mirror | my videos https://vimeo.com/sandroantonucci
 
alan722
Old
#4  
Junior Member
Thanks Meter 5
Posts: 17
Join Date: Aug 2011
Hi all,

First of all thanks for the great rom Jamal, however my camera app is FC in general and from the lockscreen which is a feature I wanted from stock.

If this fix works how would it be applied as I'm not a developer in any way and not able to post in the actual rom thread.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes