get:
Show a patch.

patch:
Update a patch.

put:
Update a patch.

GET /api/patches/133279/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 133279,
    "url": "http://patchwork.dpdk.org/api/patches/133279/?format=api",
    "web_url": "http://patchwork.dpdk.org/project/dpdk/patch/20231025092717.2069-1-artur.paszkiewicz@intel.com/",
    "project": {
        "id": 1,
        "url": "http://patchwork.dpdk.org/api/projects/1/?format=api",
        "name": "DPDK",
        "link_name": "dpdk",
        "list_id": "dev.dpdk.org",
        "list_email": "dev@dpdk.org",
        "web_url": "http://core.dpdk.org",
        "scm_url": "git://dpdk.org/dpdk",
        "webscm_url": "http://git.dpdk.org/dpdk",
        "list_archive_url": "https://inbox.dpdk.org/dev",
        "list_archive_url_format": "https://inbox.dpdk.org/dev/{}",
        "commit_url_format": ""
    },
    "msgid": "<20231025092717.2069-1-artur.paszkiewicz@intel.com>",
    "list_archive_url": "https://inbox.dpdk.org/dev/20231025092717.2069-1-artur.paszkiewicz@intel.com",
    "date": "2023-10-25T09:27:17",
    "name": "[v3] mem: allow using ASan in multi-process mode",
    "commit_ref": null,
    "pull_url": null,
    "state": "new",
    "archived": false,
    "hash": "da44698f1c30b2e07d05ff2cfa5be8e58add163f",
    "submitter": {
        "id": 3175,
        "url": "http://patchwork.dpdk.org/api/people/3175/?format=api",
        "name": "Artur Paszkiewicz",
        "email": "artur.paszkiewicz@intel.com"
    },
    "delegate": {
        "id": 24651,
        "url": "http://patchwork.dpdk.org/api/users/24651/?format=api",
        "username": "dmarchand",
        "first_name": "David",
        "last_name": "Marchand",
        "email": "david.marchand@redhat.com"
    },
    "mbox": "http://patchwork.dpdk.org/project/dpdk/patch/20231025092717.2069-1-artur.paszkiewicz@intel.com/mbox/",
    "series": [
        {
            "id": 29973,
            "url": "http://patchwork.dpdk.org/api/series/29973/?format=api",
            "web_url": "http://patchwork.dpdk.org/project/dpdk/list/?series=29973",
            "date": "2023-10-25T09:27:17",
            "name": "[v3] mem: allow using ASan in multi-process mode",
            "version": 3,
            "mbox": "http://patchwork.dpdk.org/series/29973/mbox/"
        }
    ],
    "comments": "http://patchwork.dpdk.org/api/patches/133279/comments/",
    "check": "warning",
    "checks": "http://patchwork.dpdk.org/api/patches/133279/checks/",
    "tags": {},
    "related": [],
    "headers": {
        "Return-Path": "<dev-bounces@dpdk.org>",
        "X-Original-To": "patchwork@inbox.dpdk.org",
        "Delivered-To": "patchwork@inbox.dpdk.org",
        "Received": [
            "from mails.dpdk.org (mails.dpdk.org [217.70.189.124])\n\tby inbox.dpdk.org (Postfix) with ESMTP id 6EC83431FA;\n\tWed, 25 Oct 2023 11:27:27 +0200 (CEST)",
            "from mails.dpdk.org (localhost [127.0.0.1])\n\tby mails.dpdk.org (Postfix) with ESMTP id 1CD2B402E8;\n\tWed, 25 Oct 2023 11:27:27 +0200 (CEST)",
            "from mgamail.intel.com (mgamail.intel.com [192.55.52.151])\n by mails.dpdk.org (Postfix) with ESMTP id CA164402C5\n for <dev@dpdk.org>; Wed, 25 Oct 2023 11:27:25 +0200 (CEST)",
            "from orviesa002.jf.intel.com ([10.64.159.142])\n by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;\n 25 Oct 2023 02:27:24 -0700",
            "from apaszkie-mobl2.apaszkie-mobl2 (HELO apaszkie-mobl2.intel.com)\n ([10.213.3.61])\n by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;\n 25 Oct 2023 02:26:49 -0700"
        ],
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple;\n d=intel.com; i=@intel.com; q=dns/txt; s=Intel;\n t=1698226046; x=1729762046;\n h=from:to:cc:subject:date:message-id:in-reply-to:\n references:mime-version:content-transfer-encoding;\n bh=EpRg8JjEiwPqDX+MLrH9xxaydOvoITbC38OLL8XnsVA=;\n b=jIqjUOSZsZyb/oBeVmv2JxVyynX5ctj8h1GKdnlAgYaufA7FC+Fc0kjn\n ZPLItN70F/pNLjj+15aKuFEbjHPhsdHmmfCrpfi06nEsHsQl/wAqxR3nC\n ddb/TZYTBcA+hfPnBznCajHH4aoh9aLKJTiwm/FzITPmdUWFPZABNC0Ud\n xrwevBcbY38RrXFH09vQptZXAW8rSbtAo8Ttwtfe54tQLvBNUzQMh6TTy\n z3X6b+oUTjpCom4m169tuqObhotKE76WICDxkqyMaAExyaGkL+XdnKWi/\n 2zhC3ViOcHcm/IpwFy6S1yDW3uid2chCjUE21298FihGJuZhk1tuDmmAK A==;",
        "X-IronPort-AV": [
            "E=McAfee;i=\"6600,9927,10873\"; a=\"367486340\"",
            "E=Sophos;i=\"6.03,250,1694761200\"; d=\"scan'208\";a=\"367486340\"",
            "E=Sophos;i=\"6.03,250,1694761200\";\n   d=\"scan'208\";a=\"32098\""
        ],
        "X-ExtLoop1": "1",
        "From": "Artur Paszkiewicz <artur.paszkiewicz@intel.com>",
        "To": "anatoly.burakov@intel.com",
        "Cc": "dev@dpdk.org, david.marchand@redhat.com,\n Artur Paszkiewicz <artur.paszkiewicz@intel.com>",
        "Subject": "[PATCH v3] mem: allow using ASan in multi-process mode",
        "Date": "Wed, 25 Oct 2023 11:27:17 +0200",
        "Message-Id": "<20231025092717.2069-1-artur.paszkiewicz@intel.com>",
        "X-Mailer": "git-send-email 2.35.3",
        "In-Reply-To": "<20231004142308.15395-1-artur.paszkiewicz@intel.com>",
        "References": "<20231004142308.15395-1-artur.paszkiewicz@intel.com>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "X-BeenThere": "dev@dpdk.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "DPDK patches and discussions <dev.dpdk.org>",
        "List-Unsubscribe": "<https://mails.dpdk.org/options/dev>,\n <mailto:dev-request@dpdk.org?subject=unsubscribe>",
        "List-Archive": "<http://mails.dpdk.org/archives/dev/>",
        "List-Post": "<mailto:dev@dpdk.org>",
        "List-Help": "<mailto:dev-request@dpdk.org?subject=help>",
        "List-Subscribe": "<https://mails.dpdk.org/listinfo/dev>,\n <mailto:dev-request@dpdk.org?subject=subscribe>",
        "Errors-To": "dev-bounces@dpdk.org"
    },
    "content": "Multi-process applications operate on shared hugepage memory but each\nprocess has its own ASan shadow region which is not synchronized with\nthe other processes. This causes issues when different processes try to\nuse the same memory because they have their own view of which addresses\nare valid.\n\nFix it by mapping the shadow regions for memseg lists as shared memory.\nThe primary process is responsible for creating and removing the shared\nmemory objects.\n\nDisable ASan instrumentation for triggering the page fault in\nalloc_seg() because if the segment is already allocated by another\nprocess and is marked as free in the shadow, accessing this address will\ncause an ASan error.\n\nSigned-off-by: Artur Paszkiewicz <artur.paszkiewicz@intel.com>\n---\nv3:\n- Removed conditional compilation from eal_common_memory.c.\n- Improved comments.\nv2:\n- Added checks for config options disabling multi-process support.\n- Fixed missing unmap in legacy mode.\n\n lib/eal/common/eal_common_memory.c |   7 ++\n lib/eal/common/eal_private.h       |  35 ++++++++++\n lib/eal/linux/eal_memalloc.c       |  23 +++++--\n lib/eal/linux/eal_memory.c         | 101 +++++++++++++++++++++++++++++\n lib/eal/linux/meson.build          |   4 ++\n 5 files changed, 164 insertions(+), 6 deletions(-)",
    "diff": "diff --git a/lib/eal/common/eal_common_memory.c b/lib/eal/common/eal_common_memory.c\nindex d9433db623..5daf53d4d2 100644\n--- a/lib/eal/common/eal_common_memory.c\n+++ b/lib/eal/common/eal_common_memory.c\n@@ -263,6 +263,11 @@ eal_memseg_list_alloc(struct rte_memseg_list *msl, int reserve_flags)\n \tRTE_LOG(DEBUG, EAL, \"VA reserved for memseg list at %p, size %zx\\n\",\n \t\t\taddr, mem_sz);\n \n+\tif (eal_memseg_list_map_asan_shadow(msl) != 0) {\n+\t\tRTE_LOG(ERR, EAL, \"Failed to map ASan shadow region for memseg list\");\n+\t\treturn -1;\n+\t}\n+\n \treturn 0;\n }\n \n@@ -1050,6 +1055,8 @@ rte_eal_memory_detach(void)\n \t\t\t\tRTE_LOG(ERR, EAL, \"Could not unmap memory: %s\\n\",\n \t\t\t\t\t\trte_strerror(rte_errno));\n \n+\t\teal_memseg_list_unmap_asan_shadow(msl);\n+\n \t\t/*\n \t\t * we are detaching the fbarray rather than destroying because\n \t\t * other processes might still reference this fbarray, and we\ndiff --git a/lib/eal/common/eal_private.h b/lib/eal/common/eal_private.h\nindex 5eadba4902..6535b38637 100644\n--- a/lib/eal/common/eal_private.h\n+++ b/lib/eal/common/eal_private.h\n@@ -300,6 +300,41 @@ eal_memseg_list_alloc(struct rte_memseg_list *msl, int reserve_flags);\n void\n eal_memseg_list_populate(struct rte_memseg_list *msl, void *addr, int n_segs);\n \n+/**\n+ * Map shared memory for MSL ASan shadow region.\n+ *\n+ * @param msl\n+ *  Memory segment list.\n+ * @return\n+ *  0 on success, (-1) on failure.\n+ */\n+#ifdef RTE_MALLOC_ASAN\n+int\n+eal_memseg_list_map_asan_shadow(struct rte_memseg_list *msl);\n+#else\n+static inline int\n+eal_memseg_list_map_asan_shadow(__rte_unused struct rte_memseg_list *msl)\n+{\n+\treturn 0;\n+}\n+#endif\n+\n+/**\n+ * Unmap the MSL ASan shadow region.\n+ *\n+ * @param msl\n+ *  Memory segment list.\n+ */\n+#ifdef RTE_MALLOC_ASAN\n+void\n+eal_memseg_list_unmap_asan_shadow(struct rte_memseg_list *msl);\n+#else\n+static inline void\n+eal_memseg_list_unmap_asan_shadow(__rte_unused struct rte_memseg_list *msl)\n+{\n+}\n+#endif\n+\n /**\n  * Distribute available memory between MSLs.\n  *\ndiff --git a/lib/eal/linux/eal_memalloc.c b/lib/eal/linux/eal_memalloc.c\nindex f8b1588cae..a4151534a8 100644\n--- a/lib/eal/linux/eal_memalloc.c\n+++ b/lib/eal/linux/eal_memalloc.c\n@@ -511,6 +511,21 @@ resize_hugefile(int fd, uint64_t fa_offset, uint64_t page_sz, bool grow,\n \t\t\tgrow, dirty);\n }\n \n+__rte_no_asan\n+static inline void\n+page_fault(void *addr)\n+{\n+\t/* We need to trigger a write to the page to enforce page fault but we\n+\t * can't overwrite value that is already there, so read the old value\n+\t * and write it back. Kernel populates the page with zeroes initially.\n+\t *\n+\t * Disable ASan instrumentation here because if the segment is already\n+\t * allocated by another process and is marked as free in the shadow,\n+\t * accessing this address will cause an ASan error.\n+\t */\n+\t*(volatile int *)addr = *(volatile int *)addr;\n+}\n+\n static int\n alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,\n \t\tstruct hugepage_info *hi, unsigned int list_idx,\n@@ -636,12 +651,8 @@ alloc_seg(struct rte_memseg *ms, void *addr, int socket_id,\n \t\tgoto mapped;\n \t}\n \n-\t/* we need to trigger a write to the page to enforce page fault and\n-\t * ensure that page is accessible to us, but we can't overwrite value\n-\t * that is already there, so read the old value, and write itback.\n-\t * kernel populates the page with zeroes initially.\n-\t */\n-\t*(volatile int *)addr = *(volatile int *)addr;\n+\t/* enforce page fault and ensure that page is accessible to us */\n+\tpage_fault(addr);\n \n \tiova = rte_mem_virt2iova(addr);\n \tif (iova == RTE_BAD_PHYS_ADDR) {\ndiff --git a/lib/eal/linux/eal_memory.c b/lib/eal/linux/eal_memory.c\nindex 9b6f08fba8..102a57fd23 100644\n--- a/lib/eal/linux/eal_memory.c\n+++ b/lib/eal/linux/eal_memory.c\n@@ -41,6 +41,7 @@\n #include \"eal_filesystem.h\"\n #include \"eal_hugepages.h\"\n #include \"eal_options.h\"\n+#include \"malloc_elem.h\"\n \n #define PFN_MASK_SIZE\t8\n \n@@ -1469,6 +1470,7 @@ eal_legacy_hugepage_init(void)\n \t\tif (msl->memseg_arr.count > 0)\n \t\t\tcontinue;\n \t\t/* this is an unused list, deallocate it */\n+\t\teal_memseg_list_unmap_asan_shadow(msl);\n \t\tmem_sz = msl->len;\n \t\tmunmap(msl->base_va, mem_sz);\n \t\tmsl->base_va = NULL;\n@@ -1956,3 +1958,102 @@ rte_eal_memseg_init(void)\n #endif\n \t\t\tmemseg_secondary_init();\n }\n+\n+#ifdef RTE_MALLOC_ASAN\n+int\n+eal_memseg_list_map_asan_shadow(struct rte_memseg_list *msl)\n+{\n+\tconst struct internal_config *internal_conf =\n+\t\t\teal_get_internal_configuration();\n+\tvoid *addr;\n+\tvoid *shadow_addr;\n+\tsize_t shadow_sz;\n+\tint shm_oflag;\n+\tchar shm_path[PATH_MAX];\n+\tint shm_fd;\n+\tint ret = 0;\n+\n+\tif (!msl->heap)\n+\t\treturn 0;\n+\n+\t/* these options imply no secondary process support */\n+\tif (internal_conf->hugepage_file.unlink_before_mapping ||\n+\t    internal_conf->no_shconf || internal_conf->no_hugetlbfs) {\n+\t\tRTE_ASSERT(rte_eal_process_type() != RTE_PROC_SECONDARY);\n+\t\treturn 0;\n+\t}\n+\n+\tshadow_addr = ASAN_MEM_TO_SHADOW(msl->base_va);\n+\tshadow_sz = msl->len >> ASAN_SHADOW_SCALE;\n+\n+\tsnprintf(shm_path, sizeof(shm_path), \"/%s_%s_shadow\",\n+\t\teal_get_hugefile_prefix(), msl->memseg_arr.name);\n+\n+\tshm_oflag = O_RDWR;\n+\tif (internal_conf->process_type == RTE_PROC_PRIMARY)\n+\t\tshm_oflag |= O_CREAT | O_TRUNC;\n+\n+\tshm_fd = shm_open(shm_path, shm_oflag, 0600);\n+\tif (shm_fd == -1) {\n+\t\tRTE_LOG(DEBUG, EAL, \"shadow shm_open() failed: %s\\n\",\n+\t\t\tstrerror(errno));\n+\t\treturn -1;\n+\t}\n+\n+\tif (internal_conf->process_type == RTE_PROC_PRIMARY) {\n+\t\tret = ftruncate(shm_fd, shadow_sz);\n+\t\tif (ret == -1) {\n+\t\t\tRTE_LOG(DEBUG, EAL, \"shadow ftruncate() failed: %s\\n\",\n+\t\t\t\tstrerror(errno));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\taddr = mmap(shadow_addr, shadow_sz, PROT_READ | PROT_WRITE,\n+\t\t    MAP_SHARED | MAP_FIXED, shm_fd, 0);\n+\tif (addr == MAP_FAILED) {\n+\t\tRTE_LOG(DEBUG, EAL, \"shadow mmap() failed: %s\\n\",\n+\t\t\tstrerror(errno));\n+\t\tret = -1;\n+\t\tgoto out;\n+\t}\n+\n+\tif (addr != shadow_addr) {\n+\t\tRTE_LOG(DEBUG, EAL, \"wrong shadow mmap() address\\n\");\n+\t\tmunmap(addr, shadow_sz);\n+\t\tret = -1;\n+\t}\n+out:\n+\tclose(shm_fd);\n+\tif (ret != 0) {\n+\t\tif (internal_conf->process_type == RTE_PROC_PRIMARY)\n+\t\t\tshm_unlink(shm_path);\n+\t}\n+\n+\treturn ret;\n+}\n+\n+void\n+eal_memseg_list_unmap_asan_shadow(struct rte_memseg_list *msl)\n+{\n+\tconst struct internal_config *internal_conf =\n+\t\t\teal_get_internal_configuration();\n+\n+\tif (!msl->heap || internal_conf->hugepage_file.unlink_before_mapping ||\n+\t    internal_conf->no_shconf || internal_conf->no_hugetlbfs)\n+\t\treturn;\n+\n+\tif (munmap(ASAN_MEM_TO_SHADOW(msl->base_va),\n+\t\t   msl->len >> ASAN_SHADOW_SCALE) != 0)\n+\t\tRTE_LOG(ERR, EAL, \"Could not unmap asan shadow memory: %s\\n\",\n+\t\t\tstrerror(errno));\n+\tif (internal_conf->process_type == RTE_PROC_PRIMARY) {\n+\t\tchar shm_path[PATH_MAX];\n+\n+\t\tsnprintf(shm_path, sizeof(shm_path), \"/%s_%s_shadow\",\n+\t\t\t eal_get_hugefile_prefix(),\n+\t\t\t msl->memseg_arr.name);\n+\t\tshm_unlink(shm_path);\n+\t}\n+}\n+#endif\ndiff --git a/lib/eal/linux/meson.build b/lib/eal/linux/meson.build\nindex e99ebed256..1e8a48c8d3 100644\n--- a/lib/eal/linux/meson.build\n+++ b/lib/eal/linux/meson.build\n@@ -23,3 +23,7 @@ deps += ['kvargs', 'telemetry']\n if has_libnuma\n     dpdk_conf.set10('RTE_EAL_NUMA_AWARE_HUGEPAGES', true)\n endif\n+\n+if dpdk_conf.has('RTE_MALLOC_ASAN')\n+    ext_deps += cc.find_library('rt')\n+endif\n",
    "prefixes": [
        "v3"
    ]
}