<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    The problem was that I did not know to call `sg_init_table`, it is
    not sufficient to just init the seperate entries with sg_set_page.<br>
    <br>
    <div class="moz-cite-prefix">On 02/02/15 19:49, Malte Vesper wrote:<br>
    </div>
    <blockquote cite="mid:54CFD4E4.80607@postgrad.manchester.ac.uk"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      Since my last mail went unanswered I did some further
      investigation. I compiled a kernel with the DMA-API debug fs
      support and got the dmesg output below. Note how I run the program
      3 times and it only reports on the third run that the page mapping
      count is off. The thing confusing me the most about this, is that
      I can't see where I go wrong. Since I call pci_map_sg and
      pci_unmap_sg directly after each other there is no room for
      parameter modification. (I have only one device, and the interrupt
      handler has output when invoked). So what is wrong with:<br>
      <br>
                      mapped =<b> pci_map_sg</b>(pcidev, scatterlist,
      pinned, DMA_BIDIRECTIONAL);<br>
                      <b>pci_unmap_sg</b>(pcidev, scatterlist, pinned,
      DMA_BIDIRECTIONAL);<br>
      <br>
      Do I have to recreate the vanilla scatterlist for pci_unmap_sg?<br>
      <br>
      Any hints/help is highly appreciated.<br>
      <br>
      Ted<br>
      <br>
      <br>
      <br>
      Dmesg output:<br>
      <blockquote>[ 2087.171247] SCAS FPGA 0000:01:00.0: [fpga+]:
        Removing device 0<br>
        [ 2087.171771] [fpga+]: SCAS MAJOR UNREGISTERED: 100<br>
        [ 2087.180070] [fpga+]: fpga+ registered with major number 100<br>
        [ 2087.180096] SCAS FPGA 0000:01:00.0: Probing<br>
        [ 2087.180163] SCAS FPGA 0000:01:00.0: irq 66 for MSI/MSI-X<br>
        [ 2087.180182] SCAS FPGA 0000:01:00.0: [fpga+]: Board tied to
        slot 0<br>
        [ 2092.359203] [fpga+]: IOCTL 2148040970<br>
        [ 2092.359206] [fpga+]: MINOR: 0<br>
        [ 2092.359207] [fpga+]: ARG: 140736686734672<br>
        [ 2092.359210] SCAS FPGA 0000:01:00.0: [fpga+]: IOCTL:
        IOCTL_FPGA_PIN_PAGE<br>
        [ 2092.359211] Testers hell<br>
        [ 2092.359215] pinned 2<br>
        [ 2092.359216] setting page: 0<br>
        [ 2092.359217] setting page: 1<br>
        [ 2092.359223] Mapped: 2<br>
        [ 2092.359227] Done<br>
        [ 2117.846740] [fpga+]: IOCTL 2148040970<br>
        [ 2117.846742] [fpga+]: MINOR: 0<br>
        [ 2117.846743] [fpga+]: ARG: 140735106116944<br>
        [ 2117.846746] SCAS FPGA 0000:01:00.0: [fpga+]: IOCTL:
        IOCTL_FPGA_PIN_PAGE<br>
        [ 2117.846746] Testers hell<br>
        [ 2117.846749] pinned 2<br>
        [ 2117.846749] setting page: 0<br>
        [ 2117.846750] setting page: 1<br>
        [ 2117.846755] Mapped: 2<br>
        [ 2117.846759] Done<br>
        [ 2124.136132] [fpga+]: IOCTL 2148040970<br>
        [ 2124.136134] [fpga+]: MINOR: 0<br>
        [ 2124.136135] [fpga+]: ARG: 140735560133008<br>
        [ 2124.136138] SCAS FPGA 0000:01:00.0: [fpga+]: IOCTL:
        IOCTL_FPGA_PIN_PAGE<br>
        [ 2124.136138] Testers hell<br>
        [ 2124.136141] pinned 2<br>
        [ 2124.136142] setting page: 0<br>
        [ 2124.136142] setting page: 1<br>
        [ 2124.136147] Mapped: 2<br>
        [ 2124.136152] Done<br>
        [ 2124.136251] BUG: Bad page map in process dma 
        pte:8000000f7b983867 pmd:fa9d31067<br>
        [ 2124.136254] page:ffffea003dee60c0 count:1 mapcount:-30705
        mapping:ffff880fadddb901 index:0xc0054000afddc901<br>
        [ 2124.136254] flags:
        0x2ffff000008006c(referenced|uptodate|lru|active|swapbacked)<br>
        [ 2124.136257] page dumped because: bad pte<br>
        [ 2124.136258] addr:0000000001382000 vm_flags:08100073
        anon_vma:ffff880fadddb900 mapping:          (null) index:1382<br>
        [ 2124.136261] CPU: 6 PID: 6902 Comm: dma Tainted: G    B   W 
        OE  3.18.5hotdog #2<br>
        [ 2124.136261] Hardware name: System manufacturer System Product
        Name/RAMPAGE IV BLACK EDITION, BIOS 0701 06/04/2014<br>
        [ 2124.136262]  ffff880fb842eac8 ffff880fd4093c58
        ffffffff8176ab0c 0000000000000000<br>
        [ 2124.136264]  0000000001382000 ffff880fd4093ca8
        ffffffff8119215a 8000000f7b983867<br>
        [ 2124.136265]  0000000000001382 ffff880fd4093ca8
        ffff880fa9d31c10 ffffea003dee60c0<br>
        [ 2124.136267] Call Trace:<br>
        [ 2124.136270]  [&lt;ffffffff8176ab0c&gt;] dump_stack+0x46/0x58<br>
        [ 2124.136274]  [&lt;ffffffff8119215a&gt;]
        print_bad_pte+0x1aa/0x250<br>
        [ 2124.136275]  [&lt;ffffffff8119367f&gt;]
        unmap_single_vma+0x76f/0x8f0<br>
        [ 2124.136278]  [&lt;ffffffff81175cf0&gt;] ?
        put_pages_list+0x70/0x70<br>
        [ 2124.136279]  [&lt;ffffffff811942d1&gt;] unmap_vmas+0x51/0xa0<br>
        [ 2124.136281]  [&lt;ffffffff8119d48c&gt;] exit_mmap+0x9c/0x170<br>
        [ 2124.136284]  [&lt;ffffffff8106c1c4&gt;] mmput+0x64/0x130<br>
        [ 2124.136286]  [&lt;ffffffff810716ec&gt;] do_exit+0x27c/0xa80<br>
        [ 2124.136287]  [&lt;ffffffff81071f7f&gt;]
        do_group_exit+0x3f/0xa0<br>
        [ 2124.136290]  [&lt;ffffffff81071ff4&gt;]
        SyS_exit_group+0x14/0x20<br>
        [ 2124.136292]  [&lt;ffffffff81772f6d&gt;]
        system_call_fastpath+0x16/0x1b<br>
        [ 2124.136319] BUG: Bad page state in process dma  pfn:f7b983<br>
        [ 2124.136320] page:ffffea003dee60c0 count:0 mapcount:-30705
        mapping:          (null) index:0xc0054000afddc901<br>
        [ 2124.136321] flags:
        0x2ffff000008000c(referenced|uptodate|swapbacked)<br>
        [ 2124.136322] page dumped because: nonzero mapcount<br>
        [ 2124.136323] Modules linked in: fpga+(OE) hid_generic nfsv3
        rpcsec_gss_krb5 nfsv4 vmw_vsock_vmci_transport vsock vmw_vmci
        autofs4 nfsd auth_rpcgss nfs_acl nfs snd_hda_codec_realtek
        snd_hda_codec_generic snd_hda_codec_hdmi snd_hda_intel
        snd_hda_controller snd_hda_codec x86_pkg_temp_thermal
        intel_powerclamp snd_hwdep coretemp rfcomm snd_pcm kvm_intel kvm
        bnep lockd bluetooth snd_seq_midi snd_seq_midi_event snd_rawmidi
        snd_seq grace sunrpc eeepc_wmi asus_wmi snd_seq_device snd_timer
        sparse_keymap joydev video mei_me sb_edac snd edac_core mei
        serio_raw lpc_ich soundcore fscache binfmt_misc nls_iso8859_1
        parport_pc ppdev mac_hid tpm_infineon cp210x usbserial lp
        parport dm_crypt hid_microsoft usbhid hid radeon i2c_algo_bit
        ttm drm_kms_helper mxm_wmi drm crct10dif_pclmul crc32_pclmul
        e1000e ghash_clmulni_intel aesni_intel psmouse aes_x86_64 lrw
        gf128mul glue_helper ablk_helper cryptd aacraid ahci ptp libahci
        pps_core wmi [last unloaded: fpga+]<br>
        [ 2124.136351] CPU: 6 PID: 6902 Comm: dma Tainted: G    B   W 
        OE  3.18.5hotdog #2<br>
        [ 2124.136352] Hardware name: System manufacturer System Product
        Name/RAMPAGE IV BLACK EDITION, BIOS 0701 06/04/2014<br>
        [ 2124.136353]  ffffffff81aa55c7 ffff880fd4093bb8
        ffffffff8176ab0c 0000000000000000<br>
        [ 2124.136354]  ffffea003dee60c0 ffff880fd4093be8
        ffffffff81767527 ff0000ffffffffff<br>
        [ 2124.136355]  ffffea003dee60c0 0000000000000000
        00ffff0000000000 ffff880fd4093c38<br>
        [ 2124.136356] Call Trace:<br>
        [ 2124.136358]  [&lt;ffffffff8176ab0c&gt;] dump_stack+0x46/0x58<br>
        [ 2124.136360]  [&lt;ffffffff81767527&gt;]
        bad_page.part.50+0xe0/0xfe<br>
        [ 2124.136362]  [&lt;ffffffff8116c91d&gt;]
        free_pages_prepare+0x16d/0x190<br>
        [ 2124.136363]  [&lt;ffffffff8116f1a5&gt;]
        free_hot_cold_page+0x35/0x180<br>
        [ 2124.136365]  [&lt;ffffffff8116f33e&gt;]
        free_hot_cold_page_list+0x4e/0xa0<br>
        [ 2124.136366]  [&lt;ffffffff81176076&gt;]
        release_pages+0x1f6/0x270<br>
        [ 2124.136368]  [&lt;ffffffff811a88bd&gt;]
        free_pages_and_swap_cache+0x8d/0xa0<br>
        [ 2124.136370]  [&lt;ffffffff81191b04&gt;]
        tlb_flush_mmu_free+0x34/0x60<br>
        [ 2124.136371]  [&lt;ffffffff81192524&gt;]
        tlb_flush_mmu+0x24/0x30<br>
        [ 2124.136373]  [&lt;ffffffff81192544&gt;]
        tlb_finish_mmu+0x14/0x40<br>
        [ 2124.136374]  [&lt;ffffffff8119d4bb&gt;] exit_mmap+0xcb/0x170<br>
        [ 2124.136376]  [&lt;ffffffff8106c1c4&gt;] mmput+0x64/0x130<br>
        [ 2124.136378]  [&lt;ffffffff810716ec&gt;] do_exit+0x27c/0xa80<br>
        [ 2124.136379]  [&lt;ffffffff81071f7f&gt;]
        do_group_exit+0x3f/0xa0<br>
        [ 2124.136381]  [&lt;ffffffff81071ff4&gt;]
        SyS_exit_group+0x14/0x20<br>
        [ 2124.136382]  [&lt;ffffffff81772f6d&gt;]
        system_call_fastpath+0x16/0x1b<br>
        ted@ted-headlessBrute:~$ <br>
      </blockquote>
      <br>
      <br>
      <br>
      <br>
      <div class="moz-cite-prefix">On 29/01/15 19:50, Malte Vesper
        wrote:<br>
      </div>
      <blockquote cite="mid:54CA8F1C.50909@postgrad.manchester.ac.uk"
        type="cite"> Hi,<br>
        I am trying to use pci_map_sg in combination with
        get_user_pages_fast for a driver. However my code screws the
        processes memory map over. For easier testing I bundled the
        piinin/unpinning and mapping unmapping into one ioctl call. The
        following code misses all the checks on return values, since it
        is only inteneded as a MWE.<br>
        <br>
        When I run my code I get a "<b>BUG: Bad page map in process ...</b>",


        after "Done" is printed.<br>
        <br>
        I tried following DMA-API-howto.txt and looking at other code,
        but I fail to see where I go wrong.<br>
        <br>
        Regards, Ted<br>
        <br>
        <br>
        Code from the IOCTL handler:<br>
        <br>
                case IOCTL_FPGA_PIN_PAGE:<br>
                {<br>
                    struct pageInfo pageInfo;<br>
        <br>
                    dev_dbg(&amp;pcidev-&gt;dev, pr_fmt("IOCTL:
        IOCTL_FPGA_PIN_PAGE\n"));<br>
        <br>
                    if(!copy_from_user(&amp;pageInfo, (void*)arg,
        sizeof(struct pageInfo))) {<br>
                        //horrible test<br>
                        const int noPages = pageInfo.size/PAGE_SIZE;<br>
                        int pinned;<br>
                        int mapped  = 0;<br>
                        struct scatterlist* scatterlist;<br>
                        printk("Test start\n");<br>
                        //userspacestartpointer, nopages, write?, page*
        array<br>
                        struct page** pages=kmalloc(sizeof(struct
        page*)*noPages, GFP_KERNEL);<br>
                        pinned=<b>get_user_pages_fast</b>((unsigned
        long)pageInfo.start, noPages, 1, pages);<br>
        <br>
                        scatterlist = kmalloc(sizeof(struct
        scatterlist)*pinned, GFP_KERNEL);<br>
        <br>
                        for(int i=0; i&lt;pinned; ++i) {<br>
                            sg_set_page(&amp;scatterlist[i], pages[i],
        PAGE_SIZE, 0);<br>
                        }<br>
        <br>
                        mapped =<b> pci_map_sg</b>(pcidev, scatterlist,
        pinned, DMA_BIDIRECTIONAL);<br>
                        <b>pci_unmap_sg</b>(pcidev, scatterlist,
        pinned, DMA_BIDIRECTIONAL);<br>
        <br>
                        for(int i=0; i&lt;pinned; ++i) {<br>
                            <b>put_page</b>(pages[i]); //I did place a
        print here and got two pages unpinned as I expected<br>
                        }<br>
                        kfree(scatterlist);<br>
                        kfree(pages);<br>
                        printk("Done\n");<br>
                        /*pageInfo.pageId =
        getPageId(getArea(&amp;pageInfo));<br>
                        copy_to_user((void*)arg, &amp;pageInfo,
        sizeof(struct pageInfo));*/<br>
                        return pageInfo.pageId;<br>
                    } else {<br>
                        return -EFAULT;<br>
                    }<br>
                }<br>
                    break;<br>
        <br>
        P.S.: I used the following code to allocate the memory in
        userspace, and I use chunk and chunksize to fill pageInfo.start
        and pageInfo.size respectivly:<br>
        <br>
            const int pagesize = sysconf(_SC_PAGESIZE);<br>
            const int chunksize = 2*pagesize;<br>
            void* chunk;<br>
        <br>
            if(int error = posix_memalign(&amp;chunk, pagesize,
        chunksize)) {<br>
                std::cout &lt;&lt; "Could not get a page." &lt;&lt;
        std::endl;<br>
                return error;<br>
            }<br>
        <br>
        <fieldset class="mimeAttachmentHeader"></fieldset>
        <br>
        <pre wrap="">_______________________________________________
Kernelnewbies mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a>
<a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a>
</pre>
      </blockquote>
      <br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Kernelnewbies mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a>
<a class="moz-txt-link-freetext" href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>