Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp649436imm; Mon, 9 Jul 2018 08:13:24 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfiEZfQPnNo6javGfBUKQen9E7E77a4y6pK87jxx70CXBjUhEyHwjxLtKaJ7lrvuv1gBD9h X-Received: by 2002:a17:902:722:: with SMTP id 31-v6mr21390239pli.3.1531149204151; Mon, 09 Jul 2018 08:13:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531149204; cv=none; d=google.com; s=arc-20160816; b=sJy3nTOfEaZdVNpAqC6BYCFuRO9OkZmJmkCEh7vyc/s1LAygGmW+0njofeRpvCNWGn 2L30CUYcM8XrVAG+qhjwoNGgpPQI9B+8DfPKaes+sCWyw9CJb8eCopec7BtVkr4F0bIs OnaFIDHjEmqR9cfWvPr+jPFzrqRy4uYVQoe97zyH17UeN6tePjPL9mVOfN7m7dDAA6Tw FMwmc80EwEej8W5K0BXxaczPBeiLrRMUiPiQv33T9v5sr2r9p0OAt5Zj2J+7kZ5DKeHF fQ6wkGhIER/ZNhuBmSGIU1VxWvR3XCpyEucuz6YV/QHfPVi5q9zOjZJgPcNHC/nxpLkP IVjg== 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:dkim-signature:arc-authentication-results; bh=rIOzyEFvIQL2DQ4Z97T2nwYUV09j+BmZWHxNQoPoP4M=; b=B4Dwoc8CVbrwcWGLGICQ7fpewm4pANgcwhjLoaV3whA49e79cmPnyUQt3ciSGL/U29 4+RmaLGj86EDkc8qLZRg7Jm14htYw15WwbX6ZIV0vwRtadiipJQ60/EF0b7cXItzKjId C3pdT5EJKRqTpZRns5ZBiH7jmDyMDP6Zj1KOkVq3xhTJRicb2V/EcTgmrevnIWkyDYG0 GCKq3wG3+RJtmKWTBZ2yKwpxwMt8gE4ERuxDylTPwW0cLAC1ErG6wVXxjowXRfDGeOJW DTG1xxJlG0vvoMOqtGFPFYwlPlowcOf1IezaCj2jd0u7bB5D+hZoYl4RKnPHzMWtB+JL Rkvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=g1GzJJHd; 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 a26-v6si13554237pgf.557.2018.07.09.08.13.09; Mon, 09 Jul 2018 08:13:24 -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=@colorfullife-com.20150623.gappssmtp.com header.s=20150623 header.b=g1GzJJHd; 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 S933377AbeGIPLN (ORCPT + 99 others); Mon, 9 Jul 2018 11:11:13 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:40215 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933092AbeGIPKn (ORCPT ); Mon, 9 Jul 2018 11:10:43 -0400 Received: by mail-wr1-f66.google.com with SMTP id t6-v6so11380910wrn.7 for ; Mon, 09 Jul 2018 08:10:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colorfullife-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=rIOzyEFvIQL2DQ4Z97T2nwYUV09j+BmZWHxNQoPoP4M=; b=g1GzJJHdS3H2hDBEimn3TR73pvFHLA8fwQF9wPE+7whpEEtFqQXg+LlHtvzS/7Arkj xCr8jtkXwFPVgFmd2Q8CW59Ka0npCH6U3YW4hjZd8aSA73Hy0jOsatRoWTul+kUvryjq U/rIazOHsCXjQeZUqv3GXq3fxW3pJ97HOGJyl/UQSsZW6YTHmXl/DoO3FWB+r3Xo9LO5 BEUQd/sArzmFsOrL9Dyi+pvtE6zWMT1rFFLh5FMIB7bpL7cSTJ5GFkirMyu12qe6Bn6x yK3dMzV0ME7SIx7xKtwZLaVJASdgjKD2OYRrFrOLP5YZ5kLLGn9midnqboIta1OPjeN8 sxWQ== 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:in-reply-to :references; bh=rIOzyEFvIQL2DQ4Z97T2nwYUV09j+BmZWHxNQoPoP4M=; b=YtP7lClHcc436pOqkhNbDK3o5XhJOxTw/RQczsKP/hV3PjsVCeq4313TAn/qMIM9dg dSmgm/iKUOOUXIU+RcyZX3pnruePjvdv04SNOvfVqQSdQzYbOOllbBhafx84d3fIb7So 47EtNHS7wrLxV5uJdJkPt6Lx9af4TizcEkaHXJCS/upMxVpD9omHuce0CO/+Y9SVLICt tuebDJb+hv6Thjr2rjbupJSKaC0IPKKdH9CTgGqm9iGf8LZNsbbkzh/M00JAHjFCHqJ7 vACWfJoID4yyvXzr2RHX5ZAcg5+/IPrLVRtZLC9sHtY1kBuL0NvcFBbZfMriV4THCOCT KuXA== X-Gm-Message-State: AOUpUlGS5IcxDhGILPHH4jRKtUJVlxtp9DVtFL/lsjSt39UVgnroB6Qs zxju0eDp8wCQPxV3eW2sEbPOWw== X-Received: by 2002:adf:81c3:: with SMTP id 61-v6mr2553765wra.120.1531149042910; Mon, 09 Jul 2018 08:10:42 -0700 (PDT) Received: from localhost.localdomain (p200300D993C227000209466FFA2F090C.dip0.t-ipconnect.de. [2003:d9:93c2:2700:209:466f:fa2f:90c]) by smtp.googlemail.com with ESMTPSA id u124-v6sm7817330wme.26.2018.07.09.08.10.41 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 09 Jul 2018 08:10:42 -0700 (PDT) From: Manfred Spraul To: Andrew Morton , Davidlohr Bueso , Dmitry Vyukov Cc: LKML , 1vier1@web.de, Kees Cook , Manfred Spraul Subject: [PATCH 12/12] ipc/util.c: Further ipc_idr_alloc cleanups. Date: Mon, 9 Jul 2018 17:10:19 +0200 Message-Id: <20180709151019.1336-13-manfred@colorfullife.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180709151019.1336-1-manfred@colorfullife.com> References: <20180709151019.1336-1-manfred@colorfullife.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If idr_alloc within ipc_idr_alloc fails, then the return value (-ENOSPC) is used to calculate new->id. Technically, this is not a bug, because new->id is never accessed. But: Clean it up anyways: On error, just return, do not set new->id. And improve the documentation. Signed-off-by: Manfred Spraul Cc: Dmitry Vyukov --- ipc/util.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/ipc/util.c b/ipc/util.c index d474f2b3b299..302c18fc846b 100644 --- a/ipc/util.c +++ b/ipc/util.c @@ -182,11 +182,20 @@ static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key) } /* - * Specify desired id for next allocated IPC object. + * Insert new IPC object into idr tree, and set sequence number and id + * in the correct order. + * Especially: + * - the sequence number must be set before inserting the object into the idr, + * because the sequence number is accessed without a lock. + * - the id can/must be set after inserting the object into the idr. + * All accesses must be done after getting kern_ipc_perm.lock. + * + * The caller must own kern_ipc_perm.lock.of the new object. + * On error, the function returns a (negative) error code. */ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new) { - int key, next_id = -1; + int id, next_id = -1; #ifdef CONFIG_CHECKPOINT_RESTORE next_id = ids->next_id; @@ -197,14 +206,15 @@ static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new) new->seq = ids->seq++; if (ids->seq > IPCID_SEQ_MAX) ids->seq = 0; - key = idr_alloc(&ids->ipcs_idr, new, 0, 0, GFP_NOWAIT); + id = idr_alloc(&ids->ipcs_idr, new, 0, 0, GFP_NOWAIT); } else { new->seq = ipcid_to_seqx(next_id); - key = idr_alloc(&ids->ipcs_idr, new, ipcid_to_idx(next_id), + id = idr_alloc(&ids->ipcs_idr, new, ipcid_to_idx(next_id), 0, GFP_NOWAIT); } - new->id = SEQ_MULTIPLIER * new->seq + key; - return key; + if (id >= 0) + new->id = SEQ_MULTIPLIER * new->seq + id; + return id; } /** -- 2.17.1