Re: [PATCH 26/94] Maple Tree: Add new data structure

From: Liam Howlett
Date: Fri May 14 2021 - 17:23:25 EST


* Peter Zijlstra <peterz@xxxxxxxxxxxxx> [210514 07:27]:
> On Fri, May 14, 2021 at 01:13:51PM +0200, Peter Zijlstra wrote:
> > On Wed, Apr 28, 2021 at 03:36:02PM +0000, Liam Howlett wrote:
>
> > > +static inline struct maple_node *mte_to_node(const struct maple_enode *entry)
> > > +{
> > > + return (struct maple_node *)((unsigned long)entry & ~127);
> > > +}
> >
> > > +static inline struct maple_topiary *mte_to_mat(const struct maple_enode *entry)
> > > +{
> > > + return (struct maple_topiary *)((unsigned long)entry & ~127);
> > > +}
> >
> > Can we please write masks as hex, also do they want a pretty name?
> >
> >
> > This has more magic mask values, proper names might be good:
> >
> > > +static inline void mte_set_parent(struct maple_enode *enode,
> > > + const struct maple_enode *parent,
> > > + unsigned char slot)
> > > +{
> > > + unsigned long bitmask = 0x78;
> > > + unsigned long val = (unsigned long) parent;
> > > + unsigned long type = 0;
> > > +
> > > + switch (mte_node_type(parent)) {
> > > + case maple_range_64:
> > > + case maple_arange_64:
> > > + type = 6;
> > > + break;
> > > + default:
> > > + break;
> > > + }
> > > +
> > > + val &= ~bitmask; // Remove any old slot number.
> > > + val |= (slot << MAPLE_PARENT_SHIFT); // Set the slot.
> > > + val |= type;
> > > + mte_to_node(enode)->parent = ma_parent_ptr(val);
> > > +}
> >
> > > +static inline unsigned int mte_parent_slot(const struct maple_enode *enode)
> > > +{
> > > + unsigned long bitmask = 0x7C;
> > > + unsigned long val = (unsigned long) mte_to_node(enode)->parent;
> > > +
> > > + if (val & 1)
> > > + return 0; // Root.
> > > +
> > > + return (val & bitmask) >> MAPLE_PARENT_SHIFT;
> >
> > 7c is 1111100, but then you're shifting out the one bit that makes the
> > difference from the above magic 0x78. What gives?
>
> IMO the more obvious way is something like:
>
> (val >> MAPLE_PARENT_SHIFT) & ((1 << MAPLE_SLOT_BITS)-1);
>
> And then we also see that 3+4 gives 7, which is that magical 127 above,
> are them the same? Related names would be good in that case.
>


This is a residual of having changed the mte_parent_slot() calculation
from supporting more node types which had different shifts.

Thank you for pointing this out. I will clean this up.