From: John Garry <hidden> Date: 2021-06-21 16:46:24
Members of struct "llq" will be zero-inited, apart from member max_n_shift.
But we write llq.val straight after the init, so it was pointless to zero
init those other members. As such, separately init member max_n_shift
only.
In addition, struct "head" is initialised to "llq" only so that member
max_n_shift is set. But that member is never referenced for "head", so
remove any init there.
Removing these initializations is seen as a small performance optimisation,
as this code is (very) hot path.
Signed-off-by: John Garry <redacted>
@@ -727,11 +727,11 @@ static int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,unsignedlongflags;boolowner;structarm_smmu_cmdq*cmdq=&smmu->cmdq;-structarm_smmu_ll_queuellq={-.max_n_shift=cmdq->q.llq.max_n_shift,-},head=llq;+structarm_smmu_ll_queuellq,head;intret=0;+llq.max_n_shift=cmdq->q.llq.max_n_shift;+/* 1. Allocate some space in the queue */local_irq_save(flags);llq.val=READ_ONCE(cmdq->q.llq.val);
--
2.26.2
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: John Garry <hidden> Date: 2021-08-05 10:22:53
On 21/06/2021 17:36, John Garry wrote:
Members of struct "llq" will be zero-inited, apart from member max_n_shift.
But we write llq.val straight after the init, so it was pointless to zero
init those other members. As such, separately init member max_n_shift
only.
In addition, struct "head" is initialised to "llq" only so that member
max_n_shift is set. But that member is never referenced for "head", so
remove any init there.
Removing these initializations is seen as a small performance optimisation,
as this code is (very) hot path.
Hi Will,
Any chance you can pick up this small optimisation?
Cheers
@@ -727,11 +727,11 @@ static int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,unsignedlongflags;boolowner;structarm_smmu_cmdq*cmdq=&smmu->cmdq;-structarm_smmu_ll_queuellq={-.max_n_shift=cmdq->q.llq.max_n_shift,-},head=llq;+structarm_smmu_ll_queuellq,head;intret=0;+llq.max_n_shift=cmdq->q.llq.max_n_shift;+/* 1. Allocate some space in the queue */local_irq_save(flags);llq.val=READ_ONCE(cmdq->q.llq.val);
From: Will Deacon <will@kernel.org> Date: 2021-08-05 11:21:21
On Thu, Aug 05, 2021 at 11:22:15AM +0100, John Garry wrote:
On 21/06/2021 17:36, John Garry wrote:
quoted
Members of struct "llq" will be zero-inited, apart from member max_n_shift.
But we write llq.val straight after the init, so it was pointless to zero
init those other members. As such, separately init member max_n_shift
only.
In addition, struct "head" is initialised to "llq" only so that member
max_n_shift is set. But that member is never referenced for "head", so
remove any init there.
Removing these initializations is seen as a small performance optimisation,
as this code is (very) hot path.
Hi Will,
Any chance you can pick up this small optimisation?
Yup! I've actually queued it locally, but I may end up asking Joerg to take
it directly depending on what else I queue for 5.15. So far, most of the
SMMU stuff is all part of wider refactorings.
Cheers,
Will
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Robin Murphy <robin.murphy@arm.com> Date: 2021-08-05 11:24:42
On 2021-06-21 17:36, John Garry wrote:
Members of struct "llq" will be zero-inited, apart from member max_n_shift.
But we write llq.val straight after the init, so it was pointless to zero
init those other members. As such, separately init member max_n_shift
only.
In addition, struct "head" is initialised to "llq" only so that member
max_n_shift is set. But that member is never referenced for "head", so
remove any init there.
Removing these initializations is seen as a small performance optimisation,
as this code is (very) hot path.
I looked at this and immediately thought "surely the compiler can see
that all the prod/cons/val fields are written anyway and elide the
initialisation?", so I dumped the before and after disassembly, and... oh.
You should probably clarify that it's zero-initialising all the
cacheline padding which is both pointless and painful. With that,
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
However, having looked this closely I'm now tangentially wondering why
max_n_shift isn't inside the padded union? It's read at the same time as
both prod and cons by queue_has_space(), and never updated, so there
doesn't appear to be any benefit to it being in a separate cacheline all
by itself, and llq is already twice as big as it needs to be. Sorting
that would also be a good opportunity to store the value of interest in
its appropriate form so we're not needlessly recalculating 1 << shift
every flippin' time...
Robin.
@@ -727,11 +727,11 @@ static int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,unsignedlongflags;boolowner;structarm_smmu_cmdq*cmdq=&smmu->cmdq;-structarm_smmu_ll_queuellq={-.max_n_shift=cmdq->q.llq.max_n_shift,-},head=llq;+structarm_smmu_ll_queuellq,head;intret=0;+llq.max_n_shift=cmdq->q.llq.max_n_shift;+/* 1. Allocate some space in the queue */local_irq_save(flags);llq.val=READ_ONCE(cmdq->q.llq.val);
From: Robin Murphy <robin.murphy@arm.com> Date: 2021-08-05 12:18:47
On 2021-08-05 12:24, Robin Murphy wrote:
On 2021-06-21 17:36, John Garry wrote:
quoted
Members of struct "llq" will be zero-inited, apart from member
max_n_shift.
But we write llq.val straight after the init, so it was pointless to zero
init those other members. As such, separately init member max_n_shift
only.
In addition, struct "head" is initialised to "llq" only so that member
max_n_shift is set. But that member is never referenced for "head", so
remove any init there.
Removing these initializations is seen as a small performance
optimisation,
as this code is (very) hot path.
I looked at this and immediately thought "surely the compiler can see
that all the prod/cons/val fields are written anyway and elide the
initialisation?", so I dumped the before and after disassembly, and... oh.
You should probably clarify that it's zero-initialising all the
cacheline padding which is both pointless and painful. With that,
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
However, having looked this closely I'm now tangentially wondering why
max_n_shift isn't inside the padded union? It's read at the same time as
both prod and cons by queue_has_space(), and never updated, so there
doesn't appear to be any benefit to it being in a separate cacheline all
by itself, and llq is already twice as big as it needs to be. Sorting
that would also be a good opportunity to store the value of interest in
its appropriate form so we're not needlessly recalculating 1 << shift
every flippin' time...
...on which note, how about something like this on top?
(untested since I don't have any SMMUv3 hardware to hand)
Robin.
----->8-----
Subject: [PATCH] iommu/arm-smmu-v3: Improve arm_smmu_ll_queue efficiency
Once initialised, max_n_shift is only ever read at the same time as
accessing prod or cons, thus should not have any impact on contention
to justify keeping it in its own separate cacheline. Move it inside the
padding union to halve the size of struct arm_smmu_ll_queue. Even then,
though, there are a couple more spots in the command issuing path where
we could do without the overhead of zeroing even one cache line worth of
padding, so avoid implicit initialisation of those temporary structures
as was done at the top level in arm_smmu_cmdq_issue_cmdlist().
Furthermore, the shift value is only directly relevant for initially
setting up the relevant queue base register; all we care about after
that is the number of entries, so store that value instead once a
queue is initialised and avoid needlessly recalculating it everywhere.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 35 +++++++++++----------
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 29 ++++++++++-------
2 files changed, 37 insertions(+), 27 deletions(-)
arm_smmu_cmdq *cmdq, u64 *cmds,
u32 prod, int n)
{
int i;
- struct arm_smmu_ll_queue llq = {
- .max_n_shift = cmdq->q.llq.max_n_shift,
- .prod = prod,
- };
+ struct arm_smmu_ll_queue llq;
+
+ /* Avoid zero-initialising all the padding */;
+ llq.nents = cmdq->q.llq.nents;
+ llq.prod = prod;
for (i = 0; i < n; ++i) {
u64 *cmd = &cmds[i * CMDQ_ENT_DWORDS];
@@ -736,7 +738,7 @@ static int arm_smmu_cmdq_issue_cmdlist(struct
arm_smmu_device *smmu,
struct arm_smmu_ll_queue llq, head;
int ret = 0;
- llq.max_n_shift = cmdq->q.llq.max_n_shift;
+ llq.nents = cmdq->q.llq.nents;
/* 1. Allocate some space in the queue */
local_irq_save(flags);
@@ -2845,16 +2847,18 @@ static int arm_smmu_init_one_queue(struct
From: John Garry <hidden> Date: 2021-08-05 13:40:41
On 05/08/2021 12:24, Robin Murphy wrote:
On 2021-06-21 17:36, John Garry wrote:
quoted
Members of struct "llq" will be zero-inited, apart from member
max_n_shift.
But we write llq.val straight after the init, so it was pointless to zero
init those other members. As such, separately init member max_n_shift
only.
In addition, struct "head" is initialised to "llq" only so that member
max_n_shift is set. But that member is never referenced for "head", so
remove any init there.
Removing these initializations is seen as a small performance
optimisation,
as this code is (very) hot path.
I looked at this and immediately thought "surely the compiler can see
that all the prod/cons/val fields are written anyway and elide the
initialisation?", so I dumped the before and after disassembly, and... oh.
You should probably clarify that it's zero-initialising all the
cacheline padding which is both pointless and painful. With that,
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
However, having looked this closely I'm now tangentially wondering why
max_n_shift isn't inside the padded union? It's read at the same time as
both prod and cons by queue_has_space(), and never updated, so there
doesn't appear to be any benefit to it being in a separate cacheline all
by itself, and llq is already twice as big as it needs to be.
I think that the problem is if the prod+cons 64b value and the shift are
on the same cacheline, then we have a chance of accessing a stale
cacheline twice:
static int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
u64 *cmds, int n, bool sync)
{
u64 cmd_sync[CMDQ_ENT_DWORDS];
u32 prod;
unsigned long flags;
bool owner;
struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
struct arm_smmu_ll_queue llq = {
.max_n_shift = cmdq->q.llq.max_n_shift, // here
}, head = llq;
int ret = 0;
/* 1. Allocate some space in the queue */
local_irq_save(flags);
llq.val = READ_ONCE(cmdq->q.llq.val); // and again here
since cmdq->q.llq is per-SMMU. If max_n_shift is on a separate
cacheline, then it should never be stale.
I suppose they could be combined into a smaller sub-struct and loaded in
a single operation, but it looks messy, and prob without much gain.
Thanks,
John
Sorting
that would also be a good opportunity to store the value of interest in
its appropriate form so we're not needlessly recalculating 1 << shift
every flippin' time...
Robin.
From: Robin Murphy <robin.murphy@arm.com> Date: 2021-08-05 14:42:08
On 2021-08-05 14:40, John Garry wrote:
On 05/08/2021 12:24, Robin Murphy wrote:
quoted
On 2021-06-21 17:36, John Garry wrote:
quoted
Members of struct "llq" will be zero-inited, apart from member
max_n_shift.
But we write llq.val straight after the init, so it was pointless to
zero
init those other members. As such, separately init member max_n_shift
only.
In addition, struct "head" is initialised to "llq" only so that member
max_n_shift is set. But that member is never referenced for "head", so
remove any init there.
Removing these initializations is seen as a small performance
optimisation,
as this code is (very) hot path.
I looked at this and immediately thought "surely the compiler can see
that all the prod/cons/val fields are written anyway and elide the
initialisation?", so I dumped the before and after disassembly, and...
oh.
You should probably clarify that it's zero-initialising all the
cacheline padding which is both pointless and painful. With that,
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
However, having looked this closely I'm now tangentially wondering why
max_n_shift isn't inside the padded union? It's read at the same time
as both prod and cons by queue_has_space(), and never updated, so
there doesn't appear to be any benefit to it being in a separate
cacheline all by itself, and llq is already twice as big as it needs
to be.
I think that the problem is if the prod+cons 64b value and the shift are
on the same cacheline, then we have a chance of accessing a stale
cacheline twice:
static int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
u64 *cmds, int n, bool sync)
{
u64 cmd_sync[CMDQ_ENT_DWORDS];
u32 prod;
unsigned long flags;
bool owner;
struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
struct arm_smmu_ll_queue llq = {
.max_n_shift = cmdq->q.llq.max_n_shift, // here
}, head = llq;
int ret = 0;
/* 1. Allocate some space in the queue */
local_irq_save(flags);
llq.val = READ_ONCE(cmdq->q.llq.val); // and again here
since cmdq->q.llq is per-SMMU. If max_n_shift is on a separate
cacheline, then it should never be stale.
Ah, right, even though the accesses are always going to be close
together, I suppose it could still technically cause some false sharing
if someone else is trying to update prod at exactly the right time. I
guess that might be why we need the explicit padding there in the first
place, it's just a shame that it ends up wasting even more space with
implicit padding at the end too (and I have a vague memory that trying
to force member alignment and structure packing at the same time doesn't
work well). Oh well.
I suppose they could be combined into a smaller sub-struct and loaded in
a single operation, but it looks messy, and prob without much gain.
Indeed I wouldn't say that saving memory is the primary concern here,
and any more convoluted code is hardly going to help performance. Plus
it still wouldn't help the other cases where we're just copying the size
into a fake queue to do some prod arithmetic - I hadn't fully clocked
what was going on there when I skimmed through things earlier.
Disregarding the bogus layout change, though, do you reckon the rest of
my idea makes sense?
Cheers,
Robin.
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: John Garry <hidden> Date: 2021-08-05 15:16:37
On 05/08/2021 15:41, Robin Murphy wrote:
quoted
I suppose they could be combined into a smaller sub-struct and loaded
in a single operation, but it looks messy, and prob without much gain.
Indeed I wouldn't say that saving memory is the primary concern here,
and any more convoluted code is hardly going to help performance. Plus
it still wouldn't help the other cases where we're just copying the size
into a fake queue to do some prod arithmetic - I hadn't fully clocked
what was going on there when I skimmed through things earlier.
Disregarding the bogus layout change, though, do you reckon the rest of
my idea makes sense?
I tried the similar change to avoid zero-init the padding in
arm_smmu_cmdq_write_entries() and the
_arm_smmu_cmdq_poll_set_valid_map(), but the disassembly was the same.
So the compiler must have got smart there.
But for the original change in this patch, it did make a difference.
It's nice to remove what was a memcpy:
1770: a9077eff stp xzr, xzr, [x23, #112]
}, head = llq;
1774: 94000000 bl 0 <memcpy>
And performance was very fractionally better.
As for pre-evaluating "nents", I'm not sure how much that can help, but
I am not too optimistic. I can try some testing when I get a chance.
Having said that, I would need to check the disassembly also.
Thanks,
John
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
From: Robin Murphy <robin.murphy@arm.com> Date: 2021-08-05 17:15:01
On 2021-08-05 16:16, John Garry wrote:
On 05/08/2021 15:41, Robin Murphy wrote:
quoted
quoted
I suppose they could be combined into a smaller sub-struct and loaded
in a single operation, but it looks messy, and prob without much gain.
Indeed I wouldn't say that saving memory is the primary concern here,
and any more convoluted code is hardly going to help performance. Plus
it still wouldn't help the other cases where we're just copying the
size into a fake queue to do some prod arithmetic - I hadn't fully
clocked what was going on there when I skimmed through things earlier.
Disregarding the bogus layout change, though, do you reckon the rest
of my idea makes sense?
I tried the similar change to avoid zero-init the padding in
arm_smmu_cmdq_write_entries() and the
_arm_smmu_cmdq_poll_set_valid_map(), but the disassembly was the same.
So the compiler must have got smart there.
Yeah, in my build __arm_smmu_cmdq_poll_set_valid_map() only uses 32
bytes of stack, so clearly it's managed to see through the macro magic
once queue_inc_prod_n() is inlined and elide the whole struct.
arm_smmu_cmdq_write_entries() is inlined already, but logically must be
the same deal since it's a similarly inlined queue_inc_prod_n().
However, that may all change if different compiler flags or a different
compiler lead to different inlining decisions, so I'd argue that if this
can matter anywhere then it's worth treating consistently everywhere.
But for the original change in this patch, it did make a difference.
It's nice to remove what was a memcpy:
1770: a9077eff stp xzr, xzr, [x23, #112]
}, head = llq;
1774: 94000000 bl 0 <memcpy>
And performance was very fractionally better.
As for pre-evaluating "nents", I'm not sure how much that can help, but
I am not too optimistic. I can try some testing when I get a chance.
Having said that, I would need to check the disassembly also.
It'll just turn MOV,LDR,LSL sequences into plain LDRs - a small saving
but with no real downside, and a third of it is in the place where doing
less work matters most:
add/remove: 0/0 grow/shrink: 0/8 up/down: 0/-100 (-100)
Function old new delta
arm_smmu_priq_thread 532 528 -4
arm_smmu_evtq_thread 368 364 -4
arm_smmu_device_probe 4564 4556 -8
__arm_smmu_cmdq_poll_set_valid_map.isra 316 308 -8
arm_smmu_init_one_queue.isra 320 308 -12
queue_remove_raw 192 176 -16
arm_smmu_gerror_handler 752 736 -16
arm_smmu_cmdq_issue_cmdlist 1812 1780 -32
Total: Before=23776, After=23676, chg -0.42%
Robin.
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel