Cannot get memory allocated from `flex_array_alloc` when requesting a relatively big size in linux kernel











up vote
1
down vote

favorite












I'm doing some linux kernel development.



And I'm going to allocate some memory space with something like:



ptr = flex_array_alloc(size=136B, num=1<<16, GFP_KERNEL)



And ptr turns out to be NULL every time I try.



What's more, when I change the size to 20B or num to 256,there's nothing wrong and the memory can be obtained.



So I want to know if there are some limitations for requesting memory in linux kernel modules. And how to debug it or to allocate a big memory space.



Thanks.





And kzalloc has a similar behavior in my environment. That is, requesting a 136B * (1<<16) space failed, while 20B or 1<<8 succeed.










share|improve this question
























  • With respect to your edit: kzalloc is a variant of kmalloc which zeros out the allocation. kmalloc is "the normal method of allocating memory for objects smaller than page size in the kernel." If you want to allocate larger objects, you need to use vmalloc/vzalloc. For the limitations of flexible arrays, see my (edited) answer.
    – rici
    Nov 20 at 15:53

















up vote
1
down vote

favorite












I'm doing some linux kernel development.



And I'm going to allocate some memory space with something like:



ptr = flex_array_alloc(size=136B, num=1<<16, GFP_KERNEL)



And ptr turns out to be NULL every time I try.



What's more, when I change the size to 20B or num to 256,there's nothing wrong and the memory can be obtained.



So I want to know if there are some limitations for requesting memory in linux kernel modules. And how to debug it or to allocate a big memory space.



Thanks.





And kzalloc has a similar behavior in my environment. That is, requesting a 136B * (1<<16) space failed, while 20B or 1<<8 succeed.










share|improve this question
























  • With respect to your edit: kzalloc is a variant of kmalloc which zeros out the allocation. kmalloc is "the normal method of allocating memory for objects smaller than page size in the kernel." If you want to allocate larger objects, you need to use vmalloc/vzalloc. For the limitations of flexible arrays, see my (edited) answer.
    – rici
    Nov 20 at 15:53















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm doing some linux kernel development.



And I'm going to allocate some memory space with something like:



ptr = flex_array_alloc(size=136B, num=1<<16, GFP_KERNEL)



And ptr turns out to be NULL every time I try.



What's more, when I change the size to 20B or num to 256,there's nothing wrong and the memory can be obtained.



So I want to know if there are some limitations for requesting memory in linux kernel modules. And how to debug it or to allocate a big memory space.



Thanks.





And kzalloc has a similar behavior in my environment. That is, requesting a 136B * (1<<16) space failed, while 20B or 1<<8 succeed.










share|improve this question















I'm doing some linux kernel development.



And I'm going to allocate some memory space with something like:



ptr = flex_array_alloc(size=136B, num=1<<16, GFP_KERNEL)



And ptr turns out to be NULL every time I try.



What's more, when I change the size to 20B or num to 256,there's nothing wrong and the memory can be obtained.



So I want to know if there are some limitations for requesting memory in linux kernel modules. And how to debug it or to allocate a big memory space.



Thanks.





And kzalloc has a similar behavior in my environment. That is, requesting a 136B * (1<<16) space failed, while 20B or 1<<8 succeed.







c linux linux-kernel kernel kernel-module






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 15:36

























asked Nov 20 at 14:51









Dai Zhang

85




85












  • With respect to your edit: kzalloc is a variant of kmalloc which zeros out the allocation. kmalloc is "the normal method of allocating memory for objects smaller than page size in the kernel." If you want to allocate larger objects, you need to use vmalloc/vzalloc. For the limitations of flexible arrays, see my (edited) answer.
    – rici
    Nov 20 at 15:53




















  • With respect to your edit: kzalloc is a variant of kmalloc which zeros out the allocation. kmalloc is "the normal method of allocating memory for objects smaller than page size in the kernel." If you want to allocate larger objects, you need to use vmalloc/vzalloc. For the limitations of flexible arrays, see my (edited) answer.
    – rici
    Nov 20 at 15:53


















With respect to your edit: kzalloc is a variant of kmalloc which zeros out the allocation. kmalloc is "the normal method of allocating memory for objects smaller than page size in the kernel." If you want to allocate larger objects, you need to use vmalloc/vzalloc. For the limitations of flexible arrays, see my (edited) answer.
– rici
Nov 20 at 15:53






With respect to your edit: kzalloc is a variant of kmalloc which zeros out the allocation. kmalloc is "the normal method of allocating memory for objects smaller than page size in the kernel." If you want to allocate larger objects, you need to use vmalloc/vzalloc. For the limitations of flexible arrays, see my (edited) answer.
– rici
Nov 20 at 15:53














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










There are two limits to the size of an array allocated with flex_array_allocate. First, the object size itself must not exceed a single page, as indicated in https://www.kernel.org/doc/Documentation/flexible-arrays.txt:




The down sides are that the arrays cannot be indexed directly, individual object size cannot exceed the system page size, and putting data into a flexible array requires a copy operation.




Second, there is a maximum number of elements in the array.



Both limitations are the result of the implementation technique:




…the need for memory from vmalloc() can be eliminated by piecing together an array from smaller parts…



