Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp2902530ybd; Mon, 24 Jun 2019 15:03:48 -0700 (PDT) X-Google-Smtp-Source: APXvYqxS/Tnct4EFBWFKW6p6TYUNrrKhd15zttnv1XUprwSob9/oMt2lyOXZ88xdRp9o6qEWHi0y X-Received: by 2002:a17:90a:2666:: with SMTP id l93mr27057892pje.16.1561413828671; Mon, 24 Jun 2019 15:03:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561413828; cv=none; d=google.com; s=arc-20160816; b=JCj4E+2R4fXE9WTFM44HcB8e5wpv7qsQ++zVgrcAPi8cspiWViya5iIapxnvCSiZQ+ 11ofUasCQpcMCot6Z+X+jdJOyaP4GCN5RcV7lnMINkD9bkzwdaAWtwN4a4WVhmiek0ze TIliZ58aH8YA1bp+hg5e57QNwEAtd8I/MQbUxCJw9Wwfe8Jm7jjFbVJv77mpXCtDhOGp EVeUi/qmRVQVWERSJkUttzZKQiGqhwW+psE3vQAYFL1g0ffBb02YezokV2z5j50cFJx/ Mb0Zd475k/5UVQ46nsY69iVOZNReinoWhQNu1k+7kidRXC1VMzA9pLyduta4qfzzLvns uvNw== 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=78Ty9brXQcBAfvxld/f2zy+UAZIXu4cST4SKUNq/ZtE=; b=AMbSBt1JQRH6hLWAjH7Ds2OdcR+oXncufzSmXUYN64IqZAF8ofgUiJo+8pRf+a0uXP qMo7/XTpmzmAxEo7DsKFQlo9Zh0I9UqCa5b4VO19UtIvBf4VMJRx1yI8/gEprxUHgo7h eLAzkMbXpqDQFuQUSaiwRsmQWu93ZE+ppQtphIUeBNiYTNRHpAQ9rZDfmt/0UK9aNwm7 4lgv2FtN3gs03B9WLG35zzxRBnYJvMB/AKHkWmm77waiBd/dbSt8D37aT8gTnnaLHtg2 /wGBrGpiEhlFRWp76enitUj8BuRWbEBN93hgGI2gTuz8ygnyiJLU4z3wP5mZBL6E/zyZ 6D/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=xKiw1Ebn; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1si11243751pli.151.2019.06.24.15.03.32; Mon, 24 Jun 2019 15:03:48 -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=@joelfernandes.org header.s=google header.b=xKiw1Ebn; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732048AbfFXUng (ORCPT + 99 others); Mon, 24 Jun 2019 16:43:36 -0400 Received: from mail-lj1-f195.google.com ([209.85.208.195]:46815 "EHLO mail-lj1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726920AbfFXUng (ORCPT ); Mon, 24 Jun 2019 16:43:36 -0400 Received: by mail-lj1-f195.google.com with SMTP id v24so13919107ljg.13 for ; Mon, 24 Jun 2019 13:43:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=78Ty9brXQcBAfvxld/f2zy+UAZIXu4cST4SKUNq/ZtE=; b=xKiw1EbnAnfUc3KMYQZj1husi/5d7wyoTv61YtaUEGlAk6+Cx6H9nk+XtVFoVu8wol rz/1NwN++wuI6Uhe/UEaU6btO6xG82p4eMw84Xv06jebmDuhfsLsYfOQSTCTvKeKTFh5 eFsNc5HO2n5oLw6Lv9jbXl5F20CICgwhKQhIo= 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=78Ty9brXQcBAfvxld/f2zy+UAZIXu4cST4SKUNq/ZtE=; b=N3XBDwTwfqG+KmCHmrS/dEr5VAT99k4ICaMYwwymXawAs+2NLuvTJXDFa1+a1DC5ON lZqdPSV2xk+NlYxH3XCdM2x5vxLpy/3aEVW8gDFpNMlYTN1WO9U+y4omGXYQXGacEGlo GHrVbdsMtpM1MIDfZ8SlatI7GK9IEVHvkU+D0YtIfU3tQAFHrzG3hDki74cBSibkHIDZ vc/7LkNiSObDQvopZeV4hGbjKph1mzBKPc5Wh1zPYQhpjF5mN5avb9xX7vy4M/coH07m u8g+PGdi19MZ9yI/M0kdv5jYudqN2Rp1wupuRk0m2hawTvPGlh+ClnPnHLxclQJcgRiq dX2A== X-Gm-Message-State: APjAAAXcgtiwPRAcGfnbU9/hUdL+PtLsoNBgzRG+H4u1agx6CBLO3/BY kV2tgfCdmCWuaIT76+XH6BxMudaD1q+ZqvYGxZG/7w== X-Received: by 2002:a2e:8583:: with SMTP id b3mr64474038lji.171.1561409013254; Mon, 24 Jun 2019 13:43:33 -0700 (PDT) MIME-Version: 1.0 References: <20190624184534.209896-1-joel@joelfernandes.org> <20190624185214.GA211230@google.com> In-Reply-To: From: Joel Fernandes Date: Mon, 24 Jun 2019 16:43:21 -0400 Message-ID: Subject: Re: [PATCH RFC v2] Convert struct pid count to refcount_t To: Jann Horn 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 3:10 PM Jann Horn wrote: > > 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. Makes sense now, thank you Jann! - Joel