Received: by 2002:a05:6500:2018:b0:1fb:9675:f89d with SMTP id t24csp333526lqh; Fri, 31 May 2024 02:46:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWZAzxIhYHjd/R5ADyXGYmEKrxrLVDA5BH8Xw+hWoNbgm9JXIg+rRNvbQWmWZTysY2ZfMzSwhmxjjmcFVCSy4JhiKlhV73rBTpLUN0ovw== X-Google-Smtp-Source: AGHT+IEnxE3BCvwKSfoEU4xA3vUkqTQPe3CCojl8qBH2Ku6sIyTQ9XMafsrylim/YQUjEWNrmbOS X-Received: by 2002:a05:6830:33fc:b0:6f0:577c:b29c with SMTP id 46e09a7af769-6f911f30713mr1320127a34.15.1717148776578; Fri, 31 May 2024 02:46:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717148776; cv=pass; d=google.com; s=arc-20160816; b=i0kcWsMBXpM39Y5MzgEvTcwLCU6XW9b/ws/0TH0fc7HHLlPDRCV51AwKeaY/q6posE wQwFLg+OayeYQGTYPVQ4NmFK8Y1UEKW2CY1SSMI6tYK8Qq9SSw1yLya5psz9Tr9AUhye 5V5z9Ri2DBYYD6/idI83Epcffi0F6min+E6c1+rPVijpDItmhDS9YRt8gTSRMS3pk+sz ckarMBPAlLV1QycWRK+8Z4UlV+9oH58iILdq6aj6g50tE01duYmOhh9UwTa6OHsUz+MQ n/P2Mtldml+lQxwzRjT5xFrMbz/rEqg+5hv7sSWdOy5f/1USprZg2vAq60Mj6E4STb/E 6pRg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=DVVQl3ZHyFGVE+EIxPt01UrwGP4+YpumGHdTM8K0sH0=; fh=lVBoZbjI0HAsvIgHQi3gdpXSjAivQC3KdznuJOYENbM=; b=Ek33NltSZ8Qf1PFnLgvaRWr7/o/O6AE//YduWvyH6YoUAE3DfthRuFIZe1xnyT6hfI ecYKRt/5FNm2YnKce6xwRbEH+vhaT35LWdb/LqqG3HYSGCivhHXTylt43+hfdmWuK0le jWxm1l0NbonbyeABYaqO8aTawX3mielJjuN/cxk32vQfNWEygh1ckTCYUP1rsUKyfPgH hOG7wEOiYx2BM45VPwL3FCnzkPDImTXDFB65TcAZ5At/iYiD1NTnW1eeFrCQXHMK3BSk ub4vIRKnuqKj0p+QGWQ5oSLDoymu5yNDsNJBNKhOE99bjsl96nUL4XQF3OAkUmuBfLB3 gE3g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=eEaFYA54; arc=pass (i=1 spf=pass spfdomain=amazon.com dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-196618-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196618-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43ff24673fcsi16551221cf.308.2024.05.31.02.46.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 02:46:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-196618-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=eEaFYA54; arc=pass (i=1 spf=pass spfdomain=amazon.com dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-196618-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-196618-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 2EF8C1C2168E for ; Fri, 31 May 2024 09:46:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 40E7A1422DA; Fri, 31 May 2024 09:45:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="eEaFYA54" Received: from smtp-fw-80008.amazon.com (smtp-fw-80008.amazon.com [99.78.197.219]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A15131422A5 for ; Fri, 31 May 2024 09:45:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.219 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717148751; cv=none; b=Kjeq6taPa7Uy0eZ9QW9MB63kJbJ3rF4C+icxH70i54Uu7wI+04DhRyPnmhtNlP8I56J2jRh7BTvt6gYmsR9yPcbsQBjSdQ9fNwLrpqJxmUt5vHPPPVLAQBaqmVFxXLsVJRH6iNiRMwZeWngaQ/F/Xwnbpm81ukiMcOclWkNR4H0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717148751; c=relaxed/simple; bh=SEJuXy0foCRsGs/IifrpkObXXd25A36ZEA4zEolK/Nk=; h=Date:From:To:CC:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cYkFWNJMyDjLpazPLcLGlXei/axkRVoCU9YaS8X8E+f2Fx93nmXCEr75v8vgrPmF0FkMK7zWTyBVL4kjjfc3vwpHMkuKxbVqos7t3M1uRre4OwLNssWf8zouSYp1DIVHN1RugQlvZhI9+eEfonDlqi7ljM6fUBEX3ohCkB0+fV8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=eEaFYA54; arc=none smtp.client-ip=99.78.197.219 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1717148749; x=1748684749; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=DVVQl3ZHyFGVE+EIxPt01UrwGP4+YpumGHdTM8K0sH0=; b=eEaFYA54TemklDFkokko1YVsRBeA5fQtsbGMCxJO6qemxj/rFPyzkYYH 5EVSTEqxxeUtedgc099kGxnPw/Fl7gRdTohj+wir0SNHl4PAVU9029AXc J9Qykvr/AziP80Eof983OFLKxQ5wxB+kaSqp3kmXoioe4v+0KSa7n8BHz 8=; X-IronPort-AV: E=Sophos;i="6.08,203,1712620800"; d="scan'208";a="93189632" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-80008.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 May 2024 09:45:46 +0000 Received: from EX19MTAEUA002.ant.amazon.com [10.0.43.254:5001] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.39.158:2525] with esmtp (Farcaster) id fbbfbe37-de1f-4092-b44e-f5dab31828cb; Fri, 31 May 2024 09:45:45 +0000 (UTC) X-Farcaster-Flow-ID: fbbfbe37-de1f-4092-b44e-f5dab31828cb Received: from EX19D002EUA004.ant.amazon.com (10.252.50.181) by EX19MTAEUA002.ant.amazon.com (10.252.50.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Fri, 31 May 2024 09:45:45 +0000 Received: from EX19MTAUWA001.ant.amazon.com (10.250.64.204) by EX19D002EUA004.ant.amazon.com (10.252.50.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Fri, 31 May 2024 09:45:45 +0000 Received: from dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (10.253.65.58) by mail-relay.amazon.com (10.250.64.204) with Microsoft SMTP Server id 15.2.1258.28 via Frontend Transport; Fri, 31 May 2024 09:45:44 +0000 Received: by dev-dsk-hagarhem-1b-b868d8d5.eu-west-1.amazon.com (Postfix, from userid 23002382) id 489B820BED; Fri, 31 May 2024 09:45:44 +0000 (UTC) Date: Fri, 31 May 2024 09:45:44 +0000 From: Hagar Hemdan To: Marc Zyngier CC: Maximilian Heyne , Norbert Manthey , Thomas Gleixner , Eric Auger , , , Subject: Re: [PATCH] irqchip/gic-v3-its: Fix potential race condition in its_vlpi_prop_update() Message-ID: <20240531094544.GA3406@amazon.com> References: <20240530105713.18552-1-hagarhem@amazon.com> <86v82vl34a.wl-maz@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <86v82vl34a.wl-maz@kernel.org> User-Agent: Mutt/1.5.21 (2010-09-15) On Thu, May 30, 2024 at 04:40:37PM +0100, Marc Zyngier wrote: > Hi Hagar, > > On Thu, 30 May 2024 11:57:13 +0100, > Hagar Hemdan wrote: > > > > Similar to commit 046b5054f566 ("irqchip/gic-v3-its: Lock VLPI map array > > before translating it"), its_vlpi_prop_update() calls lpi_write_config() > > which obtains the mapping information for a VLPI. > > This should always be done with vlpi_lock for this device held. Otherwise, > > its_vlpi_prop_update() could race with its_vlpi_unmap(). > > Thanks for reporting this. Note that this issue is not the same as the > one you refer to (what you have here is a total absence of locking, > while 046b5054f566 fixed a call to get_vlpi_map() outside of an > existing critical section). > > > > > This bug was discovered and resolved using Coverity Static Analysis > > Security Testing (SAST) by Synopsys, Inc. > > Should we get a scrolling banner for this kind of advertisements? ;-) We either have to include that disclaimer, or cannot send fixes related to that commercial tool :) > > > > > Fixes: 015ec0386ab6 ("irqchip/gic-v3-its: Add VLPI configuration handling") > > Signed-off-by: Hagar Hemdan > > --- > > drivers/irqchip/irq-gic-v3-its.c | 12 +++++++++--- > > 1 file changed, 9 insertions(+), 3 deletions(-) > > > > diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c > > index 40ebf1726393..ecaad1786345 100644 > > --- a/drivers/irqchip/irq-gic-v3-its.c > > +++ b/drivers/irqchip/irq-gic-v3-its.c > > @@ -1970,9 +1970,13 @@ static int its_vlpi_unmap(struct irq_data *d) > > static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) > > { > > struct its_device *its_dev = irq_data_get_irq_chip_data(d); > > + int ret = 0; > > > > - if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) > > - return -EINVAL; > > + raw_spin_lock(&its_dev->event_map.vlpi_lock); > > + if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { > > + ret = -EINVAL; > > + goto out; > > + } > > > > if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI) > > lpi_update_config(d, 0xff, info->config); > > @@ -1980,7 +1984,9 @@ static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) > > lpi_write_config(d, 0xff, info->config); > > its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED)); > > > > - return 0; > > +out: > > + raw_spin_unlock(&its_dev->event_map.vlpi_lock); > > + return ret; > > } > > > > static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) > > As it turns out, all the calls from its_irq_set_vcpu_affinity() > require the same lock to be held. So instead of peppering the locking > all over the place, we could (should?) hoist the locking into > its_irq_set_vcpu_affinity() and avoid future bugs. It also results in > a negative diffstat. > > Something like the hack below (compile-tested only), which I'm sure > the "Coverity Static Analysis Security Testing (SAST) by Synopsys, > Inc" will be able to verify... > > M. > > diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c > index 40ebf1726393..abc1fb360ce4 100644 > --- a/drivers/irqchip/irq-gic-v3-its.c > +++ b/drivers/irqchip/irq-gic-v3-its.c > @@ -1851,23 +1851,18 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) > if (!info->map) > return -EINVAL; > > - raw_spin_lock(&its_dev->event_map.vlpi_lock); > - > if (!its_dev->event_map.vm) { > struct its_vlpi_map *maps; > > maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps), > GFP_ATOMIC); > - if (!maps) { > - ret = -ENOMEM; > - goto out; > - } > + if (!maps) > + return -ENOMEM; > > its_dev->event_map.vm = info->map->vm; > its_dev->event_map.vlpi_maps = maps; > } else if (its_dev->event_map.vm != info->map->vm) { > - ret = -EINVAL; > - goto out; > + return -EINVAL; > } > > /* Get our private copy of the mapping information */ > @@ -1899,8 +1894,6 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) > its_dev->event_map.nr_vlpis++; > } > > -out: > - raw_spin_unlock(&its_dev->event_map.vlpi_lock); > return ret; > } > > @@ -1910,20 +1903,14 @@ static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) > struct its_vlpi_map *map; > int ret = 0; > > - raw_spin_lock(&its_dev->event_map.vlpi_lock); > - > map = get_vlpi_map(d); > > - if (!its_dev->event_map.vm || !map) { > - ret = -EINVAL; > - goto out; > - } > + if (!its_dev->event_map.vm || !map) > + return -EINVAL; > > /* Copy our mapping information to the incoming request */ > *info->map = *map; > > -out: > - raw_spin_unlock(&its_dev->event_map.vlpi_lock); > return ret; > } > > @@ -1933,12 +1920,8 @@ static int its_vlpi_unmap(struct irq_data *d) > u32 event = its_get_event_id(d); > int ret = 0; > > - raw_spin_lock(&its_dev->event_map.vlpi_lock); > - > - if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { > - ret = -EINVAL; > - goto out; > - } > + if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) > + return -EINVAL; > > /* Drop the virtual mapping */ > its_send_discard(its_dev, event); > @@ -1962,8 +1945,6 @@ static int its_vlpi_unmap(struct irq_data *d) > kfree(its_dev->event_map.vlpi_maps); > } > > -out: > - raw_spin_unlock(&its_dev->event_map.vlpi_lock); > return ret; > } > > @@ -1987,29 +1968,41 @@ static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) > { > struct its_device *its_dev = irq_data_get_irq_chip_data(d); > struct its_cmd_info *info = vcpu_info; > + int ret; > > /* Need a v4 ITS */ > if (!is_v4(its_dev->its)) > return -EINVAL; > > + raw_spin_lock(&its_dev->event_map.vlpi_lock); > + > /* Unmap request? */ > - if (!info) > - return its_vlpi_unmap(d); > + if (!info) { > + ret = its_vlpi_unmap(d); > + goto out; > + } > > switch (info->cmd_type) { > case MAP_VLPI: > - return its_vlpi_map(d, info); > + ret = its_vlpi_map(d, info); > + break; > > case GET_VLPI: > - return its_vlpi_get(d, info); > + ret = its_vlpi_get(d, info); > + break; > > case PROP_UPDATE_VLPI: > case PROP_UPDATE_AND_INV_VLPI: > - return its_vlpi_prop_update(d, info); > + ret = its_vlpi_prop_update(d, info); > + break; > > default: > - return -EINVAL; > + ret = -EINVAL; > } > + > +out: > + raw_spin_unlock(&its_dev->event_map.vlpi_lock); > + return ret; > } > > static struct irq_chip its_irq_chip = { > > -- > Without deviation from the norm, progress is not possible.