Magellan Linux

Annotation of /trunk/kernel-alx-legacy/patches-4.9/0343-4.9.244-all-fixes.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3645 - (hide annotations) (download)
Mon Oct 24 14:07:25 2022 UTC (19 months, 1 week ago) by niro
File size: 130164 byte(s)
-linux-4.9.244
1 niro 3645 diff --git a/Documentation/kernel-parameters.txt b/Documentation/kernel-parameters.txt
2     index 40602517ca528..e51e42d9f6460 100644
3     --- a/Documentation/kernel-parameters.txt
4     +++ b/Documentation/kernel-parameters.txt
5     @@ -5020,6 +5020,14 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
6     Disables the PV optimizations forcing the HVM guest to
7     run as generic HVM guest with no PV drivers.
8    
9     + xen.event_eoi_delay= [XEN]
10     + How long to delay EOI handling in case of event
11     + storms (jiffies). Default is 10.
12     +
13     + xen.event_loop_timeout= [XEN]
14     + After which time (jiffies) the event handling loop
15     + should start to delay EOI handling. Default is 2.
16     +
17     xirc2ps_cs= [NET,PCMCIA]
18     Format:
19     <irq>,<irq_mask>,<io>,<full_duplex>,<do_sound>,<lockup_hack>[,<irq2>[,<irq3>[,<irq4>]]]
20     diff --git a/Makefile b/Makefile
21     index c6fcfe4bfeed5..27314b9f0fe67 100644
22     --- a/Makefile
23     +++ b/Makefile
24     @@ -1,6 +1,6 @@
25     VERSION = 4
26     PATCHLEVEL = 9
27     -SUBLEVEL = 243
28     +SUBLEVEL = 244
29     EXTRAVERSION =
30     NAME = Roaring Lionus
31    
32     diff --git a/arch/x86/events/intel/pt.c b/arch/x86/events/intel/pt.c
33     index df60b58691e7c..1808c57ce1614 100644
34     --- a/arch/x86/events/intel/pt.c
35     +++ b/arch/x86/events/intel/pt.c
36     @@ -1117,7 +1117,7 @@ static int pt_event_addr_filters_validate(struct list_head *filters)
37     if (!filter->range || !filter->size)
38     return -EOPNOTSUPP;
39    
40     - if (!filter->inode) {
41     + if (!filter->path.dentry) {
42     if (!valid_kernel_ip(filter->offset))
43     return -EINVAL;
44    
45     @@ -1144,7 +1144,7 @@ static void pt_event_addr_filters_sync(struct perf_event *event)
46     return;
47    
48     list_for_each_entry(filter, &head->list, entry) {
49     - if (filter->inode && !offs[range]) {
50     + if (filter->path.dentry && !offs[range]) {
51     msr_a = msr_b = 0;
52     } else {
53     /* apply the offset */
54     diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
55     index 85c1cc0305f39..f8a7aba4b0959 100644
56     --- a/arch/x86/kernel/cpu/bugs.c
57     +++ b/arch/x86/kernel/cpu/bugs.c
58     @@ -1248,6 +1248,14 @@ static int ssb_prctl_set(struct task_struct *task, unsigned long ctrl)
59     return 0;
60     }
61    
62     +static bool is_spec_ib_user_controlled(void)
63     +{
64     + return spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
65     + spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
66     + spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
67     + spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP;
68     +}
69     +
70     static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
71     {
72     switch (ctrl) {
73     @@ -1255,17 +1263,26 @@ static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
74     if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
75     spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
76     return 0;
77     - /*
78     - * Indirect branch speculation is always disabled in strict
79     - * mode. It can neither be enabled if it was force-disabled
80     - * by a previous prctl call.
81    
82     + /*
83     + * With strict mode for both IBPB and STIBP, the instruction
84     + * code paths avoid checking this task flag and instead,
85     + * unconditionally run the instruction. However, STIBP and IBPB
86     + * are independent and either can be set to conditionally
87     + * enabled regardless of the mode of the other.
88     + *
89     + * If either is set to conditional, allow the task flag to be
90     + * updated, unless it was force-disabled by a previous prctl
91     + * call. Currently, this is possible on an AMD CPU which has the
92     + * feature X86_FEATURE_AMD_STIBP_ALWAYS_ON. In this case, if the
93     + * kernel is booted with 'spectre_v2_user=seccomp', then
94     + * spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP and
95     + * spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED.
96     */
97     - if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
98     - spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
99     - spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED ||
100     + if (!is_spec_ib_user_controlled() ||
101     task_spec_ib_force_disable(task))
102     return -EPERM;
103     +
104     task_clear_spec_ib_disable(task);
105     task_update_spec_tif(task);
106     break;
107     @@ -1278,10 +1295,10 @@ static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
108     if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
109     spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
110     return -EPERM;
111     - if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
112     - spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
113     - spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
114     +
115     + if (!is_spec_ib_user_controlled())
116     return 0;
117     +
118     task_set_spec_ib_disable(task);
119     if (ctrl == PR_SPEC_FORCE_DISABLE)
120     task_set_spec_ib_force_disable(task);
121     @@ -1344,20 +1361,17 @@ static int ib_prctl_get(struct task_struct *task)
122     if (spectre_v2_user_ibpb == SPECTRE_V2_USER_NONE &&
123     spectre_v2_user_stibp == SPECTRE_V2_USER_NONE)
124     return PR_SPEC_ENABLE;
125     - else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
126     - spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
127     - spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
128     - return PR_SPEC_DISABLE;
129     - else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_PRCTL ||
130     - spectre_v2_user_ibpb == SPECTRE_V2_USER_SECCOMP ||
131     - spectre_v2_user_stibp == SPECTRE_V2_USER_PRCTL ||
132     - spectre_v2_user_stibp == SPECTRE_V2_USER_SECCOMP) {
133     + else if (is_spec_ib_user_controlled()) {
134     if (task_spec_ib_force_disable(task))
135     return PR_SPEC_PRCTL | PR_SPEC_FORCE_DISABLE;
136     if (task_spec_ib_disable(task))
137     return PR_SPEC_PRCTL | PR_SPEC_DISABLE;
138     return PR_SPEC_PRCTL | PR_SPEC_ENABLE;
139     - } else
140     + } else if (spectre_v2_user_ibpb == SPECTRE_V2_USER_STRICT ||
141     + spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT ||
142     + spectre_v2_user_stibp == SPECTRE_V2_USER_STRICT_PREFERRED)
143     + return PR_SPEC_DISABLE;
144     + else
145     return PR_SPEC_NOT_AFFECTED;
146     }
147    
148     diff --git a/drivers/block/xen-blkback/blkback.c b/drivers/block/xen-blkback/blkback.c
149     index a700e525535ce..4f643a87f9c7d 100644
150     --- a/drivers/block/xen-blkback/blkback.c
151     +++ b/drivers/block/xen-blkback/blkback.c
152     @@ -183,7 +183,7 @@ static inline void shrink_free_pagepool(struct xen_blkif_ring *ring, int num)
153    
154     #define vaddr(page) ((unsigned long)pfn_to_kaddr(page_to_pfn(page)))
155    
156     -static int do_block_io_op(struct xen_blkif_ring *ring);
157     +static int do_block_io_op(struct xen_blkif_ring *ring, unsigned int *eoi_flags);
158     static int dispatch_rw_block_io(struct xen_blkif_ring *ring,
159     struct blkif_request *req,
160     struct pending_req *pending_req);
161     @@ -608,6 +608,8 @@ int xen_blkif_schedule(void *arg)
162     struct xen_vbd *vbd = &blkif->vbd;
163     unsigned long timeout;
164     int ret;
165     + bool do_eoi;
166     + unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
167    
168     set_freezable();
169     while (!kthread_should_stop()) {
170     @@ -632,16 +634,23 @@ int xen_blkif_schedule(void *arg)
171     if (timeout == 0)
172     goto purge_gnt_list;
173    
174     + do_eoi = ring->waiting_reqs;
175     +
176     ring->waiting_reqs = 0;
177     smp_mb(); /* clear flag *before* checking for work */
178    
179     - ret = do_block_io_op(ring);
180     + ret = do_block_io_op(ring, &eoi_flags);
181     if (ret > 0)
182     ring->waiting_reqs = 1;
183     if (ret == -EACCES)
184     wait_event_interruptible(ring->shutdown_wq,
185     kthread_should_stop());
186    
187     + if (do_eoi && !ring->waiting_reqs) {
188     + xen_irq_lateeoi(ring->irq, eoi_flags);
189     + eoi_flags |= XEN_EOI_FLAG_SPURIOUS;
190     + }
191     +
192     purge_gnt_list:
193     if (blkif->vbd.feature_gnt_persistent &&
194     time_after(jiffies, ring->next_lru)) {
195     @@ -1117,7 +1126,7 @@ static void end_block_io_op(struct bio *bio)
196     * and transmute it to the block API to hand it over to the proper block disk.
197     */
198     static int
199     -__do_block_io_op(struct xen_blkif_ring *ring)
200     +__do_block_io_op(struct xen_blkif_ring *ring, unsigned int *eoi_flags)
201     {
202     union blkif_back_rings *blk_rings = &ring->blk_rings;
203     struct blkif_request req;
204     @@ -1140,6 +1149,9 @@ __do_block_io_op(struct xen_blkif_ring *ring)
205     if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
206     break;
207    
208     + /* We've seen a request, so clear spurious eoi flag. */
209     + *eoi_flags &= ~XEN_EOI_FLAG_SPURIOUS;
210     +
211     if (kthread_should_stop()) {
212     more_to_do = 1;
213     break;
214     @@ -1198,13 +1210,13 @@ done:
215     }
216    
217     static int
218     -do_block_io_op(struct xen_blkif_ring *ring)
219     +do_block_io_op(struct xen_blkif_ring *ring, unsigned int *eoi_flags)
220     {
221     union blkif_back_rings *blk_rings = &ring->blk_rings;
222     int more_to_do;
223    
224     do {
225     - more_to_do = __do_block_io_op(ring);
226     + more_to_do = __do_block_io_op(ring, eoi_flags);
227     if (more_to_do)
228     break;
229    
230     diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c
231     index 1d1f866579679..702ebfc4face5 100644
232     --- a/drivers/block/xen-blkback/xenbus.c
233     +++ b/drivers/block/xen-blkback/xenbus.c
234     @@ -236,9 +236,8 @@ static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
235     BUG();
236     }
237    
238     - err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
239     - xen_blkif_be_int, 0,
240     - "blkif-backend", ring);
241     + err = bind_interdomain_evtchn_to_irqhandler_lateeoi(blkif->domid,
242     + evtchn, xen_blkif_be_int, 0, "blkif-backend", ring);
243     if (err < 0) {
244     xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
245     ring->blk_rings.common.sring = NULL;
246     diff --git a/drivers/char/random.c b/drivers/char/random.c
247     index c417aa19f9962..4cbc73173701d 100644
248     --- a/drivers/char/random.c
249     +++ b/drivers/char/random.c
250     @@ -1211,7 +1211,6 @@ void add_interrupt_randomness(int irq, int irq_flags)
251    
252     fast_mix(fast_pool);
253     add_interrupt_bench(cycles);
254     - this_cpu_add(net_rand_state.s1, fast_pool->pool[cycles & 3]);
255    
256     if (unlikely(crng_init == 0)) {
257     if ((fast_pool->count >= 64) &&
258     diff --git a/drivers/gpu/drm/amd/amdgpu/cik_sdma.c b/drivers/gpu/drm/amd/amdgpu/cik_sdma.c
259     index cb952acc71339..2934443fbd4dc 100644
260     --- a/drivers/gpu/drm/amd/amdgpu/cik_sdma.c
261     +++ b/drivers/gpu/drm/amd/amdgpu/cik_sdma.c
262     @@ -1053,22 +1053,19 @@ static int cik_sdma_soft_reset(void *handle)
263     {
264     u32 srbm_soft_reset = 0;
265     struct amdgpu_device *adev = (struct amdgpu_device *)handle;
266     - u32 tmp = RREG32(mmSRBM_STATUS2);
267     + u32 tmp;
268    
269     - if (tmp & SRBM_STATUS2__SDMA_BUSY_MASK) {
270     - /* sdma0 */
271     - tmp = RREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET);
272     - tmp |= SDMA0_F32_CNTL__HALT_MASK;
273     - WREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET, tmp);
274     - srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
275     - }
276     - if (tmp & SRBM_STATUS2__SDMA1_BUSY_MASK) {
277     - /* sdma1 */
278     - tmp = RREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET);
279     - tmp |= SDMA0_F32_CNTL__HALT_MASK;
280     - WREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET, tmp);
281     - srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
282     - }
283     + /* sdma0 */
284     + tmp = RREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET);
285     + tmp |= SDMA0_F32_CNTL__HALT_MASK;
286     + WREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET, tmp);
287     + srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
288     +
289     + /* sdma1 */
290     + tmp = RREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET);
291     + tmp |= SDMA0_F32_CNTL__HALT_MASK;
292     + WREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET, tmp);
293     + srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
294    
295     if (srbm_soft_reset) {
296     tmp = RREG32(mmSRBM_SOFT_RESET);
297     diff --git a/drivers/gpu/drm/gma500/psb_irq.c b/drivers/gpu/drm/gma500/psb_irq.c
298     index 78eb109028091..076b6da44f461 100644
299     --- a/drivers/gpu/drm/gma500/psb_irq.c
300     +++ b/drivers/gpu/drm/gma500/psb_irq.c
301     @@ -350,6 +350,7 @@ int psb_irq_postinstall(struct drm_device *dev)
302     {
303     struct drm_psb_private *dev_priv = dev->dev_private;
304     unsigned long irqflags;
305     + unsigned int i;
306    
307     spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
308    
309     @@ -362,20 +363,12 @@ int psb_irq_postinstall(struct drm_device *dev)
310     PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
311     PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
312    
313     - if (dev->vblank[0].enabled)
314     - psb_enable_pipestat(dev_priv, 0, PIPE_VBLANK_INTERRUPT_ENABLE);
315     - else
316     - psb_disable_pipestat(dev_priv, 0, PIPE_VBLANK_INTERRUPT_ENABLE);
317     -
318     - if (dev->vblank[1].enabled)
319     - psb_enable_pipestat(dev_priv, 1, PIPE_VBLANK_INTERRUPT_ENABLE);
320     - else
321     - psb_disable_pipestat(dev_priv, 1, PIPE_VBLANK_INTERRUPT_ENABLE);
322     -
323     - if (dev->vblank[2].enabled)
324     - psb_enable_pipestat(dev_priv, 2, PIPE_VBLANK_INTERRUPT_ENABLE);
325     - else
326     - psb_disable_pipestat(dev_priv, 2, PIPE_VBLANK_INTERRUPT_ENABLE);
327     + for (i = 0; i < dev->num_crtcs; ++i) {
328     + if (dev->vblank[i].enabled)
329     + psb_enable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
330     + else
331     + psb_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
332     + }
333    
334     if (dev_priv->ops->hotplug_enable)
335     dev_priv->ops->hotplug_enable(dev, true);
336     @@ -388,6 +381,7 @@ void psb_irq_uninstall(struct drm_device *dev)
337     {
338     struct drm_psb_private *dev_priv = dev->dev_private;
339     unsigned long irqflags;
340     + unsigned int i;
341    
342     spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
343    
344     @@ -396,14 +390,10 @@ void psb_irq_uninstall(struct drm_device *dev)
345    
346     PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
347    
348     - if (dev->vblank[0].enabled)
349     - psb_disable_pipestat(dev_priv, 0, PIPE_VBLANK_INTERRUPT_ENABLE);
350     -
351     - if (dev->vblank[1].enabled)
352     - psb_disable_pipestat(dev_priv, 1, PIPE_VBLANK_INTERRUPT_ENABLE);
353     -
354     - if (dev->vblank[2].enabled)
355     - psb_disable_pipestat(dev_priv, 2, PIPE_VBLANK_INTERRUPT_ENABLE);
356     + for (i = 0; i < dev->num_crtcs; ++i) {
357     + if (dev->vblank[i].enabled)
358     + psb_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
359     + }
360    
361     dev_priv->vdc_irq_mask &= _PSB_IRQ_SGX_FLAG |
362     _PSB_IRQ_MSVDX_FLAG |
363     diff --git a/drivers/iommu/amd_iommu_types.h b/drivers/iommu/amd_iommu_types.h
364     index da3fbf82d1cf4..e19c05d9e84ba 100644
365     --- a/drivers/iommu/amd_iommu_types.h
366     +++ b/drivers/iommu/amd_iommu_types.h
367     @@ -383,7 +383,11 @@ extern bool amd_iommu_np_cache;
368     /* Only true if all IOMMUs support device IOTLBs */
369     extern bool amd_iommu_iotlb_sup;
370    
371     -#define MAX_IRQS_PER_TABLE 256
372     +/*
373     + * AMD IOMMU hardware only support 512 IRTEs despite
374     + * the architectural limitation of 2048 entries.
375     + */
376     +#define MAX_IRQS_PER_TABLE 512
377     #define IRQ_TABLE_ALIGNMENT 128
378    
379     struct irq_remap_table {
380     diff --git a/drivers/misc/mei/client.h b/drivers/misc/mei/client.h
381     index d2bfabecd882c..f9d3211ce5994 100644
382     --- a/drivers/misc/mei/client.h
383     +++ b/drivers/misc/mei/client.h
384     @@ -152,11 +152,11 @@ static inline u8 mei_cl_me_id(const struct mei_cl *cl)
385     *
386     * @cl: host client
387     *
388     - * Return: mtu
389     + * Return: mtu or 0 if client is not connected
390     */
391     static inline size_t mei_cl_mtu(const struct mei_cl *cl)
392     {
393     - return cl->me_cl->props.max_msg_length;
394     + return cl->me_cl ? cl->me_cl->props.max_msg_length : 0;
395     }
396    
397     /**
398     diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c
399     index ffc5467a1ec2b..617eb75c7c0ce 100644
400     --- a/drivers/net/can/dev.c
401     +++ b/drivers/net/can/dev.c
402     @@ -469,9 +469,13 @@ struct sk_buff *__can_get_echo_skb(struct net_device *dev, unsigned int idx, u8
403     */
404     struct sk_buff *skb = priv->echo_skb[idx];
405     struct canfd_frame *cf = (struct canfd_frame *)skb->data;
406     - u8 len = cf->len;
407    
408     - *len_ptr = len;
409     + /* get the real payload length for netdev statistics */
410     + if (cf->can_id & CAN_RTR_FLAG)
411     + *len_ptr = 0;
412     + else
413     + *len_ptr = cf->len;
414     +
415     priv->echo_skb[idx] = NULL;
416    
417     return skb;
418     @@ -496,7 +500,11 @@ unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx)
419     if (!skb)
420     return 0;
421    
422     - netif_rx(skb);
423     + skb_get(skb);
424     + if (netif_rx(skb) == NET_RX_SUCCESS)
425     + dev_consume_skb_any(skb);
426     + else
427     + dev_kfree_skb_any(skb);
428    
429     return len;
430     }
431     diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
432     index 6cd4317fe94df..74b37309efab7 100644
433     --- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
434     +++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
435     @@ -152,14 +152,55 @@ void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
436     /* protect from getting timeval before setting now */
437     if (time_ref->tv_host.tv_sec > 0) {
438     u64 delta_us;
439     + s64 delta_ts = 0;
440     +
441     + /* General case: dev_ts_1 < dev_ts_2 < ts, with:
442     + *
443     + * - dev_ts_1 = previous sync timestamp
444     + * - dev_ts_2 = last sync timestamp
445     + * - ts = event timestamp
446     + * - ts_period = known sync period (theoretical)
447     + * ~ dev_ts2 - dev_ts1
448     + * *but*:
449     + *
450     + * - time counters wrap (see adapter->ts_used_bits)
451     + * - sometimes, dev_ts_1 < ts < dev_ts2
452     + *
453     + * "normal" case (sync time counters increase):
454     + * must take into account case when ts wraps (tsw)
455     + *
456     + * < ts_period > < >
457     + * | | |
458     + * ---+--------+----+-------0-+--+-->
459     + * ts_dev_1 | ts_dev_2 |
460     + * ts tsw
461     + */
462     + if (time_ref->ts_dev_1 < time_ref->ts_dev_2) {
463     + /* case when event time (tsw) wraps */
464     + if (ts < time_ref->ts_dev_1)
465     + delta_ts = 1 << time_ref->adapter->ts_used_bits;
466     +
467     + /* Otherwise, sync time counter (ts_dev_2) has wrapped:
468     + * handle case when event time (tsn) hasn't.
469     + *
470     + * < ts_period > < >
471     + * | | |
472     + * ---+--------+--0-+---------+--+-->
473     + * ts_dev_1 | ts_dev_2 |
474     + * tsn ts
475     + */
476     + } else if (time_ref->ts_dev_1 < ts) {
477     + delta_ts = -(1 << time_ref->adapter->ts_used_bits);
478     + }
479    
480     - delta_us = ts - time_ref->ts_dev_2;
481     - if (ts < time_ref->ts_dev_2)
482     - delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1;
483     + /* add delay between last sync and event timestamps */
484     + delta_ts += (signed int)(ts - time_ref->ts_dev_2);
485    
486     - delta_us += time_ref->ts_total;
487     + /* add time from beginning to last sync */
488     + delta_ts += time_ref->ts_total;
489    
490     - delta_us *= time_ref->adapter->us_per_ts_scale;
491     + /* convert ticks number into microseconds */
492     + delta_us = delta_ts * time_ref->adapter->us_per_ts_scale;
493     delta_us >>= time_ref->adapter->us_per_ts_shift;
494    
495     *tv = time_ref->tv_host_0;
496     diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
497     index 40647b837b31f..d314e73f3d061 100644
498     --- a/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
499     +++ b/drivers/net/can/usb/peak_usb/pcan_usb_fd.c
500     @@ -475,12 +475,18 @@ static int pcan_usb_fd_decode_canmsg(struct pcan_usb_fd_if *usb_if,
501     struct pucan_msg *rx_msg)
502     {
503     struct pucan_rx_msg *rm = (struct pucan_rx_msg *)rx_msg;
504     - struct peak_usb_device *dev = usb_if->dev[pucan_msg_get_channel(rm)];
505     - struct net_device *netdev = dev->netdev;
506     + struct peak_usb_device *dev;
507     + struct net_device *netdev;
508     struct canfd_frame *cfd;
509     struct sk_buff *skb;
510     const u16 rx_msg_flags = le16_to_cpu(rm->flags);
511    
512     + if (pucan_msg_get_channel(rm) >= ARRAY_SIZE(usb_if->dev))
513     + return -ENOMEM;
514     +
515     + dev = usb_if->dev[pucan_msg_get_channel(rm)];
516     + netdev = dev->netdev;
517     +
518     if (rx_msg_flags & PUCAN_MSG_EXT_DATA_LEN) {
519     /* CANFD frame case */
520     skb = alloc_canfd_skb(netdev, &cfd);
521     @@ -527,15 +533,21 @@ static int pcan_usb_fd_decode_status(struct pcan_usb_fd_if *usb_if,
522     struct pucan_msg *rx_msg)
523     {
524     struct pucan_status_msg *sm = (struct pucan_status_msg *)rx_msg;
525     - struct peak_usb_device *dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
526     - struct pcan_usb_fd_device *pdev =
527     - container_of(dev, struct pcan_usb_fd_device, dev);
528     + struct pcan_usb_fd_device *pdev;
529     enum can_state new_state = CAN_STATE_ERROR_ACTIVE;
530     enum can_state rx_state, tx_state;
531     - struct net_device *netdev = dev->netdev;
532     + struct peak_usb_device *dev;
533     + struct net_device *netdev;
534     struct can_frame *cf;
535     struct sk_buff *skb;
536    
537     + if (pucan_stmsg_get_channel(sm) >= ARRAY_SIZE(usb_if->dev))
538     + return -ENOMEM;
539     +
540     + dev = usb_if->dev[pucan_stmsg_get_channel(sm)];
541     + pdev = container_of(dev, struct pcan_usb_fd_device, dev);
542     + netdev = dev->netdev;
543     +
544     /* nothing should be sent while in BUS_OFF state */
545     if (dev->can.state == CAN_STATE_BUS_OFF)
546     return 0;
547     @@ -588,9 +600,14 @@ static int pcan_usb_fd_decode_error(struct pcan_usb_fd_if *usb_if,
548     struct pucan_msg *rx_msg)
549     {
550     struct pucan_error_msg *er = (struct pucan_error_msg *)rx_msg;
551     - struct peak_usb_device *dev = usb_if->dev[pucan_ermsg_get_channel(er)];
552     - struct pcan_usb_fd_device *pdev =
553     - container_of(dev, struct pcan_usb_fd_device, dev);
554     + struct pcan_usb_fd_device *pdev;
555     + struct peak_usb_device *dev;
556     +
557     + if (pucan_ermsg_get_channel(er) >= ARRAY_SIZE(usb_if->dev))
558     + return -EINVAL;
559     +
560     + dev = usb_if->dev[pucan_ermsg_get_channel(er)];
561     + pdev = container_of(dev, struct pcan_usb_fd_device, dev);
562    
563     /* keep a trace of tx and rx error counters for later use */
564     pdev->bec.txerr = er->tx_err_cnt;
565     @@ -604,11 +621,17 @@ static int pcan_usb_fd_decode_overrun(struct pcan_usb_fd_if *usb_if,
566     struct pucan_msg *rx_msg)
567     {
568     struct pcan_ufd_ovr_msg *ov = (struct pcan_ufd_ovr_msg *)rx_msg;
569     - struct peak_usb_device *dev = usb_if->dev[pufd_omsg_get_channel(ov)];
570     - struct net_device *netdev = dev->netdev;
571     + struct peak_usb_device *dev;
572     + struct net_device *netdev;
573     struct can_frame *cf;
574     struct sk_buff *skb;
575    
576     + if (pufd_omsg_get_channel(ov) >= ARRAY_SIZE(usb_if->dev))
577     + return -EINVAL;
578     +
579     + dev = usb_if->dev[pufd_omsg_get_channel(ov)];
580     + netdev = dev->netdev;
581     +
582     /* allocate an skb to store the error frame */
583     skb = alloc_can_err_skb(netdev, &cf);
584     if (!skb)
585     @@ -726,6 +749,9 @@ static int pcan_usb_fd_encode_msg(struct peak_usb_device *dev,
586     u16 tx_msg_size, tx_msg_flags;
587     u8 can_dlc;
588    
589     + if (cfd->len > CANFD_MAX_DLEN)
590     + return -EINVAL;
591     +
592     tx_msg_size = ALIGN(sizeof(struct pucan_tx_msg) + cfd->len, 4);
593     tx_msg->size = cpu_to_le16(tx_msg_size);
594     tx_msg->type = cpu_to_le16(PUCAN_MSG_CAN_TX);
595     diff --git a/drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c b/drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c
596     index 7484ad3c955db..e3e02ec8f1498 100644
597     --- a/drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c
598     +++ b/drivers/net/ethernet/intel/i40e/i40e_virtchnl_pf.c
599     @@ -188,7 +188,7 @@ static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
600     * check for the valid queue id
601     **/
602     static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
603     - u8 qid)
604     + u16 qid)
605     {
606     struct i40e_pf *pf = vf->pf;
607     struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
608     @@ -203,7 +203,7 @@ static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
609     *
610     * check for the valid vector id
611     **/
612     -static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
613     +static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u32 vector_id)
614     {
615     struct i40e_pf *pf = vf->pf;
616    
617     @@ -417,11 +417,28 @@ static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
618     u32 v_idx, i, reg_idx, reg;
619     u32 next_q_idx, next_q_type;
620     u32 msix_vf, size;
621     + int ret = 0;
622     +
623     + msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
624     +
625     + if (qvlist_info->num_vectors > msix_vf) {
626     + dev_warn(&pf->pdev->dev,
627     + "Incorrect number of iwarp vectors %u. Maximum %u allowed.\n",
628     + qvlist_info->num_vectors,
629     + msix_vf);
630     + ret = -EINVAL;
631     + goto err_out;
632     + }
633    
634     size = sizeof(struct i40e_virtchnl_iwarp_qvlist_info) +
635     (sizeof(struct i40e_virtchnl_iwarp_qv_info) *
636     (qvlist_info->num_vectors - 1));
637     + kfree(vf->qvlist_info);
638     vf->qvlist_info = kzalloc(size, GFP_KERNEL);
639     + if (!vf->qvlist_info) {
640     + ret = -ENOMEM;
641     + goto err_out;
642     + }
643     vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
644    
645     msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
646     @@ -432,8 +449,10 @@ static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
647     v_idx = qv_info->v_idx;
648    
649     /* Validate vector id belongs to this vf */
650     - if (!i40e_vc_isvalid_vector_id(vf, v_idx))
651     - goto err;
652     + if (!i40e_vc_isvalid_vector_id(vf, v_idx)) {
653     + ret = -EINVAL;
654     + goto err_free;
655     + }
656    
657     vf->qvlist_info->qv_info[i] = *qv_info;
658    
659     @@ -475,10 +494,11 @@ static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
660     }
661    
662     return 0;
663     -err:
664     +err_free:
665     kfree(vf->qvlist_info);
666     vf->qvlist_info = NULL;
667     - return -EINVAL;
668     +err_out:
669     + return ret;
670     }
671    
672     /**
673     diff --git a/drivers/net/geneve.c b/drivers/net/geneve.c
674     index 35d8c636de123..d89995f4bd433 100644
675     --- a/drivers/net/geneve.c
676     +++ b/drivers/net/geneve.c
677     @@ -732,7 +732,8 @@ free_dst:
678     static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
679     struct net_device *dev,
680     struct flowi4 *fl4,
681     - struct ip_tunnel_info *info)
682     + struct ip_tunnel_info *info,
683     + __be16 dport, __be16 sport)
684     {
685     bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
686     struct geneve_dev *geneve = netdev_priv(dev);
687     @@ -746,6 +747,8 @@ static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
688     memset(fl4, 0, sizeof(*fl4));
689     fl4->flowi4_mark = skb->mark;
690     fl4->flowi4_proto = IPPROTO_UDP;
691     + fl4->fl4_dport = dport;
692     + fl4->fl4_sport = sport;
693    
694     if (info) {
695     fl4->daddr = info->key.u.ipv4.dst;
696     @@ -791,7 +794,8 @@ static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
697     static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
698     struct net_device *dev,
699     struct flowi6 *fl6,
700     - struct ip_tunnel_info *info)
701     + struct ip_tunnel_info *info,
702     + __be16 dport, __be16 sport)
703     {
704     bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
705     struct geneve_dev *geneve = netdev_priv(dev);
706     @@ -807,6 +811,8 @@ static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
707     memset(fl6, 0, sizeof(*fl6));
708     fl6->flowi6_mark = skb->mark;
709     fl6->flowi6_proto = IPPROTO_UDP;
710     + fl6->fl6_dport = dport;
711     + fl6->fl6_sport = sport;
712    
713     if (info) {
714     fl6->daddr = info->key.u.ipv6.dst;
715     @@ -894,13 +900,14 @@ static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
716     goto tx_error;
717     }
718    
719     - rt = geneve_get_v4_rt(skb, dev, &fl4, info);
720     + sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
721     + rt = geneve_get_v4_rt(skb, dev, &fl4, info,
722     + geneve->dst_port, sport);
723     if (IS_ERR(rt)) {
724     err = PTR_ERR(rt);
725     goto tx_error;
726     }
727    
728     - sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
729     skb_reset_mac_header(skb);
730    
731     if (info) {
732     @@ -983,13 +990,14 @@ static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
733     }
734     }
735    
736     - dst = geneve_get_v6_dst(skb, dev, &fl6, info);
737     + sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
738     + dst = geneve_get_v6_dst(skb, dev, &fl6, info,
739     + geneve->dst_port, sport);
740     if (IS_ERR(dst)) {
741     err = PTR_ERR(dst);
742     goto tx_error;
743     }
744    
745     - sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
746     skb_reset_mac_header(skb);
747    
748     if (info) {
749     @@ -1114,9 +1122,14 @@ static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
750     struct dst_entry *dst;
751     struct flowi6 fl6;
752     #endif
753     + __be16 sport;
754    
755     if (ip_tunnel_info_af(info) == AF_INET) {
756     - rt = geneve_get_v4_rt(skb, dev, &fl4, info);
757     + sport = udp_flow_src_port(geneve->net, skb,
758     + 1, USHRT_MAX, true);
759     +
760     + rt = geneve_get_v4_rt(skb, dev, &fl4, info,
761     + geneve->dst_port, sport);
762     if (IS_ERR(rt))
763     return PTR_ERR(rt);
764    
765     @@ -1124,7 +1137,11 @@ static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
766     info->key.u.ipv4.src = fl4.saddr;
767     #if IS_ENABLED(CONFIG_IPV6)
768     } else if (ip_tunnel_info_af(info) == AF_INET6) {
769     - dst = geneve_get_v6_dst(skb, dev, &fl6, info);
770     + sport = udp_flow_src_port(geneve->net, skb,
771     + 1, USHRT_MAX, true);
772     +
773     + dst = geneve_get_v6_dst(skb, dev, &fl6, info,
774     + geneve->dst_port, sport);
775     if (IS_ERR(dst))
776     return PTR_ERR(dst);
777    
778     @@ -1135,8 +1152,7 @@ static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
779     return -EINVAL;
780     }
781    
782     - info->key.tp_src = udp_flow_src_port(geneve->net, skb,
783     - 1, USHRT_MAX, true);
784     + info->key.tp_src = sport;
785     info->key.tp_dst = geneve->dst_port;
786     return 0;
787     }
788     diff --git a/drivers/net/wan/cosa.c b/drivers/net/wan/cosa.c
789     index b87fe0a01c69f..3c02473a20f21 100644
790     --- a/drivers/net/wan/cosa.c
791     +++ b/drivers/net/wan/cosa.c
792     @@ -903,6 +903,7 @@ static ssize_t cosa_write(struct file *file,
793     chan->tx_status = 1;
794     spin_unlock_irqrestore(&cosa->lock, flags);
795     up(&chan->wsem);
796     + kfree(kbuf);
797     return -ERESTARTSYS;
798     }
799     }
800     diff --git a/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c b/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
801     index 2eb169b204f8d..6a9c9b4ef2c92 100644
802     --- a/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
803     +++ b/drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
804     @@ -972,7 +972,7 @@ static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
805     struct ath_htc_rx_status *rxstatus;
806     struct ath_rx_status rx_stats;
807     bool decrypt_error = false;
808     - __be16 rs_datalen;
809     + u16 rs_datalen;
810     bool is_phyerr;
811    
812     if (skb->len < HTC_RX_FRAME_HEADER_SIZE) {
813     diff --git a/drivers/net/xen-netback/common.h b/drivers/net/xen-netback/common.h
814     index 5b1d2e8402d9d..347c796afd4ed 100644
815     --- a/drivers/net/xen-netback/common.h
816     +++ b/drivers/net/xen-netback/common.h
817     @@ -140,6 +140,20 @@ struct xenvif_queue { /* Per-queue data for xenvif */
818     char name[QUEUE_NAME_SIZE]; /* DEVNAME-qN */
819     struct xenvif *vif; /* Parent VIF */
820    
821     + /*
822     + * TX/RX common EOI handling.
823     + * When feature-split-event-channels = 0, interrupt handler sets
824     + * NETBK_COMMON_EOI, otherwise NETBK_RX_EOI and NETBK_TX_EOI are set
825     + * by the RX and TX interrupt handlers.
826     + * RX and TX handler threads will issue an EOI when either
827     + * NETBK_COMMON_EOI or their specific bits (NETBK_RX_EOI or
828     + * NETBK_TX_EOI) are set and they will reset those bits.
829     + */
830     + atomic_t eoi_pending;
831     +#define NETBK_RX_EOI 0x01
832     +#define NETBK_TX_EOI 0x02
833     +#define NETBK_COMMON_EOI 0x04
834     +
835     /* Use NAPI for guest TX */
836     struct napi_struct napi;
837     /* When feature-split-event-channels = 0, tx_irq = rx_irq. */
838     @@ -356,6 +370,7 @@ int xenvif_dealloc_kthread(void *data);
839    
840     irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data);
841    
842     +bool xenvif_have_rx_work(struct xenvif_queue *queue, bool test_kthread);
843     void xenvif_rx_action(struct xenvif_queue *queue);
844     void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb);
845    
846     diff --git a/drivers/net/xen-netback/interface.c b/drivers/net/xen-netback/interface.c
847     index 46008f2845502..e61073c751428 100644
848     --- a/drivers/net/xen-netback/interface.c
849     +++ b/drivers/net/xen-netback/interface.c
850     @@ -76,12 +76,28 @@ int xenvif_schedulable(struct xenvif *vif)
851     !vif->disabled;
852     }
853    
854     +static bool xenvif_handle_tx_interrupt(struct xenvif_queue *queue)
855     +{
856     + bool rc;
857     +
858     + rc = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
859     + if (rc)
860     + napi_schedule(&queue->napi);
861     + return rc;
862     +}
863     +
864     static irqreturn_t xenvif_tx_interrupt(int irq, void *dev_id)
865     {
866     struct xenvif_queue *queue = dev_id;
867     + int old;
868    
869     - if (RING_HAS_UNCONSUMED_REQUESTS(&queue->tx))
870     - napi_schedule(&queue->napi);
871     + old = atomic_fetch_or(NETBK_TX_EOI, &queue->eoi_pending);
872     + WARN(old & NETBK_TX_EOI, "Interrupt while EOI pending\n");
873     +
874     + if (!xenvif_handle_tx_interrupt(queue)) {
875     + atomic_andnot(NETBK_TX_EOI, &queue->eoi_pending);
876     + xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
877     + }
878    
879     return IRQ_HANDLED;
880     }
881     @@ -115,19 +131,46 @@ static int xenvif_poll(struct napi_struct *napi, int budget)
882     return work_done;
883     }
884    
885     +static bool xenvif_handle_rx_interrupt(struct xenvif_queue *queue)
886     +{
887     + bool rc;
888     +
889     + rc = xenvif_have_rx_work(queue, false);
890     + if (rc)
891     + xenvif_kick_thread(queue);
892     + return rc;
893     +}
894     +
895     static irqreturn_t xenvif_rx_interrupt(int irq, void *dev_id)
896     {
897     struct xenvif_queue *queue = dev_id;
898     + int old;
899    
900     - xenvif_kick_thread(queue);
901     + old = atomic_fetch_or(NETBK_RX_EOI, &queue->eoi_pending);
902     + WARN(old & NETBK_RX_EOI, "Interrupt while EOI pending\n");
903     +
904     + if (!xenvif_handle_rx_interrupt(queue)) {
905     + atomic_andnot(NETBK_RX_EOI, &queue->eoi_pending);
906     + xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
907     + }
908    
909     return IRQ_HANDLED;
910     }
911    
912     irqreturn_t xenvif_interrupt(int irq, void *dev_id)
913     {
914     - xenvif_tx_interrupt(irq, dev_id);
915     - xenvif_rx_interrupt(irq, dev_id);
916     + struct xenvif_queue *queue = dev_id;
917     + int old;
918     +
919     + old = atomic_fetch_or(NETBK_COMMON_EOI, &queue->eoi_pending);
920     + WARN(old, "Interrupt while EOI pending\n");
921     +
922     + /* Use bitwise or as we need to call both functions. */
923     + if ((!xenvif_handle_tx_interrupt(queue) |
924     + !xenvif_handle_rx_interrupt(queue))) {
925     + atomic_andnot(NETBK_COMMON_EOI, &queue->eoi_pending);
926     + xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
927     + }
928    
929     return IRQ_HANDLED;
930     }
931     @@ -583,7 +626,7 @@ int xenvif_connect_ctrl(struct xenvif *vif, grant_ref_t ring_ref,
932     shared = (struct xen_netif_ctrl_sring *)addr;
933     BACK_RING_INIT(&vif->ctrl, shared, XEN_PAGE_SIZE);
934    
935     - err = bind_interdomain_evtchn_to_irq(vif->domid, evtchn);
936     + err = bind_interdomain_evtchn_to_irq_lateeoi(vif->domid, evtchn);
937     if (err < 0)
938     goto err_unmap;
939    
940     @@ -641,7 +684,7 @@ int xenvif_connect_data(struct xenvif_queue *queue,
941    
942     if (tx_evtchn == rx_evtchn) {
943     /* feature-split-event-channels == 0 */
944     - err = bind_interdomain_evtchn_to_irqhandler(
945     + err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
946     queue->vif->domid, tx_evtchn, xenvif_interrupt, 0,
947     queue->name, queue);
948     if (err < 0)
949     @@ -652,7 +695,7 @@ int xenvif_connect_data(struct xenvif_queue *queue,
950     /* feature-split-event-channels == 1 */
951     snprintf(queue->tx_irq_name, sizeof(queue->tx_irq_name),
952     "%s-tx", queue->name);
953     - err = bind_interdomain_evtchn_to_irqhandler(
954     + err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
955     queue->vif->domid, tx_evtchn, xenvif_tx_interrupt, 0,
956     queue->tx_irq_name, queue);
957     if (err < 0)
958     @@ -662,7 +705,7 @@ int xenvif_connect_data(struct xenvif_queue *queue,
959    
960     snprintf(queue->rx_irq_name, sizeof(queue->rx_irq_name),
961     "%s-rx", queue->name);
962     - err = bind_interdomain_evtchn_to_irqhandler(
963     + err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
964     queue->vif->domid, rx_evtchn, xenvif_rx_interrupt, 0,
965     queue->rx_irq_name, queue);
966     if (err < 0)
967     diff --git a/drivers/net/xen-netback/netback.c b/drivers/net/xen-netback/netback.c
968     index a469fbe1abaf3..fd2ac6cd0c691 100644
969     --- a/drivers/net/xen-netback/netback.c
970     +++ b/drivers/net/xen-netback/netback.c
971     @@ -162,6 +162,10 @@ void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
972    
973     if (more_to_do)
974     napi_schedule(&queue->napi);
975     + else if (atomic_fetch_andnot(NETBK_TX_EOI | NETBK_COMMON_EOI,
976     + &queue->eoi_pending) &
977     + (NETBK_TX_EOI | NETBK_COMMON_EOI))
978     + xen_irq_lateeoi(queue->tx_irq, 0);
979     }
980    
981     static void tx_add_credit(struct xenvif_queue *queue)
982     @@ -1615,9 +1619,14 @@ static bool xenvif_ctrl_work_todo(struct xenvif *vif)
983     irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data)
984     {
985     struct xenvif *vif = data;
986     + unsigned int eoi_flag = XEN_EOI_FLAG_SPURIOUS;
987    
988     - while (xenvif_ctrl_work_todo(vif))
989     + while (xenvif_ctrl_work_todo(vif)) {
990     xenvif_ctrl_action(vif);
991     + eoi_flag = 0;
992     + }
993     +
994     + xen_irq_lateeoi(irq, eoi_flag);
995    
996     return IRQ_HANDLED;
997     }
998     diff --git a/drivers/net/xen-netback/rx.c b/drivers/net/xen-netback/rx.c
999     index b1cf7c6f407a9..f152246c7dfb7 100644
1000     --- a/drivers/net/xen-netback/rx.c
1001     +++ b/drivers/net/xen-netback/rx.c
1002     @@ -490,13 +490,13 @@ static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
1003     return queue->stalled && prod - cons >= 1;
1004     }
1005    
1006     -static bool xenvif_have_rx_work(struct xenvif_queue *queue)
1007     +bool xenvif_have_rx_work(struct xenvif_queue *queue, bool test_kthread)
1008     {
1009     return xenvif_rx_ring_slots_available(queue) ||
1010     (queue->vif->stall_timeout &&
1011     (xenvif_rx_queue_stalled(queue) ||
1012     xenvif_rx_queue_ready(queue))) ||
1013     - kthread_should_stop() ||
1014     + (test_kthread && kthread_should_stop()) ||
1015     queue->vif->disabled;
1016     }
1017    
1018     @@ -527,15 +527,20 @@ static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
1019     {
1020     DEFINE_WAIT(wait);
1021    
1022     - if (xenvif_have_rx_work(queue))
1023     + if (xenvif_have_rx_work(queue, true))
1024     return;
1025    
1026     for (;;) {
1027     long ret;
1028    
1029     prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
1030     - if (xenvif_have_rx_work(queue))
1031     + if (xenvif_have_rx_work(queue, true))
1032     break;
1033     + if (atomic_fetch_andnot(NETBK_RX_EOI | NETBK_COMMON_EOI,
1034     + &queue->eoi_pending) &
1035     + (NETBK_RX_EOI | NETBK_COMMON_EOI))
1036     + xen_irq_lateeoi(queue->rx_irq, 0);
1037     +
1038     ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
1039     if (!ret)
1040     break;
1041     diff --git a/drivers/of/address.c b/drivers/of/address.c
1042     index 37619bb2c97ad..d188eacbd3b80 100644
1043     --- a/drivers/of/address.c
1044     +++ b/drivers/of/address.c
1045     @@ -901,11 +901,13 @@ EXPORT_SYMBOL_GPL(of_dma_get_range);
1046     */
1047     bool of_dma_is_coherent(struct device_node *np)
1048     {
1049     - struct device_node *node = of_node_get(np);
1050     + struct device_node *node;
1051    
1052     if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
1053     return true;
1054    
1055     + node = of_node_get(np);
1056     +
1057     while (node) {
1058     if (of_property_read_bool(node, "dma-coherent")) {
1059     of_node_put(node);
1060     diff --git a/drivers/pinctrl/aspeed/pinctrl-aspeed.c b/drivers/pinctrl/aspeed/pinctrl-aspeed.c
1061     index 49aeba9125319..23d2f0ba12db5 100644
1062     --- a/drivers/pinctrl/aspeed/pinctrl-aspeed.c
1063     +++ b/drivers/pinctrl/aspeed/pinctrl-aspeed.c
1064     @@ -387,13 +387,14 @@ int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
1065     static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr)
1066     {
1067     /*
1068     - * The signal type is GPIO if the signal name has "GPIO" as a prefix.
1069     + * The signal type is GPIO if the signal name has "GPI" as a prefix.
1070     * strncmp (rather than strcmp) is used to implement the prefix
1071     * requirement.
1072     *
1073     - * expr->signal might look like "GPIOT3" in the GPIO case.
1074     + * expr->signal might look like "GPIOB1" in the GPIO case.
1075     + * expr->signal might look like "GPIT0" in the GPI case.
1076     */
1077     - return strncmp(expr->signal, "GPIO", 4) == 0;
1078     + return strncmp(expr->signal, "GPI", 3) == 0;
1079     }
1080    
1081     static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs)
1082     diff --git a/drivers/pinctrl/devicetree.c b/drivers/pinctrl/devicetree.c
1083     index 54dad89fc9bfe..d32aedfc6dd03 100644
1084     --- a/drivers/pinctrl/devicetree.c
1085     +++ b/drivers/pinctrl/devicetree.c
1086     @@ -40,6 +40,13 @@ struct pinctrl_dt_map {
1087     static void dt_free_map(struct pinctrl_dev *pctldev,
1088     struct pinctrl_map *map, unsigned num_maps)
1089     {
1090     + int i;
1091     +
1092     + for (i = 0; i < num_maps; ++i) {
1093     + kfree_const(map[i].dev_name);
1094     + map[i].dev_name = NULL;
1095     + }
1096     +
1097     if (pctldev) {
1098     const struct pinctrl_ops *ops = pctldev->desc->pctlops;
1099     ops->dt_free_map(pctldev, map, num_maps);
1100     @@ -73,7 +80,13 @@ static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
1101    
1102     /* Initialize common mapping table entry fields */
1103     for (i = 0; i < num_maps; i++) {
1104     - map[i].dev_name = dev_name(p->dev);
1105     + const char *devname;
1106     +
1107     + devname = kstrdup_const(dev_name(p->dev), GFP_KERNEL);
1108     + if (!devname)
1109     + goto err_free_map;
1110     +
1111     + map[i].dev_name = devname;
1112     map[i].name = statename;
1113     if (pctldev)
1114     map[i].ctrl_dev_name = dev_name(pctldev->dev);
1115     @@ -81,11 +94,8 @@ static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
1116    
1117     /* Remember the converted mapping table entries */
1118     dt_map = kzalloc(sizeof(*dt_map), GFP_KERNEL);
1119     - if (!dt_map) {
1120     - dev_err(p->dev, "failed to alloc struct pinctrl_dt_map\n");
1121     - dt_free_map(pctldev, map, num_maps);
1122     - return -ENOMEM;
1123     - }
1124     + if (!dt_map)
1125     + goto err_free_map;
1126    
1127     dt_map->pctldev = pctldev;
1128     dt_map->map = map;
1129     @@ -93,6 +103,10 @@ static int dt_remember_or_free_map(struct pinctrl *p, const char *statename,
1130     list_add_tail(&dt_map->node, &p->dt_maps);
1131    
1132     return pinctrl_register_map(map, num_maps, false);
1133     +
1134     +err_free_map:
1135     + dt_free_map(pctldev, map, num_maps);
1136     + return -ENOMEM;
1137     }
1138    
1139     struct pinctrl_dev *of_pinctrl_get(struct device_node *np)
1140     diff --git a/drivers/pinctrl/pinctrl-amd.c b/drivers/pinctrl/pinctrl-amd.c
1141     index a5b7bd3c9bac3..82fac6261efa1 100644
1142     --- a/drivers/pinctrl/pinctrl-amd.c
1143     +++ b/drivers/pinctrl/pinctrl-amd.c
1144     @@ -140,7 +140,7 @@ static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
1145     pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
1146     pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
1147     } else if (debounce < 250000) {
1148     - time = debounce / 15600;
1149     + time = debounce / 15625;
1150     pin_reg |= time & DB_TMR_OUT_MASK;
1151     pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
1152     pin_reg |= BIT(DB_TMR_LARGE_OFF);
1153     @@ -150,14 +150,14 @@ static int amd_gpio_set_debounce(struct gpio_chip *gc, unsigned offset,
1154     pin_reg |= BIT(DB_TMR_OUT_UNIT_OFF);
1155     pin_reg |= BIT(DB_TMR_LARGE_OFF);
1156     } else {
1157     - pin_reg &= ~DB_CNTRl_MASK;
1158     + pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
1159     ret = -EINVAL;
1160     }
1161     } else {
1162     pin_reg &= ~BIT(DB_TMR_OUT_UNIT_OFF);
1163     pin_reg &= ~BIT(DB_TMR_LARGE_OFF);
1164     pin_reg &= ~DB_TMR_OUT_MASK;
1165     - pin_reg &= ~DB_CNTRl_MASK;
1166     + pin_reg &= ~(DB_CNTRl_MASK << DB_CNTRL_OFF);
1167     }
1168     writel(pin_reg, gpio_dev->base + offset * 4);
1169     spin_unlock_irqrestore(&gpio_dev->lock, flags);
1170     diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
1171     index 0f730e4bf6bcb..0caf751d85ded 100644
1172     --- a/drivers/regulator/core.c
1173     +++ b/drivers/regulator/core.c
1174     @@ -3185,6 +3185,8 @@ static int _regulator_get_voltage(struct regulator_dev *rdev)
1175     ret = rdev->desc->fixed_uV;
1176     } else if (rdev->supply) {
1177     ret = _regulator_get_voltage(rdev->supply->rdev);
1178     + } else if (rdev->supply_name) {
1179     + return -EPROBE_DEFER;
1180     } else {
1181     return -EINVAL;
1182     }
1183     diff --git a/drivers/scsi/device_handler/scsi_dh_alua.c b/drivers/scsi/device_handler/scsi_dh_alua.c
1184     index 60c288526355a..2bc3dc6244a5e 100644
1185     --- a/drivers/scsi/device_handler/scsi_dh_alua.c
1186     +++ b/drivers/scsi/device_handler/scsi_dh_alua.c
1187     @@ -657,8 +657,8 @@ static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
1188     rcu_read_lock();
1189     list_for_each_entry_rcu(h,
1190     &tmp_pg->dh_list, node) {
1191     - /* h->sdev should always be valid */
1192     - BUG_ON(!h->sdev);
1193     + if (!h->sdev)
1194     + continue;
1195     h->sdev->access_state = desc[0];
1196     }
1197     rcu_read_unlock();
1198     @@ -704,7 +704,8 @@ static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
1199     pg->expiry = 0;
1200     rcu_read_lock();
1201     list_for_each_entry_rcu(h, &pg->dh_list, node) {
1202     - BUG_ON(!h->sdev);
1203     + if (!h->sdev)
1204     + continue;
1205     h->sdev->access_state =
1206     (pg->state & SCSI_ACCESS_STATE_MASK);
1207     if (pg->pref)
1208     @@ -1149,7 +1150,6 @@ static void alua_bus_detach(struct scsi_device *sdev)
1209     spin_lock(&h->pg_lock);
1210     pg = h->pg;
1211     rcu_assign_pointer(h->pg, NULL);
1212     - h->sdev = NULL;
1213     spin_unlock(&h->pg_lock);
1214     if (pg) {
1215     spin_lock_irq(&pg->lock);
1216     @@ -1158,6 +1158,7 @@ static void alua_bus_detach(struct scsi_device *sdev)
1217     kref_put(&pg->kref, release_port_group);
1218     }
1219     sdev->handler_data = NULL;
1220     + synchronize_rcu();
1221     kfree(h);
1222     }
1223    
1224     diff --git a/drivers/scsi/hpsa.c b/drivers/scsi/hpsa.c
1225     index b82df8cdf9626..7f1d6d52d48bd 100644
1226     --- a/drivers/scsi/hpsa.c
1227     +++ b/drivers/scsi/hpsa.c
1228     @@ -8937,7 +8937,7 @@ reinit_after_soft_reset:
1229     /* hook into SCSI subsystem */
1230     rc = hpsa_scsi_add_host(h);
1231     if (rc)
1232     - goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
1233     + goto clean8; /* lastlogicals, perf, sg, cmd, irq, shost, pci, lu, aer/h */
1234    
1235     /* Monitor the controller for firmware lockups */
1236     h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
1237     @@ -8949,6 +8949,8 @@ reinit_after_soft_reset:
1238     h->heartbeat_sample_interval);
1239     return 0;
1240    
1241     +clean8: /* lastlogicals, perf, sg, cmd, irq, shost, pci, lu, aer/h */
1242     + kfree(h->lastlogicals);
1243     clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
1244     hpsa_free_performant_mode(h);
1245     h->access.set_intr_mask(h, HPSA_INTR_OFF);
1246     diff --git a/drivers/usb/class/cdc-acm.c b/drivers/usb/class/cdc-acm.c
1247     index 726291c5562da..90f4a5c8012ca 100644
1248     --- a/drivers/usb/class/cdc-acm.c
1249     +++ b/drivers/usb/class/cdc-acm.c
1250     @@ -1648,6 +1648,15 @@ static const struct usb_device_id acm_ids[] = {
1251     { USB_DEVICE(0x0870, 0x0001), /* Metricom GS Modem */
1252     .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1253     },
1254     + { USB_DEVICE(0x045b, 0x023c), /* Renesas USB Download mode */
1255     + .driver_info = DISABLE_ECHO, /* Don't echo banner */
1256     + },
1257     + { USB_DEVICE(0x045b, 0x0248), /* Renesas USB Download mode */
1258     + .driver_info = DISABLE_ECHO, /* Don't echo banner */
1259     + },
1260     + { USB_DEVICE(0x045b, 0x024D), /* Renesas USB Download mode */
1261     + .driver_info = DISABLE_ECHO, /* Don't echo banner */
1262     + },
1263     { USB_DEVICE(0x0e8d, 0x0003), /* FIREFLY, MediaTek Inc; andrey.arapov@gmail.com */
1264     .driver_info = NO_UNION_NORMAL, /* has no union descriptor */
1265     },
1266     diff --git a/drivers/usb/gadget/udc/goku_udc.c b/drivers/usb/gadget/udc/goku_udc.c
1267     index 5107987bd3538..d363224dce6f5 100644
1268     --- a/drivers/usb/gadget/udc/goku_udc.c
1269     +++ b/drivers/usb/gadget/udc/goku_udc.c
1270     @@ -1772,6 +1772,7 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1271     goto err;
1272     }
1273    
1274     + pci_set_drvdata(pdev, dev);
1275     spin_lock_init(&dev->lock);
1276     dev->pdev = pdev;
1277     dev->gadget.ops = &goku_ops;
1278     @@ -1805,7 +1806,6 @@ static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1279     }
1280     dev->regs = (struct goku_udc_regs __iomem *) base;
1281    
1282     - pci_set_drvdata(pdev, dev);
1283     INFO(dev, "%s\n", driver_desc);
1284     INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1285     INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
1286     diff --git a/drivers/xen/events/events_2l.c b/drivers/xen/events/events_2l.c
1287     index bdff01095f549..c31c08a708be5 100644
1288     --- a/drivers/xen/events/events_2l.c
1289     +++ b/drivers/xen/events/events_2l.c
1290     @@ -90,6 +90,8 @@ static void evtchn_2l_unmask(unsigned port)
1291    
1292     BUG_ON(!irqs_disabled());
1293    
1294     + smp_wmb(); /* All writes before unmask must be visible. */
1295     +
1296     if (unlikely((cpu != cpu_from_evtchn(port))))
1297     do_hypercall = 1;
1298     else {
1299     @@ -158,7 +160,7 @@ static inline xen_ulong_t active_evtchns(unsigned int cpu,
1300     * a bitset of words which contain pending event bits. The second
1301     * level is a bitset of pending events themselves.
1302     */
1303     -static void evtchn_2l_handle_events(unsigned cpu)
1304     +static void evtchn_2l_handle_events(unsigned cpu, struct evtchn_loop_ctrl *ctrl)
1305     {
1306     int irq;
1307     xen_ulong_t pending_words;
1308     @@ -239,10 +241,7 @@ static void evtchn_2l_handle_events(unsigned cpu)
1309    
1310     /* Process port. */
1311     port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
1312     - irq = get_evtchn_to_irq(port);
1313     -
1314     - if (irq != -1)
1315     - generic_handle_irq(irq);
1316     + handle_irq_for_port(port, ctrl);
1317    
1318     bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
1319    
1320     diff --git a/drivers/xen/events/events_base.c b/drivers/xen/events/events_base.c
1321     index 83bb9fdbadc6a..5308bc1e0189d 100644
1322     --- a/drivers/xen/events/events_base.c
1323     +++ b/drivers/xen/events/events_base.c
1324     @@ -32,6 +32,10 @@
1325     #include <linux/slab.h>
1326     #include <linux/irqnr.h>
1327     #include <linux/pci.h>
1328     +#include <linux/spinlock.h>
1329     +#include <linux/cpuhotplug.h>
1330     +#include <linux/atomic.h>
1331     +#include <linux/ktime.h>
1332    
1333     #ifdef CONFIG_X86
1334     #include <asm/desc.h>
1335     @@ -62,6 +66,15 @@
1336    
1337     #include "events_internal.h"
1338    
1339     +#undef MODULE_PARAM_PREFIX
1340     +#define MODULE_PARAM_PREFIX "xen."
1341     +
1342     +static uint __read_mostly event_loop_timeout = 2;
1343     +module_param(event_loop_timeout, uint, 0644);
1344     +
1345     +static uint __read_mostly event_eoi_delay = 10;
1346     +module_param(event_eoi_delay, uint, 0644);
1347     +
1348     const struct evtchn_ops *evtchn_ops;
1349    
1350     /*
1351     @@ -70,6 +83,24 @@ const struct evtchn_ops *evtchn_ops;
1352     */
1353     static DEFINE_MUTEX(irq_mapping_update_lock);
1354    
1355     +/*
1356     + * Lock protecting event handling loop against removing event channels.
1357     + * Adding of event channels is no issue as the associated IRQ becomes active
1358     + * only after everything is setup (before request_[threaded_]irq() the handler
1359     + * can't be entered for an event, as the event channel will be unmasked only
1360     + * then).
1361     + */
1362     +static DEFINE_RWLOCK(evtchn_rwlock);
1363     +
1364     +/*
1365     + * Lock hierarchy:
1366     + *
1367     + * irq_mapping_update_lock
1368     + * evtchn_rwlock
1369     + * IRQ-desc lock
1370     + * percpu eoi_list_lock
1371     + */
1372     +
1373     static LIST_HEAD(xen_irq_list_head);
1374    
1375     /* IRQ <-> VIRQ mapping. */
1376     @@ -94,17 +125,20 @@ static bool (*pirq_needs_eoi)(unsigned irq);
1377     static struct irq_info *legacy_info_ptrs[NR_IRQS_LEGACY];
1378    
1379     static struct irq_chip xen_dynamic_chip;
1380     +static struct irq_chip xen_lateeoi_chip;
1381     static struct irq_chip xen_percpu_chip;
1382     static struct irq_chip xen_pirq_chip;
1383     static void enable_dynirq(struct irq_data *data);
1384     static void disable_dynirq(struct irq_data *data);
1385    
1386     +static DEFINE_PER_CPU(unsigned int, irq_epoch);
1387     +
1388     static void clear_evtchn_to_irq_row(unsigned row)
1389     {
1390     unsigned col;
1391    
1392     for (col = 0; col < EVTCHN_PER_ROW; col++)
1393     - evtchn_to_irq[row][col] = -1;
1394     + WRITE_ONCE(evtchn_to_irq[row][col], -1);
1395     }
1396    
1397     static void clear_evtchn_to_irq_all(void)
1398     @@ -141,7 +175,7 @@ static int set_evtchn_to_irq(unsigned evtchn, unsigned irq)
1399     clear_evtchn_to_irq_row(row);
1400     }
1401    
1402     - evtchn_to_irq[row][col] = irq;
1403     + WRITE_ONCE(evtchn_to_irq[row][col], irq);
1404     return 0;
1405     }
1406    
1407     @@ -151,7 +185,7 @@ int get_evtchn_to_irq(unsigned evtchn)
1408     return -1;
1409     if (evtchn_to_irq[EVTCHN_ROW(evtchn)] == NULL)
1410     return -1;
1411     - return evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)];
1412     + return READ_ONCE(evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)]);
1413     }
1414    
1415     /* Get info for IRQ */
1416     @@ -260,10 +294,14 @@ static void xen_irq_info_cleanup(struct irq_info *info)
1417     */
1418     unsigned int evtchn_from_irq(unsigned irq)
1419     {
1420     - if (unlikely(WARN(irq >= nr_irqs, "Invalid irq %d!\n", irq)))
1421     + const struct irq_info *info = NULL;
1422     +
1423     + if (likely(irq < nr_irqs))
1424     + info = info_for_irq(irq);
1425     + if (!info)
1426     return 0;
1427    
1428     - return info_for_irq(irq)->evtchn;
1429     + return info->evtchn;
1430     }
1431    
1432     unsigned irq_from_evtchn(unsigned int evtchn)
1433     @@ -382,9 +420,157 @@ void notify_remote_via_irq(int irq)
1434     }
1435     EXPORT_SYMBOL_GPL(notify_remote_via_irq);
1436    
1437     +struct lateeoi_work {
1438     + struct delayed_work delayed;
1439     + spinlock_t eoi_list_lock;
1440     + struct list_head eoi_list;
1441     +};
1442     +
1443     +static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
1444     +
1445     +static void lateeoi_list_del(struct irq_info *info)
1446     +{
1447     + struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
1448     + unsigned long flags;
1449     +
1450     + spin_lock_irqsave(&eoi->eoi_list_lock, flags);
1451     + list_del_init(&info->eoi_list);
1452     + spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
1453     +}
1454     +
1455     +static void lateeoi_list_add(struct irq_info *info)
1456     +{
1457     + struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
1458     + struct irq_info *elem;
1459     + u64 now = get_jiffies_64();
1460     + unsigned long delay;
1461     + unsigned long flags;
1462     +
1463     + if (now < info->eoi_time)
1464     + delay = info->eoi_time - now;
1465     + else
1466     + delay = 1;
1467     +
1468     + spin_lock_irqsave(&eoi->eoi_list_lock, flags);
1469     +
1470     + if (list_empty(&eoi->eoi_list)) {
1471     + list_add(&info->eoi_list, &eoi->eoi_list);
1472     + mod_delayed_work_on(info->eoi_cpu, system_wq,
1473     + &eoi->delayed, delay);
1474     + } else {
1475     + list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
1476     + if (elem->eoi_time <= info->eoi_time)
1477     + break;
1478     + }
1479     + list_add(&info->eoi_list, &elem->eoi_list);
1480     + }
1481     +
1482     + spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
1483     +}
1484     +
1485     +static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
1486     +{
1487     + evtchn_port_t evtchn;
1488     + unsigned int cpu;
1489     + unsigned int delay = 0;
1490     +
1491     + evtchn = info->evtchn;
1492     + if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
1493     + return;
1494     +
1495     + if (spurious) {
1496     + if ((1 << info->spurious_cnt) < (HZ << 2))
1497     + info->spurious_cnt++;
1498     + if (info->spurious_cnt > 1) {
1499     + delay = 1 << (info->spurious_cnt - 2);
1500     + if (delay > HZ)
1501     + delay = HZ;
1502     + if (!info->eoi_time)
1503     + info->eoi_cpu = smp_processor_id();
1504     + info->eoi_time = get_jiffies_64() + delay;
1505     + }
1506     + } else {
1507     + info->spurious_cnt = 0;
1508     + }
1509     +
1510     + cpu = info->eoi_cpu;
1511     + if (info->eoi_time &&
1512     + (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
1513     + lateeoi_list_add(info);
1514     + return;
1515     + }
1516     +
1517     + info->eoi_time = 0;
1518     + unmask_evtchn(evtchn);
1519     +}
1520     +
1521     +static void xen_irq_lateeoi_worker(struct work_struct *work)
1522     +{
1523     + struct lateeoi_work *eoi;
1524     + struct irq_info *info;
1525     + u64 now = get_jiffies_64();
1526     + unsigned long flags;
1527     +
1528     + eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
1529     +
1530     + read_lock_irqsave(&evtchn_rwlock, flags);
1531     +
1532     + while (true) {
1533     + spin_lock(&eoi->eoi_list_lock);
1534     +
1535     + info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
1536     + eoi_list);
1537     +
1538     + if (info == NULL || now < info->eoi_time) {
1539     + spin_unlock(&eoi->eoi_list_lock);
1540     + break;
1541     + }
1542     +
1543     + list_del_init(&info->eoi_list);
1544     +
1545     + spin_unlock(&eoi->eoi_list_lock);
1546     +
1547     + info->eoi_time = 0;
1548     +
1549     + xen_irq_lateeoi_locked(info, false);
1550     + }
1551     +
1552     + if (info)
1553     + mod_delayed_work_on(info->eoi_cpu, system_wq,
1554     + &eoi->delayed, info->eoi_time - now);
1555     +
1556     + read_unlock_irqrestore(&evtchn_rwlock, flags);
1557     +}
1558     +
1559     +static void xen_cpu_init_eoi(unsigned int cpu)
1560     +{
1561     + struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
1562     +
1563     + INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
1564     + spin_lock_init(&eoi->eoi_list_lock);
1565     + INIT_LIST_HEAD(&eoi->eoi_list);
1566     +}
1567     +
1568     +void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
1569     +{
1570     + struct irq_info *info;
1571     + unsigned long flags;
1572     +
1573     + read_lock_irqsave(&evtchn_rwlock, flags);
1574     +
1575     + info = info_for_irq(irq);
1576     +
1577     + if (info)
1578     + xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
1579     +
1580     + read_unlock_irqrestore(&evtchn_rwlock, flags);
1581     +}
1582     +EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
1583     +
1584     static void xen_irq_init(unsigned irq)
1585     {
1586     struct irq_info *info;
1587     +
1588     #ifdef CONFIG_SMP
1589     /* By default all event channels notify CPU#0. */
1590     cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(0));
1591     @@ -399,6 +585,7 @@ static void xen_irq_init(unsigned irq)
1592    
1593     set_info_for_irq(irq, info);
1594    
1595     + INIT_LIST_HEAD(&info->eoi_list);
1596     list_add_tail(&info->list, &xen_irq_list_head);
1597     }
1598    
1599     @@ -447,16 +634,24 @@ static int __must_check xen_allocate_irq_gsi(unsigned gsi)
1600     static void xen_free_irq(unsigned irq)
1601     {
1602     struct irq_info *info = info_for_irq(irq);
1603     + unsigned long flags;
1604    
1605     if (WARN_ON(!info))
1606     return;
1607    
1608     + write_lock_irqsave(&evtchn_rwlock, flags);
1609     +
1610     + if (!list_empty(&info->eoi_list))
1611     + lateeoi_list_del(info);
1612     +
1613     list_del(&info->list);
1614    
1615     set_info_for_irq(irq, NULL);
1616    
1617     WARN_ON(info->refcnt > 0);
1618    
1619     + write_unlock_irqrestore(&evtchn_rwlock, flags);
1620     +
1621     kfree(info);
1622    
1623     /* Legacy IRQ descriptors are managed by the arch. */
1624     @@ -848,7 +1043,7 @@ int xen_pirq_from_irq(unsigned irq)
1625     }
1626     EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1627    
1628     -int bind_evtchn_to_irq(unsigned int evtchn)
1629     +static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip)
1630     {
1631     int irq;
1632     int ret;
1633     @@ -865,7 +1060,7 @@ int bind_evtchn_to_irq(unsigned int evtchn)
1634     if (irq < 0)
1635     goto out;
1636    
1637     - irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1638     + irq_set_chip_and_handler_name(irq, chip,
1639     handle_edge_irq, "event");
1640    
1641     ret = xen_irq_info_evtchn_setup(irq, evtchn);
1642     @@ -886,8 +1081,19 @@ out:
1643    
1644     return irq;
1645     }
1646     +
1647     +int bind_evtchn_to_irq(evtchn_port_t evtchn)
1648     +{
1649     + return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip);
1650     +}
1651     EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1652    
1653     +int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1654     +{
1655     + return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip);
1656     +}
1657     +EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1658     +
1659     static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1660     {
1661     struct evtchn_bind_ipi bind_ipi;
1662     @@ -929,8 +1135,9 @@ static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1663     return irq;
1664     }
1665    
1666     -int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
1667     - unsigned int remote_port)
1668     +static int bind_interdomain_evtchn_to_irq_chip(unsigned int remote_domain,
1669     + evtchn_port_t remote_port,
1670     + struct irq_chip *chip)
1671     {
1672     struct evtchn_bind_interdomain bind_interdomain;
1673     int err;
1674     @@ -941,10 +1148,26 @@ int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
1675     err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1676     &bind_interdomain);
1677    
1678     - return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
1679     + return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1680     + chip);
1681     +}
1682     +
1683     +int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
1684     + evtchn_port_t remote_port)
1685     +{
1686     + return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1687     + &xen_dynamic_chip);
1688     }
1689     EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq);
1690    
1691     +int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
1692     + evtchn_port_t remote_port)
1693     +{
1694     + return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1695     + &xen_lateeoi_chip);
1696     +}
1697     +EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1698     +
1699     static int find_virq(unsigned int virq, unsigned int cpu)
1700     {
1701     struct evtchn_status status;
1702     @@ -1040,14 +1263,15 @@ static void unbind_from_irq(unsigned int irq)
1703     mutex_unlock(&irq_mapping_update_lock);
1704     }
1705    
1706     -int bind_evtchn_to_irqhandler(unsigned int evtchn,
1707     - irq_handler_t handler,
1708     - unsigned long irqflags,
1709     - const char *devname, void *dev_id)
1710     +static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1711     + irq_handler_t handler,
1712     + unsigned long irqflags,
1713     + const char *devname, void *dev_id,
1714     + struct irq_chip *chip)
1715     {
1716     int irq, retval;
1717    
1718     - irq = bind_evtchn_to_irq(evtchn);
1719     + irq = bind_evtchn_to_irq_chip(evtchn, chip);
1720     if (irq < 0)
1721     return irq;
1722     retval = request_irq(irq, handler, irqflags, devname, dev_id);
1723     @@ -1058,18 +1282,38 @@ int bind_evtchn_to_irqhandler(unsigned int evtchn,
1724    
1725     return irq;
1726     }
1727     +
1728     +int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1729     + irq_handler_t handler,
1730     + unsigned long irqflags,
1731     + const char *devname, void *dev_id)
1732     +{
1733     + return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1734     + devname, dev_id,
1735     + &xen_dynamic_chip);
1736     +}
1737     EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1738    
1739     -int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1740     - unsigned int remote_port,
1741     - irq_handler_t handler,
1742     - unsigned long irqflags,
1743     - const char *devname,
1744     - void *dev_id)
1745     +int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1746     + irq_handler_t handler,
1747     + unsigned long irqflags,
1748     + const char *devname, void *dev_id)
1749     +{
1750     + return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1751     + devname, dev_id,
1752     + &xen_lateeoi_chip);
1753     +}
1754     +EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1755     +
1756     +static int bind_interdomain_evtchn_to_irqhandler_chip(
1757     + unsigned int remote_domain, evtchn_port_t remote_port,
1758     + irq_handler_t handler, unsigned long irqflags,
1759     + const char *devname, void *dev_id, struct irq_chip *chip)
1760     {
1761     int irq, retval;
1762    
1763     - irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1764     + irq = bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1765     + chip);
1766     if (irq < 0)
1767     return irq;
1768    
1769     @@ -1081,8 +1325,33 @@ int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1770    
1771     return irq;
1772     }
1773     +
1774     +int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1775     + evtchn_port_t remote_port,
1776     + irq_handler_t handler,
1777     + unsigned long irqflags,
1778     + const char *devname,
1779     + void *dev_id)
1780     +{
1781     + return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1782     + remote_port, handler, irqflags, devname,
1783     + dev_id, &xen_dynamic_chip);
1784     +}
1785     EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1786    
1787     +int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
1788     + evtchn_port_t remote_port,
1789     + irq_handler_t handler,
1790     + unsigned long irqflags,
1791     + const char *devname,
1792     + void *dev_id)
1793     +{
1794     + return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1795     + remote_port, handler, irqflags, devname,
1796     + dev_id, &xen_lateeoi_chip);
1797     +}
1798     +EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1799     +
1800     int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1801     irq_handler_t handler,
1802     unsigned long irqflags, const char *devname, void *dev_id)
1803     @@ -1195,7 +1464,7 @@ int evtchn_get(unsigned int evtchn)
1804     goto done;
1805    
1806     err = -EINVAL;
1807     - if (info->refcnt <= 0)
1808     + if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1809     goto done;
1810    
1811     info->refcnt++;
1812     @@ -1234,6 +1503,54 @@ void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1813     notify_remote_via_irq(irq);
1814     }
1815    
1816     +struct evtchn_loop_ctrl {
1817     + ktime_t timeout;
1818     + unsigned count;
1819     + bool defer_eoi;
1820     +};
1821     +
1822     +void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1823     +{
1824     + int irq;
1825     + struct irq_info *info;
1826     +
1827     + irq = get_evtchn_to_irq(port);
1828     + if (irq == -1)
1829     + return;
1830     +
1831     + /*
1832     + * Check for timeout every 256 events.
1833     + * We are setting the timeout value only after the first 256
1834     + * events in order to not hurt the common case of few loop
1835     + * iterations. The 256 is basically an arbitrary value.
1836     + *
1837     + * In case we are hitting the timeout we need to defer all further
1838     + * EOIs in order to ensure to leave the event handling loop rather
1839     + * sooner than later.
1840     + */
1841     + if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1842     + ktime_t kt = ktime_get();
1843     +
1844     + if (!ctrl->timeout.tv64) {
1845     + kt = ktime_add_ms(kt,
1846     + jiffies_to_msecs(event_loop_timeout));
1847     + ctrl->timeout = kt;
1848     + } else if (kt.tv64 > ctrl->timeout.tv64) {
1849     + ctrl->defer_eoi = true;
1850     + }
1851     + }
1852     +
1853     + info = info_for_irq(irq);
1854     +
1855     + if (ctrl->defer_eoi) {
1856     + info->eoi_cpu = smp_processor_id();
1857     + info->irq_epoch = __this_cpu_read(irq_epoch);
1858     + info->eoi_time = get_jiffies_64() + event_eoi_delay;
1859     + }
1860     +
1861     + generic_handle_irq(irq);
1862     +}
1863     +
1864     static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1865    
1866     static void __xen_evtchn_do_upcall(void)
1867     @@ -1241,6 +1558,9 @@ static void __xen_evtchn_do_upcall(void)
1868     struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1869     int cpu = get_cpu();
1870     unsigned count;
1871     + struct evtchn_loop_ctrl ctrl = { 0 };
1872     +
1873     + read_lock(&evtchn_rwlock);
1874    
1875     do {
1876     vcpu_info->evtchn_upcall_pending = 0;
1877     @@ -1248,7 +1568,7 @@ static void __xen_evtchn_do_upcall(void)
1878     if (__this_cpu_inc_return(xed_nesting_count) - 1)
1879     goto out;
1880    
1881     - xen_evtchn_handle_events(cpu);
1882     + xen_evtchn_handle_events(cpu, &ctrl);
1883    
1884     BUG_ON(!irqs_disabled());
1885    
1886     @@ -1257,6 +1577,14 @@ static void __xen_evtchn_do_upcall(void)
1887     } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1888    
1889     out:
1890     + read_unlock(&evtchn_rwlock);
1891     +
1892     + /*
1893     + * Increment irq_epoch only now to defer EOIs only for
1894     + * xen_irq_lateeoi() invocations occurring from inside the loop
1895     + * above.
1896     + */
1897     + __this_cpu_inc(irq_epoch);
1898    
1899     put_cpu();
1900     }
1901     @@ -1613,6 +1941,21 @@ static struct irq_chip xen_dynamic_chip __read_mostly = {
1902     .irq_retrigger = retrigger_dynirq,
1903     };
1904    
1905     +static struct irq_chip xen_lateeoi_chip __read_mostly = {
1906     + /* The chip name needs to contain "xen-dyn" for irqbalance to work. */
1907     + .name = "xen-dyn-lateeoi",
1908     +
1909     + .irq_disable = disable_dynirq,
1910     + .irq_mask = disable_dynirq,
1911     + .irq_unmask = enable_dynirq,
1912     +
1913     + .irq_ack = mask_ack_dynirq,
1914     + .irq_mask_ack = mask_ack_dynirq,
1915     +
1916     + .irq_set_affinity = set_affinity_irq,
1917     + .irq_retrigger = retrigger_dynirq,
1918     +};
1919     +
1920     static struct irq_chip xen_pirq_chip __read_mostly = {
1921     .name = "xen-pirq",
1922    
1923     @@ -1680,12 +2023,31 @@ void xen_callback_vector(void)
1924     void xen_callback_vector(void) {}
1925     #endif
1926    
1927     -#undef MODULE_PARAM_PREFIX
1928     -#define MODULE_PARAM_PREFIX "xen."
1929     -
1930     static bool fifo_events = true;
1931     module_param(fifo_events, bool, 0);
1932    
1933     +static int xen_evtchn_cpu_prepare(unsigned int cpu)
1934     +{
1935     + int ret = 0;
1936     +
1937     + xen_cpu_init_eoi(cpu);
1938     +
1939     + if (evtchn_ops->percpu_init)
1940     + ret = evtchn_ops->percpu_init(cpu);
1941     +
1942     + return ret;
1943     +}
1944     +
1945     +static int xen_evtchn_cpu_dead(unsigned int cpu)
1946     +{
1947     + int ret = 0;
1948     +
1949     + if (evtchn_ops->percpu_deinit)
1950     + ret = evtchn_ops->percpu_deinit(cpu);
1951     +
1952     + return ret;
1953     +}
1954     +
1955     void __init xen_init_IRQ(void)
1956     {
1957     int ret = -EINVAL;
1958     @@ -1695,6 +2057,12 @@ void __init xen_init_IRQ(void)
1959     if (ret < 0)
1960     xen_evtchn_2l_init();
1961    
1962     + xen_cpu_init_eoi(smp_processor_id());
1963     +
1964     + cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
1965     + "CPUHP_XEN_EVTCHN_PREPARE",
1966     + xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
1967     +
1968     evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
1969     sizeof(*evtchn_to_irq), GFP_KERNEL);
1970     BUG_ON(!evtchn_to_irq);
1971     diff --git a/drivers/xen/events/events_fifo.c b/drivers/xen/events/events_fifo.c
1972     index 7ef27c6ed72fb..0a4fece5fd8d3 100644
1973     --- a/drivers/xen/events/events_fifo.c
1974     +++ b/drivers/xen/events/events_fifo.c
1975     @@ -227,19 +227,25 @@ static bool evtchn_fifo_is_masked(unsigned port)
1976     return sync_test_bit(EVTCHN_FIFO_BIT(MASKED, word), BM(word));
1977     }
1978     /*
1979     - * Clear MASKED, spinning if BUSY is set.
1980     + * Clear MASKED if not PENDING, spinning if BUSY is set.
1981     + * Return true if mask was cleared.
1982     */
1983     -static void clear_masked(volatile event_word_t *word)
1984     +static bool clear_masked_cond(volatile event_word_t *word)
1985     {
1986     event_word_t new, old, w;
1987    
1988     w = *word;
1989    
1990     do {
1991     + if (w & (1 << EVTCHN_FIFO_PENDING))
1992     + return false;
1993     +
1994     old = w & ~(1 << EVTCHN_FIFO_BUSY);
1995     new = old & ~(1 << EVTCHN_FIFO_MASKED);
1996     w = sync_cmpxchg(word, old, new);
1997     } while (w != old);
1998     +
1999     + return true;
2000     }
2001    
2002     static void evtchn_fifo_unmask(unsigned port)
2003     @@ -248,8 +254,7 @@ static void evtchn_fifo_unmask(unsigned port)
2004    
2005     BUG_ON(!irqs_disabled());
2006    
2007     - clear_masked(word);
2008     - if (evtchn_fifo_is_pending(port)) {
2009     + if (!clear_masked_cond(word)) {
2010     struct evtchn_unmask unmask = { .port = port };
2011     (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
2012     }
2013     @@ -270,19 +275,9 @@ static uint32_t clear_linked(volatile event_word_t *word)
2014     return w & EVTCHN_FIFO_LINK_MASK;
2015     }
2016    
2017     -static void handle_irq_for_port(unsigned port)
2018     -{
2019     - int irq;
2020     -
2021     - irq = get_evtchn_to_irq(port);
2022     - if (irq != -1)
2023     - generic_handle_irq(irq);
2024     -}
2025     -
2026     -static void consume_one_event(unsigned cpu,
2027     +static void consume_one_event(unsigned cpu, struct evtchn_loop_ctrl *ctrl,
2028     struct evtchn_fifo_control_block *control_block,
2029     - unsigned priority, unsigned long *ready,
2030     - bool drop)
2031     + unsigned priority, unsigned long *ready)
2032     {
2033     struct evtchn_fifo_queue *q = &per_cpu(cpu_queue, cpu);
2034     uint32_t head;
2035     @@ -315,16 +310,17 @@ static void consume_one_event(unsigned cpu,
2036     clear_bit(priority, ready);
2037    
2038     if (evtchn_fifo_is_pending(port) && !evtchn_fifo_is_masked(port)) {
2039     - if (unlikely(drop))
2040     + if (unlikely(!ctrl))
2041     pr_warn("Dropping pending event for port %u\n", port);
2042     else
2043     - handle_irq_for_port(port);
2044     + handle_irq_for_port(port, ctrl);
2045     }
2046    
2047     q->head[priority] = head;
2048     }
2049    
2050     -static void __evtchn_fifo_handle_events(unsigned cpu, bool drop)
2051     +static void __evtchn_fifo_handle_events(unsigned cpu,
2052     + struct evtchn_loop_ctrl *ctrl)
2053     {
2054     struct evtchn_fifo_control_block *control_block;
2055     unsigned long ready;
2056     @@ -336,14 +332,15 @@ static void __evtchn_fifo_handle_events(unsigned cpu, bool drop)
2057    
2058     while (ready) {
2059     q = find_first_bit(&ready, EVTCHN_FIFO_MAX_QUEUES);
2060     - consume_one_event(cpu, control_block, q, &ready, drop);
2061     + consume_one_event(cpu, ctrl, control_block, q, &ready);
2062     ready |= xchg(&control_block->ready, 0);
2063     }
2064     }
2065    
2066     -static void evtchn_fifo_handle_events(unsigned cpu)
2067     +static void evtchn_fifo_handle_events(unsigned cpu,
2068     + struct evtchn_loop_ctrl *ctrl)
2069     {
2070     - __evtchn_fifo_handle_events(cpu, false);
2071     + __evtchn_fifo_handle_events(cpu, ctrl);
2072     }
2073    
2074     static void evtchn_fifo_resume(void)
2075     @@ -381,21 +378,6 @@ static void evtchn_fifo_resume(void)
2076     event_array_pages = 0;
2077     }
2078    
2079     -static const struct evtchn_ops evtchn_ops_fifo = {
2080     - .max_channels = evtchn_fifo_max_channels,
2081     - .nr_channels = evtchn_fifo_nr_channels,
2082     - .setup = evtchn_fifo_setup,
2083     - .bind_to_cpu = evtchn_fifo_bind_to_cpu,
2084     - .clear_pending = evtchn_fifo_clear_pending,
2085     - .set_pending = evtchn_fifo_set_pending,
2086     - .is_pending = evtchn_fifo_is_pending,
2087     - .test_and_set_mask = evtchn_fifo_test_and_set_mask,
2088     - .mask = evtchn_fifo_mask,
2089     - .unmask = evtchn_fifo_unmask,
2090     - .handle_events = evtchn_fifo_handle_events,
2091     - .resume = evtchn_fifo_resume,
2092     -};
2093     -
2094     static int evtchn_fifo_alloc_control_block(unsigned cpu)
2095     {
2096     void *control_block = NULL;
2097     @@ -418,19 +400,36 @@ static int evtchn_fifo_alloc_control_block(unsigned cpu)
2098     return ret;
2099     }
2100    
2101     -static int xen_evtchn_cpu_prepare(unsigned int cpu)
2102     +static int evtchn_fifo_percpu_init(unsigned int cpu)
2103     {
2104     if (!per_cpu(cpu_control_block, cpu))
2105     return evtchn_fifo_alloc_control_block(cpu);
2106     return 0;
2107     }
2108    
2109     -static int xen_evtchn_cpu_dead(unsigned int cpu)
2110     +static int evtchn_fifo_percpu_deinit(unsigned int cpu)
2111     {
2112     - __evtchn_fifo_handle_events(cpu, true);
2113     + __evtchn_fifo_handle_events(cpu, NULL);
2114     return 0;
2115     }
2116    
2117     +static const struct evtchn_ops evtchn_ops_fifo = {
2118     + .max_channels = evtchn_fifo_max_channels,
2119     + .nr_channels = evtchn_fifo_nr_channels,
2120     + .setup = evtchn_fifo_setup,
2121     + .bind_to_cpu = evtchn_fifo_bind_to_cpu,
2122     + .clear_pending = evtchn_fifo_clear_pending,
2123     + .set_pending = evtchn_fifo_set_pending,
2124     + .is_pending = evtchn_fifo_is_pending,
2125     + .test_and_set_mask = evtchn_fifo_test_and_set_mask,
2126     + .mask = evtchn_fifo_mask,
2127     + .unmask = evtchn_fifo_unmask,
2128     + .handle_events = evtchn_fifo_handle_events,
2129     + .resume = evtchn_fifo_resume,
2130     + .percpu_init = evtchn_fifo_percpu_init,
2131     + .percpu_deinit = evtchn_fifo_percpu_deinit,
2132     +};
2133     +
2134     int __init xen_evtchn_fifo_init(void)
2135     {
2136     int cpu = get_cpu();
2137     @@ -444,9 +443,6 @@ int __init xen_evtchn_fifo_init(void)
2138    
2139     evtchn_ops = &evtchn_ops_fifo;
2140    
2141     - cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
2142     - "CPUHP_XEN_EVTCHN_PREPARE",
2143     - xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
2144     out:
2145     put_cpu();
2146     return ret;
2147     diff --git a/drivers/xen/events/events_internal.h b/drivers/xen/events/events_internal.h
2148     index 50c2050a1e320..b9b4f59198930 100644
2149     --- a/drivers/xen/events/events_internal.h
2150     +++ b/drivers/xen/events/events_internal.h
2151     @@ -32,11 +32,16 @@ enum xen_irq_type {
2152     */
2153     struct irq_info {
2154     struct list_head list;
2155     - int refcnt;
2156     + struct list_head eoi_list;
2157     + short refcnt;
2158     + short spurious_cnt;
2159     enum xen_irq_type type; /* type */
2160     unsigned irq;
2161     unsigned int evtchn; /* event channel */
2162     unsigned short cpu; /* cpu bound */
2163     + unsigned short eoi_cpu; /* EOI must happen on this cpu */
2164     + unsigned int irq_epoch; /* If eoi_cpu valid: irq_epoch of event */
2165     + u64 eoi_time; /* Time in jiffies when to EOI. */
2166    
2167     union {
2168     unsigned short virq;
2169     @@ -55,6 +60,8 @@ struct irq_info {
2170     #define PIRQ_SHAREABLE (1 << 1)
2171     #define PIRQ_MSI_GROUP (1 << 2)
2172    
2173     +struct evtchn_loop_ctrl;
2174     +
2175     struct evtchn_ops {
2176     unsigned (*max_channels)(void);
2177     unsigned (*nr_channels)(void);
2178     @@ -69,14 +76,18 @@ struct evtchn_ops {
2179     void (*mask)(unsigned port);
2180     void (*unmask)(unsigned port);
2181    
2182     - void (*handle_events)(unsigned cpu);
2183     + void (*handle_events)(unsigned cpu, struct evtchn_loop_ctrl *ctrl);
2184     void (*resume)(void);
2185     +
2186     + int (*percpu_init)(unsigned int cpu);
2187     + int (*percpu_deinit)(unsigned int cpu);
2188     };
2189    
2190     extern const struct evtchn_ops *evtchn_ops;
2191    
2192     extern int **evtchn_to_irq;
2193     int get_evtchn_to_irq(unsigned int evtchn);
2194     +void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl);
2195    
2196     struct irq_info *info_for_irq(unsigned irq);
2197     unsigned cpu_from_irq(unsigned irq);
2198     @@ -134,9 +145,10 @@ static inline void unmask_evtchn(unsigned port)
2199     return evtchn_ops->unmask(port);
2200     }
2201    
2202     -static inline void xen_evtchn_handle_events(unsigned cpu)
2203     +static inline void xen_evtchn_handle_events(unsigned cpu,
2204     + struct evtchn_loop_ctrl *ctrl)
2205     {
2206     - return evtchn_ops->handle_events(cpu);
2207     + return evtchn_ops->handle_events(cpu, ctrl);
2208     }
2209    
2210     static inline void xen_evtchn_resume(void)
2211     diff --git a/drivers/xen/evtchn.c b/drivers/xen/evtchn.c
2212     index e8c7f09d01be8..bcf0b1e60f2c3 100644
2213     --- a/drivers/xen/evtchn.c
2214     +++ b/drivers/xen/evtchn.c
2215     @@ -178,7 +178,6 @@ static irqreturn_t evtchn_interrupt(int irq, void *data)
2216     "Interrupt for port %d, but apparently not enabled; per-user %p\n",
2217     evtchn->port, u);
2218    
2219     - disable_irq_nosync(irq);
2220     evtchn->enabled = false;
2221    
2222     spin_lock(&u->ring_prod_lock);
2223     @@ -304,7 +303,7 @@ static ssize_t evtchn_write(struct file *file, const char __user *buf,
2224     evtchn = find_evtchn(u, port);
2225     if (evtchn && !evtchn->enabled) {
2226     evtchn->enabled = true;
2227     - enable_irq(irq_from_evtchn(port));
2228     + xen_irq_lateeoi(irq_from_evtchn(port), 0);
2229     }
2230     }
2231    
2232     @@ -404,8 +403,8 @@ static int evtchn_bind_to_user(struct per_user_data *u, int port)
2233     if (rc < 0)
2234     goto err;
2235    
2236     - rc = bind_evtchn_to_irqhandler(port, evtchn_interrupt, 0,
2237     - u->name, evtchn);
2238     + rc = bind_evtchn_to_irqhandler_lateeoi(port, evtchn_interrupt, 0,
2239     + u->name, evtchn);
2240     if (rc < 0)
2241     goto err;
2242    
2243     diff --git a/drivers/xen/xen-pciback/pci_stub.c b/drivers/xen/xen-pciback/pci_stub.c
2244     index ee5ce9286d613..83d798d12400e 100644
2245     --- a/drivers/xen/xen-pciback/pci_stub.c
2246     +++ b/drivers/xen/xen-pciback/pci_stub.c
2247     @@ -733,10 +733,17 @@ static pci_ers_result_t common_process(struct pcistub_device *psdev,
2248     wmb();
2249     notify_remote_via_irq(pdev->evtchn_irq);
2250    
2251     + /* Enable IRQ to signal "request done". */
2252     + xen_pcibk_lateeoi(pdev, 0);
2253     +
2254     ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
2255     !(test_bit(_XEN_PCIB_active, (unsigned long *)
2256     &sh_info->flags)), 300*HZ);
2257    
2258     + /* Enable IRQ for pcifront request if not already active. */
2259     + if (!test_bit(_PDEVF_op_active, &pdev->flags))
2260     + xen_pcibk_lateeoi(pdev, 0);
2261     +
2262     if (!ret) {
2263     if (test_bit(_XEN_PCIB_active,
2264     (unsigned long *)&sh_info->flags)) {
2265     @@ -750,13 +757,6 @@ static pci_ers_result_t common_process(struct pcistub_device *psdev,
2266     }
2267     clear_bit(_PCIB_op_pending, (unsigned long *)&pdev->flags);
2268    
2269     - if (test_bit(_XEN_PCIF_active,
2270     - (unsigned long *)&sh_info->flags)) {
2271     - dev_dbg(&psdev->dev->dev,
2272     - "schedule pci_conf service in " DRV_NAME "\n");
2273     - xen_pcibk_test_and_schedule_op(psdev->pdev);
2274     - }
2275     -
2276     res = (pci_ers_result_t)aer_op->err;
2277     return res;
2278     }
2279     diff --git a/drivers/xen/xen-pciback/pciback.h b/drivers/xen/xen-pciback/pciback.h
2280     index 7af369b6aaa29..b97cf348cdc18 100644
2281     --- a/drivers/xen/xen-pciback/pciback.h
2282     +++ b/drivers/xen/xen-pciback/pciback.h
2283     @@ -13,6 +13,7 @@
2284     #include <linux/spinlock.h>
2285     #include <linux/workqueue.h>
2286     #include <linux/atomic.h>
2287     +#include <xen/events.h>
2288     #include <xen/interface/io/pciif.h>
2289    
2290     #define DRV_NAME "xen-pciback"
2291     @@ -26,6 +27,8 @@ struct pci_dev_entry {
2292     #define PDEVF_op_active (1<<(_PDEVF_op_active))
2293     #define _PCIB_op_pending (1)
2294     #define PCIB_op_pending (1<<(_PCIB_op_pending))
2295     +#define _EOI_pending (2)
2296     +#define EOI_pending (1<<(_EOI_pending))
2297    
2298     struct xen_pcibk_device {
2299     void *pci_dev_data;
2300     @@ -181,12 +184,17 @@ static inline void xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
2301     irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id);
2302     void xen_pcibk_do_op(struct work_struct *data);
2303    
2304     +static inline void xen_pcibk_lateeoi(struct xen_pcibk_device *pdev,
2305     + unsigned int eoi_flag)
2306     +{
2307     + if (test_and_clear_bit(_EOI_pending, &pdev->flags))
2308     + xen_irq_lateeoi(pdev->evtchn_irq, eoi_flag);
2309     +}
2310     +
2311     int xen_pcibk_xenbus_register(void);
2312     void xen_pcibk_xenbus_unregister(void);
2313    
2314     extern int verbose_request;
2315     -
2316     -void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev);
2317     #endif
2318    
2319     /* Handles shared IRQs that can to device domain and control domain. */
2320     diff --git a/drivers/xen/xen-pciback/pciback_ops.c b/drivers/xen/xen-pciback/pciback_ops.c
2321     index e7fbed56c0446..eb5dd80530e74 100644
2322     --- a/drivers/xen/xen-pciback/pciback_ops.c
2323     +++ b/drivers/xen/xen-pciback/pciback_ops.c
2324     @@ -296,26 +296,41 @@ int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
2325     return 0;
2326     }
2327     #endif
2328     +
2329     +static inline bool xen_pcibk_test_op_pending(struct xen_pcibk_device *pdev)
2330     +{
2331     + return test_bit(_XEN_PCIF_active,
2332     + (unsigned long *)&pdev->sh_info->flags) &&
2333     + !test_and_set_bit(_PDEVF_op_active, &pdev->flags);
2334     +}
2335     +
2336     /*
2337     * Now the same evtchn is used for both pcifront conf_read_write request
2338     * as well as pcie aer front end ack. We use a new work_queue to schedule
2339     * xen_pcibk conf_read_write service for avoiding confict with aer_core
2340     * do_recovery job which also use the system default work_queue
2341     */
2342     -void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
2343     +static void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
2344     {
2345     + bool eoi = true;
2346     +
2347     /* Check that frontend is requesting an operation and that we are not
2348     * already processing a request */
2349     - if (test_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags)
2350     - && !test_and_set_bit(_PDEVF_op_active, &pdev->flags)) {
2351     + if (xen_pcibk_test_op_pending(pdev)) {
2352     schedule_work(&pdev->op_work);
2353     + eoi = false;
2354     }
2355     /*_XEN_PCIB_active should have been cleared by pcifront. And also make
2356     sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
2357     if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
2358     && test_bit(_PCIB_op_pending, &pdev->flags)) {
2359     wake_up(&xen_pcibk_aer_wait_queue);
2360     + eoi = false;
2361     }
2362     +
2363     + /* EOI if there was nothing to do. */
2364     + if (eoi)
2365     + xen_pcibk_lateeoi(pdev, XEN_EOI_FLAG_SPURIOUS);
2366     }
2367    
2368     /* Performing the configuration space reads/writes must not be done in atomic
2369     @@ -323,10 +338,8 @@ void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
2370     * use of semaphores). This function is intended to be called from a work
2371     * queue in process context taking a struct xen_pcibk_device as a parameter */
2372    
2373     -void xen_pcibk_do_op(struct work_struct *data)
2374     +static void xen_pcibk_do_one_op(struct xen_pcibk_device *pdev)
2375     {
2376     - struct xen_pcibk_device *pdev =
2377     - container_of(data, struct xen_pcibk_device, op_work);
2378     struct pci_dev *dev;
2379     struct xen_pcibk_dev_data *dev_data = NULL;
2380     struct xen_pci_op *op = &pdev->op;
2381     @@ -399,16 +412,31 @@ void xen_pcibk_do_op(struct work_struct *data)
2382     smp_mb__before_atomic(); /* /after/ clearing PCIF_active */
2383     clear_bit(_PDEVF_op_active, &pdev->flags);
2384     smp_mb__after_atomic(); /* /before/ final check for work */
2385     +}
2386    
2387     - /* Check to see if the driver domain tried to start another request in
2388     - * between clearing _XEN_PCIF_active and clearing _PDEVF_op_active.
2389     - */
2390     - xen_pcibk_test_and_schedule_op(pdev);
2391     +void xen_pcibk_do_op(struct work_struct *data)
2392     +{
2393     + struct xen_pcibk_device *pdev =
2394     + container_of(data, struct xen_pcibk_device, op_work);
2395     +
2396     + do {
2397     + xen_pcibk_do_one_op(pdev);
2398     + } while (xen_pcibk_test_op_pending(pdev));
2399     +
2400     + xen_pcibk_lateeoi(pdev, 0);
2401     }
2402    
2403     irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
2404     {
2405     struct xen_pcibk_device *pdev = dev_id;
2406     + bool eoi;
2407     +
2408     + /* IRQs might come in before pdev->evtchn_irq is written. */
2409     + if (unlikely(pdev->evtchn_irq != irq))
2410     + pdev->evtchn_irq = irq;
2411     +
2412     + eoi = test_and_set_bit(_EOI_pending, &pdev->flags);
2413     + WARN(eoi, "IRQ while EOI pending\n");
2414    
2415     xen_pcibk_test_and_schedule_op(pdev);
2416    
2417     diff --git a/drivers/xen/xen-pciback/xenbus.c b/drivers/xen/xen-pciback/xenbus.c
2418     index 5ce878c51d034..f33eb40cb4148 100644
2419     --- a/drivers/xen/xen-pciback/xenbus.c
2420     +++ b/drivers/xen/xen-pciback/xenbus.c
2421     @@ -122,7 +122,7 @@ static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
2422    
2423     pdev->sh_info = vaddr;
2424    
2425     - err = bind_interdomain_evtchn_to_irqhandler(
2426     + err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
2427     pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
2428     0, DRV_NAME, pdev);
2429     if (err < 0) {
2430     diff --git a/drivers/xen/xen-scsiback.c b/drivers/xen/xen-scsiback.c
2431     index 992cb8fa272c2..3243d917651a3 100644
2432     --- a/drivers/xen/xen-scsiback.c
2433     +++ b/drivers/xen/xen-scsiback.c
2434     @@ -91,7 +91,6 @@ struct vscsibk_info {
2435     unsigned int irq;
2436    
2437     struct vscsiif_back_ring ring;
2438     - int ring_error;
2439    
2440     spinlock_t ring_lock;
2441     atomic_t nr_unreplied_reqs;
2442     @@ -723,7 +722,8 @@ static struct vscsibk_pend *prepare_pending_reqs(struct vscsibk_info *info,
2443     return pending_req;
2444     }
2445    
2446     -static int scsiback_do_cmd_fn(struct vscsibk_info *info)
2447     +static int scsiback_do_cmd_fn(struct vscsibk_info *info,
2448     + unsigned int *eoi_flags)
2449     {
2450     struct vscsiif_back_ring *ring = &info->ring;
2451     struct vscsiif_request ring_req;
2452     @@ -740,11 +740,12 @@ static int scsiback_do_cmd_fn(struct vscsibk_info *info)
2453     rc = ring->rsp_prod_pvt;
2454     pr_warn("Dom%d provided bogus ring requests (%#x - %#x = %u). Halting ring processing\n",
2455     info->domid, rp, rc, rp - rc);
2456     - info->ring_error = 1;
2457     - return 0;
2458     + return -EINVAL;
2459     }
2460    
2461     while ((rc != rp)) {
2462     + *eoi_flags &= ~XEN_EOI_FLAG_SPURIOUS;
2463     +
2464     if (RING_REQUEST_CONS_OVERFLOW(ring, rc))
2465     break;
2466    
2467     @@ -803,13 +804,16 @@ static int scsiback_do_cmd_fn(struct vscsibk_info *info)
2468     static irqreturn_t scsiback_irq_fn(int irq, void *dev_id)
2469     {
2470     struct vscsibk_info *info = dev_id;
2471     + int rc;
2472     + unsigned int eoi_flags = XEN_EOI_FLAG_SPURIOUS;
2473    
2474     - if (info->ring_error)
2475     - return IRQ_HANDLED;
2476     -
2477     - while (scsiback_do_cmd_fn(info))
2478     + while ((rc = scsiback_do_cmd_fn(info, &eoi_flags)) > 0)
2479     cond_resched();
2480    
2481     + /* In case of a ring error we keep the event channel masked. */
2482     + if (!rc)
2483     + xen_irq_lateeoi(irq, eoi_flags);
2484     +
2485     return IRQ_HANDLED;
2486     }
2487    
2488     @@ -830,7 +834,7 @@ static int scsiback_init_sring(struct vscsibk_info *info, grant_ref_t ring_ref,
2489     sring = (struct vscsiif_sring *)area;
2490     BACK_RING_INIT(&info->ring, sring, PAGE_SIZE);
2491    
2492     - err = bind_interdomain_evtchn_to_irq(info->domid, evtchn);
2493     + err = bind_interdomain_evtchn_to_irq_lateeoi(info->domid, evtchn);
2494     if (err < 0)
2495     goto unmap_page;
2496    
2497     @@ -1253,7 +1257,6 @@ static int scsiback_probe(struct xenbus_device *dev,
2498    
2499     info->domid = dev->otherend_id;
2500     spin_lock_init(&info->ring_lock);
2501     - info->ring_error = 0;
2502     atomic_set(&info->nr_unreplied_reqs, 0);
2503     init_waitqueue_head(&info->waiting_to_free);
2504     info->dev = dev;
2505     diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
2506     index d6c827a9ebc56..5c2f4f58da8ff 100644
2507     --- a/fs/btrfs/extent_io.c
2508     +++ b/fs/btrfs/extent_io.c
2509     @@ -3879,6 +3879,10 @@ retry:
2510     if (!ret) {
2511     free_extent_buffer(eb);
2512     continue;
2513     + } else if (ret < 0) {
2514     + done = 1;
2515     + free_extent_buffer(eb);
2516     + break;
2517     }
2518    
2519     ret = write_one_eb(eb, fs_info, wbc, &epd);
2520     diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
2521     index 981091bd6c3c4..ebca009030c3a 100644
2522     --- a/fs/btrfs/ioctl.c
2523     +++ b/fs/btrfs/ioctl.c
2524     @@ -3854,6 +3854,8 @@ process_slot:
2525     ret = -EINTR;
2526     goto out;
2527     }
2528     +
2529     + cond_resched();
2530     }
2531     ret = 0;
2532    
2533     diff --git a/fs/cifs/cifs_unicode.c b/fs/cifs/cifs_unicode.c
2534     index 211ac472cb9dc..942874257a092 100644
2535     --- a/fs/cifs/cifs_unicode.c
2536     +++ b/fs/cifs/cifs_unicode.c
2537     @@ -493,7 +493,13 @@ cifsConvertToUTF16(__le16 *target, const char *source, int srclen,
2538     else if (map_chars == SFM_MAP_UNI_RSVD) {
2539     bool end_of_string;
2540    
2541     - if (i == srclen - 1)
2542     + /**
2543     + * Remap spaces and periods found at the end of every
2544     + * component of the path. The special cases of '.' and
2545     + * '..' do not need to be dealt with explicitly because
2546     + * they are addressed in namei.c:link_path_walk().
2547     + **/
2548     + if ((i == srclen - 1) || (source[i+1] == '\\'))
2549     end_of_string = true;
2550     else
2551     end_of_string = false;
2552     diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
2553     index 4df4d31057b36..5bf62bdd84b87 100644
2554     --- a/fs/ext4/inline.c
2555     +++ b/fs/ext4/inline.c
2556     @@ -1890,6 +1890,7 @@ void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
2557    
2558     ext4_write_lock_xattr(inode, &no_expand);
2559     if (!ext4_has_inline_data(inode)) {
2560     + ext4_write_unlock_xattr(inode, &no_expand);
2561     *has_inline = 0;
2562     ext4_journal_stop(handle);
2563     return;
2564     diff --git a/fs/ext4/super.c b/fs/ext4/super.c
2565     index 2527eb3049494..84d28d3efc602 100644
2566     --- a/fs/ext4/super.c
2567     +++ b/fs/ext4/super.c
2568     @@ -1571,8 +1571,8 @@ static const struct mount_opts {
2569     {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
2570     EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
2571     MOPT_CLEAR | MOPT_Q},
2572     - {Opt_usrjquota, 0, MOPT_Q},
2573     - {Opt_grpjquota, 0, MOPT_Q},
2574     + {Opt_usrjquota, 0, MOPT_Q | MOPT_STRING},
2575     + {Opt_grpjquota, 0, MOPT_Q | MOPT_STRING},
2576     {Opt_offusrjquota, 0, MOPT_Q},
2577     {Opt_offgrpjquota, 0, MOPT_Q},
2578     {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
2579     @@ -4336,6 +4336,7 @@ cantfind_ext4:
2580     #ifdef CONFIG_QUOTA
2581     failed_mount8:
2582     ext4_unregister_sysfs(sb);
2583     + kobject_put(&sbi->s_kobj);
2584     #endif
2585     failed_mount7:
2586     ext4_unregister_li_request(sb);
2587     diff --git a/fs/gfs2/glock.c b/fs/gfs2/glock.c
2588     index efd44d5645d83..f19e49a5d032b 100644
2589     --- a/fs/gfs2/glock.c
2590     +++ b/fs/gfs2/glock.c
2591     @@ -758,7 +758,8 @@ again:
2592     }
2593     kfree(gl->gl_lksb.sb_lvbptr);
2594     kmem_cache_free(cachep, gl);
2595     - atomic_dec(&sdp->sd_glock_disposal);
2596     + if (atomic_dec_and_test(&sdp->sd_glock_disposal))
2597     + wake_up(&sdp->sd_glock_wait);
2598     *glp = tmp;
2599    
2600     return ret;
2601     diff --git a/fs/gfs2/rgrp.c b/fs/gfs2/rgrp.c
2602     index 0a80f66365492..9621badb95995 100644
2603     --- a/fs/gfs2/rgrp.c
2604     +++ b/fs/gfs2/rgrp.c
2605     @@ -730,9 +730,9 @@ void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
2606     }
2607    
2608     gfs2_free_clones(rgd);
2609     + return_all_reservations(rgd);
2610     kfree(rgd->rd_bits);
2611     rgd->rd_bits = NULL;
2612     - return_all_reservations(rgd);
2613     kmem_cache_free(gfs2_rgrpd_cachep, rgd);
2614     }
2615     }
2616     @@ -1371,6 +1371,9 @@ int gfs2_fitrim(struct file *filp, void __user *argp)
2617     if (!capable(CAP_SYS_ADMIN))
2618     return -EPERM;
2619    
2620     + if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
2621     + return -EROFS;
2622     +
2623     if (!blk_queue_discard(q))
2624     return -EOPNOTSUPP;
2625    
2626     diff --git a/fs/ocfs2/super.c b/fs/ocfs2/super.c
2627     index 4d6e99ea37b8e..e0fb62f5cf63d 100644
2628     --- a/fs/ocfs2/super.c
2629     +++ b/fs/ocfs2/super.c
2630     @@ -1733,6 +1733,7 @@ static void ocfs2_inode_init_once(void *data)
2631    
2632     oi->ip_blkno = 0ULL;
2633     oi->ip_clusters = 0;
2634     + oi->ip_next_orphan = NULL;
2635    
2636     ocfs2_resv_init_once(&oi->ip_la_data_resv);
2637    
2638     diff --git a/fs/xfs/libxfs/xfs_rmap.c b/fs/xfs/libxfs/xfs_rmap.c
2639     index 3a8cc7139912b..89fdcc641715f 100644
2640     --- a/fs/xfs/libxfs/xfs_rmap.c
2641     +++ b/fs/xfs/libxfs/xfs_rmap.c
2642     @@ -1318,7 +1318,7 @@ xfs_rmap_convert_shared(
2643     * record for our insertion point. This will also give us the record for
2644     * start block contiguity tests.
2645     */
2646     - error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, flags,
2647     + error = xfs_rmap_lookup_le_range(cur, bno, owner, offset, oldext,
2648     &PREV, &i);
2649     XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
2650    
2651     diff --git a/fs/xfs/libxfs/xfs_rmap_btree.c b/fs/xfs/libxfs/xfs_rmap_btree.c
2652     index 33a28efc3085b..c5a24b80c7f72 100644
2653     --- a/fs/xfs/libxfs/xfs_rmap_btree.c
2654     +++ b/fs/xfs/libxfs/xfs_rmap_btree.c
2655     @@ -262,8 +262,8 @@ xfs_rmapbt_key_diff(
2656     else if (y > x)
2657     return -1;
2658    
2659     - x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
2660     - y = rec->rm_offset;
2661     + x = be64_to_cpu(kp->rm_offset);
2662     + y = xfs_rmap_irec_offset_pack(rec);
2663     if (x > y)
2664     return 1;
2665     else if (y > x)
2666     @@ -294,8 +294,8 @@ xfs_rmapbt_diff_two_keys(
2667     else if (y > x)
2668     return -1;
2669    
2670     - x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
2671     - y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
2672     + x = be64_to_cpu(kp1->rm_offset);
2673     + y = be64_to_cpu(kp2->rm_offset);
2674     if (x > y)
2675     return 1;
2676     else if (y > x)
2677     @@ -401,8 +401,8 @@ xfs_rmapbt_keys_inorder(
2678     return 1;
2679     else if (a > b)
2680     return 0;
2681     - a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
2682     - b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
2683     + a = be64_to_cpu(k1->rmap.rm_offset);
2684     + b = be64_to_cpu(k2->rmap.rm_offset);
2685     if (a <= b)
2686     return 1;
2687     return 0;
2688     @@ -431,8 +431,8 @@ xfs_rmapbt_recs_inorder(
2689     return 1;
2690     else if (a > b)
2691     return 0;
2692     - a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
2693     - b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
2694     + a = be64_to_cpu(r1->rmap.rm_offset);
2695     + b = be64_to_cpu(r2->rmap.rm_offset);
2696     if (a <= b)
2697     return 1;
2698     return 0;
2699     diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c
2700     index 7bfddcd32d73e..0d587657056d8 100644
2701     --- a/fs/xfs/xfs_iops.c
2702     +++ b/fs/xfs/xfs_iops.c
2703     @@ -864,6 +864,16 @@ xfs_setattr_size(
2704     if (newsize > oldsize) {
2705     error = xfs_zero_eof(ip, newsize, oldsize, &did_zeroing);
2706     } else {
2707     + /*
2708     + * iomap won't detect a dirty page over an unwritten block (or a
2709     + * cow block over a hole) and subsequently skips zeroing the
2710     + * newly post-EOF portion of the page. Flush the new EOF to
2711     + * convert the block before the pagecache truncate.
2712     + */
2713     + error = filemap_write_and_wait_range(inode->i_mapping, newsize,
2714     + newsize);
2715     + if (error)
2716     + return error;
2717     error = iomap_truncate_page(inode, newsize, &did_zeroing,
2718     &xfs_iomap_ops);
2719     }
2720     diff --git a/fs/xfs/xfs_pnfs.c b/fs/xfs/xfs_pnfs.c
2721     index cecd37569ddb3..353bfe9c5cdd9 100644
2722     --- a/fs/xfs/xfs_pnfs.c
2723     +++ b/fs/xfs/xfs_pnfs.c
2724     @@ -144,7 +144,7 @@ xfs_fs_map_blocks(
2725     goto out_unlock;
2726     error = invalidate_inode_pages2(inode->i_mapping);
2727     if (WARN_ON_ONCE(error))
2728     - return error;
2729     + goto out_unlock;
2730    
2731     end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + length);
2732     offset_fsb = XFS_B_TO_FSBT(mp, offset);
2733     diff --git a/include/linux/can/skb.h b/include/linux/can/skb.h
2734     index 51bb6532785c3..1a2111c775ae1 100644
2735     --- a/include/linux/can/skb.h
2736     +++ b/include/linux/can/skb.h
2737     @@ -60,21 +60,17 @@ static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk)
2738     */
2739     static inline struct sk_buff *can_create_echo_skb(struct sk_buff *skb)
2740     {
2741     - if (skb_shared(skb)) {
2742     - struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2743     + struct sk_buff *nskb;
2744    
2745     - if (likely(nskb)) {
2746     - can_skb_set_owner(nskb, skb->sk);
2747     - consume_skb(skb);
2748     - return nskb;
2749     - } else {
2750     - kfree_skb(skb);
2751     - return NULL;
2752     - }
2753     + nskb = skb_clone(skb, GFP_ATOMIC);
2754     + if (unlikely(!nskb)) {
2755     + kfree_skb(skb);
2756     + return NULL;
2757     }
2758    
2759     - /* we can assume to have an unshared skb with proper owner */
2760     - return skb;
2761     + can_skb_set_owner(nskb, skb->sk);
2762     + consume_skb(skb);
2763     + return nskb;
2764     }
2765    
2766     #endif /* !_CAN_SKB_H */
2767     diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
2768     index ae8ecf8210192..a7057b7726129 100644
2769     --- a/include/linux/perf_event.h
2770     +++ b/include/linux/perf_event.h
2771     @@ -475,7 +475,7 @@ struct pmu {
2772     */
2773     struct perf_addr_filter {
2774     struct list_head entry;
2775     - struct inode *inode;
2776     + struct path path;
2777     unsigned long offset;
2778     unsigned long size;
2779     unsigned int range : 1,
2780     diff --git a/include/linux/prandom.h b/include/linux/prandom.h
2781     index aa16e6468f91e..cc1e71334e53c 100644
2782     --- a/include/linux/prandom.h
2783     +++ b/include/linux/prandom.h
2784     @@ -16,12 +16,44 @@ void prandom_bytes(void *buf, size_t nbytes);
2785     void prandom_seed(u32 seed);
2786     void prandom_reseed_late(void);
2787    
2788     +#if BITS_PER_LONG == 64
2789     +/*
2790     + * The core SipHash round function. Each line can be executed in
2791     + * parallel given enough CPU resources.
2792     + */
2793     +#define PRND_SIPROUND(v0, v1, v2, v3) ( \
2794     + v0 += v1, v1 = rol64(v1, 13), v2 += v3, v3 = rol64(v3, 16), \
2795     + v1 ^= v0, v0 = rol64(v0, 32), v3 ^= v2, \
2796     + v0 += v3, v3 = rol64(v3, 21), v2 += v1, v1 = rol64(v1, 17), \
2797     + v3 ^= v0, v1 ^= v2, v2 = rol64(v2, 32) \
2798     +)
2799     +
2800     +#define PRND_K0 (0x736f6d6570736575 ^ 0x6c7967656e657261)
2801     +#define PRND_K1 (0x646f72616e646f6d ^ 0x7465646279746573)
2802     +
2803     +#elif BITS_PER_LONG == 32
2804     +/*
2805     + * On 32-bit machines, we use HSipHash, a reduced-width version of SipHash.
2806     + * This is weaker, but 32-bit machines are not used for high-traffic
2807     + * applications, so there is less output for an attacker to analyze.
2808     + */
2809     +#define PRND_SIPROUND(v0, v1, v2, v3) ( \
2810     + v0 += v1, v1 = rol32(v1, 5), v2 += v3, v3 = rol32(v3, 8), \
2811     + v1 ^= v0, v0 = rol32(v0, 16), v3 ^= v2, \
2812     + v0 += v3, v3 = rol32(v3, 7), v2 += v1, v1 = rol32(v1, 13), \
2813     + v3 ^= v0, v1 ^= v2, v2 = rol32(v2, 16) \
2814     +)
2815     +#define PRND_K0 0x6c796765
2816     +#define PRND_K1 0x74656462
2817     +
2818     +#else
2819     +#error Unsupported BITS_PER_LONG
2820     +#endif
2821     +
2822     struct rnd_state {
2823     __u32 s1, s2, s3, s4;
2824     };
2825    
2826     -DECLARE_PER_CPU(struct rnd_state, net_rand_state);
2827     -
2828     u32 prandom_u32_state(struct rnd_state *state);
2829     void prandom_bytes_state(struct rnd_state *state, void *buf, size_t nbytes);
2830     void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state);
2831     diff --git a/include/linux/time64.h b/include/linux/time64.h
2832     index 980c71b3001a5..2a45b8c87edbf 100644
2833     --- a/include/linux/time64.h
2834     +++ b/include/linux/time64.h
2835     @@ -188,6 +188,10 @@ static inline bool timespec64_valid_strict(const struct timespec64 *ts)
2836     */
2837     static inline s64 timespec64_to_ns(const struct timespec64 *ts)
2838     {
2839     + /* Prevent multiplication overflow */
2840     + if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
2841     + return KTIME_MAX;
2842     +
2843     return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
2844     }
2845    
2846     diff --git a/include/xen/events.h b/include/xen/events.h
2847     index 88da2abaf5359..ad0c61cf399be 100644
2848     --- a/include/xen/events.h
2849     +++ b/include/xen/events.h
2850     @@ -12,11 +12,16 @@
2851    
2852     unsigned xen_evtchn_nr_channels(void);
2853    
2854     -int bind_evtchn_to_irq(unsigned int evtchn);
2855     -int bind_evtchn_to_irqhandler(unsigned int evtchn,
2856     +int bind_evtchn_to_irq(evtchn_port_t evtchn);
2857     +int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn);
2858     +int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
2859     irq_handler_t handler,
2860     unsigned long irqflags, const char *devname,
2861     void *dev_id);
2862     +int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
2863     + irq_handler_t handler,
2864     + unsigned long irqflags, const char *devname,
2865     + void *dev_id);
2866     int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu);
2867     int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
2868     irq_handler_t handler,
2869     @@ -29,13 +34,21 @@ int bind_ipi_to_irqhandler(enum ipi_vector ipi,
2870     const char *devname,
2871     void *dev_id);
2872     int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
2873     - unsigned int remote_port);
2874     + evtchn_port_t remote_port);
2875     +int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
2876     + evtchn_port_t remote_port);
2877     int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
2878     - unsigned int remote_port,
2879     + evtchn_port_t remote_port,
2880     irq_handler_t handler,
2881     unsigned long irqflags,
2882     const char *devname,
2883     void *dev_id);
2884     +int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
2885     + evtchn_port_t remote_port,
2886     + irq_handler_t handler,
2887     + unsigned long irqflags,
2888     + const char *devname,
2889     + void *dev_id);
2890    
2891     /*
2892     * Common unbind function for all event sources. Takes IRQ to unbind from.
2893     @@ -44,6 +57,14 @@ int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
2894     */
2895     void unbind_from_irqhandler(unsigned int irq, void *dev_id);
2896    
2897     +/*
2898     + * Send late EOI for an IRQ bound to an event channel via one of the *_lateeoi
2899     + * functions above.
2900     + */
2901     +void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags);
2902     +/* Signal an event was spurious, i.e. there was no action resulting from it. */
2903     +#define XEN_EOI_FLAG_SPURIOUS 0x00000001
2904     +
2905     #define XEN_IRQ_PRIORITY_MAX EVTCHN_FIFO_PRIORITY_MAX
2906     #define XEN_IRQ_PRIORITY_DEFAULT EVTCHN_FIFO_PRIORITY_DEFAULT
2907     #define XEN_IRQ_PRIORITY_MIN EVTCHN_FIFO_PRIORITY_MIN
2908     diff --git a/kernel/events/core.c b/kernel/events/core.c
2909     index 7aad4d22b4223..9d4a71deac992 100644
2910     --- a/kernel/events/core.c
2911     +++ b/kernel/events/core.c
2912     @@ -5069,11 +5069,11 @@ static void perf_pmu_output_stop(struct perf_event *event);
2913     static void perf_mmap_close(struct vm_area_struct *vma)
2914     {
2915     struct perf_event *event = vma->vm_file->private_data;
2916     -
2917     struct ring_buffer *rb = ring_buffer_get(event);
2918     struct user_struct *mmap_user = rb->mmap_user;
2919     int mmap_locked = rb->mmap_locked;
2920     unsigned long size = perf_data_size(rb);
2921     + bool detach_rest = false;
2922    
2923     if (event->pmu->event_unmapped)
2924     event->pmu->event_unmapped(event);
2925     @@ -5104,7 +5104,8 @@ static void perf_mmap_close(struct vm_area_struct *vma)
2926     mutex_unlock(&event->mmap_mutex);
2927     }
2928    
2929     - atomic_dec(&rb->mmap_count);
2930     + if (atomic_dec_and_test(&rb->mmap_count))
2931     + detach_rest = true;
2932    
2933     if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
2934     goto out_put;
2935     @@ -5113,7 +5114,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)
2936     mutex_unlock(&event->mmap_mutex);
2937    
2938     /* If there's still other mmap()s of this buffer, we're done. */
2939     - if (atomic_read(&rb->mmap_count))
2940     + if (!detach_rest)
2941     goto out_put;
2942    
2943     /*
2944     @@ -6271,7 +6272,7 @@ static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
2945    
2946     raw_spin_lock_irqsave(&ifh->lock, flags);
2947     list_for_each_entry(filter, &ifh->list, entry) {
2948     - if (filter->inode) {
2949     + if (filter->path.dentry) {
2950     event->addr_filters_offs[count] = 0;
2951     restart++;
2952     }
2953     @@ -6814,7 +6815,11 @@ static bool perf_addr_filter_match(struct perf_addr_filter *filter,
2954     struct file *file, unsigned long offset,
2955     unsigned long size)
2956     {
2957     - if (filter->inode != file->f_inode)
2958     + /* d_inode(NULL) won't be equal to any mapped user-space file */
2959     + if (!filter->path.dentry)
2960     + return false;
2961     +
2962     + if (d_inode(filter->path.dentry) != file_inode(file))
2963     return false;
2964    
2965     if (filter->offset > offset + size)
2966     @@ -8028,8 +8033,7 @@ static void free_filters_list(struct list_head *filters)
2967     struct perf_addr_filter *filter, *iter;
2968    
2969     list_for_each_entry_safe(filter, iter, filters, entry) {
2970     - if (filter->inode)
2971     - iput(filter->inode);
2972     + path_put(&filter->path);
2973     list_del(&filter->entry);
2974     kfree(filter);
2975     }
2976     @@ -8123,7 +8127,7 @@ static void perf_event_addr_filters_apply(struct perf_event *event)
2977     * Adjust base offset if the filter is associated to a binary
2978     * that needs to be mapped:
2979     */
2980     - if (filter->inode)
2981     + if (filter->path.dentry)
2982     event->addr_filters_offs[count] =
2983     perf_addr_filter_apply(filter, mm);
2984    
2985     @@ -8196,7 +8200,6 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
2986     {
2987     struct perf_addr_filter *filter = NULL;
2988     char *start, *orig, *filename = NULL;
2989     - struct path path;
2990     substring_t args[MAX_OPT_ARGS];
2991     int state = IF_STATE_ACTION, token;
2992     unsigned int kernel = 0;
2993     @@ -8259,6 +8262,7 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
2994     if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
2995     int fpos = filter->range ? 2 : 1;
2996    
2997     + kfree(filename);
2998     filename = match_strdup(&args[fpos]);
2999     if (!filename) {
3000     ret = -ENOMEM;
3001     @@ -8287,19 +8291,15 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
3002     goto fail;
3003    
3004     /* look up the path and grab its inode */
3005     - ret = kern_path(filename, LOOKUP_FOLLOW, &path);
3006     + ret = kern_path(filename, LOOKUP_FOLLOW,
3007     + &filter->path);
3008     if (ret)
3009     - goto fail_free_name;
3010     -
3011     - filter->inode = igrab(d_inode(path.dentry));
3012     - path_put(&path);
3013     - kfree(filename);
3014     - filename = NULL;
3015     + goto fail;
3016    
3017     ret = -EINVAL;
3018     - if (!filter->inode ||
3019     - !S_ISREG(filter->inode->i_mode))
3020     - /* free_filters_list() will iput() */
3021     + if (!filter->path.dentry ||
3022     + !S_ISREG(d_inode(filter->path.dentry)
3023     + ->i_mode))
3024     goto fail;
3025     }
3026    
3027     @@ -8312,13 +8312,13 @@ perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
3028     if (state != IF_STATE_ACTION)
3029     goto fail;
3030    
3031     + kfree(filename);
3032     kfree(orig);
3033    
3034     return 0;
3035    
3036     -fail_free_name:
3037     - kfree(filename);
3038     fail:
3039     + kfree(filename);
3040     free_filters_list(filters);
3041     kfree(orig);
3042    
3043     diff --git a/kernel/events/internal.h b/kernel/events/internal.h
3044     index 486fd78eb8d5e..c8c1c3db5d065 100644
3045     --- a/kernel/events/internal.h
3046     +++ b/kernel/events/internal.h
3047     @@ -212,7 +212,7 @@ static inline int get_recursion_context(int *recursion)
3048     rctx = 3;
3049     else if (in_irq())
3050     rctx = 2;
3051     - else if (in_softirq())
3052     + else if (in_serving_softirq())
3053     rctx = 1;
3054     else
3055     rctx = 0;
3056     diff --git a/kernel/exit.c b/kernel/exit.c
3057     index 27f4168eaeb1b..f9943ef23fa82 100644
3058     --- a/kernel/exit.c
3059     +++ b/kernel/exit.c
3060     @@ -483,7 +483,10 @@ static void exit_mm(struct task_struct *tsk)
3061     up_read(&mm->mmap_sem);
3062    
3063     self.task = tsk;
3064     - self.next = xchg(&core_state->dumper.next, &self);
3065     + if (self.task->flags & PF_SIGNALED)
3066     + self.next = xchg(&core_state->dumper.next, &self);
3067     + else
3068     + self.task = NULL;
3069     /*
3070     * Implies mb(), the result of xchg() must be visible
3071     * to core_state->dumper.
3072     diff --git a/kernel/irq/Kconfig b/kernel/irq/Kconfig
3073     index 3bbfd6a9c4756..bb3a46cbe034c 100644
3074     --- a/kernel/irq/Kconfig
3075     +++ b/kernel/irq/Kconfig
3076     @@ -67,6 +67,7 @@ config IRQ_DOMAIN_HIERARCHY
3077     # Generic IRQ IPI support
3078     config GENERIC_IRQ_IPI
3079     bool
3080     + select IRQ_DOMAIN_HIERARCHY
3081    
3082     # Generic MSI interrupt support
3083     config GENERIC_MSI_IRQ
3084     diff --git a/kernel/reboot.c b/kernel/reboot.c
3085     index bd30a973fe946..2946ed1d99d4d 100644
3086     --- a/kernel/reboot.c
3087     +++ b/kernel/reboot.c
3088     @@ -512,22 +512,22 @@ static int __init reboot_setup(char *str)
3089     break;
3090    
3091     case 's':
3092     - {
3093     - int rc;
3094     -
3095     - if (isdigit(*(str+1))) {
3096     - rc = kstrtoint(str+1, 0, &reboot_cpu);
3097     - if (rc)
3098     - return rc;
3099     - } else if (str[1] == 'm' && str[2] == 'p' &&
3100     - isdigit(*(str+3))) {
3101     - rc = kstrtoint(str+3, 0, &reboot_cpu);
3102     - if (rc)
3103     - return rc;
3104     - } else
3105     + if (isdigit(*(str+1)))
3106     + reboot_cpu = simple_strtoul(str+1, NULL, 0);
3107     + else if (str[1] == 'm' && str[2] == 'p' &&
3108     + isdigit(*(str+3)))
3109     + reboot_cpu = simple_strtoul(str+3, NULL, 0);
3110     + else
3111     reboot_mode = REBOOT_SOFT;
3112     + if (reboot_cpu >= num_possible_cpus()) {
3113     + pr_err("Ignoring the CPU number in reboot= option. "
3114     + "CPU %d exceeds possible cpu number %d\n",
3115     + reboot_cpu, num_possible_cpus());
3116     + reboot_cpu = 0;
3117     + break;
3118     + }
3119     break;
3120     - }
3121     +
3122     case 'g':
3123     reboot_mode = REBOOT_GPIO;
3124     break;
3125     diff --git a/kernel/time/timer.c b/kernel/time/timer.c
3126     index d2e4698d43fec..c9325a1e30d05 100644
3127     --- a/kernel/time/timer.c
3128     +++ b/kernel/time/timer.c
3129     @@ -1636,13 +1636,6 @@ void update_process_times(int user_tick)
3130     #endif
3131     scheduler_tick();
3132     run_posix_cpu_timers(p);
3133     -
3134     - /* The current CPU might make use of net randoms without receiving IRQs
3135     - * to renew them often enough. Let's update the net_rand_state from a
3136     - * non-constant value that's not affine to the number of calls to make
3137     - * sure it's updated when there's some activity (we don't care in idle).
3138     - */
3139     - this_cpu_add(net_rand_state.s1, rol32(jiffies, 24) + user_tick);
3140     }
3141    
3142     /**
3143     diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
3144     index fb2aa2430edcc..55f60d2edc3fb 100644
3145     --- a/kernel/trace/ring_buffer.c
3146     +++ b/kernel/trace/ring_buffer.c
3147     @@ -416,14 +416,16 @@ struct rb_event_info {
3148    
3149     /*
3150     * Used for which event context the event is in.
3151     - * NMI = 0
3152     - * IRQ = 1
3153     - * SOFTIRQ = 2
3154     - * NORMAL = 3
3155     + * TRANSITION = 0
3156     + * NMI = 1
3157     + * IRQ = 2
3158     + * SOFTIRQ = 3
3159     + * NORMAL = 4
3160     *
3161     * See trace_recursive_lock() comment below for more details.
3162     */
3163     enum {
3164     + RB_CTX_TRANSITION,
3165     RB_CTX_NMI,
3166     RB_CTX_IRQ,
3167     RB_CTX_SOFTIRQ,
3168     @@ -2579,10 +2581,10 @@ rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
3169     * a bit of overhead in something as critical as function tracing,
3170     * we use a bitmask trick.
3171     *
3172     - * bit 0 = NMI context
3173     - * bit 1 = IRQ context
3174     - * bit 2 = SoftIRQ context
3175     - * bit 3 = normal context.
3176     + * bit 1 = NMI context
3177     + * bit 2 = IRQ context
3178     + * bit 3 = SoftIRQ context
3179     + * bit 4 = normal context.
3180     *
3181     * This works because this is the order of contexts that can
3182     * preempt other contexts. A SoftIRQ never preempts an IRQ
3183     @@ -2605,6 +2607,30 @@ rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
3184     * The least significant bit can be cleared this way, and it
3185     * just so happens that it is the same bit corresponding to
3186     * the current context.
3187     + *
3188     + * Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
3189     + * is set when a recursion is detected at the current context, and if
3190     + * the TRANSITION bit is already set, it will fail the recursion.
3191     + * This is needed because there's a lag between the changing of
3192     + * interrupt context and updating the preempt count. In this case,
3193     + * a false positive will be found. To handle this, one extra recursion
3194     + * is allowed, and this is done by the TRANSITION bit. If the TRANSITION
3195     + * bit is already set, then it is considered a recursion and the function
3196     + * ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
3197     + *
3198     + * On the trace_recursive_unlock(), the TRANSITION bit will be the first
3199     + * to be cleared. Even if it wasn't the context that set it. That is,
3200     + * if an interrupt comes in while NORMAL bit is set and the ring buffer
3201     + * is called before preempt_count() is updated, since the check will
3202     + * be on the NORMAL bit, the TRANSITION bit will then be set. If an
3203     + * NMI then comes in, it will set the NMI bit, but when the NMI code
3204     + * does the trace_recursive_unlock() it will clear the TRANSTION bit
3205     + * and leave the NMI bit set. But this is fine, because the interrupt
3206     + * code that set the TRANSITION bit will then clear the NMI bit when it
3207     + * calls trace_recursive_unlock(). If another NMI comes in, it will
3208     + * set the TRANSITION bit and continue.
3209     + *
3210     + * Note: The TRANSITION bit only handles a single transition between context.
3211     */
3212    
3213     static __always_inline int
3214     @@ -2623,8 +2649,16 @@ trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
3215     } else
3216     bit = RB_CTX_NORMAL;
3217    
3218     - if (unlikely(val & (1 << bit)))
3219     - return 1;
3220     + if (unlikely(val & (1 << bit))) {
3221     + /*
3222     + * It is possible that this was called by transitioning
3223     + * between interrupt context, and preempt_count() has not
3224     + * been updated yet. In this case, use the TRANSITION bit.
3225     + */
3226     + bit = RB_CTX_TRANSITION;
3227     + if (val & (1 << bit))
3228     + return 1;
3229     + }
3230    
3231     val |= (1 << bit);
3232     cpu_buffer->current_context = val;
3233     diff --git a/lib/random32.c b/lib/random32.c
3234     index d5c3137d93f49..3c5b67b69cbaa 100644
3235     --- a/lib/random32.c
3236     +++ b/lib/random32.c
3237     @@ -39,16 +39,6 @@
3238     #include <linux/sched.h>
3239     #include <asm/unaligned.h>
3240    
3241     -#ifdef CONFIG_RANDOM32_SELFTEST
3242     -static void __init prandom_state_selftest(void);
3243     -#else
3244     -static inline void prandom_state_selftest(void)
3245     -{
3246     -}
3247     -#endif
3248     -
3249     -DEFINE_PER_CPU(struct rnd_state, net_rand_state) __latent_entropy;
3250     -
3251     /**
3252     * prandom_u32_state - seeded pseudo-random number generator.
3253     * @state: pointer to state structure holding seeded state.
3254     @@ -68,25 +58,6 @@ u32 prandom_u32_state(struct rnd_state *state)
3255     }
3256     EXPORT_SYMBOL(prandom_u32_state);
3257    
3258     -/**
3259     - * prandom_u32 - pseudo random number generator
3260     - *
3261     - * A 32 bit pseudo-random number is generated using a fast
3262     - * algorithm suitable for simulation. This algorithm is NOT
3263     - * considered safe for cryptographic use.
3264     - */
3265     -u32 prandom_u32(void)
3266     -{
3267     - struct rnd_state *state = &get_cpu_var(net_rand_state);
3268     - u32 res;
3269     -
3270     - res = prandom_u32_state(state);
3271     - put_cpu_var(net_rand_state);
3272     -
3273     - return res;
3274     -}
3275     -EXPORT_SYMBOL(prandom_u32);
3276     -
3277     /**
3278     * prandom_bytes_state - get the requested number of pseudo-random bytes
3279     *
3280     @@ -118,20 +89,6 @@ void prandom_bytes_state(struct rnd_state *state, void *buf, size_t bytes)
3281     }
3282     EXPORT_SYMBOL(prandom_bytes_state);
3283    
3284     -/**
3285     - * prandom_bytes - get the requested number of pseudo-random bytes
3286     - * @buf: where to copy the pseudo-random bytes to
3287     - * @bytes: the requested number of bytes
3288     - */
3289     -void prandom_bytes(void *buf, size_t bytes)
3290     -{
3291     - struct rnd_state *state = &get_cpu_var(net_rand_state);
3292     -
3293     - prandom_bytes_state(state, buf, bytes);
3294     - put_cpu_var(net_rand_state);
3295     -}
3296     -EXPORT_SYMBOL(prandom_bytes);
3297     -
3298     static void prandom_warmup(struct rnd_state *state)
3299     {
3300     /* Calling RNG ten times to satisfy recurrence condition */
3301     @@ -147,96 +104,6 @@ static void prandom_warmup(struct rnd_state *state)
3302     prandom_u32_state(state);
3303     }
3304    
3305     -static u32 __extract_hwseed(void)
3306     -{
3307     - unsigned int val = 0;
3308     -
3309     - (void)(arch_get_random_seed_int(&val) ||
3310     - arch_get_random_int(&val));
3311     -
3312     - return val;
3313     -}
3314     -
3315     -static void prandom_seed_early(struct rnd_state *state, u32 seed,
3316     - bool mix_with_hwseed)
3317     -{
3318     -#define LCG(x) ((x) * 69069U) /* super-duper LCG */
3319     -#define HWSEED() (mix_with_hwseed ? __extract_hwseed() : 0)
3320     - state->s1 = __seed(HWSEED() ^ LCG(seed), 2U);
3321     - state->s2 = __seed(HWSEED() ^ LCG(state->s1), 8U);
3322     - state->s3 = __seed(HWSEED() ^ LCG(state->s2), 16U);
3323     - state->s4 = __seed(HWSEED() ^ LCG(state->s3), 128U);
3324     -}
3325     -
3326     -/**
3327     - * prandom_seed - add entropy to pseudo random number generator
3328     - * @seed: seed value
3329     - *
3330     - * Add some additional seeding to the prandom pool.
3331     - */
3332     -void prandom_seed(u32 entropy)
3333     -{
3334     - int i;
3335     - /*
3336     - * No locking on the CPUs, but then somewhat random results are, well,
3337     - * expected.
3338     - */
3339     - for_each_possible_cpu(i) {
3340     - struct rnd_state *state = &per_cpu(net_rand_state, i);
3341     -
3342     - state->s1 = __seed(state->s1 ^ entropy, 2U);
3343     - prandom_warmup(state);
3344     - }
3345     -}
3346     -EXPORT_SYMBOL(prandom_seed);
3347     -
3348     -/*
3349     - * Generate some initially weak seeding values to allow
3350     - * to start the prandom_u32() engine.
3351     - */
3352     -static int __init prandom_init(void)
3353     -{
3354     - int i;
3355     -
3356     - prandom_state_selftest();
3357     -
3358     - for_each_possible_cpu(i) {
3359     - struct rnd_state *state = &per_cpu(net_rand_state, i);
3360     - u32 weak_seed = (i + jiffies) ^ random_get_entropy();
3361     -
3362     - prandom_seed_early(state, weak_seed, true);
3363     - prandom_warmup(state);
3364     - }
3365     -
3366     - return 0;
3367     -}
3368     -core_initcall(prandom_init);
3369     -
3370     -static void __prandom_timer(unsigned long dontcare);
3371     -
3372     -static DEFINE_TIMER(seed_timer, __prandom_timer, 0, 0);
3373     -
3374     -static void __prandom_timer(unsigned long dontcare)
3375     -{
3376     - u32 entropy;
3377     - unsigned long expires;
3378     -
3379     - get_random_bytes(&entropy, sizeof(entropy));
3380     - prandom_seed(entropy);
3381     -
3382     - /* reseed every ~60 seconds, in [40 .. 80) interval with slack */
3383     - expires = 40 + prandom_u32_max(40);
3384     - seed_timer.expires = jiffies + msecs_to_jiffies(expires * MSEC_PER_SEC);
3385     -
3386     - add_timer(&seed_timer);
3387     -}
3388     -
3389     -static void __init __prandom_start_seed_timer(void)
3390     -{
3391     - seed_timer.expires = jiffies + msecs_to_jiffies(40 * MSEC_PER_SEC);
3392     - add_timer(&seed_timer);
3393     -}
3394     -
3395     void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state)
3396     {
3397     int i;
3398     @@ -256,51 +123,6 @@ void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state)
3399     }
3400     EXPORT_SYMBOL(prandom_seed_full_state);
3401    
3402     -/*
3403     - * Generate better values after random number generator
3404     - * is fully initialized.
3405     - */
3406     -static void __prandom_reseed(bool late)
3407     -{
3408     - unsigned long flags;
3409     - static bool latch = false;
3410     - static DEFINE_SPINLOCK(lock);
3411     -
3412     - /* Asking for random bytes might result in bytes getting
3413     - * moved into the nonblocking pool and thus marking it
3414     - * as initialized. In this case we would double back into
3415     - * this function and attempt to do a late reseed.
3416     - * Ignore the pointless attempt to reseed again if we're
3417     - * already waiting for bytes when the nonblocking pool
3418     - * got initialized.
3419     - */
3420     -
3421     - /* only allow initial seeding (late == false) once */
3422     - if (!spin_trylock_irqsave(&lock, flags))
3423     - return;
3424     -
3425     - if (latch && !late)
3426     - goto out;
3427     -
3428     - latch = true;
3429     - prandom_seed_full_state(&net_rand_state);
3430     -out:
3431     - spin_unlock_irqrestore(&lock, flags);
3432     -}
3433     -
3434     -void prandom_reseed_late(void)
3435     -{
3436     - __prandom_reseed(true);
3437     -}
3438     -
3439     -static int __init prandom_reseed(void)
3440     -{
3441     - __prandom_reseed(false);
3442     - __prandom_start_seed_timer();
3443     - return 0;
3444     -}
3445     -late_initcall(prandom_reseed);
3446     -
3447     #ifdef CONFIG_RANDOM32_SELFTEST
3448     static struct prandom_test1 {
3449     u32 seed;
3450     @@ -420,7 +242,28 @@ static struct prandom_test2 {
3451     { 407983964U, 921U, 728767059U },
3452     };
3453    
3454     -static void __init prandom_state_selftest(void)
3455     +static u32 __extract_hwseed(void)
3456     +{
3457     + unsigned int val = 0;
3458     +
3459     + (void)(arch_get_random_seed_int(&val) ||
3460     + arch_get_random_int(&val));
3461     +
3462     + return val;
3463     +}
3464     +
3465     +static void prandom_seed_early(struct rnd_state *state, u32 seed,
3466     + bool mix_with_hwseed)
3467     +{
3468     +#define LCG(x) ((x) * 69069U) /* super-duper LCG */
3469     +#define HWSEED() (mix_with_hwseed ? __extract_hwseed() : 0)
3470     + state->s1 = __seed(HWSEED() ^ LCG(seed), 2U);
3471     + state->s2 = __seed(HWSEED() ^ LCG(state->s1), 8U);
3472     + state->s3 = __seed(HWSEED() ^ LCG(state->s2), 16U);
3473     + state->s4 = __seed(HWSEED() ^ LCG(state->s3), 128U);
3474     +}
3475     +
3476     +static int __init prandom_state_selftest(void)
3477     {
3478     int i, j, errors = 0, runs = 0;
3479     bool error = false;
3480     @@ -460,5 +303,266 @@ static void __init prandom_state_selftest(void)
3481     pr_warn("prandom: %d/%d self tests failed\n", errors, runs);
3482     else
3483     pr_info("prandom: %d self tests passed\n", runs);
3484     + return 0;
3485     }
3486     +core_initcall(prandom_state_selftest);
3487     #endif
3488     +
3489     +/*
3490     + * The prandom_u32() implementation is now completely separate from the
3491     + * prandom_state() functions, which are retained (for now) for compatibility.
3492     + *
3493     + * Because of (ab)use in the networking code for choosing random TCP/UDP port
3494     + * numbers, which open DoS possibilities if guessable, we want something
3495     + * stronger than a standard PRNG. But the performance requirements of
3496     + * the network code do not allow robust crypto for this application.
3497     + *
3498     + * So this is a homebrew Junior Spaceman implementation, based on the
3499     + * lowest-latency trustworthy crypto primitive available, SipHash.
3500     + * (The authors of SipHash have not been consulted about this abuse of
3501     + * their work.)
3502     + *
3503     + * Standard SipHash-2-4 uses 2n+4 rounds to hash n words of input to
3504     + * one word of output. This abbreviated version uses 2 rounds per word
3505     + * of output.
3506     + */
3507     +
3508     +struct siprand_state {
3509     + unsigned long v0;
3510     + unsigned long v1;
3511     + unsigned long v2;
3512     + unsigned long v3;
3513     +};
3514     +
3515     +static DEFINE_PER_CPU(struct siprand_state, net_rand_state) __latent_entropy;
3516     +
3517     +/*
3518     + * This is the core CPRNG function. As "pseudorandom", this is not used
3519     + * for truly valuable things, just intended to be a PITA to guess.
3520     + * For maximum speed, we do just two SipHash rounds per word. This is
3521     + * the same rate as 4 rounds per 64 bits that SipHash normally uses,
3522     + * so hopefully it's reasonably secure.
3523     + *
3524     + * There are two changes from the official SipHash finalization:
3525     + * - We omit some constants XORed with v2 in the SipHash spec as irrelevant;
3526     + * they are there only to make the output rounds distinct from the input
3527     + * rounds, and this application has no input rounds.
3528     + * - Rather than returning v0^v1^v2^v3, return v1+v3.
3529     + * If you look at the SipHash round, the last operation on v3 is
3530     + * "v3 ^= v0", so "v0 ^ v3" just undoes that, a waste of time.
3531     + * Likewise "v1 ^= v2". (The rotate of v2 makes a difference, but
3532     + * it still cancels out half of the bits in v2 for no benefit.)
3533     + * Second, since the last combining operation was xor, continue the
3534     + * pattern of alternating xor/add for a tiny bit of extra non-linearity.
3535     + */
3536     +static inline u32 siprand_u32(struct siprand_state *s)
3537     +{
3538     + unsigned long v0 = s->v0, v1 = s->v1, v2 = s->v2, v3 = s->v3;
3539     +
3540     + PRND_SIPROUND(v0, v1, v2, v3);
3541     + PRND_SIPROUND(v0, v1, v2, v3);
3542     + s->v0 = v0; s->v1 = v1; s->v2 = v2; s->v3 = v3;
3543     + return v1 + v3;
3544     +}
3545     +
3546     +
3547     +/**
3548     + * prandom_u32 - pseudo random number generator
3549     + *
3550     + * A 32 bit pseudo-random number is generated using a fast
3551     + * algorithm suitable for simulation. This algorithm is NOT
3552     + * considered safe for cryptographic use.
3553     + */
3554     +u32 prandom_u32(void)
3555     +{
3556     + struct siprand_state *state = get_cpu_ptr(&net_rand_state);
3557     + u32 res = siprand_u32(state);
3558     +
3559     + put_cpu_ptr(&net_rand_state);
3560     + return res;
3561     +}
3562     +EXPORT_SYMBOL(prandom_u32);
3563     +
3564     +/**
3565     + * prandom_bytes - get the requested number of pseudo-random bytes
3566     + * @buf: where to copy the pseudo-random bytes to
3567     + * @bytes: the requested number of bytes
3568     + */
3569     +void prandom_bytes(void *buf, size_t bytes)
3570     +{
3571     + struct siprand_state *state = get_cpu_ptr(&net_rand_state);
3572     + u8 *ptr = buf;
3573     +
3574     + while (bytes >= sizeof(u32)) {
3575     + put_unaligned(siprand_u32(state), (u32 *)ptr);
3576     + ptr += sizeof(u32);
3577     + bytes -= sizeof(u32);
3578     + }
3579     +
3580     + if (bytes > 0) {
3581     + u32 rem = siprand_u32(state);
3582     +
3583     + do {
3584     + *ptr++ = (u8)rem;
3585     + rem >>= BITS_PER_BYTE;
3586     + } while (--bytes > 0);
3587     + }
3588     + put_cpu_ptr(&net_rand_state);
3589     +}
3590     +EXPORT_SYMBOL(prandom_bytes);
3591     +
3592     +/**
3593     + * prandom_seed - add entropy to pseudo random number generator
3594     + * @entropy: entropy value
3595     + *
3596     + * Add some additional seed material to the prandom pool.
3597     + * The "entropy" is actually our IP address (the only caller is
3598     + * the network code), not for unpredictability, but to ensure that
3599     + * different machines are initialized differently.
3600     + */
3601     +void prandom_seed(u32 entropy)
3602     +{
3603     + int i;
3604     +
3605     + add_device_randomness(&entropy, sizeof(entropy));
3606     +
3607     + for_each_possible_cpu(i) {
3608     + struct siprand_state *state = per_cpu_ptr(&net_rand_state, i);
3609     + unsigned long v0 = state->v0, v1 = state->v1;
3610     + unsigned long v2 = state->v2, v3 = state->v3;
3611     +
3612     + do {
3613     + v3 ^= entropy;
3614     + PRND_SIPROUND(v0, v1, v2, v3);
3615     + PRND_SIPROUND(v0, v1, v2, v3);
3616     + v0 ^= entropy;
3617     + } while (unlikely(!v0 || !v1 || !v2 || !v3));
3618     +
3619     + WRITE_ONCE(state->v0, v0);
3620     + WRITE_ONCE(state->v1, v1);
3621     + WRITE_ONCE(state->v2, v2);
3622     + WRITE_ONCE(state->v3, v3);
3623     + }
3624     +}
3625     +EXPORT_SYMBOL(prandom_seed);
3626     +
3627     +/*
3628     + * Generate some initially weak seeding values to allow
3629     + * the prandom_u32() engine to be started.
3630     + */
3631     +static int __init prandom_init_early(void)
3632     +{
3633     + int i;
3634     + unsigned long v0, v1, v2, v3;
3635     +
3636     + if (!arch_get_random_long(&v0))
3637     + v0 = jiffies;
3638     + if (!arch_get_random_long(&v1))
3639     + v1 = random_get_entropy();
3640     + v2 = v0 ^ PRND_K0;
3641     + v3 = v1 ^ PRND_K1;
3642     +
3643     + for_each_possible_cpu(i) {
3644     + struct siprand_state *state;
3645     +
3646     + v3 ^= i;
3647     + PRND_SIPROUND(v0, v1, v2, v3);
3648     + PRND_SIPROUND(v0, v1, v2, v3);
3649     + v0 ^= i;
3650     +
3651     + state = per_cpu_ptr(&net_rand_state, i);
3652     + state->v0 = v0; state->v1 = v1;
3653     + state->v2 = v2; state->v3 = v3;
3654     + }
3655     +
3656     + return 0;
3657     +}
3658     +core_initcall(prandom_init_early);
3659     +
3660     +
3661     +/* Stronger reseeding when available, and periodically thereafter. */
3662     +static void prandom_reseed(unsigned long dontcare);
3663     +
3664     +static DEFINE_TIMER(seed_timer, prandom_reseed, 0, 0);
3665     +
3666     +static void prandom_reseed(unsigned long dontcare)
3667     +{
3668     + unsigned long expires;
3669     + int i;
3670     +
3671     + /*
3672     + * Reinitialize each CPU's PRNG with 128 bits of key.
3673     + * No locking on the CPUs, but then somewhat random results are,
3674     + * well, expected.
3675     + */
3676     + for_each_possible_cpu(i) {
3677     + struct siprand_state *state;
3678     + unsigned long v0 = get_random_long(), v2 = v0 ^ PRND_K0;
3679     + unsigned long v1 = get_random_long(), v3 = v1 ^ PRND_K1;
3680     +#if BITS_PER_LONG == 32
3681     + int j;
3682     +
3683     + /*
3684     + * On 32-bit machines, hash in two extra words to
3685     + * approximate 128-bit key length. Not that the hash
3686     + * has that much security, but this prevents a trivial
3687     + * 64-bit brute force.
3688     + */
3689     + for (j = 0; j < 2; j++) {
3690     + unsigned long m = get_random_long();
3691     +
3692     + v3 ^= m;
3693     + PRND_SIPROUND(v0, v1, v2, v3);
3694     + PRND_SIPROUND(v0, v1, v2, v3);
3695     + v0 ^= m;
3696     + }
3697     +#endif
3698     + /*
3699     + * Probably impossible in practice, but there is a
3700     + * theoretical risk that a race between this reseeding
3701     + * and the target CPU writing its state back could
3702     + * create the all-zero SipHash fixed point.
3703     + *
3704     + * To ensure that never happens, ensure the state
3705     + * we write contains no zero words.
3706     + */
3707     + state = per_cpu_ptr(&net_rand_state, i);
3708     + WRITE_ONCE(state->v0, v0 ? v0 : -1ul);
3709     + WRITE_ONCE(state->v1, v1 ? v1 : -1ul);
3710     + WRITE_ONCE(state->v2, v2 ? v2 : -1ul);
3711     + WRITE_ONCE(state->v3, v3 ? v3 : -1ul);
3712     + }
3713     +
3714     + /* reseed every ~60 seconds, in [40 .. 80) interval with slack */
3715     + expires = round_jiffies(jiffies + 40 * HZ + prandom_u32_max(40 * HZ));
3716     + mod_timer(&seed_timer, expires);
3717     +}
3718     +
3719     +/*
3720     + * The random ready callback can be called from almost any interrupt.
3721     + * To avoid worrying about whether it's safe to delay that interrupt
3722     + * long enough to seed all CPUs, just schedule an immediate timer event.
3723     + */
3724     +static void prandom_timer_start(struct random_ready_callback *unused)
3725     +{
3726     + mod_timer(&seed_timer, jiffies);
3727     +}
3728     +
3729     +/*
3730     + * Start periodic full reseeding as soon as strong
3731     + * random numbers are available.
3732     + */
3733     +static int __init prandom_init_late(void)
3734     +{
3735     + static struct random_ready_callback random_ready = {
3736     + .func = prandom_timer_start
3737     + };
3738     + int ret = add_random_ready_callback(&random_ready);
3739     +
3740     + if (ret == -EALREADY) {
3741     + prandom_timer_start(&random_ready);
3742     + ret = 0;
3743     + }
3744     + return ret;
3745     +}
3746     +late_initcall(prandom_init_late);
3747     diff --git a/lib/swiotlb.c b/lib/swiotlb.c
3748     index 7ff9dc36c2f80..74b5b88621989 100644
3749     --- a/lib/swiotlb.c
3750     +++ b/lib/swiotlb.c
3751     @@ -199,6 +199,7 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
3752     io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
3753     }
3754     io_tlb_index = 0;
3755     + no_iotlb_memory = false;
3756    
3757     if (verbose)
3758     swiotlb_print_info();
3759     @@ -229,9 +230,11 @@ swiotlb_init(int verbose)
3760     if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose))
3761     return;
3762    
3763     - if (io_tlb_start)
3764     + if (io_tlb_start) {
3765     memblock_free_early(io_tlb_start,
3766     PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
3767     + io_tlb_start = 0;
3768     + }
3769     pr_warn("Cannot allocate buffer");
3770     no_iotlb_memory = true;
3771     }
3772     @@ -330,6 +333,7 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
3773     io_tlb_orig_addr[i] = INVALID_PHYS_ADDR;
3774     }
3775     io_tlb_index = 0;
3776     + no_iotlb_memory = false;
3777    
3778     swiotlb_print_info();
3779    
3780     diff --git a/mm/mempolicy.c b/mm/mempolicy.c
3781     index a2be65bf5d8cc..2f443767fd1b4 100644
3782     --- a/mm/mempolicy.c
3783     +++ b/mm/mempolicy.c
3784     @@ -487,7 +487,7 @@ static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr,
3785     struct queue_pages *qp = walk->private;
3786     unsigned long flags = qp->flags;
3787     int nid, ret;
3788     - pte_t *pte;
3789     + pte_t *pte, *mapped_pte;
3790     spinlock_t *ptl;
3791    
3792     if (pmd_trans_huge(*pmd)) {
3793     @@ -515,7 +515,7 @@ static int queue_pages_pte_range(pmd_t *pmd, unsigned long addr,
3794     if (pmd_trans_unstable(pmd))
3795     return 0;
3796     retry:
3797     - pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
3798     + mapped_pte = pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
3799     for (; addr != end; pte++, addr += PAGE_SIZE) {
3800     if (!pte_present(*pte))
3801     continue;
3802     @@ -554,7 +554,7 @@ retry:
3803     } else
3804     break;
3805     }
3806     - pte_unmap_unlock(pte - 1, ptl);
3807     + pte_unmap_unlock(mapped_pte, ptl);
3808     cond_resched();
3809     return addr != end ? -EIO : 0;
3810     }
3811     diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c
3812     index b596c413d297c..bcf743432c8de 100644
3813     --- a/net/ipv4/syncookies.c
3814     +++ b/net/ipv4/syncookies.c
3815     @@ -304,7 +304,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
3816     __u32 cookie = ntohl(th->ack_seq) - 1;
3817     struct sock *ret = sk;
3818     struct request_sock *req;
3819     - int mss;
3820     + int full_space, mss;
3821     struct rtable *rt;
3822     __u8 rcv_wscale;
3823     struct flowi4 fl4;
3824     @@ -388,8 +388,13 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
3825    
3826     /* Try to redo what tcp_v4_send_synack did. */
3827     req->rsk_window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
3828     + /* limit the window selection if the user enforce a smaller rx buffer */
3829     + full_space = tcp_full_space(sk);
3830     + if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
3831     + (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
3832     + req->rsk_window_clamp = full_space;
3833    
3834     - tcp_select_initial_window(tcp_full_space(sk), req->mss,
3835     + tcp_select_initial_window(full_space, req->mss,
3836     &req->rsk_rcv_wnd, &req->rsk_window_clamp,
3837     ireq->wscale_ok, &rcv_wscale,
3838     dst_metric(&rt->dst, RTAX_INITRWND));
3839     diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c
3840     index 16eba7b5f1a9d..df705303992e0 100644
3841     --- a/net/ipv6/sit.c
3842     +++ b/net/ipv6/sit.c
3843     @@ -1072,7 +1072,6 @@ static void ipip6_tunnel_bind_dev(struct net_device *dev)
3844     if (tdev && !netif_is_l3_master(tdev)) {
3845     int t_hlen = tunnel->hlen + sizeof(struct iphdr);
3846    
3847     - dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
3848     dev->mtu = tdev->mtu - t_hlen;
3849     if (dev->mtu < IPV6_MIN_MTU)
3850     dev->mtu = IPV6_MIN_MTU;
3851     @@ -1372,7 +1371,6 @@ static void ipip6_tunnel_setup(struct net_device *dev)
3852     dev->destructor = ipip6_dev_free;
3853    
3854     dev->type = ARPHRD_SIT;
3855     - dev->hard_header_len = LL_MAX_HEADER + t_hlen;
3856     dev->mtu = ETH_DATA_LEN - t_hlen;
3857     dev->flags = IFF_NOARP;
3858     netif_keep_dst(dev);
3859     diff --git a/net/ipv6/syncookies.c b/net/ipv6/syncookies.c
3860     index 4834015b27f49..0e368c1dd78b8 100644
3861     --- a/net/ipv6/syncookies.c
3862     +++ b/net/ipv6/syncookies.c
3863     @@ -143,7 +143,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
3864     __u32 cookie = ntohl(th->ack_seq) - 1;
3865     struct sock *ret = sk;
3866     struct request_sock *req;
3867     - int mss;
3868     + int full_space, mss;
3869     struct dst_entry *dst;
3870     __u8 rcv_wscale;
3871    
3872     @@ -236,7 +236,13 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
3873     }
3874    
3875     req->rsk_window_clamp = tp->window_clamp ? :dst_metric(dst, RTAX_WINDOW);
3876     - tcp_select_initial_window(tcp_full_space(sk), req->mss,
3877     + /* limit the window selection if the user enforce a smaller rx buffer */
3878     + full_space = tcp_full_space(sk);
3879     + if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
3880     + (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
3881     + req->rsk_window_clamp = full_space;
3882     +
3883     + tcp_select_initial_window(full_space, req->mss,
3884     &req->rsk_rcv_wnd, &req->rsk_window_clamp,
3885     ireq->wscale_ok, &rcv_wscale,
3886     dst_metric(dst, RTAX_INITRWND));
3887     diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
3888     index 02e10deef5b45..ba81655a584bd 100644
3889     --- a/net/iucv/af_iucv.c
3890     +++ b/net/iucv/af_iucv.c
3891     @@ -1542,7 +1542,8 @@ static int iucv_sock_shutdown(struct socket *sock, int how)
3892     break;
3893     }
3894    
3895     - if (how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) {
3896     + if ((how == SEND_SHUTDOWN || how == SHUTDOWN_MASK) &&
3897     + sk->sk_state == IUCV_CONNECTED) {
3898     if (iucv->transport == AF_IUCV_TRANS_IUCV) {
3899     txmsg.class = 0;
3900     txmsg.tag = 0;
3901     diff --git a/net/mac80211/tx.c b/net/mac80211/tx.c
3902     index 6216279efc468..eebbddccb47b7 100644
3903     --- a/net/mac80211/tx.c
3904     +++ b/net/mac80211/tx.c
3905     @@ -1847,19 +1847,24 @@ static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
3906    
3907     /* device xmit handlers */
3908    
3909     +enum ieee80211_encrypt {
3910     + ENCRYPT_NO,
3911     + ENCRYPT_MGMT,
3912     + ENCRYPT_DATA,
3913     +};
3914     +
3915     static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
3916     struct sk_buff *skb,
3917     - int head_need, bool may_encrypt)
3918     + int head_need,
3919     + enum ieee80211_encrypt encrypt)
3920     {
3921     struct ieee80211_local *local = sdata->local;
3922     - struct ieee80211_hdr *hdr;
3923     bool enc_tailroom;
3924     int tail_need = 0;
3925    
3926     - hdr = (struct ieee80211_hdr *) skb->data;
3927     - enc_tailroom = may_encrypt &&
3928     - (sdata->crypto_tx_tailroom_needed_cnt ||
3929     - ieee80211_is_mgmt(hdr->frame_control));
3930     + enc_tailroom = encrypt == ENCRYPT_MGMT ||
3931     + (encrypt == ENCRYPT_DATA &&
3932     + sdata->crypto_tx_tailroom_needed_cnt);
3933    
3934     if (enc_tailroom) {
3935     tail_need = IEEE80211_ENCRYPT_TAILROOM;
3936     @@ -1892,21 +1897,27 @@ void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
3937     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3938     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
3939     int headroom;
3940     - bool may_encrypt;
3941     + enum ieee80211_encrypt encrypt;
3942    
3943     - may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
3944     + if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
3945     + encrypt = ENCRYPT_NO;
3946     + else if (ieee80211_is_mgmt(hdr->frame_control))
3947     + encrypt = ENCRYPT_MGMT;
3948     + else
3949     + encrypt = ENCRYPT_DATA;
3950    
3951     headroom = local->tx_headroom;
3952     - if (may_encrypt)
3953     + if (encrypt != ENCRYPT_NO)
3954     headroom += sdata->encrypt_headroom;
3955     headroom -= skb_headroom(skb);
3956     headroom = max_t(int, 0, headroom);
3957    
3958     - if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
3959     + if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
3960     ieee80211_free_txskb(&local->hw, skb);
3961     return;
3962     }
3963    
3964     + /* reload after potential resize */
3965     hdr = (struct ieee80211_hdr *) skb->data;
3966     info->control.vif = &sdata->vif;
3967    
3968     @@ -2688,7 +2699,7 @@ static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
3969     head_need += sdata->encrypt_headroom;
3970     head_need += local->tx_headroom;
3971     head_need = max_t(int, 0, head_need);
3972     - if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
3973     + if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
3974     ieee80211_free_txskb(&local->hw, skb);
3975     skb = NULL;
3976     return ERR_PTR(-ENOMEM);
3977     @@ -3313,7 +3324,7 @@ static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3978     if (unlikely(ieee80211_skb_resize(sdata, skb,
3979     max_t(int, extra_head + hw_headroom -
3980     skb_headroom(skb), 0),
3981     - false))) {
3982     + ENCRYPT_NO))) {
3983     kfree_skb(skb);
3984     return true;
3985     }
3986     diff --git a/net/wireless/reg.c b/net/wireless/reg.c
3987     index a649763b854d5..04da31c52d092 100644
3988     --- a/net/wireless/reg.c
3989     +++ b/net/wireless/reg.c
3990     @@ -2759,7 +2759,7 @@ static void print_rd_rules(const struct ieee80211_regdomain *rd)
3991     power_rule = &reg_rule->power_rule;
3992    
3993     if (reg_rule->flags & NL80211_RRF_AUTO_BW)
3994     - snprintf(bw, sizeof(bw), "%d KHz, %d KHz AUTO",
3995     + snprintf(bw, sizeof(bw), "%d KHz, %u KHz AUTO",
3996     freq_range->max_bandwidth_khz,
3997     reg_get_max_bandwidth(rd, reg_rule));
3998     else
3999     diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
4000     index a8ca79810dcc6..3bec5c59169b5 100644
4001     --- a/net/x25/af_x25.c
4002     +++ b/net/x25/af_x25.c
4003     @@ -823,7 +823,7 @@ static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
4004     sock->state = SS_CONNECTED;
4005     rc = 0;
4006     out_put_neigh:
4007     - if (rc) {
4008     + if (rc && x25->neighbour) {
4009     read_lock_bh(&x25_list_lock);
4010     x25_neigh_put(x25->neighbour);
4011     x25->neighbour = NULL;
4012     diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
4013     index 0eb85765d35a1..4d19f2ff6e052 100644
4014     --- a/net/xfrm/xfrm_state.c
4015     +++ b/net/xfrm/xfrm_state.c
4016     @@ -1591,6 +1591,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
4017     int err = -ENOENT;
4018     __be32 minspi = htonl(low);
4019     __be32 maxspi = htonl(high);
4020     + __be32 newspi = 0;
4021     u32 mark = x->mark.v & x->mark.m;
4022    
4023     spin_lock_bh(&x->lock);
4024     @@ -1609,21 +1610,22 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
4025     xfrm_state_put(x0);
4026     goto unlock;
4027     }
4028     - x->id.spi = minspi;
4029     + newspi = minspi;
4030     } else {
4031     u32 spi = 0;
4032     for (h = 0; h < high-low+1; h++) {
4033     spi = low + prandom_u32()%(high-low+1);
4034     x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
4035     if (x0 == NULL) {
4036     - x->id.spi = htonl(spi);
4037     + newspi = htonl(spi);
4038     break;
4039     }
4040     xfrm_state_put(x0);
4041     }
4042     }
4043     - if (x->id.spi) {
4044     + if (newspi) {
4045     spin_lock_bh(&net->xfrm.xfrm_state_lock);
4046     + x->id.spi = newspi;
4047     h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
4048     hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
4049     spin_unlock_bh(&net->xfrm.xfrm_state_lock);
4050     diff --git a/sound/hda/ext/hdac_ext_controller.c b/sound/hda/ext/hdac_ext_controller.c
4051     index 261469188566c..49d42971d90da 100644
4052     --- a/sound/hda/ext/hdac_ext_controller.c
4053     +++ b/sound/hda/ext/hdac_ext_controller.c
4054     @@ -155,6 +155,8 @@ struct hdac_ext_link *snd_hdac_ext_bus_get_link(struct hdac_ext_bus *ebus,
4055     return NULL;
4056     if (ebus->idx != bus_idx)
4057     return NULL;
4058     + if (addr < 0 || addr > 31)
4059     + return NULL;
4060    
4061     list_for_each_entry(hlink, &ebus->hlink_list, list) {
4062     for (i = 0; i < HDA_MAX_CODECS; i++) {
4063     diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
4064     index 7e0573e55a356..89808ab008ad2 100644
4065     --- a/tools/perf/util/session.c
4066     +++ b/tools/perf/util/session.c
4067     @@ -482,6 +482,7 @@ static void perf_event__mmap2_swap(union perf_event *event,
4068     event->mmap2.maj = bswap_32(event->mmap2.maj);
4069     event->mmap2.min = bswap_32(event->mmap2.min);
4070     event->mmap2.ino = bswap_64(event->mmap2.ino);
4071     + event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation);
4072    
4073     if (sample_id_all) {
4074     void *data = &event->mmap2.filename;