A flexible array holds an arbitrary (within limits) number of fixed-sized objects, accessed via an integer index.… Only single-page allocations are made




The array is "pieced" together by using an array of pointers to individual parts, where each part is one system page. Since this array is also allocated, and only single-page allocations are made (as noted above), the maximum number of parts is slightly less than the number of pointers which can fit in a page (slightly less because there is also some bookkeeping data.) In effect, this limits the total size of a flexible array to about 2MB on systems with 8-byte pointers and 4kb pages. (The precise limitation will vary depending on the amount of wasted space in a page if the object size is not a power of two.)






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395633%2fcannot-get-memory-allocated-from-flex-array-alloc-when-requesting-a-relatively%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    There are two limits to the size of an array allocated with flex_array_allocate. First, the object size itself must not exceed a single page, as indicated in https://www.kernel.org/doc/Documentation/flexible-arrays.txt:




    The down sides are that the arrays cannot be indexed directly, individual object size cannot exceed the system page size, and putting data into a flexible array requires a copy operation.




    Second, there is a maximum number of elements in the array.



    Both limitations are the result of the implementation technique:




    …the need for memory from vmalloc() can be eliminated by piecing together an array from smaller parts…



    A flexible array holds an arbitrary (within limits) number of fixed-sized objects, accessed via an integer index.… Only single-page allocations are made




    The array is "pieced" together by using an array of pointers to individual parts, where each part is one system page. Since this array is also allocated, and only single-page allocations are made (as noted above), the maximum number of parts is slightly less than the number of pointers which can fit in a page (slightly less because there is also some bookkeeping data.) In effect, this limits the total size of a flexible array to about 2MB on systems with 8-byte pointers and 4kb pages. (The precise limitation will vary depending on the amount of wasted space in a page if the object size is not a power of two.)






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      There are two limits to the size of an array allocated with flex_array_allocate. First, the object size itself must not exceed a single page, as indicated in https://www.kernel.org/doc/Documentation/flexible-arrays.txt:




      The down sides are that the arrays cannot be indexed directly, individual object size cannot exceed the system page size, and putting data into a flexible array requires a copy operation.




      Second, there is a maximum number of elements in the array.



      Both limitations are the result of the implementation technique:




      …the need for memory from vmalloc() can be eliminated by piecing together an array from smaller parts…



      A flexible array holds an arbitrary (within limits) number of fixed-sized objects, accessed via an integer index.… Only single-page allocations are made




      The array is "pieced" together by using an array of pointers to individual parts, where each part is one system page. Since this array is also allocated, and only single-page allocations are made (as noted above), the maximum number of parts is slightly less than the number of pointers which can fit in a page (slightly less because there is also some bookkeeping data.) In effect, this limits the total size of a flexible array to about 2MB on systems with 8-byte pointers and 4kb pages. (The precise limitation will vary depending on the amount of wasted space in a page if the object size is not a power of two.)






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        There are two limits to the size of an array allocated with flex_array_allocate. First, the object size itself must not exceed a single page, as indicated in https://www.kernel.org/doc/Documentation/flexible-arrays.txt:




        The down sides are that the arrays cannot be indexed directly, individual object size cannot exceed the system page size, and putting data into a flexible array requires a copy operation.




        Second, there is a maximum number of elements in the array.



        Both limitations are the result of the implementation technique:




        …the need for memory from vmalloc() can be eliminated by piecing together an array from smaller parts…



        A flexible array holds an arbitrary (within limits) number of fixed-sized objects, accessed via an integer index.… Only single-page allocations are made




        The array is "pieced" together by using an array of pointers to individual parts, where each part is one system page. Since this array is also allocated, and only single-page allocations are made (as noted above), the maximum number of parts is slightly less than the number of pointers which can fit in a page (slightly less because there is also some bookkeeping data.) In effect, this limits the total size of a flexible array to about 2MB on systems with 8-byte pointers and 4kb pages. (The precise limitation will vary depending on the amount of wasted space in a page if the object size is not a power of two.)






        share|improve this answer














        There are two limits to the size of an array allocated with flex_array_allocate. First, the object size itself must not exceed a single page, as indicated in https://www.kernel.org/doc/Documentation/flexible-arrays.txt:




        The down sides are that the arrays cannot be indexed directly, individual object size cannot exceed the system page size, and putting data into a flexible array requires a copy operation.




        Second, there is a maximum number of elements in the array.



        Both limitations are the result of the implementation technique:




        …the need for memory from vmalloc() can be eliminated by piecing together an array from smaller parts…



        A flexible array holds an arbitrary (within limits) number of fixed-sized objects, accessed via an integer index.… Only single-page allocations are made




        The array is "pieced" together by using an array of pointers to individual parts, where each part is one system page. Since this array is also allocated, and only single-page allocations are made (as noted above), the maximum number of parts is slightly less than the number of pointers which can fit in a page (slightly less because there is also some bookkeeping data.) In effect, this limits the total size of a flexible array to about 2MB on systems with 8-byte pointers and 4kb pages. (The precise limitation will vary depending on the amount of wasted space in a page if the object size is not a power of two.)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 15:53

























        answered Nov 20 at 15:20









        rici

        151k19131195




        151k19131195






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395633%2fcannot-get-memory-allocated-from-flex-array-alloc-when-requesting-a-relatively%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen