Received: by 10.213.65.68 with SMTP id h4csp2171978imn; Mon, 2 Apr 2018 02:29:35 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/FbPB3lvZ3DhgYyLFYbsuMxSego9vDDhZI8wFURUEb3H3XAByjyphU4DLlYQaKSnKoY0nt X-Received: by 10.99.124.79 with SMTP id l15mr5823297pgn.19.1522661375117; Mon, 02 Apr 2018 02:29:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522661375; cv=none; d=google.com; s=arc-20160816; b=ZEhfeVuxFElT0cU86m9NrYT9XxtmyRscuZBm7TDEqjyNhr8ruBL/M3gvUo6hA6y8Xo TlXCP1c24DE63vt1bb6kdM+/ZtyvC4F7uSdzBEAl0q6gV7Y/3D6jIElkL6Wpt6VZZkfT Di0V9X7sWk9cfeOZu6UXBGb4hCz0f/Lm3IjIFumJcUcATGUhrvLP6gQYEX6fXXGOwgCw Q6RtPRNE5sTMmB5q9Zze88Jxcn8ik4E6ewamOug0ntM1aPetZsHEFdn1HRTLzIl7Hip8 cdMsnPejthnZVXIIjgnjWMftjlXkevROuhfCy3dlI6yb0LUs3RFcsdRE4JlJsgEwkDPv gD4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=dAUDK2g3EbR3gdJWH0XJvA4jgVy0+HOOPRRgSIEgjqk=; b=dIOPPNmAGxWvDrVWop1UrbZv5ZYxeU/x0mdYVKYkNFJX9jJqR9aB2ahHGFGXnaAVxI 7MXKLubmL8cE2Psp47HmYiDqaK32p1Tkbn6vmkW2NbxapjPKYhbzePSIR5ZM4ND5veEB spzjYwom8D8J041HovV97o2jLoLpIG0dnn5jsBQCo8lo6vjccrnbTp2muNPldtcE1ZM+ nVe2JuojylZKpSDEkqBzNsytfSfacgT8odwXk7VNptMsG/Xjh0KmjGr6p0RA7CJ6ehNW 5edlPk+Nb66UVFsTgWXlkA9JFfLH4jMGs0Oqm6ajJ2rSsXRVxKZuBRiZxns2nHcjX+7a QVaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EqZbs+Ss; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f9-v6si162975plo.664.2018.04.02.02.29.21; Mon, 02 Apr 2018 02:29:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EqZbs+Ss; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754315AbeDBJ2H (ORCPT + 99 others); Mon, 2 Apr 2018 05:28:07 -0400 Received: from mail-wm0-f54.google.com ([74.125.82.54]:36820 "EHLO mail-wm0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752420AbeDBJ2G (ORCPT ); Mon, 2 Apr 2018 05:28:06 -0400 Received: by mail-wm0-f54.google.com with SMTP id x82so25779252wmg.1 for ; Mon, 02 Apr 2018 02:28:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=dAUDK2g3EbR3gdJWH0XJvA4jgVy0+HOOPRRgSIEgjqk=; b=EqZbs+SsSL5upq7pDGgB8nXiDnL3c5E63+h4F5fL3yRiwO0A7sjLGcAVC1LE8Vonjp N7seIWsSfImDkhNF1jZ+U2v5jgoEjuAklH9+kPqSJz2dWgjIl7FeUBE39u2N3uXVSpRh F8yvh6SGsqRRpIU2qeFoE9EEInJzdWQixGPCnWuhYmws4LVW4J3ayJ+jfVROVVxIVJBx bVOWbNYOhYZBFHUAgNEJYdQlM42sYJJHUp7WoOsXDk45cx3QTrXAB5WF4VwACDdTCZm8 Cgw6icKFW/D2qrf3us5MIt+OHFrYZXY5vxEOIEqkmtAWC43IuYGXRFaEZ7keZsK92Rui v6Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=dAUDK2g3EbR3gdJWH0XJvA4jgVy0+HOOPRRgSIEgjqk=; b=Yf36qll1xzJK8g66dbEmQMYrux9INLnTR4Dz9znamoAQMe2BR7B84SK0vZtRxYsZUt g2cjoxoNuSt4uFMzGEDnlKum0alkNHRf/VVamMoeAGLhuQx2pzAID3pHyf9nvrrJyZe8 euBz0dRbidbVFI2VP1D92azKQaDwmkg/2WX/9WBPSvMT/IZffv95oHdJz7iHUlOQc8OQ jX5d3LHbMlQ/Cteg5FP2UZlkrYo1abcvW6r4+ZPmurRMm+ngouq2PghO1pcnPyZ/dG4Y 9oGe3Dy2C9ZBtJGyTT4+FPfd0BhUse2hdw/Yxn6+gA7NtmSkAnyvc7U5gsRgZUVua2pt 1i7A== X-Gm-Message-State: ALQs6tDawRe3D5o/kwDp1NU6YDke4JMiB0xhiuH7+s4kacwlCptDTPGl aQQSgaKlIrJ8Va+NIWmhhi0m7jgzEshjTZV0mN6syJLT X-Received: by 10.28.153.213 with SMTP id b204mr322121wme.79.1522661285417; Mon, 02 Apr 2018 02:28:05 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.160.75 with HTTP; Mon, 2 Apr 2018 02:28:04 -0700 (PDT) In-Reply-To: <152261521484.30503.16131389653845029164.stgit@warthog.procyon.org.uk> References: <152261521484.30503.16131389653845029164.stgit@warthog.procyon.org.uk> From: Vegard Nossum Date: Mon, 2 Apr 2018 11:28:04 +0200 Message-ID: Subject: Re: [PATCH 00/45] C++: Convert the kernel to C++ To: David Howells Cc: LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 1 April 2018 at 22:40, David Howells wrote: > > Here are a series of patches to start converting the kernel to C++. It > requires g++ v8. Nice! I tried something similar a few years ago, but I don't think it was nearly as neat. I did get RTTI and exceptions to work (using libcxxrt + libunwind), though. Having noticed that a lot of really trivial kernel bugs are due to control flow issues (e.g. when somebody adds a possibly-failing step to a function but forget to add a new label to clean it up) I really wanted to see how/whether exceptions and RAII could help in that space. Just in case you want to compare notes, I've pushed my branch to: https://github.com/vegard/linux-2.6/tree/cxx I also started a little bit of work on converting a driver to use RAII, and quickly ran into a few problems: C++ destructors don't take arguments, which means that some objects would have to carry extra state around because some of the information needed to destroy an object resides with somebody else. This means that you would have to do more refactoring work to avoid needing this in the first place, i.e. mapping creation/destructing of various C-style structs to C++ is _not_ straightforward. Take dma_alloc_coherent() for example. It pairs up with dma_free_coherent() and that one needs to know the device and buffer size that you passed too: void *dma_alloc_coherent(struct device *, size_t, dma_addr_t *, gfp_t); void dma_free_coherent(struct device *, size_t, void *, dma_addr_t); This means that if you have 1 device using 2 buffers of the same size and the size is stored only by the device struct, then you must always do the destruction from the device struct, since the individual buffers don't know their size (unless you move the member there; but it feels like a waste of memory if you could do it just fine in C). Maybe there's a "proper" way to do it that I didn't see, but problems like this turned me off the whole approach a little. Another real bummer is the size and complexity of the RTTI and unwinding support code. First of all, unwinding requires parsing and executing DWARF code on the fly, and that just makes everything very slow. Not to mention that it needs to be threading-aware and does a lot of memory allocations. IIRC handling out-of-memory conditions was extremely ugly (not that the kernel is perfect in this respect to start with) and involved the use of "reserve buffers". I didn't like it at all. Also, for reference, I found a few other projects doing similar things in the past: https://github.com/veltzer/kcpp http://www.drdobbs.com/cpp/c-exceptions-the-linux-kernel/229100146 https://pograph.wordpress.com/2009/04/05/porting-cpp-code-to-linux-kernel/ https://www.threatstack.com/blog/c-in-the-linux-kernel/ There's probably more, I seem to remember at least 1 commercial product using C++ for their out-of-tree module (albeit without RTTI/exceptions), but I can't find it right now. Vegard