From patchwork Tue Mar 13 22:07:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 10281039 X-Patchwork-Delegate: sean.hefty@intel.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5D2266038F for ; Tue, 13 Mar 2018 22:07:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 467032580E for ; Tue, 13 Mar 2018 22:07:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B0B528484; Tue, 13 Mar 2018 22:07:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B3CDF2580E for ; Tue, 13 Mar 2018 22:07:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751595AbeCMWHx (ORCPT ); Tue, 13 Mar 2018 18:07:53 -0400 Received: from mail-pg0-f68.google.com ([74.125.83.68]:42097 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751295AbeCMWHr (ORCPT ); Tue, 13 Mar 2018 18:07:47 -0400 Received: by mail-pg0-f68.google.com with SMTP id x2so479520pgo.9 for ; Tue, 13 Mar 2018 15:07:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=YTZ8+ydl+bRdsLg+tXxcsTEAy7pxESycxS+a+iGLVuY=; b=moYTolElfTGV/xNyO5ynWJY+x0I9UXmRHCpnbjvG9JmAvo4RJ1gPDsAiBfqB4iBkCj UJMtzP/yZnc03xNeXoL7q6uJOJq15nbODqMPw6q9lUp30VbIRphJThAKTDSugSpGSZi9 jmpA8rHi2DEnJJs7VOZBRuRhQJ7juLOk0uPnDiLPOZdLen73xg9ev89GC76lnXZ1WFDN W9KLvx84gOnMO8h9KIzz8vdOZBBExeQK7buCO2rChuc3jEmNoQyHze0JYn5lix1ZoTJl qiHX4MPovOnD7/mgw5R1eGzuo1+EWkYMIVxL4oHTk2LtUU28d7PzBJumYddWK/ABCDrk Soxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=YTZ8+ydl+bRdsLg+tXxcsTEAy7pxESycxS+a+iGLVuY=; b=Jw8wakawFcx7upvKPD5I+rvULu65XqDqZWejWX73N3F9qvNyYDfjlbk0sHkp+jTTdD Q6Ot5Ya7G9PRgJuqeW7YrMH+n4+/T27zXVzPja0YtnvtzmmOQg8idK5Xt/1BTDR0sCr7 W8fPLxqnB6/5QO5OgmvUb2xakglbyXiFhXtfTe4S3v3N511rNwLs6MpWlMfeyB4RM9b0 ztCXac1t/3JKEpEMwkO8Mx4e1SdPPss0YoNSSWZ0tU1k1bFWB6dFS+KKt9n5IWBgIf37 akYzpE9/zNJZZUo7Z88Z8TJ2Lz/UwjGmS6z0ciPkOnWkxtBnAfD67UBFmyJzVVvynT38 bBig== X-Gm-Message-State: AElRT7G5F6qOL0Rx+np00shQKsc0lfnR89uSeNq4FSwiDx556HdrokhQ IUloWGR8AurHk944rkMTsU/9X/Om5Os= X-Google-Smtp-Source: AG47ELuilxN804EeO9r7YJ8b06fNVglDyXAFHrL9Uez/w1YxQcSWIHkuEiwpSLCk8W5fSuTz8lAMZQ== X-Received: by 10.99.120.201 with SMTP id t192mr1726675pgc.39.1520978866489; Tue, 13 Mar 2018 15:07:46 -0700 (PDT) Received: from ziepe.ca (S010614cc2056d97f.ed.shawcable.net. [174.3.196.123]) by smtp.gmail.com with ESMTPSA id m3sm1411895pgd.3.2018.03.13.15.07.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Mar 2018 15:07:42 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.86_2) (envelope-from ) id 1evs57-0001B6-7J; Tue, 13 Mar 2018 16:07:41 -0600 From: Jason Gunthorpe To: linux-rdma@vger.kernel.org Cc: Jason Gunthorpe Subject: [PATCH rdma-core 6/8] verbs: Provide infrastructure to remove kernel headers copies Date: Tue, 13 Mar 2018 16:07:35 -0600 Message-Id: <20180313220737.4336-7-jgg@ziepe.ca> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180313220737.4336-1-jgg@ziepe.ca> References: <20180313220737.4336-1-jgg@ziepe.ca> Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Jason Gunthorpe The remaining kernel headers are all inlined into a struct. To avoid major code changes we want to retain this arrangement, but still use the kernel headers directly. This solution is to mangle the kernel headers by script into an anonymous and tagless struct that can be used in place of the copied data. With the support of a few macros the whole thing becomes fairly straightforward. Signed-off-by: Jason Gunthorpe --- buildlib/make_abi_structs.py | 56 +++++++++++++++++++++++++++++++++++++++++++ kernel-headers/CMakeLists.txt | 35 +++++++++++++++++++++++++++ kernel-headers/kern-abi.c | 1 + libibverbs/CMakeLists.txt | 1 + 4 files changed, 93 insertions(+) create mode 100644 buildlib/make_abi_structs.py create mode 100644 kernel-headers/kern-abi.c diff --git a/buildlib/make_abi_structs.py b/buildlib/make_abi_structs.py new file mode 100644 index 00000000000000..029f10428ad300 --- /dev/null +++ b/buildlib/make_abi_structs.py @@ -0,0 +1,56 @@ +#/usr/bin/env python +"""This script transforms the structs inside the kernel ABI headers into a define +of an anonymous struct. + +eg + struct abc {int foo;}; +becomes + #define _STRUCT_abc struct {int foo;}; + +This allows the exact same struct to be included in the provider wrapper struct: + +struct abc_resp { + struct ibv_abc ibv_resp; + _STRUCT_abc; +}; + +Which duplicates the struct layout and naming we have historically used, but +sources the data directly from the kernel headers instead of manually copying.""" +import re; +import functools; +import sys; + +def in_struct(ln,FO,nesting=0): + """Copy a top level structure over to the #define output, keeping track of + nested structures.""" + if nesting == 0: + if ln == "};": + FO.write("}\n\n"); + return find_struct; + + FO.write(ln + " \\\n"); + + if ln == "struct {" or ln == "union {": + return functools.partial(in_struct,nesting=nesting+1); + + if re.match(r"}.*;",ln): + return functools.partial(in_struct,nesting=nesting-1); + return functools.partial(in_struct,nesting=nesting); + +def find_struct(ln,FO): + """Look for the start of a top level structure""" + if ln.startswith("struct ") or ln.startswith("union "): + g = re.match(r"(struct|union)\s+(\S+)\s+{",ln); + FO.write("#define _STRUCT_%s %s { \\\n"%(g.group(2),g.group(1))); + return in_struct; + return find_struct; + +with open(sys.argv[1]) as FI: + with open(sys.argv[2],"w") as FO: + state = find_struct; + for ln in FI: + # Drop obvious comments + ln = ln.strip(); + ln = re.sub(r"/\*.*\*/","",ln); + ln = re.sub(r"//.*$","",ln); + state = state(ln,FO); diff --git a/kernel-headers/CMakeLists.txt b/kernel-headers/CMakeLists.txt index 64bc5e66c21b18..0e163bd86c184d 100644 --- a/kernel-headers/CMakeLists.txt +++ b/kernel-headers/CMakeLists.txt @@ -25,3 +25,38 @@ publish_internal_headers(rdma/hfi rdma/hfi/hfi1_ioctl.h rdma/hfi/hfi1_user.h ) + +function(rdma_kernel_provider_abi) + # Older versions of cmake do not create the output directory automatically + set(DDIR "${BUILD_INCLUDE}/kernel-abi") + if(NOT EXISTS "${DDIR}/") + execute_process(COMMAND "${CMAKE_COMMAND}" "-E" "make_directory" + "${DDIR}" RESULT_VARIABLE retcode) + if(NOT "${retcode}" STREQUAL "0") + message(FATAL_ERROR "Failed to create directory ${DDIR}") + endif() + endif() + + set(HDRS "") + foreach(IHDR ${ARGN}) + get_filename_component(FIL ${IHDR} NAME) + set(OHDR "${DDIR}/${FIL}") + set(HDRS ${HDRS} ${OHDR}) + add_custom_command( + OUTPUT "${OHDR}" + COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/buildlib/make_abi_structs.py" "${IHDR}" "${OHDR}" + MAIN_DEPENDENCY "${IHDR}" + DEPENDS "${CMAKE_SOURCE_DIR}/buildlib/make_abi_structs.py" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + COMMENT "Creating ABI wrapper ${OHDR}" + ) + endforeach() + + # This weird construction is needed to ensure ordering of the build. + add_library(kern-abi STATIC kern-abi.c ${HDRS}) +endfunction() + +# Transform the kernel ABIs used by the providers +rdma_kernel_provider_abi( + rdma/ib_user_verbs.h + ) diff --git a/kernel-headers/kern-abi.c b/kernel-headers/kern-abi.c new file mode 100644 index 00000000000000..cd2941ed829a5a --- /dev/null +++ b/kernel-headers/kern-abi.c @@ -0,0 +1 @@ +/* empty file for cmake */ diff --git a/libibverbs/CMakeLists.txt b/libibverbs/CMakeLists.txt index fd4374d9c8428b..6fdec63ac0c54c 100644 --- a/libibverbs/CMakeLists.txt +++ b/libibverbs/CMakeLists.txt @@ -46,4 +46,5 @@ target_link_libraries(ibverbs LINK_PRIVATE ${NL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS} + kern-abi )