[][openwrt][mt7988][crypto][Fix race condition and refactor SG release in DDK driver]

[Description]
Fix race condition and refactor SG release in DDK driver.
In DMAResourceLib_DMAPool_Get(), it try to get an address from the
list "outside" the lock. Thus, the address is possible to be replaced.

[Release-log]
N/A


Change-Id: I7e55895032215d3a7f32b6c7ff9a8cc51be97450
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/9463835
diff --git a/feed/kernel/crypto-eip/src/ddk/device/dmares_gen.c b/feed/kernel/crypto-eip/src/ddk/device/dmares_gen.c
index 7c2c8a5..27472b3 100644
--- a/feed/kernel/crypto-eip/src/ddk/device/dmares_gen.c
+++ b/feed/kernel/crypto-eip/src/ddk/device/dmares_gen.c
@@ -4,20 +4,16 @@
  */
 
 /*****************************************************************************
-* Copyright (c) 2012-2020 by Rambus, Inc. and/or its subsidiaries.
+* Copyright (c) 2012-2024 by Rambus, Inc. and/or its subsidiaries
+* All rights reserved. Unauthorized use (including, without limitation,
+* distribution and copying) is strictly prohibited. All use requires,
+* and is subject to, explicit written authorization and nondisclosure
+* Rambus, Inc. and/or its subsidiaries
 *
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 2 of the License, or
-* any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <http://www.gnu.org/licenses/>.
+* For more information or support, please go to our online support system at
+* https://sipsupport.rambus.com.
+* In case you do not have an account for this system, please send an e-mail
+* to sipsupport@rambus.com.
 *****************************************************************************/
 
 /*----------------------------------------------------------------------------
@@ -784,6 +780,7 @@
     List_Status_t List_rc1, List_rc2;
     List_Element_t * LE_p;
     unsigned long Flags = 0;
+    void *Address;
 
     // Pool list access protected by lock
     HWPAL_DMAResource_Lock_Acquire(Bank_p->Lock_p, &Flags);
@@ -805,6 +802,7 @@
         return -1;
     }
 
+    Address = LE_p->DataObject_p;
     List_rc2 = List_AddToHead(Bank_p->DanglingListID, NULL, LE_p);
 
     HWPAL_DMAResource_Lock_Release(Bank_p->Lock_p, &Flags);
@@ -816,7 +814,7 @@
         return -1;
     }
 
-    *Addr_pp = LE_p->DataObject_p;
+    *Addr_pp = Address;
 
     return 0;
 }
@@ -1081,6 +1079,8 @@
         LOG_CRIT("DMAResource_SG_Release: static banks not supported\n");
         return -1;
     }
+#else
+    return HWPAL_DMAResource_Release(Handle);
 #endif // HWPAL_DMARESOURCE_BANKS_ENABLE
 }
 
@@ -1157,6 +1157,7 @@
                     LOG_CRIT("DMAResource_Release: "
                              "put to DMA pool failed for handle %p\n",
                              Handle);
+                    HWPAL_DMAResource_Release(Handle);
                     return -1;
                 }
             }
@@ -1281,7 +1282,6 @@
 #endif
 
     Pair_p = DMAResourceLib_LookupDomain(Rec_p, DMARES_DOMAIN_HOST);
-#ifndef HWPAL_DMARESOURCE_OPT1
     if (Pair_p == NULL)
     {
         LOG_WARN(
@@ -1291,8 +1291,7 @@
 
         return 0;
     }
-#endif
-
+    else
     {
         uint32_t * Address_p = Pair_p->Address_p;
         uint32_t Value = read32_volatile(Address_p + WordOffset);
@@ -1359,7 +1358,6 @@
 #endif
 
     Pair_p = DMAResourceLib_LookupDomain(Rec_p, DMARES_DOMAIN_HOST);
-#ifndef HWPAL_DMARESOURCE_OPT1
     if (Pair_p == NULL)
     {
         LOG_WARN(
@@ -1369,23 +1367,21 @@
 
         return;
     }
-#endif
-
-#ifdef HWPAL_TRACE_DMARESOURCE_WRITE
-    Log_FormattedMessage(
-        "DMAResource_Write32: "
-        "(handle %p) "
-        "[%u] = 0x%08x "
-        "(swap=%d)\n",
-        Handle,
-        WordOffset,
-        Value,
-        Rec_p->fSwapEndianness);
-#endif
-
+    else
     {
         uint32_t * Address_p = Pair_p->Address_p;
         uint32_t WriteValue = Value;
+#ifdef HWPAL_TRACE_DMARESOURCE_WRITE
+        Log_FormattedMessage(
+            "DMAResource_Write32: "
+            "(handle %p) "
+            "[%u] = 0x%08x "
+            "(swap=%d)\n",
+            Handle,
+            WordOffset,
+            Value,
+            Rec_p->fSwapEndianness);
+#endif
 
 #ifndef HWPAL_DMARESOURCE_OPT2
         // swap endianness, if required
@@ -1446,7 +1442,7 @@
 
         return;
     }
-
+    else
     {
         uint32_t * Address_p = Pair_p->Address_p;
         unsigned int i;
@@ -1461,11 +1457,7 @@
 
             Values_p[i] = Value;
         } // for
-    }
-
 #ifdef HWPAL_TRACE_DMARESOURCE_READ
-    {
-        uint32_t * Address_p = Pair_p->Address_p;
         if (Values_p == Address_p + StartWordOffset)
         {
             Log_FormattedMessage(
@@ -1490,8 +1482,8 @@
                 StartWordOffset + WordCount - 1,
                 Rec_p->fSwapEndianness);
         }
-    }
 #endif
+    }
 }
 
 
@@ -1543,7 +1535,7 @@
 
         return;
     }
-
+    else
     {
         uint32_t * Address_p = Pair_p->Address_p;
         unsigned int i;
@@ -1558,11 +1550,7 @@
 
             write32_volatile(Value, Address_p + StartWordOffset + i);
         } // for
-    }
-
 #ifdef HWPAL_TRACE_DMARESOURCE_WRITE
-    {
-        uint32_t * Address_p = Pair_p->Address_p;
         if (Values_p == Address_p + StartWordOffset)
         {
             Log_FormattedMessage(
@@ -1587,8 +1575,8 @@
                 StartWordOffset + WordCount - 1,
                 Rec_p->fSwapEndianness);
         }
-    }
 #endif /* HWPAL_TRACE_DMARESOURCE_WRITE */
