Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1178193imu; Fri, 9 Nov 2018 12:12:41 -0800 (PST) X-Google-Smtp-Source: AJdET5eIEUGqev27TWvcvJlXDNLGCKZvqqFgV1h8NHU1os6NlqymG3B92m/kOzMAzEMclCk2yKVT X-Received: by 2002:a65:41c2:: with SMTP id b2mr8629896pgq.67.1541794360989; Fri, 09 Nov 2018 12:12:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541794360; cv=none; d=google.com; s=arc-20160816; b=rV2OtxcMtPn9yxTfNZWyCuElvlwjOfI0WRgssUnxqIRqSq2okgE9I2laWllofWx7l1 Ldt1OokWNhKicSkcMSgJlhutkcdSV58djmkJ7QbreKBZyrcI6GA3mu1UGAh+H+03zBiO yEJNxf1nuLOeCDlayQHGKCalzTTgN+mHyDus564MYCd/8IFJM4ncBiw5lBkkCbq+a5uZ D7NRdzpS+RhnS6NTR1uEZF1+4ocTO6kYG3rxhKQl/pnCjJCag8j+efSi1+umFDDqq1ql 6BuvLrc7jNLcfIQe8075ht1KAzw/8jyl2x3njYrFCeZkgzNpc/CfBFc0LlzTzKSqQ3Dl 9lyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=KqsMx67bOHCyAiorqKLdlhBX6lE34NiEexp6goP1AxI=; b=qHRYhfgGwxVLHYlS0/ddcAIJdVHqs9K2ZiQgZq3fFso8rjGw+CrPvapYu09AZci4Rp iwBsyrLoH+nUXc6SVzvpKmuatOpJI8yHAwBeY5d4EgUALBtkmMhWkfWYtFZ8KEzS5iwZ H1UJ3c3MO+LD6AbfardsS0SRZ1XNX8zFpc4/xJoNAiaBefGQro17NEWHySpnHPH5ni+N K9USXRniC63kWpasEvk7j+ZoEbk2JIXyhjJSE32blAX8Zpv5GRCmjK2uu7XV5fDisjQ/ ImGrwJN7f7I5PxykmqMVvZyO76QjWhyyGEnIOu/DrXdjrSe236a9Iz3Md8663pawK96Q H5Hw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v32-v6si7666940pgk.16.2018.11.09.12.12.24; Fri, 09 Nov 2018 12:12:40 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728299AbeKJFyA (ORCPT + 99 others); Sat, 10 Nov 2018 00:54:00 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34428 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725872AbeKJFx7 (ORCPT ); Sat, 10 Nov 2018 00:53:59 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F35D65D698; Fri, 9 Nov 2018 20:11:51 +0000 (UTC) Received: from llong.com (dhcp-17-55.bos.redhat.com [10.18.17.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8C43D6B8C8; Fri, 9 Nov 2018 20:11:50 +0000 (UTC) From: Waiman Long To: "Luis R. Rodriguez" , Kees Cook , Andrew Morton , Jonathan Corbet Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-doc@vger.kernel.org, Al Viro , Matthew Wilcox , "Eric W. Biederman" , Takashi Iwai , Davidlohr Bueso , Manfred Spraul , Waiman Long Subject: [PATCH v11 2/3] ipc: Conserve sequence numbers in ipcmni_extend mode Date: Fri, 9 Nov 2018 15:11:31 -0500 Message-Id: <1541794292-19425-3-git-send-email-longman@redhat.com> In-Reply-To: <1541794292-19425-1-git-send-email-longman@redhat.com> References: <1541794292-19425-1-git-send-email-longman@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 09 Nov 2018 20:11:52 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The mixing in of a sequence number into the IPC IDs is probably to avoid ID reuse in userspace as much as possible. With ipcmni_extend mode, the number of usable sequence numbers is greatly reduced leading to higher chance of ID reuse. To address this issue, we need to conserve the sequence number space as much as possible. Right now, the sequence number is incremented for every new ID created. In reality, we only need to increment the sequence number when one or more IDs have been removed previously to make sure that those IDs will not be reused when a new one is built. This is being done irrespective of the ipcmni mode. Signed-off-by: Waiman Long --- include/linux/ipc_namespace.h | 1 + ipc/util.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/linux/ipc_namespace.h b/include/linux/ipc_namespace.h index 6ab8c1b..7d5f553 100644 --- a/include/linux/ipc_namespace.h +++ b/include/linux/ipc_namespace.h @@ -16,6 +16,7 @@ struct ipc_ids { int in_use; unsigned short seq; + unsigned short deleted; struct rw_semaphore rwsem; struct idr ipcs_idr; int max_idx; diff --git a/ipc/util.c b/ipc/util.c index 07ae117..00000a1 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -115,6 +115,7 @@ static int __init ipc_init(void) void ipc_init_ids(struct ipc_ids *ids) { ids->in_use = 0; + ids->deleted = false; ids->seq = 0; init_rwsem(&ids->rwsem); rhashtable_init(&ids->key_ht, &ipc_kht_params); @@ -193,6 +194,10 @@ static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) * * The caller must own kern_ipc_perm.lock.of the new object. * On error, the function returns a (negative) error code. + * + * To conserve sequence number space, especially with extended ipc_mni, + * the sequence number is incremented only when one or more IDs have been + * removed previously. */ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new) { @@ -216,9 +221,13 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new) */ if (next_id < 0) { /* !CHECKPOINT_RESTORE or next_id is unset */ - new->seq = ids->seq++; - if (ids->seq > IPCID_SEQ_MAX) - ids->seq = 0; + if (ids->deleted) { + ids->seq++; + if (ids->seq > IPCID_SEQ_MAX) + ids->seq = 0; + ids->deleted = false; + } + new->seq = ids->seq; idx = idr_alloc(&ids->ipcs_idr, new, 0, 0, GFP_NOWAIT); } else { new->seq = ipcid_to_seqx(next_id); @@ -436,6 +445,7 @@ void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp) idr_remove(&ids->ipcs_idr, idx); ipc_kht_remove(ids, ipcp); ids->in_use--; + ids->deleted = true; ipcp->deleted = true; if (unlikely(idx == ids->max_idx)) { -- 1.8.3.1