Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp760181pxa; Thu, 27 Aug 2020 15:17:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFw2joGBn4LhiLiMjvpzVaPS1H4xdhtARs+D/Ko0+STBFa6Rc0iXMpWqdSiJVthO9aHN2D X-Received: by 2002:a17:906:d84:: with SMTP id m4mr24187167eji.205.1598566633813; Thu, 27 Aug 2020 15:17:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598566633; cv=none; d=google.com; s=arc-20160816; b=uowCMTvTj6G/LhPZ+hrZpMxaQM03YK1SkulnnD266f5PrGes2h3fDQfAIfEyRozorG EB/dtuU+TP3xJjmhnHKqV0Mkvq1ERl9zej5t5idImzaNXtok9adEyB+oUUbfy54fTHhK YyAB+lorz3/tYzgXaVUNB9FEtK+GEcabgK/K9U4vMTgiBkJjdxARmGW9YlpUYSoqm3XV HrHJtj3cYGqr5RinhA52ljg/zt74VbaPyMFwuek78X3F9sWeEiiSXMAUdk/xy05y99my 11w9R/IUmbuOERfOnuwXyMWraVFZ81lvXbtmY7xmem8a9XQRG2LdnmpXB+rys/DE526u PW/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=xSHVxplRRPCUgSjEzraSIa857XoWFefBlda6XAXIvdQ=; b=KQUEM2+i4hdB4ECscSpn7kyycxlkOombX3bT8QphjIIF83UEqBvFea+Nn0tcpxWG3/ 8t9wwH1W+DmF2VBNcEhDH8AWjE07wXZv3QSYruOx0cz/G4dPldTc9ALJd2PnKV6GJzI0 DjGWrGW1KxnVcUBM9B9a2QqVhf7MlalgIEzZer/zt+qeGepKlbsAXr2yucjdiAEYmrNP Szd8XBXIBy2KbozUkYWltPpbK9T8OtRalotbHNCFLWCk/VonN5WSOJOxNtuQrqO7Uw5d nggcoMXVJNME8LOvBd6s2k/bKnwegXb23/rT1G3WoARMihjhfJz+SWGfvIY/lYcwI/C4 4A3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NQyZty75; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n8si1878107ejj.649.2020.08.27.15.16.50; Thu, 27 Aug 2020 15:17:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NQyZty75; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726826AbgH0WOk (ORCPT + 99 others); Thu, 27 Aug 2020 18:14:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726234AbgH0WOk (ORCPT ); Thu, 27 Aug 2020 18:14:40 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F394DC061264 for ; Thu, 27 Aug 2020 15:14:39 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id m8so4585509pfh.3 for ; Thu, 27 Aug 2020 15:14:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=xSHVxplRRPCUgSjEzraSIa857XoWFefBlda6XAXIvdQ=; b=NQyZty75W6TDW0DbDmCZoFLE4rwRGo93ocgxSw3l2ktgUfDM3EmeMcChBppYodL1XQ bn418nCR3PC0BEtfy5zMYiYPEwaL18pkZHuZ1pGhsn6xyCecKlzEWJCMywDFr46RFzU0 /PJJbr4+4sQvp5Klx+CQweeDbyomn7hyGSjxQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=xSHVxplRRPCUgSjEzraSIa857XoWFefBlda6XAXIvdQ=; b=kOWX7Au1sWF10+fSRFjvdosk31RSuzX9jU5RoJ7XANJwtmPPmvSBtzqWY61XWnvKYW vrmk5eMNXRRT1UZtGFLGy+60pisDLmHZOeGIklhhIKxmH+8zZK3HC6XTZwUS07aLRdgG HSjM5XXD/rK6IaDlux6oM1eNvCBU/4jCM5e1SbxUh10BjFi05t3wY8Op6+4Ip40WPm5l 9SSwV1snasjU/jUtaStSm0OrexAo44h/kXTLADgoUI7kQ5xUeBe6375XMEe7vRbmvBRn B5FKJjS7WldGoIy+1xFtdB34xJeJGw/OT6gvDMug7x4eM0CQdIG8sh22zlWgyDjnAEfM wpaw== X-Gm-Message-State: AOAM531oi2YHd+Kd9ai+ZVl6wLGlzEaZ2xQH9FyC97y0pxmbTySmG7LR gfDBK4CgMUzxaVuhWRiT2qR5Bg== X-Received: by 2002:a63:5ec6:: with SMTP id s189mr2494555pgb.40.1598566479099; Thu, 27 Aug 2020 15:14:39 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id w5sm3467529pgk.20.2020.08.27.15.14.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2020 15:14:38 -0700 (PDT) Date: Thu, 27 Aug 2020 15:14:37 -0700 From: Kees Cook To: Al Viro Cc: Linus Torvalds , James Bottomley , Allen Pais , Andrew Morton , Thomas Gleixner , Linux Kernel Mailing List , Greg Kroah-Hartman , Jens Axboe Subject: Re: [PATCH] linux/kernel.h: add container_from() Message-ID: <202008271503.181A6A609@keescook> References: <20200827013636.149307-1-allen.lkml@gmail.com> <1598553133.4237.8.camel@HansenPartnership.com> <202008271150.7231B901@keescook> <20200827213636.GF1236603@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200827213636.GF1236603@ZenIV.linux.org.uk> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 27, 2020 at 10:36:36PM +0100, Al Viro wrote: > On Thu, Aug 27, 2020 at 01:46:33PM -0700, Linus Torvalds wrote: > > You really have to pick some pretty excessive type names (or variable > > names) to get close to 80 characters. Again, to pick an example: > > > > struct timer_group_priv *priv = container_of(handle, > > struct timer_group_priv, timer[handle->num]); > > > > ends up being long even if you were to split it, but that funky > > container_from() wouldn't have helped the real problem - the fact that > > the above is complex and nasty. The point about doing the assignment with the declaration certainly makes the "ugliness" worse, I agree. I'm still not generally convinced about the redundancy level pros/cons, but I concede that having a common idiom (rather than a succinct but subsystem-dependent idiom) is better for people reading the code for the first time. > > And I had to _search_ for that example. All the normal cases of > > split-line container-of's were due to doing it with the declaration, > > or beause the first argument ended up being an expression in itself > > and the nested expressions made it more complex. > > Speaking of searching, this kind of typeof use is, IMO, actively > harmful - it makes finding the places where we might get from > e.g. linked list to containing objects much harder. container_of > (unless combined with obfuscating use of typeof()) at least gives > you a chance to grep - struct foo *not* followed by '*' is a pattern > that doesn't give too many false positives. This one, OTOH, is > essentially impossible to grep for. And this observation about workflow does strike a chord with me. I do end up with those kind of searches too. In trying to examine my preferences here, I think my instincts are to avoid open-coded types (leading me to want to use typeof()) but I think those instincts were actually developed from dealing with _sizeof_ and all the way it goes terribly wrong. So, okay, I'm convinced. container_of() it is. Doing these conversions becomes a little less mechanical if assignment needs to be split from declaration, but hey, we've got a 100 character line "limit" now, so maybe it'll be less needed. :) -- Kees Cook