All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: stable@vger.kernel.org
Subject: [PATCH v2 13/13] xen/events: block rogue events for some time
Date: Tue,  3 Nov 2020 15:35:28 +0100	[thread overview]
Message-ID: <20201103143528.22780-14-jgross@suse.com> (raw)
In-Reply-To: <20201103143528.22780-1-jgross@suse.com>

In order to avoid high dom0 load due to rogue guests sending events at
high frequency, block those events in case there was no action needed
in dom0 to handle the events.

This is done by adding a per-event counter, which set to zero in case
an EOI without the XEN_EOI_FLAG_SPURIOUS is received from a backend
driver, and incremented when this flag has been set. In case the
counter is 2 or higher delay the EOI by 1 << (cnt - 2) jiffies, but
not more than 1 second.

In order not to waste memory shorten the per-event refcnt to two bytes
(it should normally never exceed a value of 2). Add an overflow check
to evtchn_get() to make sure the 2 bytes really won't overflow.

This is part of XSA-332.

This is upstream commit 5f7f77400ab5b357b5fdb7122c3442239672186c

Cc: stable@vger.kernel.org
Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>
Reviewed-by: Wei Liu <wl@xen.org>
---
 drivers/xen/events/events_base.c     | 27 ++++++++++++++++++++++-----
 drivers/xen/events/events_internal.h |  3 ++-
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/drivers/xen/events/events_base.c b/drivers/xen/events/events_base.c
index a204e1d50255..5308bc1e0189 100644
--- a/drivers/xen/events/events_base.c
+++ b/drivers/xen/events/events_base.c
@@ -468,17 +468,34 @@ static void lateeoi_list_add(struct irq_info *info)
 	spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
 }
 
-static void xen_irq_lateeoi_locked(struct irq_info *info)
+static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
 {
 	evtchn_port_t evtchn;
 	unsigned int cpu;
+	unsigned int delay = 0;
 
 	evtchn = info->evtchn;
 	if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
 		return;
 
+	if (spurious) {
+		if ((1 << info->spurious_cnt) < (HZ << 2))
+			info->spurious_cnt++;
+		if (info->spurious_cnt > 1) {
+			delay = 1 << (info->spurious_cnt - 2);
+			if (delay > HZ)
+				delay = HZ;
+			if (!info->eoi_time)
+				info->eoi_cpu = smp_processor_id();
+			info->eoi_time = get_jiffies_64() + delay;
+		}
+	} else {
+		info->spurious_cnt = 0;
+	}
+
 	cpu = info->eoi_cpu;
-	if (info->eoi_time && info->irq_epoch == per_cpu(irq_epoch, cpu)) {
+	if (info->eoi_time &&
+	    (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
 		lateeoi_list_add(info);
 		return;
 	}
@@ -515,7 +532,7 @@ static void xen_irq_lateeoi_worker(struct work_struct *work)
 
 		info->eoi_time = 0;
 
-		xen_irq_lateeoi_locked(info);
+		xen_irq_lateeoi_locked(info, false);
 	}
 
 	if (info)
@@ -544,7 +561,7 @@ void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
 	info = info_for_irq(irq);
 
 	if (info)
-		xen_irq_lateeoi_locked(info);
+		xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
 
 	read_unlock_irqrestore(&evtchn_rwlock, flags);
 }
@@ -1447,7 +1464,7 @@ int evtchn_get(unsigned int evtchn)
 		goto done;
 
 	err = -EINVAL;
-	if (info->refcnt <= 0)
+	if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
 		goto done;
 
 	info->refcnt++;
diff --git a/drivers/xen/events/events_internal.h b/drivers/xen/events/events_internal.h
index 2cb9c2d2c5c0..b9b4f5919893 100644
--- a/drivers/xen/events/events_internal.h
+++ b/drivers/xen/events/events_internal.h
@@ -33,7 +33,8 @@ enum xen_irq_type {
 struct irq_info {
 	struct list_head list;
 	struct list_head eoi_list;
-	int refcnt;
+	short refcnt;
+	short spurious_cnt;
 	enum xen_irq_type type;	/* type */
 	unsigned irq;
 	unsigned int evtchn;	/* event channel */
-- 
2.26.2


  parent reply	other threads:[~2020-11-03 14:35 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-03 14:35 [PATCH v2 00/13] Backport of patch series for stable 4.9 branch Juergen Gross
2020-11-03 14:35 ` [PATCH v2 01/13] xen/events: don't use chip_data for legacy IRQs Juergen Gross
2020-11-03 14:35 ` [PATCH v2 02/13] xen/events: avoid removing an event channel while handling it Juergen Gross
2020-11-03 14:35 ` [PATCH v2 03/13] xen/events: add a proper barrier to 2-level uevent unmasking Juergen Gross
2020-11-03 14:35 ` [PATCH v2 04/13] xen/events: fix race in evtchn_fifo_unmask() Juergen Gross
2020-11-03 14:35 ` [PATCH v2 05/13] xen/events: add a new "late EOI" evtchn framework Juergen Gross
2020-11-03 14:35 ` [PATCH v2 06/13] xen/blkback: use lateeoi irq binding Juergen Gross
2020-11-03 14:35 ` [PATCH v2 07/13] xen/netback: " Juergen Gross
2020-11-03 14:35 ` [PATCH v2 08/13] xen/scsiback: " Juergen Gross
2020-11-03 14:35 ` [PATCH v2 09/13] xen/pciback: " Juergen Gross
2020-11-03 14:35 ` [PATCH v2 10/13] xen/events: switch user event channels to lateeoi model Juergen Gross
2020-11-03 14:35 ` [PATCH v2 11/13] xen/events: use a common cpu hotplug hook for event channels Juergen Gross
2020-11-03 14:35 ` [PATCH v2 12/13] xen/events: defer eoi in case of excessive number of events Juergen Gross
2020-11-03 14:35 ` Juergen Gross [this message]
2020-11-17 11:35 ` [PATCH v2 00/13] Backport of patch series for stable 4.9 branch Greg KH
  -- strict thread matches above, loose matches on Subject: below --
2020-11-03 14:13 [PATCH v2 00/13] Backport of patch series for stable 5.4 branch Juergen Gross
2020-11-03 14:13 ` [PATCH v2 13/13] xen/events: block rogue events for some time Juergen Gross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201103143528.22780-14-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.