Received: by 2002:a25:f815:0:0:0:0:0 with SMTP id u21csp2885184ybd; Mon, 24 Jun 2019 14:40:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqzx9eeRFLD/f0XIZ+6IP83F/fr9OYhE+R7Au5wCwYa6OFZPQVe9BeK4LxLWcbam0YIgz65v X-Received: by 2002:a17:90a:bc42:: with SMTP id t2mr27289266pjv.121.1561412451772; Mon, 24 Jun 2019 14:40:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561412451; cv=none; d=google.com; s=arc-20160816; b=bIdP96JK92ucDhOqHBY67uT2CI1fcFIsd55sXLIunQv9rWpyMluWxWMwzKirwCDjzG QbhWxtIa9JWhZrJPE0wg6H5c/wVc37SoNXrUbgiSxWu5LvcDUpBr40pf0F61bmkW63PW 4vV1N56qoJfnb1+0FGafEK7+3LzrvvPa7420DlGdzmKGjIVvMuCEUNhGL5BIaWKSgHfq Hdh63jbatcQ9GQMAH8DouwiD738DPkMzIWWTqSKm5VW2Qw0UFquirZ59zyMfxoHoovw8 8xgROYWWcuj3n1R0JcigZrKohjcXYoo2CcV19y3p58kLNFS3Cnid16rCQt5cUzl8aqQW pVHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=r7vmIFoMsLv6AsnWiTP/27K1ZwOU/uez0zA/43q6sf8=; b=ywmwh3AFOjdXrsAED4wQyxP/Cyg0fr7V903/7NUK1hg/TWQBbWNhn1g4FHCHFjUuIN qwR971dD/oOZRuPqeUkf7QOzXB9ZIUUHAlxX4K1MH5D7kjqCajqjIyhCgkgwbPJZRSoR 5DHICm+W/6uj6Cy+8Iu8v4THFtNNoQ4/oa/ug2zVwObXTkKdaPtYKHaSSsbN7zBRBU4X f2Rx95TJ9Wl06NklbY9l0REs6+tBx3Hva692WxHiiyd9iia6btIeFvM0G5G6r2J+sPIt 9Dy+lT2dnHy0UbYdxENyV/MjhHcdtW5KkySwSELeUwfktNOI2SyocoF4+IZW9gAKWsYm Nk8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=hV9Hginv; 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 f63si11198589plf.224.2019.06.24.14.40.36; Mon, 24 Jun 2019 14:40:51 -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=hV9Hginv; 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 S1728382AbfFXSp7 (ORCPT + 99 others); Mon, 24 Jun 2019 14:45:59 -0400 Received: from mail-qt1-f194.google.com ([209.85.160.194]:38608 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726285AbfFXSp7 (ORCPT ); Mon, 24 Jun 2019 14:45:59 -0400 Received: by mail-qt1-f194.google.com with SMTP id n11so4371041qtl.5 for ; Mon, 24 Jun 2019 11:45:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=r7vmIFoMsLv6AsnWiTP/27K1ZwOU/uez0zA/43q6sf8=; b=hV9Hginvi+G8TJyLdFMXeusvL3jKgQeZ98y9O6WGRT8Lj4OL7/hTzbF1SbIu9vAQ5y KHr6CiTlqIoq0p/yWkLE00/bQd14rCQytxBXYchlWzPlq8nkHLXnMLYb0b+sHVxaoqSq DE0YShUZYUMvzHlQ3leSkTcQznouO4lQVt3mA= 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:mime-version :content-transfer-encoding; bh=r7vmIFoMsLv6AsnWiTP/27K1ZwOU/uez0zA/43q6sf8=; b=GvAKGRW1QvWxVO8jH3NTiHIC3gcCwefdcOatPMwkdX4dhzpq1lr1jeXSVN7Oqo6ntv t+ff4NZLZCtjuuJtHpC4zmZkoOFaDdcN12VCHkE+EjsSF1wpSJuewPVsIUmc/x2mIHOF Sl5ALlptJLgNFppydNWlxTbBLbwL7bA+ifcK3Y4WYqSjiNCcOPUseNfpAezYs+viJXAy 0UWYcTnJIaDnXcKm+ywiaTOABOQmco4xxbn6L/tx2mGYrfvid2gW8tQcHNTWXs6mO+ik ndts/mAj3aTufSCFSS3BDGVe5w27vqShDJiwsvUaOUs2x8cHbK7UYBE+motkcwTOb1Yq iYGw== X-Gm-Message-State: APjAAAVhlm/bndSVdVMKhe1g/k0o/MFj/n+7iITuGrX8nV+KUaPAZuM7 8FsDRG+P85LkUEtEbSZNMTLt8eDUm1yJEg== X-Received: by 2002:a0c:8a26:: with SMTP id 35mr58659957qvt.158.1561401957565; Mon, 24 Jun 2019 11:45:57 -0700 (PDT) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id f1sm5964663qke.117.2019.06.24.11.45.56 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 24 Jun 2019 11:45:56 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , jannh@google.com, oleg@redhat.com, mathieu.desnoyers@efficios.com, willy@infradead.org, peterz@infradead.org, will.deacon@arm.com, paulmck@linux.vnet.ibm.com, elena.reshetova@intel.com, keescook@chromium.org, kernel-team@android.com, kernel-hardening@lists.openwall.com, Andrew Morton , "Eric W. Biederman" , Greg Kroah-Hartman , Michal Hocko Subject: [PATCH RFC v2] Convert struct pid count to refcount_t Date: Mon, 24 Jun 2019 14:45:34 -0400 Message-Id: <20190624184534.209896-1-joel@joelfernandes.org> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Cc: jannh@google.com Cc: oleg@redhat.com Cc: mathieu.desnoyers@efficios.com Cc: willy@infradead.org Cc: peterz@infradead.org Cc: will.deacon@arm.com Cc: paulmck@linux.vnet.ibm.com Cc: elena.reshetova@intel.com Cc: keescook@chromium.org Cc: kernel-team@android.com Cc: kernel-hardening@lists.openwall.com Signed-off-by: Joel Fernandes (Google) --- Changed to RFC to get any feedback on the memory ordering. include/linux/pid.h | 5 +++-- kernel/pid.c | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/linux/pid.h b/include/linux/pid.h index 14a9a39da9c7..8cb86d377ff5 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -3,6 +3,7 @@ #define _LINUX_PID_H #include +#include enum pid_type { @@ -56,7 +57,7 @@ struct upid { struct pid { - atomic_t count; + refcount_t count; unsigned int level; /* lists of tasks that use this pid */ struct hlist_head tasks[PIDTYPE_MAX]; @@ -69,7 +70,7 @@ extern struct pid init_struct_pid; static inline struct pid *get_pid(struct pid *pid) { if (pid) - atomic_inc(&pid->count); + refcount_inc(&pid->count); return pid; } diff --git a/kernel/pid.c b/kernel/pid.c index 20881598bdfa..89c4849fab5d 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include @@ -106,8 +106,7 @@ void put_pid(struct pid *pid) return; ns = pid->numbers[pid->level].ns; - 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); put_pid_ns(ns); } @@ -210,7 +209,7 @@ struct pid *alloc_pid(struct pid_namespace *ns) } get_pid_ns(ns); - atomic_set(&pid->count, 1); + refcount_set(&pid->count, 1); for (type = 0; type < PIDTYPE_MAX; ++type) INIT_HLIST_HEAD(&pid->tasks[type]); -- 2.22.0.410.gd8fdbe21b5-goog