Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp2894924ybd; Mon, 24 Jun 2019 14:54:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqyGyzOXTw0aL9G/rYU5CnR5o048Fwq8eakIIsp8Qng9mylIJG2yxRvTvcMOBrXPCX/AnV4g X-Received: by 2002:a63:fc15:: with SMTP id j21mr1624907pgi.217.1561413267360; Mon, 24 Jun 2019 14:54:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561413267; cv=none; d=google.com; s=arc-20160816; b=Oor/GfI3m8rIAgXEU0+V8DqdO6W59Y2B5Rn74mPC8RYbpfdhmQY/hQkBSZTmpCiafc nt7zL4jrOcpPmBgsRRkvnugHd0KYz6MW6iYCQRy7lMDuyN9mp55VuCF0uw9bBzaamCjW iRNEjIFIo5eNG8uV2DX6nBQ1zqdsaVcl27LUlUaeci9IrQtBCloE54QMQUQcrHROSIt2 p9ScBSoWYhpSjQ6zQD/V+aC3TIH07IIIpJQ+1MWGyOACE0Va/HIkDSPmfy/NmLHQLrLq 2f5AcITGXM5ozGShdv4t3YnHFFLVupnDIYgq2ULQOV4sebdeoBjq09clvMbwhWyNYO/F ZQJQ== 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 :in-reply-to:references:mime-version:dkim-signature; bh=6efmcv3HY90Ho573RNTB0lMwiTQ78McbsH3sWRMqrYk=; b=ZuKJsSfgQzrkywn7drOs8Ow92lT1tGmdvIrkIy1wqvloyObhEDocjEU+CSpsymMLPT 9eb7K6AmGCxPWp1P38GsnAfxOAqLtTsxp86MQOT4wxgxhT9aQiOEfxp+s2L4AN6/GnAg ELHG8jdVnyb/IP0+nveh8CqOiz/7YDSf/x5diLezwkTHvyruQYA8/cVfC3Uikw8R0JIr CPM6AAUFY/s3S3ePlAmpsxgTkyGCwpKwMVHx3zYq9J7unAG7k+CZRSZXfuB/qgMhuQaf WUuKS/nDU09rg7paDeDY4PSwMBMNrmQvJ4FPnTWouRj5L/Nd72XNWpfqBtzeQr3WFvpJ 41mA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=v96EkVK0; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x20si2654478pll.105.2019.06.24.14.54.11; Mon, 24 Jun 2019 14:54:27 -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=@google.com header.s=20161025 header.b=v96EkVK0; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730172AbfFXTKm (ORCPT + 99 others); Mon, 24 Jun 2019 15:10:42 -0400 Received: from mail-oi1-f196.google.com ([209.85.167.196]:33725 "EHLO mail-oi1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725881AbfFXTKm (ORCPT ); Mon, 24 Jun 2019 15:10:42 -0400 Received: by mail-oi1-f196.google.com with SMTP id f80so10634903oib.0 for ; Mon, 24 Jun 2019 12:10:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=6efmcv3HY90Ho573RNTB0lMwiTQ78McbsH3sWRMqrYk=; b=v96EkVK0rNaZLFxkF7opm/k0qR7ZGCW1EEBz6MJCo2tipnxwyTyODH+yLTEszkDcIQ NHQcjyr+c+mCBKXqHh0Fil2CcQLb8YFQU/Ksf1c+N61qsIoRjLymFo4BDX1PcblcIznf qjMucXWC2ExPQvzYgHNCFcFpiR63f8dr4LJVvRdkX/iHYoc9YrLKT9NClTlMAyb7cmJI Zc9EHtzZOXhZSVY1Bv6pTAALVcCCaXT+UXCPqx1dAE+oAS30WbI/r5i9cs3ALUSz5lcs lZo2GFSpntTnejnszDUgtV1QuiTqPU1jWyptpkyvyTC99a3417aIPvRonjbeNsbi0Z3d +bVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=6efmcv3HY90Ho573RNTB0lMwiTQ78McbsH3sWRMqrYk=; b=GIKe2d8toKi/RCWSzZ1aeSeSv1dd099ZFdcQZvk2bXVNlT2DkbUExN+IdJ7t1ZsX1X /GR0DP9rwpJ0VIwKnvrTpZgxWTT0L7a/voT+5AZ+EMkDwq6amBZfFefGtjkUpMSAlRCu +6VLnUMRKW4eOpVvhdFB2XHEegABePLhkQmtQP0XAkpeqykn40LCeNEreIcMMQAm3K3w hY9ICetBRveyTB+VwGxGh8i3qRd1RvVozMjTRZnPZ76rd8SsFWURRSYXxoGrp9m3HXky XLp9HV6y3u1h6Gv3msix54LIZBi7u6dcWcO1V4bAIZ1HG8TW+u1wVDtv0cVcs/KEyzdD x2aA== X-Gm-Message-State: APjAAAVZeRZxPBfhisMUdEmt8Koht4DEetVBnbWqM0VYfQdgWB8zIQj+ hglvp1SSQn5UhOWdgvkTHF/Ql1E+0zp2QVpBS75JushKkNw= X-Received: by 2002:aca:1b04:: with SMTP id b4mr11524190oib.157.1561403441214; Mon, 24 Jun 2019 12:10:41 -0700 (PDT) MIME-Version: 1.0 References: <20190624184534.209896-1-joel@joelfernandes.org> <20190624185214.GA211230@google.com> In-Reply-To: <20190624185214.GA211230@google.com> From: Jann Horn Date: Mon, 24 Jun 2019 21:10:15 +0200 Message-ID: Subject: Re: [PATCH RFC v2] Convert struct pid count to refcount_t To: Joel Fernandes Cc: kernel list , Oleg Nesterov , Mathieu Desnoyers , Matthew Wilcox , Peter Zijlstra , Will Deacon , "Paul E . McKenney" , Elena Reshetova , Kees Cook , kernel-team , Kernel Hardening , Andrew Morton , "Eric W. Biederman" , Greg Kroah-Hartman , Michal Hocko 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 Mon, Jun 24, 2019 at 8:52 PM Joel Fernandes wrote: > On Mon, Jun 24, 2019 at 02:45:34PM -0400, Joel Fernandes (Google) wrote: > > struct pid's count is an atomic_t field used as a refcount. Use > > refcount_t for it which is basically atomic_t but does additional > > checking to prevent use-after-free bugs. > > > > For memory ordering, the only change is with the following: > > - if ((atomic_read(&pid->count) == 1) || > > - atomic_dec_and_test(&pid->count)) { > > + if (refcount_dec_and_test(&pid->count)) { > > kmem_cache_free(ns->pid_cachep, pid); > > > > Here the change is from: > > Fully ordered --> RELEASE + ACQUIRE (as per refcount-vs-atomic.rst) > > This ACQUIRE should take care of making sure the free happens after the > > refcount_dec_and_test(). > > > > The above hunk also removes atomic_read() since it is not needed for the > > code to work and it is unclear how beneficial it is. The removal lets > > refcount_dec_and_test() check for cases where get_pid() happened before > > the object was freed. [...] > I had a question about refcount_inc(). > > As per Documentation/core-api/refcount-vs-atomic.rst , it says: > > A control dependency (on success) for refcounters guarantees that > if a reference for an object was successfully obtained (reference > counter increment or addition happened, function returned true), > then further stores are ordered against this operation. > > However, in refcount_inc() I don't see any memory barriers (in the case where > CONFIG_REFCOUNT_FULL=n). Is the documentation wrong? That part of the documentation only talks about cases where you have a control dependency on the return value of the refcount operation. But refcount_inc() does not return a value, so this isn't relevant for refcount_inc(). Also, AFAIU, the control dependency mentioned in the documentation has to exist *in the caller* - it's just pointing out that if you write code like the following, you have a control dependency between the refcount operation and the write: if (refcount_inc_not_zero(&obj->refcount)) { WRITE_ONCE(obj->x, y); } For more information on the details of this stuff, try reading the section "CONTROL DEPENDENCIES" of Documentation/memory-barriers.txt.