+    }
 }
 
 
diff --git a/feed/kernel/crypto-eip/src/ddk/device/lkm/dmares_lkm.c b/feed/kernel/crypto-eip/src/ddk/device/lkm/dmares_lkm.c
index 49d183a..bb11b2f 100644
--- a/feed/kernel/crypto-eip/src/ddk/device/lkm/dmares_lkm.c
+++ b/feed/kernel/crypto-eip/src/ddk/device/lkm/dmares_lkm.c
@@ -185,7 +185,6 @@
 
 static void * HWPAL_Lock_p;
 
-
 /*----------------------------------------------------------------------------
  * DMAResourceLib_IdxPair2RecordPtr
  *
@@ -639,45 +638,14 @@
 
     // Allocate DMA resource
     {
-        struct device * DMADevice_p;
         size_t n = 0;
-        void  * UnalignedAddr_p = NULL;
-        //void * AlignedAddr_p = NULL;
-        //dma_addr_t DMAAddr = 0;
-        //phys_addr_t PhysAddr = 0;
-        Device_Data_t DevData;
-
-        ZEROINIT(DevData);
 
-        // Get device reference for this resource
-        DMADevice_p = Device_GetReference(NULL, &DevData);
-        {
-            gfp_t flags = HWPAL_DMA_FLAGS;
-
-            // Non-fixed dynamic address buffer allocation
-
-            // Align if required
-            n = DMAResourceLib_AlignForAddress(
-                 DMAResourceLib_AlignForSize(RequestedProperties.Size,
-                 AlignTo),
-                 AlignTo);
-
-            if (in_atomic())
-                flags |= GFP_ATOMIC;    // non-sleepable
-            else
-                flags |= GFP_KERNEL;    // sleepable
+        // Align if required
+        n = DMAResourceLib_AlignForAddress(
+             DMAResourceLib_AlignForSize(RequestedProperties.Size,
+             AlignTo),
+             AlignTo);
 
-            UnalignedAddr_p = kmalloc(n, flags);
-            if (UnalignedAddr_p == NULL)
-            {
-                LOG_CRIT("DMAResource_Alloc: failed for handle 0x%p,"
-                        " size %d\n",
-                         Handle,(unsigned int)n);
-                DMAResource_DestroyRecord(Handle);
-                return -1;
-            }
-        }
-        
         DMAResourceLib_Setup_Record(&ActualProperties, 'A', Rec_p, n);
 
         Pair_p = Rec_p->AddrPairs;
@@ -685,7 +653,7 @@
         Pair_p->Domain = DMARES_DOMAIN_BUS;
 
         ++Pair_p;
-        Pair_p->Address_p = UnalignedAddr_p;
+        Pair_p->Address_p = (void *)(uintptr_t) DmaAddress; //UnalignedAddr_p;
         Pair_p->Domain = DMARES_DOMAIN_HOST;
 
         // Return this address
@@ -693,7 +661,7 @@
 
         // This host address will be used for freeing the allocated buffer
         ++Pair_p;
-        Pair_p->Address_p = UnalignedAddr_p;
+        Pair_p->Address_p = (void *)(uintptr_t) DmaAddress; //UnalignedAddr_p;
         Pair_p->Domain = DMARES_DOMAIN_HOST_UNALIGNED;
     }
 
@@ -1082,19 +1050,6 @@
                 Handle);
             return -1;
         }
-
-        Pair_p = DMAResourceLib_LookupDomain(Rec_p,
-                                             DMARES_DOMAIN_HOST_UNALIGNED);
-        if (Pair_p == NULL)
-        {
-            LOG_WARN(
-                "HW_DMAResource_SG_Release: "
-                "No host address found for Handle %p?\n",
-                Handle);
-            return -1;
-        }
-        
-        kfree(Pair_p->Address_p);
     }
     // free administration resources
     Rec_p->Magic = 0;